Layout work on an AngelScript implementation.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
47
src/AngelScript/AngelScriptTypeInfo.hpp
Normal file
47
src/AngelScript/AngelScriptTypeInfo.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user