-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathCore.h
More file actions
540 lines (447 loc) · 18 KB
/
Core.h
File metadata and controls
540 lines (447 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once
#include "Console.h"
#include "CoreDefs.h"
#include "Export.h"
#include "Hooks.h"
#include "modules/Graphic.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <map>
#include <memory>
#include <mutex>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <stdint.h>
struct WINDOW;
struct lua_State;
namespace df
{
struct viewscreen;
struct world_data;
struct map_block;
}
namespace DFHack
{
constexpr auto DFH_MOD_SHIFT = 1;
constexpr auto DFH_MOD_CTRL = 2;
constexpr auto DFH_MOD_ALT = 4;
constexpr auto DFH_MOD_SUPER = 8;
class Process;
class Module;
class Materials;
struct VersionInfo;
class VersionInfoFactory;
class PluginManager;
class Core;
class ServerMain;
class CoreSuspender;
class HotkeyManager;
namespace Lua { namespace Core {
DFHACK_EXPORT void Reset(color_ostream &out, const char *where);
} }
namespace Screen
{
struct Hide;
}
class DFHACK_EXPORT PerfCounters
{
public:
uint32_t baseline_elapsed_ms;
uint32_t elapsed_ms;
uint32_t total_update_ms;
uint32_t update_event_manager_ms;
uint32_t update_plugin_ms;
uint32_t update_lua_ms;
uint32_t total_keybinding_ms;
uint32_t total_overlay_ms;
std::unordered_map<int32_t, uint32_t> event_manager_event_total_ms;
std::unordered_map<int32_t, std::unordered_map<std::string, uint32_t>> event_manager_event_per_plugin_ms;
std::unordered_map<std::string, uint32_t> update_per_plugin;
std::unordered_map<std::string, uint32_t> state_change_per_plugin;
std::unordered_map<std::string, uint32_t> update_lua_per_repeat;
std::unordered_map<std::string, uint32_t> overlay_per_widget;
std::unordered_map<std::string, uint32_t> zscreen_per_focus;
void reset(bool ignorePauseState = false);
bool getIgnorePauseState();
// noop if game is paused and getIgnorePauseState() returns false
void incCounter(uint32_t &counter, uint32_t baseline_ms);
// returns number of unpaused ms since last tick
uint32_t registerTick(uint32_t baseline_ms);
uint32_t getUnpausedFps();
private:
bool ignore_pause_state = false;
static const size_t RECENT_TICKS_HISTORY_SIZE = 1000;
int32_t last_frame_counter;
uint32_t last_tick_baseline_ms;
struct {
uint32_t history[RECENT_TICKS_HISTORY_SIZE];
size_t head_idx;
bool full;
uint32_t sum_ms;
} recent_ticks;
};
class DFHACK_EXPORT StateChangeScript
{
public:
state_change_event event;
std::string path;
bool save_specific;
StateChangeScript(state_change_event event, std::string path, bool save_specific = false)
:event(event), path(path), save_specific(save_specific)
{ }
bool const operator==(const StateChangeScript& other)
{
return event == other.event && path == other.path && save_specific == other.save_specific;
}
bool const operator!=(const StateChangeScript& other)
{
return !(operator==(other));
}
};
// Core is a singleton. Why? Because it is closely tied to SDL calls. It tracks the global state of DF.
// There should never be more than one instance
// Better than tracking some weird variables all over the place.
class DFHACK_EXPORT Core
{
friend void ::dfhooks_init();
friend void ::dfhooks_shutdown();
friend void ::dfhooks_update();
friend void ::dfhooks_prerender();
friend bool ::dfhooks_sdl_event(SDL_Event* event);
friend void ::dfhooks_sdl_loop();
friend bool ::dfhooks_ncurses_key(int key);
public:
/// Get the current active Core instance. will assert if none exists
/// Use noInstance() to check first if unsure
static Core& getInstance();
static bool noInstance() { return active_instance == nullptr; }
/// check if the activity lock is owned by this thread
bool isSuspended(void);
/// Is everything OK?
bool isValid(void) { return !errorstate; }
/// get the materials module
Materials * getMaterials();
/// get the graphic module
Graphic * getGraphic();
command_result runCommand(color_ostream &out, const std::string &command, std::vector <std::string> ¶meters, bool no_autocomplete = false);
command_result runCommand(color_ostream& out, const std::string& command);
bool loadScriptFile(color_ostream &out, std::filesystem::path fname, bool silent = false);
bool addScriptPath(std::filesystem::path path, bool search_before = false);
bool setModScriptPaths(const std::vector<std::filesystem::path> & mod_script_paths);
bool removeScriptPath(std::filesystem::path path);
std::filesystem::path findScript(std::string name);
void getScriptPaths(std::vector<std::filesystem::path> *dest);
bool getSuppressDuplicateKeyboardEvents() const;
void setSuppressDuplicateKeyboardEvents(bool suppress);
void setMortalMode(bool value);
bool getMortalMode();
void setArmokTools(const std::vector<std::string> &tool_names);
bool isArmokTool(const std::string& name);
int8_t getModstate() { return modstate; }
bool AddAlias(const std::string &name, const std::vector<std::string> &command, bool replace = false);
bool RemoveAlias(const std::string &name);
bool IsAlias(const std::string &name);
bool RunAlias(color_ostream &out, const std::string &name,
const std::vector<std::string> ¶meters, command_result &result);
std::map<std::string, std::vector<std::string>> ListAliases();
std::string GetAliasCommand(const std::string &name, bool ignore_params = false);
std::filesystem::path getHackPath();
bool isWorldLoaded() { return (last_world_data_ptr != nullptr); }
bool isMapLoaded() { return (last_local_map_ptr != nullptr && last_world_data_ptr != nullptr); }
static df::viewscreen *getTopViewscreen();
DFHack::Console &getConsole() { return con; }
std::unique_ptr<DFHack::Process> p;
std::shared_ptr<DFHack::VersionInfo> vinfo;
template <typename... Args>
static void print(fmt::format_string<Args...> format, Args&& ... args)
{
color_ostream_proxy proxy(getInstance().con);
proxy.print(format, std::forward<Args>(args)...);
}
template <typename... Args>
static void printerr(fmt::format_string<Args...> format, Args&& ... args)
{
color_ostream_proxy proxy(getInstance().con);
proxy.printerr(format, std::forward<Args>(args)...);
}
PluginManager* getPluginManager() const { return plug_mgr; }
HotkeyManager* getHotkeyManager() { return hotkey_mgr; }
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());
return State;
}
static command_result enableLuaScript(color_ostream& out, const std::string_view name, bool enabled);
const std::vector<StateChangeScript> getStateChangeScripts() const
{
return state_change_scripts;
}
void addStateChangeScript(const StateChangeScript& script)
{
state_change_scripts.push_back(script);
}
bool removeStateChangeScript(const StateChangeScript& script)
{
auto it = std::find(state_change_scripts.begin(), state_change_scripts.end(), script);
if (it != state_change_scripts.end())
{
state_change_scripts.erase(it);
return true;
}
return false;
}
Core();
~Core();
private:
static Core* active_instance;
DFHack::Console con;
struct Private;
std::unique_ptr<Private> d;
bool InitMainThread();
bool InitSimulationThread();
int Update (void);
int Shutdown (void);
bool DFH_SDL_Event(SDL_Event* event);
void DFH_SDL_Loop();
bool ncurses_wgetch(int in, int & out);
bool DFH_ncurses_key(int key);
bool doSdlInputEvent(SDL_Event* event);
void doUpdate(color_ostream &out);
void onUpdate(color_ostream &out);
void onStateChange(color_ostream &out, state_change_event event);
void handleLoadAndUnloadScripts(color_ostream &out, state_change_event event);
Core(Core const&) = delete;
void operator=(Core const&) = delete;
// report error to user while failing
void fatal (std::string output, const char * title = nullptr);
// 1 = fatal failure
bool errorstate;
// regulate access to DF
struct Cond;
// FIXME: shouldn't be kept around like this
std::unique_ptr<DFHack::VersionInfoFactory> vif;
// Module storage
struct
{
Materials * pMaterials;
Graphic * pGraphic;
} s_mods;
std::vector<std::unique_ptr<Module>> allModules;
DFHack::PluginManager *plug_mgr;
// Hotkey Manager
DFHack::HotkeyManager *hotkey_mgr;
std::vector<std::filesystem::path> script_paths[3];
std::mutex script_path_mutex;
int8_t modstate;
bool suppress_duplicate_keyboard_events;
std::atomic<bool> mortal_mode;
std::unordered_set<std::string> armok_tools;
std::mutex armok_mutex;
std::map<std::string, std::vector<std::string>> aliases;
std::recursive_mutex alias_mutex;
// for state change tracking
df::world_data *last_world_data_ptr;
// for state change tracking
df::map_block**** last_local_map_ptr;
friend struct Screen::Hide;
df::viewscreen *top_viewscreen;
bool last_pause_state;
// Very important!
std::atomic<bool> started;
// Additional state change scripts
std::vector<StateChangeScript> state_change_scripts;
/*!
* \defgroup core_suspend CoreSuspender state handling serialization to
* DF memory.
* \sa DFHack::CoreSuspender
* \{
*/
std::recursive_timed_mutex CoreSuspendMutex;
std::condition_variable_any CoreWakeup;
std::atomic<std::thread::id> ownerThread;
std::atomic<size_t> toolCount;
std::atomic<bool> shutdown;
//! \}
std::thread::id df_render_thread;
std::thread::id df_simulation_thread;
lua_State* State;
uint32_t unpaused_ms; // reset to 0 on map load
friend class CoreService;
friend class ServerConnection;
friend class CoreSuspender;
friend class CoreSuspenderBase;
friend struct CoreSuspendClaimMain;
friend struct CoreSuspendReleaseMain;
};
class CoreSuspenderBase : protected std::unique_lock< decltype(Core::CoreSuspendMutex) > {
protected:
using mutex_type = decltype(Core::CoreSuspendMutex);
using parent_t = std::unique_lock< mutex_type >;
std::thread::id tid;
Core& core;
CoreSuspenderBase() : CoreSuspenderBase{ Core::getInstance() } {}
CoreSuspenderBase(Core& core) :
/* Lock the core (jk not really) */
parent_t{core.CoreSuspendMutex,std::defer_lock},
/* Mark this thread to be the core owner */
tid{},
core{ core }
{
assert(core.df_render_thread != std::thread::id{} && core.df_render_thread != std::this_thread::get_id());
}
public:
void lock()
{
parent_t::lock();
complete_lock();
}
bool try_lock()
{
bool locked = parent_t::try_lock_for(std::chrono::milliseconds(100));
if (locked) complete_lock();
return locked;
}
void unlock()
{
/* Restore core owner to previous value */
if (tid == std::thread::id{})
Lua::Core::Reset(core.getConsole(), "suspend");
core.ownerThread.store(tid, std::memory_order_release);
parent_t::unlock();
}
~CoreSuspenderBase() {
if (owns_lock())
unlock();
}
protected:
void complete_lock()
{
tid = core.ownerThread.exchange(std::this_thread::get_id(),
std::memory_order_acquire);
}
friend class MainThread;
};
/*!
* CoreSuspender allows serialization to DF data with std::unique_lock like
* interface. It includes handling for recursive CoreSuspender calls and
* notification to main thread after all queue tools have been handled.
*
* State transitions are:
* - Startup setups Core::SuspendMutex to unlocked states
* - Core::Init locks Core::SuspendMutex until the thread exits or that thread
* calls Core::Shutdown or Core::~Core.
* - Other thread request core suspend by atomic incrementation of Core::toolCount
* and then locking Core::CoreSuspendMutex. After locking CoreSuspendMutex
* success callers exchange their std::thread::id to Core::ownerThread.
* - Core::Update() makes sure that queued tools are run when it calls
* Core::CoreWakup::wait. The wait keeps Core::CoreSuspendMutex unlocked
* and waits until Core::toolCount is reduced back to zero.
* - CoreSuspender::~CoreSuspender() first stores the previous Core::ownerThread
* back. In case of recursive call Core::ownerThread equals tid. If tis is
* zero then we are releasing the recursive_mutex which means suspend
* context is over. It is time to reset lua.
* The last step is to decrement Core::toolCount and wakeup main thread if
* no more tools are queued trying to acquire the
* Core::CoreSuspenderMutex.
*
* The public API for CoreSuspender only supports construction and destruction;
* all other locking operations are reserved
*/
class CoreSuspender : protected CoreSuspenderBase {
using parent_t = CoreSuspenderBase;
public:
CoreSuspender() : CoreSuspender{Core::getInstance()} {}
CoreSuspender(Core& core) : CoreSuspenderBase{core}
{
lock();
}
// note that this is needed so the destructor will call CoreSuspender::unlock instead of CoreSuspenderBase::unlock
~CoreSuspender() {
if (owns_lock())
unlock();
}
protected:
// deferred locking is not part of CoreSuspender's public API
// these constructors are only for use in derived classes,
// specifically ConditionalCoreSuspender
CoreSuspender(std::defer_lock_t d) : CoreSuspender{ Core::getInstance(),d } {}
CoreSuspender(Core& core, std::defer_lock_t d) : CoreSuspenderBase{ core } {}
void lock()
{
inc_tool_count();
parent_t::lock();
}
bool try_lock()
{
inc_tool_count();
if (parent_t::try_lock())
return true;
dec_tool_count();
return false;
}
void unlock()
{
parent_t::unlock();
dec_tool_count();
}
void inc_tool_count()
{
core.toolCount.fetch_add(1, std::memory_order_relaxed);
}
void dec_tool_count()
{
/* Notify core to continue when all queued tools have completed
* 0 = None wants to own the core
* 1+ = There are tools waiting core access
* fetch_add returns old value before subtraction
*/
if (core.toolCount.fetch_add(-1, std::memory_order_relaxed) == 1)
core.CoreWakeup.notify_one();
}
};
/*!
* ConditionalCoreSuspender attempts to acquire a CoreSuspender, but will fail if doing so
* would cause a thread wait. The caller can determine if the suspender was acquired by casting
* the created object to bool:
* ConditionalCoreSuspender suspend;
* if (suspend) {
* ...
* }
*/
class ConditionalCoreSuspender : protected CoreSuspender {
public:
ConditionalCoreSuspender() : ConditionalCoreSuspender{ Core::getInstance() } { }
ConditionalCoreSuspender(Core& core) : CoreSuspender{ core, std::defer_lock } { try_lock(); }
operator bool() const { return owns_lock(); }
};
// unclassified functions related to core
void help_helper(color_ostream& con, const std::string& entry_name);
std::string dfhack_version_desc();
bool is_builtin(color_ostream& con, const std::string& command);
std::string sc_event_name(state_change_event id);
state_change_event sc_event_id(std::string name);
}