2020-01-06 22:00:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-04-03 23:23:13 +00:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-01-06 22:00:22 +00:00
|
|
|
|
using System.Dynamic;
|
|
|
|
|
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) //
|
|
|
|
|
{
|
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;
|
|
|
|
|
}
|
2020-03-03 21:53:29 +00:00
|
|
|
|
|
2020-04-04 04:02:53 +00:00
|
|
|
|
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, [MaybeNullWhen(false)] out object? result)
|
2020-03-03 21:53:29 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 14:48:55 +00:00
|
|
|
|
public class SectionData : DynamicObject
|
2019-07-21 04:40:54 +00:00
|
|
|
|
{
|
2020-01-18 14:48:55 +00:00
|
|
|
|
public KeyDataCollection Coll { get; }
|
2019-07-21 04:40:54 +00:00
|
|
|
|
|
|
|
|
|
public SectionData(KeyDataCollection coll)
|
|
|
|
|
{
|
2020-01-18 14:48:55 +00:00
|
|
|
|
Coll = coll;
|
2019-07-21 04:40:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
|
|
|
|
{
|
2020-01-18 14:48:55 +00:00
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
{
|
2020-03-04 05:23:08 +00:00
|
|
|
|
if (s.Trim().StartsWith("\"") || s.Contains("\\\\"))
|
|
|
|
|
return Regex.Unescape(s.Trim('"'));
|
|
|
|
|
return s;
|
2020-01-06 22:00:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string UnescapeUTF8(string s)
|
|
|
|
|
{
|
2020-01-18 13:05:46 +00:00
|
|
|
|
var acc = new List<byte>();
|
2020-01-06 22:00:22 +00:00
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-04 04:02:53 +00:00
|
|
|
|
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, [MaybeNullWhen(false)] out object? result)
|
2019-09-23 21:37:10 +00:00
|
|
|
|
{
|
|
|
|
|
if (indexes.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
result = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 14:48:55 +00:00
|
|
|
|
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
|
|
|
|
}
|
2020-01-06 22:00:22 +00:00
|
|
|
|
}
|