wabbajack/Wabbajack.Common/DynamicIniData.cs

64 lines
1.7 KiB
C#
Raw Normal View History

2019-09-14 04:35:42 +00:00
using System.Dynamic;
using System.Text.RegularExpressions;
2019-09-14 04:35:42 +00:00
using IniParser;
using IniParser.Model;
2019-07-21 04:40:54 +00:00
namespace Wabbajack.Common
{
public class DynamicIniData : DynamicObject
{
2019-09-14 04:35:42 +00:00
private readonly IniData value;
2019-07-21 04:40:54 +00:00
public DynamicIniData(IniData value) //
{
this.value = value;
}
public static dynamic FromIni(IniData data)
{
return new DynamicIniData(data);
}
public static dynamic FromFile(string filename)
{
var fi = new FileIniDataParser();
return new DynamicIniData(fi.ReadFile(filename));
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = new SectionData(value[binder.Name]);
return true;
}
}
2019-09-14 04:35:42 +00:00
internal class SectionData : DynamicObject
2019-07-21 04:40:54 +00:00
{
2019-09-14 04:35:42 +00:00
private readonly KeyDataCollection _coll;
2019-07-21 04:40:54 +00:00
public SectionData(KeyDataCollection coll)
{
2019-09-14 04:35:42 +00:00
_coll = coll;
2019-07-21 04:40:54 +00:00
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _coll[binder.Name];
2019-09-14 04:35:42 +00:00
if (result is string) result = Regex.Unescape(((string) result).Trim('"'));
2019-07-21 04:40:54 +00:00
return true;
}
2019-09-23 21:37:10 +00:00
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
if (indexes.Length > 1)
{
result = null;
return false;
}
result = _coll[(string) indexes[0]];
if (result is string) result = Regex.Unescape(((string)result).Trim('"'));
return true;
}
2019-07-21 04:40:54 +00:00
}
2019-09-14 04:35:42 +00:00
}