Refactor a few classes to use new serialization method.

This commit is contained in:
Greg Edwards 2014-08-04 18:56:40 -04:00
parent 5323e6bb68
commit 7c115417d7
5 changed files with 311 additions and 43 deletions

View File

@ -63,6 +63,7 @@
<Compile Include="Structures\BattleVideoHeader5.cs" />
<Compile Include="Structures\BattleVideoRecord4.cs" />
<Compile Include="Structures\BattleVideoRecord5.cs" />
<Compile Include="Structures\BinarySerializableBase.cs" />
<Compile Include="Structures\BoxRecord4.cs" />
<Compile Include="Structures\DressupRecord4.cs" />
<Compile Include="Structures\GtsRecord5.cs" />

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace PkmnFoundations.Structures
{
/// <summary>
/// Base class for objects which serialize using BinaryReader and BinaryWriter
/// </summary>
public abstract class BinarySerializableBase : ISerializable
{
public BinarySerializableBase()
{
}
public BinarySerializableBase(BinaryReader data)
{
Load(data);
}
public BinarySerializableBase(byte[] data)
{
Load(data);
}
public BinarySerializableBase(byte[] data, int offset)
{
Load(data, offset);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("data", Save());
}
protected abstract void Save(BinaryWriter writer);
protected abstract void Load(BinaryReader reader);
/// <summary>
/// Size of the serialized structure in bytes
/// </summary>
public abstract int Size
{
get;
}
public byte[] Save()
{
byte[] data = new byte[Size];
Save(new BinaryWriter(new MemoryStream(data)));
return data;
}
public void Load(byte[] data, int offset)
{
if (offset + Size > data.Length) throw new ArgumentOutOfRangeException("offset");
if (offset < 0) throw new ArgumentOutOfRangeException("offset");
MemoryStream m = new MemoryStream(data, offset, Size);
Load(new BinaryReader(m));
}
public void Load(byte[] data)
{
if (Size > data.Length) throw new ArgumentException("Buffer is too small");
Load(data, 0);
}
}
}

View File

@ -1,35 +1,87 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace PkmnFoundations.Structures
{
public class BoxRecord4
public class BoxRecord4 : BinarySerializableBase
{
public BoxRecord4()
{
}
public BoxRecord4(int pid, BoxLabels4 label, ulong serial_number, byte[] data)
public BoxRecord4(int pid, BoxLabels4 label, ulong serial_number, BinaryReader data)
: base(data)
{
if (data.Length != 540) throw new ArgumentException("Box data must be 540 bytes.");
PID = pid;
Label = label;
SerialNumber = serial_number;
Data = data;
}
// todo: encapsulate these so calculated fields are always correct
public int PID;
public BoxLabels4 Label;
public ulong SerialNumber;
public byte[] Data;
public BoxRecord4(int pid, BoxLabels4 label, ulong serial_number, byte[] data)
: base(data)
{
PID = pid;
Label = label;
SerialNumber = serial_number;
}
public BoxRecord4(int pid, BoxLabels4 label, ulong serial_number, byte[] data, int offset)
: base(data, offset)
{
PID = pid;
Label = label;
SerialNumber = serial_number;
}
public int PID { get; set; }
public BoxLabels4 Label { get; set; }
public ulong SerialNumber { get; set; }
private byte[] m_data;
public byte[] Data
{
get
{
return m_data;
}
set
{
if (m_data == value) return;
if (value == null)
{
m_data = null;
return;
}
if (value.Length != 540) throw new ArgumentException("Box data must be 540 bytes.");
m_data = value.ToArray();
}
}
public override int Size
{
get
{
return 540;
}
}
protected override void Load(System.IO.BinaryReader reader)
{
m_data = reader.ReadBytes(540);
}
protected override void Save(System.IO.BinaryWriter writer)
{
writer.Write(m_data);
}
public BoxRecord4 Clone()
{
return new BoxRecord4(PID, Label, SerialNumber, Data.ToArray());
return new BoxRecord4(PID, Label, SerialNumber, Data);
}
}

View File

@ -1,29 +1,61 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace PkmnFoundations.Structures
{
public class DressupRecord4
public class DressupRecord4 : BinarySerializableBase
{
public DressupRecord4()
{
}
public DressupRecord4(int pid, ulong serial_number, byte[] data)
public DressupRecord4(int pid, ulong serial_number, BinaryReader data)
: base(data)
{
if (data.Length != 224) throw new ArgumentException("Dressup data must be 224 bytes.");
PID = pid;
SerialNumber = serial_number;
Data = data;
}
// todo: encapsulate these so calculated fields are always correct
public int PID;
public ulong SerialNumber;
public byte[] Data;
public DressupRecord4(int pid, ulong serial_number, byte[] data)
: base(data)
{
PID = pid;
SerialNumber = serial_number;
}
public DressupRecord4(int pid, ulong serial_number, byte[] data, int offset)
: base(data, offset)
{
PID = pid;
SerialNumber = serial_number;
}
public int PID { get; set; }
public ulong SerialNumber { get; set; }
private byte[] m_data;
public byte[] Data
{
get
{
return m_data;
}
set
{
if (m_data == value) return;
if (value == null)
{
m_data = null;
return;
}
if (value.Length != 224) throw new ArgumentException("Dressup data must be 224 bytes.");
m_data = value.ToArray();
}
}
public ushort Species
{
@ -33,9 +65,27 @@ namespace PkmnFoundations.Structures
}
}
public override int Size
{
get
{
return 224;
}
}
protected override void Load(System.IO.BinaryReader reader)
{
m_data = reader.ReadBytes(224);
}
protected override void Save(System.IO.BinaryWriter writer)
{
writer.Write(m_data);
}
public DressupRecord4 Clone()
{
return new DressupRecord4(PID, SerialNumber, Data.ToArray());
return new DressupRecord4(PID, SerialNumber, Data);
}
}
}

View File

@ -1,29 +1,41 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace PkmnFoundations.Structures
{
public class MusicalRecord5
public class MusicalRecord5 : BinarySerializableBase
{
public MusicalRecord5()
{
}
public MusicalRecord5(int pid, ulong serial_number, byte[] data)
public MusicalRecord5(int pid, ulong serial_number, BinaryReader data)
: base(data)
{
PID = pid;
SerialNumber = serial_number;
}
public MusicalRecord5(int pid, ulong serial_number, byte[] data)
: base(data)
{
PID = pid;
SerialNumber = serial_number;
}
public MusicalRecord5(int pid, ulong serial_number, byte[] data, int offset)
: base(data, offset)
{
if (data.Length != 560) throw new ArgumentException("Musical data must be 560 bytes.");
PID = pid;
SerialNumber = serial_number;
Data = data;
UpdateParticipants();
}
// todo: encapsulate these so calculated fields are always correct
public int PID;
public ulong SerialNumber;
public int PID { get; set; }
public ulong SerialNumber { get; set; }
private byte[] m_data;
public byte[] Data
@ -36,51 +48,110 @@ namespace PkmnFoundations.Structures
set
{
if (m_data == value) return;
if (value.Length != 560) throw new ArgumentException("Musical data must be 560 bytes.");
if (value == null)
{
m_data = null;
return;
}
m_data = value;
if (value.Length != 560) throw new ArgumentException("Musical data must be 560 bytes.");
m_data = value.ToArray();
UpdateParticipants();
}
}
public MusicalParticipant5[] Participants;
private MusicalParticipant5[] m_participants;
public MusicalParticipant5[] Participants
{
get
{
// fixme: Data doesn't update if the consumer modifies this array.
// todo: Split out participants from main data.
return m_participants;
}
}
public void UpdateParticipants()
{
Participants = new MusicalParticipant5[4];
if (m_data.Length != 560) throw new InvalidOperationException("Musical data must be 560 bytes.");
if (m_data == null)
{
m_participants = null;
return;
}
m_participants = new MusicalParticipant5[4];
for (int x = 0; x < 4; x++)
{
Participants[x] = new MusicalParticipant5(m_data, x * 0x58 + 0x84);
}
}
public override int Size
{
get
{
return 560;
}
}
protected override void Load(System.IO.BinaryReader reader)
{
m_data = reader.ReadBytes(560);
UpdateParticipants();
}
protected override void Save(System.IO.BinaryWriter writer)
{
writer.Write(m_data);
}
public MusicalRecord5 Clone()
{
return new MusicalRecord5(PID, SerialNumber, Data.ToArray());
return new MusicalRecord5(PID, SerialNumber, Data);
}
}
public class MusicalParticipant5
public class MusicalParticipant5 : BinarySerializableBase
{
public MusicalParticipant5()
{
}
public MusicalParticipant5(BinaryReader data)
: base(data)
{
}
public MusicalParticipant5(byte[] data)
: base(data)
{
if (data.Length != 88) throw new ArgumentException("Musical Participant data must be 88 bytes.");
Data = data;
}
public MusicalParticipant5(byte[] buffer, int offset)
public MusicalParticipant5(byte[] data, int offset)
: base(data, offset)
{
if (buffer.Length - offset < 88) throw new ArgumentException("Not enough room in buffer to copy 88 bytes of Musical Participant data.");
Data = new byte[88];
Array.Copy(buffer, offset, Data, 0, 88);
}
public byte[] Data;
private byte[] m_data;
public byte[] Data
{
get
{
return m_data;
}
set
{
if (m_data == value) return;
if (value == null)
{
m_data = null;
return;
}
if (value.Length != 88) throw new ArgumentException("Musical Participant data must be 88 bytes.");
m_data = value.ToArray();
}
}
public ushort Species
{
@ -89,5 +160,28 @@ namespace PkmnFoundations.Structures
return BitConverter.ToUInt16(Data, 0);
}
}
public override int Size
{
get
{
return 88;
}
}
protected override void Load(System.IO.BinaryReader reader)
{
m_data = reader.ReadBytes(88);
}
protected override void Save(System.IO.BinaryWriter writer)
{
writer.Write(m_data);
}
public MusicalParticipant5 Clone()
{
return new MusicalParticipant5(Data);
}
}
}