Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing #24

Merged
merged 41 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
4a14c26
rename IO library
barncastle Aug 8, 2019
dceb12e
fix projects
barncastle Aug 9, 2019
0a9bbf5
updae other projects
barncastle Aug 9, 2019
63799aa
expose additional fields
barncastle Aug 9, 2019
dccc9c8
WIP db writing
barncastle Aug 9, 2019
6f29aaf
formatting, extensions + code cleanup
barncastle Sep 11, 2019
735c49b
fixes
barncastle Sep 12, 2019
ffbe681
bubble Enumerate errors
barncastle Sep 12, 2019
da5ac49
revert last unittest changes push
barncastle Sep 12, 2019
f32e25a
big refactor
barncastle Sep 29, 2019
7223fe6
Merge branch 'master' into feature/db_writing
Marlamin Aug 8, 2024
cbbd718
Merge changes between repos
Marlamin Aug 8, 2024
b665b88
Fix WDC5 header size
Marlamin Aug 8, 2024
e6a5f3b
Reconcile reader differences
Marlamin Aug 8, 2024
ab2f0d7
Fix compile
Marlamin Aug 8, 2024
d0e72e8
Merge tests
Marlamin Aug 8, 2024
25224bb
Remove try/catch to mirror current
Marlamin Aug 8, 2024
7dd1077
Add Benchmark project
Marlamin Aug 8, 2024
f537bcd
Update README
Marlamin Aug 8, 2024
ecce99a
Add ReadingBenchmark
Marlamin Aug 8, 2024
87332b1
Update tests
Marlamin Aug 8, 2024
5003e22
Add util for checking if a build exists in a DBD for tests
Marlamin Aug 8, 2024
e9ae98c
Fix WDC4/WDC5 writing crashes
Marlamin Aug 8, 2024
b9eddb5
Also look for DBCs in FileSystemProvider
Marlamin Aug 9, 2024
ca15a14
WDC5 schema reading/writing
Marlamin Aug 9, 2024
64479c5
Fix some WDC1/WDC2 crashes
Marlamin Aug 9, 2024
93740e8
Sign-extend WDC1 Immediate values
Marlamin Aug 9, 2024
324cf92
Don't handle noninline relations as fields
Marlamin Aug 9, 2024
545e5e0
WDC2 writing crash fix
Marlamin Aug 9, 2024
1079e52
Workaround offset issue in WDC1/WDC2 sparse tables
Marlamin Aug 9, 2024
247c1cc
Fix WDC1/WDC2 offset map writing
Marlamin Aug 9, 2024
fca6efb
Attempt at cleaning up DBCDStorage.cs diff
Marlamin Aug 9, 2024
7d649c5
Make DBCDRow.ID public again
Marlamin Aug 9, 2024
4484076
Fix WDB6 oopsie
Marlamin Aug 10, 2024
52f6fce
Sync benchmarks
Marlamin Aug 10, 2024
e545a08
Add caching to filesystem providers
Marlamin Aug 10, 2024
3845e08
Use faster and shared string table reader
Marlamin Aug 10, 2024
06f66ee
Instead of returning empty strings, 0 out index
Marlamin Aug 10, 2024
57672ef
Update project files
Marlamin Aug 10, 2024
298464e
Update local-only (disabled!) tests
Marlamin Aug 10, 2024
01e52f7
Add Version to project files
Marlamin Aug 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions DBCD.Benchmark/Benchmarks/ReadingBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using BenchmarkDotNet.Attributes;
using DBCD.Providers;

namespace DBCD.Benchmark.Benchmarks
{
[MemoryDiagnoser]
public class ReadingBenchmark
{
private static readonly FilesystemDBDProvider localDBDProvider = new FilesystemDBDProvider("D:\\Projects\\WoWDBDefs\\definitions");
private static readonly FilesystemDBCProvider localDBCProvider = new FilesystemDBCProvider("D:\\Projects\\DBCDStaging\\DBCD.Tests\\bin\\Debug\\net8.0\\DBCCache\\11.0.2.56044");
private readonly string[] allDB2s = Directory.GetFiles("D:\\Projects\\DBCDStaging\\DBCD.Tests\\bin\\Debug\\net8.0\\DBCCache\\11.0.2.56044", "*.db2", SearchOption.AllDirectories).Select(x => Path.GetFileNameWithoutExtension(x)).ToArray();
private readonly string build = "11.0.2.56044";

[Benchmark]
public int TestReadingAllDB2s()
{
var inputDBCD = new DBCD(localDBCProvider, localDBDProvider);

//var build = "3.3.5.12340"; // WDBC
//var build = "6.0.1.18179"; // WDB2
//var build = "7.0.1.20740"; // WDB3, only 1 DBD sadly
//var build = "7.0.1.20810"; // WDB4, only 2 DBDs sadly
//var build = "7.2.0.23436"; // WDB5, only Map.db2
//var build = "7.3.5.25928"; // WDB6
//var build = "7.3.5.25928"; // WDC1
//var build = "8.0.1.26231"; // WDC2
//var build = "9.1.0.39653"; // WDC3
//var build = "10.1.0.48480"; // WDC4
var build = "11.0.2.56044"; // WDC5

foreach (var tableName in allDB2s)
{
if (tableName == "UnitTestSparse")
continue;

if (!localDBDProvider.ContainsBuild(tableName, build))
continue;

var storage = inputDBCD.Load(tableName, build);
}

return allDB2s.Count();
}
}
}
76 changes: 76 additions & 0 deletions DBCD.Benchmark/Benchmarks/StringTableBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using BenchmarkDotNet.Attributes;
using System.Text;

namespace DBCD.Benchmark.Benchmarks
{
[MemoryDiagnoser]
public class StringTableBench
{
private static byte[] InputBytes = File.ReadAllBytes("E:\\stringtable.bytes");
private static int StringTableSize = (int)InputBytes.Length;

[Benchmark]
public void OldMethod()
{
using (var stream = new MemoryStream(InputBytes))
using (var reader = new BinaryReader(stream))
{
var StringTable = new Dictionary<long, string>(StringTableSize / 0x20);
for (int i = 0; i < StringTableSize;)
{
long oldPos = reader.BaseStream.Position;
StringTable[i] = reader.ReadCString();
i += (int)(reader.BaseStream.Position - oldPos);
}
}
}

[Benchmark]
public void NewMethod()
{
using (var stream = new MemoryStream(InputBytes))
using (var reader = new BinaryReader(stream))
{
var StringTable = reader.ReadStringTable(StringTableSize);
}
}
}

public static class BinaryReaderExtensions
{
public static string ReadCString(this BinaryReader reader)
{
var bytes = new List<byte>();
byte b;
while ((b = reader.ReadByte()) != 0)
bytes.Add(b);

return Encoding.UTF8.GetString(bytes.ToArray());
}

public static Dictionary<long, string> ReadStringTable(this BinaryReader reader, int stringTableSize, int baseOffset = 0, bool usePos = false)
{
var StringTable = new Dictionary<long, string>(stringTableSize / 0x20);

if (stringTableSize == 0)
return StringTable;

var curOfs = 0;
var decoded = Encoding.UTF8.GetString(reader.ReadBytes(stringTableSize));
foreach (var str in decoded.Split('\0'))
{
if (curOfs == stringTableSize)
break;

if (usePos)
StringTable[(reader.BaseStream.Position - stringTableSize) + curOfs] = str;
else
StringTable[baseOffset + curOfs] = str;

curOfs += Encoding.UTF8.GetByteCount(str) + 1;
}

return StringTable;
}
}
}
40 changes: 40 additions & 0 deletions DBCD.Benchmark/Benchmarks/WritingBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using BenchmarkDotNet.Attributes;
using DBCD.Benchmark.Utilities;
using DBCD.Providers;

namespace DBCD.Benchmark.Benchmarks
{
[MemoryDiagnoser]
public class WritingBenchmark
{
public static GithubDBDProvider DBDProvider { get; } = new GithubDBDProvider(true);
public static string InputPath { get; } = $"{Directory.GetCurrentDirectory()}\\dbc";
public static DBCD InputDBCD { get; } = new DBCD(new FilesystemDBCProvider(InputPath), DBDProvider);
public static DBCD SavedDBCD { get; } = new DBCD(new FilesystemDBCProvider("tmp"), DBDProvider);

public static string Build { get; } = "9.1.0.39653";

[Benchmark]
public void TestWritingAllDB2s()
{
string[] allDB2s = Directory.GetFiles(InputPath, "*.db2", SearchOption.TopDirectoryOnly);

if (Directory.Exists("tmp"))
Directory.Delete("tmp", true);
Directory.CreateDirectory("tmp");

foreach (var db2File in allDB2s)
{
if (Utilities.IO.TryGetExactPath(db2File, out string exactPath))
{
var tableName = Path.GetFileNameWithoutExtension(exactPath);

var originalStorage = InputDBCD.Load(tableName, Build);
originalStorage.Save($"tmp/{tableName}.db2");
}
}

Directory.Delete("tmp", true);
}
}
}
19 changes: 19 additions & 0 deletions DBCD.Benchmark/DBCD.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Optimize>true</Optimize>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DBCD\DBCD.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions DBCD.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// See https://aka.ms/new-console-template for more information
using BenchmarkDotNet.Running;
using DBCD.Benchmark.Benchmarks;

BenchmarkRunner.Run<StringTableBench>();
53 changes: 53 additions & 0 deletions DBCD.Benchmark/Utilities/IO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace DBCD.Benchmark.Utilities
{
internal class IO
{
public static bool TryGetExactPath(string path, out string exactPath)
{
bool result = false;
exactPath = null;

Check warning on line 13 in DBCD.Benchmark/Utilities/IO.cs

View workflow job for this annotation

GitHub Actions / tests

Cannot convert null literal to non-nullable reference type.

// DirectoryInfo accepts either a file path or a directory path, and most of its properties work for either.
// However, its Exists property only works for a directory path.
DirectoryInfo directory = new DirectoryInfo(path);
if (File.Exists(path) || directory.Exists)
{
List<string> parts = new List<string>();

DirectoryInfo parentDirectory = directory.Parent;

Check warning on line 22 in DBCD.Benchmark/Utilities/IO.cs

View workflow job for this annotation

GitHub Actions / tests

Converting null literal or possible null value to non-nullable type.
while (parentDirectory != null)
{
FileSystemInfo entry = parentDirectory.EnumerateFileSystemInfos(directory.Name).First();
parts.Add(entry.Name);

directory = parentDirectory;
parentDirectory = directory.Parent;
}

// Handle the root part (i.e., drive letter or UNC \\server\share).
string root = directory.FullName;
if (root.Contains(':'))
{
root = root.ToUpper();
}
else
{
string[] rootParts = root.Split('\\');
root = string.Join("\\", rootParts.Select(part => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(part)));
}

parts.Add(root);
parts.Reverse();
exactPath = Path.Combine(parts.ToArray());
result = true;
}

return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace DBFileReaderLib.Attributes
namespace DBCD.IO.Attributes
{
public class CardinalityAttribute : Attribute
{
Expand Down
11 changes: 11 additions & 0 deletions DBCD.IO/Attributes/ForeignAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace DBCD.IO.Attributes
{
public class ForeignAttribute : Attribute
{
public readonly bool IsForeign;

public ForeignAttribute(bool isForeign) => IsForeign = isForeign;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace DBFileReaderLib.Attributes
namespace DBCD.IO.Attributes
{
public class IndexAttribute : Attribute
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace DBFileReaderLib.Attributes
namespace DBCD.IO.Attributes
{
public class LocaleAttribute : Attribute
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System;

namespace DBFileReaderLib.Attributes
namespace DBCD.IO.Attributes
{
public class RelationAttribute : Attribute
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Runtime.CompilerServices;
using System.Text;

namespace DBFileReaderLib.Common
namespace DBCD.IO.Common
{
class BitReader
{
Expand Down
Loading