LuaFile.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "LuaFile.h"
  26. #include "ProcessUtils.h"
  27. #include "Serializer.h"
  28. extern "C"
  29. {
  30. #include <lua.h>
  31. #include <lualib.h>
  32. #include <lauxlib.h>
  33. }
  34. namespace Urho3D
  35. {
  36. LuaFile::LuaFile(Context* context) :
  37. Resource(context),
  38. size_(0),
  39. hasExecuted_(false)
  40. {
  41. }
  42. LuaFile::~LuaFile()
  43. {
  44. }
  45. void LuaFile::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<LuaFile>();
  48. }
  49. bool LuaFile::Load(Deserializer& source)
  50. {
  51. size_ = source.GetSize();
  52. if (size_ == 0)
  53. return false;
  54. // Read all data.
  55. data_ = new char[size_];
  56. if (source.Read(data_, size_) != size_)
  57. return false;
  58. SetMemoryUse(size_);
  59. return true;
  60. }
  61. bool LuaFile::Save(Serializer& dest) const
  62. {
  63. if (size_ == 0)
  64. return false;
  65. dest.Write(data_, size_);
  66. return true;
  67. }
  68. bool LuaFile::Execute(lua_State* luaState)
  69. {
  70. if (hasExecuted_)
  71. return true;
  72. if (size_ == 0)
  73. return false;
  74. if (!luaState)
  75. return false;
  76. int top = lua_gettop(luaState);
  77. const char* name = GetName().CString();
  78. int error = luaL_loadbuffer(luaState, data_, size_, name);
  79. if (error)
  80. {
  81. const char* message = lua_tostring(luaState, -1);
  82. ErrorDialog("Load Buffer Failed", message);
  83. lua_settop(luaState, top);
  84. return false;
  85. }
  86. if (lua_pcall(luaState, 0, 0, 0))
  87. {
  88. const char* message = lua_tostring(luaState, -1);
  89. ErrorDialog("Lua Execute Failed", message);
  90. lua_settop(luaState, top);
  91. return false;
  92. }
  93. hasExecuted_ = true;
  94. return true;
  95. }
  96. }