Skip to content

Commit

Permalink
fix: improve error handling if globals are accessed before ready
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed May 31, 2024
1 parent eea6451 commit e207be2
Showing 1 changed file with 72 additions and 41 deletions.
113 changes: 72 additions & 41 deletions src/scripting/natives/natives_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,69 @@ namespace counterstrikesharp {

const char* GetMapName(ScriptContext& script_context)
{
if (globals::getGlobalVars() == nullptr)
auto globalVars = globals::getGlobalVars();
if (globalVars == nullptr)
{
script_context.ThrowNativeError("Global Variables not initialized yet.");
return nullptr;
}

return globals::getGlobalVars()->mapname.ToCStr();
return globalVars->mapname.ToCStr();
}

const char* GetGameDirectory(ScriptContext& script_context)
{
return strdup(Plat_GetGameDirectory());
}
const char* GetGameDirectory(ScriptContext& script_context) { return strdup(Plat_GetGameDirectory()); }

bool IsMapValid(ScriptContext& script_context)
{
auto mapname = script_context.GetArgument<const char*>(0);
return globals::engine->IsMapValid(mapname) != 0;
}

float GetTickInterval(ScriptContext& script_context)
float GetTickInterval(ScriptContext& script_context) { return globals::engine_fixed_tick_interval; }

float GetCurrentTime(ScriptContext& script_context)
{
return globals::engine_fixed_tick_interval;
auto globalVars = globals::getGlobalVars();
if (globalVars == nullptr)
{
script_context.ThrowNativeError("Global Variables not initialized yet.");
return -1;
}

return globalVars->curtime;
}

float GetCurrentTime(ScriptContext& script_context) { return globals::getGlobalVars()->curtime; }
int GetTickCount(ScriptContext& script_context)
{
auto globalVars = globals::getGlobalVars();
if (globalVars == nullptr)
{
script_context.ThrowNativeError("Global Variables not initialized yet.");
return -1;
}

int GetTickCount(ScriptContext& script_context) { return globals::getGlobalVars()->tickcount; }
return globalVars->tickcount;
}

float GetGameFrameTime(ScriptContext& script_context)
{
return globals::getGlobalVars()->frametime;
auto globalVars = globals::getGlobalVars();
if (globalVars == nullptr)
{
script_context.ThrowNativeError("Global Variables not initialized yet.");
return -1;
}

return globalVars->frametime;
}

double GetEngineTime(ScriptContext& script_context) { return Plat_FloatTime(); }

int GetMaxClients(ScriptContext& script_context)
{
auto globalVars = globals::getGlobalVars();
if (globalVars == nullptr) {
if (globalVars == nullptr)
{
script_context.ThrowNativeError("Global Variables not initialized yet.");
return -1;
}
Expand Down Expand Up @@ -152,10 +178,13 @@ Ray_t* CreateRay1(ScriptContext& script_context)

Ray_t* pRay = new Ray_t;

if (ray_type == RayType_EndPoint) {
if (ray_type == RayType_EndPoint)
{
pRay->Init(*vec1, *vec2);
return pRay;
} else if (ray_type == RayType_Infinite) {
}
else if (ray_type == RayType_Infinite)
{
QAngle angles;
Vector endVec;
angles.Init(vec2->x, vec2->y, vec2->z);
Expand Down Expand Up @@ -200,10 +229,7 @@ CSimpleTraceFilter* NewSimpleTraceFilter(ScriptContext& script_context)
return new CSimpleTraceFilter(index_to_ignore);
}

TraceFilterProxy* NewTraceFilterProxy(ScriptContext& script_context)
{
return new TraceFilterProxy();
}
TraceFilterProxy* NewTraceFilterProxy(ScriptContext& script_context) { return new TraceFilterProxy(); }

void TraceFilterProxySetTraceTypeCallback(ScriptContext& script_context)
{
Expand All @@ -228,15 +254,19 @@ void QueueTaskForNextFrame(ScriptContext& script_context)
auto func = script_context.GetArgument<void*>(0);

typedef void(voidfunc)(void);
globals::mmPlugin->AddTaskForNextFrame([func]() { reinterpret_cast<voidfunc*>(func)(); });
globals::mmPlugin->AddTaskForNextFrame([func]() {
reinterpret_cast<voidfunc*>(func)();
});
}

void QueueTaskForNextWorldUpdate(ScriptContext& script_context)
{
auto func = script_context.GetArgument<void*>(0);

typedef void(voidfunc)(void);
globals::serverManager.AddTaskForNextWorldUpdate([func]() { reinterpret_cast<voidfunc*>(func)(); });
globals::serverManager.AddTaskForNextWorldUpdate([func]() {
reinterpret_cast<voidfunc*>(func)();
});
}

void QueueTaskForFrame(ScriptContext& script_context)
Expand All @@ -259,15 +289,19 @@ void* GetValveInterface(ScriptContext& scriptContext)
auto [interfaceType, interfaceName] = scriptContext.GetArguments<InterfaceType, const char*>();

CreateInterfaceFn factoryFn;
if (interfaceType == Server) {
if (interfaceType == Server)
{
factoryFn = globals::ismm->GetServerFactory();
} else if (interfaceType == Engine) {
}
else if (interfaceType == Engine)
{
factoryFn = globals::ismm->GetEngineFactory();
}

auto foundInterface = globals::ismm->VInterfaceMatch(factoryFn, interfaceName);

if (foundInterface == nullptr) {
if (foundInterface == nullptr)
{
scriptContext.ThrowNativeError("Could not find interface");
}

Expand All @@ -280,25 +314,24 @@ void GetCommandParamValue(ScriptContext& scriptContext)
auto paramType = scriptContext.GetArgument<DataType_t>(1);

int iContextIndex = 2;
switch (paramType) {
case DATA_TYPE_STRING:
scriptContext.SetResult(CommandLine()->ParmValue(
paramName, scriptContext.GetArgument<const char*>(iContextIndex)));
return;
case DATA_TYPE_INT:
scriptContext.SetResult(
CommandLine()->ParmValue(paramName, scriptContext.GetArgument<int>(iContextIndex)));
return;
case DATA_TYPE_FLOAT:
scriptContext.SetResult(
CommandLine()->ParmValue(paramName, scriptContext.GetArgument<float>(iContextIndex)));
return;
switch (paramType)
{
case DATA_TYPE_STRING:
scriptContext.SetResult(CommandLine()->ParmValue(paramName, scriptContext.GetArgument<const char*>(iContextIndex)));
return;
case DATA_TYPE_INT:
scriptContext.SetResult(CommandLine()->ParmValue(paramName, scriptContext.GetArgument<int>(iContextIndex)));
return;
case DATA_TYPE_FLOAT:
scriptContext.SetResult(CommandLine()->ParmValue(paramName, scriptContext.GetArgument<float>(iContextIndex)));
return;
}

scriptContext.ThrowNativeError("Invalid param type");
}

void PrintToServerConsole(ScriptContext& scriptContext) {
void PrintToServerConsole(ScriptContext& scriptContext)
{
auto message = scriptContext.GetArgument<const char*>(0);

META_CONPRINT(message);
Expand Down Expand Up @@ -330,10 +363,8 @@ REGISTER_NATIVES(engine, {
ScriptEngine::RegisterNativeHandler("TRACE_RESULT_ENTITY", TraceResultGetEntity);

ScriptEngine::RegisterNativeHandler("NEW_TRACE_FILTER_PROXY", NewTraceFilterProxy);
ScriptEngine::RegisterNativeHandler("TRACE_FILTER_PROXY_SET_TRACE_TYPE_CALLBACK",
TraceFilterProxySetTraceTypeCallback);
ScriptEngine::RegisterNativeHandler("TRACE_FILTER_PROXY_SET_SHOULD_HIT_ENTITY_CALLBACK",
TraceFilterProxySetShouldHitEntityCallback);
ScriptEngine::RegisterNativeHandler("TRACE_FILTER_PROXY_SET_TRACE_TYPE_CALLBACK", TraceFilterProxySetTraceTypeCallback);
ScriptEngine::RegisterNativeHandler("TRACE_FILTER_PROXY_SET_SHOULD_HIT_ENTITY_CALLBACK", TraceFilterProxySetShouldHitEntityCallback);

ScriptEngine::RegisterNativeHandler("CREATE_RAY_1", CreateRay1);
ScriptEngine::RegisterNativeHandler("CREATE_RAY_2", CreateRay2);
Expand Down

0 comments on commit e207be2

Please sign in to comment.