-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathData.cs
153 lines (141 loc) · 5.91 KB
/
Data.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
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Resources;
namespace INVedit
{
internal static class Data
{
static readonly Dictionary<string, Image> images = new Dictionary<string, Image>();
public static readonly ImageList list = new ImageList();
public static readonly Dictionary<short, Item> items = new Dictionary<short, Item>();
public static readonly Dictionary<string, Group> groups = new Dictionary<string, Group>();
public static Image unknown;
public static int version = int.MinValue;
public static void Init(string path)
{
ResourceManager resources = new ResourceManager("INVedit.Resources", typeof(Data).Assembly);
unknown = (Image)resources.GetObject("unknown");
string[] lines = File.ReadAllLines(path);
for (int i = 1; i <= lines.Length; ++i) {
try {
string line = lines[i-1].TrimStart();
if (line=="" || line[0]=='#') continue;
string[] split;
if (line[0] == ':') {
split = line.Split(new char[]{' '}, 2, StringSplitOptions.RemoveEmptyEntries);
switch (split[0].ToLower()) {
case ":version":
try { version = int.Parse(split[1]); }
catch (Exception e) { throw new DataException("Failed to parse option "+split[0]+" at line "+i+" in file '"+path+"'.", e); }
break;
default:
throw new DataException("Unknown option '"+split[0]+"' at line "+i+" in file '"+path+"'.");
} continue;
}
split = line.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
Exception ex = new DataException("Invalid number of colums at line "+i+" in file '"+path+"'.");
if (line[0]=='~') { if (split.Length != 4) throw ex; }
else { if (split.Length < 4 || split.Length > 5) throw ex; }
string name = split[1].Replace('_', ' ');
if (line[0]=='~') {
short icon;
try { icon = short.Parse(split[2]); }
catch (Exception e) { throw new DataException("Failed to parse column 'ICON' at line "+i+" in file '"+path+"'.", e); }
if (!items.ContainsKey(icon)) throw new DataException("Invalid item id '"+icon+"' in column 'ICON' at line "+i+" in file '"+path+"'.");
int imageIndex = items[icon].imageIndex;
string[] l = split[3].Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
Group group = new Group(name, imageIndex);
foreach (string n in l) {
short s;
try { s = short.Parse(n); }
catch (Exception e) { throw new DataException("Failed to parse column 'ITEMS' at line "+i+" in file '"+path+"'.", e); }
if (items.ContainsKey(s)) group.Add(items[s]);
else MessageBox.Show("Invalid item id '"+s+"' in column 'ITEMS' at line "+i+" in file '"+path+"'.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
groups.Add(name, group);
} else {
Image image;
try { image = LoadImage(split[2]); }
catch (Exception e) { throw new DataException("Failed to load image '"+split[2]+"' at line "+i+" in file '"+path+"' ("+e.Message+").", e); }
short id;
try { id = short.Parse(split[0]); }
catch (Exception e) { throw new DataException("Failed to parse column 'ID' at line "+i+" in file '"+path+"'.", e); }
string[] cords = split[3].Split(',');
if (cords.Length != 2) throw new DataException("Failed to parse column 'CORDS' at line "+i+" in file '"+path+"'.");
int x, y;
try {
x = int.Parse(cords[0]);
y = int.Parse(cords[1]);
} catch (Exception e) { throw new DataException("Failed to parse column 'CORDS' at line "+i+" in file '"+path+"'.", e); }
if (x < 0 || y < 0 || x*16+16 > image.Width || y*16+16 > image.Height)
throw new DataException("Invalid image cords "+x+","+y+" at line "+i+" in file '"+path+"'.");
bool stackable = true;
short maxDamage = 0;
if (split.Length == 5) {
stackable = false;
try { maxDamage = short.Parse(split[4]); }
catch (Exception e) { throw new DataException("Failed to parse column 'DAMAGE' at line "+i+" in file '"+path+"'.", e); }
if (maxDamage < 0) throw new DataException("Failed to parse column 'DAMAGE' at line "+i+" in file '"+path+"'.");
}
items.Add(id, new Item(id, name, stackable, maxDamage, image, x, y));
}
} catch (Exception e) {
if (MessageBox.Show(e.Message, "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
return;
}
}
}
static Image LoadImage(string path)
{
if (images.ContainsKey(path)) return images[path];
Image image = Image.FromFile(path, true);
images[path] = image;
return image;
}
internal class Item
{
public readonly short id;
public readonly string name;
public readonly bool stackable;
public readonly short maxDamage;
public readonly int imageIndex;
internal Item(short id, string name, bool stackable, short maxDamage, Image image, int x, int y)
{
this.id = id;
this.name = name;
this.stackable = stackable;
this.maxDamage = maxDamage;
Bitmap b = new Bitmap(16, 16);
using (Graphics g = Graphics.FromImage(b))
g.DrawImage(image, new Rectangle(0, 0, 16, 16), new Rectangle(x*16, y*16, 16, 16), GraphicsUnit.Pixel);
imageIndex = Data.list.Images.Count;
Data.list.Images.Add(b);
}
}
internal class Group
{
public readonly string name;
public readonly int imageIndex;
public readonly List<Item> items = new List<Item>();
internal Group(string name, int imageIndex)
{
this.name = name;
this.imageIndex = imageIndex;
}
internal void Add(Item item) {
items.Add(item);
}
}
}
public class DataException : Exception
{
public DataException() : base() { }
public DataException(string message) : base(message) { }
public DataException(string message, Exception innerException) : base(message, innerException) { }
}
}