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

Added DBML Serializer (just hacked it dirty in, better than nothing) #159

Merged
merged 15 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
3 changes: 1 addition & 2 deletions code/C#/DBDefsConverter/DBDXMLSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using static DBDefsLib.Structs;

using FieldLookup = System.Collections.Generic.Dictionary<string, System.Reflection.FieldInfo>;

Expand Down Expand Up @@ -79,7 +78,7 @@ public struct SerializableDBDefinition
[XmlElement("columnDefinitions")]
public SerializableColumnDefinition columnDefinitions;
[XmlElement("versionDefinitions")]
public VersionDefinitions[] versionDefinitions;
public VersionDefinition[] versionDefinitions;
}

[Serializable]
Expand Down
145 changes: 135 additions & 10 deletions code/C#/DBDefsConverter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
using DBDefsLib;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using static DBDefsLib.Structs;
using System.Linq;

namespace DBDefsConverter
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 1 || args.Length > 3)
if (args.Length < 1 || args.Length > 4)
{
throw new ArgumentException("Invalid argument count, need at least 1 argument: indbdfile/indbddir (outdir, default current dir) (json, xml)");
throw new ArgumentException("Invalid argument count, need at least 1 argument: indbdfile/indbddir (outdir, default current dir) (json, xml, dbml) (build number for dbml export)");
}

var inFile = args[0];
var outDir = Directory.GetCurrentDirectory();
var exportFormat = "json";
Build exportBuild = null;

if (args.Length >= 2)
{
Expand All @@ -41,14 +43,51 @@ static void Main(string[] args)
}
}

if (Directory.Exists(args[0]))
if (args.Length == 4)
{
var files = Directory.GetFiles(args[0]);
DoExport(exportFormat, outDir, files);
if (!args[2].Equals("dbml"))
{
throw new ArgumentException(
"Converting to a specific build is only supported for export format dbml");
}

exportFormat = args[2];
var succeededParsingBuild = Build.TryParse(args[3], out var parsedBuild);
if (!succeededParsingBuild)
{
throw new ArgumentException(
"Couldn't parse build. Make sure format looks like this: \"Expansion.Major.Minor.Build\"");
}

exportBuild = parsedBuild;
}
else if (File.Exists(args[0]))

if (Directory.Exists(inFile))
{
DoExport(exportFormat, outDir, args[0]);
var files = Directory.GetFiles(inFile);
switch (exportFormat)
{
case "xml":
case "json":
DoExport(exportFormat, outDir, files);
break;
case "dbml":
DoExportDBML(outDir, exportBuild, files);
break;
}
}
else if (File.Exists(inFile))
{
switch (exportFormat)
{
case "xml":
case "json":
DoExport(exportFormat, outDir, inFile);
break;
case "dbml":
DoExportDBML(outDir, exportBuild, inFile);
break;
}
}
else
{
Expand All @@ -58,8 +97,8 @@ static void Main(string[] args)

private static void DoExport(string exportFormat, string outDir, params string[] files)
{
JsonSerializer jsonserializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore };
DBDXMLSerializer xmlserializer = new DBDXMLSerializer();
var jsonserializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore };
var xmlserializer = new DBDXMLSerializer();

foreach (var file in files)
{
Expand All @@ -83,5 +122,91 @@ private static void DoExport(string exportFormat, string outDir, params string[]
}
}
}

private static void DoExportDBML(string outDir, Build build, params string[] files)
{
var dbmlSerializer = new DBDDBMLSerializer();
var dbmlDocument = new DBMLDocument
{
Project = new DBMLProject
{
Name = $"DBC_{build.expansion}_{build.major}_{build.minor}_{build.build}",
Note = $"Database Client tables for World of Warcraft {build}"
}
};

var shippedTables = new List<string>();

foreach (var file in files)
{
Console.WriteLine("Parsing " + file);

var reader = new DBDReader();
var dbdIdentifier = Path.GetFileNameWithoutExtension(file);
var dbDefinition = reader.Read(file);
var buildDefinition = dbDefinition.versionDefinitions
.Where(d => d.builds.Equals(build) ||
d.buildRanges.Any(br => br.Contains(build)))
.Cast<VersionDefinition?>()
.FirstOrDefault();

if (buildDefinition is null)
{
continue;
}

shippedTables.Add(dbdIdentifier);
var columns = new List<DBMLColumn>();
foreach (var definition in buildDefinition.Value.definitions)
{
if (!dbDefinition.columnDefinitions.ContainsKey(definition.name))
continue;

var columnDefinition = dbDefinition.columnDefinitions[definition.name];
var column = new DBMLColumn
{
Name = definition.name,
Type = columnDefinition.type,
Settings = new DBMLColumnSettings
{
IsPrimaryKey = definition.isID,
RelationshipType = !string.IsNullOrEmpty(columnDefinition.foreignTable)
? DBMLColumnRelationshipType.OneToMany
: DBMLColumnRelationshipType.None,
Relationship = $"{columnDefinition.foreignTable}.{columnDefinition.foreignColumn}",
Note = columnDefinition.comment
}
};
columns.Add(column);
}

var table = new DBMLTable
{
Name = dbdIdentifier,
Note = $"{dbdIdentifier} client database table for version {build}",
Columns = columns
};
dbmlDocument.Tables.Add(table);
}

// Filter out relations of unshipped tables
foreach (var table in dbmlDocument.Tables)
{
foreach (var column in table.Columns)
{
if (shippedTables.Contains(column.Settings.Relationship.Split(".")[0]))
continue;
column.Settings.Relationship = string.Empty;
column.Settings.RelationshipType = DBMLColumnRelationshipType.None;
}
}

// Now we can write our DBML file
var outFile = $"{build}.dbml";
Console.WriteLine($"Writing {outFile}");
var target = Path.Combine(outDir, outFile);
using var writer = File.CreateText(target);
dbmlSerializer.Serialize(writer, dbmlDocument);
}
}
}
7 changes: 3 additions & 4 deletions code/C#/DBDefsLib/DBDReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static DBDefsLib.Structs;

namespace DBDefsLib
{
Expand Down Expand Up @@ -130,7 +129,7 @@ public DBDefinition Read(Stream stream, bool validate = false)

// There will be less comments from this point on, stuff used in above code is mostly repeated

var versionDefinitions = new List<VersionDefinitions>();
var versionDefinitions = new List<VersionDefinition>();

var definitions = new List<Definition>();
var layoutHashes = new List<string>();
Expand All @@ -146,7 +145,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
{
if (builds.Count != 0 || buildRanges.Count != 0 || layoutHashes.Count != 0) {
versionDefinitions.Add(
new VersionDefinitions()
new VersionDefinition()
{
builds = builds.ToArray(),
buildRanges = buildRanges.ToArray(),
Expand Down Expand Up @@ -283,7 +282,7 @@ public DBDefinition Read(Stream stream, bool validate = false)
{
if (builds.Count != 0 || buildRanges.Count != 0 || layoutHashes.Count != 0) {
versionDefinitions.Add(
new VersionDefinitions()
new VersionDefinition()
{
builds = builds.ToArray(),
buildRanges = buildRanges.ToArray(),
Expand Down
5 changes: 2 additions & 3 deletions code/C#/DBDefsLib/DBDWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static DBDefsLib.Structs;

namespace DBDefsLib
{
Expand Down Expand Up @@ -198,7 +197,7 @@ public void Save(DBDefinition definition, string target, bool sort = false)
}
}

internal class DBDVersionsComparer : IComparer<VersionDefinitions>
internal class DBDVersionsComparer : IComparer<VersionDefinition>
{
private readonly bool _asc;

Expand All @@ -207,7 +206,7 @@ public DBDVersionsComparer(bool ascending = true)
_asc = ascending;
}

public int Compare(VersionDefinitions x, VersionDefinitions y)
public int Compare(VersionDefinition x, VersionDefinition y)
{
Build xmax, ymax;

Expand Down
63 changes: 30 additions & 33 deletions code/C#/DBDefsLib/Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,39 @@

namespace DBDefsLib
{
public class Structs
bloerwald marked this conversation as resolved.
Show resolved Hide resolved
public struct DBDefinition
{
public struct DBDefinition
{
public Dictionary<string, ColumnDefinition> columnDefinitions;
public VersionDefinitions[] versionDefinitions;
}
public Dictionary<string, ColumnDefinition> columnDefinitions;
public VersionDefinition[] versionDefinitions;
}

public struct VersionDefinitions
{
public Build[] builds;
public BuildRange[] buildRanges;
public string[] layoutHashes;
public string comment;
public Definition[] definitions;
}
public struct VersionDefinition
{
public Build[] builds;
public BuildRange[] buildRanges;
public string[] layoutHashes;
public string comment;
public Definition[] definitions;
}

public struct Definition
{
public int size;
public int arrLength;
public string name;
public bool isID;
public bool isRelation;
public bool isNonInline;
public bool isSigned;
public string comment;
}
public struct Definition
{
public int size;
public int arrLength;
public string name;
public bool isID;
public bool isRelation;
public bool isNonInline;
public bool isSigned;
public string comment;
}

public struct ColumnDefinition
{
public string type;
public string foreignTable;
public string foreignColumn;
public bool verified;
public string comment;
}
public struct ColumnDefinition
{
public string type;
public string foreignTable;
public string foreignColumn;
public bool verified;
public string comment;
}
}
6 changes: 2 additions & 4 deletions code/C#/DBDefsLib/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using static DBDefsLib.Structs;
using System.Linq;
using System.IO;
using System.Text;

namespace DBDefsLib
{
Expand All @@ -21,7 +19,7 @@ public static string BuildToString(Build build)
return build.ToString();
}

public static bool GetVersionDefinitionByLayoutHash(DBDefinition definition, string layoutHash, out VersionDefinitions? versionToUse)
public static bool GetVersionDefinitionByLayoutHash(DBDefinition definition, string layoutHash, out VersionDefinition? versionToUse)
{
foreach (var versionDefinition in definition.versionDefinitions)
{
Expand All @@ -36,7 +34,7 @@ public static bool GetVersionDefinitionByLayoutHash(DBDefinition definition, str
return false;
}

public static bool GetVersionDefinitionByBuild(DBDefinition definition, Build build, out VersionDefinitions? versionToUse)
public static bool GetVersionDefinitionByBuild(DBDefinition definition, Build build, out VersionDefinition? versionToUse)
{
foreach (var versionDefinition in definition.versionDefinitions)
{
Expand Down
1 change: 0 additions & 1 deletion code/C#/DBDefsMerge/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static DBDefsLib.Structs;

namespace DBDefsMerge
{
Expand Down
1 change: 0 additions & 1 deletion code/C#/DBDefsTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using static DBDefsLib.Structs;

namespace DBDTest
{
Expand Down
1 change: 0 additions & 1 deletion code/C#/DBDefsValidator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static DBDefsLib.Structs;

namespace DBDefsTest
{
Expand Down