-
Notifications
You must be signed in to change notification settings - Fork 324
/
SaveTreeBaseRecord.cs
286 lines (230 loc) · 8.01 KB
/
SaveTreeBaseRecord.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
using System;
using System.IO;
using System.Collections.Generic;
namespace DaggerfallConnect.Save
{
/// <summary>
/// Base record of SAVETREE.DAT.
/// </summary>
public class SaveTreeBaseRecord
{
#region Fields
// Constants
public const int RecordRootLength = 71;
public const int LightDataLengthMultiplier = 39;
// Source data from file stream
protected long streamPosition;
protected int streamLength;
protected byte[] streamData;
// RecordBase data
protected RecordTypes recordType;
protected RecordRoot recordRoot = new RecordRoot();
// Tree management
protected SaveTreeBaseRecord parent;
protected List<SaveTreeBaseRecord> children = new List<SaveTreeBaseRecord>();
protected bool failedRecord = false;
#endregion
#region Properties
/// <summary>
/// Gets position of data in source file stream.
/// </summary>
public long StreamPosition
{
get { return streamPosition; }
}
/// <summary>
/// Gets length of raw data in source file stream.
/// </summary>
public int StreamLength
{
get { return streamLength; }
}
/// <summary>
/// Gets raw data as read from file stream.
/// </summary>
public byte[] StreamData
{
get { return streamData; }
}
/// <summary>
/// Gets type of this record.
/// </summary>
public RecordTypes RecordType
{
get { return recordType; }
}
/// <summary>
/// Gets or sets RecordRoot data of this record.
/// </summary>
public RecordRoot RecordRoot
{
get { return recordRoot; }
set { recordRoot = value; }
}
/// <summary>
/// Gets length of actual record data, excluding RecordRoot header.
/// </summary>
public int RecordLength
{
get { return (streamLength > 0) ? streamLength - RecordRootLength : 0; }
}
/// <summary>
/// Gets or sets actual record data, excluding RecordRoot header.
/// </summary>
public byte[] RecordData
{
get { return GetRecordData(); }
set { SetRecordData(value); }
}
/// <summary>
/// Gets or sets parent of this record.
/// </summary>
public SaveTreeBaseRecord Parent
{
get { return parent; }
set { parent = value; }
}
/// <summary>
/// Gets children of this record.
/// </summary>
public List<SaveTreeBaseRecord> Children
{
get { return children; }
}
/// <summary>
/// True if record failed to read (e.g. end of stream reached unexpectedly).
/// </summary>
public bool IsFailedRecord
{
get { return failedRecord; }
}
#endregion
#region Constructors
/// <summary>
/// Constructor.
/// </summary>
public SaveTreeBaseRecord()
{
}
/// <summary>
/// Reader constructor.
/// </summary>
/// <param name="reader">Reader positioned at start of record data.</param>
/// <param name="length">Length of data record to read.</param>
public SaveTreeBaseRecord(BinaryReader reader, int length)
{
Open(reader, length);
}
#endregion
#region Public Methods
/// <summary>
/// Reads record data from stream.
/// </summary>
/// <param name="reader">Reader positioned at start of record data.</param>
/// <param name="length">Length of record data to read.</param>
public virtual void Open(BinaryReader reader, int length)
{
streamPosition = reader.BaseStream.Position;
streamLength = length;
// Cannot read zero-length records
if (length <= 0)
return;
// Peek record type and adjust for light size
recordType = SaveTree.PeekRecordType(reader);
if (recordType == RecordTypes.Light)
streamLength *= LightDataLengthMultiplier;
// Read raw record data
streamData = reader.ReadBytes(streamLength);
// Read RecordRoot data from start of memory buffer
ReadRecordRoot();
}
/// <summary>
/// Shallow copy record data from this record to another.
/// </summary>
/// <param name="other">Other record to receive data.</param>
public virtual void CopyTo(SaveTreeBaseRecord other)
{
if (other == null)
return;
other.streamPosition = this.streamPosition;
other.streamLength = this.streamLength;
other.streamData = (byte[])this.streamData.Clone();
other.recordType = this.recordType;
other.recordRoot = this.recordRoot;
other.parent = this.parent;
other.children.AddRange(this.children.ToArray());
}
#endregion
#region Private Methods
byte[] GetRecordData()
{
byte[] data = new byte[RecordLength];
Array.Copy(streamData, RecordRootLength, data, 0, RecordLength);
return data;
}
void SetRecordData(byte[] data)
{
if (data.Length != RecordLength)
throw new Exception("New record data length != original record data length.");
Array.Copy(data, 0, streamData, RecordRootLength, RecordLength);
}
void ReadRecordRoot()
{
MemoryStream stream = new MemoryStream(streamData);
BinaryReader reader = new BinaryReader(stream);
// Must have RecordRootLength of bytes to read or something has gone wrong
if (stream.Length < RecordRootLength)
{
failedRecord = true;
stream.Close();
return;
}
// Direction
reader.BaseStream.Position = 1;
recordRoot.Pitch = reader.ReadInt16();
recordRoot.Yaw = reader.ReadInt16();
recordRoot.Roll = reader.ReadInt16();
// Position
recordRoot.Position = SaveTree.ReadPosition(reader);
// 3d View Picture
reader.BaseStream.Position = 27;
recordRoot.SpriteIndex = reader.ReadUInt16();
// Inventory Picture
recordRoot.Picture2 = reader.ReadUInt16();
// RecordID
recordRoot.RecordID = reader.ReadUInt32();
// QuestID
reader.BaseStream.Position = 38;
recordRoot.QuestID = reader.ReadByte();
// ParentRecordID
recordRoot.ParentRecordID = reader.ReadUInt32();
// Time
reader.BaseStream.Position = 43;
recordRoot.Time = reader.ReadUInt32();
// ItemObject
recordRoot.ItemObject = reader.ReadUInt32();
// QuestObjectID
recordRoot.QuestObjectID = reader.ReadUInt32();
// NextObject
recordRoot.NextObject = reader.ReadUInt32();
// ChildObject
recordRoot.ChildObject = reader.ReadUInt32();
// SublistHead
recordRoot.SublistHead = reader.ReadUInt32();
// ParentRecordType
recordRoot.ParentRecordType = (RecordTypes)reader.ReadInt32();
reader.Close();
}
#endregion
}
}