1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-26 18:35:20 +00:00
OpenMW/apps/openmw/mwdialogue/scripttest.cpp

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

172 lines
5.9 KiB
C++
Raw Normal View History

#include "scripttest.hpp"
#include "../mwworld/class.hpp"
2015-02-09 15:01:49 +01:00
#include "../mwworld/esmstore.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwbase/environment.hpp"
#include "../mwbase/scriptmanager.hpp"
#include "../mwscript/compilercontext.hpp"
#include <components/compiler/exception.hpp>
#include <components/compiler/locals.hpp>
#include <components/compiler/scanner.hpp>
#include <components/compiler/scriptparser.hpp>
#include <components/compiler/streamerrorhandler.hpp>
#include <components/debug/debuglog.hpp>
#include <components/esm3/loadcrea.hpp>
#include <components/esm3/loadnpc.hpp>
#include "filter.hpp"
#include <memory>
#include <set>
#include <sstream>
namespace
{
bool test(const MWWorld::Ptr& actor, const ESM::DialInfo& info, int& compiled, int& total,
const Compiler::Extensions* extensions, const MWScript::CompilerContext& context,
Compiler::StreamErrorHandler& errorHandler)
{
bool success = true;
++total;
try
{
std::istringstream input(info.mResultScript + "\n");
Compiler::Scanner scanner(errorHandler, input, extensions);
Compiler::Locals locals;
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 ESM::RefId& actorScript = actor.getClass().getScript(actor);
if (!actorScript.empty())
{
// grab local variables from actor's script, if available.
locals = MWBase::Environment::get().getScriptManager()->getLocals(actorScript);
}
Compiler::ScriptParser parser(errorHandler, context, locals, false);
scanner.scan(parser);
if (!errorHandler.isGood())
success = false;
++compiled;
}
catch (const Compiler::SourceException&)
{
// error has already been reported via error handler
success = false;
}
catch (const std::exception& error)
{
Log(Debug::Error) << "Dialogue error: An exception has been thrown: " << error.what();
success = false;
}
return success;
}
bool superficiallyMatches(const MWWorld::Ptr& ptr, const ESM::DialInfo& info)
{
if (ptr.isEmpty())
return false;
MWDialogue::Filter filter(ptr, 0, false);
return filter.couldPotentiallyMatch(info);
}
}
namespace MWDialogue
{
namespace ScriptTest
{
std::pair<int, int> compileAll(const Compiler::Extensions* extensions, int warningsMode)
{
int compiled = 0, total = 0;
2023-04-20 21:07:53 +02:00
const auto& store = *MWBase::Environment::get().getESMStore();
MWScript::CompilerContext compilerContext(MWScript::CompilerContext::Type_Dialogue);
compilerContext.setExtensions(extensions);
Compiler::StreamErrorHandler errorHandler;
errorHandler.setWarningsMode(warningsMode);
2022-09-22 21:26:05 +03:00
std::unique_ptr<MWWorld::ManualRef> ref;
for (const ESM::Dialogue& topic : store.get<ESM::Dialogue>())
2022-09-22 21:26:05 +03:00
{
MWWorld::Ptr ptr;
for (const ESM::DialInfo& info : topic.mInfo)
{
if (info.mResultScript.empty())
continue;
if (!info.mActor.empty())
{
// We know it can only ever be this actor
try
{
ref = std::make_unique<MWWorld::ManualRef>(store, info.mActor);
ptr = ref->getPtr();
}
catch (const std::logic_error&)
{
// Too bad it doesn't exist
ptr = {};
}
}
else
{
// Try to find a matching actor
if (!superficiallyMatches(ptr, info))
{
ptr = {};
bool found = false;
for (const auto& npc : store.get<ESM::NPC>())
{
ref = std::make_unique<MWWorld::ManualRef>(store, npc.mId);
if (superficiallyMatches(ref->getPtr(), info))
{
ptr = ref->getPtr();
found = true;
break;
}
}
if (!found)
{
for (const auto& creature : store.get<ESM::Creature>())
{
ref = std::make_unique<MWWorld::ManualRef>(store, creature.mId);
if (superficiallyMatches(ref->getPtr(), info))
{
ptr = ref->getPtr();
break;
}
}
}
}
}
if (ptr.isEmpty())
Log(Debug::Error) << "Could not find an actor to test " << info.mId << " in " << topic.mId;
else
{
errorHandler.reset();
errorHandler.setContext(info.mId.getRefIdString() + " in " + topic.mStringId);
if (!test(ptr, info, compiled, total, extensions, compilerContext, errorHandler))
Log(Debug::Error) << "Test failed for " << info.mId << " in " << topic.mId << '\n'
<< info.mResultScript;
}
}
2022-09-22 21:26:05 +03:00
}
return std::make_pair(total, compiled);
}
}
}