From 18d473b6732fc1cae213f25305e3ca5d37e2a0b2 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 24 Jul 2024 17:11:43 +0300 Subject: [PATCH] improve parsing error alert --- res/scripts/stdlib.lua | 52 ++++++++++++++++++++-------------- src/files/files.cpp | 7 +---- src/graphics/ui/gui_util.cpp | 2 +- src/logic/EngineController.cpp | 9 ++++++ 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 7db93096..463d9d94 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -25,13 +25,17 @@ package = { } local __cached_scripts = {} +function on_deprecated_call(name) + debug.warning("deprecated function called ("..name..")\n"..debug.traceback()) +end + -- Load script with caching -- -- path - script path `contentpack:filename`. -- Example `base:scripts/tests.lua` -- -- nocache - ignore cached script, load anyway -function load_script(path, nocache) +local function __load_script(path, nocache) local packname, filename = parse_path(path) -- __cached_scripts used in condition because cached result may be nil @@ -68,7 +72,7 @@ end function require(path) local prefix, file = parse_path(path) - return load_script(prefix..":modules/"..file..".lua") + return __load_script(prefix..":modules/"..file..".lua") end function sleep(timesec) @@ -78,24 +82,6 @@ function sleep(timesec) end end -_dofile = dofile --- Replaces dofile('*/content/packid/*') with load_script('packid:*') -function dofile(path) - local index = string.find(path, "/content/") - if index then - local newpath = string.sub(path, index+9) - index = string.find(newpath, "/") - if index then - local label = string.sub(newpath, 1, index-1) - newpath = label..':'..string.sub(newpath, index+1) - if file.isfile(newpath) then - return load_script(newpath, true) - end - end - end - return _dofile(path) -end - function pack.is_installed(packid) return file.isfile(packid..":package.json") end @@ -301,7 +287,7 @@ end math.randomseed(time.uptime()*1536227939) --- Deprecated functions +-- --------- Deprecated functions ------ -- block_index = block.index block_name = block.name blocks_count = block.defs_count @@ -318,3 +304,27 @@ get_block_rotation = block.get_rotation set_block_rotation = block.set_rotation get_block_user_bits = block.get_user_bits set_block_user_bits = block.set_user_bits + +function load_script(path, nocache) + on_deprecated_call("load_script") + __load_script(path, nocache) +end + +_dofile = dofile +-- Replaces dofile('*/content/packid/*') with load_script('packid:*') +function dofile(path) + on_deprecated_call("dofile") + local index = string.find(path, "/content/") + if index then + local newpath = string.sub(path, index+9) + index = string.find(newpath, "/") + if index then + local label = string.sub(newpath, 1, index-1) + newpath = label..':'..string.sub(newpath, index+1) + if file.isfile(newpath) then + return __load_script(newpath, true) + end + end + end + return _dofile(path) +end diff --git a/src/files/files.cpp b/src/files/files.cpp index 8b66fa55..4c4d4ffd 100644 --- a/src/files/files.cpp +++ b/src/files/files.cpp @@ -123,12 +123,7 @@ bool files::write_binary_json(const fs::path& filename, const dynamic::Map* obj, std::shared_ptr files::read_json(const fs::path& filename) { std::string text = files::read_string(filename); - try { - return json::parse(filename.string(), text);; - } catch (const parsing_error& error) { - std::cerr << error.errorLog() << std::endl; - throw std::runtime_error("could not to parse "+filename.string()); - } + return json::parse(filename.string(), text); } std::shared_ptr files::read_binary_json(const fs::path& file) { diff --git a/src/graphics/ui/gui_util.cpp b/src/graphics/ui/gui_util.cpp index 41d13c5d..15e31d8a 100644 --- a/src/graphics/ui/gui_util.cpp +++ b/src/graphics/ui/gui_util.cpp @@ -29,7 +29,7 @@ void guiutil::alert(GUI* gui, const std::wstring& text, const runnable& on_hidde auto label = std::make_shared