mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Move Ticket, SwitchFs, Cnmt, Nro, Xci again
This commit is contained in:
parent
da930dfa81
commit
a88b3058e9
10 changed files with 30 additions and 164 deletions
|
@ -1,132 +0,0 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace LibHac;
|
||||
|
||||
public class BitReader
|
||||
{
|
||||
public byte[] Buffer { get; private set; }
|
||||
public int LengthBits { get; private set; }
|
||||
public int Position { get; set; }
|
||||
public int Remaining => LengthBits - Position;
|
||||
|
||||
public BitReader(byte[] buffer) => SetBuffer(buffer);
|
||||
|
||||
public void SetBuffer(byte[] buffer)
|
||||
{
|
||||
Buffer = buffer;
|
||||
LengthBits = Buffer?.Length * 8 ?? 0;
|
||||
Position = 0;
|
||||
}
|
||||
|
||||
public int ReadInt(int bitCount)
|
||||
{
|
||||
int value = PeekInt(bitCount);
|
||||
Position += bitCount;
|
||||
return value;
|
||||
}
|
||||
|
||||
//public int ReadSignedInt(int bitCount)
|
||||
//{
|
||||
// int value = PeekInt(bitCount);
|
||||
// Position += bitCount;
|
||||
// return Bit.SignExtend32(value, bitCount);
|
||||
//}
|
||||
|
||||
public bool ReadBool() => ReadInt(1) == 1;
|
||||
|
||||
public int ReadOffsetBinary(int bitCount, OffsetBias bias)
|
||||
{
|
||||
int offset = (1 << (bitCount - 1)) - (int)bias;
|
||||
int value = PeekInt(bitCount) - offset;
|
||||
Position += bitCount;
|
||||
return value;
|
||||
}
|
||||
|
||||
//public void AlignPosition(int multiple)
|
||||
//{
|
||||
// Position = Helpers.GetNextMultiple(Position, multiple);
|
||||
//}
|
||||
|
||||
public int PeekInt(int bitCount)
|
||||
{
|
||||
Debug.Assert(bitCount >= 0 && bitCount <= 32);
|
||||
|
||||
if (bitCount > Remaining)
|
||||
{
|
||||
if (Position >= LengthBits) return 0;
|
||||
|
||||
int extraBits = bitCount - Remaining;
|
||||
return PeekIntFallback(Remaining) << extraBits;
|
||||
}
|
||||
|
||||
int byteIndex = Position / 8;
|
||||
int bitIndex = Position % 8;
|
||||
|
||||
if (bitCount <= 9 && Remaining >= 16)
|
||||
{
|
||||
int value = Buffer[byteIndex] << 8 | Buffer[byteIndex + 1];
|
||||
value &= 0xFFFF >> bitIndex;
|
||||
value >>= 16 - bitCount - bitIndex;
|
||||
return value;
|
||||
}
|
||||
|
||||
if (bitCount <= 17 && Remaining >= 24)
|
||||
{
|
||||
int value = Buffer[byteIndex] << 16 | Buffer[byteIndex + 1] << 8 | Buffer[byteIndex + 2];
|
||||
value &= 0xFFFFFF >> bitIndex;
|
||||
value >>= 24 - bitCount - bitIndex;
|
||||
return value;
|
||||
}
|
||||
|
||||
if (bitCount <= 25 && Remaining >= 32)
|
||||
{
|
||||
int value = Buffer[byteIndex] << 24 | Buffer[byteIndex + 1] << 16 | Buffer[byteIndex + 2] << 8 | Buffer[byteIndex + 3];
|
||||
value &= (int)(0xFFFFFFFF >> bitIndex);
|
||||
value >>= 32 - bitCount - bitIndex;
|
||||
return value;
|
||||
}
|
||||
return PeekIntFallback(bitCount);
|
||||
}
|
||||
|
||||
private int PeekIntFallback(int bitCount)
|
||||
{
|
||||
int value = 0;
|
||||
int byteIndex = Position / 8;
|
||||
int bitIndex = Position % 8;
|
||||
|
||||
while (bitCount > 0)
|
||||
{
|
||||
if (bitIndex >= 8)
|
||||
{
|
||||
bitIndex = 0;
|
||||
byteIndex++;
|
||||
}
|
||||
|
||||
int bitsToRead = Math.Min(bitCount, 8 - bitIndex);
|
||||
int mask = 0xFF >> bitIndex;
|
||||
int currentByte = (mask & Buffer[byteIndex]) >> (8 - bitIndex - bitsToRead);
|
||||
|
||||
value = (value << bitsToRead) | currentByte;
|
||||
bitIndex += bitsToRead;
|
||||
bitCount -= bitsToRead;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the bias of an offset binary value. A positive bias can represent one more
|
||||
/// positive value than negative value, and a negative bias can represent one more
|
||||
/// negative value than positive value.
|
||||
/// </summary>
|
||||
/// <remarks>Example:
|
||||
/// A 4-bit offset binary value with a positive bias can store
|
||||
/// the values 8 through -7 inclusive.
|
||||
/// A 4-bit offset binary value with a negative bias can store
|
||||
/// the values 7 through -8 inclusive.</remarks>
|
||||
public enum OffsetBias
|
||||
{
|
||||
Positive = 1,
|
||||
Negative = 0
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
namespace LibHac;
|
||||
|
||||
public static class BitTools
|
||||
{
|
||||
public static int SignExtend32(int value, int bits)
|
||||
{
|
||||
int shift = 8 * sizeof(int) - bits;
|
||||
return (value << shift) >> shift;
|
||||
}
|
||||
}
|
|
@ -75,7 +75,7 @@ public class FileReader
|
|||
{
|
||||
FillBuffer(offset, 3, updatePosition);
|
||||
|
||||
return BitTools.SignExtend32(MemoryMarshal.Read<int>(_buffer), 24);
|
||||
return SignExtend32(MemoryMarshal.Read<int>(_buffer), 24);
|
||||
}
|
||||
|
||||
public uint ReadUInt32(long offset, bool updatePosition)
|
||||
|
@ -176,4 +176,10 @@ public class FileReader
|
|||
public byte[] ReadBytes(int length) => ReadBytes(Position, length, true);
|
||||
public void ReadBytes(Span<byte> destination) => ReadBytes(destination, Position, true);
|
||||
public string ReadAscii(int length) => ReadAscii(Position, length, true);
|
||||
|
||||
public static int SignExtend32(int value, int bits)
|
||||
{
|
||||
int shift = 8 * sizeof(int) - bits;
|
||||
return (value << shift) >> shift;
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@ using System.IO;
|
|||
using LibHac.Common.Keys;
|
||||
using LibHac.Util;
|
||||
|
||||
namespace LibHac;
|
||||
namespace LibHac.Tools.Es;
|
||||
|
||||
public class Ticket
|
||||
{
|
|
@ -12,9 +12,10 @@ using LibHac.FsSystem.NcaUtils;
|
|||
using LibHac.FsSystem.Save;
|
||||
using LibHac.Ncm;
|
||||
using LibHac.Ns;
|
||||
using LibHac.Tools.Ncm;
|
||||
using LibHac.Util;
|
||||
|
||||
namespace LibHac;
|
||||
namespace LibHac.Tools.Fs;
|
||||
|
||||
public class SwitchFs : IDisposable
|
||||
{
|
||||
|
@ -44,10 +45,10 @@ public class SwitchFs : IDisposable
|
|||
{
|
||||
var concatFs = new ConcatenationFileSystem(fileSystem);
|
||||
|
||||
using var contentDirPath = new Fs.Path();
|
||||
using var contentDirPath = new LibHac.Fs.Path();
|
||||
PathFunctions.SetUpFixedPath(ref contentDirPath.Ref(), "/Nintendo/Contents".ToU8String()).ThrowIfFailure();
|
||||
|
||||
using var saveDirPath = new Fs.Path();
|
||||
using var saveDirPath = new LibHac.Fs.Path();
|
||||
PathFunctions.SetUpFixedPath(ref saveDirPath.Ref(), "/Nintendo/save".ToU8String()).ThrowIfFailure();
|
||||
|
||||
var contentDirFs = new SubdirectoryFileSystem(concatFs);
|
||||
|
@ -75,14 +76,14 @@ public class SwitchFs : IDisposable
|
|||
|
||||
if (concatFs.DirectoryExists("/save"))
|
||||
{
|
||||
using var savePath = new Fs.Path();
|
||||
using var savePath = new LibHac.Fs.Path();
|
||||
PathFunctions.SetUpFixedPath(ref savePath.Ref(), "/save".ToU8String());
|
||||
|
||||
saveDirFs = new SubdirectoryFileSystem(concatFs);
|
||||
saveDirFs.Initialize(in savePath).ThrowIfFailure();
|
||||
}
|
||||
|
||||
using var contentsPath = new Fs.Path();
|
||||
using var contentsPath = new LibHac.Fs.Path();
|
||||
PathFunctions.SetUpFixedPath(ref contentsPath.Ref(), "/Contents".ToU8String());
|
||||
|
||||
contentDirFs = new SubdirectoryFileSystem(concatFs);
|
||||
|
@ -195,11 +196,11 @@ public class SwitchFs : IDisposable
|
|||
|
||||
switch (content.Type)
|
||||
{
|
||||
case Ncm.ContentType.Program:
|
||||
case Ncm.ContentType.Data:
|
||||
case LibHac.Ncm.ContentType.Program:
|
||||
case LibHac.Ncm.ContentType.Data:
|
||||
title.MainNca = contentNca;
|
||||
break;
|
||||
case Ncm.ContentType.Control:
|
||||
case LibHac.Ncm.ContentType.Control:
|
||||
title.ControlNca = contentNca;
|
||||
break;
|
||||
}
|
|
@ -3,9 +3,8 @@ using LibHac.Common.Keys;
|
|||
using LibHac.Fs;
|
||||
using LibHac.Fs.Fsa;
|
||||
using LibHac.FsSystem;
|
||||
using LibHac.Tools.Fs;
|
||||
|
||||
namespace LibHac;
|
||||
namespace LibHac.Tools.Fs;
|
||||
|
||||
public class Xci
|
||||
{
|
||||
|
|
|
@ -4,7 +4,7 @@ using LibHac.FsSystem.NcaUtils;
|
|||
using LibHac.Ncm;
|
||||
using ContentType = LibHac.Ncm.ContentType;
|
||||
|
||||
namespace LibHac;
|
||||
namespace LibHac.Tools.Ncm;
|
||||
|
||||
public class Cnmt
|
||||
{
|
|
@ -2,7 +2,7 @@
|
|||
using LibHac.Fs;
|
||||
using LibHac.FsSystem;
|
||||
|
||||
namespace LibHac;
|
||||
namespace LibHac.Tools.Ro;
|
||||
|
||||
public class Nro
|
||||
{
|
|
@ -1,9 +1,10 @@
|
|||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using LibHac;
|
||||
using LibHac.Fs;
|
||||
using LibHac.FsSystem;
|
||||
using LibHac.Tools.Es;
|
||||
using LibHac.Tools.Fs;
|
||||
using LibHac.Util;
|
||||
using static hactoolnet.Print;
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ using LibHac.FsSystem;
|
|||
using LibHac.FsSystem.NcaUtils;
|
||||
using LibHac.FsSystem.Save;
|
||||
using LibHac.Ns;
|
||||
using LibHac.Tools.Fs;
|
||||
using Path = System.IO.Path;
|
||||
|
||||
namespace hactoolnet;
|
||||
|
|
Loading…
Reference in a new issue