mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-04 21:40:03 +00:00
125b21de20
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!
229 lines
6.8 KiB
C++
229 lines
6.8 KiB
C++
#include "scriptmanagerimp.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <exception>
|
|
#include <sstream>
|
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
#include <components/esm3/loadscpt.hpp>
|
|
#include <components/esm/refid.hpp>
|
|
|
|
#include <components/misc/strings/lower.hpp>
|
|
|
|
#include <components/compiler/context.hpp>
|
|
#include <components/compiler/exception.hpp>
|
|
#include <components/compiler/quickfileparser.hpp>
|
|
#include <components/compiler/scanner.hpp>
|
|
|
|
#include "../mwworld/esmstore.hpp"
|
|
|
|
#include "extensions.hpp"
|
|
#include "interpretercontext.hpp"
|
|
|
|
namespace MWScript
|
|
{
|
|
ScriptManager::ScriptManager(const MWWorld::ESMStore& store, Compiler::Context& compilerContext, int warningsMode,
|
|
const std::vector<std::string>& scriptBlacklist)
|
|
: mErrorHandler()
|
|
, mStore(store)
|
|
, mCompilerContext(compilerContext)
|
|
, mParser(mErrorHandler, mCompilerContext)
|
|
, mOpcodesInstalled(false)
|
|
, mGlobalScripts(store)
|
|
{
|
|
mErrorHandler.setWarningsMode(warningsMode);
|
|
|
|
mScriptBlacklist.resize(scriptBlacklist.size());
|
|
|
|
std::transform(scriptBlacklist.begin(), scriptBlacklist.end(), mScriptBlacklist.begin(),
|
|
[](const std::string& s) { return Misc::StringUtils::lowerCase(s); });
|
|
std::sort(mScriptBlacklist.begin(), mScriptBlacklist.end());
|
|
}
|
|
|
|
bool ScriptManager::compile(const ESM::RefId& name)
|
|
{
|
|
mParser.reset();
|
|
mErrorHandler.reset();
|
|
|
|
if (const ESM::Script* script = mStore.get<ESM::Script>().find(name))
|
|
{
|
|
mErrorHandler.setContext(script->mId.getRefIdString());
|
|
|
|
bool Success = true;
|
|
try
|
|
{
|
|
std::istringstream input(script->mScriptText);
|
|
|
|
Compiler::Scanner scanner(mErrorHandler, input, mCompilerContext.getExtensions());
|
|
|
|
scanner.scan(mParser);
|
|
|
|
if (!mErrorHandler.isGood())
|
|
Success = false;
|
|
}
|
|
catch (const Compiler::SourceException&)
|
|
{
|
|
// error has already been reported via error handler
|
|
Success = false;
|
|
}
|
|
catch (const std::exception& error)
|
|
{
|
|
Log(Debug::Error) << "Error: An exception has been thrown: " << error.what();
|
|
Success = false;
|
|
}
|
|
|
|
if (!Success)
|
|
{
|
|
Log(Debug::Error) << "Error: script compiling failed: " << name;
|
|
}
|
|
|
|
if (Success)
|
|
{
|
|
std::vector<Interpreter::Type_Code> code;
|
|
mParser.getCode(code);
|
|
mScripts.emplace(name, CompiledScript(code, mParser.getLocals()));
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool ScriptManager::run(const ESM::RefId& name, Interpreter::Context& interpreterContext)
|
|
{
|
|
// compile script
|
|
auto iter = mScripts.find(name);
|
|
|
|
if (iter == mScripts.end())
|
|
{
|
|
if (!compile(name))
|
|
{
|
|
// failed -> ignore script from now on.
|
|
std::vector<Interpreter::Type_Code> empty;
|
|
mScripts.emplace(name, CompiledScript(empty, Compiler::Locals()));
|
|
return false;
|
|
}
|
|
|
|
iter = mScripts.find(name);
|
|
assert(iter != mScripts.end());
|
|
}
|
|
|
|
// execute script
|
|
auto target = interpreterContext.getTarget();
|
|
if (!iter->second.mByteCode.empty() && iter->second.mInactive.find(target) == iter->second.mInactive.end())
|
|
try
|
|
{
|
|
if (!mOpcodesInstalled)
|
|
{
|
|
installOpcodes(mInterpreter);
|
|
mOpcodesInstalled = true;
|
|
}
|
|
|
|
mInterpreter.run(&iter->second.mByteCode[0], iter->second.mByteCode.size(), interpreterContext);
|
|
return true;
|
|
}
|
|
catch (const MissingImplicitRefError& e)
|
|
{
|
|
Log(Debug::Error) << "Execution of script " << name << " failed: " << e.what();
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
Log(Debug::Error) << "Execution of script " << name << " failed: " << e.what();
|
|
|
|
iter->second.mInactive.insert(target); // don't execute again.
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void ScriptManager::clear()
|
|
{
|
|
for (auto& script : mScripts)
|
|
{
|
|
script.second.mInactive.clear();
|
|
}
|
|
|
|
mGlobalScripts.clear();
|
|
}
|
|
|
|
std::pair<int, int> ScriptManager::compileAll()
|
|
{
|
|
int count = 0;
|
|
int success = 0;
|
|
|
|
for (auto& script : mStore.get<ESM::Script>())
|
|
{
|
|
if (!std::binary_search(
|
|
mScriptBlacklist.begin(), mScriptBlacklist.end(), Misc::StringUtils::lowerCase(script.mId.getRefIdString())))
|
|
{
|
|
++count;
|
|
|
|
if (compile(script.mId))
|
|
++success;
|
|
}
|
|
}
|
|
|
|
return std::make_pair(count, success);
|
|
}
|
|
|
|
const Compiler::Locals& ScriptManager::getLocals(const ESM::RefId& name)
|
|
{
|
|
{
|
|
auto iter = mScripts.find(name);
|
|
|
|
if (iter != mScripts.end())
|
|
return iter->second.mLocals;
|
|
}
|
|
|
|
{
|
|
auto iter = mOtherLocals.find(name);
|
|
|
|
if (iter != mOtherLocals.end())
|
|
return iter->second;
|
|
}
|
|
|
|
if (const ESM::Script* script = mStore.get<ESM::Script>().search(name))
|
|
{
|
|
Compiler::Locals locals;
|
|
|
|
const Compiler::ContextOverride override(mErrorHandler, std::string{ name.getRefIdString() } + "[local variables]");
|
|
|
|
std::istringstream stream(script->mScriptText);
|
|
Compiler::QuickFileParser parser(mErrorHandler, mCompilerContext, locals);
|
|
Compiler::Scanner scanner(mErrorHandler, stream, mCompilerContext.getExtensions());
|
|
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();
|
|
}
|
|
|
|
auto iter = mOtherLocals.emplace(name, locals).first;
|
|
|
|
return iter->second;
|
|
}
|
|
|
|
throw std::logic_error("script " + std::string{ name.getRefIdString() } + " does not exist");
|
|
}
|
|
|
|
GlobalScripts& ScriptManager::getGlobalScripts()
|
|
{
|
|
return mGlobalScripts;
|
|
}
|
|
|
|
const Compiler::Extensions& ScriptManager::getExtensions() const
|
|
{
|
|
return *mCompilerContext.getExtensions();
|
|
}
|
|
}
|