Skip to content

Commit cd7f10b

Browse files
authored
全局配置文件存储在版本号子文件夹中 (#872)
* feat: 全局配置文件存储在版本号子文件夹中 * chore: 删除无用的翻译资源 * fix: 修复一个读取旧配置的错误
1 parent 2923586 commit cd7f10b

20 files changed

+80
-351
lines changed

src/Magpie.App/AppSettings.cpp

+76-68
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using namespace ::Magpie::Core;
2121

2222
namespace winrt::Magpie::App {
2323

24-
static constexpr uint32_t SETTINGS_VERSION = 2;
24+
static constexpr uint32_t CONFIG_VERSION = 2;
2525

2626
_AppSettingsData::_AppSettingsData() {}
2727

@@ -213,13 +213,15 @@ bool AppSettings::Initialize() noexcept {
213213
Logger& logger = Logger::Get();
214214

215215
// 若程序所在目录存在配置文件则为便携模式
216-
_isPortableMode = Win32Utils::FileExists(
217-
StrUtils::Concat(CommonSharedConstants::CONFIG_DIR, CommonSharedConstants::CONFIG_NAME).c_str());
218-
_UpdateConfigPath();
216+
_isPortableMode = Win32Utils::FileExists(StrUtils::Concat(
217+
CommonSharedConstants::CONFIG_DIR, CommonSharedConstants::CONFIG_FILENAME).c_str());
218+
219+
std::wstring existingConfigPath;
220+
_UpdateConfigPath(&existingConfigPath);
219221

220222
logger.Info(StrUtils::Concat("便携模式:", _isPortableMode ? "" : ""));
221223

222-
if (!Win32Utils::FileExists(_configPath.c_str())) {
224+
if (existingConfigPath.empty()) {
223225
logger.Info("不存在配置文件");
224226
_SetDefaultScalingModes();
225227
_SetDefaultShortcuts();
@@ -230,13 +232,14 @@ bool AppSettings::Initialize() noexcept {
230232
// 此时 ResourceLoader 使用“首选语言”
231233

232234
std::string configText;
233-
if (!Win32Utils::ReadTextFile(_configPath.c_str(), configText)) {
235+
if (!Win32Utils::ReadTextFile(existingConfigPath.c_str(), configText)) {
234236
logger.Error("读取配置文件失败");
235237
ResourceLoader resourceLoader =
236238
ResourceLoader::GetForCurrentView(CommonSharedConstants::APP_RESOURCE_MAP_ID);
237239
hstring title = resourceLoader.GetString(L"AppSettings_ErrorDialog_ReadFailed");
238240
hstring content = resourceLoader.GetString(L"AppSettings_ErrorDialog_ConfigLocation");
239-
ShowErrorMessage(title.c_str(), fmt::format(fmt::runtime(std::wstring_view(content)), _configPath).c_str());
241+
ShowErrorMessage(title.c_str(),
242+
fmt::format(fmt::runtime(std::wstring_view(content)), existingConfigPath).c_str());
240243
return false;
241244
}
242245

@@ -256,7 +259,8 @@ bool AppSettings::Initialize() noexcept {
256259
ResourceLoader::GetForCurrentView(CommonSharedConstants::APP_RESOURCE_MAP_ID);
257260
hstring title = resourceLoader.GetString(L"AppSettings_ErrorDialog_NotValidJson");
258261
hstring content = resourceLoader.GetString(L"AppSettings_ErrorDialog_ConfigLocation");
259-
ShowErrorMessage(title.c_str(), fmt::format(fmt::runtime(std::wstring_view(content)), _configPath).c_str());
262+
ShowErrorMessage(title.c_str(),
263+
fmt::format(fmt::runtime(std::wstring_view(content)), existingConfigPath).c_str());
260264
return false;
261265
}
262266

@@ -266,53 +270,14 @@ bool AppSettings::Initialize() noexcept {
266270
ResourceLoader::GetForCurrentView(CommonSharedConstants::APP_RESOURCE_MAP_ID);
267271
hstring title = resourceLoader.GetString(L"AppSettings_ErrorDialog_ParseFailed");
268272
hstring content = resourceLoader.GetString(L"AppSettings_ErrorDialog_ConfigLocation");
269-
ShowErrorMessage(title.c_str(), fmt::format(fmt::runtime(std::wstring_view(content)), _configPath).c_str());
273+
ShowErrorMessage(title.c_str(),
274+
fmt::format(fmt::runtime(std::wstring_view(content)), existingConfigPath).c_str());
270275
return false;
271276
}
272277

273278
auto root = ((const rapidjson::Document&)doc).GetObj();
274279

275-
uint32_t settingsVersion = 0;
276-
// 不存在 version 字段则视为 0
277-
JsonHelper::ReadUInt(root, "version", settingsVersion);
278-
279-
if (settingsVersion > SETTINGS_VERSION) {
280-
Logger::Get().Warn("未知的配置文件版本");
281-
282-
ResourceLoader resourceLoader =
283-
ResourceLoader::GetForCurrentView(CommonSharedConstants::APP_RESOURCE_MAP_ID);
284-
if (_isPortableMode) {
285-
hstring contentStr = resourceLoader.GetString(
286-
L"AppSettings_PortableModeUnkownConfiguration_Content");
287-
hstring continueStr = resourceLoader.GetString(
288-
L"AppSettings_PortableModeUnkownConfiguration_Continue");
289-
hstring exitStr = resourceLoader.GetString(
290-
L"AppSettings_PortableModeUnkownConfiguration_Exit");
291-
if (!ShowOkCancelWarningMessage(nullptr,
292-
contentStr.c_str(), continueStr.c_str(), exitStr.c_str())
293-
) {
294-
return false;
295-
}
296-
} else {
297-
hstring contentStr = resourceLoader.GetString(
298-
L"AppSettings_UnkownConfiguration_Content");
299-
hstring continueStr = resourceLoader.GetString(
300-
L"AppSettings_UnkownConfiguration_Continue");
301-
hstring enablePortableModeStr = resourceLoader.GetString(
302-
L"AppSettings_UnkownConfiguration_EnablePortableMode");
303-
if (!ShowOkCancelWarningMessage(nullptr,
304-
contentStr.c_str(), continueStr.c_str(), enablePortableModeStr.c_str())
305-
) {
306-
IsPortableMode(true);
307-
_SetDefaultScalingModes();
308-
_SetDefaultShortcuts();
309-
SaveAsync();
310-
return true;
311-
}
312-
}
313-
}
314-
315-
_LoadSettings(root, settingsVersion);
280+
_LoadSettings(root);
316281

317282
if (_SetDefaultShortcuts()) {
318283
SaveAsync();
@@ -343,7 +308,7 @@ void AppSettings::IsPortableMode(bool value) noexcept {
343308
if (!value) {
344309
// 关闭便携模式需删除本地配置文件
345310
// 不关心是否成功
346-
DeleteFile(StrUtils::Concat(_configDir, CommonSharedConstants::CONFIG_NAME).c_str());
311+
DeleteFile(StrUtils::Concat(_configDir, CommonSharedConstants::CONFIG_FILENAME).c_str());
347312
}
348313

349314
Logger::Get().Info(value ? "已开启便携模式" : "已关闭便携模式");
@@ -486,9 +451,6 @@ bool AppSettings::_Save(const _AppSettingsData& data) noexcept {
486451
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(json);
487452
writer.StartObject();
488453

489-
writer.Key("version");
490-
writer.Uint(SETTINGS_VERSION);
491-
492454
writer.Key("language");
493455
if (_language < 0) {
494456
writer.String("");
@@ -582,7 +544,7 @@ bool AppSettings::_Save(const _AppSettingsData& data) noexcept {
582544
}
583545

584546
// 永远不会失败,遇到不合法的配置项时静默忽略
585-
void AppSettings::_LoadSettings(const rapidjson::GenericObject<true, rapidjson::Value>& root, uint32_t /*version*/) noexcept {
547+
void AppSettings::_LoadSettings(const rapidjson::GenericObject<true, rapidjson::Value>& root) noexcept {
586548
{
587549
std::wstring language;
588550
JsonHelper::ReadString(root, "language", language);
@@ -697,7 +659,7 @@ void AppSettings::_LoadSettings(const rapidjson::GenericObject<true, rapidjson::
697659
// v0.10.0-preview1 使用 alwaysRunAsElevated
698660
JsonHelper::ReadBool(root, "alwaysRunAsElevated", _isAlwaysRunAsAdmin);
699661
}
700-
if (!JsonHelper::ReadBool(root, "showNotifyIcon", _isShowNotifyIcon)) {
662+
if (!JsonHelper::ReadBool(root, "showNotifyIcon", _isShowNotifyIcon, true)) {
701663
// v0.10 使用 showTrayIcon
702664
JsonHelper::ReadBool(root, "showTrayIcon", _isShowNotifyIcon);
703665
}
@@ -991,33 +953,79 @@ void AppSettings::_SetDefaultScalingModes() noexcept {
991953
_defaultProfile.scalingMode = 0;
992954
}
993955

994-
void AppSettings::_UpdateConfigPath() noexcept {
956+
static std::wstring FindOldConfig(const wchar_t* localAppDataDir) noexcept {
957+
for (uint32_t version = CONFIG_VERSION - 1; version >= 2; --version) {
958+
std::wstring oldConfigPath = fmt::format(
959+
L"{}\\Magpie\\{}v{}\\{}",
960+
localAppDataDir,
961+
CommonSharedConstants::CONFIG_DIR,
962+
version,
963+
CommonSharedConstants::CONFIG_FILENAME
964+
);
965+
966+
if (Win32Utils::FileExists(oldConfigPath.c_str())) {
967+
return oldConfigPath;
968+
}
969+
}
970+
971+
// v1 版本的配置文件不在子目录中
972+
std::wstring v1ConfigPath = StrUtils::Concat(
973+
localAppDataDir,
974+
L"\\Magpie\\",
975+
CommonSharedConstants::CONFIG_DIR,
976+
CommonSharedConstants::CONFIG_FILENAME
977+
);
978+
979+
if (Win32Utils::FileExists(v1ConfigPath.c_str())) {
980+
return v1ConfigPath;
981+
}
982+
983+
return {};
984+
}
985+
986+
void AppSettings::_UpdateConfigPath(std::wstring* existingConfigPath) noexcept {
995987
if (_isPortableMode) {
996988
wchar_t curDir[MAX_PATH];
997989
GetCurrentDirectory(MAX_PATH, curDir);
998990

999-
_configDir = curDir;
1000-
if (_configDir.back() != L'\\') {
1001-
_configDir.push_back(L'\\');
991+
_configDir = StrUtils::Concat(curDir, L"\\", CommonSharedConstants::CONFIG_DIR);
992+
_configPath = _configDir + CommonSharedConstants::CONFIG_FILENAME;
993+
994+
if (existingConfigPath) {
995+
if (Win32Utils::FileExists(_configPath.c_str())) {
996+
*existingConfigPath = _configPath;
997+
}
1002998
}
1003-
_configDir += CommonSharedConstants::CONFIG_DIR;
1004999
} else {
10051000
wchar_t localAppDataDir[MAX_PATH];
10061001
HRESULT hr = SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, localAppDataDir);
10071002
if (SUCCEEDED(hr)) {
1008-
_configDir = StrUtils::Concat(
1009-
localAppDataDir,
1010-
localAppDataDir[StrUtils::StrLen(localAppDataDir) - 1] == L'\\' ? L"Magpie\\" : L"\\Magpie\\",
1011-
CommonSharedConstants::CONFIG_DIR
1012-
);
1003+
_configDir = fmt::format(L"{}\\Magpie\\{}v{}\\",
1004+
localAppDataDir, CommonSharedConstants::CONFIG_DIR, CONFIG_VERSION);
1005+
_configPath = _configDir + CommonSharedConstants::CONFIG_FILENAME;
1006+
1007+
if (existingConfigPath) {
1008+
if (Win32Utils::FileExists(_configPath.c_str())) {
1009+
*existingConfigPath = _configPath;
1010+
} else {
1011+
// 查找旧版本配置文件
1012+
*existingConfigPath = FindOldConfig(localAppDataDir);
1013+
}
1014+
}
10131015
} else {
10141016
Logger::Get().ComError("SHGetFolderPath 失败", hr);
1017+
10151018
_configDir = CommonSharedConstants::CONFIG_DIR;
1019+
_configPath = _configDir + CommonSharedConstants::CONFIG_FILENAME;
1020+
1021+
if (existingConfigPath) {
1022+
if (Win32Utils::FileExists(_configPath.c_str())) {
1023+
*existingConfigPath = _configPath;
1024+
}
1025+
}
10161026
}
10171027
}
10181028

1019-
_configPath = _configDir + CommonSharedConstants::CONFIG_NAME;
1020-
10211029
// 确保 ConfigDir 存在
10221030
Win32Utils::CreateDir(_configDir.c_str(), true);
10231031
}

src/Magpie.App/AppSettings.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ class AppSettings : private _AppSettingsData {
382382
void _UpdateWindowPlacement() noexcept;
383383
bool _Save(const _AppSettingsData& data) noexcept;
384384

385-
void _LoadSettings(const rapidjson::GenericObject<true, rapidjson::Value>& root, uint32_t version) noexcept;
385+
void _LoadSettings(const rapidjson::GenericObject<true, rapidjson::Value>& root) noexcept;
386386
bool _LoadProfile(
387387
const rapidjson::GenericObject<true, rapidjson::Value>& profileObj,
388388
Profile& profile,
@@ -391,7 +391,7 @@ class AppSettings : private _AppSettingsData {
391391
bool _SetDefaultShortcuts() noexcept;
392392
void _SetDefaultScalingModes() noexcept;
393393

394-
void _UpdateConfigPath() noexcept;
394+
void _UpdateConfigPath(std::wstring* existingConfigPath = nullptr) noexcept;
395395

396396
// 用于同步保存
397397
Win32Utils::SRWMutex _saveMutex;

src/Magpie.App/Resources.language-de.resw

-18
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,6 @@
268268
<data name="AppSettings_Dialog_Warning" xml:space="preserve">
269269
<value>Warnung</value>
270270
</data>
271-
<data name="AppSettings_UnkownConfiguration_Continue" xml:space="preserve">
272-
<value>Fortfahren</value>
273-
</data>
274271
<data name="About_Version_UpdateCard_ReleaseNotes.Content" xml:space="preserve">
275272
<value>Versionshinweise</value>
276273
</data>
@@ -283,18 +280,6 @@
283280
<data name="About_DeveloperModeEnabled" xml:space="preserve">
284281
<value>Entwicklermodus ist aktiviert.</value>
285282
</data>
286-
<data name="AppSettings_PortableModeUnkownConfiguration_Content" xml:space="preserve">
287-
<value>Die lokale Konfigurationsdatei stammt von einer unbekannten Version und wird möglicherweise nicht korrekt geparst.</value>
288-
</data>
289-
<data name="AppSettings_PortableModeUnkownConfiguration_Continue" xml:space="preserve">
290-
<value>Fortfahren</value>
291-
</data>
292-
<data name="AppSettings_PortableModeUnkownConfiguration_Exit" xml:space="preserve">
293-
<value>Schließen</value>
294-
</data>
295-
<data name="AppSettings_UnkownConfiguration_Content" xml:space="preserve">
296-
<value>Die globale Konfigurationsdatei stammt von einer unbekannten Version und wird möglicherweise nicht korrekt geparst.</value>
297-
</data>
298283
<data name="Profile_Cursor_DrawCursor_ScalingFactor.Header" xml:space="preserve">
299284
<value>Skalierungsfaktor</value>
300285
</data>
@@ -544,9 +529,6 @@
544529
<data name="Profile_Cursor_DrawCursor_ScalingFactor_Custom.Content" xml:space="preserve">
545530
<value>Benutzerdefiniert</value>
546531
</data>
547-
<data name="AppSettings_UnkownConfiguration_EnablePortableMode" xml:space="preserve">
548-
<value>Portable mode Aktivieren</value>
549-
</data>
550532
<data name="Profile_SourceWindow_CaptureTitleBar.Description" xml:space="preserve">
551533
<value>Beschränkt auf Grafikerfassung und Desktopduplikation</value>
552534
</data>

src/Magpie.App/Resources.language-en-US.resw

-18
Original file line numberDiff line numberDiff line change
@@ -669,24 +669,6 @@
669669
<data name="AppSettings_Dialog_Warning" xml:space="preserve">
670670
<value>Warning</value>
671671
</data>
672-
<data name="AppSettings_PortableModeUnkownConfiguration_Content" xml:space="preserve">
673-
<value>The local configuration file comes from an unknown version and may not be parsed correctly.</value>
674-
</data>
675-
<data name="AppSettings_PortableModeUnkownConfiguration_Continue" xml:space="preserve">
676-
<value>Continue</value>
677-
</data>
678-
<data name="AppSettings_PortableModeUnkownConfiguration_Exit" xml:space="preserve">
679-
<value>Exit</value>
680-
</data>
681-
<data name="AppSettings_UnkownConfiguration_Content" xml:space="preserve">
682-
<value>The global configuration file comes from an unknown version and may not be parsed correctly.</value>
683-
</data>
684-
<data name="AppSettings_UnkownConfiguration_Continue" xml:space="preserve">
685-
<value>Continue</value>
686-
</data>
687-
<data name="AppSettings_UnkownConfiguration_EnablePortableMode" xml:space="preserve">
688-
<value>Enable portable mode</value>
689-
</data>
690672
<data name="Settings_DeveloperOptions.Description" xml:space="preserve">
691673
<value>These settings are for development use only</value>
692674
</data>

src/Magpie.App/Resources.language-es.resw

-18
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,6 @@
495495
<data name="AppSettings_Dialog_Warning" xml:space="preserve">
496496
<value>Advertencia</value>
497497
</data>
498-
<data name="AppSettings_PortableModeUnkownConfiguration_Continue" xml:space="preserve">
499-
<value>Continuar</value>
500-
</data>
501-
<data name="AppSettings_PortableModeUnkownConfiguration_Exit" xml:space="preserve">
502-
<value>Cerrar</value>
503-
</data>
504-
<data name="AppSettings_UnkownConfiguration_EnablePortableMode" xml:space="preserve">
505-
<value>Habilitar el modo portable</value>
506-
</data>
507498
<data name="Settings_DeveloperOptions_DebugMode.Content" xml:space="preserve">
508499
<value>Modo de depuración</value>
509500
</data>
@@ -693,9 +684,6 @@
693684
<data name="Profile_SourceWindow_CaptureTitleBar.Header" xml:space="preserve">
694685
<value>Capturar barra de título</value>
695686
</data>
696-
<data name="AppSettings_UnkownConfiguration_Content" xml:space="preserve">
697-
<value>El archivo de configuración global proviene de una versión desconocida y es posible que no se analice correctamente.</value>
698-
</data>
699687
<data name="About_Feedback_Discussion.Header" xml:space="preserve">
700688
<value>Discusiones</value>
701689
</data>
@@ -729,15 +717,9 @@
729717
<data name="Settings_Launch_RunAtStartup_MinimizeAtStartup.Header" xml:space="preserve">
730718
<value>Minimizar a la bandeja del sistema al inicio</value>
731719
</data>
732-
<data name="AppSettings_PortableModeUnkownConfiguration_Content" xml:space="preserve">
733-
<value>El archivo de configuración local proviene de una versión desconocida y es posible que no se analice correctamente.</value>
734-
</data>
735720
<data name="Settings_DeveloperOptions.Description" xml:space="preserve">
736721
<value>Esta configuración es solo para uso de desarrollo</value>
737722
</data>
738-
<data name="AppSettings_UnkownConfiguration_Continue" xml:space="preserve">
739-
<value>Continuar</value>
740-
</data>
741723
<data name="Settings_DeveloperOptions.Header" xml:space="preserve">
742724
<value>Opciones de desarrollador</value>
743725
</data>

0 commit comments

Comments
 (0)