Converter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Component/Entity.h>
  9. #include <AzCore/Debug/Trace.h>
  10. #include <AzCore/JSON/prettywriter.h>
  11. #include <AzCore/Module/Module.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzCore/Serialization/Utils.h>
  15. #include <AzCore/Serialization/Json/JsonSerialization.h>
  16. #include <AzCore/Settings/SettingsRegistryImpl.h>
  17. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  18. #include <AzCore/Settings/SettingsRegistryVisitorUtils.h>
  19. #include <AzCore/std/containers/unordered_set.h>
  20. #include <AzCore/Utils/Utils.h>
  21. #include <Application.h>
  22. #include <Converter.h>
  23. #include <Utilities.h>
  24. namespace AZ
  25. {
  26. namespace SerializeContextTools
  27. {
  28. bool Converter::ConvertObjectStreamFiles(Application& application)
  29. {
  30. using namespace AZ::JsonSerializationResult;
  31. const AZ::CommandLine* commandLine = application.GetAzCommandLine();
  32. if (!commandLine)
  33. {
  34. AZ_Error("SerializeContextTools", false, "Command line not available.");
  35. return false;
  36. }
  37. JsonSerializerSettings convertSettings;
  38. convertSettings.m_keepDefaults = commandLine->HasSwitch("keepdefaults");
  39. convertSettings.m_registrationContext = application.GetJsonRegistrationContext();
  40. convertSettings.m_serializeContext = application.GetSerializeContext();
  41. if (!convertSettings.m_serializeContext)
  42. {
  43. AZ_Error("Convert", false, "No serialize context found.");
  44. return false;
  45. }
  46. if (!convertSettings.m_registrationContext)
  47. {
  48. AZ_Error("Convert", false, "No json registration context found.");
  49. return false;
  50. }
  51. AZStd::string logggingScratchBuffer;
  52. SetupLogging(logggingScratchBuffer, convertSettings.m_reporting, *commandLine);
  53. if (!commandLine->HasSwitch("ext"))
  54. {
  55. AZ_Error("Convert", false, "No extension provided through the 'ext' argument.");
  56. return false;
  57. }
  58. const AZStd::string& extension = commandLine->GetSwitchValue("ext", 0);
  59. bool isDryRun = commandLine->HasSwitch("dryrun");
  60. bool skipVerify = commandLine->HasSwitch("skipverify");
  61. JsonDeserializerSettings verifySettings;
  62. if (!skipVerify)
  63. {
  64. verifySettings.m_registrationContext = application.GetJsonRegistrationContext();
  65. verifySettings.m_serializeContext = application.GetSerializeContext();
  66. SetupLogging(logggingScratchBuffer, verifySettings.m_reporting, *commandLine);
  67. }
  68. bool result = true;
  69. rapidjson::StringBuffer scratchBuffer;
  70. AZStd::vector<AZStd::string> fileList = Utilities::ReadFileListFromCommandLine(application, "files");
  71. for (AZStd::string& filePath : fileList)
  72. {
  73. AZ_Printf("Convert", "Converting '%s'\n", filePath.c_str());
  74. PathDocumentContainer documents;
  75. auto callback = [&result, &documents, &convertSettings, &verifySettings, skipVerify]
  76. (void* classPtr, const Uuid& classId, SerializeContext* /*context*/)
  77. {
  78. rapidjson::Document document;
  79. ResultCode parseResult = JsonSerialization::Store(document.SetObject(), document.GetAllocator(), classPtr, nullptr, classId, convertSettings);
  80. if (parseResult.GetProcessing() != Processing::Halted)
  81. {
  82. if (skipVerify || VerifyConvertedData(document, classPtr, classId, verifySettings))
  83. {
  84. if (parseResult.GetOutcome() == Outcomes::DefaultsUsed)
  85. {
  86. AZ_Printf("Convert", " File not converted as only default values were found.\n");
  87. }
  88. else
  89. {
  90. documents.emplace_back(GetClassName(classId, convertSettings.m_serializeContext), AZStd::move(document));
  91. }
  92. }
  93. else
  94. {
  95. AZ_Printf("Convert", " Verification of the converted file failed.\n");
  96. result = false;
  97. }
  98. }
  99. else
  100. {
  101. AZ_Printf("Convert", " Conversion to JSON failed.\n");
  102. result = false;
  103. }
  104. return true;
  105. };
  106. if (!AZ::Utils::InspectSerializedFile(filePath.c_str(), convertSettings.m_serializeContext, callback))
  107. {
  108. AZ_Warning("Convert", false, "Failed to load '%s'. File may not contain an object stream.", filePath.c_str());
  109. result = false;
  110. }
  111. // If there's only one file, then use the original name instead of the extended name
  112. AZ::StringFunc::Path::ReplaceExtension(filePath, extension.c_str());
  113. if (documents.size() == 1)
  114. {
  115. AZ_Printf("Convert", " Exporting to '%s'\n", filePath.c_str());
  116. if (!isDryRun)
  117. {
  118. AZStd::string jsonDocumentRootPrefix;
  119. if (commandLine->HasSwitch("json-prefix"))
  120. {
  121. jsonDocumentRootPrefix = commandLine->GetSwitchValue("json-prefix", 0);
  122. }
  123. result = WriteDocumentToDisk(filePath, documents[0].second, jsonDocumentRootPrefix, scratchBuffer) && result;
  124. scratchBuffer.Clear();
  125. }
  126. }
  127. else
  128. {
  129. AZStd::string fileName;
  130. AZ::StringFunc::Path::GetFileName(filePath.c_str(), fileName);
  131. for (PathDocumentPair& document : documents)
  132. {
  133. AZStd::string fileNameExtended = fileName;
  134. fileNameExtended += '_';
  135. fileNameExtended += document.first;
  136. Utilities::SanitizeFilePath(fileNameExtended);
  137. AZStd::string finalFilePath = filePath;
  138. AZ::StringFunc::Path::ReplaceFullName(finalFilePath, fileNameExtended.c_str(), extension.c_str());
  139. AZ_Printf("Convert", " Exporting to '%s'\n", finalFilePath.c_str());
  140. if (!isDryRun)
  141. {
  142. AZStd::string jsonDocumentRootPrefix;
  143. if (commandLine->HasSwitch("json-prefix"))
  144. {
  145. jsonDocumentRootPrefix = commandLine->GetSwitchValue("json-prefix", 0);
  146. }
  147. result = WriteDocumentToDisk(finalFilePath, document.second, jsonDocumentRootPrefix, scratchBuffer) && result;
  148. scratchBuffer.Clear();
  149. }
  150. }
  151. }
  152. }
  153. return result;
  154. }
  155. bool Converter::ConvertConfigFile(Application& application)
  156. {
  157. bool result = true;
  158. const AZ::CommandLine* commandLine = application.GetAzCommandLine();
  159. if (!commandLine)
  160. {
  161. AZ_Error("SerializeContextTools", false, "Command line not available.");
  162. return false;
  163. }
  164. AZStd::string outputExtension;
  165. if (!commandLine->HasSwitch("ext"))
  166. {
  167. AZ_TracePrintf("Convert", "No extension provided through the 'ext' argument.\nThe extension of .setreg will be used instead\n");
  168. outputExtension = "setreg";
  169. }
  170. else
  171. {
  172. outputExtension = commandLine->GetSwitchValue("ext", 0);
  173. }
  174. const bool isDryRun = commandLine->HasSwitch("dryrun");
  175. // The AZ CommandLine internally splits switches on <comma> and semicolon
  176. AZStd::vector<AZStd::string_view> fileList;
  177. size_t filesToConvert = commandLine->GetNumSwitchValues("files");
  178. for (size_t fileIndex{}; fileIndex < filesToConvert; ++fileIndex)
  179. {
  180. fileList.emplace_back(commandLine->GetSwitchValue("files", fileIndex));
  181. }
  182. // Gather list of INI style files to convert using the SystemFile::FindFiles function
  183. PathDocumentContainer documents;
  184. for (AZStd::string_view configFileView : fileList)
  185. {
  186. // Convert the supplied file list to an absolute path
  187. AZStd::optional<AZ::IO::FixedMaxPathString> absFilePath = AZ::Utils::ConvertToAbsolutePath(configFileView);
  188. AZ::IO::FixedMaxPath configFilePath = absFilePath ? *absFilePath : configFileView;
  189. auto callback = [&documents, &configFilePath](AZ::IO::PathView configFileView, bool isFile) -> bool
  190. {
  191. if (configFileView == "." || configFileView == "..")
  192. {
  193. return true;
  194. }
  195. if (isFile)
  196. {
  197. AZ::IO::FixedMaxPath foundFilePath{ configFilePath.ParentPath() };
  198. foundFilePath /= configFileView;
  199. // Initialize added documents with an empty JSON object(instead of a JSON null)
  200. // This prevents a JSON document from being output with just null when there
  201. // are no configuration entries
  202. documents.emplace_back(foundFilePath.String(), rapidjson::Document{rapidjson::kObjectType});
  203. }
  204. return true;
  205. };
  206. AZ::IO::SystemFile::FindFiles(configFilePath.c_str(), callback);
  207. }
  208. // JSON pointer prefix to use as a temporary root for merging the config file to the settings registry
  209. // and dumping it to a rapidjson document. The prefix is used to make sure other settings outside
  210. // of the config settings are not output
  211. constexpr AZStd::string_view ConvertJsonPointer = "/Amazon/Config/Root";
  212. for (auto&& [iniFilename, iniJsonDocument] : documents)
  213. {
  214. // Local Settings Registry is used to contain only the converted INI-style file settings
  215. AZ::SettingsRegistryImpl settingsRegistry;
  216. AZ::SettingsRegistryMergeUtils::ConfigParserSettings configParserSettings;
  217. configParserSettings.m_commentPrefixFunc = [](AZStd::string_view line) -> AZStd::string_view
  218. {
  219. constexpr AZStd::string_view commentPrefixes[]{ "--", ";","#" };
  220. for (AZStd::string_view commentPrefix : commentPrefixes)
  221. {
  222. if (size_t commentOffset = line.find(commentPrefix); commentOffset != AZStd::string_view::npos)
  223. {
  224. line = line.substr(0, commentOffset);
  225. }
  226. }
  227. return line;
  228. };
  229. configParserSettings.m_registryRootPointerPath = ConvertJsonPointer;
  230. if (!AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_ConfigFile(settingsRegistry, iniFilename, configParserSettings))
  231. {
  232. AZ_TracePrintf("Convert", "Merging of config file %s has failed. It will be skipped", iniFilename.c_str());
  233. result = false;
  234. continue;
  235. }
  236. // If the Config file contained no settings, then Settings Registry contains no settings to dump at the JSON Pointer
  237. // In this scenario there are no settings to dump so continue to the next iteration
  238. if (settingsRegistry.GetType(ConvertJsonPointer) == AZ::SettingsRegistryInterface::Type::Object)
  239. {
  240. // Dump the Settings Registry to a string that can be stored in a rapidjson::Document
  241. AZ::SettingsRegistryMergeUtils::DumperSettings dumperSettings;
  242. dumperSettings.m_prettifyOutput = true;
  243. AZStd::string configJson;
  244. AZ::IO::ByteContainerStream configJsonStream(&configJson);
  245. if (!AZ::SettingsRegistryMergeUtils::DumpSettingsRegistryToStream(settingsRegistry, ConvertJsonPointer, configJsonStream, dumperSettings))
  246. {
  247. AZ_TracePrintf("Convert", "Config Settings for file %s cannot be queried from the Setting Registry", iniFilename.c_str());
  248. result = false;
  249. continue;
  250. }
  251. iniJsonDocument.Parse(configJson.c_str());
  252. }
  253. else
  254. {
  255. AZ_TracePrintf("Convert", "Config file %s contained no convertible settings, an empty JSON object anchored"
  256. " at the -json-prefix will be output", iniFilename.c_str());
  257. }
  258. }
  259. if (!isDryRun)
  260. {
  261. AZStd::string jsonDocumentRootPrefix;
  262. if (commandLine->GetNumSwitchValues("json-prefix") > 0)
  263. {
  264. jsonDocumentRootPrefix = commandLine->GetSwitchValue("json-prefix", 0);
  265. }
  266. rapidjson::StringBuffer scratchBuffer;
  267. for (auto&& [iniFilename, iniJsonDocument] : documents)
  268. {
  269. // Update the extension on the the input filename at this point
  270. AZ::IO::Path outputFilename{ AZStd::move(iniFilename) };
  271. outputFilename.ReplaceExtension(AZ::IO::PathView(outputExtension));
  272. result = WriteDocumentToDisk(outputFilename.Native(), iniJsonDocument, jsonDocumentRootPrefix, scratchBuffer) && result;
  273. scratchBuffer.Clear();
  274. }
  275. }
  276. return result;
  277. }
  278. bool Converter::VerifyConvertedData(rapidjson::Value& convertedData, const void* original, const Uuid& originalType,
  279. const JsonDeserializerSettings& settings)
  280. {
  281. using namespace AZ::JsonSerializationResult;
  282. // Need special handling if the original type is `any', because `CreateAny' creates an empty `any' in that case,
  283. // because it's not possible to store an any inside an any
  284. const bool originalTypeIsAny = originalType == azrtti_typeid<AZStd::any>();
  285. AZStd::any convertedDeserialized = settings.m_serializeContext->CreateAny(originalType);
  286. if (!originalTypeIsAny && convertedDeserialized.empty())
  287. {
  288. AZ_Printf("Convert", " Failed to deserialized from converted document.\n");
  289. return false;
  290. }
  291. // Get a storage suitable to hold this data.
  292. void* objectPtr = originalTypeIsAny ? &convertedDeserialized : AZStd::any_cast<void>(&convertedDeserialized);
  293. ResultCode loadResult = JsonSerialization::Load(objectPtr, originalType, convertedData, settings);
  294. if (loadResult.GetProcessing() == Processing::Halted)
  295. {
  296. AZ_Printf("Convert", " Failed to verify converted document because it couldn't be loaded.\n");
  297. return false;
  298. }
  299. const SerializeContext::ClassData* data = settings.m_serializeContext->FindClassData(originalType);
  300. if (!data)
  301. {
  302. AZ_Printf("Convert", " Failed to find serialization information for type '%s'.\n",
  303. originalType.ToString<AZStd::string>().c_str());
  304. return false;
  305. }
  306. bool result = false;
  307. if (data->m_serializer)
  308. {
  309. result = data->m_serializer->CompareValueData(original, objectPtr);
  310. }
  311. else
  312. {
  313. AZStd::vector<AZ::u8> originalData;
  314. AZ::IO::ByteContainerStream<decltype(originalData)> orignalStream(&originalData);
  315. AZ::Utils::SaveObjectToStream(orignalStream, AZ::ObjectStream::ST_BINARY, original, originalType);
  316. AZStd::vector<AZ::u8> loadedData;
  317. AZ::IO::ByteContainerStream<decltype(loadedData)> loadedStream(&loadedData);
  318. AZ::Utils::SaveObjectToStream(loadedStream, AZ::ObjectStream::ST_BINARY,
  319. objectPtr, originalType);
  320. result =
  321. (originalData.size() == loadedData.size()) &&
  322. (memcmp(originalData.data(), loadedData.data(), originalData.size()) == 0);
  323. }
  324. if (!result)
  325. {
  326. AZ_Printf("Convert", " Differences found between the original and converted data.\n");
  327. }
  328. return result;
  329. }
  330. AZStd::string Converter::GetClassName(const Uuid& classId, SerializeContext* context)
  331. {
  332. const SerializeContext::ClassData* data = context->FindClassData(classId);
  333. if (data)
  334. {
  335. if (data->m_editData)
  336. {
  337. return data->m_editData->m_name;
  338. }
  339. else
  340. {
  341. return data->m_name;
  342. }
  343. }
  344. else
  345. {
  346. return classId.ToString<AZStd::string>();
  347. }
  348. }
  349. bool Converter::WriteDocumentToDisk(const AZStd::string& filename, const rapidjson::Document& document,
  350. AZStd::string_view pointerRoot, rapidjson::StringBuffer& scratchBuffer)
  351. {
  352. IO::SystemFile outputFile;
  353. if (!outputFile.Open(filename.c_str(),
  354. IO::SystemFile::OpenMode::SF_OPEN_CREATE |
  355. IO::SystemFile::OpenMode::SF_OPEN_CREATE_PATH |
  356. IO::SystemFile::OpenMode::SF_OPEN_WRITE_ONLY))
  357. {
  358. AZ_Error("SerializeContextTools", false, "Unable to open output file '%s'.", filename.c_str());
  359. return false;
  360. }
  361. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(scratchBuffer);
  362. // rapidjson::Pointer constructor attempts to dereference the const char* index 0 even if the size is 0
  363. // so make sure an string_view isn't referencing a nullptr
  364. rapidjson::Pointer jsonPointerAnchor(pointerRoot.data() ? pointerRoot.data() : "", pointerRoot.size());
  365. // Anchor the content in the Json Document under the Json Pointer root path
  366. rapidjson::Document rootDocument;
  367. rapidjson::SetValueByPointer(rootDocument, jsonPointerAnchor, document);
  368. rootDocument.Accept(writer);
  369. outputFile.Write(scratchBuffer.GetString(), scratchBuffer.GetSize());
  370. outputFile.Close();
  371. scratchBuffer.Clear();
  372. return true;
  373. }
  374. void Converter::SetupLogging(AZStd::string& scratchBuffer, JsonSerializationResult::JsonIssueCallback& callback,
  375. const AZ::CommandLine& commandLine)
  376. {
  377. if (commandLine.HasSwitch("verbose"))
  378. {
  379. callback = [&scratchBuffer](
  380. AZStd::string_view message, JsonSerializationResult::ResultCode result, AZStd::string_view path)
  381. ->JsonSerializationResult::ResultCode
  382. {
  383. return VerboseLogging(scratchBuffer, message, result, path);
  384. };
  385. }
  386. else
  387. {
  388. callback = [&scratchBuffer](
  389. AZStd::string_view message, JsonSerializationResult::ResultCode result, AZStd::string_view path)
  390. ->JsonSerializationResult::ResultCode
  391. {
  392. return SimpleLogging(scratchBuffer, message, result, path);
  393. };
  394. }
  395. }
  396. AZ::JsonSerializationResult::ResultCode Converter::VerboseLogging(AZStd::string& scratchBuffer, AZStd::string_view message,
  397. AZ::JsonSerializationResult::ResultCode result, AZStd::string_view path)
  398. {
  399. scratchBuffer.append(message.begin(), message.end());
  400. scratchBuffer.append("\n Reason: ");
  401. result.AppendToString(scratchBuffer, path);
  402. scratchBuffer.append(".\n");
  403. AZ_Printf("SerializeContextTools", "%s", scratchBuffer.c_str());
  404. scratchBuffer.clear();
  405. return result;
  406. }
  407. AZ::JsonSerializationResult::ResultCode Converter::SimpleLogging(AZStd::string& scratchBuffer, AZStd::string_view message,
  408. AZ::JsonSerializationResult::ResultCode result, AZStd::string_view path)
  409. {
  410. using namespace JsonSerializationResult;
  411. if (result.GetProcessing() != Processing::Completed)
  412. {
  413. scratchBuffer.append(message.begin(), message.end());
  414. scratchBuffer.append(" @ ");
  415. scratchBuffer.append(path.begin(), path.end());
  416. scratchBuffer.append(".\n");
  417. AZ_Printf("SerializeContextTools", "%s", scratchBuffer.c_str());
  418. scratchBuffer.clear();
  419. }
  420. return result;
  421. }
  422. } // namespace SerializeContextTools
  423. } // namespace AZ