NETAssemblyImporter.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/CSComponentAssembly.h>
  28. #include "Asset.h"
  29. #include "AssetDatabase.h"
  30. #include "../NETTools/AtomicNETService.h"
  31. #include "NETAssemblyImporter.h"
  32. namespace ToolCore
  33. {
  34. NETAssemblyImporter::NETAssemblyImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
  35. {
  36. requiresCacheFile_ = false;
  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. 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. LOGERRORF("NETAssemblyImporter::HandleResult - Unable to parse assembly %s", asset_->GetPath().CString());
  68. }
  69. else
  70. {
  71. asset_->Save();
  72. }
  73. }
  74. else
  75. {
  76. LOGERRORF("NETAssemblyImporter::HandleResult - Unable to get CSComponentAssembly resource for %s", asset_->GetPath().CString());
  77. }
  78. }
  79. }
  80. }
  81. bool NETAssemblyImporter::Import()
  82. {
  83. AtomicNETService* service = GetSubsystem<AtomicNETService>();
  84. if (!service)
  85. {
  86. LOGERRORF("NETAssemblyImporter::Import - Unable to get AtomicNETService subsystem importing %s", asset_->GetPath().CString());
  87. return false;
  88. }
  89. assemblyJSON_.SetType(JSON_NULL);
  90. VariantMap cmdMap;
  91. cmdMap["command"] = "parse";
  92. cmdMap["assemblyPath"] = asset_->GetPath();
  93. service->QueueCommand(resultHandler_, cmdMap);
  94. return true;
  95. }
  96. bool NETAssemblyImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  97. {
  98. if (!AssetImporter::LoadSettingsInternal(jsonRoot))
  99. return false;
  100. JSONValue import = jsonRoot.Get("NETAssemblyImporter");
  101. assemblyJSON_.SetType(JSON_NULL);
  102. const JSONValue& ajson = import.Get("AssemblyJSON");
  103. if (ajson.IsObject())
  104. {
  105. assemblyJSON_ = ajson.GetObject();
  106. ResourceCache* cache = GetSubsystem<ResourceCache>();
  107. CSComponentAssembly* assemblyFile = cache->GetResource<CSComponentAssembly>(asset_->GetPath());
  108. if (assemblyFile)
  109. assemblyFile->ParseAssemblyJSON(assemblyJSON_);
  110. }
  111. return true;
  112. }
  113. bool NETAssemblyImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  114. {
  115. if (!AssetImporter::SaveSettingsInternal(jsonRoot))
  116. return false;
  117. JSONValue import;
  118. import.SetType(JSON_OBJECT);
  119. import.Set("AssemblyJSON", assemblyJSON_);
  120. jsonRoot.Set("NETAssemblyImporter", import);
  121. return true;
  122. }
  123. Resource* NETAssemblyImporter::GetResource(const String& typeName)
  124. {
  125. ResourceCache* cache = GetSubsystem<ResourceCache>();
  126. CSComponentAssembly* assemblyFile = cache->GetResource<CSComponentAssembly>(asset_->GetPath());
  127. return assemblyFile;
  128. }
  129. // NETAssemblyImporterClient
  130. NETAssemblyImporterResultHandler::NETAssemblyImporterResultHandler(Context* context, NETAssemblyImporter* importer) :
  131. IPCResultHandler(context),
  132. importer_(importer)
  133. {
  134. }
  135. NETAssemblyImporterResultHandler::~NETAssemblyImporterResultHandler()
  136. {
  137. }
  138. void NETAssemblyImporterResultHandler::HandleResult(unsigned cmdID, const VariantMap& cmdResult)
  139. {
  140. importer_->HandleResult(cmdID, cmdResult);
  141. }
  142. }