lua_resource.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #include "dynamic_string.h"
  25. #include "filesystem.h"
  26. #include "log.h"
  27. #include "lua_resource.h"
  28. #include "os.h"
  29. #include "temp_allocator.h"
  30. #include "array.h"
  31. #include "compile_options.h"
  32. #if defined(CROWN_DEBUG)
  33. #define LUAJIT_FLAGS "-bg" // Keep debug info
  34. #else
  35. #define LUAJIT_FLAGS "-b"
  36. #endif
  37. namespace crown
  38. {
  39. namespace lua_resource
  40. {
  41. void compile(const char* path, CompileOptions& opts)
  42. {
  43. static const uint32_t VERSION = 1;
  44. TempAllocator1024 alloc;
  45. DynamicString res_abs_path(alloc);
  46. TempAllocator1024 alloc2;
  47. DynamicString bc_abs_path(alloc2);
  48. opts.get_absolute_path(path, res_abs_path);
  49. opts.get_absolute_path("bc.tmp", bc_abs_path);
  50. const char* luajit[] =
  51. {
  52. #if CROWN_PLATFORM_LINUX
  53. "./luajit",
  54. #else
  55. "luajit.exe",
  56. #endif // CROWN_PLATFORM_LINUX
  57. LUAJIT_FLAGS,
  58. res_abs_path.c_str(),
  59. bc_abs_path.c_str(),
  60. NULL
  61. };
  62. int exitcode = os::execute_process(luajit);
  63. CE_ASSERT(exitcode == 0, "Failed to compile lua");
  64. Buffer blob = opts.read(bc_abs_path.c_str());
  65. opts.delete_file(bc_abs_path.c_str());
  66. LuaResource lr;
  67. lr.version = VERSION;
  68. lr.size = array::size(blob);
  69. opts.write(lr.version);
  70. opts.write(lr.size);
  71. opts.write(blob);
  72. }
  73. void* load(File& file, Allocator& a)
  74. {
  75. const size_t file_size = file.size();
  76. void* res = a.allocate(file_size);
  77. file.read(res, file_size);
  78. return res;
  79. }
  80. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  81. {
  82. }
  83. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  84. {
  85. }
  86. void unload(Allocator& allocator, void* resource)
  87. {
  88. allocator.deallocate(resource);
  89. }
  90. uint32_t size(const LuaResource* lr)
  91. {
  92. return lr->size;
  93. }
  94. const char* program(const LuaResource* lr)
  95. {
  96. return (char*)lr + sizeof(LuaResource);
  97. }
  98. } // namespace lua_resource
  99. } // namespace crown