f8/cpp/scriptengine.cc
aozhiwei 7a68854ee6 1
2018-08-30 18:07:58 +08:00

89 lines
2.7 KiB
C++

#include <a8/a8.h>
#include <a8/pyengine.h>
#include <a8/udplog.h>
#include "scriptengine.h"
void ScriptEngine::Init()
{
engine_ = new a8::PyEngine();
if (getenv("machine_type")) {
engine_->work_path = "/var/data/conf_test/game1008/res/pyscripts/";
} else {
engine_->work_path = "../res/pyscripts/";
}
engine_->Init();
if (!engine_->IsInitialized()) {
abort();
}
}
void ScriptEngine::UnInit()
{
engine_->UnInit();
delete engine_;
engine_ = nullptr;
}
void ScriptEngine::Update()
{
}
bool ScriptEngine::LoadModule(const std::string& module_name)
{
bool ret = engine_->LoadModule(module_name.c_str());
if (!ret) {
a8::UdpLog::Instance()->Warning("load python module %s error %s\n",
{
module_name,
engine_->GetLastError()
});
assert(false);
}
return ret;
}
bool ScriptEngine::LoadString(const std::string& str)
{
bool ret = engine_->ExecString(str.c_str());
if (!ret) {
a8::UdpLog::Instance()->Warning("load python string %s error %s",
{
str,
engine_->GetLastError()
});
assert(false);
}
return ret;
}
std::tuple<bool, a8::XValue> ScriptEngine::CallGlobalFunc(const char* func_name,
std::initializer_list<a8::XValue> args)
{
auto ret = engine_->CallGlobalFunc(func_name, args);
if (!std::get<0>(ret)) {
a8::UdpLog::Instance()->Warning("call global function %s error %s",
{
func_name,
engine_->GetLastError()
});
}
return ret;
}
std::tuple<bool, a8::XValue> ScriptEngine::CallModuleFunc(const char* module_name,
const char* func_name,
std::initializer_list<a8::XValue> args)
{
auto ret = engine_->CallModuleFunc(module_name, func_name, args);
if (!std::get<0>(ret)) {
a8::UdpLog::Instance()->Warning("call module function [%s] %s error %s",
{
module_name,
func_name,
engine_->GetLastError()
});
}
return ret;
}