48 lines
1.4 KiB
C++
48 lines
1.4 KiB
C++
|
#ifndef PKMNLIB_ANGELSCRIPTTYPEINFO_HPP
|
||
|
#define PKMNLIB_ANGELSCRIPTTYPEINFO_HPP
|
||
|
|
||
|
#include <angelscript.h>
|
||
|
#include <unordered_map>
|
||
|
#include <Core/Exceptions/CreatureException.hpp>
|
||
|
|
||
|
class AngelScriptTypeInfo {
|
||
|
private:
|
||
|
asITypeInfo* _type = nullptr;
|
||
|
std::unordered_map<std::string, asIScriptFunction*> _functions;
|
||
|
|
||
|
public:
|
||
|
explicit AngelScriptTypeInfo(asITypeInfo* type) : _type(type){}
|
||
|
~AngelScriptTypeInfo(){
|
||
|
for (const auto& f: _functions){
|
||
|
f.second->Release();
|
||
|
}
|
||
|
_functions.clear();
|
||
|
}
|
||
|
|
||
|
asIScriptFunction* GetFunction(const std::string& functionName){
|
||
|
auto find = _functions.find(functionName);
|
||
|
if (find != _functions.end()){
|
||
|
return find->second;
|
||
|
}
|
||
|
auto func = _type->GetMethodByName(functionName.c_str());
|
||
|
func->AddRef();
|
||
|
_functions.insert({functionName, func});
|
||
|
return func;
|
||
|
}
|
||
|
|
||
|
asIScriptObject* Instantiate(asIScriptContext* ctx){
|
||
|
auto factory = _type->GetFactoryByIndex(0);
|
||
|
ctx->Prepare(factory);
|
||
|
auto result = ctx->Execute();
|
||
|
if (result != asEXECUTION_FINISHED){
|
||
|
throw CreatureException("Instantiation failed.");
|
||
|
}
|
||
|
asIScriptObject* obj = *(asIScriptObject**)ctx->GetAddressOfReturnValue();
|
||
|
obj->AddRef();
|
||
|
return obj;
|
||
|
}
|
||
|
|
||
|
};
|
||
|
|
||
|
#endif // PKMNLIB_ANGELSCRIPTTYPEINFO_HPP
|