2012-08-08 15:18:55 +02:00
|
|
|
#include "scriptmanagerimp.hpp"
|
2010-07-02 16:18:25 +02:00
|
|
|
|
2014-07-21 09:34:10 +02:00
|
|
|
#include <algorithm>
|
2010-07-02 17:21:27 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <exception>
|
2014-07-21 09:34:10 +02:00
|
|
|
#include <sstream>
|
2010-07-02 17:21:27 +02:00
|
|
|
|
2018-08-14 23:05:43 +04:00
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
|
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
|
|
|
#include <components/esm/refid.hpp>
|
2022-10-18 09:26:55 +02:00
|
|
|
#include <components/esm3/loadscpt.hpp>
|
2014-02-11 13:56:56 +01:00
|
|
|
|
2022-08-03 00:00:54 +02:00
|
|
|
#include <components/misc/strings/lower.hpp>
|
2010-07-02 16:18:25 +02:00
|
|
|
|
2010-07-03 12:12:13 +02:00
|
|
|
#include <components/compiler/context.hpp>
|
2012-06-16 11:52:35 +02:00
|
|
|
#include <components/compiler/exception.hpp>
|
2014-02-14 11:15:16 +01:00
|
|
|
#include <components/compiler/quickfileparser.hpp>
|
2010-07-02 18:08:00 +02:00
|
|
|
#include <components/compiler/scanner.hpp>
|
|
|
|
|
2014-02-11 13:56:56 +01:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
|
|
|
|
2010-07-18 19:54:56 +02:00
|
|
|
#include "extensions.hpp"
|
2020-05-10 14:57:06 +02:00
|
|
|
#include "interpretercontext.hpp"
|
2010-07-03 20:35:59 +02:00
|
|
|
|
2010-07-02 16:18:25 +02:00
|
|
|
namespace MWScript
|
|
|
|
{
|
2017-02-20 21:09:15 +01:00
|
|
|
ScriptManager::ScriptManager(const MWWorld::ESMStore& store, Compiler::Context& compilerContext, int warningsMode,
|
2022-12-01 20:02:39 +01:00
|
|
|
const std::vector<ESM::RefId>& scriptBlacklist)
|
2019-04-21 14:12:32 +04:00
|
|
|
: mErrorHandler()
|
|
|
|
, mStore(store)
|
2011-05-18 16:01:19 +02:00
|
|
|
, mCompilerContext(compilerContext)
|
|
|
|
, mParser(mErrorHandler, mCompilerContext)
|
2012-08-08 15:18:55 +02:00
|
|
|
, mOpcodesInstalled(false)
|
|
|
|
, mGlobalScripts(store)
|
2014-02-02 14:09:59 +01:00
|
|
|
{
|
|
|
|
mErrorHandler.setWarningsMode(warningsMode);
|
2014-07-21 09:34:10 +02:00
|
|
|
|
|
|
|
mScriptBlacklist.resize(scriptBlacklist.size());
|
|
|
|
|
|
|
|
std::sort(mScriptBlacklist.begin(), mScriptBlacklist.end());
|
2014-02-02 14:09:59 +01:00
|
|
|
}
|
2010-08-29 23:40:59 +02:00
|
|
|
|
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 ScriptManager::compile(const ESM::RefId& name)
|
2010-07-02 17:21:27 +02:00
|
|
|
{
|
|
|
|
mParser.reset();
|
|
|
|
mErrorHandler.reset();
|
|
|
|
|
2012-11-06 12:36:21 +04:00
|
|
|
if (const ESM::Script* script = mStore.get<ESM::Script>().find(name))
|
2010-08-29 23:40:59 +02:00
|
|
|
{
|
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
|
|
|
mErrorHandler.setContext(script->mId.getRefIdString());
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2014-09-26 17:12:48 +02:00
|
|
|
bool Success = true;
|
2010-07-02 17:21:27 +02:00
|
|
|
try
|
2010-08-29 23:40:59 +02:00
|
|
|
{
|
2012-09-17 11:37:50 +04:00
|
|
|
std::istringstream input(script->mScriptText);
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2010-07-03 12:12:13 +02:00
|
|
|
Compiler::Scanner scanner(mErrorHandler, input, mCompilerContext.getExtensions());
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2010-07-02 17:21:27 +02:00
|
|
|
scanner.scan(mParser);
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2010-07-02 17:21:27 +02:00
|
|
|
if (!mErrorHandler.isGood())
|
|
|
|
Success = false;
|
|
|
|
}
|
2012-06-16 11:52:35 +02:00
|
|
|
catch (const Compiler::SourceException&)
|
2010-07-02 17:21:27 +02:00
|
|
|
{
|
2012-06-16 11:52:35 +02:00
|
|
|
// error has already been reported via error handler
|
2012-06-16 13:06:23 +02:00
|
|
|
Success = false;
|
2012-06-16 11:52:35 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception& error)
|
|
|
|
{
|
2018-08-14 23:05:43 +04:00
|
|
|
Log(Debug::Error) << "Error: An exception has been thrown: " << error.what();
|
2010-07-02 17:21:27 +02:00
|
|
|
Success = false;
|
|
|
|
}
|
|
|
|
|
2014-06-20 23:56:21 +02:00
|
|
|
if (!Success)
|
2010-07-02 17:21:27 +02:00
|
|
|
{
|
2019-04-21 14:12:32 +04:00
|
|
|
Log(Debug::Error) << "Error: script compiling failed: " << name;
|
2010-07-02 17:21:27 +02:00
|
|
|
}
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2010-07-02 17:21:27 +02:00
|
|
|
if (Success)
|
|
|
|
{
|
2023-01-10 04:10:18 +01:00
|
|
|
mScripts.emplace(name, CompiledScript(mParser.getProgram(), mParser.getLocals()));
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2010-07-02 17:21:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-08-29 23:40:59 +02:00
|
|
|
|
|
|
|
return false;
|
2010-07-02 17:21:27 +02:00
|
|
|
}
|
|
|
|
|
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 ScriptManager::run(const ESM::RefId& name, Interpreter::Context& interpreterContext)
|
2010-07-02 16:18:25 +02:00
|
|
|
{
|
2010-07-02 17:21:27 +02:00
|
|
|
// compile script
|
2022-08-12 20:56:50 +02:00
|
|
|
auto iter = mScripts.find(name);
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2010-07-02 17:21:27 +02:00
|
|
|
if (iter == mScripts.end())
|
|
|
|
{
|
|
|
|
if (!compile(name))
|
|
|
|
{
|
|
|
|
// failed -> ignore script from now on.
|
2023-01-10 04:10:18 +01:00
|
|
|
mScripts.emplace(name, CompiledScript({}, Compiler::Locals()));
|
2020-05-10 14:57:06 +02:00
|
|
|
return false;
|
2010-07-02 17:21:27 +02:00
|
|
|
}
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2010-07-02 17:21:27 +02:00
|
|
|
iter = mScripts.find(name);
|
|
|
|
assert(iter != mScripts.end());
|
|
|
|
}
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2010-07-02 17:21:27 +02:00
|
|
|
// execute script
|
2022-12-01 20:02:39 +01:00
|
|
|
const auto& target = interpreterContext.getTarget();
|
2023-01-10 04:10:18 +01:00
|
|
|
if (!iter->second.mProgram.mInstructions.empty()
|
|
|
|
&& iter->second.mInactive.find(target) == iter->second.mInactive.end())
|
2010-07-02 18:08:00 +02:00
|
|
|
try
|
|
|
|
{
|
2011-05-18 16:01:19 +02:00
|
|
|
if (!mOpcodesInstalled)
|
|
|
|
{
|
|
|
|
installOpcodes(mInterpreter);
|
|
|
|
mOpcodesInstalled = true;
|
|
|
|
}
|
|
|
|
|
2023-01-10 04:10:18 +01:00
|
|
|
mInterpreter.run(iter->second.mProgram, interpreterContext);
|
2020-05-10 14:57:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (const MissingImplicitRefError& e)
|
|
|
|
{
|
|
|
|
Log(Debug::Error) << "Execution of script " << name << " failed: " << e.what();
|
2010-07-02 18:08:00 +02:00
|
|
|
}
|
2010-07-03 20:35:59 +02:00
|
|
|
catch (const std::exception& e)
|
2010-07-02 18:08:00 +02:00
|
|
|
{
|
2020-05-10 14:57:06 +02:00
|
|
|
Log(Debug::Error) << "Execution of script " << name << " failed: " << e.what();
|
2010-08-29 23:40:59 +02:00
|
|
|
|
2021-02-14 16:25:38 +01:00
|
|
|
iter->second.mInactive.insert(target); // don't execute again.
|
2010-07-02 18:08:00 +02:00
|
|
|
}
|
2020-05-10 14:57:06 +02:00
|
|
|
return false;
|
2010-07-02 16:18:25 +02:00
|
|
|
}
|
2011-10-09 12:05:13 +02:00
|
|
|
|
2020-07-14 10:53:01 +04:00
|
|
|
void ScriptManager::clear()
|
|
|
|
{
|
|
|
|
for (auto& script : mScripts)
|
|
|
|
{
|
2021-02-14 16:25:38 +01:00
|
|
|
script.second.mInactive.clear();
|
2020-07-14 10:53:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mGlobalScripts.clear();
|
|
|
|
}
|
|
|
|
|
2011-10-09 12:05:13 +02:00
|
|
|
std::pair<int, int> ScriptManager::compileAll()
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
int success = 0;
|
|
|
|
|
2020-07-30 20:57:25 +04:00
|
|
|
for (auto& script : mStore.get<ESM::Script>())
|
|
|
|
{
|
2022-12-01 20:02:39 +01:00
|
|
|
if (!std::binary_search(mScriptBlacklist.begin(), mScriptBlacklist.end(), script.mId))
|
2014-07-21 09:34:10 +02:00
|
|
|
{
|
|
|
|
++count;
|
|
|
|
|
2020-07-30 20:57:25 +04:00
|
|
|
if (compile(script.mId))
|
2014-07-21 09:34:10 +02:00
|
|
|
++success;
|
|
|
|
}
|
2020-07-30 20:57:25 +04:00
|
|
|
}
|
2011-10-09 12:05:13 +02:00
|
|
|
|
|
|
|
return std::make_pair(count, success);
|
|
|
|
}
|
2012-03-05 16:56:14 +01:00
|
|
|
|
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
|
|
|
const Compiler::Locals& ScriptManager::getLocals(const ESM::RefId& name)
|
2012-03-05 16:56:14 +01:00
|
|
|
{
|
2013-02-23 14:40:56 +01:00
|
|
|
{
|
2022-08-12 20:56:50 +02:00
|
|
|
auto iter = mScripts.find(name);
|
2013-02-23 14:40:56 +01:00
|
|
|
|
|
|
|
if (iter != mScripts.end())
|
2020-07-14 10:53:01 +04:00
|
|
|
return iter->second.mLocals;
|
2013-02-23 14:40:56 +01:00
|
|
|
}
|
2012-03-05 16:56:14 +01:00
|
|
|
|
|
|
|
{
|
2022-08-12 20:56:50 +02:00
|
|
|
auto iter = mOtherLocals.find(name);
|
2012-06-16 13:06:23 +02:00
|
|
|
|
2013-02-23 14:40:56 +01:00
|
|
|
if (iter != mOtherLocals.end())
|
|
|
|
return iter->second;
|
|
|
|
}
|
2012-03-05 16:56:14 +01:00
|
|
|
|
2022-08-12 20:56:50 +02:00
|
|
|
if (const ESM::Script* script = mStore.get<ESM::Script>().search(name))
|
2013-02-23 14:40:56 +01:00
|
|
|
{
|
2014-02-14 11:15:16 +01:00
|
|
|
Compiler::Locals locals;
|
2013-02-23 14:40:56 +01:00
|
|
|
|
2022-12-01 20:02:39 +01:00
|
|
|
const Compiler::ContextOverride override(mErrorHandler, name.getRefIdString() + "[local variables]");
|
2017-02-20 21:09:15 +01:00
|
|
|
|
2014-02-14 11:15:16 +01:00
|
|
|
std::istringstream stream(script->mScriptText);
|
|
|
|
Compiler::QuickFileParser parser(mErrorHandler, mCompilerContext, locals);
|
|
|
|
Compiler::Scanner scanner(mErrorHandler, stream, mCompilerContext.getExtensions());
|
2021-07-03 13:02:17 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
scanner.scan(parser);
|
|
|
|
}
|
|
|
|
catch (const Compiler::SourceException&)
|
|
|
|
{
|
|
|
|
// error has already been reported via error handler
|
|
|
|
locals.clear();
|
|
|
|
}
|
|
|
|
catch (const std::exception& error)
|
|
|
|
{
|
|
|
|
Log(Debug::Error) << "Error: An exception has been thrown: " << error.what();
|
|
|
|
locals.clear();
|
|
|
|
}
|
2013-02-23 20:20:40 +01:00
|
|
|
|
2022-08-12 20:56:50 +02:00
|
|
|
auto iter = mOtherLocals.emplace(name, locals).first;
|
2013-02-23 20:20:40 +01:00
|
|
|
|
|
|
|
return iter->second;
|
2012-03-05 16:56:14 +01:00
|
|
|
}
|
|
|
|
|
2023-02-20 23:11:18 +01:00
|
|
|
throw std::logic_error("script " + name.toDebugString() + " does not exist");
|
2012-03-05 16:56:14 +01:00
|
|
|
}
|
2012-04-23 11:15:47 +02:00
|
|
|
|
|
|
|
GlobalScripts& ScriptManager::getGlobalScripts()
|
|
|
|
{
|
|
|
|
return mGlobalScripts;
|
|
|
|
}
|
2021-07-07 18:48:25 +02:00
|
|
|
|
|
|
|
const Compiler::Extensions& ScriptManager::getExtensions() const
|
|
|
|
{
|
|
|
|
return *mCompilerContext.getExtensions();
|
|
|
|
}
|
2010-07-02 16:18:25 +02:00
|
|
|
}
|