This commit is contained in:
恍兮惚兮 2024-03-04 20:33:11 +08:00
parent 3d05e89edd
commit a6600ac38a
3 changed files with 65 additions and 17 deletions

View File

@ -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<typename T>
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<std::wstring,bool> 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}
};
}

View File

@ -1,14 +1,24 @@
#ifndef LUNA_CONFIG_HELPER
#define LUNA_CONFIG_HELPER
#include<nlohmann/json.hpp>
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<class T>

View File

@ -17,7 +17,7 @@ std::optional<std::wstring>SelectFile(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::vector<std::wstring>collectQtplugs;
for (auto i=0;i<plgs.size();i++) {
bool isqt=plgs[i]["isQt"];
auto path=StringToWideString(plgs[i]["path"]);
for (auto i=0;i<host->configs->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;
}