Skip to content

Commit

Permalink
Merge pull request #25 from KalopsiaTwilight/feature/ConstructRowForA…
Browse files Browse the repository at this point in the history
…rrayTypes

Add support for initializing array types in constructrow
  • Loading branch information
Marlamin authored Aug 15, 2024
2 parents 17ec869 + 169d9ef commit 82e6da0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
50 changes: 50 additions & 0 deletions DBCD.Tests/WritingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,58 @@ namespace DBCD.Tests
public class WritingTest
{
public static string InputPath { get; } = $"{Directory.GetCurrentDirectory()}\\DBCCache";

public static string OutputPath = $"{Directory.GetCurrentDirectory()}\\tmp";

public static WagoDBCProvider wagoDBCProvider = new();
static GithubDBDProvider githubDBDProvider = new(true);

[ClassInitialize()]
public static void ClassInit(TestContext context)
{
if (Directory.Exists(OutputPath))
{
Directory.Delete(OutputPath, true);
}
Directory.CreateDirectory(OutputPath);
}

[ClassCleanup()]
public static void ClassCleanup()
{
Directory.Delete(OutputPath, true);
}


[TestMethod]
public void TestWritingNewRowDb2()
{
DBCD dbcd = new(wagoDBCProvider, githubDBDProvider);
IDBCDStorage storage = dbcd.Load("AlliedRace", "9.2.7.45745");

storage.Add(700000, storage.ConstructRow(700000));
storage.Save(Path.Join(OutputPath, "AlliedRace.db2"));

DBCD localDbcd = new(new FilesystemDBCProvider(OutputPath), githubDBDProvider);
IDBCDStorage outputStorage = localDbcd.Load("AlliedRace", "9.2.7.45745");

Assert.AreEqual(11, outputStorage.Count);
}

[TestMethod]
public void TestWritingNewRowDb2WithArrayField()
{
DBCD dbcd = new(wagoDBCProvider, githubDBDProvider);
IDBCDStorage storage = dbcd.Load("ItemDisplayInfo", "9.2.7.45745");

storage.Add(700000, storage.ConstructRow(700000));
storage.Save(Path.Join(OutputPath, "ItemDisplayInfo.db2"));

DBCD localDbcd = new(new FilesystemDBCProvider(OutputPath), githubDBDProvider);
IDBCDStorage outputStorage = localDbcd.Load("ItemDisplayInfo", "9.2.7.45745");

Assert.AreEqual(116146, outputStorage.Count);
}

[TestMethod]
public void TestWritingAllDB2s()
Expand Down
27 changes: 26 additions & 1 deletion DBCD/DBCDStorage.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using DBCD.Helpers;

using DBCD.IO;
using DBCD.IO.Attributes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace DBCD
{
Expand Down Expand Up @@ -191,7 +193,30 @@ public void Save(string filename)
storage?.Save(filename);
}

public DBCDRow ConstructRow(int index) => new DBCDRow(index, new T(), fieldAccessor);
public DBCDRow ConstructRow(int index) {
T raw = new();
var fields = typeof(T).GetFields();
// Array Fields need to be initialized to fill their length
var arrayFields = fields.Where(x => x.FieldType.IsArray);
foreach (var arrayField in arrayFields)
{
var count = arrayField.GetCustomAttribute<CardinalityAttribute>().Count;
Array rowRecords = Array.CreateInstance(arrayField.FieldType.GetElementType(), count);
for (var i = 0; i < count; i++)
{
rowRecords.SetValue(Activator.CreateInstance(arrayField.FieldType.GetElementType()), i);
}
arrayField.SetValue(raw, rowRecords);
}

// String Fields need to be initialized to empty string rather than null;
var stringFields = fields.Where(x => x.FieldType == typeof(string));
foreach (var stringField in stringFields)
{
stringField.SetValue(raw, string.Empty);
}
return new DBCDRow(index, raw, fieldAccessor);
}

public Dictionary<int, DBCDRow> ToDictionary()
{
Expand Down

0 comments on commit 82e6da0

Please sign in to comment.