Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add error message, continue process if error raised #13

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scripts/autorun/reframework-d2d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ re.on_draw_ui(
if changed then
cfg.max_update_rate = value
end

local last_error = d2d.detail.get_last_script_error()
if last_error ~= "" then
imgui.text("Last Script Error:")
imgui.text_colored(last_error, 0xFF0000FF)
end
end
)

Expand Down
33 changes: 24 additions & 9 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct Plugin {
const std::chrono::duration<double> DEFAULT_UPDATE_INTERVAL{1.0 / 60.0};
std::chrono::duration<double> d2d_update_interval{DEFAULT_UPDATE_INTERVAL};
bool update_d2d{};
std::string last_script_error{};
};

Plugin* g_plugin{};
Expand All @@ -36,6 +37,12 @@ BOOL APIENTRY DllMain(HMODULE, DWORD reason, LPVOID) {
return TRUE;
}

void handle_error_message(const std::string& msg) {
OutputDebugStringA(msg.c_str());

g_plugin->last_script_error = msg;
}

auto get_d2d_max_updaterate() {
return 1.0 / g_plugin->d2d_update_interval.count();
}
Expand Down Expand Up @@ -87,6 +94,9 @@ void on_ref_lua_state_created(lua_State* l) try {

detail["get_max_updaterate"] = []() { return get_d2d_max_updaterate(); };
detail["set_max_updaterate"] = [](double fps) { set_d2d_max_updaterate(fps); };
detail["get_last_error"] = []() {
return g_plugin->last_script_error;
};
d2d["detail"] = detail;
d2d["register"] = [](sol::protected_function init_fn, sol::protected_function draw_fn) {
g_plugin->init_fns.emplace_back(init_fn);
Expand Down Expand Up @@ -148,17 +158,18 @@ void on_ref_lua_state_created(lua_State* l) try {
lua["d2d"] = d2d;
g_plugin->needs_init = true;
} catch (const std::exception& e) {
OutputDebugStringA(e.what());
handle_error_message(e.what());
API::get()->log_error("[reframework-d2d] [on_ref_lua_state_created] %s", e.what());
}

void on_ref_lua_state_destroyed(lua_State* l) try {
g_plugin->drawlist.acquire().commands.clear();
g_plugin->draw_fns.clear();
g_plugin->init_fns.clear();
g_plugin->last_script_error.clear();
g_plugin->lua = nullptr;
} catch (const std::exception& e) {
OutputDebugStringA(e.what());
handle_error_message(e.what());
API::get()->log_error("[reframework-d2d] [on_ref_lua_state_destroyed] %s", e.what());
}

Expand All @@ -167,7 +178,7 @@ void on_ref_device_reset() try {
g_plugin->d2d = nullptr;
g_plugin->d3d12.reset();
} catch (const std::exception& e) {
OutputDebugStringA(e.what());
handle_error_message(e.what());
API::get()->log_error("[reframework-d2d] [on_ref_lua_device_reset] %s", e.what());
}

Expand Down Expand Up @@ -222,7 +233,7 @@ void on_ref_frame() try {

g_plugin->update_d2d = false;
} catch (const std::exception& e) {
OutputDebugStringA(e.what());
handle_error_message(e.what());
// g_plugin->ref->functions->log_plugin->error(e.what());
}

Expand All @@ -243,7 +254,7 @@ void on_begin_rendering() try {
}
} catch (const std::exception& e) {
MessageBox(nullptr, e.what(), "[reframework-d2d] [init_fn] error", MB_ICONERROR | MB_OK);
OutputDebugStringA(e.what());
handle_error_message(e.what());
API::get()->log_error("[reframework-d2d] [on_ref_lua_device_reset] %s", e.what());
}
}
Expand All @@ -261,10 +272,14 @@ void on_begin_rendering() try {
cmds_lock.commands.clear();

for (const auto& draw_fn : g_plugin->draw_fns) {
auto result = draw_fn();
try {
auto result = draw_fn();

if (!result.valid()) {
sol::script_throw_on_error(g_plugin->lua, std::move(result));
if (!result.valid()) {
sol::script_throw_on_error(g_plugin->lua, std::move(result));
}
} catch (const std::exception& e) {
handle_error_message(e.what());
}
}

Expand All @@ -273,7 +288,7 @@ void on_begin_rendering() try {
g_plugin->update_d2d = true;
}
} catch (const std::exception& e) {
OutputDebugStringA(e.what());
handle_error_message(e.what());
// g_plugin->ref->functions->log_error(e.what());
}

Expand Down