Use namespaces to differentiate between different categories of scripts.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-02-16 14:38:05 +01:00
parent bb06026587
commit 321afbebe4
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
10 changed files with 41 additions and 78 deletions

View File

@ -96,8 +96,23 @@ void AngelScripResolver::MessageCallback(const asSMessageInfo* msg, void* param)
type = "INFO";
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
static constexpr const char* GetCategoryNamespace(AngelScripResolver::ScriptCategory category){
switch (category){
case CreatureLib::Battling::ScriptResolver::ScriptCategory::Attack: return "Moves";
case CreatureLib::Battling::ScriptResolver::ScriptCategory::Talent: return "Abilities";
case CreatureLib::Battling::ScriptResolver::ScriptCategory::Status: return "Status";
case CreatureLib::Battling::ScriptResolver::ScriptCategory::Creature: return "Pokemon";
case CreatureLib::Battling::ScriptResolver::ScriptCategory::Battle: return "Battle";
case CreatureLib::Battling::ScriptResolver::ScriptCategory::Side: return "Side";
default: throw CreatureException("Unknown script category");
}
}
CreatureLib::Battling::Script* AngelScripResolver::LoadScript(ScriptCategory category, const std::string& scriptName) {
auto typeInfo = GetTypeInfo(scriptName);
std::stringstream decl;
decl << GetCategoryNamespace(category) << "::" << scriptName;
auto typeInfo = GetTypeInfo(decl.str());
if (typeInfo == nullptr)
return nullptr;
auto ctx = _contextPool->RequestContext();
@ -110,10 +125,6 @@ void AngelScripResolver::FinalizeModule() {
if (r < 0)
throw CreatureException("Building Script Module failed.");
}
void AngelScripResolver::CreateScript(ScriptCategory category, const char* scriptName) {
auto scriptString = _loadFunc(category, scriptName);
_mainModule->AddScriptSection(scriptName, scriptString);
}
void AngelScripResolver::CreateScript(const char* name, const char* script) {
_mainModule->AddScriptSection(name, script);
}

View File

@ -15,7 +15,6 @@ private:
asIScriptEngine* _engine = nullptr;
asIScriptModule* _mainModule = nullptr;
ContextPool* _contextPool = nullptr;
const char* (*_loadFunc)(ScriptCategory category, const char*) = nullptr;
std::unordered_map<std::string, AngelScriptTypeInfo*> _types;
static void MessageCallback(const asSMessageInfo* msg, void* param);
@ -35,12 +34,6 @@ public:
}
void Initialize(CreatureLib::Battling::BattleLibrary* library) override;
void SetCreateFunction(const char* (*loadFunc)(ScriptCategory category, const char* scriptName)) {
_loadFunc = loadFunc;
}
void CreateScript(ScriptCategory category, const char* scriptName);
void CreateScript(const char* name, const char* script);
void FinalizeModule();

View File

@ -5,7 +5,7 @@
#include "../TestLibrary/TestLibrary.hpp"
#define AS_CLASS(name, contents) \
{ #name, "class " #name " : PkmnScript { " contents "}" }
{ #name, "namespace Pokemon{ class " #name " : PkmnScript { " contents "}}" }
static std::unordered_map<const char*, const char*> _scripts = std::unordered_map<const char*, const char*>{
AS_CLASS(blankScript, ),
@ -62,18 +62,13 @@ void StopBeforeAttack(ExecutingMove@ attack, bool& result) override{
};
static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
static AngelScripResolver* _resolverCache = nullptr;
static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* mainLib) {
if (_resolverCache == nullptr) {
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc);
for (auto kv : _scripts) {
_resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack, kv.first);
_resolverCache->CreateScript(kv.first , kv.second);
}
_resolverCache->FinalizeModule();
}

View File

@ -5,6 +5,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
class testScript1 {
int add(int a, int b) {
return a + b;
@ -16,11 +17,8 @@ class testScript1 {
}
}
}
)"}};
static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
)"}};
TEST_CASE("Get a script resolver, initialize it, then delete it") {
auto lib = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
@ -28,18 +26,10 @@ TEST_CASE("Get a script resolver, initialize it, then delete it") {
delete lib;
}
TEST_CASE("Get a script resolver, set script load function, then delete it") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc);
delete lib;
}
TEST_CASE("Get a script resolver, set script load function, load script, then build module") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
lib->CreateScript("testScript1" , _scripts["testScript1"]);
lib->FinalizeModule();
delete lib;
}
@ -47,8 +37,7 @@ TEST_CASE("Get a script resolver, set script load function, load script, then bu
TEST_CASE("Build script resolver, then create object") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
lib->CreateScript("testScript1" , _scripts["testScript1"]);
lib->FinalizeModule();
auto obj = lib->LoadScript(AngelScripResolver::ScriptCategory::Creature, "testScript1");
@ -60,8 +49,7 @@ TEST_CASE("Build script resolver, then create object") {
TEST_CASE("Build script resolver, create object, invoke addition method") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary());
lib->SetCreateFunction(&_testLoadFunc);
lib->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
lib->CreateScript("testScript1" , _scripts["testScript1"]);
lib->FinalizeModule();
auto obj =

View File

@ -6,6 +6,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
class testScript1 {
bool testSpecies(Pokemon@ p, const Species@ species){ return p.Species is species; }
bool testForme(Pokemon@ p, const Forme@ forme){ return p.Forme is forme; }
@ -24,13 +25,9 @@ class testScript1 {
void testHeal(Pokemon@ p, uint32 amount){ p.Heal(amount); }
bool testMove(Pokemon@ p, uint index, LearnedMove@ move){ return p.GetMoves()[index] is move; }
}
}}
)"}};
static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData {
AngelScriptScript* Script = nullptr;
AngelScripResolver* Resolver = nullptr;
@ -48,8 +45,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
if (_resolverCache == nullptr) {
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->CreateScript("testScript1" , _scripts["testScript1"]);
_resolverCache->FinalizeModule();
}
return _resolverCache;

View File

@ -5,6 +5,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
class testScript1 {
bool testName(const Forme@ s, const string &in name){ return s.Name == name; }
bool testWeight(const Forme@ s, float weight){ return s.Weight == weight; }
@ -14,13 +15,9 @@ class testScript1 {
bool testGetType(const Forme@ s, uint8 type){ return s.GetType(0) == type; }
bool testGetStatistic(const Forme@ s, Statistic stat, uint value){ return s.GetStatistic(stat) == value; }
bool testGetAbility(const Forme@ s, const string &in ability){ return s.GetAbility(0) == ability; }
}
}}
)"}};
static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData {
AngelScriptScript* Script = nullptr;
AngelScripResolver* Resolver = nullptr;
@ -38,8 +35,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
if (_resolverCache == nullptr) {
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->CreateScript("testScript1" , _scripts["testScript1"]);
_resolverCache->FinalizeModule();
}
return _resolverCache;

View File

@ -5,18 +5,15 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
class testScript1 {
bool testName(const Item@ i, const string &in name){ return i.Name == name; }
bool testCategory(const Item@ i, ItemCategory category){ return i.Category == category; }
bool testBattleCategory(const Item@ i, BattleItemCategory category){ return i.BattleCategory == category; }
bool testPrice(const Item@ i, int price){ return i.Price == price; }
}
}}
)"}};
static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData {
AngelScriptScript* Script = nullptr;
AngelScripResolver* Resolver = nullptr;
@ -34,8 +31,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
if (_resolverCache == nullptr){
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->CreateScript("testScript1" , _scripts["testScript1"]);
_resolverCache->FinalizeModule();
}
return _resolverCache;

View File

@ -5,6 +5,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
class testScript1 {
bool testName(const MoveData@ s, const string &in name){ return s.Name == name; }
bool testType(const MoveData@ s, uint8 type){ return s.Type == type; }
@ -14,13 +15,9 @@ class testScript1 {
bool testBaseUsages(const MoveData@ s, uint8 baseUsages){ return s.BaseUsages == baseUsages; }
bool testTarget(const MoveData@ s, MoveTarget target){ return s.Target == target; }
bool testPriority(const MoveData@ s, int8 priority){ return s.Priority == priority; }
}
}}
)"}};
static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData {
AngelScriptScript* Script = nullptr;
AngelScripResolver* Resolver = nullptr;
@ -38,8 +35,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
if (_resolverCache == nullptr){
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->CreateScript("testScript1" , _scripts["testScript1"]);
_resolverCache->FinalizeModule();
}
return _resolverCache;

View File

@ -5,6 +5,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
class testScript1 {
bool testName(const Species@ s, const string &in name){ return s.Name == name; }
bool testId(const Species@ s, uint16 id){ return s.Id == id; }
@ -12,13 +13,9 @@ class testScript1 {
bool testCaptureRate(const Species@ s, uint8 rate){ return s.CaptureRate == rate; }
bool testGetForme(const Species@ s, const Forme@ forme){ return s.GetForme("default") is forme; }
bool testGetDefaultForme(const Species@ s, const Forme@ forme){ return s.GetDefaultForme() is forme; }
}
}}
)"}};
static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData {
AngelScriptScript* Script;
AngelScripResolver* Resolver;
@ -36,8 +33,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
if (_resolverCache == nullptr){
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->CreateScript("testScript1" , _scripts["testScript1"]);
_resolverCache->FinalizeModule();
}
return _resolverCache;

View File

@ -5,6 +5,7 @@
static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
class testScript1 {
bool testMaximumLevel(const StaticLibrary@ s, uint8 level){ return s.Settings.MaximalLevel == level; }
bool testMaximumMoves(const StaticLibrary@ s, uint8 moveCount){ return s.Settings.MaximalMoves == moveCount; }
@ -13,13 +14,9 @@ class testScript1 {
bool testItemLibrary(const StaticLibrary@ s, const ItemLibrary@ itemLib){ return s.ItemLibrary is itemLib; }
bool testGrowthRateLibrary(const StaticLibrary@ s, const GrowthRateLibrary@ gl){ return s.GrowthRateLibrary is gl; }
bool testTypeLibrary(const StaticLibrary@ s, const TypeLibrary@ tl){ return s.TypeLibrary is tl; }
}
}}
)"}};
static const char* _testLoadFunc(CreatureLib::Battling::ScriptResolver::ScriptCategory category, const char* name) {
return _scripts[name];
}
struct ScriptData {
AngelScriptScript* Script;
AngelScripResolver* Resolver;
@ -37,8 +34,7 @@ static AngelScripResolver* GetScriptResolver(PkmnLib::Battling::BattleLibrary* m
if (_resolverCache == nullptr){
_resolverCache = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
_resolverCache->Initialize(mainLib);
_resolverCache->SetCreateFunction(&_testLoadFunc);
_resolverCache->CreateScript(AngelScripResolver::ScriptCategory::Attack ,"testScript1");
_resolverCache->CreateScript("testScript1" , _scripts["testScript1"]);
_resolverCache->FinalizeModule();
}
return _resolverCache;