From f315cee9af6c886b6eca4c61dfe1ae7a816bd5cd Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 15 Jan 2022 13:50:15 -0700 Subject: [PATCH] Remove a workaround for CS8350 by using fixed arrays --- src/LibHac/Common/FixedArrays/Array27.cs | 33 ++++++++ src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs | 15 ++-- .../FsCreator/SaveDataFileSystemCreator.cs | 10 +-- src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs | 13 +--- .../FsSrv/SaveDataFileSystemServiceImpl.cs | 75 +++++-------------- src/LibHac/FsSrv/SaveDataSharedFileStorage.cs | 10 +-- 6 files changed, 70 insertions(+), 86 deletions(-) create mode 100644 src/LibHac/Common/FixedArrays/Array27.cs diff --git a/src/LibHac/Common/FixedArrays/Array27.cs b/src/LibHac/Common/FixedArrays/Array27.cs new file mode 100644 index 00000000..3498a736 --- /dev/null +++ b/src/LibHac/Common/FixedArrays/Array27.cs @@ -0,0 +1,33 @@ +#pragma warning disable CS0169, CS0649, IDE0051 // Field is never used, Field is never assigned to, Remove unused private members +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace LibHac.Common.FixedArrays; + +public struct Array27 +{ + public const int Length = 27; + + private Array16 _0; + private Array8 _16; + private Array2 _24; + private T _26; + + public ref T this[int i] => ref Items[i]; + + public Span Items + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => SpanHelpers.CreateSpan(ref MemoryMarshal.GetReference(_0.Items), Length); + } + + public readonly ReadOnlySpan ItemsRo + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => SpanHelpers.CreateSpan(ref MemoryMarshal.GetReference(_0.ItemsRo), Length); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator ReadOnlySpan(in Array27 value) => value.ItemsRo; +} \ No newline at end of file diff --git a/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs b/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs index e2576983..c4e30339 100644 --- a/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs +++ b/src/LibHac/FsSrv/FileSystemProxyCoreImpl.cs @@ -1,6 +1,7 @@ using System; -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; using LibHac.Common; +using LibHac.Common.FixedArrays; using LibHac.Fs; using LibHac.Fs.Fsa; using LibHac.Fs.Shim; @@ -29,11 +30,7 @@ public class FileSystemProxyCoreImpl public Result OpenCustomStorageFileSystem(ref SharedRef outFileSystem, CustomStorageId storageId) { - // Hack around error CS8350. - const int pathBufferLength = 0x40; - Span buffer = stackalloc byte[pathBufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span pathBuffer = MemoryMarshal.CreateSpan(ref bufferRef, pathBufferLength); + Unsafe.SkipInit(out Array64 pathBuffer); using var fileSystem = new SharedRef(); @@ -43,7 +40,7 @@ public class FileSystemProxyCoreImpl if (rc.IsFailure()) return rc; using var path = new Path(); - rc = PathFunctions.SetUpFixedPathSingleEntry(ref path.Ref(), pathBuffer, + rc = PathFunctions.SetUpFixedPathSingleEntry(ref path.Ref(), pathBuffer.Items, CustomStorage.GetCustomStorageDirectoryName(CustomStorageId.System)); if (rc.IsFailure()) return rc; @@ -57,7 +54,7 @@ public class FileSystemProxyCoreImpl if (rc.IsFailure()) return rc; using var path = new Path(); - rc = PathFunctions.SetUpFixedPathDoubleEntry(ref path.Ref(), pathBuffer, + rc = PathFunctions.SetUpFixedPathDoubleEntry(ref path.Ref(), pathBuffer.Items, CommonPaths.SdCardNintendoRootDirectoryName, CustomStorage.GetCustomStorageDirectoryName(CustomStorageId.System)); if (rc.IsFailure()) return rc; @@ -119,4 +116,4 @@ public class FileSystemProxyCoreImpl _sdEncryptionSeed = seed; return Result.Success; } -} +} \ No newline at end of file diff --git a/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs b/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs index 5a32bb7f..e649e72c 100644 --- a/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs +++ b/src/LibHac/FsSrv/FsCreator/SaveDataFileSystemCreator.cs @@ -1,6 +1,7 @@ using System; -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; using LibHac.Common; +using LibHac.Common.FixedArrays; using LibHac.Common.Keys; using LibHac.Diag; using LibHac.Fs; @@ -44,15 +45,12 @@ public class SaveDataFileSystemCreator : ISaveDataFileSystemCreator bool isJournalingSupported, bool isMultiCommitSupported, bool openReadOnly, bool openShared, ISaveDataCommitTimeStampGetter timeStampGetter) { - // Hack around error CS8350. - Span buffer = stackalloc byte[0x12]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, 0x12); + Unsafe.SkipInit(out Array18 saveImageNameBuffer); Assert.SdkRequiresNotNull(cacheManager); using var saveImageName = new Path(); - Result rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer, saveDataId); + Result rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer.Items, saveDataId); if (rc.IsFailure()) return rc; rc = baseFileSystem.Get.GetEntryType(out DirectoryEntryType type, in saveImageName); diff --git a/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs b/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs index 85d4c7e8..db7be714 100644 --- a/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs +++ b/src/LibHac/FsSrv/NcaFileSystemServiceImpl.cs @@ -1,8 +1,8 @@ using System; using System.Buffers.Text; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using LibHac.Common; +using LibHac.Common.FixedArrays; using LibHac.Fs; using LibHac.Fs.Fsa; using LibHac.FsSrv.FsCreator; @@ -217,8 +217,6 @@ public class NcaFileSystemServiceImpl public Result OpenContentStorageFileSystem(ref SharedRef outFileSystem, ContentStorageId contentStorageId) { - const int pathBufferLength = 0x40; - using var fileSystem = new SharedRef(); Result rc; @@ -241,21 +239,18 @@ public class NcaFileSystemServiceImpl return ResultFs.InvalidArgument.Log(); } - // Hack around error CS8350. - Span buffer = stackalloc byte[pathBufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span contentStoragePathBuffer = MemoryMarshal.CreateSpan(ref bufferRef, pathBufferLength); + Unsafe.SkipInit(out Array64 contentStoragePathBuffer); // Build the appropriate path for the content storage ID if (contentStorageId == ContentStorageId.SdCard) { - var sb = new U8StringBuilder(contentStoragePathBuffer); + var sb = new U8StringBuilder(contentStoragePathBuffer.Items); sb.Append(StringTraits.DirectorySeparator).Append(SdCardNintendoRootDirectoryName); sb.Append(StringTraits.DirectorySeparator).Append(ContentStorageDirectoryName); } else { - var sb = new U8StringBuilder(contentStoragePathBuffer); + var sb = new U8StringBuilder(contentStoragePathBuffer.Items); sb.Append(StringTraits.DirectorySeparator).Append(ContentStorageDirectoryName); } diff --git a/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs b/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs index 73d83e9f..3fd82d87 100644 --- a/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs +++ b/src/LibHac/FsSrv/SaveDataFileSystemServiceImpl.cs @@ -1,6 +1,7 @@ using System; -using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; using LibHac.Common; +using LibHac.Common.FixedArrays; using LibHac.Diag; using LibHac.Fs; using LibHac.Fs.Fsa; @@ -91,14 +92,10 @@ public class SaveDataFileSystemServiceImpl if (rc.IsFailure()) return rc.Miss(); // Get the path of the save data - // Hack around error CS8350. - const int bufferLength = 0x12; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array18 saveImageNameBuffer); using var saveImageName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer, saveDataId); + rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer.Items, saveDataId); if (rc.IsFailure()) return rc.Miss(); rc = fileSystem.Get.GetEntryType(out _, in saveImageName); @@ -195,15 +192,11 @@ public class SaveDataFileSystemServiceImpl public Result OpenSaveDataMetaDirectoryFileSystem(ref SharedRef outFileSystem, SaveDataSpaceId spaceId, ulong saveDataId) { - // Hack around error CS8350. - const int bufferLength = 0x1B; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveDataMetaIdDirectoryNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array27 saveDataMetaIdDirectoryNameBuffer); using var saveDataMetaIdDirectoryName = new Path(); Result rc = PathFunctions.SetUpFixedPathSaveMetaDir(ref saveDataMetaIdDirectoryName.Ref(), - saveDataMetaIdDirectoryNameBuffer, saveDataId); + saveDataMetaIdDirectoryNameBuffer.Items, saveDataId); if (rc.IsFailure()) return rc.Miss(); return OpenSaveDataDirectoryFileSystemImpl(ref outFileSystem, spaceId, in saveDataMetaIdDirectoryName); @@ -230,14 +223,10 @@ public class SaveDataFileSystemServiceImpl Result rc = OpenSaveDataMetaDirectoryFileSystem(ref fileSystem.Ref(), spaceId, saveDataId); if (rc.IsFailure()) return rc.Miss(); - // Hack around error CS8350. - const int bufferLength = 0xF; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveDataMetaNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array15 saveDataMetaNameBuffer); using var saveDataMetaName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName.Ref(), saveDataMetaNameBuffer, + rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName.Ref(), saveDataMetaNameBuffer.Items, (uint)metaType); if (rc.IsFailure()) return rc.Miss(); @@ -254,14 +243,10 @@ public class SaveDataFileSystemServiceImpl Result rc = OpenSaveDataMetaDirectoryFileSystem(ref fileSystem.Ref(), spaceId, saveDataId); if (rc.IsFailure()) return rc.Miss(); - // Hack around error CS8350. - const int bufferLength = 0xF; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveDataMetaNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array15 saveDataMetaNameBuffer); using var saveDataMetaName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName.Ref(), saveDataMetaNameBuffer, + rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName.Ref(), saveDataMetaNameBuffer.Items, (uint)metaType); if (rc.IsFailure()) return rc.Miss(); @@ -280,11 +265,7 @@ public class SaveDataFileSystemServiceImpl (byte)'a' }; - // Hack around error CS8350. - const int bufferLength = 0x12; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveDataIdDirectoryNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array18 saveDataIdDirectoryNameBuffer); using var fileSystem = new SharedRef(); @@ -296,7 +277,7 @@ public class SaveDataFileSystemServiceImpl if (rc.IsFailure()) return rc.Miss(); using var saveDataIdDirectoryName = new Path(); - PathFunctions.SetUpFixedPathSaveId(ref saveDataIdDirectoryName.Ref(), saveDataIdDirectoryNameBuffer, + PathFunctions.SetUpFixedPathSaveId(ref saveDataIdDirectoryName.Ref(), saveDataIdDirectoryNameBuffer.Items, saveDataId); if (rc.IsFailure()) return rc.Miss(); @@ -322,14 +303,10 @@ public class SaveDataFileSystemServiceImpl Result rc = OpenSaveDataMetaDirectoryFileSystem(ref fileSystem.Ref(), spaceId, saveDataId); if (rc.IsFailure()) return rc.Miss(); - // Hack around error CS8350. - const int bufferLength = 0xF; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveDataMetaNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array15 saveDataMetaNameBuffer); using var saveDataMetaName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName.Ref(), saveDataMetaNameBuffer, + rc = PathFunctions.SetUpFixedPathSaveMetaName(ref saveDataMetaName.Ref(), saveDataMetaNameBuffer.Items, (uint)metaType); if (rc.IsFailure()) return rc.Miss(); @@ -345,11 +322,7 @@ public class SaveDataFileSystemServiceImpl { // Use directory save data for now - // Hack around error CS8350. - const int bufferLength = 0x12; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array18 saveImageNameBuffer); using var fileSystem = new SharedRef(); @@ -358,7 +331,7 @@ public class SaveDataFileSystemServiceImpl if (rc.IsFailure()) return rc.Miss(); using var saveImageName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer, saveDataId); + rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer.Items, saveDataId); if (rc.IsFailure()) return rc.Miss(); if (_config.IsPseudoSaveData()) @@ -414,11 +387,7 @@ public class SaveDataFileSystemServiceImpl public Result DeleteSaveDataFileSystem(SaveDataSpaceId spaceId, ulong saveDataId, bool wipeSaveFile, in Path saveDataRootPath) { - // Hack around error CS8350. - const int bufferLength = 0x12; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array18 saveImageNameBuffer); using var fileSystem = new SharedRef(); @@ -429,7 +398,7 @@ public class SaveDataFileSystemServiceImpl if (rc.IsFailure()) return rc.Miss(); using var saveImageName = new Path(); - rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer, saveDataId); + rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer.Items, saveDataId); if (rc.IsFailure()) return rc.Miss(); // Check if the save data is a file or a directory @@ -664,14 +633,10 @@ public class SaveDataFileSystemServiceImpl rc = _config.BaseFsService.OpenSdCardProxyFileSystem(ref baseFileSystem.Ref(), true); if (rc.IsFailure()) return rc.Miss(); - // Hack around error CS8350. - const int bufferLength = 0x40; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span pathParentBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array64 pathParentBuffer); using var pathParent = new Path(); - rc = PathFunctions.SetUpFixedPathSingleEntry(ref pathParent.Ref(), pathParentBuffer, + rc = PathFunctions.SetUpFixedPathSingleEntry(ref pathParent.Ref(), pathParentBuffer.Items, CommonPaths.SdCardNintendoRootDirectoryName); if (rc.IsFailure()) return rc.Miss(); diff --git a/src/LibHac/FsSrv/SaveDataSharedFileStorage.cs b/src/LibHac/FsSrv/SaveDataSharedFileStorage.cs index 0cddf87d..5709b159 100644 --- a/src/LibHac/FsSrv/SaveDataSharedFileStorage.cs +++ b/src/LibHac/FsSrv/SaveDataSharedFileStorage.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using LibHac.Common; +using LibHac.Common.FixedArrays; using LibHac.Diag; using LibHac.Fs; using LibHac.Fs.Fsa; @@ -334,14 +334,10 @@ public class SaveDataFileStorageHolder ref SharedRef baseFileSystem, SaveDataSpaceId spaceId, ulong saveDataId, OpenMode mode, Optional type) { - // Hack around error CS8350. - const int bufferLength = 0x12; - Span buffer = stackalloc byte[bufferLength]; - ref byte bufferRef = ref MemoryMarshal.GetReference(buffer); - Span saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength); + Unsafe.SkipInit(out Array18 saveImageNameBuffer); using var saveImageName = new Path(); - Result rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer, saveDataId); + Result rc = PathFunctions.SetUpFixedPathSaveId(ref saveImageName.Ref(), saveImageNameBuffer.Items, saveDataId); if (rc.IsFailure()) return rc; // If an open type isn't specified, open the save without the shared file storage layer