package_resource.cpp 4.1 KB

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