Reorganized files
This commit is contained in:
103
src/ScriptResolving/AngelScript/AngelScripResolver.cpp
Normal file
103
src/ScriptResolving/AngelScript/AngelScripResolver.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include "AngelScripResolver.hpp"
|
||||
#define AS_USE_ACCESSORS
|
||||
#include "../../../extern/angelscript_addons/scriptarray/scriptarray.h"
|
||||
#undef AS_USE_ACCESSORS
|
||||
#include "../../../extern/angelscript_addons/scripthelper/scripthelper.h"
|
||||
#include "../../../extern/angelscript_addons/scriptstdstring/scriptstdstring.h"
|
||||
#include "TypeRegistry/Battling/RegisterExecutingAttack.hpp"
|
||||
#include "TypeRegistry/Battling/RegisterPokemonClass.hpp"
|
||||
#include "TypeRegistry/Library/RegisterGrowthRateTypes.hpp"
|
||||
#include "TypeRegistry/Library/RegisterItemTypes.hpp"
|
||||
#include "TypeRegistry/Library/RegisterMoveTypes.hpp"
|
||||
#include "TypeRegistry/Library/RegisterSpeciesTypes.hpp"
|
||||
#include "TypeRegistry/Library/RegisterStaticLibraryTypes.hpp"
|
||||
#include "TypeRegistry/Library/RegisterTypeLibrary.hpp"
|
||||
|
||||
CreatureLib::Battling::ScriptResolver* PkmnLib::Battling::BattleLibrary::CreateScriptResolver() {
|
||||
return new AngelScripResolver();
|
||||
}
|
||||
|
||||
void AngelScripResolver::Initialize(CreatureLib::Battling::BattleLibrary* library) {
|
||||
_engine = asCreateScriptEngine();
|
||||
|
||||
int32_t r = _engine->SetMessageCallback(asFUNCTION(MessageCallback), nullptr, asCALL_CDECL);
|
||||
if (r < 0)
|
||||
throw CreatureException("Registering message callback failed.");
|
||||
|
||||
_engine->SetEngineProperty(asEP_DISALLOW_EMPTY_LIST_ELEMENTS, true);
|
||||
_engine->SetEngineProperty(asEP_DISALLOW_VALUE_ASSIGN_FOR_REF_TYPE, true);
|
||||
_engine->SetEngineProperty(asEP_ALWAYS_IMPL_DEFAULT_CONSTRUCT, true);
|
||||
_engine->SetEngineProperty(asEP_AUTO_GARBAGE_COLLECT, false);
|
||||
_engine->SetEngineProperty(asEP_REQUIRE_ENUM_SCOPE, true);
|
||||
|
||||
RegisterStdString(_engine);
|
||||
|
||||
// Register Script Array type
|
||||
RegisterScriptArray(_engine, true);
|
||||
|
||||
r = _engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(Print), asCALL_CDECL);
|
||||
if (r < 0)
|
||||
throw CreatureException("Registering print function failed.");
|
||||
|
||||
RegisterTypes();
|
||||
RegisterExceptionRoutines(_engine);
|
||||
|
||||
_mainModule = _engine->GetModule("pkmn", asGM_ALWAYS_CREATE);
|
||||
|
||||
_contextPool = new ContextPool(_engine);
|
||||
}
|
||||
|
||||
void AngelScripResolver::RegisterTypes() {
|
||||
// Register static library types
|
||||
RegisterSpeciesTypes::Register(_engine);
|
||||
RegisterItemTypes::Register(_engine);
|
||||
RegisterMoveTypes::Register(_engine);
|
||||
RegisterGrowthRateTypes::Register(_engine);
|
||||
RegisterTypeLibrary::Register(_engine);
|
||||
RegisterStaticLibraryTypes::Register(_engine);
|
||||
|
||||
// Register battle types
|
||||
RegisterPokemonClass::Register(_engine);
|
||||
RegisterExecutingAttack::Register(_engine);
|
||||
}
|
||||
|
||||
AngelScriptTypeInfo* AngelScripResolver::GetTypeInfo(const std::string& name) {
|
||||
auto find = _types.find(name);
|
||||
if (find != _types.end()) {
|
||||
return find->second;
|
||||
}
|
||||
auto type = _mainModule->GetTypeInfoByDecl(name.c_str());
|
||||
if (type == nullptr) {
|
||||
_types.insert({name, nullptr});
|
||||
return nullptr;
|
||||
}
|
||||
auto typeinfo = new AngelScriptTypeInfo(type);
|
||||
_types.insert({name, typeinfo});
|
||||
return typeinfo;
|
||||
}
|
||||
void AngelScripResolver::MessageCallback(const asSMessageInfo* msg, void* param) {
|
||||
const char* type = "ERR ";
|
||||
if (msg->type == asMSGTYPE_WARNING)
|
||||
type = "WARN";
|
||||
else if (msg->type == asMSGTYPE_INFORMATION)
|
||||
type = "INFO";
|
||||
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
|
||||
}
|
||||
CreatureLib::Battling::Script* AngelScripResolver::LoadScript(ScriptCategory category, const std::string& scriptName) {
|
||||
auto typeInfo = GetTypeInfo(scriptName);
|
||||
if (typeInfo == nullptr)
|
||||
return nullptr;
|
||||
auto ctx = _contextPool->RequestContext();
|
||||
auto obj = typeInfo->Instantiate(ctx);
|
||||
_contextPool->ReturnContextToPool(ctx);
|
||||
return new AngelScriptScript(scriptName, typeInfo, obj, _contextPool);
|
||||
}
|
||||
void AngelScripResolver::FinalizeModule() {
|
||||
int r = _mainModule->Build();
|
||||
if (r < 0)
|
||||
throw CreatureException("Building Script Module failed.");
|
||||
}
|
||||
void AngelScripResolver::CreateScript(const char* scriptName) {
|
||||
auto scriptString = _loadFunc(scriptName);
|
||||
_mainModule->AddScriptSection(scriptName, scriptString);
|
||||
}
|
||||
Reference in New Issue
Block a user