AVA2 cross-platform client parity baseline
This commit is contained in:
@@ -0,0 +1,295 @@
|
||||
#include "eter_ui/PythonWindowManager.h"
|
||||
#include "src/eter_image/GrpImageInstance.h"
|
||||
#include "src/eter_image/GrpExpandedImageInstance.h"
|
||||
#include "src/eter_image/GrpMarkInstance.h"
|
||||
#include "python_runtime.h"
|
||||
#include "python_net.h"
|
||||
#include "python_app.h"
|
||||
#include "python_dbg.h"
|
||||
#include "python_system.h"
|
||||
#include "python_wndmgr.h"
|
||||
#include "python_gameplay_modules.h"
|
||||
#include "python_ime.h"
|
||||
#include "python_grp.h"
|
||||
#include "python_native_modules.h"
|
||||
#include "../upstream/ava2-client/UserInterface/Locale_inc.h"
|
||||
#include <Python.h>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <cctype>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
namespace ava2 {
|
||||
namespace {
|
||||
std::filesystem::path pack_root;
|
||||
std::filesystem::path resolve(const char* name) {
|
||||
std::string normalized(name);std::replace(normalized.begin(),normalized.end(),'\\','/');
|
||||
std::filesystem::path rel(normalized);
|
||||
if(rel.is_absolute()) throw std::invalid_argument("pack path must be relative");
|
||||
for(const auto& part:rel) if(part=="..") throw std::invalid_argument("pack traversal rejected");
|
||||
auto current=pack_root;
|
||||
for(const auto& part:rel) {
|
||||
if(part=="." || part.empty()) continue;
|
||||
auto wanted=part.string();std::transform(wanted.begin(),wanted.end(),wanted.begin(),[](unsigned char c){return std::tolower(c);});
|
||||
bool found=false; std::filesystem::path match;
|
||||
if(!std::filesystem::is_directory(current)) throw std::runtime_error("pack path not found");
|
||||
for(const auto& entry:std::filesystem::directory_iterator(current)) {
|
||||
auto candidate=entry.path().filename().string();std::transform(candidate.begin(),candidate.end(),candidate.begin(),[](unsigned char c){return std::tolower(c);});
|
||||
if(candidate==wanted) { if(found) throw std::runtime_error("ambiguous case-insensitive pack path");match=entry.path();found=true; }
|
||||
}
|
||||
if(!found) throw std::runtime_error("pack path not found");
|
||||
current=match;
|
||||
}
|
||||
auto canonical=std::filesystem::canonical(current);auto relative=canonical.lexically_relative(pack_root);
|
||||
if(relative.empty() || *relative.begin()=="..") throw std::runtime_error("pack path escapes mount");
|
||||
return canonical;
|
||||
}
|
||||
PyObject* pack_exist(PyObject*,PyObject* args) {
|
||||
const char* name=nullptr;if(!PyArg_ParseTuple(args,"s",&name)) return nullptr;
|
||||
try { return PyInt_FromLong(std::filesystem::is_regular_file(resolve(name))); } catch(...) { return PyInt_FromLong(0); }
|
||||
}
|
||||
PyObject* pack_get(PyObject*,PyObject* args) {
|
||||
const char* name=nullptr;if(!PyArg_ParseTuple(args,"s",&name)) return nullptr;
|
||||
try {
|
||||
auto path=resolve(name);auto size=std::filesystem::file_size(path);if(size>64*1024*1024) throw std::runtime_error("pack file too large");
|
||||
std::ifstream stream(path,std::ios::binary);std::string data(size,'\0');if(!stream.read(data.data(),data.size())) throw std::runtime_error("pack read failed");
|
||||
return PyString_FromStringAndSize(data.data(),data.size());
|
||||
} catch(const std::exception& e) { PyErr_SetString(PyExc_IOError,e.what());return nullptr; }
|
||||
}
|
||||
void initpack() {
|
||||
static PyMethodDef methods[]={{"Exist",pack_exist,METH_VARARGS,nullptr},{"Get",pack_get,METH_VARARGS,nullptr},{nullptr,nullptr,0,nullptr}};
|
||||
Py_InitModule("pack",methods);
|
||||
}
|
||||
}
|
||||
std::string ReadPackedText(const std::string& name) {
|
||||
auto path=resolve(name.c_str());
|
||||
auto size=std::filesystem::file_size(path);
|
||||
if(size>64*1024*1024) throw std::runtime_error("pack text too large");
|
||||
std::ifstream file(path,std::ios::binary); std::string text(size,'\0');
|
||||
if(!file.read(text.data(),text.size())) throw std::runtime_error("pack text read failed");
|
||||
return text;
|
||||
}
|
||||
PythonRuntime::PythonRuntime(const std::filesystem::path& stdlib,const std::filesystem::path& root) {
|
||||
if(Py_IsInitialized()) throw std::logic_error("one embedded Python runtime allowed");
|
||||
ResetPythonAppState();
|
||||
ResetSkillDescriptions();
|
||||
ResetShopState();
|
||||
if(!std::filesystem::is_regular_file(stdlib)) throw std::runtime_error("python27.zip missing");
|
||||
pack_root=std::filesystem::canonical(root);
|
||||
static bool registered=false;
|
||||
if(!registered) {
|
||||
if(PyImport_AppendInittab("pack",initpack)!=0 ||
|
||||
PyImport_AppendInittab("app",initapp)!=0 ||
|
||||
PyImport_AppendInittab("dbg",initdbg)!=0 ||
|
||||
PyImport_AppendInittab("systemSetting",initsystemSetting)!=0 ||
|
||||
PyImport_AppendInittab("wndMgr",initwndMgr)!=0 ||
|
||||
PyImport_AppendInittab("item",initItem)!=0 ||
|
||||
PyImport_AppendInittab("player",initPlayer)!=0 ||
|
||||
PyImport_AppendInittab("chrmgr",initchrmgr)!=0 ||
|
||||
PyImport_AppendInittab("chr",initchr)!=0 ||
|
||||
PyImport_AppendInittab("chat",initChat)!=0 ||
|
||||
PyImport_AppendInittab("snd",initsnd)!=0 ||
|
||||
PyImport_AppendInittab("background",initBackground)!=0 ||
|
||||
PyImport_AppendInittab("ime",initime)!=0 ||
|
||||
PyImport_AppendInittab("grp",initgrp)!=0 ||
|
||||
PyImport_AppendInittab("grpImage",initgrpImage)!=0 ||
|
||||
PyImport_AppendInittab("grpText",initgrpText)!=0 ||
|
||||
PyImport_AppendInittab("grpThing",initgrpThing)!=0 ||
|
||||
PyImport_AppendInittab("nonplayer",initNonPlayer)!=0 ||
|
||||
PyImport_AppendInittab("exchange",initTrade)!=0 ||
|
||||
PyImport_AppendInittab("textTail",initTextTail)!=0 ||
|
||||
PyImport_AppendInittab("miniMap",initMiniMap)!=0 ||
|
||||
PyImport_AppendInittab("profiler",initProfiler)!=0 ||
|
||||
PyImport_AppendInittab("event",initEvent)!=0 ||
|
||||
PyImport_AppendInittab("effect",initeffect)!=0 ||
|
||||
PyImport_AppendInittab("fly",initfly)!=0 ||
|
||||
PyImport_AppendInittab("eventMgr",initeventmgr)!=0 ||
|
||||
PyImport_AppendInittab("shop",initshop)!=0 ||
|
||||
PyImport_AppendInittab("skill",initskill)!=0 ||
|
||||
PyImport_AppendInittab("skillpet",initskillpet)!=0 ||
|
||||
PyImport_AppendInittab("quest",initquest)!=0 ||
|
||||
PyImport_AppendInittab("messenger",initMessenger)!=0 ||
|
||||
PyImport_AppendInittab("sash",initSash)!=0 ||
|
||||
PyImport_AppendInittab("ServerStateChecker",initServerStateChecker)!=0 ||
|
||||
PyImport_AppendInittab("playerLoad",initPlayerSettingsModule)!=0 ||
|
||||
PyImport_AppendInittab("switchbot",initSwitchbot)!=0 ||
|
||||
PyImport_AppendInittab("wiki",initWiki)!=0 ||
|
||||
PyImport_AppendInittab("combat_zone",initCombatZoneSystem)!=0 ||
|
||||
PyImport_AppendInittab("managerBankLogs",initBankManager)!=0 ||
|
||||
PyImport_AppendInittab("dropitem",intdropitem)!=0 ||
|
||||
PyImport_AppendInittab("drop_item",intdropitem)!=0 ||
|
||||
PyImport_AppendInittab("safebox",initsafebox)!=0 ||
|
||||
PyImport_AppendInittab("guild",initguild)!=0 ||
|
||||
PyImport_AppendInittab("dungeon_info",intdungeoninfo)!=0 ||
|
||||
PyImport_AppendInittab("renderTarget",initRenderTarget)!=0 ||
|
||||
PyImport_AppendInittab("cuberenewal",intcuberenewal)!=0 ||
|
||||
PyImport_AppendInittab("cube_renewal",intcuberenewal)!=0 ||
|
||||
PyImport_AppendInittab("battlepass",intbattlepass)!=0 ||
|
||||
PyImport_AppendInittab("privateShopSearch",initprivateShopSearch)!=0 ||
|
||||
PyImport_AppendInittab("udp",initudp)!=0)
|
||||
throw std::runtime_error("native module registration failed");
|
||||
register_python_net();
|
||||
registered=true;
|
||||
}
|
||||
Py_FrozenFlag=1;Py_IgnoreEnvironmentFlag=1;Py_NoSiteFlag=1;Py_DontWriteBytecodeFlag=1;
|
||||
m_resourceManager = std::make_unique<CResourceManager>();
|
||||
Py_Initialize();
|
||||
m_windowManager = std::make_unique<UI::CWindowManager>();
|
||||
auto* builtins=PyImport_ImportModule("__builtin__");
|
||||
if(builtins) {
|
||||
PyModule_AddIntConstant(builtins, "__DEBUG__", 1);
|
||||
PyModule_AddIntConstant(builtins, "__USE_CYTHON__", 0);
|
||||
PyModule_AddIntConstant(builtins, "__USE_EXTRA_CYTHON__", 0);
|
||||
PyModule_AddStringConstant(builtins, "__COMMAND_LINE__", "");
|
||||
PyModule_AddIntConstant(builtins, "TRUE", 1);
|
||||
PyModule_AddIntConstant(builtins, "FALSE", 0);
|
||||
auto* open_func = PyDict_GetItemString(PyModule_GetDict(builtins), "open");
|
||||
if (open_func) {
|
||||
PyDict_SetItemString(PyModule_GetDict(builtins), "old_open", open_func);
|
||||
PyDict_SetItemString(PyModule_GetDict(builtins), "new_open", open_func);
|
||||
}
|
||||
Py_DECREF(builtins);
|
||||
}
|
||||
auto* path=PyList_New(0);
|
||||
for(const auto& dir:{std::filesystem::absolute(stdlib),pack_root}) {
|
||||
auto* value=PyString_FromString(dir.string().c_str());PyList_Append(path,value);Py_DECREF(value);
|
||||
}
|
||||
PySys_SetObject(const_cast<char*>("path"),path);Py_DECREF(path);
|
||||
// Original scripts request mixed-case module names from lowercase pack files.
|
||||
// Host-side importer only; complete system.py/app/dbg bootstrap is still pending.
|
||||
try { execute(R"PY(
|
||||
import sys, imp, pack, os, __builtin__
|
||||
try:
|
||||
import encodings
|
||||
except:
|
||||
pass
|
||||
|
||||
_orig_open = __builtin__.open
|
||||
def _ava2_open(filename, mode='r', buffering=-1):
|
||||
candidates = [filename]
|
||||
if isinstance(filename, (str, unicode)):
|
||||
f = filename.replace('\\', '/')
|
||||
if len(f) >= 2 and f[1] == ':':
|
||||
f = f[2:]
|
||||
if f.startswith('/'):
|
||||
f = f[1:]
|
||||
candidates.append(f)
|
||||
if f.lower() != f:
|
||||
candidates.append(f.lower())
|
||||
for cand in candidates:
|
||||
try:
|
||||
return _orig_open(cand, mode, buffering)
|
||||
except (IOError, OSError):
|
||||
continue
|
||||
return _orig_open(filename, mode, buffering)
|
||||
|
||||
__builtin__.open = _ava2_open
|
||||
__builtin__.old_open = _orig_open
|
||||
__builtin__.new_open = _ava2_open
|
||||
|
||||
class _AVA2PackImporter(object):
|
||||
def find_module(self, fullname, path=None):
|
||||
if imp.is_builtin(fullname):
|
||||
return None
|
||||
if path is None and '.' not in fullname and pack.Exist(fullname + '.py'):
|
||||
return self
|
||||
return None
|
||||
def load_module(self, fullname):
|
||||
if fullname in sys.modules:
|
||||
return sys.modules[fullname]
|
||||
filename = fullname + '.py'
|
||||
source = pack.Get(filename).replace('\r\n', '\n')
|
||||
code = compile(source, filename, 'exec')
|
||||
module = imp.new_module(fullname)
|
||||
module.__file__ = filename
|
||||
module.__loader__ = self
|
||||
sys.modules[fullname] = module
|
||||
try:
|
||||
exec code in module.__dict__
|
||||
except:
|
||||
del sys.modules[fullname]
|
||||
raise
|
||||
return module
|
||||
sys.meta_path.insert(0, _AVA2PackImporter())
|
||||
)PY"); } catch(...) {
|
||||
reset_python_net();
|
||||
ClearPlayerPythonWindow();
|
||||
Py_Finalize();
|
||||
if (m_windowManager)
|
||||
{
|
||||
m_windowManager->Destroy();
|
||||
m_windowManager.reset();
|
||||
}
|
||||
CGraphicExpandedImageInstance::DestroySystem();
|
||||
CGraphicImageInstance::DestroySystem();
|
||||
CGraphicMarkInstance::DestroySystem();
|
||||
if (m_resourceManager)
|
||||
{
|
||||
m_resourceManager->Destroy();
|
||||
m_resourceManager.reset();
|
||||
}
|
||||
pack_root.clear();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
PythonRuntime::~PythonRuntime() {
|
||||
reset_python_net();
|
||||
ClearPlayerPythonWindow();
|
||||
Py_Finalize();
|
||||
if (m_windowManager)
|
||||
{
|
||||
m_windowManager->Destroy();
|
||||
m_windowManager.reset();
|
||||
}
|
||||
CGraphicExpandedImageInstance::DestroySystem();
|
||||
CGraphicImageInstance::DestroySystem();
|
||||
CGraphicMarkInstance::DestroySystem();
|
||||
if (m_resourceManager)
|
||||
{
|
||||
m_resourceManager->Destroy();
|
||||
m_resourceManager.reset();
|
||||
}
|
||||
pack_root.clear();
|
||||
}
|
||||
void PythonRuntime::execute(const std::string& code) { if(PyRun_SimpleString(code.c_str())!=0) throw std::runtime_error("embedded Python execution failed (see traceback)"); }
|
||||
}
|
||||
|
||||
namespace ava2 {
|
||||
namespace {
|
||||
PyObject* current_view() {
|
||||
auto* main=PyImport_AddModule("__main__");
|
||||
auto* view=PyDict_GetItemString(PyModule_GetDict(main),"view");
|
||||
if(!view) throw std::runtime_error("login view has not been loaded");
|
||||
return view;
|
||||
}
|
||||
void checked(PyObject* result) { if(!result) { PyErr_Print();throw std::runtime_error("Python view operation failed"); }Py_DECREF(result); }
|
||||
}
|
||||
void PythonRuntime::load_login_view(const std::filesystem::path& file) {
|
||||
std::ifstream input(file,std::ios::binary);if(!input) throw std::runtime_error("login view script missing");
|
||||
std::string source((std::istreambuf_iterator<char>(input)),{});execute(source);
|
||||
}
|
||||
void PythonRuntime::connect_view(const std::string& game,int game_port,const std::string& auth,int auth_port,const std::string& account,const std::string& password) {
|
||||
checked(PyObject_CallMethod(current_view(),const_cast<char*>("Connect"),const_cast<char*>("sisiss"),game.c_str(),game_port,auth.c_str(),auth_port,account.c_str(),password.c_str()));
|
||||
}
|
||||
void PythonRuntime::view_action(const char* method,int value) {
|
||||
checked(value<0?PyObject_CallMethod(current_view(),const_cast<char*>(method),nullptr):PyObject_CallMethod(current_view(),const_cast<char*>(method),const_cast<char*>("i"),value));
|
||||
}
|
||||
void PythonRuntime::pump_network() { try { pump_python_net(); } catch(...) { if(PyErr_Occurred()) PyErr_Print();throw; } }
|
||||
LoginViewSnapshot PythonRuntime::view_snapshot() {
|
||||
auto* result=PyObject_CallMethod(current_view(),const_cast<char*>("Snapshot"),nullptr);
|
||||
if(!result) { PyErr_Print();throw std::runtime_error("cannot read login view"); }
|
||||
struct Release { PyObject* p;~Release(){Py_DECREF(p);} } release{result};
|
||||
char* phase=nullptr;char* message=nullptr;int selected;PyObject* slots=nullptr;
|
||||
if(!PyArg_ParseTuple(result,"ssiO",&phase,&message,&selected,&slots) || !PyList_Check(slots)) { PyErr_Print();throw std::runtime_error("invalid login view snapshot"); }
|
||||
LoginViewSnapshot snapshot; snapshot.phase=phase;snapshot.message=message;snapshot.selected=selected;
|
||||
for(Py_ssize_t i=0;i<PyList_Size(slots);++i) {
|
||||
unsigned long id;char* name;int level,job;
|
||||
if(!PyArg_ParseTuple(PyList_GetItem(slots,i),"ksii",&id,&name,&level,&job)) { PyErr_Print();throw std::runtime_error("invalid character snapshot"); }
|
||||
snapshot.slots.push_back({id,name,level,job});
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
}
|
||||
|
||||
UI::CWindowManager* ava2::PythonRuntime::window_manager() { return m_windowManager.get(); }
|
||||
Reference in New Issue
Block a user