LuaFile.cpp 3.4 KB

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