#ifndef PKMNLIB_ANGELSCRIPTFUNCTIONCALL_HPP #define PKMNLIB_ANGELSCRIPTFUNCTIONCALL_HPP #include "AngelScriptResolver.hpp" #include "ContextPool.hpp" class AngelScriptUtils { public: static void AngelscriptFunctionCall(asIScriptFunction* func, ContextPool* ctxPool, asIScriptObject* obj, AngelScriptResolver* resolver, const ArbUt::StringView& scriptName, const std::function& setup, const std::function& onEnd) { auto ctx = asGetActiveContext(); bool newContext = false; if (ctx == nullptr) { ctx = ctxPool->RequestContext(); newContext = true; } else { ctx->PushState(); } if (ctx->GetUserData() == nullptr) { ctx->SetUserData(resolver->GetUserdata()); } 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