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; } } }
public static void Init(string path) { ResourceManager resources = new ResourceManager("INVedit.Resources", typeof(Data).Assembly); unknown = (Image)resources.GetObject("unknown"); list.Images.Add(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] + ".", e); } break; case ":mc-version": mcVersion = split[1]; break; default: throw new DataException("Unknown option '" + split[0] + "'."); } continue; } split = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Exception ex = new DataException("Invalid number of colums."); if (line[0] == '~') { if (split.Length != 4) { throw ex; } } else if (line[0] == '+') { if (split.Length != 5) { throw ex; } } else { if (split.Length < 4 || split.Length > 5) { throw ex; } } if (line[0] == '~') { string name = split[1].Replace('_', ' '); short icon; try { icon = short.Parse(split[2]); } catch (Exception e) { throw new DataException("Failed to parse column 'ICON'.", e); } if (!items.ContainsKey(icon)) { throw new DataException("Invalid item id '" + icon + "' in column 'ICON'."); } int imageIndex = items[icon][0].imageIndex; string[] l = split[3].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); Group group = new Group(name, imageIndex); foreach (string nn in l) { string n = nn; short s; short d = -1; if (n.IndexOf('~') != -1) { string[] sp = n.Split('~'); n = sp[0]; try { d = short.Parse(sp[1]); } catch (Exception e) { throw new DataException("Failed to parse column 'ITEMS'.", e); } } try { s = short.Parse(n); } catch (Exception e) { throw new DataException("Failed to parse column 'ITEMS'.", e); } if (!items.ContainsKey(s)) { throw new DataException("Invalid item id '" + s + "' in column 'ITEMS'."); } if (d == -1) { foreach (Item item in items[s].Values) { group.Add(item); } } else if (items[s].ContainsKey(d)) { group.Add(items[s][d]); } else { throw new DataException("Invalid item damage '" + d + "' in column 'ITEMS'."); } } groups.Add(name, group); } else if (line[0] == '+') { short id; try { id = short.Parse(split[1]); } catch (Exception e) { throw new DataException("Failed to parse column 'EID'.", e); } string name = split[2].Replace('_', ' '); short max; try { max = short.Parse(split[3]); } catch (Exception e) { throw new DataException("Failed to parse column 'MAX'.", e); } string[] l = split[4].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); List <short> itemList = new List <short>(); foreach (string nn in l) { string n = nn; short s; try { s = short.Parse(n); } catch (Exception e) { throw new DataException("Failed to parse column 'ITEMS'.", e); } if (!items.ContainsKey(s)) { throw new DataException("Invalid item id '" + s + "' in column 'ITEMS'."); } enchantable.Add(s); itemList.Add(s); } enchantments.Add(id, new Enchantment(id, name, max, itemList)); } else { string name = split[1].Replace('_', ' '); short id; try { id = short.Parse(split[0]); } catch (Exception e) { throw new DataException("Failed to parse column 'ID'.", e); } Image image; try { image = LoadImage(split[2]); } catch (Exception e) { throw new DataException("Failed to load image '" + split[2] + "' (" + e.Message + ").", e); } string[] cords = split[3].Split(','); if (cords.Length != 2) { throw new DataException("Failed to parse column 'CORDS'."); } 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'.", e); } if (x < 0 || y < 0 || x * 16 + 16 > image.Width || y * 16 + 16 > image.Height) { throw new DataException("Invalid image cords " + x + "," + y + "."); } byte stack = 64; byte preferred = 0; short damage = 0; short maxDamage = 0; bool brackets = false; if (split.Length == 5) { string str = split[4]; char chr = ' '; if (str[0] == '(') { if (str[str.Length - 1] != ')') { throw new DataException("Failed to parse column 'DAMAGE'."); } str = str.Substring(1, str.Length - 2); brackets = true; } if (str[0] == '+' || str[0] == 'x') { chr = str[0]; str = str.Substring(1, str.Length - 1); } short val; try { val = short.Parse(str); } catch (Exception e) { throw new DataException("Failed to parse column 'DAMAGE'.", e); } switch (chr) { case '+': if (brackets || val <= 0) { throw new DataException("Failed to parse column 'DAMAGE'."); } stack = 1; maxDamage = val; break; case 'x': if (val <= 0) { throw new DataException("Failed to parse column 'DAMAGE'."); } if (val > 255) { throw new DataException("Failed to parse column 'DAMAGE'."); } if (brackets) { preferred = (byte)val; } else { stack = (byte)val; } break; default: if (brackets) { throw new DataException("Failed to parse column 'DAMAGE'."); } damage = val; break; } } if (preferred == 0) { preferred = stack; } items.Add(new Item(id, name, stack, preferred, damage, maxDamage, image, x, y)); } } catch (Exception e) { if (MessageBox.Show("Error at line " + i + ": " + e.Message, "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) { return; } } } }