From 3c721a4c1b977fa0fbd53dbc2e8a8a2ccf317a78 Mon Sep 17 00:00:00 2001 From: hippoz Date: Sun, 14 Feb 2021 19:38:23 +0200 Subject: [PATCH] very very very very basic lua handling/mini-wrapper --- .gitignore | 2 ++ Makefile | 39 ++++++++++++++++++++++++++++++ README.md | 3 ++- Script.lua | 1 + bin/.heygitpleasekeepthisfolder | 1 - include/LuaHandler/LuaHandler.hpp | 18 ++++++++++++++ src/.heygitpleasekeepthisfolder | 1 - src/LuaHandler/LuaHandler.cpp | 22 +++++++++++++++++ src/Main/Main.cpp | 20 +++++++++++++++ vendor/.heygitpleasekeepthisfolder | 1 - 10 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 Script.lua delete mode 100644 bin/.heygitpleasekeepthisfolder create mode 100644 include/LuaHandler/LuaHandler.hpp delete mode 100644 src/.heygitpleasekeepthisfolder create mode 100644 src/LuaHandler/LuaHandler.cpp create mode 100644 src/Main/Main.cpp delete mode 100644 vendor/.heygitpleasekeepthisfolder diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbbd0b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f8a9d0e --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +LUA_VERSION := 5.3 +LUA = lua$(LUA_VERSION) + +SRC_DIR := src +OBJ_DIR := obj +BIN_DIR := bin + +EXE := $(BIN_DIR)/out +OBJ_OUT_DIRS := obj/Main obj/LuaHandler +SRC := $(SRC_DIR)/Main/Main.cpp $(SRC_DIR)/LuaHandler/LuaHandler.cpp +OBJ := $(OBJ_DIR)/Main/Main.o $(OBJ_DIR)/LuaHandler/LuaHandler.o +CC := g++ + +CPPFLAGS := -I/usr/include/$(LUA) -Iinclude -MMD -MP +CFLAGS := -Wall +LDFLAGS := +LDLIBS := -l$(LUA) -ldl + +.PHONY: all clean run + +all: $(EXE) + +run: $(EXE) + @echo -e "\033[1m---> \033[1;35mRunning binary from $(EXE)\033[0m" + @./$(EXE) + +$(EXE): $(OBJ) | $(BIN_DIR) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR) $(OBJ_OUT_DIRS) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +$(BIN_DIR) $(OBJ_DIR) $(OBJ_OUT_DIRS): + mkdir -p $@ + +clean: + @$(RM) -rv $(BIN_DIR) $(OBJ_DIR) + +-include $(OBJ:.o=.d) \ No newline at end of file diff --git a/README.md b/README.md index 491c0fa..2dce679 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # game -yes \ No newline at end of file +## dependencies + - lua5.3 \ No newline at end of file diff --git a/Script.lua b/Script.lua new file mode 100644 index 0000000..8c29f65 --- /dev/null +++ b/Script.lua @@ -0,0 +1 @@ +libepic.say_something("lol") \ No newline at end of file diff --git a/bin/.heygitpleasekeepthisfolder b/bin/.heygitpleasekeepthisfolder deleted file mode 100644 index 9f5b814..0000000 --- a/bin/.heygitpleasekeepthisfolder +++ /dev/null @@ -1 +0,0 @@ -git please \ No newline at end of file diff --git a/include/LuaHandler/LuaHandler.hpp b/include/LuaHandler/LuaHandler.hpp new file mode 100644 index 0000000..dc9442e --- /dev/null +++ b/include/LuaHandler/LuaHandler.hpp @@ -0,0 +1,18 @@ +#ifndef _LUAHANDLER_H +#define _LUAHANDLER_H + +extern "C"{ +#include +#include +} + +struct LuaHandler { + lua_State* L; + + LuaHandler(lua_State* L) : L(L) {}; + + void defineClass(const char* className); + void defineFunction(const char* className, const char* valueName, lua_CFunction func); +}; + +#endif \ No newline at end of file diff --git a/src/.heygitpleasekeepthisfolder b/src/.heygitpleasekeepthisfolder deleted file mode 100644 index 9f5b814..0000000 --- a/src/.heygitpleasekeepthisfolder +++ /dev/null @@ -1 +0,0 @@ -git please \ No newline at end of file diff --git a/src/LuaHandler/LuaHandler.cpp b/src/LuaHandler/LuaHandler.cpp new file mode 100644 index 0000000..48bc580 --- /dev/null +++ b/src/LuaHandler/LuaHandler.cpp @@ -0,0 +1,22 @@ +#include + +void LuaHandler::defineClass(const char* className) { + // Create a new table and assign it to a global variable + lua_newtable(L); + lua_setglobal(L, className); +} + +void LuaHandler::defineFunction(const char* className, const char* valueName, lua_CFunction func) { + // Get the table that should have been defined with LuaHandler::defineClass + lua_getglobal(L, className); + + // Push the function name, as well as the function onto the stack + lua_pushstring(L, valueName); + lua_pushcfunction(L, func); + + // Add the function inside the table, giving it the specified name + lua_settable(L, -3); + + // Keep the stack balanced + lua_pop(L, 1); +} \ No newline at end of file diff --git a/src/Main/Main.cpp b/src/Main/Main.cpp new file mode 100644 index 0000000..edb154f --- /dev/null +++ b/src/Main/Main.cpp @@ -0,0 +1,20 @@ +#include + +#include + +static int say_something(lua_State* L) { + std::cout << "something: " << lua_tostring(L, 1) << "\n"; + return 1; +} + +int main() { + lua_State* L = luaL_newstate(); + LuaHandler handler(L); + + handler.defineClass("libepic"); + handler.defineFunction("libepic", "say_something", &say_something); + + luaL_dofile(handler.L, "Script.lua"); + + return 0; +} \ No newline at end of file diff --git a/vendor/.heygitpleasekeepthisfolder b/vendor/.heygitpleasekeepthisfolder deleted file mode 100644 index 9f5b814..0000000 --- a/vendor/.heygitpleasekeepthisfolder +++ /dev/null @@ -1 +0,0 @@ -git please \ No newline at end of file