Initial support for item use scripts in angelscript.
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:
52
src/ScriptResolving/AngelScript/AngelScriptFunctionCall.hpp
Normal file
52
src/ScriptResolving/AngelScript/AngelScriptFunctionCall.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef PKMNLIB_ANGELSCRIPTFUNCTIONCALL_HPP
|
||||
#define PKMNLIB_ANGELSCRIPTFUNCTIONCALL_HPP
|
||||
|
||||
#include "ContextPool.hpp"
|
||||
|
||||
class AngelScriptUtils {
|
||||
public:
|
||||
static void AngelscriptFunctionCall(asIScriptFunction* func, ContextPool* ctxPool, asIScriptObject* obj,
|
||||
const ArbUt::StringView& scriptName,
|
||||
const std::function<void(asIScriptContext*)>& setup,
|
||||
const std::function<void(asIScriptContext*)>& onEnd) {
|
||||
auto ctx = asGetActiveContext();
|
||||
bool newContext = false;
|
||||
if (ctx == nullptr) {
|
||||
ctx = ctxPool->RequestContext();
|
||||
newContext = true;
|
||||
} else {
|
||||
ctx->PushState();
|
||||
}
|
||||
ctx->Prepare(func);
|
||||
ctx->SetObject(obj);
|
||||
setup(ctx);
|
||||
auto scriptResult = ctx->Execute();
|
||||
if (scriptResult != asEXECUTION_FINISHED) {
|
||||
if (scriptResult == asEXECUTION_EXCEPTION) {
|
||||
std::stringstream err;
|
||||
err << "Script exception in script '" << scriptName.c_str() << "', line "
|
||||
<< ctx->GetExceptionLineNumber() << ". Message: '" << ctx->GetExceptionString() << "'.";
|
||||
if (newContext) {
|
||||
ctxPool->ReturnContextToPool(ctx);
|
||||
} else {
|
||||
ctx->PopState();
|
||||
}
|
||||
throw ArbUt::Exception(err.str());
|
||||
}
|
||||
if (newContext) {
|
||||
ctxPool->ReturnContextToPool(ctx);
|
||||
} else {
|
||||
ctx->PopState();
|
||||
}
|
||||
THROW("Script didn't finish properly; message " << scriptResult);
|
||||
}
|
||||
onEnd(ctx);
|
||||
if (newContext) {
|
||||
ctxPool->ReturnContextToPool(ctx);
|
||||
} else {
|
||||
ctx->PopState();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user