LoadCodecs.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // LoadCodecs.h
  2. #ifndef __LOADCODECS_H
  3. #define __LOADCODECS_H
  4. #include "../../../Common/Types.h"
  5. #include "../../../Common/MyCom.h"
  6. #include "../../../Common/MyString.h"
  7. #include "../../../Common/Buffer.h"
  8. #include "../../ICoder.h"
  9. #ifdef EXTERNAL_CODECS
  10. #include "../../../Windows/DLL.h"
  11. #endif
  12. struct CDllCodecInfo
  13. {
  14. CLSID Encoder;
  15. CLSID Decoder;
  16. bool EncoderIsAssigned;
  17. bool DecoderIsAssigned;
  18. int LibIndex;
  19. UInt32 CodecIndex;
  20. };
  21. #include "../../Archive/IArchive.h"
  22. typedef IInArchive * (*CreateInArchiveP)();
  23. typedef IOutArchive * (*CreateOutArchiveP)();
  24. struct CArcExtInfo
  25. {
  26. UString Ext;
  27. UString AddExt;
  28. CArcExtInfo() {}
  29. CArcExtInfo(const UString &ext): Ext(ext) {}
  30. CArcExtInfo(const UString &ext, const UString &addExt): Ext(ext), AddExt(addExt) {}
  31. };
  32. struct CArcInfoEx
  33. {
  34. #ifdef EXTERNAL_CODECS
  35. int LibIndex;
  36. UInt32 FormatIndex;
  37. CLSID ClassID;
  38. #endif
  39. bool UpdateEnabled;
  40. CreateInArchiveP CreateInArchive;
  41. CreateOutArchiveP CreateOutArchive;
  42. UString Name;
  43. CObjectVector<CArcExtInfo> Exts;
  44. #ifndef _SFX
  45. CByteBuffer StartSignature;
  46. // CByteBuffer FinishSignature;
  47. #ifdef NEW_FOLDER_INTERFACE
  48. UStringVector AssociateExts;
  49. #endif
  50. #endif
  51. bool KeepName;
  52. UString GetMainExt() const
  53. {
  54. if (Exts.IsEmpty())
  55. return UString();
  56. return Exts[0].Ext;
  57. }
  58. int FindExtension(const UString &ext) const
  59. {
  60. for (int i = 0; i < Exts.Size(); i++)
  61. if (ext.CompareNoCase(Exts[i].Ext) == 0)
  62. return i;
  63. return -1;
  64. }
  65. UString GetAllExtensions() const
  66. {
  67. UString s;
  68. for (int i = 0; i < Exts.Size(); i++)
  69. {
  70. if (i > 0)
  71. s += ' ';
  72. s += Exts[i].Ext;
  73. }
  74. return s;
  75. }
  76. void AddExts(const wchar_t* ext, const wchar_t* addExt);
  77. CArcInfoEx():
  78. #ifdef EXTERNAL_CODECS
  79. LibIndex(-1),
  80. #endif
  81. UpdateEnabled(false),
  82. CreateInArchive(0), CreateOutArchive(0),
  83. KeepName(false)
  84. #ifndef _SFX
  85. #endif
  86. {}
  87. };
  88. #ifdef EXTERNAL_CODECS
  89. typedef UInt32 (WINAPI *GetMethodPropertyFunc)(UInt32 index, PROPID propID, PROPVARIANT *value);
  90. typedef UInt32 (WINAPI *CreateObjectFunc)(const GUID *clsID, const GUID *interfaceID, void **outObject);
  91. struct CCodecLib
  92. {
  93. NWindows::NDLL::CLibrary Lib;
  94. GetMethodPropertyFunc GetMethodProperty;
  95. CreateObjectFunc CreateObject;
  96. #ifdef NEW_FOLDER_INTERFACE
  97. struct CIconPair
  98. {
  99. UString Ext;
  100. UInt32 IconIndex;
  101. };
  102. CSysString Path;
  103. CObjectVector<CIconPair> IconPairs;
  104. void LoadIcons();
  105. int FindIconIndex(const UString &ext) const;
  106. #endif
  107. CCodecLib(): GetMethodProperty(0) {}
  108. };
  109. #endif
  110. class CCodecs:
  111. #ifdef EXTERNAL_CODECS
  112. public ICompressCodecsInfo,
  113. #else
  114. public IUnknown,
  115. #endif
  116. public CMyUnknownImp
  117. {
  118. public:
  119. #ifdef EXTERNAL_CODECS
  120. CObjectVector<CCodecLib> Libs;
  121. CObjectVector<CDllCodecInfo> Codecs;
  122. HRESULT LoadCodecs();
  123. HRESULT LoadFormats();
  124. HRESULT LoadDll(const CSysString &path);
  125. HRESULT LoadDllsFromFolder(const CSysString &folderPrefix);
  126. HRESULT CreateArchiveHandler(const CArcInfoEx &ai, void **archive, bool outHandler) const
  127. {
  128. return Libs[ai.LibIndex].CreateObject(&ai.ClassID, outHandler ? &IID_IOutArchive : &IID_IInArchive, (void **)archive);
  129. }
  130. #endif
  131. public:
  132. CObjectVector<CArcInfoEx> Formats;
  133. HRESULT Load();
  134. int FindFormatForArchiveName(const UString &archivePath) const;
  135. int FindFormatForArchiveType(const UString &arcType) const;
  136. MY_UNKNOWN_IMP
  137. #ifdef EXTERNAL_CODECS
  138. STDMETHOD(GetNumberOfMethods)(UINT32 *numMethods);
  139. STDMETHOD(GetProperty)(UINT32 index, PROPID propID, PROPVARIANT *value);
  140. STDMETHOD(CreateDecoder)(UINT32 index, const GUID *interfaceID, void **coder);
  141. STDMETHOD(CreateEncoder)(UINT32 index, const GUID *interfaceID, void **coder);
  142. #endif
  143. int GetCodecLibIndex(UInt32 index);
  144. bool GetCodecEncoderIsAssigned(UInt32 index);
  145. HRESULT GetCodecId(UInt32 index, UInt64 &id);
  146. UString GetCodecName(UInt32 index);
  147. HRESULT CreateInArchive(int formatIndex, CMyComPtr<IInArchive> &archive) const
  148. {
  149. const CArcInfoEx &ai = Formats[formatIndex];
  150. #ifdef EXTERNAL_CODECS
  151. if (ai.LibIndex < 0)
  152. #endif
  153. {
  154. archive = ai.CreateInArchive();
  155. return S_OK;
  156. }
  157. #ifdef EXTERNAL_CODECS
  158. return CreateArchiveHandler(ai, (void **)&archive, false);
  159. #endif
  160. }
  161. HRESULT CreateOutArchive(int formatIndex, CMyComPtr<IOutArchive> &archive) const
  162. {
  163. const CArcInfoEx &ai = Formats[formatIndex];
  164. #ifdef EXTERNAL_CODECS
  165. if (ai.LibIndex < 0)
  166. #endif
  167. {
  168. archive = ai.CreateOutArchive();
  169. return S_OK;
  170. }
  171. #ifdef EXTERNAL_CODECS
  172. return CreateArchiveHandler(ai, (void **)&archive, true);
  173. #endif
  174. }
  175. int FindOutFormatFromName(const UString &name) const
  176. {
  177. for (int i = 0; i < Formats.Size(); i++)
  178. {
  179. const CArcInfoEx &arc = Formats[i];
  180. if (!arc.UpdateEnabled)
  181. continue;
  182. if (arc.Name.CompareNoCase(name) == 0)
  183. return i;
  184. }
  185. return -1;
  186. }
  187. #ifdef EXTERNAL_CODECS
  188. HRESULT CreateCoder(const UString &name, bool encode, CMyComPtr<ICompressCoder> &coder) const;
  189. #endif
  190. };
  191. #endif