-
Notifications
You must be signed in to change notification settings - Fork 48
/
Sheet.cs
269 lines (218 loc) · 7.45 KB
/
Sheet.cs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
#if UNITY_EDITOR
using System.Text;
#endif
using Bencodex.Types;
using Nekoyume.Model.State;
using Serilog;
#if UNITY_EDITOR
using Serilog;
#endif
namespace Nekoyume.TableData
{
[Serializable]
public abstract class Sheet<TKey, TValue> : IDictionary<TKey, TValue>, ISheet
where TValue : SheetRow<TKey>, new()
where TKey : notnull
{
private Dictionary<TKey, TValue> _impl;
private readonly List<int> _invalidColumnIndexes = new List<int>();
private List<TValue>? _orderedList;
public string Name { get; }
public IReadOnlyList<TValue>? OrderedList => _orderedList;
public TValue? First { get; private set; }
public TValue? Last { get; private set; }
public ICollection<TKey> Keys => ((IDictionary<TKey, TValue>)_impl).Keys;
public ICollection<TValue> Values => ((IDictionary<TKey, TValue>)_impl).Values;
public int Count => ((IDictionary<TKey, TValue>)_impl).Count;
public bool IsReadOnly => ((IDictionary<TKey, TValue>)_impl).IsReadOnly;
public TValue this[TKey key] { get => ((IDictionary<TKey, TValue>)_impl)[key]; set => ((IDictionary<TKey, TValue>)_impl)[key] = value; }
protected Sheet(string name)
{
Name = name;
_impl = new Dictionary<TKey, TValue>();
}
private string? _csv;
/// <summary>
///
/// </summary>
/// <param name="csv"></param>
/// <param name="isReversed">true: csv값의 column과 row가 뒤집혀서 작성되어 있다고 판단합니다.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidDataException"></exception>
public virtual void Set(string csv, bool isReversed = false)
{
if (string.IsNullOrEmpty(csv))
{
throw new ArgumentNullException(nameof(csv));
}
// todo: 리버스 로직 작성 필요.
if (isReversed)
{
return;
}
var lines = csv
.Trim()
.Split('\n');
if (lines.Length == 0)
{
throw new InvalidDataException(nameof(csv));
}
var columnNames = lines[0].Trim().Split(',');
for (var i = 0; i < columnNames.Length; i++)
{
var columnName = columnNames[i];
if (columnName.StartsWith("_"))
{
_invalidColumnIndexes.Add(i);
}
}
var linesWithoutColumnName = lines.Skip(1);
foreach (var line in linesWithoutColumnName)
{
if (line.StartsWith(",") ||
line.StartsWith("_"))
{
continue;
}
if (!TryGetRow(line, out var row))
{
// Do not throw any exceptions for check all of lines.
continue;
}
AddRow(row.Key, row);
}
PostSet();
_csv = csv;
}
public void Set<T>(Sheet<TKey, T> sheet, bool executePostSet = true) where T : TValue, new()
{
#pragma warning disable LAA1002
foreach (var sheetRow in sheet)
#pragma warning restore LAA1002
{
AddRow(sheetRow.Key, sheetRow);
}
if (executePostSet)
{
PostSet();
}
}
public IEnumerator<TValue> GetEnumerator()
{
if (_orderedList is null)
{
throw new InvalidOperationException($"{nameof(_orderedList)} is null.");
}
return _orderedList.GetEnumerator();
}
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value, bool throwException)
{
if (_impl.TryGetValue(key, out TValue? v) && !(v is null))
{
value = v;
return true;
}
if (throwException)
{
throw new SheetRowNotFoundException(Name, key.ToString());
}
Log.Debug("{sheetName}: Key - {value}", Name, key.ToString());
value = default;
return false;
}
protected virtual void AddRow(TKey key, TValue value)
{
Add(key, value);
}
private void PostSet()
{
foreach (var value in Values)
{
value.EndOfSheetInitialize();
}
_orderedList = Values.OrderBy(value => value.Key).ToList();
First = _orderedList.FirstOrDefault();
Last = _orderedList.LastOrDefault();
}
private bool TryGetRow(string line, out TValue row)
{
var fields = line.Trim().Split(',')
.Where((column, index) => !_invalidColumnIndexes.Contains(index))
.ToArray();
row = new TValue();
row.Set(fields);
#if UNITY_EDITOR
try
{
row.Validate();
}
catch (SheetRowValidateException e)
{
var sb = new StringBuilder();
sb.AppendLine(GetType().Name);
sb.AppendLine(row.Key.ToString());
sb.AppendLine(e.Message);
Log.Error(e, sb.ToString());
return false;
}
#endif
return true;
}
public void Add(TKey key, TValue value)
{
((IDictionary<TKey, TValue>)_impl).Add(key, value);
}
public bool ContainsKey(TKey key)
{
return ((IDictionary<TKey, TValue>)_impl).ContainsKey(key);
}
public bool Remove(TKey key)
{
return ((IDictionary<TKey, TValue>)_impl).Remove(key);
}
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
{
return ((IDictionary<TKey, TValue>)_impl).TryGetValue(key, out value);
}
public void Add(KeyValuePair<TKey, TValue> item)
{
((IDictionary<TKey, TValue>)_impl).Add(item);
}
public void Clear()
{
((IDictionary<TKey, TValue>)_impl).Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return ((IDictionary<TKey, TValue>)_impl).Contains(item);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
((IDictionary<TKey, TValue>)_impl).CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return ((IDictionary<TKey, TValue>)_impl).Remove(item);
}
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return ((IDictionary<TKey, TValue>)_impl).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
if (_orderedList is null)
{
throw new InvalidOperationException($"{nameof(_orderedList)} is null.");
}
return _orderedList.GetEnumerator();
}
public IValue Serialize() => _csv.Serialize();
}
}