Entitas  0.35.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
Properties.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text.RegularExpressions;
5 
6 namespace Entitas.Serialization.Configuration {
7 
8  public class Properties {
9 
10  public string[] keys { get { return _dict.Keys.ToArray(); } }
11  public string[] values { get { return _dict.Values.ToArray(); } }
12 
13  public int count {
14  get { return _dict.Count; }
15  }
16 
17  public bool HasKey(string key) {
18  return _dict.ContainsKey(key);
19  }
20 
21  public string this[string key] {
22  get { return _dict[key]; }
23  set {
24  _dict[key.Trim()] = value
25  .TrimStart()
26  .Replace("\\n", "\n")
27  .Replace("\\t", "\t");
28  }
29  }
30 
31  readonly Dictionary<string, string> _dict;
32 
33  public Properties() : this(string.Empty) {
34  }
35 
36  public Properties(string properties) {
37  properties = convertLineEndings(properties);
38  _dict = new Dictionary<string, string>();
39  var lines = getLinesWithProperties(properties);
40  addProperties(mergeMultilineValues(lines));
41  replacePlaceholders();
42  }
43 
44  static string convertLineEndings(string str) {
45  return str.Replace("\r\n", "\n").Replace("\r", "\n");
46  }
47 
48  static string[] getLinesWithProperties(string properties) {
49  var delimiter = new [] { '\n' };
50  return properties
51  .Split(delimiter, StringSplitOptions.RemoveEmptyEntries)
52  .Select(line => line.TrimStart(' '))
53  .Where(line => !line.StartsWith("#", StringComparison.Ordinal))
54  .ToArray();
55  }
56 
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
64  );
65  } else {
66  acc.Add(currentProperty);
67  currentProperty = string.Empty;
68  }
69 
70  return acc;
71  }).ToArray();
72  }
73 
74  void addProperties(string[] lines) {
75  var keyValueDelimiter = new [] { '=' };
76  var properties = lines.Select(
77  line => line.Split(keyValueDelimiter, 2)
78  );
79  foreach(var property in properties) {
80  this[property[0]] = property[1];
81  }
82  }
83 
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]
91  );
92  }
93  }
94  }
95 
96  public override string ToString() {
97  return _dict.Aggregate(string.Empty, (properties, kv) => {
98  var content = kv.Value
99  .Replace("\n", "\\n")
100  .Replace("\t", "\\t");
101 
102  return properties + kv.Key + " = " + content + "\n";
103  });
104  }
105  }
106 }