Skip to content

Commit

Permalink
Fix incorrect handling of array fields
Browse files Browse the repository at this point in the history
  • Loading branch information
KalopsiaTwilight committed Aug 20, 2024
1 parent f084093 commit d6b2da1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Newtonsoft.Json.Linq;
using DBCD;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO.Packaging;
using System.Reflection;

namespace WDBXEditor2.Helpers
{
internal class ConvertHelper
internal class DBCDRowHelper
{
private static Dictionary<Type, Func<int, string[], object>> _arrayConverters = new Dictionary<Type, Func<int, string[], object>>()
{
Expand All @@ -26,31 +29,53 @@ public static object ConvertArray(Type type, int size, string[] records)
return _arrayConverters[type](size, records);
}

public static object ConvertValue (Type type, string fieldName, object value)
public static void SetDBCRowColumn(DBCDRow row, string colName, object value)
{
return Convert.ChangeType(value, GetFieldType(type, fieldName));
var fieldName = GetUnderlyingFieldName(row.GetUnderlyingType(), colName, out var arrayIndex);
var field = row.GetUnderlyingType().GetField(fieldName);
if (field.FieldType.IsArray)
{
((Array)row[fieldName]).SetValue(Convert.ChangeType(value, field.FieldType.GetElementType()), arrayIndex);
} else
{
row[fieldName] = Convert.ChangeType(value, field.FieldType);
}
}

public static object GetDBCRowColumn(DBCDRow row, string colName)
{
var fieldName = GetUnderlyingFieldName(row.GetUnderlyingType(), colName, out var arrayIndex);
var field = row.GetUnderlyingType().GetField(fieldName);
if (field.FieldType.IsArray)
{
return ((Array)row[fieldName]).GetValue(arrayIndex);
} else
{
return row[fieldName];
}
}

public static Type GetFieldType(Type type, string fieldName)
public static Type GetFieldType(DBCDRow row, string fieldName)
{
var field = type.GetField(fieldName);
if (field == null)
var field = row.GetUnderlyingType().GetField(GetUnderlyingFieldName(row.GetUnderlyingType(), fieldName, out _));
if (field.FieldType.IsArray)
{
var index = 0;
var n = 1;
while (int.TryParse(fieldName[^1].ToString(), out var indexN))
{
fieldName = fieldName.Substring(0, fieldName.Length - 1);
index += n * indexN;
n *= 10;
}
field = type.GetField(fieldName);
return field.FieldType.GetElementType();
}
else
return field.FieldType;
}

private static string GetUnderlyingFieldName(Type type, string fieldName, out int index)
{
index = 0;
var n = 1;
while (int.TryParse(fieldName[^1].ToString(), out var indexN))
{
return field.FieldType;
fieldName = fieldName.Substring(0, fieldName.Length - 1);
index += n * indexN;
n *= 10;
}
return fieldName;
}


Expand Down
8 changes: 4 additions & 4 deletions WDBXEditor2/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CsvHelper;
using CsvHelper;
using CsvHelper.Configuration;
using DBCD;
using Microsoft.Win32;
Expand Down Expand Up @@ -196,7 +196,7 @@ private void DB2DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEve
var colName = e.Column.Header.ToString();
try
{
dbcRow[colName] = ConvertHelper.ConvertValue(dbcRow.GetUnderlyingType(), colName, newVal.Text);
DBCDRowHelper.SetDBCRowColumn(dbcRow, colName, newVal.Text);
if (colName == dbcRow.GetDynamicMemberNames().FirstOrDefault())
{
OpenedDB2Storage.Remove(dbcRow.ID);
Expand All @@ -206,9 +206,9 @@ private void DB2DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEve
}
catch(Exception exc)
{
newVal.Text = dbcRow[colName].ToString();
newVal.Text = DBCDRowHelper.GetDBCRowColumn(dbcRow, colName).ToString();
var exceptionWindow = new ExceptionWindow();
var fieldType = ConvertHelper.GetFieldType(dbcRow.GetUnderlyingType(), colName);
var fieldType = DBCDRowHelper.GetFieldType(dbcRow, colName);

exceptionWindow.DisplayException(exc.InnerException ?? exc, $"An error occured setting this value for this cell. This is likely due to an invalid value for conversion to '{fieldType.Name}':");
exceptionWindow.Show();
Expand Down
2 changes: 1 addition & 1 deletion WDBXEditor2/Views/ReplaceColumnWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private void Ok_Click(object sender, RoutedEventArgs e)
{
if (row[columnName].ToString() == txtValueReplace.Text)
{
row[columnName] = ConvertHelper.ConvertValue(row.GetUnderlyingType(), columnName, txtValue.Text);
DBCDRowHelper.SetDBCRowColumn(row, columnName, txtValue.Text);
}
}
_mainWindow.ReloadDataView();
Expand Down
2 changes: 1 addition & 1 deletion WDBXEditor2/Views/SetColumnWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private void Ok_Click(object sender, RoutedEventArgs e)
var columnName = ddlColumnName.SelectedValue.ToString();
foreach (var row in dbcdStorage.Values)
{
row[columnName] = ConvertHelper.ConvertValue(row.GetUnderlyingType(), columnName, txtValue.Text); ;
DBCDRowHelper.SetDBCRowColumn(row, columnName, txtValue.Text);
}
_mainWindow.ReloadDataView();
Close();
Expand Down
2 changes: 1 addition & 1 deletion WDBXEditor2/Views/SetDependentColumnWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private void Ok_Click(object sender, RoutedEventArgs e)
{
if (row[columnName].ToString() == txtPrimaryValue.Text)
{
row[foreignColumnName] = ConvertHelper.ConvertValue(row.GetUnderlyingType(), foreignColumnName, txtForeignValue.Text);
DBCDRowHelper.SetDBCRowColumn(row, foreignColumnName, txtForeignValue.Text);
}
}
_mainWindow.ReloadDataView();
Expand Down
4 changes: 2 additions & 2 deletions WDBXEditor2/Views/SetFlagWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ private void Ok_Click(object sender, RoutedEventArgs e)
{
if ((rowVal & bitVal) > 0)
{
row[columnName] = ConvertHelper.ConvertValue(row.GetUnderlyingType(), columnName, rowVal - bitVal);
DBCDRowHelper.SetDBCRowColumn(row, columnName, rowVal - bitVal);
}
} else
{
if ((rowVal & bitVal) == 0)
{
row[columnName] = ConvertHelper.ConvertValue(row.GetUnderlyingType(), columnName, rowVal + bitVal);
DBCDRowHelper.SetDBCRowColumn(row, columnName, rowVal + bitVal);
}

}
Expand Down

0 comments on commit d6b2da1

Please sign in to comment.