wabbajack/Wabbajack.Common/DynamicIniData.cs

132 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;
using System.Text.RegularExpressions;
2019-09-14 04:35:42 +00:00
using IniParser;
using IniParser.Exceptions;
2019-09-14 04:35:42 +00:00
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) //
{
2020-01-18 13:05:46 +00:00
_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;
}
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
if (indexes.Length > 1)
{
result = null;
return false;
}
result = new SectionData(_value[(string) indexes[0]]);
return true;
}
2019-07-21 04:40:54 +00:00
}
public class SectionData : DynamicObject
2019-07-21 04:40:54 +00:00
{
public KeyDataCollection Coll { get; }
2019-07-21 04:40:54 +00:00
public SectionData(KeyDataCollection coll)
{
Coll = coll;
2019-07-21 04:40:54 +00:00
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = Coll[binder.Name];
2020-01-18 13:05:46 +00:00
if (result is string s) result = Interpret(s);
2019-07-21 04:40:54 +00:00
return true;
}
2019-09-23 21:37:10 +00:00
private static string Interpret(string s)
{
if (s.StartsWith("@ByteArray(") && s.EndsWith(")"))
{
return UnescapeUTF8(s.Substring("@ByteArray(".Length, s.Length - "@ByteArray(".Length - ")".Length));
}
return UnescapeString(s);
}
private static string UnescapeString(string s)
{
2020-03-04 05:23:08 +00:00
if (s.Trim().StartsWith("\"") || s.Contains("\\\\"))
return Regex.Unescape(s.Trim('"'));
return s;
}
private static string UnescapeUTF8(string s)
{
2020-01-18 13:05:46 +00:00
var acc = new List<byte>();
for (var i = 0; i < s.Length; i++)
{
var c = s[i];
switch (c)
{
case '\\':
i++;
var nc = s[i];
switch (nc)
{
case '\\':
acc.Add((byte)'\\');
break;
case 'x':
var chrs = s[i + 1] + s[i + 2].ToString();
i += 2;
acc.Add(Convert.ToByte(chrs, 16));
break;
default:
throw new ParsingException($"Not a valid escape characer {nc}");
}
break;
default:
acc.Add((byte)c);
break;
}
}
return Encoding.UTF8.GetString(acc.ToArray());
}
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]];
2020-03-04 05:23:08 +00:00
if (result is string s && s.Trim().StartsWith("\""))
result = Regex.Unescape(s.Trim('"'));
2019-09-23 21:37:10 +00:00
return true;
}
2019-07-21 04:40:54 +00:00
}
}