Last active
September 3, 2023 03:36
-
-
Save nommiin/d6a03f6e43a1308dd40103192ca7b04c to your computer and use it in GitHub Desktop.
Exception class for GameMaker 2023, allows custom message or wrapping runtime errors. Allows for error script to be loaded into memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// @function Exception(msg, args...) | |
/// @description A class containing error information for the exception being thrown | |
/// @argument {string | YYGMLException} msg - The message to store in the exception, or GM exception to wrap | |
/// @argument {any} [args...] - Variables used in template string | |
/// @returns {Exception} | |
/// @notes Enable EXCEPTION_EXTENDED to include the origin GML script's content in the exception | |
function Exception(msg) constructor { | |
if (is_struct(msg) && string_pos("YYGMLException", instanceof(msg)) != 0) { | |
self.message = msg.message; | |
self.longMessage = msg.longMessage; | |
self.stacktrace = msg.stacktrace; | |
self.script = msg.script; | |
self.line = msg.line; | |
} else { | |
var args = array_create(argument_count - 1); | |
for(var i = 0; i < array_length(args); i++) { | |
args[i] = argument[i + 1]; | |
} | |
msg = string_ext(msg, args); | |
var callstack = array_filter(debug_get_callstack(), function(e, ind) { return ind > 0 && typeof(e) == "string"; }); | |
for(var i = 0; i < array_length(callstack); i++) { | |
var call = callstack[i], ind = string_last_pos(":", call); | |
if (ind > 0) { | |
callstack[i] = string_copy(call, 1, ind - 1); | |
var line = -1; | |
try { | |
line = real(string_copy(call, ind + 1, string_length(call))); | |
} catch (_) { | |
line = -1; | |
} | |
if (i == 0) { | |
self.script = callstack[i]; | |
self.line = line; | |
} | |
callstack[i] += " (line " + string(line) + ")"; | |
} | |
} | |
self.message = msg; | |
self.longMessage = msg; | |
self.stacktrace = callstack; | |
} | |
if (EXCEPTION_EXTENDED) { | |
if (GM_build_type == "run") { | |
var file = filename_dir(EXCEPTION_ROOT) + "\\"; | |
if (!directory_exists(file)) { | |
show_debug_message($"Could not find directory \"{file}\" for extended exception data, returning early"); | |
return; | |
} | |
var copy = self.script; | |
while (true) { | |
var ind = string_pos("gml_", copy); | |
if (ind == 0) { | |
break; | |
} | |
copy = string_copy(copy, ind + 4, string_length(copy)); | |
} | |
if (string_starts_with(copy, "Script_")) { | |
copy = string_delete(copy, 1, 7); | |
file += $"scripts\\{copy}\\{copy}.gml"; | |
} else if (string_starts_with(copy, "Room_")) { | |
copy = string_delete(copy, 1, 5); | |
copy = string_delete(copy, string_length(copy) - 7, 7); | |
file += $"rooms\\{copy}\\RoomCreationCode.gml"; | |
} else if (string_starts_with(copy, "Object_")) { | |
copy = string_delete(copy, 1, 7); | |
var ind = string_last_pos("_", string_copy(copy, 1, string_last_pos("_", copy) - 1)); | |
var event = string_copy(copy, ind + 1, string_length(copy)); | |
copy = string_copy(copy, 1, ind - 1); | |
file += $"objects\\{copy}\\{event}.gml"; | |
} | |
if (!file_exists(file)) { | |
show_debug_message($"Could not find script file \"{file}\" for extended exception data, returning early"); | |
return; | |
} | |
var buffer = buffer_load(file); | |
buffer_seek(buffer, buffer_seek_start, 0); | |
var data = buffer_read(buffer, buffer_text); | |
buffer_delete(buffer); | |
self.content = array_map(string_split(data, "\n"), function(line) { | |
return string_replace_all(line, " ", string_repeat(" ", 4)); | |
}); | |
} | |
} | |
} | |
#macro EXCEPTION_ROOT GM_project_filename | |
#macro EXCEPTION_EXTENDED true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment