1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 21:49:55 +00:00

removed function ScriptManager::getLocalIndex (was redundant and was also depending on precompiled scripts)

This commit is contained in:
Marc Zinnschlag 2014-07-25 07:59:50 +02:00
parent f55084463b
commit 2e355df8b3
6 changed files with 45 additions and 80 deletions

View file

@ -50,12 +50,6 @@ namespace MWBase
///< Return locals for script \a name.
virtual MWScript::GlobalScripts& getGlobalScripts() = 0;
virtual int getLocalIndex (const std::string& scriptId, const std::string& variable,
char type) = 0;
///< Return index of the variable of the given name and type in the given script. Will
/// throw an exception, if there is no such script or variable or the type does not match.
};
}

View file

@ -3,8 +3,12 @@
#include <cmath>
#include <stdexcept>
#include <sstream>
#include <components/interpreter/types.hpp>
#include <components/compiler/locals.hpp>
#include "../mwworld/esmstore.hpp"
#include "../mwbase/environment.hpp"
@ -103,6 +107,32 @@ namespace MWScript
}
}
int InterpreterContext::findLocalVariableIndex (const std::string& scriptId,
const std::string& name, char type) const
{
int index = MWBase::Environment::get().getScriptManager()->getLocals (scriptId).
search (type, name);
if (index!=-1)
return index;
std::ostringstream stream;
stream << "Failed to access ";
switch (type)
{
case 's': stream << "short"; break;
case 'l': stream << "long"; break;
case 'f': stream << "float"; break;
}
stream << " member variable " << name << " in script " << scriptId;
throw std::runtime_error (stream.str().c_str());
}
InterpreterContext::InterpreterContext (
MWScript::Locals *locals, MWWorld::Ptr reference, const std::string& targetId)
: mLocals (locals), mReference (reference),
@ -485,10 +515,7 @@ namespace MWScript
const Locals& locals = getMemberLocals (scriptId, global);
int index = MWBase::Environment::get().getScriptManager()->getLocalIndex (
scriptId, name, 's');
return locals.mShorts[index];
return locals.mShorts[findLocalVariableIndex (scriptId, name, 's')];
}
int InterpreterContext::getMemberLong (const std::string& id, const std::string& name,
@ -498,10 +525,7 @@ namespace MWScript
const Locals& locals = getMemberLocals (scriptId, global);
int index = MWBase::Environment::get().getScriptManager()->getLocalIndex (
scriptId, name, 'l');
return locals.mLongs[index];
return locals.mLongs[findLocalVariableIndex (scriptId, name, 'l')];
}
float InterpreterContext::getMemberFloat (const std::string& id, const std::string& name,
@ -511,10 +535,7 @@ namespace MWScript
const Locals& locals = getMemberLocals (scriptId, global);
int index = MWBase::Environment::get().getScriptManager()->getLocalIndex (
scriptId, name, 'f');
return locals.mFloats[index];
return locals.mFloats[findLocalVariableIndex (scriptId, name, 'f')];
}
void InterpreterContext::setMemberShort (const std::string& id, const std::string& name,
@ -524,10 +545,7 @@ namespace MWScript
Locals& locals = getMemberLocals (scriptId, global);
int index =
MWBase::Environment::get().getScriptManager()->getLocalIndex (scriptId, name, 's');
locals.mShorts[index] = value;
locals.mShorts[findLocalVariableIndex (scriptId, name, 's')] = value;
}
void InterpreterContext::setMemberLong (const std::string& id, const std::string& name, int value, bool global)
@ -536,10 +554,7 @@ namespace MWScript
Locals& locals = getMemberLocals (scriptId, global);
int index =
MWBase::Environment::get().getScriptManager()->getLocalIndex (scriptId, name, 'l');
locals.mLongs[index] = value;
locals.mLongs[findLocalVariableIndex (scriptId, name, 'l')] = value;
}
void InterpreterContext::setMemberFloat (const std::string& id, const std::string& name, float value, bool global)
@ -548,10 +563,7 @@ namespace MWScript
Locals& locals = getMemberLocals (scriptId, global);
int index =
MWBase::Environment::get().getScriptManager()->getLocalIndex (scriptId, name, 'f');
locals.mFloats[index] = value;
locals.mFloats[findLocalVariableIndex (scriptId, name, 'f')] = value;
}
MWWorld::Ptr InterpreterContext::getReference(bool required)

View file

@ -50,6 +50,10 @@ namespace MWScript
Locals& getMemberLocals (std::string& id, bool global);
///< \a id is changed to the respective script ID, if \a id wasn't a script ID before
/// Throws an exception if local variable can't be found.
int findLocalVariableIndex (const std::string& scriptId, const std::string& name,
char type) const;
public:
InterpreterContext (MWScript::Locals *locals, MWWorld::Ptr reference,

View file

@ -196,46 +196,4 @@ namespace MWScript
{
return mGlobalScripts;
}
int ScriptManager::getLocalIndex (const std::string& scriptId, const std::string& variable,
char type)
{
const ESM::Script *script = mStore.get<ESM::Script>().find (scriptId);
int offset = 0;
int size = 0;
switch (type)
{
case 's':
offset = 0;
size = script->mData.mNumShorts;
break;
case 'l':
offset = script->mData.mNumShorts;
size = script->mData.mNumLongs;
break;
case 'f':
offset = script->mData.mNumShorts+script->mData.mNumLongs;
size = script->mData.mNumFloats;
break;
default:
throw std::runtime_error ("invalid variable type");
}
std::string variable2 = Misc::StringUtils::lowerCase (variable);
for (int i=0; i<size; ++i)
if (Misc::StringUtils::lowerCase (script->mVarNames.at (i+offset))==variable2)
return i;
throw std::runtime_error ("unable to access local variable " + variable + " of " + scriptId);
}
}

View file

@ -71,11 +71,6 @@ namespace MWScript
///< Return locals for script \a name.
virtual GlobalScripts& getGlobalScripts();
virtual int getLocalIndex (const std::string& scriptId, const std::string& variable,
char type);
///< Return index of the variable of the given name and type in the given script. Will
/// throw an exception, if there is no such script or variable or the type does not match.
};
}

View file

@ -17,8 +17,6 @@ namespace Compiler
int searchIndex (char type, const std::string& name) const;
bool search (char type, const std::string& name) const;
std::vector<std::string>& get (char type);
public:
@ -29,6 +27,10 @@ namespace Compiler
int getIndex (const std::string& name) const;
///< return index for local variable \a name (-1: does not exist).
/// Return index for local variable \a name of type \a type (-1: variable does not
/// exit).
bool search (char type, const std::string& name) const;
const std::vector<std::string>& get (char type) const;
void write (std::ostream& localFile) const;