LuaFile.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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)
  76. return false;
  77. if (!luaState)
  78. return false;
  79. int top = lua_gettop(luaState);
  80. // Get file base name
  81. String name = GetName();
  82. unsigned extPos = name.FindLast('.');
  83. if (extPos != String::NPOS)
  84. {
  85. name = name.Substring(0, extPos);
  86. }
  87. int error = luaL_loadbuffer(luaState, data_, size_, name.CString());
  88. if (error)
  89. {
  90. const char* message = lua_tostring(luaState, -1);
  91. LOGERROR("Load Buffer failed for " + GetName() + ": " + String(message));
  92. lua_settop(luaState, top);
  93. return false;
  94. }
  95. LOGINFO("Loaded Lua script " + GetName());
  96. hasLoaded_ = true;
  97. return true;
  98. }
  99. bool LuaFile::LoadAndExecute(lua_State* luaState)
  100. {
  101. if (hasExecuted_)
  102. return true;
  103. if (!LoadChunk(luaState))
  104. return false;
  105. int top = lua_gettop(luaState);
  106. if (lua_pcall(luaState, 0, 0, 0))
  107. {
  108. const char* message = lua_tostring(luaState, -1);
  109. LOGERROR("Lua Execute failed for " + GetName() + ": " + String(message));
  110. lua_settop(luaState, top);
  111. return false;
  112. }
  113. hasExecuted_ = true;
  114. return true;
  115. }
  116. }