summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/I2.Loc.SimpleJSON/JSONClass.cs
blob: da9c4320870d63abf2ebbc25ad88db524c13c6ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace I2.Loc.SimpleJSON;

public class JSONClass : JSONNode, IEnumerable
{
	private Dictionary<string, JSONNode> m_Dict = new Dictionary<string, JSONNode>(StringComparer.Ordinal);

	public override JSONNode this[string aKey]
	{
		get
		{
			if (m_Dict.ContainsKey(aKey))
			{
				return m_Dict[aKey];
			}
			return new JSONLazyCreator(this, aKey);
		}
		set
		{
			if (m_Dict.ContainsKey(aKey))
			{
				m_Dict[aKey] = value;
			}
			else
			{
				m_Dict.Add(aKey, value);
			}
		}
	}

	public override JSONNode this[int aIndex]
	{
		get
		{
			if (aIndex < 0 || aIndex >= m_Dict.Count)
			{
				return null;
			}
			return m_Dict.ElementAt(aIndex).Value;
		}
		set
		{
			if (aIndex >= 0 && aIndex < m_Dict.Count)
			{
				string key = m_Dict.ElementAt(aIndex).Key;
				m_Dict[key] = value;
			}
		}
	}

	public override int Count => m_Dict.Count;

	public override IEnumerable<JSONNode> Childs
	{
		get
		{
			foreach (KeyValuePair<string, JSONNode> item in m_Dict)
			{
				yield return item.Value;
			}
		}
	}

	public override void Add(string aKey, JSONNode aItem)
	{
		if (!string.IsNullOrEmpty(aKey))
		{
			if (m_Dict.ContainsKey(aKey))
			{
				m_Dict[aKey] = aItem;
			}
			else
			{
				m_Dict.Add(aKey, aItem);
			}
		}
		else
		{
			m_Dict.Add(Guid.NewGuid().ToString(), aItem);
		}
	}

	public override JSONNode Remove(string aKey)
	{
		if (!m_Dict.ContainsKey(aKey))
		{
			return null;
		}
		JSONNode result = m_Dict[aKey];
		m_Dict.Remove(aKey);
		return result;
	}

	public override JSONNode Remove(int aIndex)
	{
		if (aIndex < 0 || aIndex >= m_Dict.Count)
		{
			return null;
		}
		KeyValuePair<string, JSONNode> keyValuePair = m_Dict.ElementAt(aIndex);
		m_Dict.Remove(keyValuePair.Key);
		return keyValuePair.Value;
	}

	public override JSONNode Remove(JSONNode aNode)
	{
		try
		{
			KeyValuePair<string, JSONNode> keyValuePair = m_Dict.Where((KeyValuePair<string, JSONNode> k) => k.Value == aNode).First();
			m_Dict.Remove(keyValuePair.Key);
			return aNode;
		}
		catch
		{
			return null;
		}
	}

	public IEnumerator GetEnumerator()
	{
		foreach (KeyValuePair<string, JSONNode> item in m_Dict)
		{
			yield return item;
		}
	}

	public override string ToString()
	{
		string text = "{";
		foreach (KeyValuePair<string, JSONNode> item in m_Dict)
		{
			if (text.Length > 2)
			{
				text += ", ";
			}
			text = text + "\"" + JSONNode.Escape(item.Key) + "\":" + item.Value;
		}
		return text + "}";
	}

	public override string ToString(string aPrefix)
	{
		string text = "{ ";
		foreach (KeyValuePair<string, JSONNode> item in m_Dict)
		{
			if (text.Length > 3)
			{
				text += ", ";
			}
			text = text + "\n" + aPrefix + "   ";
			text = text + "\"" + JSONNode.Escape(item.Key) + "\" : " + item.Value.ToString(aPrefix + "   ");
		}
		return text + "\n" + aPrefix + "}";
	}

	public override void Serialize(BinaryWriter aWriter)
	{
		aWriter.Write((byte)2);
		aWriter.Write(m_Dict.Count);
		foreach (string key in m_Dict.Keys)
		{
			aWriter.Write(key);
			m_Dict[key].Serialize(aWriter);
		}
	}
}