aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2023-09-10 21:17:03 +0200
committerJan200101 <sentrycraft123@gmail.com>2023-09-10 21:17:03 +0200
commitac01bde6b9673b80dd225be3edebbe2255d9ebd2 (patch)
treeb458d57204d4c7377917a316f36db29165b9b2ae
parent94841a78b8126f0ed02db3e7066f4680031011a6 (diff)
downloadSouthRPC-ac01bde6b9673b80dd225be3edebbe2255d9ebd2.tar.gz
SouthRPC-ac01bde6b9673b80dd225be3edebbe2255d9ebd2.zip
implement plugin as class, create server class
-rw-r--r--cmake/Findjson-c.cmake83
-rw-r--r--cmake/R2plugin.cmake5
-rw-r--r--src/CMakeLists.txt8
-rw-r--r--src/init.cpp53
-rw-r--r--src/ns_plugin.h25
-rw-r--r--src/plugin.cpp24
-rw-r--r--src/plugin.h36
-rw-r--r--src/server.cpp18
-rw-r--r--src/server.h29
9 files changed, 266 insertions, 15 deletions
diff --git a/cmake/Findjson-c.cmake b/cmake/Findjson-c.cmake
new file mode 100644
index 0000000..99e2586
--- /dev/null
+++ b/cmake/Findjson-c.cmake
@@ -0,0 +1,83 @@
+#
+# Tries to find json-c through the config
+# before trying to query for it
+#
+#
+
+if (json-c_FOUND)
+ return()
+endif()
+
+find_package(json-c CONFIG)
+
+if (JSONC_FOUND)
+ return()
+endif()
+
+find_package(PkgConfig QUIET)
+if (PKG_CONFIG_FOUND)
+ pkg_check_modules(_JSONC json-c)
+
+ if (BUILD_STATIC AND NOT _JSONC_FOUND)
+ message(FATAL_ERROR "Cannot find static build information")
+ endif()
+ set(json-c_FOUND 1)
+endif()
+
+if (json-c_FOUND) # we can rely on pkg-config
+ set(json-c_LINK_LIBRARIES ${_JSONC_LINK_LIBRARIES})
+ if (NOT BUILD_STATIC)
+ set(json-c_INCLUDE_DIRS ${_JSONC_INCLUDE_DIRS})
+ set(json-c_CFLAGS ${_JSONC_CFLAGS_OTHER})
+ else()
+ set(json-c_INCLUDE_DIRS ${_JSONC_STATIC_INCLUDE_DIRS})
+ set(json-c_CFLAGS ${_JSONC_STATIC_CFLAGS_OTHER})
+ endif()
+else()
+ if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ set(_lib_suffix 64)
+ else()
+ set(_lib_suffix 32)
+ endif()
+
+ find_path(JSONC_INC
+ NAMES json.h
+ HINTS
+ ENV jsoncPath${_lib_suffix}
+ ENV jsoncPath
+ ${_JSONC_INCLUDE_DIRS}
+ PATHS
+ /usr/include/json-c /usr/local/include/json-c)
+
+ find_library(JSONC_LIB
+ NAMES ${_JSONC_LIBRARIES} jsonc json-c
+ HINTS
+ ENV jsoncPath${_lib_suffix}
+ ENV jsoncPath
+ ${_JSONC_LIBRARY_DIRS}
+ ${_JSONC_STATIC_LIBRARY_DIRS}
+ PATHS
+ /usr/lib{_lib_suffix} /usr/local/lib{_lib_suffix}
+ /usr/lib /usr/local/lib)
+
+ include(FindPackageHandleStandardArgs)
+ find_package_handle_standard_args(json-c DEFAULT_MSG JSONC_LIB JSONC_INC)
+ mark_as_advanced(JSONC_INC JSONC_LIB)
+
+ if(json-c_FOUND)
+ set(json-c_INCLUDE_DIRS ${JSONC_INC})
+ set(json-c_LINK_LIBRARIES ${JSONC_LIB})
+ if (BUILD_STATIC)
+ set(json-c_LINK_LIBRARIES ${json-c_LINK_LIBRARIES} ${_JSONC_STATIC_LIBRARIES})
+ endif()
+ endif()
+endif()
+
+if (json-c_FOUND)
+ add_library(json-c::json-c UNKNOWN IMPORTED)
+ set_target_properties(json-c::json-c PROPERTIES
+ IMPORTED_LOCATION "${json-c_LINK_LIBRARIES}"
+ )
+ target_compile_definitions(json-c::json-c INTERFACE ${json-c_CFLAGS})
+ target_include_directories(json-c::json-c INTERFACE ${json-c_INCLUDE_DIRS})
+endif()
diff --git a/cmake/R2plugin.cmake b/cmake/R2plugin.cmake
index 107a981..584ef05 100644
--- a/cmake/R2plugin.cmake
+++ b/cmake/R2plugin.cmake
@@ -9,8 +9,6 @@ cmake_policy(SET CMP0057 NEW)
project(R2plugin)
-find_package(spdlog REQUIRED)
-find_package(NorthstarPluginABI REQUIRED)
if (NOT WIN32)
message(FATAL_ERROR "Northstar Plugins can only be compiled for Windows")
@@ -23,6 +21,9 @@ if (__R2PLUGIN_CMAKE_INCLUDED)
endif()
set(__R2PLUGIN_CMAKE_INCLUDED TRUE)
+find_package(spdlog REQUIRED)
+find_package(NorthstarPluginABI REQUIRED)
+
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(CheckLinkerFlag)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index bddc237..5a7c651 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -2,6 +2,7 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
find_package(spdlog REQUIRED)
+find_package(json-c REQUIRED)
plugin_manifest(SouthRPC name "SouthRPC")
plugin_manifest(SouthRPC displayname "SouthRPC")
@@ -11,8 +12,13 @@ plugin_manifest(SouthRPC run_on_client ON)
add_library(SouthRPC SHARED
${CMAKE_CURRENT_SOURCE_DIR}/init.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/plugin.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/plugin.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/server.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/server.h
)
-target_precompile_headers(SouthRPC PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/plugin.h)
+target_link_libraries(SouthRPC json-c::json-c)
+target_precompile_headers(SouthRPC PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/ns_plugin.h)
plugin_link(SouthRPC)
diff --git a/src/init.cpp b/src/init.cpp
index 856af41..165c574 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1,12 +1,59 @@
-// Needed to bootstrap plugin abi
-#include "plugin.h"
+#include "ns_plugin.h"
#include "internal/logging.h"
+#include "server.h"
+#include "plugin.h"
+
+Plugin* plugin = nullptr;
+
extern "C" __declspec(dllexport)
void PLUGIN_INIT(PluginInitFuncs* funcs, PluginNorthstarData* data)
{
spdlog::default_logger()->sinks().pop_back();
spdlog::default_logger()->sinks().push_back(std::make_shared<PluginSink>(funcs->logger));
- spdlog::info(PLUGIN_NAME " succesfully initialised!");
+ plugin = new Plugin(funcs, data);
+}
+
+extern "C" __declspec(dllexport)
+void PLUGIN_DEINIT()
+{
+ if (plugin)
+ {
+ delete plugin;
+ plugin = nullptr;
+ }
+}
+
+extern "C" __declspec(dllexport)
+void PLUGIN_INFORM_DLL_LOAD(PluginLoadDLL dll, void* data) {
+ switch (dll) {
+ case PluginLoadDLL::ENGINE:
+ plugin->LoadEngineData(data);
+ break;
+ case PluginLoadDLL::CLIENT:
+ case PluginLoadDLL::SERVER:
+ break;
+ default:
+ spdlog::warn("PLUGIN_INFORM_DLL_LOAD called with unknown type {}", (int)dll);
+ break;
+ }
+}
+
+// There is no deinit logic for Plugins
+// Recreate it using DllMain
+BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
+{
+ switch (ul_reason_for_call)
+ {
+ case DLL_PROCESS_DETACH:
+ PLUGIN_DEINIT();
+
+ case DLL_PROCESS_ATTACH:
+ case DLL_THREAD_ATTACH:
+ case DLL_THREAD_DETACH:
+ break;
+ }
+
+ return TRUE;
}
diff --git a/src/ns_plugin.h b/src/ns_plugin.h
new file mode 100644
index 0000000..f1567a4
--- /dev/null
+++ b/src/ns_plugin.h
@@ -0,0 +1,25 @@
+#ifndef NS_PLUGIN_H
+#define NS_PLUGIN_H
+
+// Needed to bootstrap plugin abi
+#include <windef.h>
+#include <mutex>
+#include <optional>
+#include <map>
+#include <unordered_map>
+#include <spdlog/spdlog.h>
+
+#include "plugin_abi.h"
+// ConCommand/ConVar types
+#include "core/macros.h"
+#include "core/convar/convar.h"
+#include "core/convar/concommand.h"
+
+// This is a mess
+// hope Plugins V3 includes these in the ABI
+// pls cat :womp:
+typedef void (*ConCommandConstructorType)(ConCommand* newCommand, const char* name, FnCommandCallback_t callback, const char* helpString, int flags, void* parent);
+typedef void (*ConVarMallocType)(void* pConVarMaloc, int a2, int a3);
+typedef void (*ConVarRegisterType)(ConVar* pConVar, const char* pszName, const char* pszDefaultValue, int nFlags, const char* pszHelpString, bool bMin, float fMin, bool bMax, float fMax, void* pCallback);
+
+#endif
diff --git a/src/plugin.cpp b/src/plugin.cpp
new file mode 100644
index 0000000..a1098da
--- /dev/null
+++ b/src/plugin.cpp
@@ -0,0 +1,24 @@
+#include "ns_plugin.h"
+#include "plugin.h"
+#include "server.h"
+
+Plugin::Plugin(PluginInitFuncs* funcs, PluginNorthstarData* data)
+ : server(new jsonrpc_server(this))
+{
+ this->funcs = funcs;
+ this->data = data;
+
+ spdlog::info(PLUGIN_NAME " initialised!");
+}
+
+Plugin::~Plugin()
+{
+ delete server;
+}
+
+void Plugin::LoadEngineData(void* data)
+{
+ this->engine_data = static_cast<EngineData*>(data);
+
+ spdlog::info("Engine data loaded");
+} \ No newline at end of file
diff --git a/src/plugin.h b/src/plugin.h
index c346cee..e22798b 100644
--- a/src/plugin.h
+++ b/src/plugin.h
@@ -1,14 +1,32 @@
#ifndef PLUGIN_H
#define PLUGIN_H
-// Needed to bootstrap plugin abi
-#include <windef.h>
-#include <mutex>
-#include <optional>
-#include <map>
-#include <unordered_map>
-#include "spdlog/spdlog.h"
+#include "ns_plugin.h"
-#include "plugin_abi.h"
+class jsonrpc_server;
-#endif
+struct EngineData
+{
+ ConCommandConstructorType ConCommandConstructor;
+ ConVarMallocType conVarMalloc;
+ ConVarRegisterType conVarRegister;
+ void* ConVar_Vtable;
+ void* IConVar_Vtable;
+};
+
+class Plugin {
+ private:
+ PluginInitFuncs* funcs = nullptr;
+ PluginNorthstarData* data = nullptr;
+ EngineData* engine_data = nullptr;
+
+ jsonrpc_server* server;
+
+ public:
+ Plugin(PluginInitFuncs* funcs, PluginNorthstarData* data);
+ ~Plugin();
+
+ void LoadEngineData(void* data);
+};
+
+#endif \ No newline at end of file
diff --git a/src/server.cpp b/src/server.cpp
new file mode 100644
index 0000000..b9f7b74
--- /dev/null
+++ b/src/server.cpp
@@ -0,0 +1,18 @@
+#include <winsock2.h>
+#include <ws2tcpip.h>
+
+#include <spdlog/spdlog.h>
+
+#include "server.h"
+#include "plugin.h"
+
+jsonrpc_server::jsonrpc_server(Plugin* plugin)
+{
+ this->parent = plugin;
+ spdlog::info("jsonrpc_server::jsonrpc_server()");
+}
+
+jsonrpc_server::~jsonrpc_server()
+{
+ spdlog::info("jsonrpc_server::~jsonrpc_server()");
+} \ No newline at end of file
diff --git a/src/server.h b/src/server.h
new file mode 100644
index 0000000..a3414f6
--- /dev/null
+++ b/src/server.h
@@ -0,0 +1,29 @@
+#ifndef SERVER_H
+#define SERVER_H
+
+#include <winsock2.h>
+#include <ws2tcpip.h>
+
+#define RPC_PORT 26503
+#define MAX_CONNECTIONS 5
+
+class Plugin;
+
+struct thread_info
+{
+ HANDLE thread_handle;
+ SOCKET socket_fd;
+};
+
+class jsonrpc_server {
+ private:
+ Plugin* parent;
+ struct thread_info threads[MAX_CONNECTIONS] = {0};
+
+ public:
+ jsonrpc_server(Plugin* plugin);
+ ~jsonrpc_server();
+
+};
+
+#endif \ No newline at end of file