summaryrefslogtreecommitdiff
path: root/library/Core.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'library/Core.cpp')
-rw-r--r--library/Core.cpp156
1 files changed, 122 insertions, 34 deletions
diff --git a/library/Core.cpp b/library/Core.cpp
index 09344135..6a0dea7c 100644
--- a/library/Core.cpp
+++ b/library/Core.cpp
@@ -281,7 +281,7 @@ static command_result runLuaScript(color_ostream &out, std::string name, vector<
return ok ? CR_OK : CR_FAILURE;
}
-static command_result runRubyScript(PluginManager *plug_mgr, std::string name, vector<string> &args)
+static command_result runRubyScript(color_ostream &out, PluginManager *plug_mgr, std::string name, vector<string> &args)
{
std::string rbcmd = "$script_args = [";
for (size_t i = 0; i < args.size(); i++)
@@ -290,7 +290,7 @@ static command_result runRubyScript(PluginManager *plug_mgr, std::string name, v
rbcmd += "load './hack/scripts/" + name + ".rb'";
- return plug_mgr->eval_ruby(rbcmd.c_str());
+ return plug_mgr->eval_ruby(out, rbcmd.c_str());
}
command_result Core::runCommand(color_ostream &out, const std::string &command)
@@ -358,7 +358,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
continue;
if (pcmd.isHotkeyCommand())
- con.color(Console::COLOR_CYAN);
+ con.color(COLOR_CYAN);
con.print("%s: %s\n",pcmd.name.c_str(), pcmd.description.c_str());
con.reset_color();
if (!pcmd.usage.empty())
@@ -481,7 +481,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
{
const PluginCommand & pcmd = (plug->operator[](j));
if (pcmd.isHotkeyCommand())
- con.color(Console::COLOR_CYAN);
+ con.color(COLOR_CYAN);
con.print(" %-22s - %s\n",pcmd.name.c_str(), pcmd.description.c_str());
con.reset_color();
}
@@ -519,7 +519,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
for(auto iter = out.begin();iter != out.end();iter++)
{
if ((*iter).recolor)
- con.color(Console::COLOR_CYAN);
+ con.color(COLOR_CYAN);
con.print(" %-22s- %s\n",(*iter).name.c_str(), (*iter).description.c_str());
con.reset_color();
}
@@ -632,7 +632,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first, ve
if (fileExists(filename + ".lua"))
res = runLuaScript(con, first, parts);
else if (plug_mgr->eval_ruby && fileExists(filename + ".rb"))
- res = runRubyScript(plug_mgr, first, parts);
+ res = runRubyScript(con, plug_mgr, first, parts);
else
con.printerr("%s is not a recognized command.\n", first.c_str());
}
@@ -752,6 +752,7 @@ Core::Core()
misc_data_mutex=0;
last_world_data_ptr = NULL;
last_local_map_ptr = NULL;
+ last_pause_state = false;
top_viewscreen = NULL;
screen_window = NULL;
server = NULL;
@@ -1026,35 +1027,41 @@ int Core::TileUpdate()
return true;
}
-// should always be from simulation thread!
-int Core::Update()
+int Core::ClaimSuspend(bool force_base)
{
- if(errorstate)
- return -1;
+ auto tid = this_thread::get_id();
+ lock_guard<mutex> lock(d->AccessMutex);
- // Pretend this thread has suspended the core in the usual way
+ if (force_base || d->df_suspend_depth <= 0)
{
- lock_guard<mutex> lock(d->AccessMutex);
-
assert(d->df_suspend_depth == 0);
- d->df_suspend_thread = this_thread::get_id();
- d->df_suspend_depth = 1000;
- }
- // Initialize the core
- bool first_update = false;
-
- if(!started)
+ d->df_suspend_thread = tid;
+ d->df_suspend_depth = 1000000;
+ return 1000000;
+ }
+ else
{
- first_update = true;
- Init();
- if(errorstate)
- return -1;
- Lua::Core::Reset(con, "core init");
+ assert(d->df_suspend_thread == tid);
+ return ++d->df_suspend_depth;
}
+}
- color_ostream_proxy out(con);
+void Core::DisclaimSuspend(int level)
+{
+ auto tid = this_thread::get_id();
+ lock_guard<mutex> lock(d->AccessMutex);
+
+ assert(d->df_suspend_depth == level && d->df_suspend_thread == tid);
+ if (level == 1000000)
+ d->df_suspend_depth = 0;
+ else
+ --d->df_suspend_depth;
+}
+
+void Core::doUpdate(color_ostream &out, bool first_update)
+{
Lua::Core::Reset(out, "DF code execution");
if (first_update)
@@ -1116,18 +1123,48 @@ int Core::Update()
}
}
+ if (df::global::pause_state)
+ {
+ if (*df::global::pause_state != last_pause_state)
+ {
+ onStateChange(out, last_pause_state ? SC_UNPAUSED : SC_PAUSED);
+ last_pause_state = *df::global::pause_state;
+ }
+ }
+
// Execute per-frame handlers
onUpdate(out);
- // Release the fake suspend lock
+ out << std::flush;
+}
+
+// should always be from simulation thread!
+int Core::Update()
+{
+ if(errorstate)
+ return -1;
+
+ color_ostream_proxy out(con);
+
+ // Pretend this thread has suspended the core in the usual way,
+ // and run various processing hooks.
{
- lock_guard<mutex> lock(d->AccessMutex);
+ CoreSuspendClaimer suspend(true);
- assert(d->df_suspend_depth == 1000);
- d->df_suspend_depth = 0;
- }
+ // Initialize the core
+ bool first_update = false;
- out << std::flush;
+ if(!started)
+ {
+ first_update = true;
+ Init();
+ if(errorstate)
+ return -1;
+ Lua::Core::Reset(con, "core init");
+ }
+
+ doUpdate(out, first_update);
+ }
// wake waiting tools
// do not allow more tools to join in while we process stuff here
@@ -1148,7 +1185,7 @@ int Core::Update()
// destroy condition
delete nc;
// check lua stack depth
- Lua::Core::Reset(con, "suspend");
+ Lua::Core::Reset(out, "suspend");
}
return 0;
@@ -1188,6 +1225,7 @@ int Core::Shutdown ( void )
if(errorstate)
return true;
errorstate = 1;
+ CoreSuspendClaimer suspend;
if(plug_mgr)
{
delete plug_mgr;
@@ -1222,7 +1260,7 @@ bool Core::ncurses_wgetch(int in, int & out)
// FIXME: copypasta, push into a method!
if(df::global::ui && df::global::gview)
{
- df::viewscreen * ws = Gui::GetCurrentScreen();
+ df::viewscreen * ws = Gui::getCurViewscreen();
if (strict_virtual_cast<df::viewscreen_dwarfmodest>(ws) &&
df::global::ui->main.mode != ui_sidebar_mode::Hotkeys &&
df::global::ui->main.hotkeys[idx].cmd == df::ui_hotkey::T_cmd::None)
@@ -1538,6 +1576,56 @@ void ClassNameCheck::getKnownClassNames(std::vector<std::string> &names)
names.push_back(*it);
}
+bool Process::patchMemory(void *target, const void* src, size_t count)
+{
+ uint8_t *sptr = (uint8_t*)target;
+ uint8_t *eptr = sptr + count;
+
+ // Find the valid memory ranges
+ std::vector<t_memrange> ranges;
+ getMemRanges(ranges);
+
+ unsigned start = 0;
+ while (start < ranges.size() && ranges[start].end <= sptr)
+ start++;
+ if (start >= ranges.size() || ranges[start].start > sptr)
+ return false;
+
+ unsigned end = start+1;
+ while (end < ranges.size() && ranges[end].start < eptr)
+ {
+ if (ranges[end].start != ranges[end-1].end)
+ return false;
+ end++;
+ }
+ if (ranges[end-1].end < eptr)
+ return false;
+
+ // Verify current permissions
+ for (unsigned i = start; i < end; i++)
+ if (!ranges[i].valid || !(ranges[i].read || ranges[i].execute) || ranges[i].shared)
+ return false;
+
+ // Apply writable permissions & update
+ bool ok = true;
+
+ for (unsigned i = start; i < end && ok; i++)
+ {
+ t_memrange perms = ranges[i];
+ perms.write = perms.read = true;
+ if (!setPermisions(perms, perms))
+ ok = false;
+ }
+
+ if (ok)
+ memmove(target, src, count);
+
+ for (unsigned i = start; i < end && ok; i++)
+ setPermisions(ranges[i], ranges[i]);
+
+ return ok;
+}
+
/*******************************************************************************
M O D U L E S
*******************************************************************************/