2020-01-06 22:00:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Dynamic;
|
|
|
|
|
using System.Numerics;
|
|
|
|
|
using System.Text;
|
2019-08-11 22:57:32 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2019-09-14 04:35:42 +00:00
|
|
|
|
using IniParser;
|
2020-01-06 22:00:22 +00:00
|
|
|
|
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) //
|
|
|
|
|
{
|
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];
|
2020-01-06 22:00:22 +00:00
|
|
|
|
if (result is string) result = Interpret((string)result);
|
2019-07-21 04:40:54 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-09-23 21:37:10 +00:00
|
|
|
|
|
2020-01-06 22:00:22 +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)
|
|
|
|
|
{
|
|
|
|
|
return Regex.Unescape(s.Trim('"'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string UnescapeUTF8(string s)
|
|
|
|
|
{
|
|
|
|
|
List<byte> 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]];
|
|
|
|
|
if (result is string) result = Regex.Unescape(((string)result).Trim('"'));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-07-21 04:40:54 +00:00
|
|
|
|
}
|
2020-01-06 22:00:22 +00:00
|
|
|
|
}
|