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-11-21 15:49:14 +00:00
private readonly IniData _value;
2019-07-21 04:40:54 +00:00
public DynamicIniData(IniData value) //
{
2019-11-21 15:49:14 +00:00
this._value = value;
2019-07-21 04:40:54 +00:00
}
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)
{
2019-11-21 15:49:14 +00:00
result = new SectionData(_value[binder.Name]);
2019-07-21 04:40:54 +00:00
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
}