dfhack/plugins/spectate/pause.h

64 lines
2.8 KiB
C

#pragma once
#include <unordered_set>
#include <string>
2022-10-14 13:41:36 -06:00
#include <atomic>
#include <ColorText.h>
namespace DFHack {
////////////
// Locking mechanisms for control over pausing
namespace Pausing
{
class Lock
{
2022-10-14 13:41:36 -06:00
std::atomic_bool locked{};
public:
2022-10-14 13:41:36 -06:00
const std::string name;
explicit Lock(const char* name) : name(name){ locked = false; }
virtual ~Lock()= default;
virtual bool isAnyLocked() const = 0;
virtual bool isOnlyLocked() const = 0;
bool isLocked() const { return locked; }
2022-10-14 13:41:36 -06:00
virtual void lock() { locked = true; } //simply locks the lock
void unlock() { locked = false; }
virtual void reportLocks(color_ostream &out) = 0;
};
// non-blocking lock resource used in conjunction with the announcement functions in World
class AnnouncementLock : public Lock
{
static std::unordered_set<Lock*> locks;
public:
explicit AnnouncementLock(const char* name): Lock(name) { locks.emplace(this); }
~AnnouncementLock() override { locks.erase(this); }
bool captureState(); // captures the state of announcement settings, iff this is the only locked lock (note it does nothing if 0 locks are engaged)
void lock() override; // locks and attempts to capture state
bool isAnyLocked() const override; // returns true if any instance of AnnouncementLock is locked
bool isOnlyLocked() const override; // returns true if locked and no other instance is locked
2022-10-14 13:41:36 -06:00
void reportLocks(color_ostream &out) override;
};
// non-blocking lock resource used in conjunction with the Player pause functions in World
class PlayerLock : public Lock
{
static std::unordered_set<Lock*> locks;
public:
explicit PlayerLock(const char* name): Lock(name) { locks.emplace(this); }
~PlayerLock() override { locks.erase(this); }
bool isAnyLocked() const override; // returns true if any instance of PlayerLock is locked
bool isOnlyLocked() const override; // returns true if locked and no other instance is locked
2022-10-14 13:41:36 -06:00
void reportLocks(color_ostream &out) override;
};
}
namespace World {
2022-10-14 13:41:36 -06:00
bool DisableAnnouncementPausing(color_ostream &out); // disable announcement pausing if all locks are open
bool SaveAnnouncementSettings(); // save current announcement pause settings if all locks are open
bool RestoreAnnouncementSettings(); // restore saved announcement pause settings if all locks are open
bool EnablePlayerPausing(); // enable player pausing if all locks are open
bool DisablePlayerPausing(); // disable player pausing if all locks are open
void Update();
}
2022-10-12 15:44:51 -06:00
}