nightpatrol

dqwe12q3421423r

May 17th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.14 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <malloc.h>
  6.  
  7. #include <AssetBundleExtractor\IPluginInterface.h>
  8. #include <AssetsTools\TextureFileFormat.h>
  9. #include <AssetsTools\AssetsReplacer.h>
  10.  
  11.  
  12. void TextAsset_FreeMemoryResource(void *pResource)
  13. {
  14. free(pResource);
  15. }
  16.  
  17. void ImportFromTXT(HWND hParentWnd, IPluginInterface *pInterface, IAssetInterface **ppAssets, size_t assetCount)
  18. {
  19. IAssetInterface *pAsset = ppAssets[0];
  20. WCHAR *sourceFileName;
  21. if (SUCCEEDED(pInterface->ShowFileOpenDialog(hParentWnd, &sourceFileName, L"*.txt|Text file:")))
  22. {
  23. FILE *pSourceFile = NULL;
  24. _wfopen_s(&pSourceFile, sourceFileName, L"rb");
  25. if (pSourceFile)
  26. {
  27. fseek(pSourceFile, 0, SEEK_END);
  28. long inFileSize = ftell(pSourceFile);
  29. fseek(pSourceFile, 0, SEEK_SET);
  30. void *pFileBuffer = malloc(inFileSize);
  31. __checkoutofmemory(pFileBuffer==NULL);
  32.  
  33. fread(pFileBuffer, inFileSize, 1, pSourceFile);
  34.  
  35. AssetTypeTemplateField templateBase;
  36. char *name = "";
  37. char *pathName = "";
  38. if (pInterface->MakeTemplateField(pAsset->GetAssetsFile(), 0x31, &templateBase))
  39. {
  40. //make m_Script a TypelessData array because sometimes binary data (including 00s) is stored there
  41. for (DWORD i = 0; i < templateBase.childrenCount; i++)
  42. {
  43. if (!strcmp(templateBase.children[i].name, "m_Script"))
  44. {
  45. templateBase.children[i].type = "_string";
  46. templateBase.children[i].valueType = ValueType_None;
  47. for (DWORD k = 0; k < templateBase.children[i].childrenCount; k++)
  48. {
  49. if (!strcmp(templateBase.children[i].children[k].name, "Array"))
  50. {
  51. templateBase.children[i].children[k].type = "TypelessData";
  52. break;
  53. }
  54. }
  55. break;
  56. }
  57. }
  58. AssetTypeTemplateField *pTemplateBase = &templateBase;
  59. AssetsFileReader reader;
  60. LPARAM readerPar;
  61. unsigned __int64 assetFilePos;
  62. unsigned __int64 assetFileSize = pAsset->GetFileReader(reader, readerPar, assetFilePos);
  63. AssetTypeInstance instance(1, &pTemplateBase, reader, readerPar, pAsset->GetAssetsFile()->header.endianness, assetFilePos);
  64. pAsset->FreeFileReader(reader, readerPar);
  65. AssetTypeValueField *pBase = instance.GetBaseField();
  66. if (pBase)
  67. {
  68. AssetTypeValueField *nameField = pBase->Get("m_Name");
  69. AssetTypeValueField *scriptField = pBase->Get("m_Script");
  70. AssetTypeValueField *dataArrayField = scriptField->Get("Array");
  71. AssetTypeValueField *pathNameField = pBase->Get("m_PathName");
  72. if (!nameField->IsDummy() && !scriptField->IsDummy() && !pathNameField->IsDummy() && !dataArrayField->IsDummy())
  73. {
  74. name = nameField->GetValue()->AsString();
  75. pathName = pathNameField->GetValue()->AsString();
  76. AssetTypeByteArray byteArrayData;
  77. byteArrayData.size = (DWORD)inFileSize;
  78. byteArrayData.data = (BYTE*)pFileBuffer;
  79. dataArrayField->GetValue()->Set(&byteArrayData);
  80.  
  81. QWORD newByteSize = pBase->GetByteSize(0);
  82. void *newAssetBuffer = malloc((size_t)newByteSize);
  83. __checkoutofmemory(newAssetBuffer==NULL);
  84. LPARAM writerPar = Create_AssetsWriterToMemory(newAssetBuffer, (size_t)newByteSize);
  85. __checkoutofmemory(writerPar==NULL);
  86.  
  87. newByteSize = pBase->Write(AssetsWriterToMemory, writerPar, 0);
  88. AssetsReplacer *pReplacer = MakeAssetModifierFromMemory(pAsset->GetFileID(), pAsset->GetPathID(),
  89. pAsset->GetClassID(), pAsset->GetMonoClassID(),
  90. newAssetBuffer, newByteSize, TextAsset_FreeMemoryResource);
  91. __checkoutofmemory(pReplacer==NULL);
  92.  
  93. pInterface->AddReplacer(pReplacer);
  94.  
  95. Free_AssetsWriterToMemory(writerPar);
  96. }
  97. else
  98. MessageBox(NULL, TEXT("Unknown TextAsset format!"), TEXT("Error"), 16);
  99. }
  100. else
  101. MessageBox(NULL, TEXT("Unable to load the TextAsset!"), TEXT("Error"), 16);
  102. }
  103. else
  104. MessageBox(NULL, TEXT("TextAsset format not found!"), TEXT("Error"), 16);
  105. free(pFileBuffer);
  106. fclose(pSourceFile);
  107. }
  108. pInterface->FreeCOMFilePathBuf(&sourceFileName);
  109. }
  110. }
  111.  
  112. void ExportToTXT(HWND hParentWnd, IPluginInterface *pInterface, IAssetInterface **ppAssets, size_t assetCount)
  113. {
  114. char *outFolderPath = NULL; size_t outFolderPathLen = 0;
  115. char **prevAssetNames = NULL;
  116. if (assetCount > 1)
  117. {
  118. prevAssetNames = (char**)malloc(sizeof(char*) * (assetCount - 1));
  119. if (prevAssetNames == NULL)
  120. {
  121. MessageBox(NULL, TEXT("Out of memory!"), TEXT("ERROR"), 16);
  122. return;
  123. }
  124.  
  125. WCHAR *outFolderPathW;
  126. if (!pInterface->ShowFolderSelectDialog(hParentWnd, &outFolderPathW, L"Select an output path"))
  127. return;
  128. size_t outFolderPathWLen = wcslen(outFolderPathW);
  129. outFolderPathLen = (size_t)WideCharToMultiByte(CP_UTF8, 0, outFolderPathW, (int)outFolderPathWLen, NULL, 0, NULL, NULL);
  130. outFolderPath = (char*)malloc((outFolderPathLen + 1) * sizeof(char));
  131. if (outFolderPath == NULL)
  132. {
  133. pInterface->FreeCOMFilePathBuf(&outFolderPathW);
  134. MessageBox(NULL, TEXT("Out of memory!"), TEXT("ERROR"), 16);
  135. return;
  136. }
  137. WideCharToMultiByte(CP_UTF8, 0, outFolderPathW, (int)outFolderPathWLen, outFolderPath, (int)outFolderPathLen, NULL, NULL);
  138. outFolderPath[outFolderPathLen] = 0;
  139. pInterface->FreeCOMFilePathBuf(&outFolderPathW);
  140. }
  141. for (size_t i = 0; i < assetCount; i++)
  142. {
  143. IAssetInterface *pAsset = ppAssets[i];
  144. AssetTypeTemplateField templateBase;
  145. if (pInterface->MakeTemplateField(pAsset->GetAssetsFile(), 0x31, &templateBase))
  146. {
  147. //make m_Script a TypelessData array because it might contain binary data (even though it's a string)
  148. for (DWORD i = 0; i < templateBase.childrenCount; i++)
  149. {
  150. if (!strcmp(templateBase.children[i].name, "m_Script"))
  151. {
  152. templateBase.children[i].type = "_string";
  153. templateBase.children[i].valueType = ValueType_None;
  154. for (DWORD k = 0; k < templateBase.children[i].childrenCount; k++)
  155. {
  156. if (!strcmp(templateBase.children[i].children[k].name, "Array"))
  157. {
  158. templateBase.children[i].children[k].type = "TypelessData";
  159. break;
  160. }
  161. }
  162. break;
  163. }
  164. }
  165. AssetsFileReader reader;
  166. LPARAM readerPar;
  167. unsigned __int64 filePos;
  168. unsigned __int64 fileSize = pAsset->GetFileReader(reader, readerPar, filePos);
  169. AssetTypeTemplateField *pTemplateBase = &templateBase;
  170. AssetTypeInstance instance(1, &pTemplateBase, reader, readerPar, pAsset->GetAssetsFile()->header.endianness, filePos);
  171. pAsset->FreeFileReader(reader, readerPar);
  172. AssetTypeValueField *pBase = instance.GetBaseField();
  173. //templateBase.MakeValue(reader, readerPar, (QWORD)filePos, &pBase);
  174.  
  175. if (pBase)
  176. {
  177. AssetTypeValueField *nameField = pBase->Get("m_Name");
  178. AssetTypeValueField *scriptField = pBase->Get("m_Script");
  179. AssetTypeValueField *dataArrayField = scriptField->Get("Array");
  180. AssetTypeValueField *pathNameField = pBase->Get("m_PathName");
  181. if (!nameField->IsDummy() && !scriptField->IsDummy() && !pathNameField->IsDummy() && !dataArrayField->IsDummy())
  182. {
  183. char *name = nameField->GetValue()->AsString();
  184.  
  185. WCHAR *dlgTargetFileName = NULL;
  186. char *fileName = name;
  187. if (outFolderPath == NULL)
  188. {
  189. #ifdef _UNICODE
  190. WCHAR *exportSuggestion = pInterface->MakeExportFileNameW(
  191. #else
  192. char *exportSuggestion = pInterface->MakeExportFileName(
  193. #endif
  194. outFolderPath, fileName,
  195. pAsset->GetFileID(), pAsset->GetPathID(),
  196. prevAssetNames,
  197. i, assetCount,
  198. ".txt");
  199. WCHAR *filePath;
  200. if (SUCCEEDED(pInterface->ShowFileSaveDialog(hParentWnd, &filePath, L"*.txt|Text file:", exportSuggestion)))
  201. {
  202. size_t outFilePathLen = wcslen(filePath);
  203. if ((outFilePathLen < 4) || (_wcsicmp(&filePath[outFilePathLen-4], L".txt")))
  204. {
  205. dlgTargetFileName = (WCHAR*)malloc((outFilePathLen + 5) * sizeof(WCHAR));
  206. __checkoutofmemory(dlgTargetFileName==NULL);
  207. memcpy(dlgTargetFileName, filePath, outFilePathLen * sizeof(WCHAR));
  208. memcpy(&dlgTargetFileName[outFilePathLen], L".txt", 5 * sizeof(WCHAR));
  209. }
  210. else
  211. {
  212. dlgTargetFileName = (WCHAR*)malloc((outFilePathLen + 1) * sizeof(WCHAR));
  213. __checkoutofmemory(dlgTargetFileName==NULL);
  214. memcpy(dlgTargetFileName, filePath, (outFilePathLen+1) * sizeof(WCHAR));
  215. }
  216. pInterface->FreeCOMFilePathBuf(&filePath);
  217. }
  218. pInterface->MemFree(exportSuggestion);
  219. }
  220. else
  221. {
  222. char *cOutFilePath = pInterface->MakeExportFileName(
  223. outFolderPath, fileName,
  224. pAsset->GetFileID(), pAsset->GetPathID(),
  225. prevAssetNames,
  226. i, assetCount,
  227. ".txt");
  228. if ((i+1) < assetCount)
  229. {
  230. size_t fileNameLen = strlen(fileName);
  231. prevAssetNames[i] = (char*)malloc((fileNameLen + 1) * sizeof(char));
  232. if (prevAssetNames[i] == NULL)
  233. MessageBox(NULL, TEXT("Out of memory!"), TEXT("ERROR"), 16);
  234. else
  235. {
  236. memcpy(prevAssetNames[i], fileName, (fileNameLen + 1) * sizeof(char));
  237. }
  238. }
  239. if (cOutFilePath == NULL)
  240. MessageBox(NULL, TEXT("Out of memory!"), TEXT("ERROR"), 16);
  241. else
  242. {
  243. int filePathALen = (int)strlen(cOutFilePath);
  244. int filePathWLen = MultiByteToWideChar(CP_UTF8, 0, cOutFilePath, (int)filePathALen, NULL, 0);
  245. dlgTargetFileName = (WCHAR*)malloc((filePathWLen + 1) * sizeof(wchar_t));
  246. if (dlgTargetFileName != NULL)
  247. {
  248. MultiByteToWideChar(CP_UTF8, 0, cOutFilePath, (int)filePathALen, dlgTargetFileName, filePathWLen);
  249. dlgTargetFileName[filePathWLen] = 0;
  250. }
  251. else
  252. MessageBox(NULL, TEXT("Out of memory!"), TEXT("ERROR"), 16);
  253. pInterface->MemFree(cOutFilePath);
  254. //free(cOutFilePath);
  255. }
  256. }
  257.  
  258. if (dlgTargetFileName != NULL)
  259. {
  260. FILE *pOutFile = NULL;
  261. _wfopen_s(&pOutFile, dlgTargetFileName, L"wb");
  262. if (pOutFile != NULL)
  263. {
  264. AssetTypeByteArray *pByteArray = dataArrayField->GetValue()->AsByteArray();
  265. fwrite(pByteArray->data, pByteArray->size, 1, pOutFile);
  266. fclose(pOutFile);
  267. }
  268.  
  269. free(dlgTargetFileName);
  270. }
  271. }
  272. else
  273. {
  274. MessageBox(NULL, TEXT("Unknown TextAsset format!"), TEXT("Error"), 16);
  275. break;
  276. }
  277. }
  278. else
  279. MessageBox(NULL, TEXT("Unable to load the TextAsset!"), TEXT("Error"), 16);
  280. }
  281. else
  282. {
  283. MessageBox(NULL, TEXT("TextAsset format not found!"), TEXT("Error"), 16);
  284. break;
  285. }
  286. }
  287. if (outFolderPath)
  288. free(outFolderPath);
  289. }
  290.  
  291. BYTE PluginInfoBuffer[sizeof(PluginInfo)+2*sizeof(PluginAssetOption)];
  292. PluginInfo *GetAssetBundlePluginInfo()
  293. {
  294. PluginInfo *pPluginInfo = (PluginInfo*)PluginInfoBuffer;
  295. strcpy(pPluginInfo->name, "TextAsset");
  296. pPluginInfo->optionCount = 2;
  297.  
  298. pPluginInfo->options[0].action = PluginAction_EXPORT_Batch;
  299. strcpy(pPluginInfo->options[0].desc, "Export to .txt");
  300. pPluginInfo->options[0].unityClassID = 0x31;
  301. pPluginInfo->options[0].callback = ExportToTXT;
  302.  
  303. pPluginInfo->options[1].action = PluginAction_IMPORT;
  304. strcpy(pPluginInfo->options[1].desc, "Import from .txt");
  305. pPluginInfo->options[1].unityClassID = 0x31;
  306. pPluginInfo->options[1].callback = ImportFromTXT;
  307. return pPluginInfo;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment