Skip to content

Commit

Permalink
Merge pull request #30 from KalopsiaTwilight/feature/extra-type-info
Browse files Browse the repository at this point in the history
Feature - Extra metadata in constructed type and DBCDStorage
  • Loading branch information
Marlamin authored Jan 6, 2025
2 parents bcb1de6 + 1af829e commit 02c1b58
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
16 changes: 16 additions & 0 deletions DBCD.IO/Attributes/ForeignReferenceAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

using System;
using System.Collections.Generic;
using System.Text;

namespace DBCD.IO.Attributes
{
public class ForeignReferenceAttribute : Attribute
{
public readonly string ForeignTable;
public readonly string ForeignColumn;

public ForeignReferenceAttribute(string foreignTable, string foreignColumn) => (ForeignTable, ForeignColumn) = (foreignTable, foreignColumn);

}
}
13 changes: 13 additions & 0 deletions DBCD.IO/Attributes/SizeInBitsAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DBCD.IO.Attributes
{
public class SizeInBitsAttribute: Attribute
{
public readonly ushort Size;

public SizeInBitsAttribute(ushort size) => Size = size;
}
}
4 changes: 4 additions & 0 deletions DBCD.IO/DBParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("DBCD")]

namespace DBCD.IO
{
Expand All @@ -23,6 +26,7 @@ public class DBParser
public uint LayoutHash => _reader.LayoutHash;
public int IdFieldIndex => _reader.IdFieldIndex;
public DB2Flags Flags => _reader.Flags;
internal ColumnMetaData[] ColumnMeta => _reader.ColumnMeta;
#endregion

public DBParser(string fileName) : this(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { }
Expand Down
15 changes: 15 additions & 0 deletions DBCD/DBCDBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ internal Tuple<Type, DBCDInfo> Build(DBParser dbcReader, Stream dbd, string name
var columns = new List<string>(fields.Length);
bool localiseStrings = locale != Locale.None;

var metadataIndex = 0;
foreach (var fieldDefinition in fields)
{
var columnDefinition = databaseDefinition.columnDefinitions[fieldDefinition.name];
Expand All @@ -100,6 +101,15 @@ internal Tuple<Type, DBCDInfo> Build(DBParser dbcReader, Stream dbd, string name
if (fieldDefinition.isID)
{
AddAttribute<IndexAttribute>(field, fieldDefinition.isNonInline);
}

if (!fieldDefinition.isNonInline)
{
if (metadataIndex < dbcReader.ColumnMeta.Length)
{
AddAttribute<SizeInBitsAttribute>(field, dbcReader.ColumnMeta[metadataIndex].Size);
}
metadataIndex++;
}

if (fieldDefinition.arrLength > 1)
Expand All @@ -113,6 +123,11 @@ internal Tuple<Type, DBCDInfo> Build(DBParser dbcReader, Stream dbd, string name
AddAttribute<RelationAttribute>(field, metaDataFieldType, fieldDefinition.isNonInline);
}

if (!string.IsNullOrEmpty(columnDefinition.foreignTable))
{
AddAttribute<ForeignReferenceAttribute>(field, columnDefinition.foreignTable, columnDefinition.foreignColumn);
}

if (isLocalisedString)
{
if (localiseStrings)
Expand Down
3 changes: 3 additions & 0 deletions DBCD/DBCDStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public bool Create(int index, Action<dynamic> f)
public interface IDBCDStorage : IEnumerable<DynamicKeyValuePair<int>>, IDictionary<int, DBCDRow>
{
string[] AvailableColumns { get; }
uint LayoutHash { get; }

DBCDRow ConstructRow(int index);

Expand All @@ -129,6 +130,7 @@ public interface IDBCDStorage : IEnumerable<DynamicKeyValuePair<int>>, IDictiona
private readonly DBParser parser;

string[] IDBCDStorage.AvailableColumns => this.info.availableColumns;
public uint LayoutHash => this.storage.LayoutHash;
public override string ToString() => $"{this.info.tableName}";

public DBCDStorage(Stream stream, DBCDInfo info) : this(new DBParser(stream), info) { }
Expand All @@ -146,6 +148,7 @@ public DBCDStorage(DBParser parser, Storage<T> storage, DBCDInfo info) : base(ne
base.Add(record.Key, new DBCDRow(record.Key, record.Value, fieldAccessor));
}


public void ApplyingHotfixes(HotfixReader hotfixReader)
{
this.ApplyingHotfixes(hotfixReader, null);
Expand Down

0 comments on commit 02c1b58

Please sign in to comment.