From 9ccc224f0748ccf61883f3ddd36f55eb71107b77 Mon Sep 17 00:00:00 2001 From: Josh Nave Date: Tue, 27 Apr 2021 13:12:13 -0600 Subject: [PATCH 1/6] Basic Lua module Written in C as C++ didn't seem to work, though that might've just been because I had the luaopen function in a namespace. --- CMakeLists.txt | 4 +++- client/wrapper/Lua/CMakeLists.txt | 12 ++++++++++++ client/wrapper/Lua/FMCB.lua | 11 +++++++++++ client/wrapper/Lua/Memory.c | 21 +++++++++++++++++++++ client/wrapper/Lua/Memory.h | 4 ++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 client/wrapper/Lua/FMCB.lua create mode 100644 client/wrapper/Lua/Memory.c create mode 100644 client/wrapper/Lua/Memory.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a8671b9..e10474d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,11 @@ check_ipo_supported() # maybe error check to insure version was found file(READ info.json INFO_JSON) string(JSON VERSION GET "${INFO_JSON}" version) - message("Server version is ${VERSION}") +set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install) # Should this be in /bin? +message("Install location is \"${CMAKE_INSTALL_PREFIX}\"") + set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo" CACHE STRING "") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) diff --git a/client/wrapper/Lua/CMakeLists.txt b/client/wrapper/Lua/CMakeLists.txt index e69de29..0b01295 100644 --- a/client/wrapper/Lua/CMakeLists.txt +++ b/client/wrapper/Lua/CMakeLists.txt @@ -0,0 +1,12 @@ +find_package(Lua) +include_directories(${LUA_INCLUDE_DIR}) + +add_library(LuaWrapperDef SHARED Memory.c) + +target_link_libraries(LuaWrapperDef ClientCore ${LUA_LIBRARIES}) + +set_target_properties(LuaWrapperDef PROPERTIES OUTPUT_NAME "FMCBLua") # Can this be placed in add_library? + +install(TARGETS LuaWrapperDef) + +# Should we create some kind of lua archive? \ No newline at end of file diff --git a/client/wrapper/Lua/FMCB.lua b/client/wrapper/Lua/FMCB.lua new file mode 100644 index 0000000..3fb9c2f --- /dev/null +++ b/client/wrapper/Lua/FMCB.lua @@ -0,0 +1,11 @@ +arr = {2, 5, 6} + +function printAll() + for i = 1, 3 do + print(arr[i]) + end +end + +test = require("libFMCBLua") +print(test.int_cube(3)) +printAll() \ No newline at end of file diff --git a/client/wrapper/Lua/Memory.c b/client/wrapper/Lua/Memory.c new file mode 100644 index 0000000..e5691da --- /dev/null +++ b/client/wrapper/Lua/Memory.c @@ -0,0 +1,21 @@ +#include "Memory.h" +#include "lauxlib.h" + +static int iCube(lua_State *L) { + int x = luaL_checknumber(L, 1); + + lua_pushnumber(L, x * x * x); + + return 1; // Number of return values +} + +static const luaL_Reg fmcbReg[] = { + {"int_cube", iCube}, + {NULL, NULL} +}; + +LUALIB_API int luaopen_libFMCBLua(lua_State *L) { + luaL_newlib(L, fmcbReg); // ? Is this correct? + + return 1; // ? What does Lua expect here? +} \ No newline at end of file diff --git a/client/wrapper/Lua/Memory.h b/client/wrapper/Lua/Memory.h new file mode 100644 index 0000000..cd60a28 --- /dev/null +++ b/client/wrapper/Lua/Memory.h @@ -0,0 +1,4 @@ +// ? (C++) Could we just use C instead? The API is designed for it after all. +#include "lua.h" + +LUALIB_API int luaopen_libFMCBLua(lua_State *L); // ? (C++) Should this be in a namespace? \ No newline at end of file From a2bd3a576a0418a67bcffb0dd5c78d789f06ff12 Mon Sep 17 00:00:00 2001 From: Josh Nave Date: Tue, 27 Apr 2021 15:11:31 -0600 Subject: [PATCH 2/6] Factorio refactor + Fix Fixed mod not loading and crashing game. --- Factorio/FMCB/control.lua | 14 ++++++++++++++ Factorio/FMCB/data.lua | 2 ++ Factorio/{FMCB_0.0.1 => FMCB}/info.json | 6 +++--- .../prototypes/genericReceive.lua | 0 Factorio/FMCB/prototypes/genericSend.lua | 0 Factorio/FMCB_0.0.1/Bridge.cpp | 18 ------------------ Factorio/FMCB_0.0.1/control.lua | 14 -------------- Factorio/FMCB_0.0.1/data.lua | 2 -- Factorio/FMCB_0.0.1/prototypes/genericSend.lua | 15 --------------- 9 files changed, 19 insertions(+), 52 deletions(-) create mode 100644 Factorio/FMCB/control.lua create mode 100644 Factorio/FMCB/data.lua rename Factorio/{FMCB_0.0.1 => FMCB}/info.json (68%) rename Factorio/{FMCB_0.0.1 => FMCB}/prototypes/genericReceive.lua (100%) create mode 100644 Factorio/FMCB/prototypes/genericSend.lua delete mode 100644 Factorio/FMCB_0.0.1/Bridge.cpp delete mode 100644 Factorio/FMCB_0.0.1/control.lua delete mode 100644 Factorio/FMCB_0.0.1/data.lua delete mode 100644 Factorio/FMCB_0.0.1/prototypes/genericSend.lua diff --git a/Factorio/FMCB/control.lua b/Factorio/FMCB/control.lua new file mode 100644 index 0000000..8e1ea7b --- /dev/null +++ b/Factorio/FMCB/control.lua @@ -0,0 +1,14 @@ +script.on_event({defines.events.on_tick}, + function (e) + if e.tick == 0 then + game.print("FMCB has been correctly loaded") + + -- local chest_inventory = entity.get_inventory(1) + -- local item = "coal" + + -- for s = 1, 2 do + -- chest_inventory[s].set_stack{name = item, count = game.item_prototypes[item].stack_size} + -- end + end + end +) \ No newline at end of file diff --git a/Factorio/FMCB/data.lua b/Factorio/FMCB/data.lua new file mode 100644 index 0000000..ef93153 --- /dev/null +++ b/Factorio/FMCB/data.lua @@ -0,0 +1,2 @@ +require("prototypes.genericSend") +require("prototypes.genericSend") \ No newline at end of file diff --git a/Factorio/FMCB_0.0.1/info.json b/Factorio/FMCB/info.json similarity index 68% rename from Factorio/FMCB_0.0.1/info.json rename to Factorio/FMCB/info.json index 40a74e9..8a68134 100644 --- a/Factorio/FMCB_0.0.1/info.json +++ b/Factorio/FMCB/info.json @@ -1,11 +1,11 @@ { "name": "FMCB", - "version": "0.0.1", + "version": "0.1.0", "title": "FMCB", "author": "MegaGlub & the4naves", "contact": "", "homepage": "https://github.com/Josh194/FMCB", - "factorio_version": "0.17", - "dependencies": [ "base >= 0.17" ], + "factorio_version": "1.1", + "dependencies": [], "description": "Adds the factorio FMCB subsystem." } \ No newline at end of file diff --git a/Factorio/FMCB_0.0.1/prototypes/genericReceive.lua b/Factorio/FMCB/prototypes/genericReceive.lua similarity index 100% rename from Factorio/FMCB_0.0.1/prototypes/genericReceive.lua rename to Factorio/FMCB/prototypes/genericReceive.lua diff --git a/Factorio/FMCB/prototypes/genericSend.lua b/Factorio/FMCB/prototypes/genericSend.lua new file mode 100644 index 0000000..e69de29 diff --git a/Factorio/FMCB_0.0.1/Bridge.cpp b/Factorio/FMCB_0.0.1/Bridge.cpp deleted file mode 100644 index 5ea81cd..0000000 --- a/Factorio/FMCB_0.0.1/Bridge.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include - #include "lauxlib.h" - - /* Pop-up a Windows message box with your choice of message and caption */ - int lua_msgbox(lua_State* L) - { - const char* message = luaL_checkstring(L, 1); - const char* caption = luaL_optstring(L, 2, ""); - int result = MessageBox(NULL, message, caption, MB_OK); - lua_pushnumber(L, result); - return 1; - } - - int __declspec(dllexport) libinit (lua_State* L) - { - lua_register(L, "msgbox", lua_msgbox); - return 0; - } \ No newline at end of file diff --git a/Factorio/FMCB_0.0.1/control.lua b/Factorio/FMCB_0.0.1/control.lua deleted file mode 100644 index 371198c..0000000 --- a/Factorio/FMCB_0.0.1/control.lua +++ /dev/null @@ -1,14 +0,0 @@ -script.on_event({defines.events.on_tick}, - function (e) - if e.tick == 0 then - game.print("FMCB has been correctly loaded") - - local chest_inventory = entity.get_inventory(1) - local item = "coal" - - for s = 1, 2 do - chest_inventory[s].set_stack{name = item, count = game.item_prototypes[item].stack_size} - end - end - end -) \ No newline at end of file diff --git a/Factorio/FMCB_0.0.1/data.lua b/Factorio/FMCB_0.0.1/data.lua deleted file mode 100644 index b96be56..0000000 --- a/Factorio/FMCB_0.0.1/data.lua +++ /dev/null @@ -1,2 +0,0 @@ -require("prototypes.portalSend") -require("prototypes.portalReceive") \ No newline at end of file diff --git a/Factorio/FMCB_0.0.1/prototypes/genericSend.lua b/Factorio/FMCB_0.0.1/prototypes/genericSend.lua deleted file mode 100644 index 3a64f55..0000000 --- a/Factorio/FMCB_0.0.1/prototypes/genericSend.lua +++ /dev/null @@ -1,15 +0,0 @@ -local genSend = table.deepcopy(data.raw.container) - -script.onPlaced({defines.events.on_built_entity}, - function (e) - --External Function - registerSend(e.created_entity) - end -) - -script.onDestroyed({defines.events.on_pre_player_mined_item}, - function (e) - --External Function - unregisterSend(e.entity) - end -) \ No newline at end of file From 9f3c25134cd397ee41a0ad0c6b62e77d7bd0f574 Mon Sep 17 00:00:00 2001 From: Josh Nave Date: Tue, 27 Apr 2021 18:24:31 -0600 Subject: [PATCH 3/6] Check that build is not in-source --- CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8671b9..6e82cbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,9 @@ cmake_minimum_required(VERSION 3.19) +if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR}) + message(FATAL_ERROR "Do not build in source") +endif() + project(FMCB) include(CheckIPOSupported) @@ -11,6 +15,9 @@ string(JSON VERSION GET "${INFO_JSON}" version) message("Server version is ${VERSION}") +set(GRAPHVIZ_GENERATE_PER_TARGET false) +set(GRAPHVIZ_GENERATE_DEPENDERS false) + set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo" CACHE STRING "") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) From b0408f2d00b84b53eb028cffde67ca4eb414b2b3 Mon Sep 17 00:00:00 2001 From: Josh Nave Date: Tue, 27 Apr 2021 18:27:28 -0600 Subject: [PATCH 4/6] Fix conflict mistake --- Factorio/FMCB/control.lua | 1 + Factorio/FMCB/data.lua | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Factorio/FMCB/control.lua b/Factorio/FMCB/control.lua index 8e1ea7b..9e4ee25 100644 --- a/Factorio/FMCB/control.lua +++ b/Factorio/FMCB/control.lua @@ -2,6 +2,7 @@ script.on_event({defines.events.on_tick}, function (e) if e.tick == 0 then game.print("FMCB has been correctly loaded") + game.print(fmcb.int_cube(4)) -- local chest_inventory = entity.get_inventory(1) -- local item = "coal" diff --git a/Factorio/FMCB/data.lua b/Factorio/FMCB/data.lua index ef93153..350fff9 100644 --- a/Factorio/FMCB/data.lua +++ b/Factorio/FMCB/data.lua @@ -1,2 +1,4 @@ +fmcb = require("libFMCBLua") + require("prototypes.genericSend") require("prototypes.genericSend") \ No newline at end of file From cfc3687c541ba770cb310468c507aa38a4e3d876 Mon Sep 17 00:00:00 2001 From: Josh Nave Date: Tue, 27 Apr 2021 18:33:30 -0600 Subject: [PATCH 5/6] Prepare for hotfix merge --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e10474d..c0fb0b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,9 @@ message("Server version is ${VERSION}") set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install) # Should this be in /bin? message("Install location is \"${CMAKE_INSTALL_PREFIX}\"") +set(GRAPHVIZ_GENERATE_PER_TARGET false) +set(GRAPHVIZ_GENERATE_DEPENDERS false) + set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo" CACHE STRING "") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) From 2be48fe9e96d7ed26cb81738e255df7643dcf61f Mon Sep 17 00:00:00 2001 From: Josh Nave Date: Thu, 27 May 2021 16:19:23 -0600 Subject: [PATCH 6/6] Add Zydis dependancy At least for now, we will be using Zydis for use in injection code that needs to analyze a code section. This is to allow the process to be debugged during injection (by using a jmp instead of int3). We may need to switch to something like Capstone in the future due to Zydis only supporting x86 and x86-64. --- .gitmodules | 3 +++ zydis | 1 + 2 files changed, 4 insertions(+) create mode 160000 zydis diff --git a/.gitmodules b/.gitmodules index e4b076c..9e921cd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "minparse"] path = minparse url = https://github.com/Josh194/ArgParse +[submodule "zydis"] + path = zydis + url = https://github.com/zyantific/zydis.git diff --git a/zydis b/zydis new file mode 160000 index 0000000..25193db --- /dev/null +++ b/zydis @@ -0,0 +1 @@ +Subproject commit 25193db008e8799ff59fd655c2a26b2ffd79d40d