-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/pcall-protection' into dev
- Loading branch information
Showing
9 changed files
with
212 additions
and
37 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
local weac = worldeditadditions_core | ||
--- | ||
-- @module worldeditadditions_core | ||
|
||
-- ███████ █████ ███████ ███████ ███████ ███ ██ | ||
-- ██ ██ ██ ██ ██ ██ ████ ██ | ||
-- ███████ ███████ █████ █████ █████ ██ ██ ██ | ||
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ | ||
-- ███████ ██ ██ ██ ███████ ███████ ██ ██ ████ | ||
|
||
|
||
local function send_error(player_name, cmdname, msg, stack_trace) | ||
print("DEBUG:HAI SEND_ERROR") | ||
local msg_compiled = table.concat({ | ||
"[//", cmdname, "] Error: ", | ||
msg, | ||
"\n", | ||
"Please report this by opening an issue on GitHub! Bug report link (ctrl + click):\n", | ||
"https://github.com/sbrl/Minetest-WorldEditAdditions/issues/new?title=", | ||
weac.format.escape(stack_trace:match("^[^\n]+")), -- extract 1st line & escape | ||
"&body=", | ||
weac.format.escape(table.concat({ | ||
[[## Describe the bug | ||
What's the bug? Be clear and detailed but concise in our explanation. Don't forget to include any context, error messages, logs, and screenshots required to understand the issue if applicable. | ||
## Reproduction steps | ||
Steps to reproduce the behaviour: | ||
1. Go to '...' | ||
2. Click on '....' | ||
3. Enter this command to '....' | ||
4. See error | ||
## System information (please complete the following information) | ||
- **Operating system and version:** [e.g. iOS] | ||
- **Minetest version:** [e.g. 5.8.0] | ||
- **WorldEdit version:** | ||
- **WorldEditAdditions version:** | ||
Please add any other additional specific system information here too if you think it would help. | ||
## Stack trace | ||
- **Command name:** ]], | ||
cmdname, | ||
"\n", | ||
"```\n", | ||
stack_trace, | ||
"\n", | ||
"```\n", | ||
}, "")), | ||
|
||
"\n", | ||
"-------------------------------------\n", | ||
"*** Stack trace ***\n", | ||
stack_trace, | ||
"\n", | ||
"-------------------------------------\n" | ||
}, "") | ||
|
||
print("DEBUG:player_notify player_name", player_name, "msg_compiled", msg_compiled) | ||
worldedit.player_notify(player_name, msg_compiled) | ||
end | ||
|
||
|
||
--- Calls the given function `fn` with the UNPACKED arguments from `args`, catching errors and sending the calling player a nice error message with a report link. | ||
-- | ||
-- WARNING: Do NOT nest `safe_function()` calls!!! | ||
-- @param fn function The function to call | ||
-- @param args table The table of args to unpack and send to `fn` as arguments | ||
-- @param string|nil player_name The name of the player affected. If nil then no message is sent to the player. | ||
-- @param string error_msg The error message to send when `fn` inevitably crashes. | ||
-- @param string|nil cmdname Optional. The name of the command being run. | ||
-- @returns bool,any,... A success bool (true == success), and then if success == true the rest of the arguments are the (unpacked) return values from the function called. If success == false, then the 2nd argument will be the stack trace. | ||
local function safe_function(fn, args, player_name, error_msg, cmdname) | ||
local retvals | ||
local success_xpcall, stack_trace = xpcall(function() | ||
retvals = { fn(weac.table.unpack(args)) } | ||
end, debug.traceback) | ||
|
||
if not success_xpcall then | ||
send_error(player_name, cmdname, error_msg, stack_trace) | ||
weac:emit("error", { | ||
fn = fn, | ||
args = args, | ||
player_name = player_name, | ||
cmdname = cmdname, | ||
stack_trace = stack_trace, | ||
error_msg = error_msg | ||
}) | ||
minetest.log("error", "[//"..tostring(cmdname).."] Caught error from running function ", fn, "with args", weac.inspect(args), "for player ", player_name, "with provided error message", error_msg, ". Stack trace: ", stack_trace) | ||
return false, stack_trace | ||
end | ||
|
||
|
||
return true, retvals | ||
end | ||
|
||
|
||
return safe_function |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
-- @module worldeditadditions_core | ||
|
||
-- decodeURIComponent() implementation | ||
-- Ref https://stackoverflow.com/a/78225561/1460422 | ||
-- Adapted by @sbrl to: | ||
-- - Print leading 0 behind escape codes as it should | ||
-- - Also escape ' and # | ||
|
||
-- TODO this doesn't work. It replaces \n with %A instead of %0A, though we don't know if that's a problem or not | ||
-- it also doesn't handle quotes even though we've clearly got them in the Lua pattern | ||
local function _escape_char(char) | ||
return string.format('%%%02X', string.byte(char)) | ||
end | ||
|
||
--- Escape the given string for use in a url. | ||
-- In other words, like a space turns into %20. | ||
-- Similar to Javascript's `encodeURIComponent()`. | ||
-- @param string str The string to escape. | ||
-- @returns string The escaped string. | ||
local function escape(str) | ||
return (string.gsub(str, "[^%a%d%-_%.!~%*%(%);/%?:@&=%+%$,]", _escape_char)) | ||
end | ||
|
||
return escape |
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
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
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