lua_resource.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #if CROWN_PLATFORM_WINDOWS
  31. #define LUAJIT_EXECUTABLE "luajit.exe"
  32. #else
  33. #define LUAJIT_EXECUTABLE "./luajit"
  34. #endif
  35. #if defined(CROWN_DEBUG)
  36. #define LUAJIT_FLAGS "-bg" // Keep debug info
  37. #else
  38. #define LUAJIT_FLAGS "-b"
  39. #endif
  40. namespace crown
  41. {
  42. namespace lua_resource
  43. {
  44. //-----------------------------------------------------------------------------
  45. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  46. {
  47. TempAllocator1024 alloc;
  48. DynamicString res_abs_path(alloc);
  49. TempAllocator1024 alloc2;
  50. DynamicString bc_abs_path(alloc2);
  51. fs.get_absolute_path(resource_path, res_abs_path);
  52. fs.get_absolute_path("bc.tmp", bc_abs_path);
  53. const char* luajit[] =
  54. {
  55. LUAJIT_EXECUTABLE,
  56. LUAJIT_FLAGS,
  57. res_abs_path.c_str(),
  58. bc_abs_path.c_str(),
  59. NULL
  60. };
  61. os::execute_process(luajit);
  62. size_t program_size = 0;
  63. char* program = NULL;
  64. File* bc = fs.open(bc_abs_path.c_str(), FOM_READ);
  65. if (bc != NULL)
  66. {
  67. program_size = bc->size();
  68. program = (char*) default_allocator().allocate(program_size);
  69. bc->read(program, program_size);
  70. fs.close(bc);
  71. fs.delete_file(bc_abs_path.c_str());
  72. }
  73. else
  74. {
  75. CE_LOGE("Error while reading luajit bytecode");
  76. return;
  77. }
  78. LuaHeader header;
  79. header.version = LUA_RESOURCE_VERSION;
  80. header.size = program_size;
  81. out_file->write((char*)&header, sizeof(LuaHeader));
  82. out_file->write((char*)program, program_size);
  83. default_allocator().deallocate(program);
  84. }
  85. //-----------------------------------------------------------------------------
  86. void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  87. {
  88. File* file = bundle.open(id);
  89. const size_t file_size = file->size();
  90. void* res = allocator.allocate(file_size);
  91. file->read(res, file_size);
  92. bundle.close(file);
  93. return res;
  94. }
  95. //-----------------------------------------------------------------------------
  96. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  97. {
  98. }
  99. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  100. {
  101. }
  102. //-----------------------------------------------------------------------------
  103. void unload(Allocator& allocator, void* resource)
  104. {
  105. allocator.deallocate(resource);
  106. }
  107. } // namespace lua_resource
  108. } // namespace crown