forked from Thealexbarney/LibHac
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add remaining GetRightsId methods and U8StringBuilder
- Loading branch information
1 parent
fdd7eeb
commit 9934f47
Showing
5 changed files
with
180 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LibHac.Common | ||
{ | ||
// In order for the Visual Studio debugger to accurately display a struct, every offset | ||
// in the struct that is used for the debugger display must be part of a field. | ||
// These padding structs make it easier to accomplish that. | ||
[StructLayout(LayoutKind.Sequential, Size = 0x20)] | ||
internal struct Padding20 | ||
{ | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly ulong Padding00; | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly ulong Padding08; | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly ulong Padding10; | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly ulong Padding18; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential, Size = 0x40)] | ||
internal struct Padding40 | ||
{ | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly Padding20 Padding00; | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly Padding20 Padding20; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential, Size = 0x80)] | ||
internal struct Padding80 | ||
{ | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly Padding40 Padding00; | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly Padding40 Padding40; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential, Size = 0x100)] | ||
internal struct Padding100 | ||
{ | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly Padding80 Padding00; | ||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly Padding80 Padding80; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace LibHac.Common | ||
{ | ||
[DebuggerDisplay("{ToString()}")] | ||
public ref struct U8StringBuilder | ||
{ | ||
private const int NullTerminatorLength = 1; | ||
|
||
private readonly Span<byte> _buffer; | ||
private int _length; | ||
|
||
public bool Overflowed { get; private set; } | ||
public int Capacity => _buffer.Length - NullTerminatorLength; | ||
|
||
public U8StringBuilder(Span<byte> buffer) | ||
{ | ||
_buffer = buffer; | ||
_length = 0; | ||
Overflowed = false; | ||
|
||
ThrowIfBufferLengthIsZero(); | ||
|
||
AddNullTerminator(); | ||
} | ||
|
||
public U8StringBuilder Append(ReadOnlySpan<byte> value) | ||
{ | ||
if (Overflowed) return this; | ||
|
||
int valueLength = StringUtils.GetLength(value); | ||
|
||
if (!HasAdditionalCapacity(valueLength)) | ||
{ | ||
Overflowed = true; | ||
return this; | ||
} | ||
|
||
value.Slice(0, valueLength).CopyTo(_buffer.Slice(_length)); | ||
_length += valueLength; | ||
AddNullTerminator(); | ||
|
||
return this; | ||
} | ||
|
||
public U8StringBuilder Append(byte value) | ||
{ | ||
if (Overflowed) return this; | ||
|
||
if (!HasAdditionalCapacity(1)) | ||
{ | ||
Overflowed = true; | ||
return this; | ||
} | ||
|
||
_buffer[_length] = value; | ||
_length++; | ||
AddNullTerminator(); | ||
|
||
return this; | ||
} | ||
|
||
private bool HasCapacity(int requiredCapacity) | ||
{ | ||
return requiredCapacity <= Capacity; | ||
} | ||
|
||
private bool HasAdditionalCapacity(int requiredAdditionalCapacity) | ||
{ | ||
return HasCapacity(_length + requiredAdditionalCapacity); | ||
} | ||
|
||
private void AddNullTerminator() | ||
{ | ||
_buffer[_length] = 0; | ||
} | ||
|
||
private void ThrowIfBufferLengthIsZero() | ||
{ | ||
if (_buffer.Length == 0) throw new ArgumentException("Buffer length must be greater than 0."); | ||
} | ||
|
||
public override string ToString() => StringUtils.Utf8ZToString(_buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters