summaryrefslogtreecommitdiffstats
path: root/src/control/ScriptContext.hpp
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-09-27 16:25:26 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-09-27 16:25:26 +0200
commita5f288ca3f4621ad60ed270a2e95ef8acc6c90d2 (patch)
treef597639777fb7a96dad2765b294a53016c65b947 /src/control/ScriptContext.hpp
parent80eccfbdb29c75d8c2d3de9ed98e686a52dd9c86 (diff)
downloadrpgedit-a5f288ca3f4621ad60ed270a2e95ef8acc6c90d2.tar
rpgedit-a5f288ca3f4621ad60ed270a2e95ef8acc6c90d2.zip
Add Lua scripting context
Diffstat (limited to 'src/control/ScriptContext.hpp')
-rw-r--r--src/control/ScriptContext.hpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/control/ScriptContext.hpp b/src/control/ScriptContext.hpp
new file mode 100644
index 0000000..34dc8fc
--- /dev/null
+++ b/src/control/ScriptContext.hpp
@@ -0,0 +1,102 @@
+/*
+ Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#pragma once
+
+extern "C" {
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+}
+
+#include <memory>
+#include <string>
+
+
+namespace RPGEdit {
+
+namespace Control {
+
+class ScriptContext {
+private:
+ lua_State *L;
+
+public:
+ ScriptContext(const ScriptContext &other) = delete;
+ ScriptContext(ScriptContext &&other) = delete;
+ ScriptContext & operator=(const ScriptContext &other) = delete;
+ ScriptContext & operator=(ScriptContext &&other) = delete;
+
+ ScriptContext() {
+ const std::pair<const char *, lua_CFunction> libs[] = {
+ {"_G", luaopen_base},
+ {"bit32", luaopen_bit32},
+ {"math", luaopen_math},
+ {"string", luaopen_string},
+ {"table", luaopen_table},
+ };
+
+ L = luaL_newstate();
+
+ for (auto &lib : libs) {
+ luaL_requiref(L, lib.first, lib.second, 1);
+ lua_pop(L, 1);
+ }
+
+ for (const char *f : {"dofile", "loadfile", "require"}) {
+ lua_pushnil(L);
+ lua_setglobal(L, f);
+ }
+
+ lua_newtable(L);
+ lua_rawsetp(L, LUA_REGISTRYINDEX, this);
+ }
+
+ ~ScriptContext() {
+ lua_close(L);
+ }
+
+ void load(const std::string &name) {
+ std::string filename = "../resources/script/" + name + ".lua";
+
+ lua_rawgetp(L, LUA_REGISTRYINDEX, this);
+ lua_pushstring(L, name.c_str());
+ luaL_loadfile(L, filename.c_str());
+ lua_rawset(L, -3);
+ lua_pop(L, 1);
+ }
+
+ void run(const std::string &name) {
+ lua_rawgetp(L, LUA_REGISTRYINDEX, this);
+ lua_getfield(L, -1, name.c_str());
+ lua_remove(L, -2);
+ lua_call(L, 0, 0);
+ }
+};
+
+}
+
+}