2
0

package_resource.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "core/containers/array.h"
  6. #include "core/filesystem/file.h"
  7. #include "core/filesystem/filesystem.h"
  8. #include "core/filesystem/reader_writer.h"
  9. #include "core/json/json_object.h"
  10. #include "core/json/sjson.h"
  11. #include "core/memory/temp_allocator.h"
  12. #include "core/strings/dynamic_string.h"
  13. #include "core/strings/string_id.h"
  14. #include "resource/compile_options.h"
  15. #include "resource/package_resource.h"
  16. namespace crown
  17. {
  18. namespace package_resource_internal
  19. {
  20. void compile_resources(const char* type, const JsonArray& names, Array<PackageResource::Resource>& output, CompileOptions& opts)
  21. {
  22. const StringId64 typeh = StringId64(type);
  23. for (u32 i = 0; i < array::size(names); ++i)
  24. {
  25. TempAllocator1024 ta;
  26. DynamicString name(ta);
  27. sjson::parse_string(names[i], name);
  28. DATA_COMPILER_ASSERT_RESOURCE_EXISTS(type, name.c_str(), opts);
  29. const StringId64 nameh = sjson::parse_resource_id(names[i]);
  30. array::push_back(output, PackageResource::Resource(typeh, nameh));
  31. }
  32. }
  33. void compile(CompileOptions& opts)
  34. {
  35. Buffer buf = opts.read();
  36. TempAllocator4096 ta;
  37. JsonObject object(ta);
  38. sjson::parse(buf, object);
  39. JsonArray texture(ta);
  40. JsonArray script(ta);
  41. JsonArray sound(ta);
  42. JsonArray mesh(ta);
  43. JsonArray unit(ta);
  44. JsonArray sprite(ta);
  45. JsonArray material(ta);
  46. JsonArray font(ta);
  47. JsonArray level(ta);
  48. JsonArray phyconf(ta);
  49. JsonArray shader(ta);
  50. JsonArray sprite_animation(ta);
  51. if (json_object::has(object, "texture")) sjson::parse_array(object["texture"], texture);
  52. if (json_object::has(object, "lua")) sjson::parse_array(object["lua"], script);
  53. if (json_object::has(object, "sound")) sjson::parse_array(object["sound"], sound);
  54. if (json_object::has(object, "mesh")) sjson::parse_array(object["mesh"], mesh);
  55. if (json_object::has(object, "unit")) sjson::parse_array(object["unit"], unit);
  56. if (json_object::has(object, "sprite")) sjson::parse_array(object["sprite"], sprite);
  57. if (json_object::has(object, "material")) sjson::parse_array(object["material"], material);
  58. if (json_object::has(object, "font")) sjson::parse_array(object["font"], font);
  59. if (json_object::has(object, "level")) sjson::parse_array(object["level"], level);
  60. if (json_object::has(object, "physics_config")) sjson::parse_array(object["physics_config"], phyconf);
  61. if (json_object::has(object, "shader")) sjson::parse_array(object["shader"], shader);
  62. if (json_object::has(object, "sprite_animation")) sjson::parse_array(object["sprite_animation"], sprite_animation);
  63. Array<PackageResource::Resource> resources(default_allocator());
  64. compile_resources("texture", texture, resources, opts);
  65. compile_resources("lua", script, resources, opts);
  66. compile_resources("sound", sound, resources, opts);
  67. compile_resources("mesh", mesh, resources, opts);
  68. compile_resources("unit", unit, resources, opts);
  69. compile_resources("sprite", sprite, resources, opts);
  70. compile_resources("material", material, resources, opts);
  71. compile_resources("font", font, resources, opts);
  72. compile_resources("level", level, resources, opts);
  73. compile_resources("physics_config", phyconf, resources, opts);
  74. compile_resources("shader", shader, resources, opts);
  75. compile_resources("sprite_animation", sprite_animation, resources, opts);
  76. // Write
  77. opts.write(RESOURCE_VERSION_PACKAGE);
  78. opts.write(array::size(resources));
  79. for (u32 i = 0; i < array::size(resources); ++i)
  80. {
  81. opts.write(resources[i].type);
  82. opts.write(resources[i].name);
  83. }
  84. }
  85. void* load(File& file, Allocator& a)
  86. {
  87. BinaryReader br(file);
  88. u32 version;
  89. br.read(version);
  90. CE_ASSERT(version == RESOURCE_VERSION_PACKAGE, "Wrong version");
  91. u32 num_resources;
  92. br.read(num_resources);
  93. PackageResource* pr = CE_NEW(a, PackageResource)(a);
  94. array::resize(pr->resources, num_resources);
  95. br.read(array::begin(pr->resources), sizeof(PackageResource::Resource)*num_resources);
  96. return pr;
  97. }
  98. void unload(Allocator& a, void* resource)
  99. {
  100. CE_DELETE(a, (PackageResource*)resource);
  101. }
  102. } // namespace package_resource_internal
  103. } // namespace crown