backup&restore game config.ini

This commit is contained in:
Lightczx
2024-04-02 16:55:53 +08:00
parent a91499171d
commit 0b71053bf9
8 changed files with 66 additions and 6 deletions

View File

@@ -16,11 +16,15 @@ internal static class RuntimeOptionsExtension
public static string GetDataFolderServerCacheFolder(this RuntimeOptions options)
{
return Path.Combine(options.DataFolder, "ServerCache");
string directory = Path.Combine(options.DataFolder, "ServerCache");
Directory.CreateDirectory(directory);
return directory;
}
public static string GetDataFolderBackgroundFolder(this RuntimeOptions options)
{
return Path.Combine(options.DataFolder, "Background");
string directory = Path.Combine(options.DataFolder, "Background");
Directory.CreateDirectory(directory);
return directory;
}
}

View File

@@ -88,7 +88,6 @@ internal sealed partial class BackgroundImageService : IBackgroundImageService
if (currentBackgroundPathSet is not { Count: > 0 })
{
string backgroundFolder = runtimeOptions.GetDataFolderBackgroundFolder();
Directory.CreateDirectory(backgroundFolder);
currentBackgroundPathSet = Directory
.GetFiles(backgroundFolder, "*.*", SearchOption.AllDirectories)

View File

@@ -11,6 +11,7 @@ namespace Snap.Hutao.Service.Game.Configuration;
[Injection(InjectAs.Singleton, typeof(IGameChannelOptionsService))]
internal sealed partial class GameChannelOptionsService : IGameChannelOptionsService
{
private readonly IGameConfigurationFileService gameConfigurationFileService;
private readonly LaunchOptions launchOptions;
public ChannelOptions GetChannelOptions()
@@ -22,6 +23,13 @@ internal sealed partial class GameChannelOptionsService : IGameChannelOptionsSer
bool isOversea = LaunchScheme.ExecutableIsOversea(gameFileSystem.GameFileName);
if (!File.Exists(gameFileSystem.GameConfigFilePath))
{
// Try restore the configuration file if it does not exist
// The configuration file may be deleted by a incompatible launcher
gameConfigurationFileService.Restore(gameFileSystem.GameConfigFilePath);
}
if (!File.Exists(gameFileSystem.GameConfigFilePath))
{
return ChannelOptions.ConfigurationFileNotFound(gameFileSystem.GameConfigFilePath);

View File

@@ -0,0 +1,34 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Core;
using System.IO;
namespace Snap.Hutao.Service.Game.Configuration;
[ConstructorGenerated]
[Injection(InjectAs.Singleton, typeof(IGameConfigurationFileService))]
internal sealed partial class GameConfigurationFileService : IGameConfigurationFileService
{
private const string ConfigurationFileName = "config.ini";
private readonly RuntimeOptions runtimeOptions;
public void Backup(string source)
{
if (File.Exists(source))
{
string serverCacheFolder = runtimeOptions.GetDataFolderServerCacheFolder();
File.Copy(source, Path.Combine(serverCacheFolder, ConfigurationFileName), true);
}
}
public void Restore(string destination)
{
string serverCacheFolder = runtimeOptions.GetDataFolderServerCacheFolder();
string source = Path.Combine(serverCacheFolder, ConfigurationFileName);
if (File.Exists(source))
{
File.Copy(source, destination, true);
}
}
}

View File

@@ -0,0 +1,11 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Service.Game.Configuration;
internal interface IGameConfigurationFileService
{
void Backup(string source);
void Restore(string destination);
}

View File

@@ -7,6 +7,7 @@ using Snap.Hutao.Core.Setting;
using Snap.Hutao.Factory.ContentDialog;
using Snap.Hutao.Factory.Progress;
using Snap.Hutao.Model.Intrinsic;
using Snap.Hutao.Service.Game.Configuration;
using Snap.Hutao.Service.Game.Package;
using Snap.Hutao.View.Dialog;
using Snap.Hutao.Web.Hoyolab.SdkStatic.Hk4e.Launcher;
@@ -42,6 +43,9 @@ internal sealed class LaunchExecutionEnsureGameResourceHandler : ILaunchExecutio
return;
}
// Backup config file, in order to prevent a incompatible launcher to delete it.
context.ServiceProvider.GetRequiredService<IGameConfigurationFileService>().Backup(gameFileSystem.GameConfigFilePath);
await context.TaskContext.SwitchToMainThreadAsync();
context.UpdateGamePathEntry();
}

View File

@@ -59,7 +59,7 @@ internal sealed partial class PackageConverter
string scatteredFilesUrl = gameResource.Game.Latest.DecompressedPath;
string pkgVersionUrl = $"{scatteredFilesUrl}/{PackageVersion}";
PackageConverterFileSystemContext context = new(targetScheme.IsOversea, runtimeOptions.DataFolder, gameFolder, scatteredFilesUrl);
PackageConverterFileSystemContext context = new(targetScheme.IsOversea, runtimeOptions.GetDataFolderServerCacheFolder(), gameFolder, scatteredFilesUrl);
// Step 1
progress.Report(new(SH.ServiceGamePackageRequestPackageVerion));

View File

@@ -22,10 +22,10 @@ internal readonly struct PackageConverterFileSystemContext
public readonly string ScatteredFilesUrl;
public readonly string PkgVersionUrl;
public PackageConverterFileSystemContext(bool isTargetOversea, string dataFolder, string gameFolder, string scatteredFilesUrl)
public PackageConverterFileSystemContext(bool isTargetOversea, string serverCacheFolder, string gameFolder, string scatteredFilesUrl)
{
GameFolder = gameFolder;
ServerCacheFolder = Path.Combine(dataFolder, "ServerCache");
ServerCacheFolder = serverCacheFolder;
string serverCacheOversea = Path.Combine(ServerCacheFolder, "Oversea");
string serverCacheChinese = Path.Combine(ServerCacheFolder, "Chinese");