NETAssemblyImporter.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Core/Variant.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/Resource/ResourceCache.h>
  26. #include <Atomic/Resource/Image.h>
  27. #include <AtomicNET/NETScript/NETScriptEvents.h>
  28. #include <AtomicNET/NETScript/CSComponentAssembly.h>
  29. #include "Asset.h"
  30. #include "AssetDatabase.h"
  31. #include "../NETTools/AtomicNETService.h"
  32. #include "NETAssemblyImporter.h"
  33. namespace ToolCore
  34. {
  35. NETAssemblyImporter::NETAssemblyImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
  36. {
  37. requiresCacheFile_ = true;
  38. resultHandler_ = new NETAssemblyImporterResultHandler(context, this);
  39. }
  40. NETAssemblyImporter::~NETAssemblyImporter()
  41. {
  42. }
  43. void NETAssemblyImporter::SetDefaults()
  44. {
  45. AssetImporter::SetDefaults();
  46. }
  47. void NETAssemblyImporter::HandleResult(unsigned cmdID, const VariantMap& cmdResult)
  48. {
  49. const String& command = cmdResult["command"]->GetString();
  50. if (command == "parse")
  51. {
  52. // Assembly parse results
  53. const String& resultJSON = cmdResult["result"]->GetString();
  54. assemblyJSON_.SetType(JSON_NULL);
  55. JSONFile::ParseJSON(resultJSON, assemblyJSON_);
  56. if (!assemblyJSON_.IsObject())
  57. {
  58. ATOMIC_LOGERRORF("NETAssemblyImporter::HandleResult - Unable to parse assembly json for %s", asset_->GetPath().CString());
  59. }
  60. else
  61. {
  62. if (!SaveAssemblyCacheFile())
  63. {
  64. return;
  65. }
  66. if (!assemblyJSON_.IsObject())
  67. {
  68. ATOMIC_LOGERRORF("NETAssemblyImporter::HandleResult - Unable to parse assembly json for %s", asset_->GetPath().CString());
  69. }
  70. using namespace CSComponentAssemblyChanged;
  71. VariantMap assemblyChangedEventData;
  72. assemblyChangedEventData[P_RESOURCE] = asset_->GetResource();
  73. assemblyChangedEventData[P_ASSEMBLYPATH] = asset_->GetPath();
  74. SendEvent(E_CSCOMPONENTASSEMBLYCHANGED, assemblyChangedEventData);
  75. }
  76. }
  77. }
  78. bool NETAssemblyImporter::SaveAssemblyCacheFile()
  79. {
  80. if (!assemblyJSON_.IsObject())
  81. return false;
  82. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  83. JSONValue& root = jsonFile->GetRoot();
  84. root = assemblyJSON_;
  85. jsonFile->SaveFile(asset_->GetCachePath());
  86. ResourceCache* cache = GetSubsystem<ResourceCache>();
  87. CSComponentAssembly* assemblyFile = cache->GetResource<CSComponentAssembly>(asset_->GetCachePath());
  88. if (!assemblyFile)
  89. {
  90. ATOMIC_LOGERRORF("NETAssemblyImporter::Import - invalid cached assembly json importing %s", asset_->GetPath().CString());
  91. return false;
  92. }
  93. asset_->Save();
  94. return true;
  95. }
  96. bool NETAssemblyImporter::Import()
  97. {
  98. AtomicNETService* service = GetSubsystem<AtomicNETService>();
  99. if (!service)
  100. {
  101. if (assemblyJSON_.IsObject())
  102. {
  103. if (!SaveAssemblyCacheFile())
  104. return false;
  105. ATOMIC_LOGINFOF("NETAssemblyImporter::Import - AtomicNETService process unavailable, using cached assembly JSON for %s", asset_->GetPath().CString());
  106. return true;
  107. }
  108. else
  109. {
  110. ATOMIC_LOGERRORF("NETAssemblyImporter::Import - Unable to get AtomicNETService subsystem and no cached assembly json importing %s", asset_->GetPath().CString());
  111. return false;
  112. }
  113. }
  114. assemblyJSON_.SetType(JSON_NULL);
  115. VariantMap cmdMap;
  116. cmdMap["command"] = "parse";
  117. cmdMap["assemblyPath"] = asset_->GetPath();
  118. service->QueueCommand(resultHandler_, cmdMap);
  119. return true;
  120. }
  121. bool NETAssemblyImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  122. {
  123. if (!AssetImporter::LoadSettingsInternal(jsonRoot))
  124. return false;
  125. JSONValue import = jsonRoot.Get("NETAssemblyImporter");
  126. assemblyJSON_.SetType(JSON_NULL);
  127. const JSONValue& ajson = import.Get("AssemblyJSON");
  128. if (ajson.IsObject())
  129. {
  130. assemblyJSON_ = ajson.GetObject();
  131. ResourceCache* cache = GetSubsystem<ResourceCache>();
  132. cache->GetResource<CSComponentAssembly>(asset_->GetCachePath());
  133. }
  134. return true;
  135. }
  136. bool NETAssemblyImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  137. {
  138. if (!AssetImporter::SaveSettingsInternal(jsonRoot))
  139. return false;
  140. JSONValue import;
  141. import.SetType(JSON_OBJECT);
  142. import.Set("AssemblyJSON", assemblyJSON_);
  143. jsonRoot.Set("NETAssemblyImporter", import);
  144. return true;
  145. }
  146. Resource* NETAssemblyImporter::GetResource(const String& typeName)
  147. {
  148. ResourceCache* cache = GetSubsystem<ResourceCache>();
  149. CSComponentAssembly* assemblyFile = cache->GetResource<CSComponentAssembly>(asset_->GetCachePath());
  150. return assemblyFile;
  151. }
  152. void NETAssemblyImporter::GetAssetCacheMap(HashMap<String, String>& assetMap)
  153. {
  154. if (asset_.Null())
  155. return;
  156. String assetPath = asset_->GetRelativePath().ToLower();
  157. String cachePath = asset_->GetGUID().ToLower();
  158. // Default is load node xml
  159. assetMap["CSComponentAssembly;" + assetPath] = cachePath;
  160. }
  161. // NETAssemblyImporterClient
  162. NETAssemblyImporterResultHandler::NETAssemblyImporterResultHandler(Context* context, NETAssemblyImporter* importer) :
  163. IPCResultHandler(context),
  164. importer_(importer)
  165. {
  166. }
  167. NETAssemblyImporterResultHandler::~NETAssemblyImporterResultHandler()
  168. {
  169. }
  170. void NETAssemblyImporterResultHandler::HandleResult(unsigned cmdID, const VariantMap& cmdResult)
  171. {
  172. importer_->HandleResult(cmdID, cmdResult);
  173. }
  174. }