diff --git a/MareSynchronos/FileCache/CacheMonitor.cs b/MareSynchronos/FileCache/CacheMonitor.cs index 614b253..96314e3 100644 --- a/MareSynchronos/FileCache/CacheMonitor.cs +++ b/MareSynchronos/FileCache/CacheMonitor.cs @@ -406,6 +406,30 @@ public sealed class CacheMonitor : DisposableMediatorSubscriberBase SubstWatcher.EnableRaisingEvents = true; } + public void DeleteSubstOriginals() + { + var cacheDir = _configService.Current.CacheFolder; + var substDir = _fileDbManager.SubstFolder; + var allSubstFiles = Directory.GetFiles(substDir, "*.*", SearchOption.TopDirectoryOnly) + .Where(f => + { + var val = f.Split('\\')[^1]; + return val.Length == 40 || (val.Split('.').FirstOrDefault()?.Length ?? 0) == 40 + || val.EndsWith(".tmp", StringComparison.OrdinalIgnoreCase); + }); + + foreach (var substFile in allSubstFiles) + { + var cacheFile = Path.Join(cacheDir, Path.GetFileName(substFile)); + try + { + if (File.Exists(cacheFile)) + File.Delete(cacheFile); + } + catch { } + } + } + private void HandleChanges(Dictionary changes) { lock (_fileDbManager) diff --git a/MareSynchronos/MareConfiguration/Configurations/PlayerPerformanceConfig.cs b/MareSynchronos/MareConfiguration/Configurations/PlayerPerformanceConfig.cs index 0c2a64e..c8a14f5 100644 --- a/MareSynchronos/MareConfiguration/Configurations/PlayerPerformanceConfig.cs +++ b/MareSynchronos/MareConfiguration/Configurations/PlayerPerformanceConfig.cs @@ -12,4 +12,5 @@ public class PlayerPerformanceConfig : IMareConfiguration public int TrisAutoPauseThresholdThousands { get; set; } = 375; public bool IgnoreDirectPairs { get; set; } = true; public TextureShrinkMode TextureShrinkMode { get; set; } = TextureShrinkMode.Default; + public bool TextureShrinkDeleteOriginal { get; set; } = false; } \ No newline at end of file diff --git a/MareSynchronos/Services/PlayerPerformanceService.cs b/MareSynchronos/Services/PlayerPerformanceService.cs index a4f1180..09b8bb2 100644 --- a/MareSynchronos/Services/PlayerPerformanceService.cs +++ b/MareSynchronos/Services/PlayerPerformanceService.cs @@ -303,6 +303,19 @@ public class PlayerPerformanceService : DisposableMediatorSubscriberBase File.Move(tmpFilePath, newFilePath); _fileCacheManager.CreateSubstEntry(newFilePath); shrunken = true; + + // Make sure its a cache file before trying to delete it !! + bool shouldDelete = fileEntry.IsCacheEntry && File.Exists(filePath); + + if (_playerPerformanceConfigService.Current.TextureShrinkDeleteOriginal && shouldDelete) + { + try + { + _logger.LogDebug("Deleting original texture: {filePath}", filePath); + File.Delete(filePath); + } + catch { } + } } catch (Exception e) { diff --git a/MareSynchronos/UI/SettingsUi.cs b/MareSynchronos/UI/SettingsUi.cs index 3c77eee..9494b9f 100644 --- a/MareSynchronos/UI/SettingsUi.cs +++ b/MareSynchronos/UI/SettingsUi.cs @@ -1245,15 +1245,20 @@ public class SettingsUi : WindowMediatorSubscriberBase _uiShared.BigText("Global Configuration"); bool alwaysShrinkTextures = _playerPerformanceConfigService.Current.TextureShrinkMode == TextureShrinkMode.Always; - if (ImGui.Checkbox("Shrink downloaded textures", ref alwaysShrinkTextures)) + bool deleteOriginalTextures = _playerPerformanceConfigService.Current.TextureShrinkDeleteOriginal; + + using (ImRaii.Disabled(deleteOriginalTextures)) { - if (alwaysShrinkTextures) - _playerPerformanceConfigService.Current.TextureShrinkMode = TextureShrinkMode.Always; - else - _playerPerformanceConfigService.Current.TextureShrinkMode = TextureShrinkMode.Never; - _playerPerformanceConfigService.Save(); - recalculatePerformance = true; - _cacheMonitor.ClearSubstStorage(); + if (ImGui.Checkbox("Shrink downloaded textures", ref alwaysShrinkTextures)) + { + if (alwaysShrinkTextures) + _playerPerformanceConfigService.Current.TextureShrinkMode = TextureShrinkMode.Always; + else + _playerPerformanceConfigService.Current.TextureShrinkMode = TextureShrinkMode.Never; + _playerPerformanceConfigService.Save(); + recalculatePerformance = true; + _cacheMonitor.ClearSubstStorage(); + } } _uiShared.DrawHelpText("Automatically shrinks texture resolution of synced players to reduce VRAM utilization." + UiSharedService.TooltipSeparator + "Texture Size Limit (DXT/BC5/BC7 Compressed): 2048x2048" + Environment.NewLine @@ -1261,6 +1266,23 @@ public class SettingsUi : WindowMediatorSubscriberBase + "Enable to reduce lag in large crowds." + Environment.NewLine + "Disable this for higher quality during GPose."); + using (ImRaii.Disabled(!alwaysShrinkTextures || _cacheMonitor.FileCacheSize < 0)) + { + using var indent = ImRaii.PushIndent(); + if (ImGui.Checkbox("Delete original textures from disk", ref deleteOriginalTextures)) + { + _playerPerformanceConfigService.Current.TextureShrinkDeleteOriginal = deleteOriginalTextures; + _playerPerformanceConfigService.Save(); + _ = Task.Run(() => + { + _cacheMonitor.DeleteSubstOriginals(); + _cacheMonitor.RecalculateFileCacheSize(CancellationToken.None); + }); + } + _uiShared.DrawHelpText("Deletes original, full-sized, textures from disk after downloading and shrinking." + UiSharedService.TooltipSeparator + + "Caution!!! This will cause a re-download of all textures when the shrink option is disabled."); + } + var totalVramBytes = _pairManager.GetOnlineUserPairs().Where(p => p.IsVisible && p.LastAppliedApproximateVRAMBytes > 0).Sum(p => p.LastAppliedApproximateVRAMBytes); ImGui.TextUnformatted("Current VRAM utilization by all nearby players:");