LuaFile.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Copyright (c) 2008-2013 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 "Context.h"
  24. #include "Deserializer.h"
  25. #include "Log.h"
  26. #include "LuaFile.h"
  27. #include "ProcessUtils.h"
  28. #include "Serializer.h"
  29. extern "C"
  30. {
  31. #include <lua.h>
  32. #include <lualib.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. hasExecuted_(false)
  42. {
  43. }
  44. LuaFile::~LuaFile()
  45. {
  46. }
  47. void LuaFile::RegisterObject(Context* context)
  48. {
  49. context->RegisterFactory<LuaFile>();
  50. }
  51. bool LuaFile::Load(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::Execute(lua_State* luaState)
  71. {
  72. if (hasExecuted_)
  73. return true;
  74. if (size_ == 0)
  75. return false;
  76. if (!luaState)
  77. return false;
  78. int top = lua_gettop(luaState);
  79. const char* name = GetName().CString();
  80. int error = luaL_loadbuffer(luaState, data_, size_, name);
  81. if (error)
  82. {
  83. const char* message = lua_tostring(luaState, -1);
  84. LOGERROR("Load Buffer Failed: " + String(message));
  85. lua_settop(luaState, top);
  86. return false;
  87. }
  88. if (lua_pcall(luaState, 0, 0, 0))
  89. {
  90. const char* message = lua_tostring(luaState, -1);
  91. LOGERROR("Lua Execute Failed: " + String(message));
  92. lua_settop(luaState, top);
  93. return false;
  94. }
  95. hasExecuted_ = true;
  96. return true;
  97. }
  98. }