Remove a workaround for CS8350 by using fixed arrays

This commit is contained in:
Alex Barney 2022-01-15 13:50:15 -07:00
parent 7f1035374d
commit f315cee9af
6 changed files with 70 additions and 86 deletions

View file

@ -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<T>
{
public const int Length = 27;
private Array16<T> _0;
private Array8<T> _16;
private Array2<T> _24;
private T _26;
public ref T this[int i] => ref Items[i];
public Span<T> Items
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => SpanHelpers.CreateSpan(ref MemoryMarshal.GetReference(_0.Items), Length);
}
public readonly ReadOnlySpan<T> ItemsRo
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => SpanHelpers.CreateSpan(ref MemoryMarshal.GetReference(_0.ItemsRo), Length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator ReadOnlySpan<T>(in Array27<T> value) => value.ItemsRo;
}

View file

@ -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<IFileSystem> outFileSystem, CustomStorageId storageId)
{
// Hack around error CS8350.
const int pathBufferLength = 0x40;
Span<byte> buffer = stackalloc byte[pathBufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> pathBuffer = MemoryMarshal.CreateSpan(ref bufferRef, pathBufferLength);
Unsafe.SkipInit(out Array64<byte> pathBuffer);
using var fileSystem = new SharedRef<IFileSystem>();
@ -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;
}
}
}

View file

@ -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<byte> buffer = stackalloc byte[0x12];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, 0x12);
Unsafe.SkipInit(out Array18<byte> 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);

View file

@ -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<IFileSystem> outFileSystem,
ContentStorageId contentStorageId)
{
const int pathBufferLength = 0x40;
using var fileSystem = new SharedRef<IFileSystem>();
Result rc;
@ -241,21 +239,18 @@ public class NcaFileSystemServiceImpl
return ResultFs.InvalidArgument.Log();
}
// Hack around error CS8350.
Span<byte> buffer = stackalloc byte[pathBufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> contentStoragePathBuffer = MemoryMarshal.CreateSpan(ref bufferRef, pathBufferLength);
Unsafe.SkipInit(out Array64<byte> 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);
}

View file

@ -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<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array18<byte> 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<IFileSystem> outFileSystem,
SaveDataSpaceId spaceId, ulong saveDataId)
{
// Hack around error CS8350.
const int bufferLength = 0x1B;
Span<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveDataMetaIdDirectoryNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array27<byte> 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<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveDataMetaNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array15<byte> 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<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveDataMetaNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array15<byte> 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<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveDataIdDirectoryNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array18<byte> saveDataIdDirectoryNameBuffer);
using var fileSystem = new SharedRef<IFileSystem>();
@ -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<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveDataMetaNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array15<byte> 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<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array18<byte> saveImageNameBuffer);
using var fileSystem = new SharedRef<IFileSystem>();
@ -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<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array18<byte> saveImageNameBuffer);
using var fileSystem = new SharedRef<IFileSystem>();
@ -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<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> pathParentBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array64<byte> 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();

View file

@ -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<IFileSystem> baseFileSystem, SaveDataSpaceId spaceId, ulong saveDataId, OpenMode mode,
Optional<SaveDataOpenTypeSetFileStorage.OpenType> type)
{
// Hack around error CS8350.
const int bufferLength = 0x12;
Span<byte> buffer = stackalloc byte[bufferLength];
ref byte bufferRef = ref MemoryMarshal.GetReference(buffer);
Span<byte> saveImageNameBuffer = MemoryMarshal.CreateSpan(ref bufferRef, bufferLength);
Unsafe.SkipInit(out Array18<byte> 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