86 lines
3.3 KiB
C++
86 lines
3.3 KiB
C++
#ifndef PKMNLIB_ANGELSCRIPTRESOLVER_HPP
|
|
#define PKMNLIB_ANGELSCRIPTRESOLVER_HPP
|
|
|
|
#include <CreatureLib/Battling/ScriptHandling/ScriptResolver.hpp>
|
|
#include "../../../extern/angelscript_addons/scriptbuilder/scriptbuilder.h"
|
|
#include "../../Battling/Library/BattleLibrary.hpp"
|
|
|
|
#define ANGELSCRIPT_DLL_LIBRARY_IMPORT
|
|
#include <angelscript.h>
|
|
#include "AngelScriptScript.hpp"
|
|
#include "AngelScriptTypeInfo.hpp"
|
|
|
|
class AngelScriptResolver final : public CreatureLib::Battling::ScriptResolver {
|
|
private:
|
|
asIScriptEngine* _engine = nullptr;
|
|
asIScriptModule* _mainModule = nullptr;
|
|
ContextPool* _contextPool = nullptr;
|
|
CScriptBuilder _builder;
|
|
|
|
static void MessageCallback(const asSMessageInfo* msg, void* param);
|
|
static void Print(const std::string& str) { std::cout << str << std::endl; }
|
|
ArbUt::Dictionary<ScriptCategory, ArbUt::Dictionary<ArbUt::StringView, AngelScriptTypeInfo*>> _typeDatabase;
|
|
ArbUt::Dictionary<ArbUt::StringView, asITypeInfo*> _baseTypes;
|
|
|
|
void RegisterTypes();
|
|
void
|
|
InitializeByteCode(const ArbUt::Dictionary<ScriptCategory, ArbUt::Dictionary<ArbUt::StringView, uint32_t>>& types);
|
|
|
|
public:
|
|
~AngelScriptResolver() override {
|
|
delete _contextPool;
|
|
for (const auto& category : _typeDatabase) {
|
|
for (const auto& type : category.second) {
|
|
delete type.second;
|
|
}
|
|
}
|
|
_engine->ShutDownAndRelease();
|
|
}
|
|
|
|
void Initialize(CreatureLib::Battling::BattleLibrary* library) override { Initialize(library, true); }
|
|
void Initialize(CreatureLib::Battling::BattleLibrary* library, bool includeStandard);
|
|
void CreateScript(const char* name, const char* script);
|
|
const asIScriptModule* GetMainModule() const noexcept { return _mainModule; }
|
|
|
|
void FinalizeModule();
|
|
|
|
CreatureLib::Battling::BattleScript* LoadScript(ScriptCategory category,
|
|
const ArbUt::StringView& scriptName) override;
|
|
|
|
void WriteByteCodeToFile(const char* file, bool stripDebugInfo = false);
|
|
void LoadByteCodeFromFile(const char* file);
|
|
uint8_t* WriteByteCodeToMemory(size_t& size, bool stripDebugInfo = false);
|
|
void LoadByteCodeFromMemory(uint8_t*, size_t size);
|
|
|
|
const ArbUt::Dictionary<ScriptCategory, ArbUt::Dictionary<ArbUt::StringView, AngelScriptTypeInfo*>>&
|
|
GetTypeDatabase() const noexcept {
|
|
return _typeDatabase;
|
|
}
|
|
|
|
void RegisterType(const char* type) {
|
|
int r = _engine->RegisterObjectType(type, 0, asOBJ_REF | asOBJ_NOCOUNT);
|
|
Ensure(r >= 0);
|
|
}
|
|
void RegisterTypeMethod(const char* type, const char* decl, void*(func)(void*)) {
|
|
int r = _engine->RegisterObjectMethod(type, decl, asFunctionPtr(func), asCALL_CDECL_OBJFIRST);
|
|
Ensure(r >= 0);
|
|
}
|
|
void RegisterGlobalMethod(const char*, void*(func)(void*)) {
|
|
auto r = _engine->RegisterGlobalFunction("decl", asFunctionPtr(func), asCALL_CDECL);
|
|
Ensure(r >= 0);
|
|
}
|
|
|
|
asITypeInfo* GetBaseType(const ArbUt::StringView& name) {
|
|
asITypeInfo* t;
|
|
auto v = _baseTypes.TryGet(name);
|
|
if (!v.has_value()) {
|
|
t = this->_engine->GetTypeInfoByDecl(name.c_str());
|
|
_baseTypes.Insert(name, t);
|
|
} else {
|
|
t = v.value();
|
|
}
|
|
return t;
|
|
}
|
|
};
|
|
#endif // PKMNLIB_ANGELSCRIPRESOLVER_HPP
|