Advertisement
Guest User

MPM

a guest
Jun 9th, 2014
1,718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.73 KB | None | 0 0
  1. #include <direct.h>
  2. #include <sys/stat.h>
  3. #include <io.h>
  4. #include <boost/shared_ptr.hpp>
  5. #include <boost/algorithm/string.hpp>
  6. #include <fstream>
  7.  
  8. #pragma warning(push, 3)
  9. #include <cryptopp/osrng.h>
  10. #pragma warning(pop)
  11.  
  12. //#include <YmirBase/YmirBaseLink.h>
  13. #include <cryptopp/cryptoppLibLink.h>
  14. //#include <Mantle/MA_LibLink.h>
  15. #include <lzo-2.03/lzoLibLink.h>
  16. #pragma comment(lib, "winmm.lib")
  17. //#pragma comment(lib, "d3d8.lib")
  18.  
  19. #ifdef _DEBUG
  20.     #pragma comment(lib, "reducio_d.lib")
  21. #else
  22.     #pragma comment(lib, "reducio.lib")
  23. #endif
  24.  
  25. #include <EterBase/Utils.h>
  26. #include <EterBase/Filename.h>
  27. #include <EterLib/TextFileLoader.h>
  28. #include <EterPack/EterPackManager.h>
  29. #include <Reducio/Reducio.h>
  30.  
  31. using std::string;
  32. using std::vector;
  33. using boost::unordered_map;
  34. using std::size_t;
  35.  
  36. typedef unordered_map<string, bool, stringhash> NameDict;
  37.  
  38. string  g_st_packName;
  39.  
  40. NameDict g_map_ignoreFileName;
  41. NameDict g_map_ignoreDirName;
  42. NameDict g_map_ignoreBasePath;
  43.  
  44. typedef unordered_map<string, BYTE, stringhash> TStrMap;
  45.  
  46. TStrMap g_PackTypeByExtNameMap;
  47. std::string g_strFolderName = "pack/";
  48.  
  49. static BYTE s_IV[32];
  50.  
  51. typedef unordered_map<string, string, stringhash> MapNameToSDBFile;
  52.  
  53. MapNameToSDBFile g_map_SDBFileList;
  54. int              iCompressTexQuality = 0;
  55.  
  56. bool IsSDBSupportRequired( const std::string& strFile, std::string& strMapName )
  57. {
  58.     MapNameToSDBFile::const_iterator cit = g_map_SDBFileList.find(strFile);
  59.  
  60.     if( cit != g_map_SDBFileList.end() )
  61.     {
  62.         strMapName = cit->second;
  63.         return true;
  64.     }
  65.    
  66.     return false;
  67. }
  68.  
  69. //#define __ADD_CSHYBRID_ENCRYPT__
  70.  
  71. bool LoadList(const char* fileName, vector<string>* pvec_stLine)
  72. {
  73.     FILE* fp;
  74.  
  75.     if (0 != fopen_s(&fp, fileName, "r"))
  76.         return false;
  77.  
  78.     while (1)
  79.     {
  80.         char line[256];
  81.         if (!fgets(line, sizeof(line)-1, fp))
  82.             break;
  83.  
  84.         size_t lineLen = strlen(line);
  85.  
  86.         if (line[lineLen-1] == '\n')
  87.             line[lineLen-1] = '\0';
  88.  
  89.         if (line[lineLen-2] == '\r')
  90.             line[lineLen-2] = '\0';
  91.  
  92.         pvec_stLine->push_back(line);      
  93.     }
  94.     fclose(fp);
  95.     return true;
  96. }
  97.  
  98. void IgnoreFileList_Append(const string& c_st_fileName)
  99. {
  100.     printf("ignore_file: [%s]\n", c_st_fileName.c_str());  
  101.    
  102.     if (c_st_fileName.find('*') >= 0) // strchr(c_st_fileName.c_str(), '*')
  103.     {
  104.         WIN32_FIND_DATA findData;
  105.         HANDLE hFind;
  106.         hFind = FindFirstFile(c_st_fileName.c_str(), &findData);       
  107.         if (hFind != INVALID_HANDLE_VALUE)
  108.         {
  109.             std::string st_childName;
  110.  
  111.             do
  112.             {
  113.                 st_childName = findData.cFileName;
  114.                 StringPath(st_childName);
  115.  
  116.                 g_map_ignoreFileName.insert(NameDict::value_type(st_childName, true));
  117.             }
  118.             while (FindNextFile(hFind, &findData));
  119.         }
  120.         FindClose(hFind);
  121.     }
  122.     else
  123.     {
  124.         string st_fileName = c_st_fileName;
  125.         StringPath(st_fileName);
  126.         g_map_ignoreFileName.insert(NameDict::value_type(st_fileName, true));
  127.     }
  128. }
  129.  
  130. bool IgnoreFileList_Load(const char* fileName)
  131. {
  132.     vector<string> nameList;
  133.     if (!LoadList(fileName, &nameList))
  134.         return false;
  135.  
  136.     for_each(nameList.begin(), nameList.end(), IgnoreFileList_Append); 
  137.     return true;
  138. }
  139.  
  140. void IgnoreDirList_Append(const string& c_st_dirName)
  141. {
  142.     std::string st_dirName = c_st_dirName;
  143.     StringPath(st_dirName);
  144.  
  145.     g_map_ignoreDirName.insert(NameDict::value_type(st_dirName, true));
  146. }
  147.  
  148. bool IgnoreDirList_Load(const char* fileName)
  149. {
  150.     vector<string> nameList;
  151.     if (!LoadList(fileName, &nameList))
  152.         return false;
  153.  
  154.     for_each(nameList.begin(), nameList.end(), IgnoreDirList_Append);  
  155.     return true;
  156. }
  157.  
  158. void IgnoreBasePathList_Append(const string& c_st_basePath)
  159. {
  160.     std::string st_basePath = c_st_basePath;
  161.     StringPath(st_basePath);
  162.  
  163.     g_map_ignoreBasePath.insert(NameDict::value_type(st_basePath, true));
  164. }
  165.  
  166. bool IgnoreBasePathList_Load(const char* fileName)
  167. {
  168.     vector<string> nameList;
  169.     if (!LoadList(fileName, &nameList))
  170.         return false;
  171.  
  172.     for_each(nameList.begin(), nameList.end(), IgnoreBasePathList_Append); 
  173.     return true;
  174. }
  175.  
  176. using namespace Reducio;
  177.  
  178. void RegisterPackTypeByExtName(const char * c_pszExt, BYTE packType, bool isOverwrite=false)
  179. {
  180.     if (isOverwrite)
  181.         g_PackTypeByExtNameMap[c_pszExt] = packType;
  182.     else
  183.         g_PackTypeByExtNameMap.insert(TStrMap::value_type(c_pszExt, packType));
  184. }
  185.  
  186. void RecursivePack(CEterPack & pack, const char * filename, std::vector<std::string>& vecCompressedTextures, std::vector<int>& vecCompressedTexMipMaps )
  187. {
  188.     WIN32_FIND_DATA fdata;
  189.     HANDLE          hFind;
  190.     char            temp[512];
  191.  
  192.     if ((hFind = FindFirstFile(filename, &fdata)) != INVALID_HANDLE_VALUE)
  193.     {
  194.         do
  195.         {
  196.             if (fdata.cFileName[0] == '.')
  197.                 continue;
  198.  
  199.             if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  200.             {
  201.                 strcpy_s(temp, fdata.cFileName);
  202.                 StringPath(temp);
  203.  
  204.                 if (g_map_ignoreDirName.end() != g_map_ignoreDirName.find(temp))
  205.                 {
  206.                     //printf("Exclude1 %s\n", fdata.cFileName);
  207.                     continue;
  208.                 }
  209.  
  210.                 strcpy_s(temp, filename);
  211.                 char *p = strchr(temp, '*'); // µÚ¿¡ * À» ¾ø¾Ø´Ù.
  212.                 if (p) *p = '\0';
  213.  
  214.                 StringPath(temp);
  215.  
  216.                 if (g_map_ignoreBasePath.end() != g_map_ignoreBasePath.find(temp))
  217.                 {
  218.                     //printf("Exclude2 %s\n", fdata.cFileName);
  219.                     continue;
  220.                 }
  221.  
  222.                 sprintf_s(p, sizeof(temp) - (p - temp), "%s/*", fdata.cFileName);
  223.  
  224.                 RecursivePack(pack, temp, vecCompressedTextures, vecCompressedTexMipMaps);
  225.                 continue;
  226.             }
  227.  
  228.             strcpy_s(temp, fdata.cFileName);
  229.             StringPath(temp);
  230.  
  231.             if (g_map_ignoreFileName.end() != g_map_ignoreFileName.find(temp))
  232.             {
  233.                 //printf("Exclude3 %s\n", fdata.cFileName);
  234.                 continue;
  235.             }
  236.  
  237.             strcpy_s(temp, filename);
  238.             char *p;
  239.  
  240.             if ((p = strrchr(temp, '*')))   // µÚ¿¡ * À» ¾ø¾Ø´Ù.
  241.                 *p = '\0';
  242.  
  243.             StringPath(temp);
  244.            
  245.             if (g_map_ignoreBasePath.end() != g_map_ignoreBasePath.find(temp))
  246.             {
  247.                 //printf("Exclude4 %s\n", fdata.cFileName);
  248.                 continue;
  249.             }
  250.  
  251.             if (p)
  252.                 sprintf_s(p, sizeof(temp) - (p - temp), "%s", fdata.cFileName);
  253.             else
  254.                 sprintf_s(temp, sizeof(temp), "%s", fdata.cFileName);
  255.  
  256.             std::string ext(temp);
  257.             stl_lowers(ext);
  258.  
  259.             //FIXME : TEMP HARD CODE
  260.             if (!CFileNameHelper::GetExtension(ext).compare("wdp"))
  261.                 continue;
  262.  
  263.             BYTE packType = COMPRESSED_TYPE_NONE;
  264.             {
  265.                 bool bSuccessCompTexture = false;
  266.  
  267.                 if (!CFileNameHelper::GetExtension(ext).compare("dds") && iCompressTexQuality > 0 /* && !IsSDBSupportRequired(temp, strRelatedMap)*/)
  268.                 {
  269.                     ImageHandle h = CreateImageStreamHandle();
  270.  
  271.                     //Texture¾ÐÃàÇϱâ
  272.                     eReturnCode eRet;
  273.                     eRet = CompressWMPImageFromFile(h, temp, iCompressTexQuality);
  274.                     if( eRet == eSuccess )
  275.                     {
  276.                         std::string wdpFileName = CFileNameHelper::NoExtension(ext) + ".wdp";
  277.                         DumpWMPImageToFile( h, wdpFileName.c_str() );
  278.                         bSuccessCompTexture = true;
  279.                         strcpy(temp, wdpFileName.c_str() );
  280.  
  281.                         Reducio::U8* pb;
  282.                         int nStreamLen, nMipMapCnt;
  283.                         GetImageStreamInfo( h, &pb, nStreamLen, nMipMapCnt );
  284.                        
  285.                         vecCompressedTextures.push_back(temp);
  286.                         vecCompressedTexMipMaps.push_back(nMipMapCnt);
  287.                     }
  288.                     else
  289.                     {
  290.                         CMakePackLog::GetSingleton().Writef("failed to compress tex: %s errorcode: %d \n", temp, eRet );
  291.                         CMakePackLog::GetSingleton().WriteErrorf("failed to compress tex: %s errorcode: %d \n", temp, eRet );
  292.                     }
  293.  
  294.                     DestroyImageStreamHandle(h);
  295.                 }
  296.  
  297.                 if( !bSuccessCompTexture )
  298.                 {
  299.                     unordered_map<std::string, BYTE, stringhash>::iterator it = g_PackTypeByExtNameMap.find(ext);
  300.  
  301.                     if (g_PackTypeByExtNameMap.end() != it)
  302.                         packType = it->second;
  303.                 }
  304.  
  305.                 std::string strRelatedMapName;
  306.  
  307.                 if( packType == COMPRESSED_TYPE_HYBRIDCRYPT && IsSDBSupportRequired(temp, strRelatedMapName) )
  308.                     packType = COMPRESSED_TYPE_HYBRIDCRYPT_WITHSDB;
  309.  
  310.                 if (pack.Put(temp, NULL, packType, strRelatedMapName))
  311.                 {
  312.                     CMakePackLog::GetSingleton().Writef("pack: %s\n", temp);
  313.                 }
  314.                 else
  315.                 {
  316.                     CMakePackLog::GetSingleton().Writef("pack failed: %s\n", temp);
  317.                     CMakePackLog::GetSingleton().WriteErrorf("pack failed: %s\n", temp);
  318.                 }
  319.  
  320.             }
  321.         }
  322.         while (FindNextFile(hFind, &fdata));
  323.     }
  324. }
  325.  
  326. void AddIndex(const char * c_szName, const char * c_szDirectory)
  327. {
  328.     std::string strIndexFileName = g_strFolderName + std::string("Index.new");
  329.  
  330.     FILE * fp;
  331.        
  332.     if (0 != fopen_s(&fp, strIndexFileName.c_str(), "a+"))
  333.     {
  334.         printf("%s append open error", strIndexFileName.c_str());
  335.         abort();
  336.     }
  337.  
  338.     char szTmp[MAX_PATH + 1];
  339.     strcpy_s(szTmp, c_szDirectory);
  340.  
  341.     char * p = strrchr(szTmp, '/');
  342.     *(++p) = '\0';
  343.  
  344.     fprintf(fp, "%s\n%s\n", szTmp, c_szName);
  345.     fclose(fp);
  346. }
  347.  
  348. using namespace Reducio;
  349.  
  350. void MakeReducioHeader( std::vector<std::string>& textures, std::vector<int>& mipMapCounts )
  351. {
  352.     string st_HeaderFilePath;
  353.     st_HeaderFilePath  = g_strFolderName;
  354.     st_HeaderFilePath += g_st_packName;
  355.     st_HeaderFilePath += ".rdch";
  356.    
  357.     std::ofstream outFile( st_HeaderFilePath.c_str(), ios_base::binary | ios_base::out );
  358.  
  359.     int nItemCount = textures.size();
  360.  
  361.     outFile.write((const char*)&nItemCount, sizeof(int) );
  362.  
  363.     for( int i = 0; i < nItemCount; ++i )
  364.     {
  365.         int iNameLength = textures[i].length();
  366.         outFile.write((const char*)&iNameLength, sizeof(int) );
  367.         outFile.write((const char*)&mipMapCounts[i], sizeof(int) );
  368.         outFile.write(textures[i].c_str(), iNameLength );
  369.     }
  370.  
  371.     outFile.close();
  372. }
  373.  
  374.  
  375. bool MakePackFiles(const std::vector<std::string>& filePaths, CEterPackManager* pPackManager)
  376. {
  377.     string st_packName = g_st_packName.empty() ? "noname" : g_st_packName;
  378.  
  379.     string st_packFilePath;
  380.     st_packFilePath  = g_strFolderName;
  381.     st_packFilePath += st_packName;
  382.    
  383.     CMakePackLog::GetSingleton().Writef("\n + Making %s\n", st_packName.c_str());
  384.  
  385.     CEterFileDict fileDict;
  386.     CEterPack* pPack = new CEterPack;
  387.  
  388.     if (!pPack->Create(fileDict, st_packFilePath.c_str(), g_strFolderName.c_str(), false, s_IV))
  389.         return false;
  390.  
  391.     std::vector<std::string>::const_iterator i = filePaths.begin();
  392.     std::vector<std::string>::const_iterator e = filePaths.end();
  393.  
  394.     bool bAddToIndex = false;
  395.  
  396.     std::vector<std::string> vecCompressedTextures;
  397.     std::vector<int>         vecCompressedTexMipMaps;
  398.  
  399.     bool bSuccess = true;
  400.     while (i != e)
  401.     {
  402.         const std::string& path = *i;
  403.         std::string strRelatedMap;
  404.  
  405.         char temp[1024];
  406.         strcpy_s(temp, path.c_str());
  407.  
  408.         StringPath(temp);
  409.  
  410.         std::string lowerFilePath(temp);
  411.         stl_lowers(lowerFilePath);
  412.  
  413.         BYTE packType = COMPRESSED_TYPE_NONE;
  414.  
  415.         std::string ext = CFileNameHelper::GetExtension(lowerFilePath);
  416.  
  417.         bool bSuccessCompTexture = false;
  418.  
  419.         if( !ext.compare("wdp") )
  420.         {
  421.             i++;
  422.             continue;
  423.         }
  424.  
  425.         if( !ext.compare("dds") && iCompressTexQuality > 0 /* && !IsSDBSupportRequired(temp, strRelatedMap)*/)
  426.         {
  427.             ImageHandle h = CreateImageStreamHandle();
  428.  
  429.             //Texture¾ÐÃàÇϱâ
  430.             eReturnCode eRet;
  431.             eRet = CompressWMPImageFromFile(h, temp, iCompressTexQuality);
  432.             if( eRet == eSuccess )
  433.             {
  434.                 std::string wdpFileName = CFileNameHelper::GetExtension(lowerFilePath) + ".wdp";
  435.                 DumpWMPImageToFile( h, wdpFileName.c_str() );
  436.                 bSuccessCompTexture = true;
  437.                 strcpy(temp, wdpFileName.c_str() );
  438.  
  439.                 Reducio::U8* pb;
  440.                 int nStreamLen, nMipMapCnt;
  441.                 GetImageStreamInfo( h, &pb, nStreamLen, nMipMapCnt );
  442.                
  443.                 vecCompressedTextures.push_back(temp);
  444.                 vecCompressedTexMipMaps.push_back(nMipMapCnt);
  445.             }
  446.             else
  447.             {
  448.                 CMakePackLog::GetSingleton().Writef("failed to compress tex: %s errorcode: %d \n", temp, eRet );
  449.                 CMakePackLog::GetSingleton().WriteErrorf("failed to compress tex: %s errorcode: %d \n", temp, eRet );
  450.                 if (eRet != eFail_UnSupportedFormat)
  451.                     bSuccess = false;
  452.             }
  453.  
  454.             DestroyImageStreamHandle(h);
  455.         }
  456.  
  457.         if( !bSuccessCompTexture )
  458.         {
  459.             unordered_map<std::string, BYTE, stringhash>::iterator it = g_PackTypeByExtNameMap.find(ext);
  460.  
  461.             if (g_PackTypeByExtNameMap.end() != it)
  462.                 packType = it->second;
  463.         }
  464.  
  465.         if( packType == COMPRESSED_TYPE_HYBRIDCRYPT )
  466.         {
  467.             bAddToIndex = true;
  468.             if( IsSDBSupportRequired(temp, strRelatedMap) )
  469.             {
  470.                 packType = COMPRESSED_TYPE_HYBRIDCRYPT_WITHSDB;
  471.             }
  472.         }
  473.  
  474.         if (pPack->Put(temp, NULL, packType, strRelatedMap))
  475.         {
  476.             CMakePackLog::GetSingleton().Writef("pack: %s\n", temp);
  477.         }
  478.         else
  479.         {
  480.             CMakePackLog::GetSingleton().Writef("pack failed: %s\n", temp);
  481.             CMakePackLog::GetSingleton().WriteErrorf("pack failed: %s\n", temp);
  482.             bSuccess = false;
  483.         }
  484.         i++;
  485.     }
  486.    
  487.     pPackManager->RegisterPackWhenPackMaking(st_packFilePath.c_str(), g_strFolderName.c_str(), pPack );
  488.  
  489.     if( bAddToIndex )
  490.     {
  491.         AddIndex(st_packName.c_str(), g_strFolderName.c_str());
  492.     }
  493.  
  494.     pPack->DeleteUnreferencedData();
  495.     pPack->EncryptIndexFile();
  496.  
  497.     //write it
  498.     if( vecCompressedTextures.size() > 0 )
  499.     {
  500.         MakeReducioHeader( vecCompressedTextures, vecCompressedTexMipMaps );
  501.     }
  502.  
  503.     return bSuccess;
  504. }
  505.  
  506. void MakePack(const char * c_szPackName, const char * c_szDirectory, CEterPackManager* pPackManager)
  507. {
  508.     string st_packName = g_st_packName.empty() ? c_szPackName : g_st_packName;
  509.     string st_packFilePath;
  510.     st_packFilePath  = g_strFolderName;
  511.     st_packFilePath += st_packName;
  512.    
  513.     CMakePackLog::GetSingleton().Writef("\n + Making %s\n", st_packName.c_str());
  514.  
  515.     //CEterPack pack;
  516.     CEterFileDict fileDict;
  517.     CEterPack* pPack = new CEterPack;
  518.  
  519.     if (!pPack->Create(fileDict, st_packFilePath.c_str(), g_strFolderName.c_str(), false, s_IV))
  520.         return;
  521.    
  522.     std::string stFolder(c_szDirectory);
  523.     stl_lowers(stFolder);
  524.  
  525.     std::vector<std::string> vecCompressedTextures;
  526.     std::vector<int>         vecCompressedTexMipMaps;
  527.  
  528.     RecursivePack(*pPack, stFolder.c_str(), vecCompressedTextures, vecCompressedTexMipMaps);
  529.     AddIndex(st_packName.c_str(), stFolder.c_str());
  530.  
  531.     pPack->DeleteUnreferencedData();
  532.     pPack->EncryptIndexFile();
  533.  
  534.     //write it
  535.     if( vecCompressedTextures.size() > 0 )
  536.     {
  537.         MakeReducioHeader( vecCompressedTextures, vecCompressedTexMipMaps );
  538.     }
  539.  
  540.     pPackManager->RegisterPackWhenPackMaking(st_packFilePath.c_str(), g_strFolderName.c_str(), pPack );
  541. }
  542.  
  543. bool MakeRecursivePack(const char * c_szPackName, CTextFileLoader & rTextFileLoader, CEterPackManager* pPackManager)
  544. {
  545.     string st_packName = g_st_packName.empty() ? c_szPackName : g_st_packName;
  546.     string st_packFilePath;
  547.     st_packFilePath  = g_strFolderName;
  548.     st_packFilePath += st_packName;
  549.    
  550.     string st_testName = g_strFolderName + std::string(c_szPackName);
  551.     assert(st_testName == st_packFilePath);
  552.  
  553.     //CEterPack pack;
  554.     CEterFileDict fileDict;
  555.     CEterPack* pPack = new CEterPack;
  556.  
  557.     std::vector<std::string> vecCompressedTextures;
  558.     std::vector<int>         vecCompressedTexMipMaps;
  559.  
  560.     if (!pPack->Create(fileDict, st_packFilePath.c_str(), g_strFolderName.c_str(), false, s_IV))
  561.         return false;
  562.  
  563.     for (DWORD i = 0; i < rTextFileLoader.GetChildNodeCount(); ++i)
  564.     {
  565.         if (rTextFileLoader.SetChildNode(i))
  566.         {
  567.             bool bAddIndex = false;
  568.             std::string strAddIndex;
  569.             if (rTextFileLoader.GetTokenString("addindex", &strAddIndex))
  570.                 if (0 == strAddIndex.compare("TRUE"))
  571.                     bAddIndex = true;
  572.  
  573.             std::string strFileName;
  574.             if (!rTextFileLoader.GetTokenString("filename", &strFileName))
  575.                 continue;
  576.  
  577.             RecursivePack(*pPack, strFileName.c_str(), vecCompressedTextures, vecCompressedTexMipMaps);
  578.  
  579.             if (bAddIndex)
  580.             {
  581.                 AddIndex(c_szPackName, strFileName.c_str());
  582.             }
  583.  
  584.             rTextFileLoader.SetParentNode();
  585.         }
  586.     }
  587.  
  588.     pPack->DeleteUnreferencedData();
  589.     pPack->EncryptIndexFile();
  590.  
  591.     //write it
  592.     if( vecCompressedTextures.size() > 0 )
  593.     {
  594.         MakeReducioHeader( vecCompressedTextures, vecCompressedTexMipMaps );
  595.     }
  596.  
  597.     pPackManager->RegisterPackWhenPackMaking(st_packFilePath.c_str(), g_strFolderName.c_str(), pPack );
  598.     return true;
  599. }
  600.  
  601. int main(int argc, char **argv)
  602. {  
  603.     if (argc < 2)
  604.     {
  605.         puts("USAGE:");
  606.         printf("%s [command]\n", argv[0]);
  607.         return 0;
  608.     }
  609.  
  610.     boost::shared_ptr<CLZO> lzo(new CLZO);
  611.     boost::shared_ptr<CEterPackManager> packMgr(new CEterPackManager);
  612.  
  613.     if (!strcmp(argv[1], "--createiv"))
  614.     {
  615.         // .exe --createiv <filename>
  616.         if (argc != 3)
  617.         {
  618.             printf("Usage: %s --createiv <filename>\n", argv[0]);
  619.             return 1;
  620.         }
  621.  
  622.         CFileBase diskFile;
  623.  
  624.         if (diskFile.Create(argv[2], CFileBase::FILEMODE_WRITE))
  625.         {
  626.             BYTE iv[32];
  627.  
  628.             CryptoPP::AutoSeededRandomPool prng;
  629.             prng.GenerateBlock(iv, sizeof(iv));
  630.  
  631.             diskFile.Write(iv, sizeof(iv));
  632.             diskFile.Close();
  633.  
  634.             char szHex[32*2+1];
  635.  
  636.             for (int i = 0; i < 32; ++i)
  637.                 sprintf_s(szHex + i * 2, sizeof(szHex) - i * 2, "%02x", iv[i]);
  638.  
  639.             printf("IV %s created (hex %s)\n", argv[2], szHex);
  640.             _chmod(argv[2], _S_IREAD); // Àбâ Àü¿ëÀ¸·Î ¸¸µé±â
  641.             return 0;
  642.         }
  643.  
  644.         printf("Cannot open %s, File may already exist\n", argv[2]);
  645.         return 1;
  646.     }
  647.     else if (!strcmp(argv[1], "--openiv"))
  648.     {
  649.         // .exe --openiv <filename>
  650.         if (argc != 3)
  651.         {
  652.             printf("Usage: %s --openiv <filename>\n", argv[0]);
  653.             return 1;
  654.         }
  655.  
  656.         CMappedFile file;
  657.         const BYTE* iv;
  658.        
  659.         if (file.Create(argv[2], (const void**) &iv, 0, 32))
  660.         {
  661.             char szHex[32*2+1];
  662.  
  663.             for (int i = 0; i < 32; ++i)
  664.                 sprintf_s(szHex + i * 2, sizeof(szHex) - i * 2, "%02x", iv[i]);
  665.  
  666.             printf("IV %s loaded (hex %s)", argv[2], szHex);
  667.             return 0;
  668.         }
  669.  
  670.         printf("File not found or invalid format: %s\n", argv[3]);
  671.         return 1;
  672.     }
  673.     else if (!strcmp(argv[1], "--extract"))
  674.     {
  675.         // .exe --extract <packname> [<IV filename>]
  676.         if (argc < 3)
  677.         {
  678.             printf("Usage: %s --extract <packname> [<IV filename>]\n", argv[0]);
  679.             return 1;
  680.         }
  681.  
  682.         CMappedFile file;
  683.         const BYTE* iv = NULL;
  684.  
  685.         if (argc >= 4)
  686.         {  
  687.             if (!file.Create(argv[3], (const void**) &iv, 0, 32))              
  688.             {
  689.                 printf("Cannot load IV file %s\n", argv[3]);
  690.                 return 1;
  691.             }
  692.         }
  693.  
  694.  
  695.         CEterPack pack;
  696.         CEterFileDict dict;
  697.  
  698.         if (pack.Create(dict, argv[2], "", true, iv))
  699.         {
  700.  
  701.             pack.Extract();
  702.             return 0;
  703.         }
  704.  
  705.         printf("Cannot extract pack %s\n", argv[2]);
  706.         return 1;
  707.     }
  708.  
  709.     HANDLE hThread = GetCurrentThread();
  710.     SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
  711.     SetLogLevel(1);
  712.  
  713.     if (2 != argc)
  714.     {
  715.         printf("Usage: %s [ScriptFileName]\n", argv[0]);
  716.         return 1;
  717.     }
  718.  
  719.     char* pcExt = strrchr(argv[1], '.');
  720.  
  721.     if (pcExt)
  722.     {
  723.         std::string stLogFileName(argv[1], pcExt);
  724.         CMakePackLog::GetSingleton().SetFileName(stLogFileName.c_str());
  725.     }
  726.  
  727.     CTextFileLoader TextFileLoader;
  728.  
  729.     if (!TextFileLoader.Load(argv[1]))
  730.     {
  731.         printf("Failed to load pack script\n");
  732.         return 1;
  733.     }
  734.  
  735.     if (TextFileLoader.GetTokenString("foldername", &g_strFolderName))
  736.     {
  737.         _mkdir(g_strFolderName.c_str());
  738.         g_strFolderName += "/";
  739.     }
  740.  
  741.     if (TextFileLoader.GetTokenString("packname", &g_st_packName))
  742.     {
  743.         CMakePackLog::GetSingleton().Writef("\nPackName: %s\n", g_st_packName.c_str());
  744.     }
  745.  
  746.     TextFileLoader.GetTokenInteger("compresstexture", &iCompressTexQuality);
  747.     ////////////////////////////////////////////////////////////
  748.     IgnoreDirList_Load("ignore_dir_list.txt");
  749.     IgnoreFileList_Load("ignore_file_list.txt");
  750.     IgnoreBasePathList_Load("ignore_basepath_list.txt");
  751.  
  752.     std::string strIndexFileName = g_strFolderName + std::string("Index.new");
  753.     FILE * fp;
  754.    
  755.     if (0 != fopen_s(&fp, strIndexFileName.c_str(), "w"))
  756.     {
  757.         char szCurDir[MAX_PATH + 1];
  758.         GetCurrentDirectory(MAX_PATH, szCurDir);
  759.  
  760.         printf("%s ÆÄÀÏÀ» ¿­ ¼ö ¾ø½À´Ï´Ù. µð·ºÅ丮°¡ ¾ø°Å³ª ÆÄÀÏÀÌ ÀÌ¹Ì »ç¿ë Áß ÀÔ´Ï´Ù.\n"
  761.                "ÇöÀç µð·ºÅ丮: %s\n", strIndexFileName.c_str(), szCurDir);
  762.         return 1;
  763.     }
  764.  
  765.     fprintf(fp, "PACK\n");  // Search Mode, 1 == ÆÑ¿¡¼­ ¸ÕÀú ÀÐÀ½
  766.     fclose(fp);
  767.  
  768.     ////////////////////////////////////////////////////////////
  769.  
  770.     CTokenVector * pTokenVector;
  771.  
  772.     // ExcludedFolderNameList - ÇØ´ç Æú´õ À̸§Àº ÆÑÇÏÁö ¾Ê´Â´Ù.
  773.     if (TextFileLoader.GetTokenVector("excludedfoldernamelist", &pTokenVector))
  774.     {
  775.         //printf("\n - ExcludedFolderNameList\n");
  776.         CMakePackLog::GetSingleton().Writef("\n - ExcludedFolderNameList\n");
  777.  
  778.         CTokenVector::iterator itor = pTokenVector->begin();
  779.         for (; pTokenVector->end() != itor; ++itor)
  780.         {
  781.             std::string & rstrName = *itor;
  782.             //printf(" %s\n", rstrName.c_str());
  783.             CMakePackLog::GetSingleton().Writef(" %s\n", rstrName.c_str());
  784.  
  785.             IgnoreDirList_Append("CVS");
  786.         }
  787.     }
  788.  
  789.     // ExcludedPathList - Æнº ³»¿¡ ¸ðµç °ÍÀ» ÆÑÇÏÁö ¾Ê´Â´Ù.
  790.     if (TextFileLoader.GetTokenVector("excludedpathlist", &pTokenVector))
  791.     {
  792.         //printf("\n - ExcludedPathList\n");
  793.         CMakePackLog::GetSingleton().Write("\n - ExcludedPathList\n");
  794.  
  795.         CTokenVector::iterator itor = pTokenVector->begin();
  796.         for (; pTokenVector->end() != itor; ++itor)
  797.         {
  798.             std::string & rstrName = *itor;
  799.             //printf(" %s\n", rstrName.c_str());
  800.             CMakePackLog::GetSingleton().Writef(" %s\n", rstrName.c_str());
  801.  
  802.             IgnoreBasePathList_Append("d:/ymir work/sound/");
  803.         }
  804.     }
  805.  
  806.     // ExcludedFileNameList - ¸ðµç Æú´õ¿¡ ´ëÇØ ÇØ´ç ÆÄÀÏÀ̸§Àº ÆÑÇÏÁö ¾Ê´Â´Ù.
  807.     if (TextFileLoader.GetTokenVector("excludedfilenamelist", &pTokenVector))
  808.     {
  809.         //printf("\n - ExcludedFileNameList\n");
  810.         CMakePackLog::GetSingleton().Writef("\n - ExcludedFileNameList\n");
  811.  
  812.         CTokenVector::iterator itor = pTokenVector->begin();
  813.         for (; pTokenVector->end() != itor; ++itor)
  814.         {
  815.             std::string & rstrName = *itor;
  816.             //printf(" %s\n", rstrName.c_str());
  817.             CMakePackLog::GetSingleton().Writef(" %s\n", rstrName.c_str());
  818.  
  819.             IgnoreFileList_Append(rstrName.c_str());
  820.         }
  821.     }
  822.    
  823.     // CompressExtNameList - ÇØ´ç È®ÀåÀÚ¸¦ °¡Áø ÆÄÀÏÀº compress ÇÑ´Ù.
  824.     if (TextFileLoader.GetTokenVector("compressextnamelist", &pTokenVector))
  825.     {
  826.         CMakePackLog::GetSingleton().Writef("\n - CompressExtNameList\n");
  827.  
  828.         CTokenVector::iterator itor = pTokenVector->begin();
  829.         for (; pTokenVector->end() != itor; ++itor)
  830.         {
  831.             std::string & rstrName = *itor;
  832.             CMakePackLog::GetSingleton().Writef(" %s\n", rstrName.c_str());
  833.  
  834.             RegisterPackTypeByExtName(rstrName.c_str(), COMPRESSED_TYPE_COMPRESS);
  835.         }
  836.     }
  837.  
  838.     // SecurityExtNameList - ÇØ´ç È®ÀåÀÚ¸¦ °¡Áø ÆÄÀÏÀº encrypt ÇÑ´Ù.
  839.     if (TextFileLoader.GetTokenVector("securityextnamelist", &pTokenVector))
  840.     {
  841.         CMakePackLog::GetSingleton().Writef("\n - SecurityExtNameList\n");
  842.  
  843.         CTokenVector::iterator itor = pTokenVector->begin();
  844.         for (; pTokenVector->end() != itor; ++itor)
  845.         {
  846.             std::string & rstrName = *itor;
  847.             CMakePackLog::GetSingleton().Writef(" %s\n", rstrName.c_str());
  848.  
  849.             RegisterPackTypeByExtName(rstrName.c_str(), COMPRESSED_TYPE_SECURITY);
  850.         }
  851.     }
  852.  
  853.     // OldCompressExtNameList - ÇØ´ç È®ÀåÀÚ¸¦ °¡Áø ÆÄÀÏÀº ¾Ïȣȭ ÇÑ´Ù.
  854.     if (TextFileLoader.GetTokenVector("oldcompressextnamelist", &pTokenVector))
  855.     {
  856.         CMakePackLog::GetSingleton().Writef("\n - OldCompressExtNameList\n");
  857.  
  858.         CTokenVector::iterator itor = pTokenVector->begin();
  859.         for (; pTokenVector->end() != itor; ++itor)
  860.         {
  861.             std::string & rstrName = *itor;
  862.             CMakePackLog::GetSingleton().Writef(" %s\n", rstrName.c_str());
  863.  
  864.             // OldCompressExtNameList´Â ¾ÐÃàÀ» ÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó ¾Ïȣȭ¸¦ ÇؾßÇϹǷÎ
  865.             // ÀǵµÀûÀ¸·Î COMPRESSED_TYPE_SECURITY¸¦ ³Ö¾úÀ½
  866.             RegisterPackTypeByExtName(rstrName.c_str(), COMPRESSED_TYPE_SECURITY);
  867.         }
  868.     }
  869.  
  870.     std::string stIVFileName;
  871.  
  872.     if (TextFileLoader.GetTokenString("iv", &stIVFileName))
  873.     {
  874.         CMappedFile mappedFile;
  875.         BYTE * pIV;
  876.  
  877.         if (mappedFile.Create(stIVFileName.c_str(), (const void **) &pIV, 0, 32))
  878.             memcpy(s_IV, pIV, 32);
  879.         else
  880.         {
  881.             TraceError("IV open error: %s", stIVFileName.c_str());
  882.             return 1;
  883.         }
  884.     }
  885.  
  886.     // PanamaExtNameList - »õ·Î¿î ¾Ïȣȭ
  887.     if (TextFileLoader.GetTokenVector("panamaextnamelist", &pTokenVector))
  888.     {
  889.         CMakePackLog::GetSingleton().Writef("\n - PanamaExtNameList\n");
  890.  
  891.         CTokenVector::iterator itor = pTokenVector->begin();
  892.         for (; pTokenVector->end() != itor; ++itor)
  893.         {
  894.             std::string & rstrName = *itor;
  895.             CMakePackLog::GetSingleton().Writef(" %s\n", rstrName.c_str());
  896.  
  897.             RegisterPackTypeByExtName(rstrName.c_str(), COMPRESSED_TYPE_PANAMA, true);
  898.         }
  899.     }
  900.  
  901.     if (TextFileLoader.GetTokenVector("cshybridencryptexenamelist", &pTokenVector))
  902.     {
  903.         CMakePackLog::GetSingleton().Writef("\n - C/S Hybrid Encrypt ExtNameList\n");
  904.  
  905.         CTokenVector::iterator itor = pTokenVector->begin();
  906.         for (; pTokenVector->end() != itor; ++itor)
  907.         {
  908.             std::string & rstrName = *itor;
  909.             CMakePackLog::GetSingleton().Writef(" %s\n", rstrName.c_str());
  910.  
  911.             RegisterPackTypeByExtName(rstrName.c_str(), COMPRESSED_TYPE_HYBRIDCRYPT, true);
  912.         }
  913.     }
  914.  
  915.     if (TextFileLoader.SetChildNode("rootpackitemlist"))
  916.     {
  917.         CMakePackLog::GetSingleton().Writef("\n + Making RootPack\n");
  918.         MakeRecursivePack("root", TextFileLoader, packMgr.get());
  919.         TextFileLoader.SetParentNode();
  920.     }
  921.  
  922.     if (TextFileLoader.SetChildNode("localpathpack"))
  923.     {
  924.         CMakePackLog::GetSingleton().Writef("\n + Making RootPack\n");
  925.         MakeRecursivePack(g_st_packName.c_str(), TextFileLoader, packMgr.get());
  926.         TextFileLoader.SetParentNode();
  927.     }
  928.  
  929.     if (TextFileLoader.SetChildNode("etcpackitemlist"))
  930.     {
  931.         CMakePackLog::GetSingleton().Writef("\n + Making ETCPack\n");
  932.         MakeRecursivePack("ETC", TextFileLoader, packMgr.get());
  933.         TextFileLoader.SetParentNode();
  934.     }
  935.  
  936.     if (TextFileLoader.SetChildNode("soundpackitemlist"))
  937.     {
  938.         CMakePackLog::GetSingleton().Writef("\n + Making SoundPack\n");
  939.         MakeRecursivePack("sound", TextFileLoader, packMgr.get());
  940.         TextFileLoader.SetParentNode();
  941.     }
  942.  
  943.     if (TextFileLoader.SetChildNode("sound2packitemlist"))
  944.     {
  945.         CMakePackLog::GetSingleton().Writef("\n + Making Sound2Pack\n");
  946.         MakeRecursivePack("sound2", TextFileLoader, packMgr.get());
  947.         TextFileLoader.SetParentNode();
  948.     }
  949.  
  950.     CMakePackLog::GetSingleton().Writef("\n + PackList \n");
  951.  
  952.     if (TextFileLoader.SetChildNode("packlist"))
  953.     {
  954.         for (DWORD i = 0; i < TextFileLoader.GetChildNodeCount(); ++i)
  955.         {
  956.             if (TextFileLoader.SetChildNode(i))
  957.             {
  958.                 std::string strNodeName;
  959.                 if (!TextFileLoader.GetCurrentNodeName(&strNodeName))
  960.                     continue;
  961.  
  962.                 std::string strPathName;
  963.                 if (!TextFileLoader.GetTokenString("pathname", &strPathName))
  964.                     continue;
  965.  
  966.                 MakePack(strNodeName.c_str(), strPathName.c_str(), packMgr.get());
  967.  
  968.                 TextFileLoader.SetParentNode();
  969.             }
  970.         }
  971.     }
  972.  
  973.     // SDB ( Supplementary Data Block Required File List )
  974.     if (TextFileLoader.GetTokenVector("sdbfilelist", &pTokenVector))
  975.     {
  976.         CTokenVector::iterator itor = pTokenVector->begin();
  977.         for (; pTokenVector->end() != itor; ++itor)
  978.         {
  979.             std::string & rstrName = *itor;    
  980.  
  981.             std::string rstrNameLower = rstrName;
  982.             stl_lowers(rstrNameLower);
  983.  
  984.             std::vector<std::string> strs;
  985.             //mapname:sdbfilename
  986.             boost::split(strs, rstrNameLower, boost::is_any_of("?"));
  987.  
  988.             if( strs.size() == 1 )
  989.             {
  990.                 strs.push_back(strs[0]);
  991.                 strs[0] = "none";
  992.             }
  993.  
  994.             if( strs.size() != 2 )
  995.             {
  996.                 CMakePackLog::GetSingleton().Writef("\n SDB File Usage: MapName:SDBFileName \n");
  997.                 return -1;
  998.             }
  999.  
  1000.             StringPath(strs[1]);
  1001.             g_map_SDBFileList[strs[1]] = strs[0];
  1002.         }
  1003.     }
  1004.  
  1005.     // filelist¿¡ ¼½¼Ç¿¡ ÀԷµǾî ÀÖ´Â ¸ðµç ÆÄÀÏÀ» ÆÑÇϱâ
  1006.     if (TextFileLoader.GetTokenVector("filelist", &pTokenVector))
  1007.     {
  1008.         std::vector<std::string> filePaths;
  1009.  
  1010.         CTokenVector::iterator itor = pTokenVector->begin();
  1011.         for (; pTokenVector->end() != itor; ++itor)
  1012.         {
  1013.             std::string & rstrName = *itor;
  1014.             std::vector<std::string>::iterator it = std::find( filePaths.begin(), filePaths.end(), rstrName );
  1015.             if( it == filePaths.end() )
  1016.             {
  1017.                 filePaths.push_back(rstrName);
  1018.             }
  1019.         }
  1020.  
  1021.         if(!MakePackFiles(filePaths, packMgr.get()))
  1022.         {
  1023.             CMakePackLog::GetSingleton().FlushError();
  1024.             return -1;
  1025.         }
  1026.     }
  1027.  
  1028.     //make key file for CS hybrid crypt
  1029.     std::string strCryptFileName = g_strFolderName + "/" + "cshybridcrypt_" + g_st_packName + ".dat";
  1030.  
  1031.     packMgr->WriteHybridCryptPackInfo(strCryptFileName.c_str());
  1032.  
  1033.     std::string strOldIndexFileName = g_strFolderName + std::string("Index");
  1034.  
  1035.     DeleteFile(strOldIndexFileName.c_str());
  1036.     MoveFile(strIndexFileName.c_str(), strOldIndexFileName.c_str());
  1037.  
  1038.     TextFileLoader.Destroy();
  1039.     CTextFileLoader::DestroySystem();
  1040.     return 0;
  1041. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement