Rework AngelScript effect names to be not based on script names, but on attributes instead.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-04-10 14:57:20 +02:00
parent 72ef9599ed
commit 149b0f08f6
15 changed files with 131 additions and 48 deletions

View File

@@ -7,7 +7,7 @@ namespace PkmnLib::Battling {
class CreatePokemon {
private:
const BattleLibrary* _library;
Arbutils::CaseInsensitiveConstString _species;
Arbutils::CaseInsensitiveConstString _species = ""_cnc;
Arbutils::CaseInsensitiveConstString _forme = "default"_cnc;
uint8_t _level;
std::string _nickname = "";

View File

@@ -23,7 +23,7 @@ namespace PkmnLib::Library {
inline uint8_t GetBaseHappiness() const { return _baseHappiness; }
inline const PokemonForme* GetDefaultForme() const {
return reinterpret_cast<const PokemonForme*>(CreatureSpecies::GetVariant("default"_cnc));
return reinterpret_cast<const PokemonForme*>(CreatureSpecies::GetVariant("default"_cnc.GetHash()));
}
inline bool HasForme(const Arbutils::CaseInsensitiveConstString& key) const { return HasVariant(key); }

View File

@@ -1,6 +1,7 @@
#include "AngelScripResolver.hpp"
#include <CreatureLib/Battling/Models/Creature.hpp>
#include <cassert>
#include <regex>
#include "../../../extern/angelscript_addons/scriptarray/scriptarray.h"
#include "../../../extern/angelscript_addons/scripthandle/scripthandle.h"
#include "../../../extern/angelscript_addons/scripthelper/scripthelper.h"
@@ -24,6 +25,10 @@ CreatureLib::Battling::ScriptResolver* PkmnLib::Battling::BattleLibrary::CreateS
}
void AngelScripResolver::Initialize(CreatureLib::Battling::BattleLibrary* arg) {
for (auto scriptCategory : ScriptCategoryHelper::GetValues()) {
_typeDatabase.Insert(scriptCategory, {});
}
auto library = (PkmnLib::Battling::BattleLibrary*)arg;
_engine = asCreateScriptEngine();
@@ -87,20 +92,6 @@ void AngelScripResolver::RegisterTypes() {
BasicScriptClass::Register(_engine);
}
AngelScriptTypeInfo* AngelScripResolver::GetTypeInfo(const ConstString& name, const std::string& decl) {
auto find = _types.find(name);
if (find != _types.end()) {
return find->second;
}
auto type = _mainModule->GetTypeInfoByDecl(decl.c_str());
if (type == nullptr) {
_types.insert({name, nullptr});
return nullptr;
}
auto typeinfo = new AngelScriptTypeInfo(name, type);
_types.insert({name, typeinfo});
return typeinfo;
}
void AngelScripResolver::MessageCallback(const asSMessageInfo* msg, void* param) {
const char* type = "ERR ";
if (msg->type == asMSGTYPE_WARNING)
@@ -110,33 +101,78 @@ void AngelScripResolver::MessageCallback(const asSMessageInfo* msg, void* param)
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
static constexpr const char* GetCategoryNamespace(ScriptCategory category) {
switch (category) {
case ScriptCategory::Attack: return "Moves";
case ScriptCategory::Talent: return "Abilities";
case ScriptCategory::Status: return "Status";
case ScriptCategory::Creature: return "Pokemon";
case ScriptCategory::Battle: return "Battle";
case ScriptCategory::Side: return "Side";
default: throw CreatureException("Unknown script category");
}
}
CreatureLib::Battling::Script* AngelScripResolver::LoadScript(ScriptCategory category, const ConstString& scriptName) {
std::stringstream decl;
decl << GetCategoryNamespace(category) << "::" << scriptName.c_str();
auto typeInfo = GetTypeInfo(scriptName, decl.str());
if (typeInfo == nullptr)
Dictionary<uint32_t, AngelScriptTypeInfo*> innerDb;
if (!_typeDatabase.TryGet(category, innerDb)) {
_typeDatabase.Insert(category, innerDb);
return nullptr;
}
AngelScriptTypeInfo* t;
if (!innerDb.TryGet(scriptName, t)) {
innerDb.Insert(scriptName, nullptr);
return nullptr;
}
if (t == nullptr) {
return nullptr;
}
auto ctx = _contextPool->RequestContext();
auto obj = typeInfo->Instantiate(ctx);
auto obj = t->Instantiate(ctx);
_contextPool->ReturnContextToPool(ctx);
return new AngelScriptScript(typeInfo, obj, _contextPool);
return new AngelScriptScript(t, obj, _contextPool);
}
void AngelScripResolver::FinalizeModule() {
int r = _builder.BuildModule();
if (r < 0)
throw CreatureException("Building Script Module failed.");
int count = _mainModule->GetObjectTypeCount();
std::regex metadataMatcher("^\\s*(\\w+)([\\w\\s=]*)$", std::regex_constants::icase);
std::regex variableMatcher("\\s*(\\w+)=(\\w+)", std::regex_constants::icase);
std::smatch base_match;
for (int n = 0; n < count; n++) {
auto typeInfo = _mainModule->GetObjectTypeByIndex(n);
auto metadata = _builder.GetMetadataForType(typeInfo->GetTypeId());
for (int m = 0; m < metadata.size(); m++) {
auto data = metadata[m];
if (std::regex_match(data, base_match, metadataMatcher)) {
auto metadataKind = base_match[1].str();
auto metadataVariables = base_match[2].str();
if (!std::regex_match(metadataVariables, base_match, variableMatcher)) {
continue;
}
ConstString effectName;
for (size_t variableIndex = 1; variableIndex < base_match.size(); variableIndex += 2) {
if (base_match[variableIndex] == "effect") {
auto val = base_match[variableIndex + 1].str();
effectName = ConstString(val);
}
}
if (effectName.Empty()) {
continue;
}
if (metadataKind == "Move") {
_typeDatabase[ScriptCategory::Attack].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Pokemon") {
_typeDatabase[ScriptCategory::Creature].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Ability") {
_typeDatabase[ScriptCategory::Talent].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Status") {
_typeDatabase[ScriptCategory::Status].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Battle") {
_typeDatabase[ScriptCategory::Battle].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
} else if (metadataKind == "Side") {
_typeDatabase[ScriptCategory::Side].Insert(effectName,
new AngelScriptTypeInfo(effectName, typeInfo));
}
}
}
}
}
void AngelScripResolver::CreateScript(const char* name, const char* script) {
_builder.AddSectionFromMemory(name, script);

View File

@@ -10,6 +10,7 @@
#include <iostream>
#include "AngelScriptScript.hpp"
#include "AngelScriptTypeInfo.hpp"
using namespace Arbutils::Collections;
class AngelScripResolver : public CreatureLib::Battling::ScriptResolver {
private:
@@ -17,21 +18,21 @@ private:
asIScriptModule* _mainModule = nullptr;
ContextPool* _contextPool = nullptr;
CScriptBuilder _builder;
std::unordered_map<ConstString, AngelScriptTypeInfo*> _types;
static void MessageCallback(const asSMessageInfo* msg, void* param);
static void Print(const std::string& str) { std::cout << str << std::endl; }
AngelScriptTypeInfo* GetTypeInfo(const ConstString& name, const std::string& decl);
Dictionary<ScriptCategory, Dictionary<uint32_t, AngelScriptTypeInfo*>> _typeDatabase;
void RegisterTypes();
public:
~AngelScripResolver() override {
delete _contextPool;
for (const auto& type : _types) {
delete type.second;
for (const auto& category : _typeDatabase) {
for (const auto& type : category.second){
delete type.second;
}
}
_types.clear();
_engine->ShutDownAndRelease();
}

View File

@@ -15,7 +15,7 @@ using ConstString = Arbutils::CaseInsensitiveConstString;
class AngelScriptTypeInfo {
private:
asITypeInfo* _type = nullptr;
Arbutils::Collections::Dictionary<ConstString, asIScriptFunction*> _functions;
Arbutils::Collections::Dictionary<uint32_t, asIScriptFunction*> _functions;
ConstString _name;
struct FunctionInfo {