|
| 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 | +} |
0 commit comments