Skip to content

Instantly share code, notes, and snippets.

@fre-sch
Last active March 28, 2020 11:53
Show Gist options
  • Save fre-sch/7824902c2de61f607c34294a0a7b22cb to your computer and use it in GitHub Desktop.
Save fre-sch/7824902c2de61f607c34294a0a7b22cb to your computer and use it in GitHub Desktop.
luabindingsexplore
-- scripts/first.lua
myvars = {}
Hooks.Init = function ()
print("first.lua Hooks.Init")
myvars.foo = "first foo"
end
Hooks.MonsterHit = function (e)
print("first.lua MonsterHit", e.monster.health)
garglebla()
print("first.lua myvars.foo", myvars.foo)
myprint("first.lua has access to globals")
end
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <sol/sol.hpp>
#include <filesystem>
#pragma comment(lib, "User32.lib")
namespace fs = std::filesystem;
static sol::state LUA;
static std::unordered_map<std::string, sol::environment> SCRIPTENV;
struct Monster {
private:
int _health = 10;
public:
int getHealth() {
return _health;
}
int health() const {
return _health;
}
void health(int value) {
_health = value;
}
};
void fn(Monster* m) {
std::cout << m->health() << std::endl;
}
std::string bools(bool value) {
return value ? "true" : "false";
}
void myprint(std::string message) {
std::cout << "printing in C, from lua " << message << std::endl;
}
sol::protected_function_result logScriptError(
lua_State* luaState,
sol::protected_function_result pfr) {
sol::error err = pfr;
std::cout << "error: " << err.what() << std::endl;
return pfr;
}
void initScript(std::string path) {
SCRIPTENV[path] = sol::environment(LUA, sol::create, LUA.globals());
sol::environment& env = SCRIPTENV[path];
env["Hooks"] = sol::new_table();
LUA.safe_script_file(path, env, logScriptError);
sol::optional<sol::protected_function> var = env["Hooks"]["Init"];
if (var != sol::nullopt) {
sol::protected_function initFunction = *var;
env.set_on(initFunction);
initFunction();
}
}
void initLuaScripts() {
fs::path scriptDir = fs::current_path() / "scripts";
for (const auto& dirEntry : fs::directory_iterator(scriptDir)) {
if (dirEntry.is_regular_file()
&& dirEntry.path().filename().string().ends_with(".lua")) {
initScript(dirEntry.path().string());
}
}
}
void runLuaHook(std::string hook, Monster* m) {
for (const auto& entry : SCRIPTENV) {
const sol::environment& env = entry.second;
sol::optional<sol::protected_function> hookVar = env["Hooks"][hook];
if (hookVar != sol::nullopt) {
sol::protected_function hookFunction = *hookVar;
env.set_on(hookFunction);
sol::table args = LUA.create_table_with(
"monster", m
);
hookFunction(args);
}
}
}
int main() {
LUA = sol::state();
LUA.open_libraries();
sol::usertype<Monster> monsterType = LUA.new_usertype<Monster>(
"Monster", sol::no_constructor);
monsterType["getHealth"] = &Monster::getHealth;
monsterType["health"] = sol::property(
sol::resolve<int() const>(&Monster::health),
sol::resolve<void(int)>(&Monster::health)
);
LUA["myprint"] = &myprint;
initLuaScripts();
struct { int _health = 1337; } notmonster;
runLuaHook("MonsterHit", reinterpret_cast<Monster*>(&notmonster));
}
-- scripts/second.lua
myvars = {}
Hooks.Init = function ()
print("second.lua Hooks.Init")
myvars.foo = "second foo"
end
Hooks.MonsterHit = function (e)
print("second.lua MonsterHit", e.monster.health)
print("second.lua myvars.foo", myvars.foo)
myprint("second.lua has access to globals")
print("second.lua getHealth", e.monster:getHealth())
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment