Skip to content

Commit f83c60c

Browse files
committed
Implemented TES4 file header record
Wrote ESPReader and ESPWriter which inherit from BinaryReader and BinaryWriter Modified how generic records write out there subrecords to XML Added simple subrecord class to hold simple data like byte[] or int
1 parent 400567e commit f83c60c

21 files changed

Lines changed: 651 additions & 81 deletions

ESPSharp/ESPReader.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.IO;
7+
8+
namespace ESPSharp
9+
{
10+
public class ESPReader : BinaryReader
11+
{
12+
public ESPReader(Stream stream)
13+
: base(stream, Utility.WesternEncoding) { }
14+
15+
public string ReadTag()
16+
{
17+
return Utility.SanitizeTag(new string(ReadChars(4)));
18+
}
19+
20+
public string PeekTag()
21+
{
22+
string peek = ReadTag();
23+
BaseStream.Seek(-4, SeekOrigin.Current);
24+
25+
return peek;
26+
}
27+
28+
public string ReadNullTermString(int length)
29+
{
30+
string outString = new string(ReadChars(length - 1));
31+
ReadByte();
32+
return outString;
33+
}
34+
35+
public T ReadSimpleSubrecord<T>()
36+
{
37+
throw new NotImplementedException();
38+
}
39+
}
40+
}

ESPSharp/ESPSharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
</ItemGroup>
4444
<ItemGroup>
4545
<Compile Include="EnumsAndFlags\GameSettingType.cs" />
46+
<Compile Include="ESPReader.cs" />
47+
<Compile Include="ESPWriter.cs" />
4648
<Compile Include="Groups\CellGroup.cs" />
4749
<Compile Include="Groups\CellPersistentChildren.cs" />
4850
<Compile Include="Groups\CellTemporaryChildren.cs" />
@@ -68,6 +70,7 @@
6870
<Compile Include="Records\GameSetting.cs" />
6971
<Compile Include="Subrecord.cs" />
7072
<Compile Include="Subrecords\PluginHeader.cs" />
73+
<Compile Include="Subrecords\SimpleSubrecord.cs" />
7174
<Compile Include="Subrecords\UndecodedSubrecord.cs" />
7275
<Compile Include="Utility.cs" />
7376
<Compile Include="Zlib.cs" />

ESPSharp/ESPWriter.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.IO;
7+
8+
namespace ESPSharp
9+
{
10+
public class ESPWriter : BinaryWriter
11+
{
12+
public ESPWriter(Stream stream)
13+
:base(stream, Utility.WesternEncoding){}
14+
15+
public void WriteTag(string Tag)
16+
{
17+
Write(Utility.DesanitizeTag(Tag));
18+
}
19+
}
20+
}

ESPSharp/ElderScrollsPlugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public static ElderScrollsPlugin ReadXML(string sourceFolder)
4040
return outPlug;
4141
}
4242

43-
public void WriteBinary(BinaryWriter writer)
43+
public void WriteBinary(ESPWriter writer)
4444
{
4545
Header.WriteBinary(writer);
4646

4747
foreach (var group in Groups)
4848
group.WriteBinary(writer);
4949
}
5050

51-
public void ReadBinary(BinaryReader reader)
51+
public void ReadBinary(ESPReader reader)
5252
{
5353
Header = Record.CreateRecord(reader);
5454
Header.ReadBinary(reader);

ESPSharp/Extensions.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public static FormID ToFormID(this XElement ele)
2929
return uint.Parse(ele.Value, System.Globalization.NumberStyles.HexNumber);
3030
}
3131

32+
public static ulong ToUInt64(this XElement ele)
33+
{
34+
return ulong.Parse(ele.Value);
35+
}
36+
37+
public static long ToInt64(this XElement ele)
38+
{
39+
return long.Parse(ele.Value);
40+
}
41+
3242
public static uint ToUInt32(this XElement ele)
3343
{
3444
return uint.Parse(ele.Value);
@@ -54,6 +64,11 @@ public static byte ToByte(this XElement ele)
5464
return byte.Parse(ele.Value);
5565
}
5666

67+
public static char ToChar(this XElement ele)
68+
{
69+
return char.Parse(ele.Value);
70+
}
71+
5772
public static sbyte ToSByte(this XElement ele)
5873
{
5974
return sbyte.Parse(ele.Value);
@@ -68,26 +83,20 @@ public static byte[] ToBytes(this XElement ele)
6883
{
6984
return Convert.FromBase64String(ele.Value);
7085
}
71-
#endregion
72-
73-
#region BinaryReaderExtensions
74-
public static string PeekTag(this BinaryReader reader)
75-
{
76-
string peek = reader.ReadTag();
77-
reader.BaseStream.Seek(-4, SeekOrigin.Current);
78-
79-
return peek;
80-
}
8186

82-
public static string ReadTag(this BinaryReader reader)
87+
public static char[] ToChars(this XElement ele)
8388
{
84-
return Utility.SanitizeTag(new string(reader.ReadChars(4)));
89+
return ele.Value.ToArray();
8590
}
8691
#endregion
8792

8893
public static string ToBase64(this byte[] bytes)
8994
{
9095
return Convert.ToBase64String(bytes);
9196
}
97+
public static string ToHex(this byte[] bytes)
98+
{
99+
return bytes.ToBase64();
100+
}
92101
}
93102
}

ESPSharp/Group.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static Group ReadXML(string sourceFolder)
6666
return outGroup;
6767
}
6868

69-
public void WriteBinary(BinaryWriter writer)
69+
public void WriteBinary(ESPWriter writer)
7070
{
7171
writer.Write(Utility.DesanitizeTag(Tag).ToCharArray());
7272

@@ -103,7 +103,7 @@ public void WriteBinary(BinaryWriter writer)
103103
writer.BaseStream.Seek(dataEnd, SeekOrigin.Begin);
104104
}
105105

106-
public void ReadBinary(BinaryReader reader)
106+
public void ReadBinary(ESPReader reader)
107107
{
108108
Tag = reader.ReadTag();
109109
Size = reader.ReadUInt32() - 24;
@@ -131,8 +131,8 @@ public void ReadBinary(BinaryReader reader)
131131
}
132132
}
133133

134-
public abstract void WriteTypeData(BinaryWriter writer);
135-
public abstract void ReadTypeData(BinaryReader reader);
134+
public abstract void WriteTypeData(ESPWriter writer);
135+
public abstract void ReadTypeData(ESPReader reader);
136136
public abstract XElement WriteTypeDataXML();
137137
public abstract void ReadTypeDataXML(XElement element);
138138

@@ -183,7 +183,7 @@ public static Group CreateGroup(GroupType type)
183183
return outGroup;
184184
}
185185

186-
public static Group CreateGroup(BinaryReader reader)
186+
public static Group CreateGroup(ESPReader reader)
187187
{
188188
reader.BaseStream.Seek(12, SeekOrigin.Current);
189189

ESPSharp/Groups/CellGroup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public CellGroup()
1313
type = GroupType.CellGroup;
1414
}
1515

16-
public override void WriteTypeData(BinaryWriter writer)
16+
public override void WriteTypeData(ESPWriter writer)
1717
{
1818
writer.Write(Cell);
1919
}
2020

21-
public override void ReadTypeData(BinaryReader reader)
21+
public override void ReadTypeData(ESPReader reader)
2222
{
2323
Cell = reader.ReadUInt32();
2424
}

ESPSharp/Groups/ExteriorCellBlock.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ public ExteriorCellBlock()
1414
type = GroupType.ExteriorCellBlock;
1515
}
1616

17-
public override void WriteTypeData(BinaryWriter writer)
17+
public override void WriteTypeData(ESPWriter writer)
1818
{
1919
writer.Write(CoordY);
2020
writer.Write(CoordX);
2121
}
2222

23-
public override void ReadTypeData(BinaryReader reader)
23+
public override void ReadTypeData(ESPReader reader)
2424
{
2525
CoordY = reader.ReadInt16();
2626
CoordX = reader.ReadInt16();

ESPSharp/Groups/InteriorCellBlock.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public InteriorCellBlock()
1313
type = GroupType.InteriorCellBlock;
1414
}
1515

16-
public override void WriteTypeData(BinaryWriter writer)
16+
public override void WriteTypeData(ESPWriter writer)
1717
{
1818
writer.Write(Index);
1919
}
2020

21-
public override void ReadTypeData(BinaryReader reader)
21+
public override void ReadTypeData(ESPReader reader)
2222
{
2323
Index = reader.ReadInt32();
2424
}

ESPSharp/Groups/TopGroup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public TopGroup()
1313
type = GroupType.TopGroup;
1414
}
1515

16-
public override void WriteTypeData(BinaryWriter writer)
16+
public override void WriteTypeData(ESPWriter writer)
1717
{
1818
writer.Write(RecordType.ToCharArray());
1919
}
2020

21-
public override void ReadTypeData(BinaryReader reader)
21+
public override void ReadTypeData(ESPReader reader)
2222
{
2323
RecordType = reader.ReadTag();
2424
}

0 commit comments

Comments
 (0)