Skip to content

Commit

Permalink
add API for getting unpaused ms
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Feb 2, 2025
1 parent 9504cd8 commit 492e6f9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Template for new versions:
## Documentation

## API
- ``Core::getUnpausedMs``: new API for getting unpaused ms since load in a fort-mode game

## Lua

Expand Down
8 changes: 6 additions & 2 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool PerfCounters::getIgnorePauseState() {
return ignore_pause_state;
}

void PerfCounters::registerTick(uint32_t baseline_ms) {
uint32_t PerfCounters::registerTick(uint32_t baseline_ms) {
if (!World::isFortressMode() || World::ReadPauseState()) {
last_tick_baseline_ms = 0;
return;
Expand Down Expand Up @@ -173,6 +173,8 @@ void PerfCounters::registerTick(uint32_t baseline_ms) {

recent_ticks.history[recent_ticks.head_idx] = elapsed_ms;
recent_ticks.sum_ms += elapsed_ms;

return elapsed_ms;
}

uint32_t PerfCounters::getUnpausedFps() {
Expand Down Expand Up @@ -1705,6 +1707,7 @@ bool Core::InitMainThread() {
}

perf_counters.reset();
unpaused_ms = 0;

return true;
}
Expand Down Expand Up @@ -2141,7 +2144,7 @@ int Core::Update()
}

uint32_t start_ms = p->getTickCount();
perf_counters.registerTick(start_ms);
unpaused_ms += perf_counters.registerTick(start_ms);
doUpdate(out);
perf_counters.incCounter(perf_counters.total_update_ms, start_ms);
}
Expand Down Expand Up @@ -2337,6 +2340,7 @@ void Core::onStateChange(color_ostream &out, state_change_event event)
case SC_WORLD_LOADED:
{
perf_counters.reset();
unpaused_ms = 0;
Persistence::Internal::load(out);
plug_mgr->doLoadWorldData(out);
loadModScriptPaths(out);
Expand Down
9 changes: 7 additions & 2 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ namespace DFHack
bool getIgnorePauseState();

// noop if game is paused and getIgnorePauseState() returns false
void incCounter(uint32_t &perf_counter, uint32_t baseline_ms);
void incCounter(uint32_t &counter, uint32_t baseline_ms);

// returns number of unpaused ms since last tick
uint32_t registerTick(uint32_t baseline_ms);

void registerTick(uint32_t baseline_ms);
uint32_t getUnpausedFps();

private:
Expand Down Expand Up @@ -219,6 +221,7 @@ namespace DFHack
static void cheap_tokenise(std::string const& input, std::vector<std::string> &output);

PerfCounters perf_counters;
uint32_t getUnpausedMs() { return unpaused_ms; }

lua_State* getLuaState(bool bypass_assertion = false) {
assert(bypass_assertion || isSuspended());
Expand Down Expand Up @@ -334,6 +337,8 @@ namespace DFHack

lua_State* State;

uint32_t unpaused_ms; // reset to 0 on map load

friend class CoreService;
friend class ServerConnection;
friend class CoreSuspender;
Expand Down

0 comments on commit 492e6f9

Please sign in to comment.