LuaFile.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "../Core/Context.h"
  23. #include "../IO/Deserializer.h"
  24. #include "../IO/FileSystem.h"
  25. #include "../IO/Log.h"
  26. #include "../LuaScript/LuaFile.h"
  27. #include "../Core/ProcessUtils.h"
  28. #include "../IO/Serializer.h"
  29. extern "C"
  30. {
  31. #include <lua.h>
  32. #include <lauxlib.h>
  33. }
  34. #include "../DebugNew.h"
  35. namespace Urho3D
  36. {
  37. LuaFile::LuaFile(Context* context) :
  38. Resource(context),
  39. size_(0),
  40. hasLoaded_(false),
  41. hasExecuted_(false)
  42. {
  43. }
  44. LuaFile::~LuaFile()
  45. {
  46. }
  47. void LuaFile::RegisterObject(Context* context)
  48. {
  49. context->RegisterFactory<LuaFile>();
  50. }
  51. bool LuaFile::BeginLoad(Deserializer& source)
  52. {
  53. size_ = source.GetSize();
  54. if (size_ == 0)
  55. return false;
  56. // Read all data.
  57. data_ = new char[size_];
  58. if (source.Read(data_, size_) != size_)
  59. return false;
  60. SetMemoryUse(size_);
  61. return true;
  62. }
  63. bool LuaFile::Save(Serializer& dest) const
  64. {
  65. if (size_ == 0)
  66. return false;
  67. dest.Write(data_, size_);
  68. return true;
  69. }
  70. bool LuaFile::LoadChunk(lua_State* luaState)
  71. {
  72. if (hasLoaded_)
  73. return true;
  74. if (size_ == 0)
  75. return false;
  76. if (!luaState)
  77. return false;
  78. int top = lua_gettop(luaState);
  79. // Get file base name
  80. String name = GetName();
  81. unsigned extPos = name.FindLast('.');
  82. if (extPos != String::NPOS)
  83. {
  84. name = name.Substring(0, extPos);
  85. }
  86. int error = luaL_loadbuffer(luaState, data_, size_, name.CString());
  87. if (error)
  88. {
  89. const char* message = lua_tostring(luaState, -1);
  90. LOGERROR("Load Buffer failed for " + GetName() + ": " + String(message));
  91. lua_settop(luaState, top);
  92. return false;
  93. }
  94. LOGINFO("Loaded Lua script " + GetName());
  95. hasLoaded_ = true;
  96. return true;
  97. }
  98. bool LuaFile::LoadAndExecute(lua_State* luaState)
  99. {
  100. if (hasExecuted_)
  101. return true;
  102. if (!LoadChunk(luaState))
  103. return false;
  104. int top = lua_gettop(luaState);
  105. if (lua_pcall(luaState, 0, 0, 0))
  106. {
  107. const char* message = lua_tostring(luaState, -1);
  108. LOGERROR("Lua Execute failed for " + GetName() + ": " + String(message));
  109. lua_settop(luaState, top);
  110. return false;
  111. }
  112. hasExecuted_ = true;
  113. return true;
  114. }
  115. }