NETAssemblyImporter.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_ = false;
  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. LOGERRORF("NETAssemblyImporter::HandleResult - Unable to parse assembly json for %s", asset_->GetPath().CString());
  59. }
  60. else
  61. {
  62. ResourceCache* cache = GetSubsystem<ResourceCache>();
  63. CSComponentAssembly* assemblyFile = cache->GetResource<CSComponentAssembly>(asset_->GetPath());
  64. if (assemblyFile)
  65. {
  66. if (!assemblyFile->ParseAssemblyJSON(assemblyJSON_))
  67. {
  68. LOGERRORF("NETAssemblyImporter::HandleResult - Unable to parse assembly %s", asset_->GetPath().CString());
  69. }
  70. else
  71. {
  72. asset_->Save();
  73. using namespace CSComponentAssemblyChanged;
  74. VariantMap assemblyChangedEventData;
  75. assemblyChangedEventData[P_RESOURCE] = asset_->GetResource();
  76. assemblyChangedEventData[P_ASSEMBLYPATH] = asset_->GetPath();
  77. SendEvent(E_CSCOMPONENTASSEMBLYCHANGED, assemblyChangedEventData);
  78. }
  79. }
  80. else
  81. {
  82. LOGERRORF("NETAssemblyImporter::HandleResult - Unable to get CSComponentAssembly resource for %s", asset_->GetPath().CString());
  83. }
  84. }
  85. }
  86. }
  87. bool NETAssemblyImporter::Import()
  88. {
  89. AtomicNETService* service = GetSubsystem<AtomicNETService>();
  90. if (!service)
  91. {
  92. LOGERRORF("NETAssemblyImporter::Import - Unable to get AtomicNETService subsystem importing %s", asset_->GetPath().CString());
  93. return false;
  94. }
  95. assemblyJSON_.SetType(JSON_NULL);
  96. VariantMap cmdMap;
  97. cmdMap["command"] = "parse";
  98. cmdMap["assemblyPath"] = asset_->GetPath();
  99. service->QueueCommand(resultHandler_, cmdMap);
  100. return true;
  101. }
  102. bool NETAssemblyImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  103. {
  104. if (!AssetImporter::LoadSettingsInternal(jsonRoot))
  105. return false;
  106. JSONValue import = jsonRoot.Get("NETAssemblyImporter");
  107. assemblyJSON_.SetType(JSON_NULL);
  108. const JSONValue& ajson = import.Get("AssemblyJSON");
  109. if (ajson.IsObject())
  110. {
  111. assemblyJSON_ = ajson.GetObject();
  112. ResourceCache* cache = GetSubsystem<ResourceCache>();
  113. CSComponentAssembly* assemblyFile = cache->GetResource<CSComponentAssembly>(asset_->GetPath());
  114. if (assemblyFile)
  115. assemblyFile->ParseAssemblyJSON(assemblyJSON_);
  116. }
  117. return true;
  118. }
  119. bool NETAssemblyImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  120. {
  121. if (!AssetImporter::SaveSettingsInternal(jsonRoot))
  122. return false;
  123. JSONValue import;
  124. import.SetType(JSON_OBJECT);
  125. import.Set("AssemblyJSON", assemblyJSON_);
  126. jsonRoot.Set("NETAssemblyImporter", import);
  127. return true;
  128. }
  129. Resource* NETAssemblyImporter::GetResource(const String& typeName)
  130. {
  131. ResourceCache* cache = GetSubsystem<ResourceCache>();
  132. CSComponentAssembly* assemblyFile = cache->GetResource<CSComponentAssembly>(asset_->GetPath());
  133. return assemblyFile;
  134. }
  135. // NETAssemblyImporterClient
  136. NETAssemblyImporterResultHandler::NETAssemblyImporterResultHandler(Context* context, NETAssemblyImporter* importer) :
  137. IPCResultHandler(context),
  138. importer_(importer)
  139. {
  140. }
  141. NETAssemblyImporterResultHandler::~NETAssemblyImporterResultHandler()
  142. {
  143. }
  144. void NETAssemblyImporterResultHandler::HandleResult(unsigned cmdID, const VariantMap& cmdResult)
  145. {
  146. importer_->HandleResult(cmdID, cmdResult);
  147. }
  148. }