LuaFile.cpp 3.4 KB

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