LuaCompiler.cpp 1.4 KB

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