1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-10-24 15:56:36 +00:00

show global script variables in showVars

This commit is contained in:
Kindi 2025-04-04 15:36:01 +08:00
parent d13f108779
commit 0a49c5f71e
2 changed files with 47 additions and 0 deletions

View file

@ -64,6 +64,8 @@ namespace MWScript
void removeScript(const ESM::RefId& name);
const std::unordered_map<ESM::RefId, std::shared_ptr<GlobalScriptDesc>>& getScripts() const { return mScripts; }
bool isRunning(const ESM::RefId& name) const;
void run();

View file

@ -1223,6 +1223,50 @@ namespace MWScript
runtime.getContext().report(str.str());
}
void printGlobalScriptsVars(Interpreter::Runtime& runtime)
{
std::stringstream str;
str << std::endl << "Global Scripts:";
const auto& scripts = MWBase::Environment::get().getScriptManager()->getGlobalScripts().getScripts();
// sort for user convenience
std::map<ESM::RefId, std::shared_ptr<GlobalScriptDesc>> globalScripts(scripts.begin(), scripts.end());
auto printVariables = [&str](std::string_view scptName, const auto& names, const auto& values,
std::string_view type) {
size_t size = std::min(names.size(), values.size());
for (size_t i = 0; i < size; ++i)
{
str << std::endl << scptName << "->" << names[i] << " = " << values[i] << " (" << type << ")";
}
};
for (const auto& [refId, script] : globalScripts)
{
// Skip dormant global scripts
if (!script->mRunning)
continue;
const std::string scptName = refId.serializeText();
const Compiler::Locals& complocals
= MWBase::Environment::get().getScriptManager()->getLocals(refId);
const Locals& locals
= MWBase::Environment::get().getScriptManager()->getGlobalScripts().getLocals(refId);
if (locals.isEmpty())
str << std::endl << "No variables in script " << scptName;
else
{
printVariables(scptName, complocals.get('s'), locals.mShorts, "short");
printVariables(scptName, complocals.get('l'), locals.mLongs, "long");
printVariables(scptName, complocals.get('f'), locals.mFloats, "float");
}
}
runtime.getContext().report(str.str());
}
public:
void execute(Interpreter::Runtime& runtime) override
{
@ -1233,6 +1277,7 @@ namespace MWScript
{
// No reference, no problem.
printGlobalVars(runtime);
printGlobalScriptsVars(runtime)
}
}
};