2020-04-23 22:05:43 +00:00
|
|
|
#ifndef PKMNLIB_ANGELSCRIPTRESOLVER_HPP
|
|
|
|
#define PKMNLIB_ANGELSCRIPTRESOLVER_HPP
|
2020-01-11 21:30:23 +00:00
|
|
|
|
2020-02-08 18:22:29 +00:00
|
|
|
#include <CreatureLib/Battling/ScriptHandling/ScriptResolver.hpp>
|
2020-02-19 08:07:44 +00:00
|
|
|
#include "../../../extern/angelscript_addons/scriptbuilder/scriptbuilder.h"
|
2020-04-11 12:42:49 +00:00
|
|
|
#include "../../Battling/Library/BattleLibrary.hpp"
|
2020-01-11 21:30:23 +00:00
|
|
|
|
2020-01-12 17:20:59 +00:00
|
|
|
#define ANGELSCRIPT_DLL_LIBRARY_IMPORT
|
2021-03-07 16:09:30 +00:00
|
|
|
#include <Arbutils/StringView.hpp>
|
2020-01-11 21:30:23 +00:00
|
|
|
#include <angelscript.h>
|
2021-03-07 16:09:30 +00:00
|
|
|
#include "AngelScriptItemUseScript.hpp"
|
2020-01-11 21:30:23 +00:00
|
|
|
#include "AngelScriptScript.hpp"
|
|
|
|
#include "AngelScriptTypeInfo.hpp"
|
|
|
|
|
2020-10-15 16:13:00 +00:00
|
|
|
class AngelScriptResolver final : public CreatureLib::Battling::ScriptResolver {
|
2020-01-11 21:30:23 +00:00
|
|
|
private:
|
|
|
|
asIScriptEngine* _engine = nullptr;
|
|
|
|
asIScriptModule* _mainModule = nullptr;
|
|
|
|
ContextPool* _contextPool = nullptr;
|
2020-02-19 08:07:44 +00:00
|
|
|
CScriptBuilder _builder;
|
2020-01-11 21:30:23 +00:00
|
|
|
|
|
|
|
static void MessageCallback(const asSMessageInfo* msg, void* param);
|
|
|
|
static void Print(const std::string& str) { std::cout << str << std::endl; }
|
2021-03-28 15:15:06 +00:00
|
|
|
static void PrintConst(const ArbUt::StringView& str) { Print(std::string(str.std_str())); }
|
2020-07-04 13:50:30 +00:00
|
|
|
ArbUt::Dictionary<ScriptCategory, ArbUt::Dictionary<ArbUt::StringView, AngelScriptTypeInfo*>> _typeDatabase;
|
|
|
|
ArbUt::Dictionary<ArbUt::StringView, asITypeInfo*> _baseTypes;
|
2021-03-07 16:09:30 +00:00
|
|
|
ArbUt::Dictionary<ArbUt::StringView, asITypeInfo*> _itemUseTypes;
|
|
|
|
ArbUt::Dictionary<const CreatureLib::Library::Item*, AngelScriptItemUseScript*> _itemUseScripts;
|
2020-01-11 21:30:23 +00:00
|
|
|
|
2020-01-26 14:18:04 +00:00
|
|
|
void RegisterTypes();
|
2020-07-04 13:50:30 +00:00
|
|
|
void
|
2020-07-18 10:42:54 +00:00
|
|
|
InitializeByteCode(const ArbUt::Dictionary<ScriptCategory, ArbUt::Dictionary<ArbUt::StringView, uint32_t>>& types);
|
2021-03-07 16:09:30 +00:00
|
|
|
void RegisterScriptType(asITypeInfo* typeInfo, const ArbUt::StringView& metadataKind,
|
|
|
|
const ArbUt::StringView& effectName);
|
2020-01-26 14:18:04 +00:00
|
|
|
|
2020-01-11 21:30:23 +00:00
|
|
|
public:
|
2020-04-23 22:05:43 +00:00
|
|
|
~AngelScriptResolver() override {
|
2021-03-07 17:20:42 +00:00
|
|
|
for (const auto& ius : _itemUseScripts) {
|
|
|
|
delete ius.second;
|
|
|
|
}
|
2020-01-11 21:30:23 +00:00
|
|
|
delete _contextPool;
|
2020-04-10 12:57:20 +00:00
|
|
|
for (const auto& category : _typeDatabase) {
|
2020-04-11 12:42:49 +00:00
|
|
|
for (const auto& type : category.second) {
|
2020-04-10 12:57:20 +00:00
|
|
|
delete type.second;
|
|
|
|
}
|
2020-01-11 21:30:23 +00:00
|
|
|
}
|
|
|
|
_engine->ShutDownAndRelease();
|
|
|
|
}
|
|
|
|
|
2020-12-11 13:08:13 +00:00
|
|
|
void Initialize(CreatureLib::Battling::BattleLibrary* library) override { Initialize(library, true); }
|
2020-09-19 13:57:20 +00:00
|
|
|
void Initialize(CreatureLib::Battling::BattleLibrary* library, bool includeStandard);
|
2020-02-16 12:33:58 +00:00
|
|
|
void CreateScript(const char* name, const char* script);
|
2020-09-19 11:53:56 +00:00
|
|
|
const asIScriptModule* GetMainModule() const noexcept { return _mainModule; }
|
2020-01-11 21:30:23 +00:00
|
|
|
|
|
|
|
void FinalizeModule();
|
|
|
|
|
2021-03-07 10:12:18 +00:00
|
|
|
CreatureLib::Battling::BattleScript* LoadScript(ScriptCategory category,
|
|
|
|
const ArbUt::StringView& scriptName) override;
|
2021-03-07 16:09:30 +00:00
|
|
|
CreatureLib::Battling::ItemUseScript* LoadItemScript(const CreatureLib::Library::Item* item) override;
|
2020-04-11 12:42:49 +00:00
|
|
|
|
|
|
|
void WriteByteCodeToFile(const char* file, bool stripDebugInfo = false);
|
2020-05-02 09:13:04 +00:00
|
|
|
void LoadByteCodeFromFile(const char* file);
|
2020-04-11 12:42:49 +00:00
|
|
|
uint8_t* WriteByteCodeToMemory(size_t& size, bool stripDebugInfo = false);
|
2020-05-02 09:13:04 +00:00
|
|
|
void LoadByteCodeFromMemory(uint8_t*, size_t size);
|
2020-04-11 12:42:49 +00:00
|
|
|
|
2020-07-04 13:50:30 +00:00
|
|
|
const ArbUt::Dictionary<ScriptCategory, ArbUt::Dictionary<ArbUt::StringView, AngelScriptTypeInfo*>>&
|
2020-05-27 15:26:25 +00:00
|
|
|
GetTypeDatabase() const noexcept {
|
2020-04-11 12:42:49 +00:00
|
|
|
return _typeDatabase;
|
|
|
|
}
|
2020-04-23 22:02:10 +00:00
|
|
|
|
2020-05-02 09:53:12 +00:00
|
|
|
void RegisterType(const char* type) {
|
|
|
|
int r = _engine->RegisterObjectType(type, 0, asOBJ_REF | asOBJ_NOCOUNT);
|
2020-12-13 11:32:52 +00:00
|
|
|
Ensure(r >= 0);
|
2020-05-02 09:53:12 +00:00
|
|
|
}
|
|
|
|
void RegisterTypeMethod(const char* type, const char* decl, void*(func)(void*)) {
|
|
|
|
int r = _engine->RegisterObjectMethod(type, decl, asFunctionPtr(func), asCALL_CDECL_OBJFIRST);
|
2020-12-13 11:32:52 +00:00
|
|
|
Ensure(r >= 0);
|
2020-05-02 09:53:12 +00:00
|
|
|
}
|
2020-07-18 10:42:54 +00:00
|
|
|
void RegisterGlobalMethod(const char*, void*(func)(void*)) {
|
2020-05-02 09:53:12 +00:00
|
|
|
auto r = _engine->RegisterGlobalFunction("decl", asFunctionPtr(func), asCALL_CDECL);
|
2020-12-13 11:32:52 +00:00
|
|
|
Ensure(r >= 0);
|
2020-05-02 09:53:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 13:50:30 +00:00
|
|
|
asITypeInfo* GetBaseType(const ArbUt::StringView& name) {
|
2020-12-12 13:25:27 +00:00
|
|
|
asITypeInfo* t;
|
|
|
|
auto v = _baseTypes.TryGet(name);
|
|
|
|
if (!v.has_value()) {
|
2021-03-27 21:19:18 +00:00
|
|
|
t = _mainModule->GetTypeInfoByName(name.c_str());
|
2021-03-27 21:40:16 +00:00
|
|
|
if (t == NULL) {
|
2021-03-27 21:19:18 +00:00
|
|
|
t = _engine->GetTypeInfoByDecl(name.c_str());
|
|
|
|
}
|
2020-04-23 22:02:10 +00:00
|
|
|
_baseTypes.Insert(name, t);
|
2020-12-12 13:25:27 +00:00
|
|
|
} else {
|
|
|
|
t = v.value();
|
2020-04-23 22:02:10 +00:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
2021-03-07 16:09:30 +00:00
|
|
|
|
|
|
|
inline ContextPool* GetContextPool() const noexcept { return _contextPool; }
|
2020-01-11 21:30:23 +00:00
|
|
|
};
|
|
|
|
#endif // PKMNLIB_ANGELSCRIPRESOLVER_HPP
|