mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/PCK-Studio.git
synced 2026-07-13 09:08:08 +00:00
50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using PckStudio.Classes.FileTypes;
|
|
|
|
namespace PckStudio.Classes.IO.ARC
|
|
{
|
|
internal class ARCFileReader : StreamDataReader<ConsoleArchive>
|
|
{
|
|
public static ConsoleArchive Read(Stream stream, bool useLittleEndian = false)
|
|
{
|
|
return new ARCFileReader(useLittleEndian).ReadFromStream(stream);
|
|
}
|
|
|
|
private ARCFileReader(bool useLittleEndian) : base(useLittleEndian)
|
|
{
|
|
}
|
|
|
|
protected override ConsoleArchive ReadFromStream(Stream stream)
|
|
{
|
|
ConsoleArchive _archive = new ConsoleArchive();
|
|
int numberOfFiles = ReadInt(stream);
|
|
for(int i = 0; i < numberOfFiles; i++)
|
|
{
|
|
string name = ReadString(stream);
|
|
int offset = ReadInt(stream);
|
|
int size = ReadInt(stream);
|
|
_archive[name] = ReadBytesFromPosition(stream, offset, size);
|
|
}
|
|
return _archive;
|
|
}
|
|
|
|
private string ReadString(Stream stream)
|
|
{
|
|
short length = ReadShort(stream);
|
|
return ReadString(stream, length, Encoding.UTF8);
|
|
}
|
|
|
|
private byte[] ReadBytesFromPosition(Stream stream, int position, int size)
|
|
{
|
|
long originalPOS = stream.Position;
|
|
if (stream.Seek(position, SeekOrigin.Begin) != position) throw new Exception();
|
|
byte[] data = ReadBytes(stream, size);
|
|
if (stream.Seek(originalPOS, SeekOrigin.Begin) != originalPOS) throw new Exception();
|
|
return data;
|
|
}
|
|
|
|
}
|
|
}
|