Merge branch 'showVars' into 'master'
Some checks failed
Build and test / Ubuntu (push) Has been cancelled
Build and test / MacOS (push) Has been cancelled
Build and test / Read .env file and expose it as output (push) Has been cancelled
Build and test / Windows (2019) (push) Has been cancelled
Build and test / Windows (2022) (push) Has been cancelled

Show global script variables in showVars

See merge request OpenMW/openmw!4614
This commit is contained in:
Kuyondo 2025-04-26 08:40:08 +00:00
commit 8c9f4677ff
2 changed files with 52 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

@ -1192,6 +1192,10 @@ namespace MWScript
MWBase::World* world = MWBase::Environment::get().getWorld();
std::vector<std::string> names = runtime.getContext().getGlobals();
// sort for user convenience
std::sort(names.begin(), names.end());
for (size_t i = 0; i < names.size(); ++i)
{
char type = world->getGlobalVariableType(names[i]);
@ -1223,6 +1227,51 @@ 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 +1282,7 @@ namespace MWScript
{
// No reference, no problem.
printGlobalVars(runtime);
printGlobalScriptsVars(runtime);
}
}
};