-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCollection.cs
87 lines (73 loc) · 2.54 KB
/
Collection.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
using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace MatrixGraphicsGenerator
{
public class Collection
{
readonly string filePath;
public List<MatrixData> Graphics { get; }
public Collection(string path)
{
Graphics = new List<MatrixData>();
filePath = path;
if (!File.Exists(filePath))
{
Save();
return;
}
string jsonString = File.ReadAllText(filePath);
Graphics = JsonConvert.DeserializeObject<List<MatrixData>>(jsonString);
Graphics.Sort((x, y) => x.Name.CompareTo(y.Name));
}
/// <summary>
/// Adds a graphic to the list or overwrites if one is present with the same name
/// (Used instead of Graphics.Add())
/// </summary>
public void AddGraphic(MatrixData graphic)
{
int existingDataIndex = Graphics.FindIndex(x => x.Name == graphic.Name);
if (existingDataIndex != -1)
Graphics[existingDataIndex] = graphic;
else
Graphics.Add(graphic);
Graphics.Sort((x, y) => x.Name.CompareTo(y.Name));
Save();
}
public void Save()
{
string jsonString = JsonConvert.SerializeObject(Graphics, Formatting.Indented);
File.WriteAllText(filePath, jsonString);
}
public void Export(string path)
{
int line = 0;
string[] lines = new string[Graphics.Count + 1];
lines[line] = "const int GRAPHICS_COUNT = " + Graphics.Count + ";";
line++;
for (int i = 0; i < Graphics.Count; i++, line++)
{
lines[line] = "const int " + Graphics[i].Name.Replace(" ", string.Empty) + "[" + Graphics[i].Size.Item2 + "] = { " + Graphics[i].Code + " };";
}
File.WriteAllLines(path, lines);
}
}
/// <summary>
/// Contains needed information for saving a graphic
/// </summary>
public struct MatrixData
{
public string Name { get; private set; }
public string Code { get; private set; }
public List<int> RowCodes { get; private set; }
public Tuple<int, int> Size { get; private set; }
public MatrixData(string name, string code, List<int> rowCodes, Tuple<int, int> size)
{
Name = name;
Code = code;
RowCodes = rowCodes;
Size = size;
}
}
}