Update ARC File Reader/Writer

This commit is contained in:
miku-666
2022-11-07 15:05:14 +01:00
parent 40ddb22aac
commit 265b5d96b8
2 changed files with 13 additions and 16 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using PckStudio.Classes.FileTypes;
@@ -24,9 +23,9 @@ namespace PckStudio.Classes.IO.ARC
for(int i = 0; i < numberOfFiles; i++)
{
string name = ReadString(stream);
int pos = ReadInt(stream);
int offset = ReadInt(stream);
int size = ReadInt(stream);
_archive[name] = ReadBytesFromPosition(stream, size, pos);
_archive[name] = ReadBytesFromPosition(stream, offset, size);
}
return _archive;
}
@@ -37,7 +36,7 @@ namespace PckStudio.Classes.IO.ARC
return ReadString(stream, length, Encoding.UTF8);
}
private byte[] ReadBytesFromPosition(Stream stream, int size, int position)
private byte[] ReadBytesFromPosition(Stream stream, int position, int size)
{
long originalPOS = stream.Position;
if (stream.Seek(position, SeekOrigin.Begin) != position) throw new Exception();

View File

@@ -1,9 +1,6 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PckStudio.Classes.FileTypes;
namespace PckStudio.Classes.IO.ARC
@@ -24,26 +21,27 @@ namespace PckStudio.Classes.IO.ARC
protected override void WriteToStream(Stream stream)
{
WriteInt(stream, _archive.Count);
int currentOffset = 4 + _archive.Keys.Sum(key => 10 + key.Length);
foreach (var pair in _archive)
var arc = _archive.ToArray();
WriteInt(stream, arc.Length);
int offset = 4 + arc.Sum(pair => 10 + pair.Key.Length);
foreach (var pair in arc)
{
int size = pair.Value.Length;
WriteString(stream, pair.Key);
WriteInt(stream, currentOffset);
WriteInt(stream, offset);
WriteInt(stream, size);
currentOffset += size;
offset += size;
}
foreach (byte[] data in _archive.Values)
foreach (var pair in arc)
{
WriteBytes(stream, data);
WriteBytes(stream, pair.Value);
}
}
private void WriteString(Stream stream, string s)
{
WriteShort(stream, (short)s.Length);
WriteString(stream, s, Encoding.UTF8);
WriteString(stream, s, Encoding.ASCII);
}
}
}