improve parsing error alert

This commit is contained in:
MihailRis 2024-07-24 17:11:43 +03:00
parent fe1ac69b57
commit 18d473b673
4 changed files with 42 additions and 28 deletions

View File

@ -25,13 +25,17 @@ package = {
} }
local __cached_scripts = {} local __cached_scripts = {}
function on_deprecated_call(name)
debug.warning("deprecated function called ("..name..")\n"..debug.traceback())
end
-- Load script with caching -- Load script with caching
-- --
-- path - script path `contentpack:filename`. -- path - script path `contentpack:filename`.
-- Example `base:scripts/tests.lua` -- Example `base:scripts/tests.lua`
-- --
-- nocache - ignore cached script, load anyway -- nocache - ignore cached script, load anyway
function load_script(path, nocache) local function __load_script(path, nocache)
local packname, filename = parse_path(path) local packname, filename = parse_path(path)
-- __cached_scripts used in condition because cached result may be nil -- __cached_scripts used in condition because cached result may be nil
@ -68,7 +72,7 @@ end
function require(path) function require(path)
local prefix, file = parse_path(path) local prefix, file = parse_path(path)
return load_script(prefix..":modules/"..file..".lua") return __load_script(prefix..":modules/"..file..".lua")
end end
function sleep(timesec) function sleep(timesec)
@ -78,24 +82,6 @@ function sleep(timesec)
end end
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) function pack.is_installed(packid)
return file.isfile(packid..":package.json") return file.isfile(packid..":package.json")
end end
@ -301,7 +287,7 @@ end
math.randomseed(time.uptime()*1536227939) math.randomseed(time.uptime()*1536227939)
-- Deprecated functions -- --------- Deprecated functions ------ --
block_index = block.index block_index = block.index
block_name = block.name block_name = block.name
blocks_count = block.defs_count blocks_count = block.defs_count
@ -318,3 +304,27 @@ get_block_rotation = block.get_rotation
set_block_rotation = block.set_rotation set_block_rotation = block.set_rotation
get_block_user_bits = block.get_user_bits get_block_user_bits = block.get_user_bits
set_block_user_bits = block.set_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

View File

@ -123,12 +123,7 @@ bool files::write_binary_json(const fs::path& filename, const dynamic::Map* obj,
std::shared_ptr<dynamic::Map> files::read_json(const fs::path& filename) { std::shared_ptr<dynamic::Map> files::read_json(const fs::path& filename) {
std::string text = files::read_string(filename); std::string text = files::read_string(filename);
try { return json::parse(filename.string(), text);
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());
}
} }
std::shared_ptr<dynamic::Map> files::read_binary_json(const fs::path& file) { std::shared_ptr<dynamic::Map> files::read_binary_json(const fs::path& file) {

View File

@ -29,7 +29,7 @@ void guiutil::alert(GUI* gui, const std::wstring& text, const runnable& on_hidde
auto label = std::make_shared<Label>(text); auto label = std::make_shared<Label>(text);
label->setMultiline(true); label->setMultiline(true);
label->setSize(glm::vec2(1, 80)); label->setSize(glm::vec2(1, 90));
panel->add(label); panel->add(label);
panel->add(std::make_shared<Button>( panel->add(std::make_shared<Button>(
langs::get(L"Ok"), glm::vec4(10.f), langs::get(L"Ok"), glm::vec4(10.f),

View File

@ -1,5 +1,6 @@
#include "EngineController.hpp" #include "EngineController.hpp"
#include "../coders/commons.hpp"
#include "../content/ContentLUT.hpp" #include "../content/ContentLUT.hpp"
#include "../debug/Logger.hpp" #include "../debug/Logger.hpp"
#include "../engine.hpp" #include "../engine.hpp"
@ -103,6 +104,10 @@ static bool loadWorldContent(Engine* engine, const fs::path& folder) {
util::str2wstr_utf8(error.what()) util::str2wstr_utf8(error.what())
); );
return false; return false;
} catch (const parsing_error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert(engine->getGUI(), util::str2wstr_utf8(error.errorLog()));
return false;
} catch (const std::runtime_error& error) { } catch (const std::runtime_error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine->setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert( guiutil::alert(
@ -204,6 +209,10 @@ void EngineController::createWorld(
L":\n"+util::str2wstr_utf8(error.what()) L":\n"+util::str2wstr_utf8(error.what())
); );
return; return;
} catch (const parsing_error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert(engine->getGUI(), util::str2wstr_utf8(error.errorLog()));
return;
} catch (const std::runtime_error& error) { } catch (const std::runtime_error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine->setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert( guiutil::alert(