LuaFile.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/Deserializer.h"
  25. #include "../IO/FileSystem.h"
  26. #include "../IO/Log.h"
  27. #include "../LuaScript/LuaFile.h"
  28. #include "../Core/ProcessUtils.h"
  29. #include "../IO/Serializer.h"
  30. extern "C"
  31. {
  32. #include <lua.h>
  33. #include <lauxlib.h>
  34. }
  35. #include "../DebugNew.h"
  36. namespace Urho3D
  37. {
  38. LuaFile::LuaFile(Context* context) :
  39. Resource(context),
  40. size_(0),
  41. hasLoaded_(false),
  42. hasExecuted_(false)
  43. {
  44. }
  45. LuaFile::~LuaFile()
  46. {
  47. }
  48. void LuaFile::RegisterObject(Context* context)
  49. {
  50. context->RegisterFactory<LuaFile>();
  51. }
  52. bool LuaFile::BeginLoad(Deserializer& source)
  53. {
  54. size_ = source.GetSize();
  55. if (size_ == 0)
  56. return false;
  57. // Read all data.
  58. data_ = new char[size_];
  59. if (source.Read(data_, size_) != size_)
  60. return false;
  61. SetMemoryUse(size_);
  62. return true;
  63. }
  64. bool LuaFile::Save(Serializer& dest) const
  65. {
  66. if (size_ == 0)
  67. return false;
  68. dest.Write(data_, size_);
  69. return true;
  70. }
  71. bool LuaFile::LoadChunk(lua_State* luaState)
  72. {
  73. if (hasLoaded_)
  74. return true;
  75. if (size_ == 0 || !luaState)
  76. return false;
  77. int top = lua_gettop(luaState);
  78. // Get file base name
  79. String name = GetName();
  80. unsigned extPos = name.FindLast('.');
  81. if (extPos != String::NPOS)
  82. {
  83. name = name.Substring(0, extPos);
  84. }
  85. int error = luaL_loadbuffer(luaState, data_, size_, name.CString());
  86. if (error)
  87. {
  88. const char* message = lua_tostring(luaState, -1);
  89. LOGERROR("Load Buffer failed for " + GetName() + ": " + String(message));
  90. lua_settop(luaState, top);
  91. return false;
  92. }
  93. LOGINFO("Loaded Lua script " + GetName());
  94. hasLoaded_ = true;
  95. return true;
  96. }
  97. bool LuaFile::LoadAndExecute(lua_State* luaState)
  98. {
  99. if (hasExecuted_)
  100. return true;
  101. if (!LoadChunk(luaState))
  102. return false;
  103. if (lua_pcall(luaState, 0, 0, 0))
  104. {
  105. const char* message = lua_tostring(luaState, -1);
  106. LOGERROR("Lua Execute failed for " + GetName() + ": " + String(message));
  107. lua_pop(luaState, 1);
  108. return false;
  109. }
  110. LOGINFO("Executed Lua script " + GetName());
  111. hasExecuted_ = true;
  112. return true;
  113. }
  114. }