1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 09:35:28 +00:00
OpenMW/apps/openmw/mwscript/globalscripts.hpp

96 lines
2.5 KiB
C++
Raw Normal View History

2010-07-04 10:43:34 +02:00
#ifndef GAME_SCRIPT_GLOBALSCRIPTS_H
#define GAME_SCRIPT_GLOBALSCRIPTS_H
#include <string>
#include <string_view>
2022-08-12 20:56:50 +02:00
#include <unordered_map>
#include <memory>
#include <utility>
2022-04-06 23:00:13 +02:00
#include <variant>
#include <cstdint>
2022-08-12 20:56:50 +02:00
#include <components/misc/algorithm.hpp>
2010-07-04 10:43:34 +02:00
#include "locals.hpp"
#include "../mwworld/ptr.hpp"
namespace ESM
{
class ESMWriter;
class ESMReader;
struct RefNum;
}
namespace Loading
{
class Listener;
}
namespace MWWorld
2010-07-04 10:43:34 +02:00
{
class ESMStore;
2010-07-04 10:43:34 +02:00
}
namespace MWScript
{
struct GlobalScriptDesc
{
bool mRunning;
Locals mLocals;
2022-04-06 23:00:13 +02:00
std::variant<MWWorld::Ptr, std::pair<ESM::RefNum, std::string>> mTarget; // Used to start targeted script
GlobalScriptDesc();
const MWWorld::Ptr* getPtrIfPresent() const; // Returns a Ptr if one has been resolved
MWWorld::Ptr getPtr(); // Resolves mTarget to a Ptr and caches the (potentially empty) result
std::string_view getId() const; // Returns the target's ID -- if any
};
2010-07-04 10:43:34 +02:00
class GlobalScripts
{
2012-10-01 19:17:04 +04:00
const MWWorld::ESMStore& mStore;
2022-08-12 20:56:50 +02:00
std::unordered_map<std::string, std::shared_ptr<GlobalScriptDesc>, ::Misc::StringUtils::CiHash, ::Misc::StringUtils::CiEqual> mScripts;
2010-07-04 10:43:34 +02:00
public:
2012-10-01 19:17:04 +04:00
GlobalScripts (const MWWorld::ESMStore& store);
void addScript(std::string_view name, const MWWorld::Ptr& target = MWWorld::Ptr());
void removeScript (std::string_view name);
bool isRunning (std::string_view name) const;
void run();
///< run all active global scripts
void clear();
void addStartup();
///< Add startup script
int countSavedGameRecords() const;
void write (ESM::ESMWriter& writer, Loading::Listener& progress) const;
bool readRecord (ESM::ESMReader& reader, uint32_t type, const std::map<int, int>& contentFileMap);
///< Records for variables that do not exist are dropped silently.
///
/// \return Known type?
Locals& getLocals(std::string_view name);
///< If the script \a name has not been added as a global script yet, it is added
/// automatically, but is not set to running state.
const Locals* getLocalsIfPresent(std::string_view name) const;
2020-07-29 18:43:56 +02:00
void updatePtrs(const MWWorld::Ptr& base, const MWWorld::Ptr& updated);
///< Update the Ptrs stored in mTarget. Should be called after the reference has been moved to a new cell.
2010-07-04 10:43:34 +02:00
};
}
#endif