From a6600ac38a7da08e2ca4141ed4a5eb4ead97a894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Mon, 4 Mar 2024 20:33:11 +0800 Subject: [PATCH] ref path --- LunaHost/GUI/confighelper.cpp | 54 +++++++++++++++++++++++++++++----- LunaHost/GUI/confighelper.h | 14 +++++++-- LunaHost/GUI/pluginmanager.cpp | 14 ++++----- 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/LunaHost/GUI/confighelper.cpp b/LunaHost/GUI/confighelper.cpp index a563119..028d009 100644 --- a/LunaHost/GUI/confighelper.cpp +++ b/LunaHost/GUI/confighelper.cpp @@ -1,5 +1,5 @@ #include"confighelper.h" - +#include"stringutils.h" std::string readfile(const wchar_t* fname) { FILE* f; _wfopen_s(&f, fname, L"rb"); @@ -52,12 +52,50 @@ void confighelper::pluginrankswap(int a,int b){ plgs[b]=plgs[a]; plgs[a]=_b; } -void confighelper::pluginsadd(const std::string& p,bool isQt){ - configs["plugins"].push_back({ - {"path",p}, - {"isQt",isQt} - }); +void confighelper::pluginsadd(const pluginitem& item){ + configs["plugins"].push_back(item.dump()); } -nlohmann::json confighelper::pluginsget(){ - return configs["plugins"]; +int confighelper::pluginsnum(){ + return configs["plugins"].size(); +} +pluginitem confighelper::pluginsget(int i){ + return pluginitem{configs["plugins"][i]}; +} +template +T safequeryjson(const nlohmann::json& js,const std::string& key,const T &defaultv){ + if(js.find(key)==js.end()){ + return defaultv; + } + return js[key]; +} +pluginitem::pluginitem(const nlohmann::json& js){ + path=js["path"]; + isQt=safequeryjson(js,"isQt",false); + isref=safequeryjson(js,"isref",false); +} +std::wstring pluginitem::wpath(){ + auto wp=StringToWideString(path); + if(isref)return std::filesystem::current_path()/wp; + else return wp; +} + +std::pair castabs2ref(const std::wstring&p){ + auto curr=std::filesystem::current_path().wstring(); + if(startWith(p,curr)){ + return {p.substr(curr.size()+1),true}; + } + return {p,false}; +} +pluginitem::pluginitem(const std::wstring& pabs,bool _isQt){ + isQt=_isQt; + auto [p,_isref]=castabs2ref(pabs); + isref=_isref; + path=WideStringToString(p); +} +nlohmann::json pluginitem::dump() const{ + return { + {"path",path}, + {"isQt",isQt}, + {"isref",isref} + }; } \ No newline at end of file diff --git a/LunaHost/GUI/confighelper.h b/LunaHost/GUI/confighelper.h index 0168bf6..3c9a7f9 100644 --- a/LunaHost/GUI/confighelper.h +++ b/LunaHost/GUI/confighelper.h @@ -1,14 +1,24 @@ #ifndef LUNA_CONFIG_HELPER #define LUNA_CONFIG_HELPER #include +struct pluginitem{ + std::string path; + bool isQt; + bool isref; + pluginitem(const nlohmann::json&); + pluginitem(const std::wstring&,bool); + std::wstring wpath(); + nlohmann::json dump() const; +}; class confighelper{ nlohmann::json configs; std::wstring configpath; public: confighelper(); ~confighelper(); - nlohmann::json pluginsget(); - void pluginsadd(const std::string&,bool); + pluginitem pluginsget(int); + int pluginsnum(); + void pluginsadd(const pluginitem&); void pluginsremove(const std::string&); void pluginrankswap(int,int); template diff --git a/LunaHost/GUI/pluginmanager.cpp b/LunaHost/GUI/pluginmanager.cpp index ab3f83d..41ce4f1 100644 --- a/LunaHost/GUI/pluginmanager.cpp +++ b/LunaHost/GUI/pluginmanager.cpp @@ -17,7 +17,7 @@ std::optionalSelectFile(HWND hwnd,LPCWSTR lpstrFilter){ ofn.lpstrFilter = lpstrFilter; ofn.lpstrFile = szFileName; ofn.nMaxFile = sizeof(szFileName); - ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; + ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR; if (GetOpenFileName(&ofn)) { @@ -67,11 +67,11 @@ Pluginmanager::Pluginmanager(LunaHost* _host):host(_host){ std::scoped_lock lock(OnNewSentenceSLock); OnNewSentenceS[L"InternalClipBoard"]=GetProcAddress(GetModuleHandle(0),"OnNewSentence");//内部链接的剪贴板插件 - auto plgs=host->configs->pluginsget(); std::vectorcollectQtplugs; - for (auto i=0;iconfigs->pluginsnum();i++) { + auto plg=host->configs->pluginsget(i); + bool isqt=plg.isQt; + auto path=plg.wpath(); PluginRank.push_back(path); OnNewSentenceS[path]=0; if(isqt){ @@ -149,7 +149,7 @@ void Pluginmanager::swaprank(int a,int b){ } bool Pluginmanager::addplugin(const std::wstring& p,bool isQt){ if(isQt){ - host->configs->pluginsadd(WideStringToString(p),isQt); + host->configs->pluginsadd({p,isQt}); return true; } auto plugin=GetProcAddress(LoadLibraryW(p.c_str()),"OnNewSentence"); @@ -159,7 +159,7 @@ bool Pluginmanager::addplugin(const std::wstring& p,bool isQt){ PluginRank.push_back(p); //std::swap(PluginRank.end()-2,PluginRank.end()-1); OnNewSentenceS[p]=plugin; - host->configs->pluginsadd(WideStringToString(p),isQt); + host->configs->pluginsadd({p,isQt}); } return true; }