ScriptComponent.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // Copyright (c) 2014-2016, 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 "../Core/Context.h"
  23. #include "../IO/Log.h"
  24. #include "ScriptComponentFile.h"
  25. #include "ScriptComponent.h"
  26. namespace Atomic
  27. {
  28. ScriptComponent::ScriptComponent(Context* context) : Component(context),
  29. saving_(false),
  30. loading_(false)
  31. {
  32. }
  33. void ScriptComponent::RegisterObject(Context* context)
  34. {
  35. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  36. ATOMIC_ACCESSOR_ATTRIBUTE("FieldValues", GetFieldValuesAttr, SetFieldValuesAttr, VariantMap, Variant::emptyVariantMap, AM_FILE);
  37. }
  38. ScriptComponent::~ScriptComponent()
  39. {
  40. }
  41. bool ScriptComponent::Load(Deserializer& source, bool setInstanceDefault)
  42. {
  43. loading_ = true;
  44. bool success = Component::Load(source, setInstanceDefault);
  45. loading_ = false;
  46. return success;
  47. }
  48. bool ScriptComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  49. {
  50. loading_ = true;
  51. bool success = Component::LoadXML(source, setInstanceDefault);
  52. loading_ = false;
  53. return success;
  54. }
  55. bool ScriptComponent::Save(Serializer& dest) const
  56. {
  57. saving_ = true;
  58. bool success = Component::Save(dest);
  59. saving_ = false;
  60. return success;
  61. }
  62. bool ScriptComponent::SaveXML(XMLElement& dest) const
  63. {
  64. saving_ = true;
  65. bool success = Component::SaveXML(dest);
  66. saving_ = false;
  67. return success;
  68. }
  69. const VariantMap& ScriptComponent::GetFieldValuesAttr() const
  70. {
  71. ScriptComponentFile* componentFile = GetComponentFile();
  72. const String& classname = GetComponentClassName();
  73. if (saving_ && componentFile && classname.Length())
  74. {
  75. // When serializing enumerations, enum integer values are converted to enum strings
  76. // This makes them independent of enum values/order changing
  77. fieldValuesAttr_ = fieldValues_;
  78. const EnumMap& enums = componentFile->GetEnums(classname);
  79. VariantMap::ConstIterator itr = fieldValues_.Begin();
  80. String fieldName;
  81. while (itr != fieldValues_.End())
  82. {
  83. const StringHash& nameHash = itr->first_;
  84. const Variant& v = itr->second_;
  85. const FieldInfo* fieldInfo = 0;
  86. bool skip = false;
  87. // If we're not a numeric field, can skip
  88. if (v.GetType() != VAR_INT &&
  89. v.GetType() != VAR_FLOAT &&
  90. v.GetType() != VAR_DOUBLE)
  91. {
  92. skip = true;
  93. }
  94. else if (!StringHash::GetSignificantString(nameHash, fieldName))
  95. {
  96. ATOMIC_LOGWARNING("ScriptComponent::GetFieldValuesAttr - unable to retrieve field name");
  97. skip = true;
  98. }
  99. else if (!(fieldInfo = componentFile->GetFields(classname)[fieldName]) || !fieldInfo->isEnum_)
  100. {
  101. skip = true;
  102. }
  103. if (skip)
  104. {
  105. itr++;
  106. continue;
  107. }
  108. int intValue = v.GetInt();
  109. EnumMap::ConstIterator eitr = enums.Begin();
  110. // find corresponding enum, and convert to enum name representation
  111. while (eitr != enums.End())
  112. {
  113. if (!eitr->first_.Compare(fieldName, false))
  114. {
  115. const Vector<EnumInfo>& infos = eitr->second_;
  116. for (unsigned i = 0; i < infos.Size(); i++)
  117. {
  118. if (intValue == infos[i].value_)
  119. {
  120. // use enum name string instead of numeric value
  121. fieldValuesAttr_[nameHash] = infos[i].name_;
  122. break;
  123. }
  124. }
  125. break;
  126. }
  127. eitr++;
  128. }
  129. itr++;
  130. }
  131. return fieldValuesAttr_;
  132. }
  133. return fieldValues_;
  134. }
  135. void ScriptComponent::SetFieldValuesAttr(const VariantMap& value)
  136. {
  137. fieldValues_ = value;
  138. ScriptComponentFile* componentFile = GetComponentFile();
  139. const String& classname = GetComponentClassName();
  140. if (loading_ && componentFile && classname.Length())
  141. {
  142. // When serializing enumerations, enum integer values are converted to enum strings
  143. // This makes them independent of enum values/order changing
  144. const EnumMap& enums = componentFile->GetEnums(classname);
  145. VariantMap::ConstIterator itr = value.Begin();
  146. String fieldName;
  147. // run through field values looking for enum name strings
  148. // which need to be converted from string to numeric representation
  149. while (itr != value.End())
  150. {
  151. const StringHash& nameHash = itr->first_;
  152. const FieldInfo* fieldInfo = 0;
  153. bool skip = false;
  154. if (itr->second_.GetType() != VAR_STRING)
  155. {
  156. skip = true;
  157. }
  158. else if (!StringHash::GetSignificantString(nameHash, fieldName))
  159. {
  160. ATOMIC_LOGWARNING("ScriptComponent::SetFieldValuesAttr - unable to retrieve field name");
  161. skip = true;
  162. }
  163. else if (!(fieldInfo = componentFile->GetFields(classname)[fieldName]) || !fieldInfo->isEnum_)
  164. {
  165. skip = true;
  166. }
  167. if (skip)
  168. {
  169. itr++;
  170. continue;
  171. }
  172. EnumMap::ConstIterator eitr = enums.Begin();
  173. const String& svalue = itr->second_.GetString();
  174. // find the corresponding enum if any
  175. while (eitr != enums.End())
  176. {
  177. if (!eitr->first_.Compare(fieldName, false))
  178. {
  179. const Vector<EnumInfo>& infos = eitr->second_;
  180. // default
  181. fieldValues_[nameHash] = infos.Size() > 0 ? infos[0].value_ : 0;
  182. for (unsigned i = 0; i < infos.Size(); i++)
  183. {
  184. if (!svalue.Compare(infos[i].name_, false))
  185. {
  186. fieldValues_[nameHash] = infos[i].value_;
  187. break;
  188. }
  189. }
  190. break;
  191. }
  192. eitr++;
  193. }
  194. itr++;
  195. }
  196. }
  197. }
  198. }