2 using System.Collections.Generic;
4 using System.Text.RegularExpressions;
6 namespace Entitas.Serialization.Configuration {
10 public string[] keys {
get {
return _dict.Keys.ToArray(); } }
11 public string[] values {
get {
return _dict.Values.ToArray(); } }
14 get {
return _dict.Count; }
17 public bool HasKey(
string key) {
18 return _dict.ContainsKey(key);
21 public string this[
string key] {
22 get {
return _dict[key]; }
24 _dict[key.Trim()] = value
27 .Replace(
"\\t",
"\t");
31 readonly Dictionary<string, string> _dict;
37 properties = convertLineEndings(properties);
38 _dict =
new Dictionary<string, string>();
39 var lines = getLinesWithProperties(properties);
40 addProperties(mergeMultilineValues(lines));
41 replacePlaceholders();
44 static string convertLineEndings(
string str) {
45 return str.Replace(
"\r\n",
"\n").Replace(
"\r",
"\n");
48 static string[] getLinesWithProperties(
string properties) {
49 var delimiter =
new [] {
'\n' };
51 .Split(delimiter, StringSplitOptions.RemoveEmptyEntries)
52 .Select(line => line.TrimStart(
' '))
53 .Where(line => !line.StartsWith(
"#", StringComparison.Ordinal))
57 static string[] mergeMultilineValues(
string[] lines) {
58 var currentProperty =
string.Empty;
59 return lines.Aggregate(
new List<string>(), (acc, line) => {
60 currentProperty += line;
61 if(currentProperty.EndsWith(
"\\", StringComparison.Ordinal)) {
62 currentProperty = currentProperty.Substring(
63 0, currentProperty.Length - 1
66 acc.Add(currentProperty);
67 currentProperty =
string.Empty;
74 void addProperties(
string[] lines) {
75 var keyValueDelimiter =
new [] {
'=' };
76 var properties = lines.Select(
77 line => line.Split(keyValueDelimiter, 2)
79 foreach(var property
in properties) {
80 this[
property[0]] =
property[1];
84 void replacePlaceholders() {
85 const string placeholderPattern =
@"(?:(?<=\${).+?(?=}))";
86 foreach(var key
in _dict.Keys.ToArray()) {
87 var matches = Regex.Matches(_dict[key], placeholderPattern);
88 foreach(Match match
in matches) {
89 _dict[key] = _dict[key].Replace(
90 "${" + match.Value +
"}", _dict[match.Value]
96 public override string ToString() {
97 return _dict.Aggregate(
string.Empty, (properties, kv) => {
98 var content = kv.Value
100 .Replace(
"\t",
"\\t");
102 return properties + kv.Key +
" = " + content +
"\n";