diff options
Diffstat (limited to 'Ryujinx.Graphics.Gpu')
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Ryujinx.Graphics.Gpu.csproj | 6 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs | 24 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs | 38 | ||||
| -rw-r--r-- | Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs | 46 |
4 files changed, 77 insertions, 37 deletions
diff --git a/Ryujinx.Graphics.Gpu/Ryujinx.Graphics.Gpu.csproj b/Ryujinx.Graphics.Gpu/Ryujinx.Graphics.Gpu.csproj index 01e8e235..e3645668 100644 --- a/Ryujinx.Graphics.Gpu/Ryujinx.Graphics.Gpu.csproj +++ b/Ryujinx.Graphics.Gpu/Ryujinx.Graphics.Gpu.csproj @@ -1,7 +1,7 @@ <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> - <TargetFramework>net5.0</TargetFramework> + <TargetFramework>net6.0</TargetFramework> <AllowUnsafeBlocks>true</AllowUnsafeBlocks> </PropertyGroup> @@ -14,4 +14,8 @@ <ProjectReference Include="..\Ryujinx.Graphics.Shader\Ryujinx.Graphics.Shader.csproj" /> </ItemGroup> + <ItemGroup> + <PackageReference Include="SharpZipLib" Version="1.3.3" /> + </ItemGroup> + </Project> diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs index 316e027f..a98531f6 100644 --- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheCollection.cs @@ -1,11 +1,11 @@ -using Ryujinx.Common; +using ICSharpCode.SharpZipLib.Zip; +using Ryujinx.Common; using Ryujinx.Common.Logging; using Ryujinx.Graphics.Gpu.Shader.Cache.Definition; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.IO.Compression; using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -119,7 +119,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache /// <summary> /// Main storage of the cache collection. /// </summary> - private ZipArchive _cacheArchive; + private ZipFile _cacheArchive; /// <summary> /// Indicates if the cache collection supports modification. @@ -324,7 +324,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache EnsureArchiveUpToDate(); // Open the zip in readonly to avoid anyone modifying/corrupting it during normal operations. - _cacheArchive = ZipFile.Open(GetArchivePath(), ZipArchiveMode.Read); + _cacheArchive = new ZipFile(File.OpenRead(GetArchivePath())); } /// <summary> @@ -336,7 +336,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache // First close previous opened instance if found. if (_cacheArchive != null) { - _cacheArchive.Dispose(); + _cacheArchive.Close(); } string archivePath = GetArchivePath(); @@ -355,8 +355,18 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache return; } + if (!File.Exists(archivePath)) + { + using (ZipFile newZip = ZipFile.Create(archivePath)) + { + // Workaround for SharpZipLib issue #395 + newZip.BeginUpdate(); + newZip.CommitUpdate(); + } + } + // Open the zip in read/write. - _cacheArchive = ZipFile.Open(archivePath, ZipArchiveMode.Update); + _cacheArchive = new ZipFile(File.Open(archivePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)); Logger.Info?.Print(LogClass.Gpu, $"Updating cache collection archive {archivePath}..."); @@ -366,7 +376,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache CacheHelper.EnsureArchiveUpToDate(_cacheDirectory, _cacheArchive, _hashTable); // Close the instance to force a flush. - _cacheArchive.Dispose(); + _cacheArchive.Close(); _cacheArchive = null; string cacheTempDataPath = GetCacheTempDataPath(); diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs index 09107346..ee4e1265 100644 --- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheHelper.cs @@ -1,4 +1,5 @@ -using Ryujinx.Common; +using ICSharpCode.SharpZipLib.Zip; +using Ryujinx.Common; using Ryujinx.Common.Configuration; using Ryujinx.Common.Logging; using Ryujinx.Graphics.GAL; @@ -9,7 +10,6 @@ using Ryujinx.Graphics.Shader.Translation; using System; using System.Collections.Generic; using System.IO; -using System.IO.Compression; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -192,19 +192,19 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache /// <param name="entry">The given hash</param> /// <returns>The cached file if present or null</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static byte[] ReadFromArchive(ZipArchive archive, Hash128 entry) + public static byte[] ReadFromArchive(ZipFile archive, Hash128 entry) { if (archive != null) { - ZipArchiveEntry archiveEntry = archive.GetEntry($"{entry}"); + ZipEntry archiveEntry = archive.GetEntry($"{entry}"); if (archiveEntry != null) { try { - byte[] result = new byte[archiveEntry.Length]; + byte[] result = new byte[archiveEntry.Size]; - using (Stream archiveStream = archiveEntry.Open()) + using (Stream archiveStream = archive.GetInputStream(archiveEntry)) { archiveStream.Read(result); @@ -538,8 +538,12 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache /// <param name="archive">The archive to use</param> /// <param name="entries">The entries in the cache</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void EnsureArchiveUpToDate(string baseCacheDirectory, ZipArchive archive, HashSet<Hash128> entries) + public static void EnsureArchiveUpToDate(string baseCacheDirectory, ZipFile archive, HashSet<Hash128> entries) { + List<string> filesToDelete = new List<string>(); + + archive.BeginUpdate(); + foreach (Hash128 hash in entries) { string cacheTempFilePath = GenCacheTempFilePath(baseCacheDirectory, hash); @@ -548,15 +552,25 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache { string cacheHash = $"{hash}"; - ZipArchiveEntry entry = archive.GetEntry(cacheHash); - - entry?.Delete(); + ZipEntry entry = archive.GetEntry(cacheHash); - archive.CreateEntryFromFile(cacheTempFilePath, cacheHash); + if (entry != null) + { + archive.Delete(entry); + } - File.Delete(cacheTempFilePath); + // We enforce deflate compression here to avoid possible incompatibilities on older version of Ryujinx that use System.IO.Compression. + archive.Add(new StaticDiskDataSource(cacheTempFilePath), cacheHash, CompressionMethod.Deflated); + filesToDelete.Add(cacheTempFilePath); } } + + archive.CommitUpdate(); + + foreach (string filePath in filesToDelete) + { + File.Delete(filePath); + } } public static bool IsArchiveReadOnly(string archivePath) diff --git a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs index e726bc2c..5b4a1713 100644 --- a/Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs +++ b/Ryujinx.Graphics.Gpu/Shader/Cache/CacheMigration.cs @@ -1,11 +1,11 @@ -using Ryujinx.Common; +using ICSharpCode.SharpZipLib.Zip; +using Ryujinx.Common; using Ryujinx.Common.Logging; using Ryujinx.Graphics.GAL; using Ryujinx.Graphics.Gpu.Shader.Cache.Definition; using System; using System.Collections.Generic; using System.IO; -using System.IO.Compression; namespace Ryujinx.Graphics.Gpu.Shader.Cache { @@ -35,27 +35,36 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache return false; } + private class StreamZipEntryDataSource : IStaticDataSource + { + private readonly ZipFile Archive; + private readonly ZipEntry Entry; + public StreamZipEntryDataSource(ZipFile archive, ZipEntry entry) + { + Archive = archive; + Entry = entry; + } + + public Stream GetSource() + { + return Archive.GetInputStream(Entry); + } + } + /// <summary> /// Move a file with the name of a given hash to another in the cache archive. /// </summary> /// <param name="archive">The archive in use</param> /// <param name="oldKey">The old key</param> /// <param name="newKey">The new key</param> - private static void MoveEntry(ZipArchive archive, Hash128 oldKey, Hash128 newKey) + private static void MoveEntry(ZipFile archive, Hash128 oldKey, Hash128 newKey) { - ZipArchiveEntry oldGuestEntry = archive.GetEntry($"{oldKey}"); + ZipEntry oldGuestEntry = archive.GetEntry($"{oldKey}"); if (oldGuestEntry != null) { - ZipArchiveEntry newGuestEntry = archive.CreateEntry($"{newKey}"); - - using (Stream oldStream = oldGuestEntry.Open()) - using (Stream newStream = newGuestEntry.Open()) - { - oldStream.CopyTo(newStream); - } - - oldGuestEntry.Delete(); + archive.Add(new StreamZipEntryDataSource(archive, oldGuestEntry), $"{newKey}", CompressionMethod.Deflated); + archive.Delete(oldGuestEntry); } } @@ -81,8 +90,8 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache string guestArchivePath = CacheHelper.GetArchivePath(guestBaseCacheDirectory); string hostArchivePath = CacheHelper.GetArchivePath(hostBaseCacheDirectory); - ZipArchive guestArchive = ZipFile.Open(guestArchivePath, ZipArchiveMode.Update); - ZipArchive hostArchive = ZipFile.Open(hostArchivePath, ZipArchiveMode.Update); + ZipFile guestArchive = new ZipFile(File.Open(guestArchivePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)); + ZipFile hostArchive = new ZipFile(File.Open(hostArchivePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)); CacheHelper.EnsureArchiveUpToDate(guestBaseCacheDirectory, guestArchive, guestEntries); CacheHelper.EnsureArchiveUpToDate(hostBaseCacheDirectory, hostArchive, hostEntries); @@ -129,8 +138,11 @@ namespace Ryujinx.Graphics.Gpu.Shader.Cache File.WriteAllBytes(guestManifestPath, newGuestManifestContent); File.WriteAllBytes(hostManifestPath, newHostManifestContent); - guestArchive.Dispose(); - hostArchive.Dispose(); + guestArchive.CommitUpdate(); + hostArchive.CommitUpdate(); + + guestArchive.Close(); + hostArchive.Close(); } } |
