Implement ShowVars (SV) console function

actorid
Chris Robinson 12 years ago
parent 2353ac1739
commit 30973352e8

@ -349,5 +349,7 @@ op 0x2000219: UndoWerewolf
op 0x200021a: UndoWerewolfExplicit
op 0x200021b: SetWerewolfAcrobatics
op 0x200021c: SetWerewolfAcrobaticsExplicit
op 0x200021d: ShowVars
op 0x200021e: ShowVarsExplicit
opcodes 0x200021d-0x3ffffff unused
opcodes 0x200021f-0x3ffffff unused

@ -5,6 +5,7 @@
#include <components/compiler/extensions.hpp>
#include <components/compiler/opcodes.hpp>
#include <components/compiler/locals.hpp>
#include <components/interpreter/interpreter.hpp>
#include <components/interpreter/runtime.hpp>
@ -12,6 +13,7 @@
#include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/scriptmanager.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/player.hpp"
@ -630,6 +632,99 @@ namespace MWScript
};
class OpShowVarsExplicit : public Interpreter::Opcode0
{
protected:
void printLocalVars(Interpreter::Runtime &runtime, const MWWorld::Ptr &ptr)
{
std::stringstream str;
const std::string script = MWWorld::Class::get(ptr).getScript(ptr);
if(script.empty())
str<< ptr.getCellRef().mRefID<<" ("<<ptr.getRefData().getHandle()<<") does not have a script.";
else
{
str<< "Local variables for "<<ptr.getCellRef().mRefID<<" ("<<ptr.getRefData().getHandle()<<")";
const Locals &locals = ptr.getRefData().getLocals();
const Compiler::Locals &complocals = MWBase::Environment::get().getScriptManager()->getLocals(script);
const std::vector<std::string> *names = &complocals.get('s');
for(size_t i = 0;i < names->size();++i)
{
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());
}
public:
virtual void execute (Interpreter::Runtime& runtime)
{
MWWorld::Ptr ptr = ExplicitRef()(runtime);
printLocalVars(runtime, ptr);
}
};
class OpShowVarsImplicit : public OpShowVarsExplicit
{
void printGlobalVars(Interpreter::Runtime &runtime)
{
std::stringstream str;
str<< "Global variables:";
MWBase::World *world = MWBase::Environment::get().getWorld();
std::vector<std::string> names = world->getGlobals();
for(size_t i = 0;i < names.size();++i)
{
char type = world->getGlobalVariableType(names[i]);
if(type == 's')
str<<std::endl<< " "<<names[i]<<" = "<<world->getGlobalVariable(names[i]).mShort<<" (short)";
else if(type == 'l')
str<<std::endl<< " "<<names[i]<<" = "<<world->getGlobalVariable(names[i]).mLong<<" (long)";
else if(type == 'f')
str<<std::endl<< " "<<names[i]<<" = "<<world->getGlobalVariable(names[i]).mFloat<<" (float)";
else
str<<std::endl<< " "<<names[i]<<" = "<<((void*)*(int*)&world->getGlobalVariable(names[i]).mLong)<<" (unknown)";
}
runtime.getContext().report(str.str());
}
public:
virtual void execute(Interpreter::Runtime& runtime)
{
// No way to tell if we have a reference before trying to get it, and it will
// cause an exception is there isn't one :(
try {
MWWorld::Ptr ptr = ImplicitRef()(runtime);
printLocalVars(runtime, ptr);
}
catch(std::runtime_error&) {
// No reference, no problem.
printGlobalVars(runtime);
}
}
};
void installOpcodes (Interpreter::Interpreter& interpreter)
{
interpreter.installSegment5 (Compiler::Misc::opcodeXBox, new OpXBox);
@ -685,6 +780,8 @@ namespace MWScript
interpreter.installSegment5 (Compiler::Misc::opcodeHitOnMeExplicit, new OpHitOnMe<ExplicitRef>);
interpreter.installSegment5 (Compiler::Misc::opcodeDisableTeleporting, new OpEnableTeleporting<false>);
interpreter.installSegment5 (Compiler::Misc::opcodeEnableTeleporting, new OpEnableTeleporting<true>);
interpreter.installSegment5 (Compiler::Misc::opcodeShowVars, new OpShowVarsImplicit);
interpreter.installSegment5 (Compiler::Misc::opcodeShowVarsExplicit, new OpShowVarsExplicit);
}
}
}

@ -256,6 +256,8 @@ namespace Compiler
extensions.registerFunction ("hitonme", 'l', "S", opcodeHitOnMe, opcodeHitOnMeExplicit);
extensions.registerInstruction ("disableteleporting", "", opcodeDisableTeleporting);
extensions.registerInstruction ("enableteleporting", "", opcodeEnableTeleporting);
extensions.registerInstruction ("showvars", "", opcodeShowVars, opcodeShowVarsExplicit);
extensions.registerInstruction ("sv", "", opcodeShowVars, opcodeShowVarsExplicit);
}
}

@ -15,8 +15,6 @@ namespace Compiler
std::vector<std::string> mLongs;
std::vector<std::string> mFloats;
const std::vector<std::string>& get (char type) const;
int searchIndex (char type, const std::string& name) const;
bool search (char type, const std::string& name) const;
@ -31,6 +29,8 @@ namespace Compiler
int getIndex (const std::string& name) const;
///< return index for local variable \a name (-1: does not exist).
const std::vector<std::string>& get (char type) const;
void write (std::ostream& localFile) const;
///< write declarations to file.

@ -219,6 +219,8 @@ namespace Compiler
const int opcodeHitOnMeExplicit = 0x2000214;
const int opcodeDisableTeleporting = 0x2000215;
const int opcodeEnableTeleporting = 0x2000216;
const int opcodeShowVars = 0x200021d;
const int opcodeShowVarsExplicit = 0x200021e;
}
namespace Sky

Loading…
Cancel
Save