LuaFile.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Copyright (c) 2008-2017 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() = default;
  46. void LuaFile::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<LuaFile>();
  49. }
  50. bool LuaFile::BeginLoad(Deserializer& source)
  51. {
  52. size_ = source.GetSize();
  53. if (size_ == 0)
  54. return false;
  55. // Read all data.
  56. data_ = new char[size_];
  57. if (source.Read(data_, size_) != size_)
  58. return false;
  59. SetMemoryUse(size_);
  60. return true;
  61. }
  62. bool LuaFile::Save(Serializer& dest) const
  63. {
  64. if (size_ == 0)
  65. return false;
  66. dest.Write(data_, size_);
  67. return true;
  68. }
  69. bool LuaFile::LoadChunk(lua_State* luaState)
  70. {
  71. if (hasLoaded_)
  72. return true;
  73. if (size_ == 0 || !luaState)
  74. return false;
  75. // Get file base name
  76. String name = GetName();
  77. unsigned extPos = name.FindLast('.');
  78. if (extPos != String::NPOS)
  79. name = name.Substring(0, extPos);
  80. if (luaL_loadbuffer(luaState, data_, size_, name.CString()))
  81. {
  82. const char* message = lua_tostring(luaState, -1);
  83. URHO3D_LOGERRORF("Load Buffer failed for %s: %s", GetName().CString(), message);
  84. lua_pop(luaState, 1);
  85. return false;
  86. }
  87. URHO3D_LOGINFO("Loaded Lua script " + GetName());
  88. hasLoaded_ = true;
  89. return true;
  90. }
  91. bool LuaFile::LoadAndExecute(lua_State* luaState)
  92. {
  93. if (hasExecuted_)
  94. return true;
  95. if (!LoadChunk(luaState))
  96. return false;
  97. if (lua_pcall(luaState, 0, 0, 0))
  98. {
  99. const char* message = lua_tostring(luaState, -1);
  100. URHO3D_LOGERRORF("Lua Execute failed for %s: %s", GetName().CString(), message);
  101. lua_pop(luaState, 1);
  102. return false;
  103. }
  104. URHO3D_LOGINFO("Executed Lua script " + GetName());
  105. hasExecuted_ = true;
  106. return true;
  107. }
  108. }