Files
PkmnLib/src/ScriptResolving/AngelScript/AngelScriptFunctionCall.hpp
Deukhoofd e361507ec9
Some checks failed
continuous-integration/drone/push Build is failing
Adds support for modifying volatile scripts after adding them.
2021-03-27 22:19:18 +01:00

53 lines
1.9 KiB
C++

#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();
}
printf("%s\n", err.str().c_str());
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