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

Feature - Extra metadata in constructed type and DBCDStorage #30

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading