NETAssemblyFile.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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/Context.h>
  23. #include <Atomic/IO/Deserializer.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include <Atomic/Core/Profiler.h>
  27. #include <Atomic/Resource/ResourceCache.h>
  28. #include <Atomic/IO/Serializer.h>
  29. #include "NETManaged.h"
  30. #include "NETAssemblyFile.h"
  31. namespace Atomic
  32. {
  33. HashMap<StringHash, VariantType> NETAssemblyFile::typeMap_;
  34. NETAssemblyFile::NETAssemblyFile(Context* context) :
  35. ScriptComponentFile(context)
  36. {
  37. }
  38. NETAssemblyFile::~NETAssemblyFile()
  39. {
  40. }
  41. void NETAssemblyFile::InitTypeMap()
  42. {
  43. typeMap_["Boolean"] = VAR_BOOL;
  44. typeMap_["Int32"] = VAR_INT;
  45. typeMap_["Single"] = VAR_FLOAT;
  46. typeMap_["Double"] = VAR_DOUBLE;
  47. typeMap_["String"] = VAR_STRING;
  48. typeMap_["Vector2"] = VAR_VECTOR2;
  49. typeMap_["Vector3"] = VAR_VECTOR3;
  50. typeMap_["Vector4"] = VAR_VECTOR4;
  51. typeMap_["Quaternion"] = VAR_QUATERNION;
  52. }
  53. CSComponent* NETAssemblyFile::CreateCSComponent(const String& classname)
  54. {
  55. const String& name = GetName();
  56. // TODO: cache this
  57. String pathName, fileName, ext;
  58. SplitPath(name, pathName, fileName, ext);
  59. NETManaged* managed = GetSubsystem<NETManaged>();
  60. return managed->CSComponentCreate(fileName, classname);
  61. }
  62. bool NETAssemblyFile::ParseComponentClassJSON(const JSONValue& json)
  63. {
  64. if (!typeMap_.Size())
  65. InitTypeMap();
  66. String className = json.Get("name").GetString();
  67. const JSONValue& jfields = json.Get("fields");
  68. PODVector<StringHash> enumsAdded;
  69. if (jfields.IsArray())
  70. {
  71. for (unsigned i = 0; i < jfields.GetArray().Size(); i++)
  72. {
  73. const JSONValue& jfield = jfields.GetArray().At(i);
  74. VariantType varType = VAR_NONE;
  75. bool isEnum = jfield.Get("isEnum").GetBool();
  76. String typeName = jfield.Get("typeName").GetString();
  77. String fieldName = jfield.Get("name").GetString();
  78. String defaultValue = jfield.Get("defaultValue").GetString();
  79. if (isEnum && assemblyEnums_.Contains(typeName) && !enumsAdded.Contains(fieldName))
  80. {
  81. varType = VAR_INT;
  82. enumsAdded.Push(fieldName);
  83. const Vector<EnumInfo>& einfos = assemblyEnums_[typeName];
  84. for (unsigned i = 0; i < einfos.Size(); i++)
  85. AddEnum(/*typeName*/fieldName, einfos[i], className);
  86. }
  87. if (varType == VAR_NONE && typeMap_.Contains(typeName))
  88. varType = typeMap_[typeName];
  89. if (varType == VAR_NONE)
  90. {
  91. LOGERRORF("Component Class %s contains unmappable type %s in field %s",
  92. className.CString(), typeName.CString(), fieldName.CString());
  93. continue;
  94. }
  95. if (defaultValue.Length())
  96. {
  97. Variant value;
  98. value.FromString(varType, defaultValue);
  99. AddDefaultValue(fieldName, value, className);
  100. }
  101. AddField(fieldName, varType, className);
  102. }
  103. }
  104. return true;
  105. }
  106. bool NETAssemblyFile::ParseAssemblyJSON(const JSONValue& json)
  107. {
  108. Clear();
  109. assemblyEnums_.Clear();
  110. const JSONArray& enums = json.Get("enums").GetArray();
  111. // parse to all enums hash
  112. for (unsigned i = 0; i < enums.Size(); i++)
  113. {
  114. const JSONValue& ejson = enums.At(i);
  115. String enumName = ejson.Get("name").GetString();
  116. const JSONObject& evalues = ejson.Get("values").GetObject();
  117. JSONObject::ConstIterator itr = evalues.Begin();
  118. Vector<EnumInfo> values;
  119. while(itr != evalues.End())
  120. {
  121. EnumInfo info;
  122. info.name_ = itr->first_;
  123. info.value_ = itr->second_.GetInt();
  124. values.Push(info);
  125. itr++;
  126. }
  127. assemblyEnums_[enumName] = values;
  128. }
  129. const JSONArray& components = json.Get("components").GetArray();
  130. for (unsigned i = 0; i < components.Size(); i++)
  131. {
  132. const JSONValue& cjson = components.At(i);
  133. ParseComponentClassJSON(cjson);
  134. }
  135. return true;
  136. }
  137. void NETAssemblyFile::RegisterObject(Context* context)
  138. {
  139. context->RegisterFactory<NETAssemblyFile>();
  140. }
  141. bool NETAssemblyFile::BeginLoad(Deserializer& source)
  142. {
  143. return true;
  144. }
  145. bool NETAssemblyFile::Save(Serializer& dest) const
  146. {
  147. return true;
  148. }
  149. }