Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache migration and other fixes #4240

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Cmdline/Action/Cache.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

using CommandLine;
Expand Down Expand Up @@ -177,7 +178,9 @@ private int SetCacheDirectory(SetOptions options)

if (manager != null)
{
if (manager.TrySetupCache(options.Path, out string? failReason))
if (manager.TrySetupCache(options.Path,
new Progress<int>(p => {}),
out string? failReason))
{
IConfiguration cfg = ServiceLocator.Container.Resolve<IConfiguration>();
user?.RaiseMessage(Properties.Resources.CacheSet, cfg.DownloadCacheDir ?? "");
Expand Down Expand Up @@ -205,7 +208,9 @@ private int ResetCacheDirectory()
{
if (manager != null)
{
if (manager.TrySetupCache("", out string? failReason))
if (manager.TrySetupCache("",
new Progress<int>(p => {}),
out string? failReason))
{
IConfiguration cfg = ServiceLocator.Container.Resolve<IConfiguration>();
user?.RaiseMessage(Properties.Resources.CacheReset, cfg.DownloadCacheDir ?? "");
Expand Down
9 changes: 8 additions & 1 deletion Core/CKANPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ public static string ToAbsolute(string path, string root)
return NormalizePath(Path.Combine(root, path));
}

public static void CheckFreeSpace(DirectoryInfo where, long bytesToStore, string errorDescription)
public static void CheckFreeSpace(DirectoryInfo where,
long bytesToStore,
string errorDescription)
{
if (bytesToStore > 0)
{
Expand All @@ -131,5 +133,10 @@ public static void CheckFreeSpace(DirectoryInfo where, long bytesToStore, string
}
}

public static bool PathEquals(this FileSystemInfo a,
FileSystemInfo b)
=> NormalizePath(a.FullName).Equals(NormalizePath(b.FullName),
Platform.PathComparison);

}
}
77 changes: 74 additions & 3 deletions Core/GameInstanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class GameInstanceManager : IDisposable
public GameInstance? CurrentInstance { get; set; }

public NetModuleCache? Cache { get; private set; }
public event Action<NetModuleCache>? CacheChanged;

public readonly SteamLibrary SteamLibrary = new SteamLibrary();

Expand Down Expand Up @@ -580,11 +581,14 @@ private void LoadCacheSettings()
}
}

if (!TrySetupCache(Configuration.DownloadCacheDir, out string? failReason))
var progress = new Progress<int>(p => {});
if (!TrySetupCache(Configuration.DownloadCacheDir,
progress,
out string? failReason))
{
log.ErrorFormat("Cache not found at configured path {0}: {1}", Configuration.DownloadCacheDir, failReason);
// Fall back to default path to minimize chance of ending up in an invalid state at startup
TrySetupCache("", out _);
TrySetupCache("", progress, out _);
}
}

Expand All @@ -597,9 +601,11 @@ private void LoadCacheSettings()
/// true if successful, false otherwise
/// </returns>
public bool TrySetupCache(string? path,
IProgress<int> progress,
[NotNullWhen(returnValue: false)] out string? failureReason)
{
var origPath = Configuration.DownloadCacheDir;
var origPath = Configuration.DownloadCacheDir;
var origCache = Cache;
try
{
if (path == null || string.IsNullOrEmpty(path))
Expand All @@ -614,17 +620,81 @@ public bool TrySetupCache(string? path,
Cache = new NetModuleCache(this, path);
Configuration.DownloadCacheDir = path;
}
if (origPath != null && origCache != null)
{
origCache.GetSizeInfo(out _, out long oldNumBytes, out _);
Cache.GetSizeInfo(out _, out _, out long bytesFree);

if (oldNumBytes > 0)
{
switch (User.RaiseSelectionDialog(
string.Format(Properties.Resources.GameInstanceManagerCacheMigrationPrompt,
CkanModule.FmtSize(oldNumBytes),
CkanModule.FmtSize(bytesFree)),
oldNumBytes < bytesFree ? 0 : 2,
Properties.Resources.GameInstanceManagerCacheMigrationMove,
Properties.Resources.GameInstanceManagerCacheMigrationDelete,
Properties.Resources.GameInstanceManagerCacheMigrationOpen,
Properties.Resources.GameInstanceManagerCacheMigrationNothing,
Properties.Resources.GameInstanceManagerCacheMigrationRevert))
{
case 0:
if (oldNumBytes < bytesFree)
{
Cache.MoveFrom(new DirectoryInfo(origPath), progress);
CacheChanged?.Invoke(origCache);
}
else
{
User.RaiseError(Properties.Resources.GameInstanceManagerCacheMigrationNotEnoughFreeSpace);
// Abort since the user picked an option that doesn't work
Cache = origCache;
Configuration.DownloadCacheDir = origPath;
failureReason = "";
}
break;

case 1:
origCache.RemoveAll();
CacheChanged?.Invoke(origCache);
break;

case 2:
Utilities.OpenFileBrowser(origPath);
Utilities.OpenFileBrowser(Configuration.DownloadCacheDir);
CacheChanged?.Invoke(origCache);
break;

case 3:
CacheChanged?.Invoke(origCache);
break;

case -1:
case 4:
Cache = origCache;
Configuration.DownloadCacheDir = origPath;
failureReason = "";
return false;
}
}
else
{
CacheChanged?.Invoke(origCache);
}
}
failureReason = null;
return true;
}
catch (DirectoryNotFoundKraken)
{
Cache = origCache;
Configuration.DownloadCacheDir = origPath;
failureReason = string.Format(Properties.Resources.GameInstancePathNotFound, path);
return false;
}
catch (Exception ex)
{
Cache = origCache;
Configuration.DownloadCacheDir = origPath;
failureReason = ex.Message;
return false;
Expand All @@ -647,6 +717,7 @@ public void Dispose()
}

// Attempting to dispose of the related RegistryManager object here is a bad idea, it cause loads of failures
GC.SuppressFinalize(this);
}

public static bool IsGameInstanceDir(DirectoryInfo path)
Expand Down
6 changes: 2 additions & 4 deletions Core/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,9 +1210,8 @@ public void Upgrade(ICollection<CkanModule> modules,
if (installed_mod == null)
{
if (!Cache.IsMaybeCachedZip(module)
&& Cache.GetInProgressFileName(module) is string p)
&& Cache.GetInProgressFileName(module) is FileInfo inProgressFile)
{
var inProgressFile = new FileInfo(p);
if (inProgressFile.Exists)
{
User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingResuming,
Expand Down Expand Up @@ -1253,9 +1252,8 @@ public void Upgrade(ICollection<CkanModule> modules,
else
{
if (!Cache.IsMaybeCachedZip(module)
&& Cache.GetInProgressFileName(module) is string p)
&& Cache.GetInProgressFileName(module) is FileInfo inProgressFile)
{
var inProgressFile = new FileInfo(p);
if (inProgressFile.Exists)
{
User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingResuming,
Expand Down
2 changes: 1 addition & 1 deletion Core/Net/NetAsyncModulesDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private NetAsyncDownloader.DownloadTargetFile TargetFromModuleGroup(
.OrderBy(u => u,
new PreferredHostUriComparer(preferredHosts))
.ToList(),
cache.GetInProgressFileName(first),
cache.GetInProgressFileName(first)?.FullName,
first.download_size,
string.IsNullOrEmpty(first.download_content_type)
? defaultMimeType
Expand Down
Loading