Skip to content

Commit b625eee

Browse files
committed
Experimental BDBD support
1 parent 2fe3b07 commit b625eee

File tree

5 files changed

+110
-12
lines changed

5 files changed

+110
-12
lines changed

DBCD.Tests/ReadingTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public void TestWDC5Reading()
8181
Assert.AreEqual("Dragon Isles", row["MapName_lang"]);
8282
}
8383

84+
[TestMethod]
85+
public void TestWDC5ReadingBDBDNoCache()
86+
{
87+
DBCD dbcd = new(wagoDBCProvider, GithubBDBDProvider.GetStream(true));
88+
IDBCDStorage storage = dbcd.Load("Map", "10.2.5.52432");
89+
90+
var row = storage[2574];
91+
Assert.AreEqual("Dragon Isles", row["MapName_lang"]);
92+
}
93+
8494
[TestMethod]
8595
public void TestSparseReading()
8696
{
@@ -110,7 +120,7 @@ public void TestEncryptedInfo()
110120

111121
foreach (var section in storage.GetEncryptedSections())
112122
{
113-
System.Console.WriteLine($"Found encrypted section encrypted with key {section.Key} containing {section.Value} rows");
123+
Console.WriteLine($"Found encrypted section encrypted with key {section.Key} containing {section.Value} rows");
114124
}
115125
}
116126

DBCD/DBCD.cs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using DBCD.Providers;
21
using DBCD.IO;
2+
using DBCD.Providers;
3+
using DBDefsLib;
34
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
47

58
namespace DBCD
69
{
@@ -9,21 +12,65 @@ public class DBCD
912
{
1013
private readonly IDBCProvider dbcProvider;
1114
private readonly IDBDProvider dbdProvider;
15+
16+
private readonly bool useBDBD;
17+
private readonly Dictionary<string, Structs.TableInfo> BDBDCache;
18+
19+
/// <summary>
20+
/// Creates a DBCD instance that uses the given DBC and DBD providers.
21+
/// </summary>
22+
/// <param name="dbcProvider">The IDBCProvider for DBC files.</param>
23+
/// <param name="dbdProvider">The IDBDProvider for DBD files.</param>
1224
public DBCD(IDBCProvider dbcProvider, IDBDProvider dbdProvider)
1325
{
1426
this.dbcProvider = dbcProvider;
1527
this.dbdProvider = dbdProvider;
28+
this.useBDBD = false;
1629
}
1730

31+
/// <summary>
32+
/// Creates a DBCD instance that uses the given DBC provider and BDBD stream.
33+
/// </summary>
34+
/// <param name="dbcProvider">The IDBCProvider for DBC files.</param>
35+
/// <param name="bdbdStream">The stream for a BDBD (Binary DBD) file to load all definitions from.</param>
36+
public DBCD(IDBCProvider dbcProvider, Stream bdbdStream)
37+
{
38+
this.dbcProvider = dbcProvider;
39+
this.useBDBD = true;
40+
this.BDBDCache = BDBDReader.Read(bdbdStream);
41+
}
42+
43+
/// <summary>
44+
/// Loads a table by its name, and optionally build/locale.
45+
/// </summary>
46+
/// <param name="tableName">The name of the DBC/DB2 table to load.</param>
47+
/// <param name="build">The source build of the table formatted as x.x.x.xxxxx (optional, recommended in general but required for tables older than Legion).</param>
48+
/// <param name="locale">The locale to use (optional, recommended for DBC files from WotLK or older).</param>
49+
/// <returns>An instance of <see cref="IDBCDStorage"/> representing the loaded table.</returns>
1850
public IDBCDStorage Load(string tableName, string build = null, Locale locale = Locale.None)
1951
{
2052
var dbcStream = this.dbcProvider.StreamForTableName(tableName, build);
21-
var dbdStream = this.dbdProvider.StreamForTableName(tableName, build);
53+
54+
Structs.DBDefinition databaseDefinition;
55+
56+
if (!useBDBD)
57+
{
58+
var dbdStream = this.dbdProvider.StreamForTableName(tableName, build);
59+
var dbdReader = new DBDReader();
60+
databaseDefinition = dbdReader.Read(dbdStream);
61+
}
62+
else
63+
{
64+
if (!BDBDCache.TryGetValue(tableName, out var tableInfo))
65+
throw new FileNotFoundException($"Table {tableName} not found in BDBD.");
66+
67+
databaseDefinition = tableInfo.dbd;
68+
}
2269

2370
var builder = new DBCDBuilder(locale);
2471

2572
var dbReader = new DBParser(dbcStream);
26-
var definition = builder.Build(dbReader, dbdStream, tableName, build);
73+
var definition = builder.Build(dbReader, databaseDefinition, tableName, build);
2774

2875
var type = typeof(DBCDStorage<>).MakeGenericType(definition.Item1);
2976

DBCD/DBCDBuilder.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,10 @@ internal DBCDBuilder(Locale locale = Locale.None)
3535
this.locale = locale;
3636
}
3737

38-
internal Tuple<Type, DBCDInfo> Build(DBParser dbcReader, Stream dbd, string name, string build)
38+
internal Tuple<Type, DBCDInfo> Build(DBParser dbcReader, Structs.DBDefinition databaseDefinition, string name, string build)
3939
{
40-
var dbdReader = new DBDReader();
41-
4240
if (name == null)
43-
{
4441
name = Guid.NewGuid().ToString();
45-
}
46-
47-
var databaseDefinition = dbdReader.Read(dbd);
4842

4943
Structs.VersionDefinitions? versionDefinition = null;
5044

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Http;
4+
5+
namespace DBCD.Providers
6+
{
7+
public static class GithubBDBDProvider
8+
{
9+
public static string BDBDUrl = "https://github.com/wowdev/WoWDBDefs/releases/latest/download/all.bdbd";
10+
11+
private static string CachePath { get; } = "BDBDCache/";
12+
private static readonly TimeSpan CacheExpiryTime = new TimeSpan(1, 0, 0, 0);
13+
14+
public static Stream GetStream(bool forceNew = false)
15+
{
16+
var currentFile = Path.Combine(CachePath, "all.bdbd");
17+
if (File.Exists(currentFile))
18+
{
19+
var fileInfo = new FileInfo(currentFile);
20+
if (fileInfo.Length == 0)
21+
{
22+
File.Delete(currentFile);
23+
}
24+
else
25+
{
26+
if (!forceNew && DateTime.Now - fileInfo.LastWriteTime < CacheExpiryTime)
27+
return new MemoryStream(File.ReadAllBytes(currentFile));
28+
}
29+
}
30+
31+
if (!Directory.Exists(CachePath))
32+
Directory.CreateDirectory(CachePath);
33+
34+
var bdbdStream = new MemoryStream();
35+
using (var fileStream = new FileStream(currentFile, FileMode.Create, FileAccess.Write))
36+
using (var client = new HttpClient())
37+
{
38+
var response = client.GetAsync(BDBDUrl).Result;
39+
response.EnsureSuccessStatusCode();
40+
response.Content.CopyToAsync(bdbdStream).Wait();
41+
bdbdStream.CopyTo(fileStream);
42+
bdbdStream.Position = 0;
43+
}
44+
return bdbdStream;
45+
}
46+
}
47+
}

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<DebugType>embedded</DebugType>
44
<LangVersion>latest</LangVersion>
5-
<Version>2.1.2</Version>
5+
<Version>2.1.3</Version>
66
</PropertyGroup>
77
<PropertyGroup>
88
<Authors>WoWDev</Authors>

0 commit comments

Comments
 (0)