#include "pet_skill_state.h" #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_minimap.h" #include "quest_state.h" #include "guild_state.h" #include "battlepass_state.h" #include "drop_item_state.h" #include "dungeon_info_state.h" #include "switchbot_state.h" #include "messenger_state.h" #include "cube_renewal_state.h" #include "shop_search_state.h" #include "python_net.h" #include "python_app.h" #include "python_dbg.h" #include "python_touch.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 "python_event_manager.h" #include "python_chat.h" #include "../upstream/ava2-client/UserInterface/Locale_inc.h" #include #include #include #include #include #include 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"); std::vector mounts{pack_root}; // The bundle keeps scripts in root/ and the other virtual packs beside it. if (pack_root.filename()=="root" && std::filesystem::is_regular_file(pack_root.parent_path()/"asset_manifest.json")) mounts.push_back(pack_root.parent_path()); for (const auto& mount:mounts) { auto current=mount; bool missing=false; for(const auto& part:rel) { if(part=="." || part.empty()) continue; if(!std::filesystem::is_directory(current)) {missing=true;break;} 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; 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) {missing=true;break;} current=match; } if(missing) continue; auto canonical=std::filesystem::canonical(current);auto relative=canonical.lexically_relative(mount); if(relative.empty() || *relative.begin()=="..") throw std::runtime_error("pack path escapes mount"); return canonical; } throw std::runtime_error("pack path not found"); } 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(); GetPetSkillState()=PetSkillState{}; ResetSkillDescriptions(); ResetShopState(); GetQuestState().records.clear(); GetGuildState()=GuildState{}; GetShopSearchItems().clear(); GetMessengerState().Reset(); GetCubeRenewalState().Reset(); GetBattlePassState().Reset(); GetDropItemState().Reset(); GetDungeonInfoState()=DungeonInfoState{}; GetSwitchbotState()=SwitchbotState{}; 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("touch",inittouch)!=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 || PyImport_AppendInittab("impMgr",initModuleNameManager)!=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; CResourceManager::SetResourceRoot(std::filesystem::absolute(root)); m_resourceManager = std::make_unique(); Py_Initialize(); m_windowManager = std::make_unique(); 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("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(); GetMessengerState().Reset(); GetCubeRenewalState().Reset(); GetBattlePassState().Reset(); GetDropItemState().Reset(); GetDungeonInfoState()=DungeonInfoState{}; GetSwitchbotState()=SwitchbotState{}; // The event sets hold weak references to Python objects. CPythonEventManager::Instance().Destroy();CPythonChat::Instance().Destroy();CPythonSystem::Instance().Clear(); CPythonEventManager::Instance().SetInterfaceWindow(nullptr); clear_compiled_code(); Py_Finalize(); if (m_windowManager) { m_windowManager->Destroy(); m_windowManager.reset(); } CPythonMiniMap::Instance().Destroy(); 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(); GetMessengerState().Reset(); GetCubeRenewalState().Reset(); GetBattlePassState().Reset(); GetDropItemState().Reset(); GetDungeonInfoState()=DungeonInfoState{}; GetSwitchbotState()=SwitchbotState{}; // The event sets hold weak references to Python objects. CPythonEventManager::Instance().Destroy();CPythonChat::Instance().Destroy();CPythonSystem::Instance().Clear(); CPythonEventManager::Instance().SetInterfaceWindow(nullptr); clear_compiled_code(); Py_Finalize(); if (m_windowManager) { m_windowManager->Destroy(); m_windowManager.reset(); } CPythonMiniMap::Instance().Destroy(); CGraphicExpandedImageInstance::DestroySystem(); CGraphicImageInstance::DestroySystem(); CGraphicMarkInstance::DestroySystem(); if (m_resourceManager) { m_resourceManager->Destroy(); m_resourceManager.reset(); } pack_root.clear(); } void PythonRuntime::clear_compiled_code() { for(auto& entry:m_compiledCode)Py_DECREF(entry.object); m_compiledCode.clear();m_compiledBytes=0; } void PythonRuntime::execute(const std::string& source) { PyObject* code=nullptr; for(auto it=m_compiledCode.begin();it!=m_compiledCode.end();++it) { if(it->source!=source)continue; code=it->object;Py_INCREF(code);m_compiledCode.splice(m_compiledCode.begin(),m_compiledCode,it);break; } if(!code) { code=Py_CompileString(source.c_str(),"",Py_file_input);++m_compileCount; if(!code){PyErr_Print();throw std::runtime_error("embedded Python execution failed (see traceback)");} if(source.size()<=16u*1024u) { Py_INCREF(code);m_compiledCode.push_front({source,code});m_compiledBytes+=source.size(); while(m_compiledCode.size()>64 || m_compiledBytes>128u*1024u) { auto& entry=m_compiledCode.back();m_compiledBytes-=entry.source.size();Py_DECREF(entry.object);m_compiledCode.pop_back(); } } } auto* module=PyImport_AddModule("__main__"); auto* globals=module ? PyModule_GetDict(module) : nullptr; auto* result=globals ? PyEval_EvalCode(reinterpret_cast(code),globals,globals) : nullptr; Py_DECREF(code); if(!result){PyErr_Print();throw std::runtime_error("embedded Python execution failed (see traceback)");} Py_DECREF(result); } } 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(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("Connect"),const_cast("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(method),nullptr):PyObject_CallMethod(current_view(),const_cast(method),const_cast("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("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