LuaCompiler.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "LuaCompiler.h"
  2. #include "FileStream.h"
  3. namespace crown
  4. {
  5. //-----------------------------------------------------------------------------
  6. LuaCompiler::LuaCompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
  7. Compiler(root_path, dest_path, resource, seed),
  8. m_file_size(0),
  9. m_file_data(NULL)
  10. {
  11. }
  12. //-----------------------------------------------------------------------------
  13. bool LuaCompiler::compile()
  14. {
  15. Filesystem fs(root_path());
  16. char tmp_resource[os::MAX_PATH_LENGTH];
  17. string::strcpy(tmp_resource, resource_path());
  18. string::strcat(tmp_resource, ".script");
  19. if (!fs.exists(tmp_resource))
  20. {
  21. os::printf("Resource cannot be found.\n");
  22. return false;
  23. }
  24. FileStream* file = (FileStream*)fs.open(tmp_resource, SOM_READ);
  25. m_file_size = file->size();
  26. if (m_file_size == 0)
  27. {
  28. return false;
  29. }
  30. m_file_data = new char[m_file_size];
  31. // Copy the entire file into the buffer
  32. file->read(m_file_data, m_file_size);
  33. // Prepare for writing
  34. Compiler::prepare_header(m_file_size);
  35. return true;
  36. }
  37. //-----------------------------------------------------------------------------
  38. void LuaCompiler::write()
  39. {
  40. Compiler::write_header();
  41. FileStream* file = Compiler::destination_file();
  42. file->write(&m_file_size, sizeof(uint32_t));
  43. file->write(m_file_data, m_file_size);
  44. }
  45. } // namespace crown