NETAssemblyImporter.cpp 6.2 KB

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