Skip to content

Commit 79310e5

Browse files
committed
C#: Initial dbdm parsing and some more dbdm entries for testing
1 parent a7b5540 commit 79310e5

4 files changed

Lines changed: 145 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace DBDefsLib.Constants
2+
{
3+
public enum MetaType
4+
{
5+
FLAGS,
6+
ENUM,
7+
COLOR
8+
}
9+
}

code/C#/DBDefsLib/DBDMReader.cs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using DBDefsLib.Structs;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace DBDefsLib
7+
{
8+
public class DBDMReader
9+
{
10+
private List<MappingDefinition> Read(Stream stream)
11+
{
12+
var reader = new StreamReader(stream);
13+
var lines = reader.ReadLines();
14+
15+
reader.Close();
16+
reader.Dispose();
17+
18+
var mappings = new List<MappingDefinition>();
19+
20+
var lineNumber = 0;
21+
while (lineNumber < lines.Count)
22+
{
23+
var line = lines[lineNumber++];
24+
25+
// Mapping file could contain empty lines for organization
26+
if (string.IsNullOrEmpty(line))
27+
continue;
28+
29+
var mappingDefinition = new MappingDefinition();
30+
var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
31+
32+
// Comment available in mapping line, we handle split differently here as well
33+
if (line.Contains("//"))
34+
{
35+
var indexOfComment = line.IndexOf("//", StringComparison.OrdinalIgnoreCase);
36+
37+
var lineWithComment = line[..indexOfComment];
38+
split = lineWithComment.Split(' ', StringSplitOptions.RemoveEmptyEntries);
39+
40+
// Ignore "// " in comments
41+
var comment = line[(indexOfComment + 3)..];
42+
mappingDefinition.comment = comment;
43+
}
44+
45+
// "Meta TableName::ColumnName" are required in the Mapping file
46+
if (split.Length < 2)
47+
throw new Exception($"Line: {lineNumber} has invalid size for fields, expected: 2, current: {split.Length}");
48+
49+
// Retrieve the meta type
50+
var metaType = split[0];
51+
if (!Enum.TryParse(metaType, out mappingDefinition.meta))
52+
throw new Exception($"Line: {lineNumber} does not contain a valid MetaType: {metaType}");
53+
54+
// Retrieve the tablename and column name
55+
var tableColumnSplit = split[1].Split("::");
56+
if (tableColumnSplit.Length > 2)
57+
throw new Exception($"Line: {lineNumber} has invalid size for Table::Column, excepted: 2, current: {tableColumnSplit.Length}");
58+
59+
mappingDefinition.tableName = tableColumnSplit[0];
60+
61+
// Array column
62+
var columnName = tableColumnSplit[1];
63+
if (columnName.Contains('[') && columnName.Contains(']'))
64+
{
65+
var indexOfStartArr = columnName.IndexOf('[');
66+
var indexOfEndArr = columnName.IndexOf(']');
67+
68+
var arrIndexString = columnName[(indexOfStartArr + 1)..indexOfEndArr];
69+
if (!int.TryParse(arrIndexString, out var arrIndex))
70+
throw new Exception($"Line: {lineNumber} has invalid array index for column: {columnName}");
71+
72+
mappingDefinition.columnName = columnName[..indexOfStartArr];
73+
mappingDefinition.arrIndex = arrIndex;
74+
}
75+
else
76+
{
77+
mappingDefinition.columnName = columnName;
78+
}
79+
80+
if (split.Length >= 3)
81+
mappingDefinition.metaValue = split[2];
82+
83+
if (split.Length == 4)
84+
{
85+
var conditionalData = split[3];
86+
if (!conditionalData.Contains("="))
87+
throw new Exception($"Line: {lineNumber} has no conditional Table::Column value assignment");
88+
89+
var assignmentIndexOf = conditionalData.IndexOf('=');
90+
tableColumnSplit = conditionalData[..assignmentIndexOf].Split("::");
91+
if (tableColumnSplit.Length > 2)
92+
throw new Exception($"Line: {lineNumber} has invalid size for Conditional Table::Column, excepted: 2, current: {tableColumnSplit.Length}");
93+
94+
mappingDefinition.conditionalTable = tableColumnSplit[0];
95+
mappingDefinition.conditionalColumn = tableColumnSplit[1];
96+
mappingDefinition.conditionalValue = conditionalData[(assignmentIndexOf + 1)..];
97+
}
98+
99+
mappings.Add(mappingDefinition);
100+
}
101+
102+
return mappings;
103+
}
104+
105+
public List<MappingDefinition> Read(string file)
106+
{
107+
if (!File.Exists(file))
108+
throw new FileNotFoundException($"Unable to find mapping file: {file}");
109+
110+
using var stream = File.Open(file, FileMode.Open, FileAccess.Read);
111+
return Read(stream);
112+
}
113+
}
114+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using DBDefsLib.Constants;
2+
3+
namespace DBDefsLib.Structs
4+
{
5+
public record struct MappingDefinition
6+
{
7+
public MetaType meta;
8+
public string tableName;
9+
public string columnName;
10+
public int? arrIndex;
11+
public string metaValue;
12+
public string conditionalTable;
13+
public string conditionalColumn;
14+
public string conditionalValue;
15+
public string comment;
16+
}
17+
}

meta/mapping.dbdm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
FLAGS Achievement::Flags AchievementFlags
22
FLAGS SpellMisc::Flags[0] SpellAttributes0
3-
FLAGS SpellMisc::Flags[15]
3+
FLAGS SpellMisc::Flags[2]
4+
FLAGS SpellMisc::Flags[15] SpellAttributes15 // Some comment
45
ENUM Map::ExpansionID ExpansionLevels
56
COLOR LightData::SkySmogColor
7+
ENUM SpellEffect::EffectMiscValue[0] DamageClass SpellEffect::EffectAura=39
8+
ENUM SpellEffect::EffectMiscValue[0] DamageClass SpellEffect::EffectAura=69
9+
ENUM SpellEffect::EffectMiscValue[0] CombatRatingFlags SpellEffect::EffectAura=189

0 commit comments

Comments
 (0)