Skip to content

Commit d3874af

Browse files
committed
feat(core): Crash Prevention
1 parent d75feac commit d3874af

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/scripting/engine/events.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <scripting/core.h>
2+
#include <tools/crashreporter/crashreporter.h>
23
#include <memory/gamedata/gamedata.h>
34
#include <sdk/game.h>
5+
#include <utils/utils.h>
46

57
extern std::map<std::string, std::string> gameEventsRegister;
68
EValue SerializeData(std::any data, EContext* state);
@@ -178,7 +180,7 @@ LoadScriptingComponent(events, [](PluginObject plugin, EContext* ctx) -> void {
178180

179181
IGameEventListener2* playerListener = g_GameData.FetchSignature<GetLegacyGameEventListener>("LegacyGameEventListener")(slot);
180182
if (!g_gameEventManager->FindListener(playerListener, data->GetData<IGameEvent*>("event_data")->GetName())) {
181-
/* TODO: Crash Reporter - Report crash prevention */
183+
ReportPreventionIncident("Fire Event", string_format("Tried to fire event '%s' but the client isn't listening to this event.", data->GetData<IGameEvent*>("event_data")->GetName()));
182184
return;
183185
}
184186
playerListener->FireGameEvent(data->GetData<IGameEvent*>("event_data"));

src/scripting/sdk/schema.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sdk/components/CPlayerPawnComponent.h>
1212
#include <sdk/components/EmitSound_t.h>
1313
#include <sdk/components/CSingleRecipientFilter.h>
14+
#include <tools/crashreporter/crashreporter.h>
1415
#include <utils/common.h>
1516
#include <utils/utils.h>
1617

@@ -311,7 +312,7 @@ void SchemaCallback(PluginObject plugin, EContext* ctx) {
311312

312313
void* instance = data->GetData<void*>("class_ptr");
313314
if (!instance) {
314-
/* TODO: Crash Reporter - Report crash prevention */
315+
ReportPreventionIncident("Schema / SDK", string_format("Tried to get member '%s::%s' but the entity is invalid.", className.c_str(), fieldName.c_str()));
315316
return context->StopExecution();
316317
}
317318
context->SetReturn(AccessSDK(data->GetData<void*>("class_ptr"), className, fieldName, path, context->GetPluginContext()));
@@ -320,6 +321,12 @@ void SchemaCallback(PluginObject plugin, EContext* ctx) {
320321
std::string className = data->GetData<std::string>("class_name");
321322
std::string fieldName = explode(context->GetFunctionKey(), " ").back();
322323

324+
void* instance = data->GetData<void*>("class_ptr");
325+
if (!instance) {
326+
ReportPreventionIncident("Schema / SDK", string_format("Tried to set member '%s::%s' but the entity is invalid.", className.c_str(), fieldName.c_str()));
327+
return context->StopExecution();
328+
}
329+
323330
UpdateSDK(data->GetData<void*>("class_ptr"), className, fieldName, context->GetArgument<EValue>(0), context->GetPluginContext());
324331
context->StopExecution();
325332
});
@@ -333,7 +340,7 @@ void SchemaCallback(PluginObject plugin, EContext* ctx) {
333340
if (classFuncs.find(path) != classFuncs.end()) {
334341
void* instance = data->GetData<void*>("class_ptr");
335342
if (!instance) {
336-
/* TODO: Crash Reporter - Report crash prevention */
343+
ReportPreventionIncident("Schema / SDK", string_format("Tried to call function '%s::%s' but the entity is invalid.", className.c_str(), function_name.c_str()));
337344
return context->StopExecution();
338345
}
339346
return;

src/tools/crashreporter/crashreporter.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ bool BeginCrashListener() {
5353
}
5454
}
5555

56+
if (!Files::ExistsPath("addons/swiftly/dumps/prevention"))
57+
{
58+
if (!Files::CreateDirectory("addons/swiftly/dumps/prevention"))
59+
{
60+
PLUGIN_PRINTF("Crash Listener", "Couldn't create dumps prevention folder.\n");
61+
return false;
62+
}
63+
}
64+
5665
startup_cmd = CommandLine()->GetCmdLine();
5766
std::vector<std::string> exp = explode(startup_cmd, " ");
5867
std::vector<std::string> exp2;
@@ -181,6 +190,15 @@ bool BeginCrashListener() {
181190
}
182191
}
183192

193+
if (!Files::ExistsPath("addons/swiftly/dumps/prevention"))
194+
{
195+
if (!Files::CreateDirectory("addons/swiftly/dumps/prevention"))
196+
{
197+
PLUGIN_PRINTF("Crash Listener", "Couldn't create dumps prevention folder.\n");
198+
return false;
199+
}
200+
}
201+
184202
startup_cmd = CommandLine()->GetCmdLine();
185203
std::vector<std::string> exp = explode(startup_cmd, " ");
186204
std::vector<std::string> exp2;
@@ -253,4 +271,28 @@ void WriteCrashDump(std::vector<std::string> functionStack)
253271

254272
Files::Append(file_path, string_format("================================\nCommand: %s\nMap: %s\nVersion: %s (%s)\n================================\n\n%s\n\n%s", startup_cmd.c_str(), engine->GetServerGlobals() ? engine->GetServerGlobals()->mapname.ToCStr() : "None", g_Plugin.GetVersion(), GITHUB_SHA, implode(functionStack, "\n").c_str(), WritePluginsCallStack().c_str()), false);
255273
PLUGIN_PRINTF("Crash Reporter", "A dump log file has been created at: %s\n", file_path.c_str());
274+
exit(1);
275+
}
276+
277+
void ReportPreventionIncident(std::string category, std::string incident_str)
278+
{
279+
PLUGIN_PRINTF("Crash Prevention", "A crash has been prevented by Swiftly Core and the details will be listed below:\n");
280+
281+
TextTable backtraceTable('-', '|', '+');
282+
283+
backtraceTable.add(" Category ");
284+
backtraceTable.add(" Message ");
285+
backtraceTable.endOfRow();
286+
287+
backtraceTable.add(string_format(" %s ", category.c_str()));
288+
backtraceTable.add(string_format(" %s ", incident_str.c_str()));
289+
backtraceTable.endOfRow();
290+
291+
PrintTextTable("Crash Prevention", backtraceTable);
292+
293+
std::string file_path = string_format("addons/swiftly/dumps/prevention/incident.%s.log", get_uuid().c_str());
294+
if (Files::ExistsPath(file_path)) Files::Delete(file_path);
295+
296+
Files::Append(file_path, string_format("================================\nCategory: %s\nDetails: %s\n\n%s", category.c_str(), incident_str.c_str(), WritePluginsCallStack().c_str()), false);
297+
PLUGIN_PRINTF("Crash Prevention", "A log file has been created at: %s\n", file_path.c_str());
256298
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#ifndef _tools_crashreporter_h
22
#define _tools_crashreporter_h
33

4+
#include <string>
5+
46
bool BeginCrashListener();
57
void EndCrashListener();
68

9+
void ReportPreventionIncident(std::string category, std::string incident_str);
10+
711
#endif

0 commit comments

Comments
 (0)