1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-30 12:32:36 +00:00
OpenMW/apps/openmw/mwscript/locals.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

254 lines
7.8 KiB
C++
Raw Normal View History

2013-02-03 13:27:27 +00:00
#include "locals.hpp"
2020-07-29 18:43:56 +02:00
#include "globalscripts.hpp"
2013-02-03 13:27:27 +00:00
#include <components/compiler/locals.hpp>
2018-08-14 23:05:43 +04:00
#include <components/debug/debuglog.hpp>
#include <components/esm3/loadscpt.hpp>
#include <components/esm3/locals.hpp>
#include <components/esm3/variant.hpp>
2013-02-03 13:27:27 +00:00
#include "../mwbase/environment.hpp"
#include "../mwbase/scriptmanager.hpp"
#include "../mwworld/esmstore.hpp"
2013-02-03 13:27:27 +00:00
namespace MWScript
{
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
void Locals::ensure(const ESM::RefId& scriptName)
2013-02-03 13:27:27 +00:00
{
if (!mInitialised)
{
2023-04-20 21:07:53 +02:00
const ESM::Script* script = MWBase::Environment::get().getESMStore()->get<ESM::Script>().find(scriptName);
configure(*script);
}
}
Locals::Locals()
: mInitialised(false)
{
}
bool Locals::configure(const ESM::Script& script)
{
if (mInitialised)
return false;
2023-04-01 15:15:28 +02:00
const GlobalScriptDesc* global
= MWBase::Environment::get().getScriptManager()->getGlobalScripts().getScriptIfPresent(script.mId);
2020-07-29 18:43:56 +02:00
if (global)
{
2023-04-01 15:15:28 +02:00
mShorts = global->mLocals.mShorts;
mLongs = global->mLocals.mLongs;
mFloats = global->mLocals.mFloats;
2020-07-29 18:43:56 +02:00
}
else
{
const Compiler::Locals& locals = MWBase::Environment::get().getScriptManager()->getLocals(script.mId);
mShorts.clear();
mShorts.resize(locals.get('s').size(), 0);
mLongs.clear();
mLongs.resize(locals.get('l').size(), 0);
mFloats.clear();
mFloats.resize(locals.get('f').size(), 0);
}
mScriptId = script.mId;
mInitialised = true;
return true;
2013-02-03 13:27:27 +00:00
}
2013-03-31 13:13:46 +02:00
bool Locals::isEmpty() const
{
return (mShorts.empty() && mLongs.empty() && mFloats.empty());
}
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
bool Locals::hasVar(const ESM::RefId& script, std::string_view var)
{
ensure(script);
const Compiler::Locals& locals = MWBase::Environment::get().getScriptManager()->getLocals(script);
int index = locals.getIndex(var);
return (index != -1);
}
double Locals::getVarAsDouble(const ESM::RefId& script, std::string_view var)
2013-03-31 13:13:46 +02:00
{
ensure(script);
2014-07-25 09:26:30 +02:00
const Compiler::Locals& locals = MWBase::Environment::get().getScriptManager()->getLocals(script);
2013-03-31 13:13:46 +02:00
int index = locals.getIndex(var);
if (index == -1)
return 0;
switch (locals.getType(var))
2013-03-31 13:13:46 +02:00
{
case 's':
return mShorts.at(index);
2016-02-27 13:40:53 +01:00
case 'l':
return mLongs.at(index);
2016-02-27 13:40:53 +01:00
case 'f':
return mFloats.at(index);
default:
return 0;
2013-03-31 13:13:46 +02:00
}
}
bool Locals::setVar(const ESM::RefId& script, std::string_view var, double val)
{
ensure(script);
2014-07-25 09:26:30 +02:00
const Compiler::Locals& locals = MWBase::Environment::get().getScriptManager()->getLocals(script);
2013-02-03 13:27:27 +00:00
int index = locals.getIndex(var);
if (index == -1)
return false;
switch (locals.getType(var))
2013-02-03 13:27:27 +00:00
{
case 's':
mShorts.at(index) = static_cast<Interpreter::Type_Short>(val);
break;
case 'l':
mLongs.at(index) = static_cast<Interpreter::Type_Integer>(val);
break;
case 'f':
mFloats.at(index) = static_cast<Interpreter::Type_Float>(val);
break;
2013-02-03 13:27:27 +00:00
}
return true;
2013-02-03 13:27:27 +00:00
}
2024-06-16 19:12:36 +02:00
std::size_t Locals::getSize(const ESM::RefId& script)
{
ensure(script);
return mShorts.size() + mLongs.size() + mFloats.size();
}
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
bool Locals::write(ESM::Locals& locals, const ESM::RefId& script) const
{
if (!mInitialised)
return false;
const Compiler::Locals& declarations = MWBase::Environment::get().getScriptManager()->getLocals(script);
for (int i = 0; i < 3; ++i)
{
char type = 0;
switch (i)
{
case 0:
type = 's';
break;
case 1:
type = 'l';
break;
case 2:
type = 'f';
break;
}
const std::vector<std::string>& names = declarations.get(type);
for (int i2 = 0; i2 < static_cast<int>(names.size()); ++i2)
{
ESM::Variant value;
switch (i)
{
case 0:
value.setType(ESM::VT_Int);
value.setInteger(mShorts.at(i2));
break;
case 1:
value.setType(ESM::VT_Int);
value.setInteger(mLongs.at(i2));
break;
case 2:
value.setType(ESM::VT_Float);
value.setFloat(mFloats.at(i2));
break;
}
locals.mVariables.emplace_back(names[i2], value);
}
}
return true;
}
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
void Locals::read(const ESM::Locals& locals, const ESM::RefId& script)
{
ensure(script);
const Compiler::Locals& declarations = MWBase::Environment::get().getScriptManager()->getLocals(script);
int index = 0, numshorts = 0, numlongs = 0;
for (unsigned int v = 0; v < locals.mVariables.size(); ++v)
{
ESM::VarType type = locals.mVariables[v].second.getType();
if (type == ESM::VT_Short)
++numshorts;
else if (type == ESM::VT_Int)
++numlongs;
}
for (std::vector<std::pair<std::string, ESM::Variant>>::const_iterator iter = locals.mVariables.begin();
iter != locals.mVariables.end(); ++iter, ++index)
{
if (iter->first.empty())
{
// no variable names available (this will happen for legacy, i.e. ESS-imported savegames only)
try
{
if (index >= numshorts + numlongs)
mFloats.at(index - (numshorts + numlongs)) = iter->second.getFloat();
else if (index >= numshorts)
mLongs.at(index - numshorts) = iter->second.getInteger();
else
mShorts.at(index) = iter->second.getInteger();
}
catch (std::exception& e)
{
Log(Debug::Error) << "Failed to read local variable state for script '" << script
<< "' (legacy format): " << e.what() << "\nNum shorts: " << numshorts << " / "
<< mShorts.size() << " Num longs: " << numlongs << " / " << mLongs.size();
}
}
else
{
char type = declarations.getType(iter->first);
int index2 = declarations.getIndex(iter->first);
// silently ignore locals that don't exist anymore
if (type == ' ' || index2 == -1)
continue;
try
{
switch (type)
{
case 's':
mShorts.at(index2) = iter->second.getInteger();
break;
case 'l':
mLongs.at(index2) = iter->second.getInteger();
break;
case 'f':
mFloats.at(index2) = iter->second.getFloat();
break;
}
}
catch (...)
{
// ignore type changes
/// \todo write to log
}
}
}
}
2013-02-03 13:27:27 +00:00
}