This commit is contained in:
Lily Tsuru 2023-07-19 03:18:34 -04:00
parent e3ffcb02c2
commit 16ac661643
10 changed files with 113 additions and 62 deletions

16
build_module.sh Normal file
View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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()

View File

@ -1,10 +1,13 @@
#include "SourceSink.hpp"
#include <cstdlib>
#include <lucore/Assert.hpp>
#include <lucore/Library.hpp>
#include <lucore/Types.hpp>
// 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<T>(#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<T>(#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() {

View File

@ -4,10 +4,16 @@
#include <lucore/Assert.hpp>
LUA_FUNCTION(lcpu_native_test) {
}
GMOD_MODULE_OPEN() {
lucore::Logger::The().AttachSink(lcpu::SourceSink::The());
lucore::LogInfo("LCPU Native Module loading");
return 0;
}

View File

@ -1,3 +1,5 @@
#pragma once
#include <string_view>
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);

View File

@ -1,5 +1,6 @@
//! Logging utilities for Lucore
//! Using Standard C++ <format>
#pragma once
#include <chrono>
#include <cstdint>

View File

@ -1,3 +1,5 @@
#pragma once
#include <lucore/Logger.hpp>
namespace lucore {

View File

@ -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) {