add scriptengine.*
This commit is contained in:
parent
d2215a7422
commit
58ef5f9e7d
84
cpp/scriptengine.cc
Normal file
84
cpp/scriptengine.cc
Normal file
@ -0,0 +1,84 @@
|
||||
#include <a8/a8.h>
|
||||
#include <a8/pyengine.h>
|
||||
#include <a8/udplog.h>
|
||||
|
||||
#include "scriptengine.h"
|
||||
|
||||
void ScriptEngine::Init()
|
||||
{
|
||||
engine_ = new a8::PyEngine();
|
||||
engine_->work_path = "pyscript/";
|
||||
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;
|
||||
}
|
30
cpp/scriptengine.h
Normal file
30
cpp/scriptengine.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
namespace a8
|
||||
{
|
||||
class PyEngine;
|
||||
};
|
||||
|
||||
class ScriptEngine : public a8::Singleton<ScriptEngine>
|
||||
{
|
||||
private:
|
||||
ScriptEngine() {};
|
||||
friend class a8::Singleton<ScriptEngine>;
|
||||
|
||||
public:
|
||||
void Init();
|
||||
void UnInit();
|
||||
void Update();
|
||||
|
||||
bool LoadModule(const std::string& module_name);
|
||||
bool LoadString(const std::string& str);
|
||||
std::tuple<bool, a8::XValue> CallGlobalFunc(const char* func_name,
|
||||
std::initializer_list<a8::XValue> args);
|
||||
std::tuple<bool, a8::XValue> CallModuleFunc(const char* module_name,
|
||||
const char* func_name,
|
||||
std::initializer_list<a8::XValue> args);
|
||||
|
||||
private:
|
||||
a8::PyEngine *engine_ = nullptr;
|
||||
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user