Skip to content

Commit

Permalink
Handle nullable primitives in INIDefineable
Browse files Browse the repository at this point in the history
Co-authored-by: Kirill Andriiashin <kirill.andriiashin@gmail.com>
  • Loading branch information
Rampastring and ZivDero committed Jan 5, 2024
1 parent f0395a7 commit 04bcb18
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/TSMapEditor/Models/INIDefineable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Rampastring.Tools;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace TSMapEditor.Models
{
Expand Down Expand Up @@ -72,11 +73,34 @@ public void ReadPropertiesFromIniSection(IniSection iniSection)
setter.Invoke(this, new object[] { (byte)Math.Min(byte.MaxValue, iniSection.GetIntValue(property.Name, (byte)property.GetValue(this, null))) });
else if (propertyType.Equals(typeof(char)))
setter.Invoke(this, new object[] { iniSection.GetStringValue(property.Name, ((char)property.GetValue(this, null)).ToString())[0] });
else if (propertyType.Equals(typeof(int?)))
{
if (int.TryParse(iniSection.GetStringValue(property.Name, ""), CultureInfo.InvariantCulture, out int value))
setter.Invoke(this, new object[] { value });
}
else if (propertyType.Equals(typeof(double?)))
{
if (double.TryParse(iniSection.GetStringValue(property.Name, ""), CultureInfo.InvariantCulture, out double value))
setter.Invoke(this, new object[] { value });
}
else if (propertyType.Equals(typeof(float?)))
{
if (float.TryParse(iniSection.GetStringValue(property.Name, ""), CultureInfo.InvariantCulture, out float value))
setter.Invoke(this, new object[] { value });
}
else if (propertyType.Equals(typeof(bool?)))
{
if (iniSection.KeyExists(property.Name))
{
setter.Invoke(this, new object[] { iniSection.GetBooleanValue(property.Name, ((bool?)property.GetValue(this, null)).GetValueOrDefault()) });
}
}
else if (propertyType.Equals(typeof(List<string>)))
setter.Invoke(this, new object[] { iniSection.GetListValue(property.Name, ',', (s) => s) });
}
}


public void WritePropertiesToIniSection(IniSection iniSection)
{
var type = GetType();
Expand Down Expand Up @@ -127,6 +151,42 @@ public void WritePropertiesToIniSection(IniSection iniSection)
else
iniSection.RemoveKey(property.Name);
}
else if (propertyType.Equals(typeof(int?)))
{
int? value = (int?)getter.Invoke(this, null);

if (value != null)
iniSection.SetIntValue(property.Name, value.Value);
else
iniSection.RemoveKey(property.Name);
}
else if (propertyType.Equals(typeof(double?)))
{
double? value = (double?)getter.Invoke(this, null);

if (value != null)
iniSection.SetDoubleValue(property.Name, value.Value);
else
iniSection.RemoveKey(property.Name);
}
else if (propertyType.Equals(typeof(float?)))
{
float? value = (float?)getter.Invoke(this, null);

if (value != null)
iniSection.SetFloatValue(property.Name, value.Value);
else
iniSection.RemoveKey(property.Name);
}
else if (propertyType.Equals(typeof(bool?)))
{
bool? value = (bool?)getter.Invoke(this, null);

if (value != null)
iniSection.SetBooleanValue(property.Name, value.Value, BooleanStringStyle);
else
iniSection.RemoveKey(property.Name);
}
}
}
}
Expand Down

0 comments on commit 04bcb18

Please sign in to comment.