68 lines
2.4 KiB
C++
68 lines
2.4 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 <iostream>
|
|
#include "AngelScriptScript.hpp"
|
|
#include "AngelScriptTypeInfo.hpp"
|
|
using namespace Arbutils::Collections;
|
|
|
|
class AngelScriptResolver : 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; }
|
|
Dictionary<ScriptCategory, Dictionary<ConstString, AngelScriptTypeInfo*>> _typeDatabase;
|
|
Dictionary<ConstString, asITypeInfo*> _baseTypes;
|
|
|
|
void RegisterTypes();
|
|
void InitializeByteCode(asIBinaryStream* stream,
|
|
const Dictionary<ScriptCategory, Dictionary<ConstString, 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;
|
|
void CreateScript(const char* name, const char* script);
|
|
|
|
void FinalizeModule();
|
|
|
|
CreatureLib::Battling::Script* LoadScript(ScriptCategory category, const ConstString& 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 Dictionary<ScriptCategory, Dictionary<ConstString, AngelScriptTypeInfo*>>& GetTypeDatabase() const noexcept {
|
|
return _typeDatabase;
|
|
}
|
|
|
|
asITypeInfo* GetBaseType(const ConstString& name) {
|
|
asITypeInfo* t = nullptr;
|
|
if (!_baseTypes.TryGet(name, t)) {
|
|
t = this->_engine->GetTypeInfoByDecl(name.c_str());
|
|
_baseTypes.Insert(name, t);
|
|
}
|
|
return t;
|
|
}
|
|
};
|
|
#endif // PKMNLIB_ANGELSCRIPRESOLVER_HPP
|