Fix several issues when saving new files.

- Fix writing raw bytes.
- In case of saving into Stream instances, uniquely leave them open everywhere.
This commit is contained in:
Ray Koopa 2020-06-26 18:53:25 +02:00
parent 84ba8c56a0
commit ed2785e339
14 changed files with 31 additions and 27 deletions

View File

@ -67,6 +67,8 @@ namespace Syroot.Worms.Armageddon.ProjectX
if (reader.ReadInt32() != _signature) if (reader.ReadInt32() != _signature)
throw new InvalidDataException("Invalid PXL file signature."); throw new InvalidDataException("Invalid PXL file signature.");
Version = reader.Read1Byte(); Version = reader.Read1Byte();
if (Version != _version)
throw new InvalidDataException("Invalid PXL file version.");
// Read the items. // Read the items.
Clear(); Clear();
@ -106,7 +108,7 @@ namespace Syroot.Worms.Armageddon.ProjectX
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param> /// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream) public void Save(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
// Write the header. // Write the header.
writer.Write(_signature); writer.Write(_signature);
@ -123,7 +125,7 @@ namespace Syroot.Worms.Armageddon.ProjectX
case LibraryItemType.File: case LibraryItemType.File:
byte[] value = (byte[])item.Value; byte[] value = (byte[])item.Value;
writer.Write(value.Length); writer.Write(value.Length);
writer.WriteStructs(value); writer.Write(value);
break; break;
case LibraryItemType.Script: case LibraryItemType.Script:
writer.Write((string)item.Value, StringCoding.Int32CharCount); writer.Write((string)item.Value, StringCoding.Int32CharCount);

View File

@ -129,7 +129,7 @@ namespace Syroot.Worms.Armageddon.ProjectX
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param> /// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream) public void Save(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
// Write the header. // Write the header.
writer.Write(_signature, StringCoding.Raw); writer.Write(_signature, StringCoding.Raw);
@ -152,7 +152,7 @@ namespace Syroot.Worms.Armageddon.ProjectX
{ {
writer.Write(file.Key, StringCoding.Int32CharCount); writer.Write(file.Key, StringCoding.Int32CharCount);
writer.Write(file.Value.Length); writer.Write(file.Value.Length);
writer.WriteStructs(file.Value); writer.Write(file.Value);
} }
// Write attached scripts. // Write attached scripts.

View File

@ -132,7 +132,7 @@ namespace Syroot.Worms.WorldParty
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param> /// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream) public void Save(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
// Write the header. // Write the header.
writer.Write(_signature); writer.Write(_signature);

View File

@ -269,7 +269,7 @@ namespace Syroot.Worms.WorldParty
{ {
writer.WriteString(GraveFileName, 0x20); writer.WriteString(GraveFileName, 0x20);
writer.WriteStructs(Grave.Palette); writer.WriteStructs(Grave.Palette);
writer.WriteStructs(Grave.Data); writer.Write(Grave.Data);
} }
writer.WriteEnum(TeamWeapon, true); writer.WriteEnum(TeamWeapon, true);
@ -287,13 +287,13 @@ namespace Syroot.Worms.WorldParty
writer.WriteString(FlagFileName, 0x20); writer.WriteString(FlagFileName, 0x20);
writer.WriteStructs(Flag.Palette); writer.WriteStructs(Flag.Palette);
writer.WriteStructs(Flag.Data); writer.Write(Flag.Data);
writer.Write(Unknown1); writer.Write(Unknown1);
writer.Write(DeathmatchRank); writer.Write(DeathmatchRank);
writer.Write(TrainingMissionTimes); writer.Write(TrainingMissionTimes);
writer.Write(UnknownTrainingMissionTime); writer.Write(UnknownTrainingMissionTime);
writer.WriteStructs(WeaponPoints); writer.Write(WeaponPoints);
writer.Write(Fort); writer.Write(Fort);
writer.Write(Unknown2); writer.Write(Unknown2);
} }

View File

@ -104,7 +104,7 @@ namespace Syroot.Worms.WorldParty
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param> /// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream) public void Save(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
// Write the header. // Write the header.
writer.Write(_signature, StringCoding.ZeroTerminated); writer.Write(_signature, StringCoding.ZeroTerminated);
@ -114,7 +114,7 @@ namespace Syroot.Worms.WorldParty
writer.Write((byte)Teams.Count); writer.Write((byte)Teams.Count);
writer.Write(Unknown1); writer.Write(Unknown1);
writer.Write(Unknown2); writer.Write(Unknown2);
writer.WriteStructs(Unknown3); writer.Write(Unknown3);
// Write the teams. // Write the teams.
foreach (Team team in Teams) foreach (Team team in Teams)

View File

@ -138,7 +138,7 @@ namespace Syroot.Worms.Worms2
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param> /// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream) public void Save(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
// Write the header. // Write the header.
writer.Write(_signature); writer.Write(_signature);

View File

@ -272,7 +272,7 @@ namespace Syroot.Worms.Worms2
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param> /// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream) public void Save(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
// Write the header. // Write the header.
writer.Write(_signature, StringCoding.Raw); writer.Write(_signature, StringCoding.Raw);

View File

@ -82,10 +82,10 @@ namespace Syroot.Worms.Worms2
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param> /// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream) public void Save(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
// Write the header. // Write the header.
writer.WriteStructs(new byte[_trashLength]); writer.Write(new byte[_trashLength]);
writer.Write(_signature, StringCoding.ZeroTerminated); writer.Write(_signature, StringCoding.ZeroTerminated);
// Write the weapon settings. // Write the weapon settings.

View File

@ -69,7 +69,7 @@ namespace Syroot.Worms.Worms2
/// <param name="stream">The <see cref="Stream"/> to save the data to.</param> /// <param name="stream">The <see cref="Stream"/> to save the data to.</param>
public void Save(Stream stream) public void Save(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
foreach (Team team in Teams) foreach (Team team in Teams)
team.Save(writer.BaseStream); team.Save(writer.BaseStream);
} }

View File

@ -106,7 +106,7 @@ namespace Syroot.Worms
/// <param name="stream">The <see cref="Stream"/> to save the data in.</param> /// <param name="stream">The <see cref="Stream"/> to save the data in.</param>
public void Save(Stream stream) public void Save(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream); using BinaryStream writer = new BinaryStream(stream, leaveOpen: true);
// Write the header. // Write the header.
writer.Write(_signature); writer.Write(_signature);
@ -123,7 +123,7 @@ namespace Syroot.Worms
Offset = (int)writer.Position, Offset = (int)writer.Position,
Length = item.Value.Length Length = item.Value.Length
}; };
writer.WriteStructs(item.Value); writer.Write(item.Value);
int hash = CalculateHash(item.Key); int hash = CalculateHash(item.Key);
if (hashTable[hash] == null) if (hashTable[hash] == null)

View File

@ -67,7 +67,7 @@ namespace Syroot.Worms.Core.Riff
/// <param name="stream">The <see cref="Stream"/> to save the RIFF data in.</param> /// <param name="stream">The <see cref="Stream"/> to save the RIFF data in.</param>
protected void SaveRiff(Stream stream) protected void SaveRiff(Stream stream)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
// Write the header. // Write the header.
writer.Write(_signature, StringCoding.Raw); writer.Write(_signature, StringCoding.Raw);

View File

@ -180,7 +180,7 @@ namespace Syroot.Worms
/// <param name="alignData"><c>true</c> to align the data array by 4 bytes.</param> /// <param name="alignData"><c>true</c> to align the data array by 4 bytes.</param>
public void Save(Stream stream, bool compress, bool alignData) public void Save(Stream stream, bool compress, bool alignData)
{ {
using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true);
// Write the header. // Write the header.
writer.Write(_signature); writer.Write(_signature);
@ -222,7 +222,7 @@ namespace Syroot.Worms
byte[] data = Data; byte[] data = Data;
if (compress) if (compress)
data = Team17Compression.Compress(data); data = Team17Compression.Compress(data);
writer.WriteStructs(data); writer.Write(data);
writer.SatisfyOffset(fileSizeOffset, (int)writer.Position); writer.SatisfyOffset(fileSizeOffset, (int)writer.Position);
} }

View File

@ -101,6 +101,7 @@ namespace Syroot.Worms
// ---- METHODS (PRIVATE) -------------------------------------------------------------------------------------- // ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------
#pragma warning disable IDE0051 // Remove unused private members
[RiffChunkLoad("data")] [RiffChunkLoad("data")]
private void LoadDataChunk(BinaryStream reader, int length) private void LoadDataChunk(BinaryStream reader, int length)
{ {
@ -146,12 +147,13 @@ namespace Syroot.Worms
} }
[RiffChunkSave("offl")] [RiffChunkSave("offl")]
private void SaveOfflChunk(BinaryStream writer) => writer.WriteStructs(OfflData); private void SaveOfflChunk(BinaryStream writer) => writer.Write(OfflData);
[RiffChunkSave("tran")] [RiffChunkSave("tran")]
private void SaveTranChunk(BinaryStream writer) => writer.WriteStructs(TranData); private void SaveTranChunk(BinaryStream writer) => writer.Write(TranData);
[RiffChunkSave("unde")] [RiffChunkSave("unde")]
private void SaveUndeChunk(BinaryStream writer) => writer.WriteStructs(UndeData); private void SaveUndeChunk(BinaryStream writer) => writer.Write(UndeData);
#pragma warning restore IDE0051
} }
} }