aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmake/R2plugin.cmake2
m---------deps/NorthstarLauncher0
-rw-r--r--src/handler.cpp26
-rw-r--r--src/init.cpp17
-rw-r--r--src/internal/concommandproxy.h8
-rw-r--r--src/internal/convarproxy.h4
-rw-r--r--src/internal/types.h2
-rw-r--r--src/ns_plugin.h14
-rw-r--r--src/plugin.cpp25
-rw-r--r--src/plugin.h6
-rw-r--r--src/rpc_server.cpp3
-rw-r--r--src/rpc_server.h3
12 files changed, 51 insertions, 59 deletions
diff --git a/cmake/R2plugin.cmake b/cmake/R2plugin.cmake
index cd665a6..c76404e 100644
--- a/cmake/R2plugin.cmake
+++ b/cmake/R2plugin.cmake
@@ -134,7 +134,7 @@ set(MANIFEST_TEMPLATE "
\"name\": \"NSPlugin\",
\"displayname\": \"Northstar Plugin\",
\"description\": \"\",
- \"api_version\": \"2\",
+ \"api_version\": \"3\",
\"version\": \"0\",
\"run_on_server\": false,
\"run_on_client\": false
diff --git a/deps/NorthstarLauncher b/deps/NorthstarLauncher
-Subproject d463a434746cab295c4db57001650b623bd481b
+Subproject 6f091ed0ffcd1f6a94e13d2647bed0d58c63fe4
diff --git a/src/handler.cpp b/src/handler.cpp
index 56aea76..5dea515 100644
--- a/src/handler.cpp
+++ b/src/handler.cpp
@@ -1,6 +1,7 @@
#include <winsock2.h>
#include "handler.h"
+#include "helper.h"
#include "plugin.h"
#include "internal/convarproxy.h"
@@ -106,18 +107,21 @@ void ServerHandler::run()
if (!request)
continue;
- std::string method_name = request->get_method();
-
- callback_list::const_iterator method_pos = this->methods.find(method_name);
- if (method_pos != this->methods.end())
- {
- spdlog::info("Invoked method '{}'", method_name);
- request->result(method_pos->second(request->get_allocator(), request->get_params()));
- }
- else
+ if (request->is_valid())
{
- spdlog::error("Attempted to invoke unknown method '{}'", method_name);
- request->error(rapidjson::Value("Unknown method"));
+ std::string method_name = request->get_method();
+
+ callback_list::const_iterator method_pos = this->methods.find(method_name);
+ if (method_pos != this->methods.end())
+ {
+ spdlog::info("Invoked method '{}'", method_name);
+ request->result(method_pos->second(request->get_allocator(), request->get_params()));
+ }
+ else
+ {
+ spdlog::error("Attempted to invoke unknown method '{}'", method_name);
+ request->error(rapidjson::Value("Unknown method"));
+ }
}
delete request;
diff --git a/src/init.cpp b/src/init.cpp
index 7ecc84a..36aea76 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -24,20 +24,13 @@ void PLUGIN_DEINIT()
}
extern "C" __declspec(dllexport)
-void PLUGIN_INFORM_DLL_LOAD(PluginLoadDLL dll, void* data) {
+void PLUGIN_INFORM_DLL_LOAD(const char* dll, PluginEngineData* data, void* dllPtr) {
assert(plugin);
- switch (dll) {
- case PluginLoadDLL::ENGINE:
- plugin->LoadEngineData(data);
- plugin->StartServer();
- case PluginLoadDLL::CLIENT:
- break;
- case PluginLoadDLL::SERVER:
- break;
- default:
- spdlog::warn("PLUGIN_INFORM_DLL_LOAD called with unknown type {}", (int)dll);
- break;
+ if (!strcmp(dll, "engine.dll"))
+ {
+ plugin->LoadEngineData(data, (HMODULE)dllPtr);
+ plugin->StartServer();
}
}
diff --git a/src/internal/concommandproxy.h b/src/internal/concommandproxy.h
index 145339a..406675e 100644
--- a/src/internal/concommandproxy.h
+++ b/src/internal/concommandproxy.h
@@ -8,7 +8,7 @@
class ConCommandProxy : public ClassProxy<ConCommand> {
private:
const char* name;
- FnCommandCallback_t callback;
+ PluginFnCommandCallback_t callback;
const char* helpString;
int flags;
void* parent;
@@ -16,7 +16,7 @@ class ConCommandProxy : public ClassProxy<ConCommand> {
public:
ConCommandProxy(
const char* name,
- FnCommandCallback_t callback,
+ PluginFnCommandCallback_t callback,
const char* helpString,
int flags,
void* parent
@@ -33,7 +33,7 @@ class ConCommandProxy : public ClassProxy<ConCommand> {
PLUGIN_DATA_TYPES* plugin_data = static_cast<PLUGIN_DATA_TYPES*>(data);
PluginInitFuncs* funcs = plugin_data->funcs;
- EngineData* engine_data = plugin_data->engine_data;
+ PluginEngineData* engine_data = plugin_data->engine_data;
assert(funcs->createObject);
assert(engine_data->ConCommandConstructor);
@@ -41,7 +41,7 @@ class ConCommandProxy : public ClassProxy<ConCommand> {
ptr = static_cast<ConCommand*>(funcs->createObject(ObjectType::CONCOMMANDS));
spdlog::info("Registering ConCommand {}", name);
- engine_data->ConCommandConstructor(ptr, name, callback, helpString, flags, parent);
+ engine_data->ConCommandConstructor(ptr, const_cast<char*>(name), callback, const_cast<char*>(helpString), flags, parent);
}
};
diff --git a/src/internal/convarproxy.h b/src/internal/convarproxy.h
index 3291bd8..f2e1b15 100644
--- a/src/internal/convarproxy.h
+++ b/src/internal/convarproxy.h
@@ -45,7 +45,7 @@ public:
PLUGIN_DATA_TYPES* plugin_data = static_cast<PLUGIN_DATA_TYPES*>(data);
PluginInitFuncs* funcs = plugin_data->funcs;
- EngineData* engine_data = plugin_data->engine_data;
+ PluginEngineData* engine_data = plugin_data->engine_data;
assert(funcs->createObject);
assert(engine_data->ConCommandConstructor);
@@ -58,7 +58,7 @@ public:
this->ptr->m_ConCommandBase.s_pConCommandBases = (ConCommandBase*)engine_data->IConVar_Vtable;
engine_data->conVarMalloc(&(this->ptr->m_pMalloc), 0, 0);
- engine_data->conVarRegister(this->ptr, pszName, pszDefaultValue, nFlags, pszHelpString, bMin, fMin, bMax, fMax, (void*)pCallback);
+ engine_data->conVarRegister(this->ptr, const_cast<char*>(pszName), const_cast<char*>(pszDefaultValue), nFlags, const_cast<char*>(pszHelpString), bMin, fMin, bMax, fMax, (void*)pCallback);
}
const char* GetString() const {
diff --git a/src/internal/types.h b/src/internal/types.h
index a19c00c..157f90e 100644
--- a/src/internal/types.h
+++ b/src/internal/types.h
@@ -6,7 +6,7 @@
typedef struct {
PluginInitFuncs* funcs;
PluginNorthstarData* data;
- EngineData* engine_data;
+ PluginEngineData* engine_data;
} PLUGIN_DATA_TYPES ;
#ifndef SQTrue
diff --git a/src/ns_plugin.h b/src/ns_plugin.h
index 99c90e9..b25a92d 100644
--- a/src/ns_plugin.h
+++ b/src/ns_plugin.h
@@ -73,18 +73,4 @@ typedef ECommandTarget_t (*Cbuf_GetCurrentPlayerType)();
typedef void (*Cbuf_AddTextType)(ECommandTarget_t eTarget, const char* text, cmd_source_t source);
typedef void (*Cbuf_ExecuteType)();
-// This is a mess
-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);
-
-struct EngineData
-{
- ConCommandConstructorType ConCommandConstructor;
- ConVarMallocType conVarMalloc;
- ConVarRegisterType conVarRegister;
- void* ConVar_Vtable;
- void* IConVar_Vtable;
-};
-
#endif
diff --git a/src/plugin.cpp b/src/plugin.cpp
index 273b41f..179a802 100644
--- a/src/plugin.cpp
+++ b/src/plugin.cpp
@@ -126,6 +126,7 @@ void Plugin::register_server_callbacks()
assert(0);
}
+ spdlog::warn("{}", cmd);
this->RunCommand(cmd);
return rapidjson::Value();
@@ -157,9 +158,9 @@ HMODULE Plugin::GetModuleByName(const char* name)
return nullptr;
}
-void Plugin::LoadEngineData(void* data)
+void Plugin::LoadEngineData(PluginEngineData* data, HMODULE dllPtr)
{
- this->engine_data = *static_cast<EngineData*>(data);
+ this->engine_data = *data;
PLUGIN_DATA_TYPES plugin_data = {
&this->funcs,
@@ -176,15 +177,16 @@ void Plugin::LoadEngineData(void* data)
proxy->initialize(&plugin_data);
}
- this->engine = GetModuleByName("engine.dll");
+ this->engine = dllPtr;
if (this->engine)
{
+ uintptr_t addr = (uintptr_t)this->engine;
+
// Offsets by Northstar
// https://github.com/R2Northstar/NorthstarLauncher/blob/0cbdd5672815f956e6b2d2de48d596e87514a07b/NorthstarDLL/engine/r2engine.cpp#L29
-
- this->engine_funcs.Cbuf_GetCurrentPlayer = reinterpret_cast<Cbuf_GetCurrentPlayerType>(((uintptr_t)this->engine) + 0x120630);
- this->engine_funcs.Cbuf_AddText = reinterpret_cast<Cbuf_AddTextType>(((uintptr_t)this->engine) + 0x1203B0);
- this->engine_funcs.Cbuf_Execute = reinterpret_cast<Cbuf_ExecuteType>(((uintptr_t)this->engine) + 0x1204B0);
+ this->engine_funcs.Cbuf_GetCurrentPlayer = reinterpret_cast<Cbuf_GetCurrentPlayerType>(addr + (uintptr_t)0x120630);
+ this->engine_funcs.Cbuf_AddText = reinterpret_cast<Cbuf_AddTextType>(addr + (uintptr_t)0x1203B0);
+ this->engine_funcs.Cbuf_Execute = reinterpret_cast<Cbuf_ExecuteType>(addr + (uintptr_t)0x1204B0);
}
}
@@ -277,8 +279,11 @@ void Plugin::RunCommand(const char* cmd)
if (!cmd)
return;
- this->engine_funcs.Cbuf_AddText(this->engine_funcs.Cbuf_GetCurrentPlayer(), cmd, cmd_source_t::kCommandSrcCode);
- this->engine_funcs.Cbuf_Execute();
+ spdlog::info("Running command '{}'", cmd);
+
+ ECommandTarget_t cur_player = this->engine_funcs.Cbuf_GetCurrentPlayer();
+ this->engine_funcs.Cbuf_AddText(cur_player, cmd, cmd_source_t::kCommandSrcCode);
+ //this->engine_funcs.Cbuf_Execute(); Crashes?
}
SQRESULT Plugin::RunSquirrelCode(ScriptContext context, std::string code, SQObject* ret_val)
@@ -351,7 +356,7 @@ SQFuncRegistrationProxy* Plugin::AddNativeSquirrelFunction(std::string returnTyp
return this->squirrel_functions.emplace_back(sqfrp);
}
-ConCommandProxy* Plugin::ConCommand(const char* name, FnCommandCallback_t callback, const char* helpString, int flags, void* parent)
+ConCommandProxy* Plugin::ConCommand(const char* name, PluginFnCommandCallback_t callback, const char* helpString, int flags, void* parent)
{
ConCommandProxy* proxy = new ConCommandProxy(name, callback, helpString, flags, parent);
diff --git a/src/plugin.h b/src/plugin.h
index b55cb40..7f5b04f 100644
--- a/src/plugin.h
+++ b/src/plugin.h
@@ -14,7 +14,7 @@ class Plugin {
private:
PluginInitFuncs funcs = { 0 };
PluginNorthstarData data = { 0 };
- EngineData engine_data = { 0 };
+ PluginEngineData engine_data = { 0 };
SquirrelFunctions client_sqvm_funcs = { 0 };
SquirrelFunctions server_sqvm_funcs = { 0 };
@@ -45,7 +45,7 @@ class Plugin {
Plugin(PluginInitFuncs* funcs, PluginNorthstarData* data);
~Plugin();
- void LoadEngineData(void* data);
+ void LoadEngineData(PluginEngineData* data, HMODULE dllPtr);
void LoadSQVMFunctions(ScriptContext context, SquirrelFunctions* funcs);
void LoadSQVM(ScriptContext context, CSquirrelVM* sqvm);
void RemoveSQVM(ScriptContext context);
@@ -56,7 +56,7 @@ class Plugin {
SQFuncRegistrationProxy* AddNativeSquirrelFunction(std::string returnType, std::string name, std::string argTypes, std::string helpText, ScriptContext context, SQFunction func);
// Wraps around the internals we receive
- ConCommandProxy* ConCommand(const char* name, FnCommandCallback_t callback, const char* helpString, int flags, void* parent = nullptr);
+ ConCommandProxy* ConCommand(const char* name, PluginFnCommandCallback_t callback, const char* helpString, int flags, void* parent = nullptr);
ConVarProxy* ConVar(const char* pszName, const char* pszDefaultValue, int nFlags, const char* pszHelpString, bool bMin = 0, float fMin = 0, bool bMax = 0, float fMax = 0, FnChangeCallback_t pCallback = nullptr);
};
diff --git a/src/rpc_server.cpp b/src/rpc_server.cpp
index 20b55cf..f6fa361 100644
--- a/src/rpc_server.cpp
+++ b/src/rpc_server.cpp
@@ -74,6 +74,7 @@ RPCRequest::RPCRequest(HTTPRequest* request):
}
}
+ this->valid = true;
spdlog::debug("Valid request");
}
@@ -101,7 +102,7 @@ void RPCRequest::response(rapidjson::Value& response)
void RPCRequest::result(rapidjson::Value result)
{
- if (!this->body.HasMember("id"))
+ if (this->valid && !this->body.HasMember("id"))
return;
rapidjson::MemoryPoolAllocator<>& allocator = this->body.GetAllocator();
diff --git a/src/rpc_server.h b/src/rpc_server.h
index edfc15b..f5e1f4b 100644
--- a/src/rpc_server.h
+++ b/src/rpc_server.h
@@ -10,6 +10,7 @@
class RPCRequest {
private:
+ bool valid = false;
HTTPRequest* request;
rapidjson::Document body;
@@ -24,6 +25,8 @@ class RPCRequest {
RPCRequest(HTTPRequest* request);
~RPCRequest();
+ bool is_valid() { return this->valid; };
+
rapidjson::MemoryPoolAllocator<>& get_allocator() { return this->body.GetAllocator(); };
std::string get_method() { return this->method; }
rapidjson::Value& get_params() { return this->params; }