Skip to content

Commit

Permalink
Introduce SL DB2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
funjoker authored and BAndysc committed Nov 30, 2021
1 parent 8eb705f commit 9545803
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 182 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "Prism.Avalonia"]
path = Prism.Avalonia
url = https://github.com/BAndysc/Prism.Avalonia
[submodule "DBCD"]
path = DBCD
url = https://github.com/wowdev/DBCD
1 change: 1 addition & 0 deletions DBCD
Submodule DBCD added at 142c79
79 changes: 75 additions & 4 deletions WoWDatabaseEditor.Common/WDE.DbcStore/DbcStore.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Prism.Events;
using WDBXEditor.Storage;
using DBCD;
using WDE.Common.DBC;
using WDE.Common.Parameters;
using WDE.Common.Services;
Expand All @@ -22,7 +23,8 @@ public enum DBCVersions
{
WOTLK_12340 = 12340,
CATA_15595 = 15595,
LEGION_26972 = 26972
LEGION_26972 = 26972,
SHADOWLANDS_41079 = 41079
}

[AutoRegister]
Expand Down Expand Up @@ -102,6 +104,8 @@ private class DbcLoadTask : IThreadedTask
private readonly IDbcSettingsProvider dbcSettingsProvider;
private readonly IParameterFactory parameterFactory;
private readonly DbcStore store;
private readonly DBDProvider dbdProvider;
private readonly DBCProvider dbcProvider;

private Dictionary<long, string> AreaTriggerStore { get; } = new();
private Dictionary<long, long> FactionTemplateStore { get; } = new();
Expand Down Expand Up @@ -138,6 +142,8 @@ public DbcLoadTask(IParameterFactory parameterFactory, IDbcSettingsProvider sett
this.store = store;
opener = new DatabaseClientFileOpener();
dbcSettingsProvider = settingsProvider;
dbdProvider = new DBDProvider();
dbcProvider = new DBCProvider();
}

private void Load(string filename, Action<IDbcIterator> foreachRow)
Expand All @@ -161,7 +167,51 @@ private void Load(string filename, int id, int nameIndex, Dictionary<long, long>
{
Load(filename, row => dictionary.Add(row.GetInt(id), row.GetInt(nameIndex)));
}


private void Load(string filename, string fieldName, Dictionary<long, string> dictionary)
{
var dbcd = new DBCD.DBCD(dbcProvider, dbdProvider);
var storage = dbcd.Load($"{dbcSettingsProvider.GetSettings().Path}/{filename}");

if (fieldName == String.Empty)
{
foreach (DBCDRow item in storage.Values)
dictionary.Add(item.ID, String.Empty);
}
else
{
foreach (DBCDRow item in storage.Values)
{
if (item[fieldName] == null)
return;

dictionary.Add(item.ID, item[fieldName].ToString());
}
}
}

private void Load(string filename, string fieldName, Dictionary<long, long> dictionary)
{
var dbcd = new DBCD.DBCD(dbcProvider, dbdProvider);
var storage = dbcd.Load($"{dbcSettingsProvider.GetSettings().Path}/{filename}");

if (fieldName == String.Empty)
{
foreach (DBCDRow item in storage.Values)
dictionary.Add(item.ID, 0);
}
else
{
foreach (DBCDRow item in storage.Values)
{
if (item[fieldName] == null)
return;

dictionary.Add(item.ID, Convert.ToInt64(item[fieldName]));
}
}
}

public void FinishMainThread()
{
store.AreaTriggerStore = AreaTriggerStore;
Expand Down Expand Up @@ -296,7 +346,7 @@ public void Run(ITaskProgress progress)
}
case DBCVersions.LEGION_26972:
{
max = 17;
max = 18;
Load("AreaTrigger.db2", row => AreaTriggerStore.Add(row.GetInt(16), $"Area trigger at {row.GetFloat(0)}, {row.GetFloat(1)}, {row.GetFloat(2)}"));
Load("spell.db2", 0, 1, SpellStore);
Load("achievement.db2", 12, 1, AchievementStore);
Expand All @@ -318,6 +368,27 @@ public void Run(ITaskProgress progress)
Load("CreatureDisplayInfo.db2", 0, 2, CreatureDisplayInfoStore);
break;
}
case DBCVersions.SHADOWLANDS_41079:
{
max = 16;
Load("AreaTrigger.db2", string.Empty, AreaTriggerStore);
Load("SpellName.db2", "Name_lang", SpellStore);
Load("Achievement.db2", "Title_lang", AchievementStore);
Load("AreaTable.db2", "AreaName_lang", AreaStore);
Load("ChrClasses.db2", "Name_lang", ClassStore);
Load("ChrRaces.db2", "Name_lang", RaceStore);
Load("Emotes.db2", "EmoteSlashCommand", EmoteStore);
Load("EmotesText.db2", "Name", TextEmoteStore);
Load("ItemSparse.db2", "Display_lang", ItemStore);
Load("Languages.db2", "Name_lang", LanguageStore);
Load("Map.db2", "MapName_lang", MapDirectoryStore);
Load("Faction.db2", "Name_lang", FactionStore);
Load("FactionTemplate.db2", "Faction", FactionTemplateStore);
Load("SpellFocusObject.db2", "Name_lang", SpellFocusObjectStore);
Load("QuestInfo.db2", "InfoName_lang", QuestInfoStore);
Load("CharTitles.db2", "Name_lang", CharTitleStore);
break;
}
default:
return;
}
Expand Down
10 changes: 10 additions & 0 deletions WoWDatabaseEditor.Common/WDE.DbcStore/Providers/DBCProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using DBCD.Providers;
using System.IO;

namespace WDE.DbcStore.Providers
{
public class DBCProvider : IDBCProvider
{
public Stream StreamForTableName(string tableName, string build) => File.OpenRead(tableName);
}
}
47 changes: 47 additions & 0 deletions WoWDatabaseEditor.Common/WDE.DbcStore/Providers/DBDProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using DBCD.Providers;
using System;
using System.IO;
using System.Net.Http;

namespace WDE.DbcStore.Providers
{
public class DBDProvider : IDBDProvider
{
private static Uri BaseURI = new Uri("https://raw.githubusercontent.com/wowdev/WoWDBDefs/master/definitions/");
private static string CachePath = "dbdCache/";
private HttpClient client = new HttpClient();

public DBDProvider()
{
if (!Directory.Exists(CachePath))
Directory.CreateDirectory(CachePath);

client.BaseAddress = BaseURI;
}

public Stream StreamForTableName(string tableName, string build = null)
{
string dbdName = Path.GetFileName(tableName).Replace(".db2", ".dbd");

if (!File.Exists($"{CachePath}/{dbdName}") || (DateTime.Now - File.GetLastWriteTime($"{CachePath}/{dbdName}")).TotalHours > 24)
{
try
{
var bytes = client.GetByteArrayAsync(dbdName).Result;
File.WriteAllBytes($"{CachePath}/{dbdName}", bytes);

return new MemoryStream(bytes);
}
catch (HttpRequestException /*requestException*/)
{
if (File.Exists($"{CachePath}/{dbdName}"))
return new MemoryStream(File.ReadAllBytes($"{CachePath}/{dbdName}"));
else
return null;
}
}
else
return new MemoryStream(File.ReadAllBytes($"{CachePath}/{dbdName}"));
}
}
}
1 change: 1 addition & 0 deletions WoWDatabaseEditor.Common/WDE.DbcStore/WDE.DbcStore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<OutputPath>..\..\bin\$(Configuration)\</OutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\DBCD\DBCD\DBCD.csproj" />
<ProjectReference Include="..\WDE.Common\WDE.Common.csproj" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 9545803

Please sign in to comment.