refactor opshowvars

This commit is contained in:
Kindi 2025-04-04 11:53:55 +08:00 committed by Kuyondo
parent d40c4855b1
commit c4a4c435f1

View File

@ -1146,9 +1146,18 @@ namespace MWScript
{ {
void printLocalVars(Interpreter::Runtime& runtime, const MWWorld::Ptr& ptr) void printLocalVars(Interpreter::Runtime& runtime, const MWWorld::Ptr& ptr)
{ {
std::stringstream str; std::ostringstream str;
const ESM::RefId& script = ptr.getClass().getScript(ptr); const ESM::RefId& script = ptr.getClass().getScript(ptr);
auto printVariables = [&str](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 << "\n " << names[i] << " = " << values[i] << " (" << type << ")";
}
};
if (script.empty()) if (script.empty())
str << ptr.getCellRef().getRefId() << " does not have a script."; str << ptr.getCellRef().getRefId() << " does not have a script.";
else else
@ -1159,27 +1168,9 @@ namespace MWScript
const Compiler::Locals& complocals const Compiler::Locals& complocals
= MWBase::Environment::get().getScriptManager()->getLocals(script); = MWBase::Environment::get().getScriptManager()->getLocals(script);
const std::vector<std::string>* names = &complocals.get('s'); printVariables(complocals.get('s'), locals.mShorts, "short");
for (size_t i = 0; i < names->size(); ++i) printVariables(complocals.get('l'), locals.mLongs, "long");
{ printVariables(complocals.get('f'), locals.mFloats, "float");
if (i >= locals.mShorts.size())
break;
str << std::endl << " " << (*names)[i] << " = " << locals.mShorts[i] << " (short)";
}
names = &complocals.get('l');
for (size_t i = 0; i < names->size(); ++i)
{
if (i >= locals.mLongs.size())
break;
str << std::endl << " " << (*names)[i] << " = " << locals.mLongs[i] << " (long)";
}
names = &complocals.get('f');
for (size_t i = 0; i < names->size(); ++i)
{
if (i >= locals.mFloats.size())
break;
str << std::endl << " " << (*names)[i] << " = " << locals.mFloats[i] << " (float)";
}
} }
runtime.getContext().report(str.str()); runtime.getContext().report(str.str());
@ -1187,50 +1178,43 @@ namespace MWScript
void printGlobalVars(Interpreter::Runtime& runtime) void printGlobalVars(Interpreter::Runtime& runtime)
{ {
std::stringstream str; std::ostringstream str;
str << "Global variables:"; str << "Global Variables:";
MWBase::World* world = MWBase::Environment::get().getWorld(); MWBase::World* world = MWBase::Environment::get().getWorld();
std::vector<std::string> names = runtime.getContext().getGlobals(); auto& context = runtime.getContext();
std::vector<std::string> names = context.getGlobals();
// sort for user convenience std::sort(names.begin(), names.end(), ::Misc::StringUtils::ciLess);
std::sort(names.begin(), names.end());
for (size_t i = 0; i < names.size(); ++i)
{
char type = world->getGlobalVariableType(names[i]);
str << std::endl << " " << names[i] << " = ";
auto printVariable = [&str, &context](const std::string& name, char type) {
str << "\n " << name << " = ";
switch (type) switch (type)
{ {
case 's': case 's':
str << context.getGlobalShort(name) << " (short)";
str << runtime.getContext().getGlobalShort(names[i]) << " (short)";
break; break;
case 'l': case 'l':
str << context.getGlobalLong(name) << " (long)";
str << runtime.getContext().getGlobalLong(names[i]) << " (long)";
break; break;
case 'f': case 'f':
str << context.getGlobalFloat(name) << " (float)";
str << runtime.getContext().getGlobalFloat(names[i]) << " (float)";
break; break;
default: default:
str << "<unknown type>"; str << "<unknown type>";
} }
} };
runtime.getContext().report(str.str()); for (const auto& name : names)
printVariable(name, world->getGlobalVariableType(name));
context.report(str.str());
} }
void printGlobalScriptsVars(Interpreter::Runtime& runtime) void printGlobalScriptsVars(Interpreter::Runtime& runtime)
{ {
std::stringstream str; std::ostringstream str;
str << std::endl << "Global Scripts:"; str << "\nGlobal Scripts:";
const auto& scripts = MWBase::Environment::get().getScriptManager()->getGlobalScripts().getScripts(); const auto& scripts = MWBase::Environment::get().getScriptManager()->getGlobalScripts().getScripts();
@ -1238,12 +1222,11 @@ namespace MWScript
std::map<ESM::RefId, std::shared_ptr<GlobalScriptDesc>> globalScripts(scripts.begin(), scripts.end()); std::map<ESM::RefId, std::shared_ptr<GlobalScriptDesc>> globalScripts(scripts.begin(), scripts.end());
auto printVariables auto printVariables
= [&str](const ESM::RefId& scptName, const auto& names, const auto& values, std::string_view type) { = [&str](std::string_view scptName, const auto& names, const auto& values, std::string_view type) {
size_t size = std::min(names.size(), values.size()); size_t size = std::min(names.size(), values.size());
for (size_t i = 0; i < size; ++i) for (size_t i = 0; i < size; ++i)
{ {
str << std::endl str << "\n " << scptName << "->" << names[i] << " = " << values[i] << " (" << type << ")";
<< " " << scptName << "->" << names[i] << " = " << values[i] << " (" << type << ")";
} }
}; };
@ -1253,18 +1236,20 @@ namespace MWScript
if (!script->mRunning) if (!script->mRunning)
continue; continue;
std::string_view scptName = refId.getRefIdString();
const Compiler::Locals& complocals const Compiler::Locals& complocals
= MWBase::Environment::get().getScriptManager()->getLocals(refId); = MWBase::Environment::get().getScriptManager()->getLocals(refId);
const Locals& locals const Locals& locals
= MWBase::Environment::get().getScriptManager()->getGlobalScripts().getLocals(refId); = MWBase::Environment::get().getScriptManager()->getGlobalScripts().getLocals(refId);
if (locals.isEmpty()) if (locals.isEmpty())
str << std::endl << " No variables in script " << refId; str << "\n No variables in script " << scptName;
else else
{ {
printVariables(refId, complocals.get('s'), locals.mShorts, "short"); printVariables(scptName, complocals.get('s'), locals.mShorts, "short");
printVariables(refId, complocals.get('l'), locals.mLongs, "long"); printVariables(scptName, complocals.get('l'), locals.mLongs, "long");
printVariables(refId, complocals.get('f'), locals.mFloats, "float"); printVariables(scptName, complocals.get('f'), locals.mFloats, "float");
} }
} }