JSComponentFile.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  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/Core/Profiler.h>
  26. #include <Atomic/Resource/ResourceCache.h>
  27. #include <Atomic/IO/Serializer.h>
  28. #include "JSComponentFile.h"
  29. namespace Atomic
  30. {
  31. JSComponentFile::JSComponentFile(Context* context) :
  32. Resource(context)
  33. {
  34. }
  35. JSComponentFile::~JSComponentFile()
  36. {
  37. }
  38. void JSComponentFile::RegisterObject(Context* context)
  39. {
  40. context->RegisterFactory<JSComponentFile>();
  41. }
  42. bool JSComponentFile::BeginLoad(Deserializer& source)
  43. {
  44. // TODO: cache these for non-editor builds
  45. unsigned dataSize = source.GetSize();
  46. if (!dataSize && !source.GetName().Empty())
  47. {
  48. LOGERROR("Zero sized component file in " + source.GetName());
  49. return false;
  50. }
  51. SharedArrayPtr<char> buffer(new char[dataSize + 1]);
  52. if (source.Read(buffer.Get(), dataSize) != dataSize)
  53. return false;
  54. buffer[dataSize] = '\0';
  55. String text = buffer.Get();
  56. Vector<String> lines = text.Split('\n');
  57. for (unsigned i = 0; i < lines.Size(); i++)
  58. {
  59. String line = lines[i].Trimmed();
  60. if (line[0] != '\"')
  61. continue;
  62. unsigned pos = line.Find("\";");
  63. if (pos == String::NPOS)
  64. continue;
  65. text = line.Substring(1, pos - 1);
  66. Vector<String> field = text.Split(' ');
  67. if (field.Size() != 2 || !field[0].Length() || !field[1].Length())
  68. continue;
  69. String name = field[0];
  70. String stringType = field[1];
  71. VariantType variantType = VAR_NONE;
  72. if (stringType == "boolean")
  73. {
  74. variantType = VAR_BOOL;
  75. }
  76. else if (stringType == "string")
  77. {
  78. variantType = VAR_STRING;
  79. }
  80. else if (stringType == "number")
  81. {
  82. variantType = VAR_FLOAT;
  83. }
  84. else if (stringType == "Vector3")
  85. {
  86. variantType = VAR_VECTOR3;
  87. }
  88. if (variantType == VAR_NONE)
  89. continue;
  90. fields_[name] = variantType;
  91. }
  92. SetMemoryUse(dataSize);
  93. return true;
  94. }
  95. bool JSComponentFile::Save(Serializer& dest) const
  96. {
  97. return true;
  98. }
  99. }