/
ContentReader.cs
364 lines (311 loc) · 12.7 KB
/
ContentReader.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2022 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 UnityEngine;
using System.Collections.Generic;
using System.IO;
using DaggerfallConnect;
using DaggerfallConnect.Arena2;
using System;
namespace DaggerfallWorkshop.Utility
{
/// <summary>
/// General interface between DaggerfallConnect API reader classes and Unity.
/// </summary>
public class ContentReader
{
bool isReady = false;
string arena2Path;
BlocksFile blockFileReader;
MapsFile mapFileReader;
MonsterFile monsterFileReader;
WoodsFile woodsFileReader;
FactionFile factionFileReader;
FlatsFile flatsFileReader;
PaintFile paintFileReader;
Dictionary<int, MapSummary> mapDict;
Dictionary<int, int> locationIdToMapIdDict;
public struct MapSummary
{
public int ID; // mapTable.MapId & 0x000fffff for dict key and matching with ExteriorData.MapId
public int MapID; // Full mapTable.MapId for matching with localization key
public int RegionIndex;
public int MapIndex;
public DFRegion.LocationTypes LocationType;
public DFRegion.DungeonTypes DungeonType;
public bool Discovered;
}
public bool IsReady
{
get { return isReady; }
}
public BlocksFile BlockFileReader
{
get { return blockFileReader; }
}
public MapsFile MapFileReader
{
get { return mapFileReader; }
}
public MonsterFile MonsterFileReader
{
get { return monsterFileReader; }
}
public WoodsFile WoodsFileReader
{
get { return woodsFileReader; }
}
public FactionFile FactionFileReader
{
get { return factionFileReader; }
}
public FlatsFile FlatsFileReader
{
get { return flatsFileReader; }
}
public PaintFile PaintFileReader
{
get { return paintFileReader; }
}
#region Constructors
public ContentReader(string arena2Path)
{
this.arena2Path = arena2Path;
SetupReaders();
}
#endregion
#region Blocks & Locations
/// <summary>
/// Attempts to get a Daggerfall block from BLOCKS.BSA.
/// </summary>
/// <param name="name">Name of block.</param>
/// <param name="blockOut">DFBlock data out.</param>
/// <returns>True if successful.</returns>
public bool GetBlock(string name, out DFBlock blockOut)
{
blockOut = new DFBlock();
if (!isReady)
return false;
// Get block data
blockOut = blockFileReader.GetBlock(name);
if (blockOut.Type == DFBlock.BlockTypes.Unknown)
{
DaggerfallUnity.LogMessage(string.Format("Unknown block '{0}'.", name), true);
return false;
}
return true;
}
/// <summary>
/// Attempts to get a Daggerfall location from MAPS.BSA.
/// </summary>
/// <param name="regionIndex">Index of region.</param>
/// <param name="locationIndex">Index of location.</param>
/// <param name="locationOut">DFLocation data out.</param>
/// <returns>True if successful.</returns>
public bool GetLocation(int regionIndex, int locationIndex, out DFLocation locationOut)
{
locationOut = new DFLocation();
if (!isReady)
return false;
// Get location data
locationOut = mapFileReader.GetLocation(regionIndex, locationIndex);
if (!locationOut.Loaded)
{
DaggerfallUnity.LogMessage(string.Format("Unknown location RegionIndex='{0}', LocationIndex='{1}'.", regionIndex, locationIndex), true);
return false;
}
return true;
}
/// <summary>
/// Attempts to get a Daggerfall location from MAPS.BSA.
/// </summary>
/// <param name="regionName">Name of region.</param>
/// <param name="locationName">Name of location.</param>
/// <param name="locationOut">DFLocation data out.</param>
/// <returns>True if successful.</returns>
public bool GetLocation(string regionName, string locationName, out DFLocation locationOut)
{
locationOut = new DFLocation();
if (!isReady)
return false;
// Get location data
locationOut = mapFileReader.GetLocation(regionName, locationName);
if (!locationOut.Loaded)
{
DaggerfallUnity.LogMessage(string.Format("Unknown location RegionName='{0}', LocationName='{1}'.", regionName, locationName), true);
return false;
}
return true;
}
/// <summary>
/// Attempts to get a Daggerfall location from MAPS.BSA using a locationId from quest system.
/// The locationId is different to the mapId, which is derived from location coordinates in world.
/// At this time, best known way to determine locationId is from LocationRecordElementHeader data.
/// This is linked to mapId at in EnumerateMaps().
/// Note: Not all locations have a locationId, only certain key locations
/// </summary>
/// <param name="locationId">LocationId of map from quest system.</param>
/// <param name="locationOut">DFLocation data out.</param>
/// <returns>True if successful.</returns>
public bool GetQuestLocation(int locationId, out DFLocation locationOut)
{
locationOut = new DFLocation();
if (!isReady)
return false;
MapDictCheck();
// Get mapId from locationId
int mapId = LocationIdToMapId(locationId);
if (mapDict.ContainsKey(mapId))
{
MapSummary summary = mapDict[mapId];
return GetLocation(summary.RegionIndex, summary.MapIndex, out locationOut);
}
return false;
}
/// <summary>
/// Determines if the current WorldCoord has a location.
/// </summary>
/// <param name="mapPixelX">Map pixel X.</param>
/// <param name="mapPixelY">Map pixel Y.</param>
/// <returns>True if there is a location at this map pixel.</returns>
public bool HasLocation(int mapPixelX, int mapPixelY, out MapSummary summaryOut)
{
if (!isReady)
{
summaryOut = new MapSummary();
return false;
}
MapDictCheck();
int id = MapsFile.GetMapPixelID(mapPixelX, mapPixelY);
if (mapDict.ContainsKey(id))
{
summaryOut = mapDict[id];
return true;
}
summaryOut = new MapSummary();
return false;
}
/// <summary>
/// Determines if the current WorldCoord has a location.
/// </summary>
/// <param name="mapPixelX">Map pixel X.</param>
/// <param name="mapPixelY">Map pixel Y.</param>
/// <returns>True if there is a location at this map pixel.</returns>
public bool HasLocation(int mapPixelX, int mapPixelY)
{
if (!isReady)
{
return false;
}
MapDictCheck();
int id = MapsFile.GetMapPixelID(mapPixelX, mapPixelY);
if (mapDict.ContainsKey(id))
{
return true;
}
return false;
}
/// <summary>
/// Converts LocationId from quest system to a MapId for map lookups.
/// </summary>
/// <param name="locationId">LocationId from quest system.</param>
/// <returns>MapId if present or -1.</returns>
public int LocationIdToMapId(int locationId)
{
if (locationIdToMapIdDict.ContainsKey(locationId))
{
return locationIdToMapIdDict[locationId];
}
return -1;
}
/// <summary>
/// Gets path to FACTION.TXT file in StreamingAssets/Factions folder.
/// </summary>
/// <returns>Full path to file.</returns>
public string GetFactionFilePath()
{
string path = Path.Combine(Application.streamingAssetsPath, "Factions");
return Path.Combine(path, FactionFile.Filename);
}
#endregion
#region Private Methods
/// <summary>
/// Setup API file readers.
/// </summary>
private void SetupReaders()
{
// Try to setup Arena2-dependent content readers
if (blockFileReader == null)
blockFileReader = new BlocksFile(Path.Combine(arena2Path, BlocksFile.Filename), FileUsage.UseMemory, true);
if (mapFileReader == null)
mapFileReader = new MapsFile(Path.Combine(arena2Path, MapsFile.Filename), FileUsage.UseMemory, true);
if (monsterFileReader == null)
monsterFileReader = new MonsterFile(Path.Combine(arena2Path, MonsterFile.Filename), FileUsage.UseMemory, true);
if (woodsFileReader == null)
woodsFileReader = new WoodsFile(Path.Combine(arena2Path, WoodsFile.Filename), FileUsage.UseMemory, true);
if (factionFileReader == null)
factionFileReader = new FactionFile(GetFactionFilePath(), FileUsage.UseMemory, true);
if (flatsFileReader == null)
flatsFileReader = new FlatsFile(Path.Combine(arena2Path, FlatsFile.Filename), FileUsage.UseMemory, true);
if (paintFileReader == null)
paintFileReader = new PaintFile(Path.Combine(arena2Path, PaintFile.Filename), FileUsage.UseMemory, true);
// Raise ready flag
isReady = true;
}
private void MapDictCheck()
{
// Build map lookup dictionary
if (mapDict == null && mapFileReader != null)
EnumerateMaps();
}
/// <summary>
/// Build dictionary of locations.
/// </summary>
private void EnumerateMaps()
{
//System.Diagnostics.Stopwatch s = System.Diagnostics.Stopwatch.StartNew();
//long startTime = s.ElapsedMilliseconds;
mapDict = new Dictionary<int, MapSummary>();
locationIdToMapIdDict = new Dictionary<int, int>();
for (int region = 0; region < mapFileReader.RegionCount; region++)
{
DFRegion dfRegion = mapFileReader.GetRegion(region);
for (int location = 0; location < dfRegion.LocationCount; location++)
{
MapSummary summary = new MapSummary();
try
{
// Get map summary
DFRegion.RegionMapTable mapTable = dfRegion.MapTable[location];
summary.ID = mapTable.MapId & 0x000fffff;
summary.MapID = mapTable.MapId;
summary.RegionIndex = region;
summary.MapIndex = location;
summary.LocationType = mapTable.LocationType;
summary.DungeonType = mapTable.DungeonType;
// TODO: This by itself doesn't account for DFRegion.LocationTypes.GraveyardForgotten locations that start the game discovered in classic
summary.Discovered = mapTable.Discovered;
mapDict.Add(summary.ID, summary);
// Link locationId with mapId - adds ~25ms overhead
int locationId = mapFileReader.ReadLocationIdFast(region, location);
locationIdToMapIdDict.Add(locationId, summary.ID);
}
catch (ArgumentException)
{
Debug.LogErrorFormat("Colliding location for MapId:{0} found when enumerating maps! Unable to initialise content reader. ", summary.ID);
}
}
}
//long totalTime = s.ElapsedMilliseconds - startTime;
//Debug.LogFormat("Total time to enum maps: {0}ms", totalTime);
}
#endregion
}
}