From 16ac6616431a532e994920d16967375ebb08c87c Mon Sep 17 00:00:00 2001 From: modeco80 Date: Wed, 19 Jul 2023 03:18:34 -0400 Subject: [PATCH] cleanup --- build_module.sh | 16 ++++ lua/autorun/lcpu_load.lua | 9 +-- native/projects/lcpu/CMakeLists.txt | 29 +------- native/projects/lcpu/gmod_headers.cmake | 26 +++++++ native/projects/lcpu/src/SourceSink.cpp | 73 +++++++++++++------ native/projects/lcpu/src/main.cpp | 6 ++ .../lucore/include/lucore/Library.hpp | 6 +- .../projects/lucore/include/lucore/Logger.hpp | 1 + .../lucore/include/lucore/StdoutSink.hpp | 2 + native/projects/lucore/src/Library.cpp | 7 +- 10 files changed, 113 insertions(+), 62 deletions(-) create mode 100644 build_module.sh create mode 100644 native/projects/lcpu/gmod_headers.cmake diff --git a/build_module.sh b/build_module.sh new file mode 100644 index 0000000..48198c3 --- /dev/null +++ b/build_module.sh @@ -0,0 +1,16 @@ +#!/bin/bash + + +# Build the LCPU addon for the reccomended environment + +set -x + + +cmake -Wno-dev -S native -B build -DCMAKE_BUILD_TYPE=Release +ninja -C build + +[[ ! -d ''../../lua/bin' ]] && { + mkdir -p ../../lua/bin +} + +cp -v build/projects/lcpu/*.dll ../../lua/bin diff --git a/lua/autorun/lcpu_load.lua b/lua/autorun/lcpu_load.lua index 04fafa5..be069d4 100644 --- a/lua/autorun/lcpu_load.lua +++ b/lua/autorun/lcpu_load.lua @@ -1,10 +1,3 @@ -- skeleton load file to get gmod to recognize this as an addon -- this will contain files later on in life. - -print("hello world?") - --- detect if wiremod is installed -if not istable(WireLib) then - print("[LCPU] we need wiremod..") - return -end +AddCSLuaFile() diff --git a/native/projects/lcpu/CMakeLists.txt b/native/projects/lcpu/CMakeLists.txt index c6f0cf7..7b60104 100644 --- a/native/projects/lcpu/CMakeLists.txt +++ b/native/projects/lcpu/CMakeLists.txt @@ -1,31 +1,4 @@ - - -add_library(gmod_headers INTERFACE) -target_include_directories(gmod_headers INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gmod_headers/include) - -# Originally from facepunch cmake build system, modified to be slightly less painful -function(set_gmod_suffix_prefix library) - set_target_properties(${library} PROPERTIES PREFIX "gmsv_") - if(APPLE) - if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") - set_target_properties(${library} PROPERTIES SUFFIX "_osx.dll") - else() - set_target_properties(${library} PROPERTIES SUFFIX "_osx64.dll") - endif() - elseif(UNIX) - if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") - set_target_properties(${library} PROPERTIES SUFFIX "_linux.dll") - else() - set_target_properties(${library} PROPERTIES SUFFIX "_linux64.dll") - endif() - elseif(WIN32) - if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") - set_target_properties(${library} PROPERTIES SUFFIX "_win32.dll") - else() - set_target_properties(${library} PROPERTIES SUFFIX "_win64.dll") - endif() - endif() -endfunction() +include(./gmod_headers.cmake) add_library(lcpu_native SHARED diff --git a/native/projects/lcpu/gmod_headers.cmake b/native/projects/lcpu/gmod_headers.cmake new file mode 100644 index 0000000..aab62fe --- /dev/null +++ b/native/projects/lcpu/gmod_headers.cmake @@ -0,0 +1,26 @@ +add_library(gmod_headers INTERFACE) +target_include_directories(gmod_headers INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gmod_headers/include) + +# Originally from facepunch cmake build system, modified to be slightly less painful +function(set_gmod_suffix_prefix library) + set_target_properties(${library} PROPERTIES PREFIX "gmsv_") + if(APPLE) + if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") + set_target_properties(${library} PROPERTIES SUFFIX "_osx.dll") + else() + set_target_properties(${library} PROPERTIES SUFFIX "_osx64.dll") + endif() + elseif(UNIX) + if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") + set_target_properties(${library} PROPERTIES SUFFIX "_linux.dll") + else() + set_target_properties(${library} PROPERTIES SUFFIX "_linux64.dll") + endif() + elseif(WIN32) + if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") + set_target_properties(${library} PROPERTIES SUFFIX "_win32.dll") + else() + set_target_properties(${library} PROPERTIES SUFFIX "_win64.dll") + endif() + endif() +endfunction() diff --git a/native/projects/lcpu/src/SourceSink.cpp b/native/projects/lcpu/src/SourceSink.cpp index ddbeabc..80a60e9 100644 --- a/native/projects/lcpu/src/SourceSink.cpp +++ b/native/projects/lcpu/src/SourceSink.cpp @@ -1,10 +1,13 @@ #include "SourceSink.hpp" +#include +#include #include #include // The old non-beta branch of GMod on Linux has multiple tier0 libraries for client and server. -// This compatibility define allows to support that case (for now). +// This compatibility define allows to support that case (for now). Once this define is removed, +// the old codepath can be totally removed. #define LCPU_SUPPORT_OLD_GMOD namespace tier0 { @@ -14,6 +17,44 @@ namespace tier0 { using Msg_t = void (*)(const char*, ...); Msg_t Msg {}; + bool OpenLibrary() { +#ifdef LCPU_SUPPORT_OLD_GMOD + constexpr static std::string_view tier0_libraries[] { + #ifdef __linux__ + "tier0_srv", + #endif + "tier0" + }; + + for(auto lib : tier0_libraries) { + if(lucore::Library::Loaded(lib)) { + // Found the correct tier0 library to open; use that. + tier0::library = lucore::Library::OpenExisting(lib); + break; + } + } +#else + // The x86-64 branch of GMod, including the 32-bit binaries in the branch, + // have a single tier0 library, which makes the codepath much simpler. + // Hopefully I can switch to this path at some point. + tier0::library = lucore::Library::OpenExisting("tier0"); +#endif + + if(tier0::library == nullptr) + return false; + return true; + } + + bool GrabSymbols() { +#define GRAB_SYMBOL(name, T) \ + name = library->Symbol(#name); \ + if(name == nullptr) \ + return false; + GRAB_SYMBOL(Msg, Msg_t); +#undef GRAB_SYMBOL + return true; + } + } // namespace tier0 namespace lcpu { @@ -24,29 +65,17 @@ namespace lcpu { } SourceSink::SourceSink() { -#ifdef LCPU_SUPPORT_OLD_GMOD - constexpr static std::string_view tier0_libraries[] { -#ifdef __linux__ - "tier0_srv" -#endif - "tier0" - }; - - for(auto lib : tier0_libraries) { - if(lucore::Library::Loaded(lib)) { - // Found the correct tier0 library to open; use that. - tier0::library = lucore::Library::Open(lib); - break; - } + if(!tier0::OpenLibrary()) { + std::printf("Tier0 could not be opened\n"); + std::quick_exit(10); } -#else - // The x86-64 branch of GMod, including the 32-bit binaries in the branch, - // have a single tier0 library, which makes the codepath much simpler. - tier0::library = lucore::Library::Open("tier0"); -#endif -#define GRAB_SYMBOL(name, T) tier0::name = tier0::library->Symbol(#name); - GRAB_SYMBOL(Msg, tier0::Msg_t); + // TODO: A bit nicer of an error message? + // Explain *what* to do if you see this message. + if(!tier0::GrabSymbols()) { + std::printf("Tier0 symbols could not be grabbed\n"); + std::quick_exit(10); + } } SourceSink::~SourceSink() { diff --git a/native/projects/lcpu/src/main.cpp b/native/projects/lcpu/src/main.cpp index 23fb61f..fc0ebc9 100644 --- a/native/projects/lcpu/src/main.cpp +++ b/native/projects/lcpu/src/main.cpp @@ -4,10 +4,16 @@ #include +LUA_FUNCTION(lcpu_native_test) { + +} + GMOD_MODULE_OPEN() { lucore::Logger::The().AttachSink(lcpu::SourceSink::The()); lucore::LogInfo("LCPU Native Module loading"); + + return 0; } diff --git a/native/projects/lucore/include/lucore/Library.hpp b/native/projects/lucore/include/lucore/Library.hpp index 808c045..7378d31 100644 --- a/native/projects/lucore/include/lucore/Library.hpp +++ b/native/projects/lucore/include/lucore/Library.hpp @@ -1,3 +1,5 @@ +#pragma once + #include namespace lucore { @@ -5,8 +7,8 @@ namespace lucore { struct Library { using Handle = void*; - /// Create a new library instance. - static Library* Open(std::string_view dllname); + /// Open an already loaded library + static Library* OpenExisting(std::string_view dllname); /// Query if [dllname] is loaded in the process. static bool Loaded(std::string_view dllname); diff --git a/native/projects/lucore/include/lucore/Logger.hpp b/native/projects/lucore/include/lucore/Logger.hpp index 77eeec4..ce6bd9e 100644 --- a/native/projects/lucore/include/lucore/Logger.hpp +++ b/native/projects/lucore/include/lucore/Logger.hpp @@ -1,5 +1,6 @@ //! Logging utilities for Lucore //! Using Standard C++ +#pragma once #include #include diff --git a/native/projects/lucore/include/lucore/StdoutSink.hpp b/native/projects/lucore/include/lucore/StdoutSink.hpp index b6eeefa..e22580b 100644 --- a/native/projects/lucore/include/lucore/StdoutSink.hpp +++ b/native/projects/lucore/include/lucore/StdoutSink.hpp @@ -1,3 +1,5 @@ +#pragma once + #include namespace lucore { diff --git a/native/projects/lucore/src/Library.cpp b/native/projects/lucore/src/Library.cpp index 5899b2d..e95f7da 100644 --- a/native/projects/lucore/src/Library.cpp +++ b/native/projects/lucore/src/Library.cpp @@ -16,8 +16,11 @@ namespace lucore { } } // namespace - Library* Library::Open(std::string_view dllname) { - return new Library(detail::OsOpenLibrary(FormatLibraryName(dllname).c_str())); + Library* Library::OpenExisting(std::string_view dllname) { + auto name = FormatLibraryName(dllname); + if(!detail::OsLibraryLoaded(name.c_str())) + return nullptr; + return new Library(detail::OsOpenLibrary(name.c_str())); } bool Library::Loaded(std::string_view dllname) {