public static void Init() { try { Reset(); foreach (string str in System.IO.Directory.GetDirectories(Application.StartupPath + "\\data\\trains\\resources")) { string[] l = System.IO.File.ReadAllLines(str + @"\data.xml", System.Text.Encoding.Default); if (l[0] == @"<locomotive>") { Locomotive loc = new Locomotive(); loc.Load(str.Substring(str.LastIndexOf('\\') + 1, str.Length - str.LastIndexOf('\\') - 1)); Locomotives.Add(loc); continue; } else if (l[0] == @"<coach>") { Coach car = new Coach(); car.Load(str.Substring(str.LastIndexOf('\\') + 1, str.Length - str.LastIndexOf('\\') - 1)); Coachs.Add(car); continue; } else if (l[0] == @"<enginecoach>") { EngineCoach lc = new EngineCoach(); lc.Load(str.Substring(str.LastIndexOf('\\') + 1, str.Length - str.LastIndexOf('\\') - 1)); EngineCoachs.Add(lc); continue; } } foreach (string str in System.IO.Directory.GetDirectories(".\\data\\trains\\datas")) { TrainData train = new TrainData(); train.Load(str.Substring(str.LastIndexOf('\\') + 1, str.Length - str.LastIndexOf('\\') - 1)); TrainDatas.Add(train); } } catch (Exception e) { Environment.ReportError(e, AccessManager.AccessKey); } }
public void Load(string name) { try { string path = ".\\data\\trains\\datas\\" + name; XmlDocument xml = new XmlDocument(); xml.Load(path + "\\data.xml"); XmlNode root = xml.SelectNodes("traindata")[0]; string args = root.SelectNodes("args")[0].InnerText; Name = root.SelectNodes("name")[0].InnerText; Image = Image.FromFile(path + @"\" + root.SelectNodes("image")[0].InnerText); XmlDocument arg = new XmlDocument(); arg.Load(path + "\\" + args); XmlNode _args = arg.SelectNodes("args")[0]; foreach (XmlNode it in _args.SelectNodes("arg")) { if (it.Attributes["type"].Value == "locomotive") { if (TrainManager.GetLocomotiveByName(it.Attributes["name"].Value) == null) { throw new WrongTrainDataException("해당 이름을 가진 기관차가 없습니다."); } Locomotive t = TrainManager.Locomotives.First(x => x.Name == it.Attributes["name"].Value); Args.Add(t); } else if (it.Attributes["type"].Value == "coach") { if (TrainManager.GetCoachByName(it.Attributes["name"].Value) == null) { throw new WrongTrainDataException("해당 이름을 가진 객차가 없습니다."); } Coach t = TrainManager.Coachs.First(x => x.Name == it.Attributes["name"].Value); Args.Add(t); } else if (it.Attributes["type"].Value == "enginecoach") { if (TrainManager.GetEngineCoachByName(it.Attributes["name"].Value) == null) { throw new WrongTrainDataException("해당 이름을 가진 객차가 없습니다."); } EngineCoach ec = TrainManager.GetEngineCoachByName(it.Attributes["name"].Value); Args.Add(ec); } else { throw new WrongTrainDataException("'" + it.Attributes["type"].Value + "' 라는 type 은 없습니다."); } } // 검사 if (Args.Count <= 1) { throw new WrongTrainDataException("기관차와 객차의 수를 합친 수가 1 이하 입니다. 최소한 2 이상이여야 합니다."); } List <Locomotive> loc = new List <Locomotive>(); List <EngineCoach> lc = new List <EngineCoach>(); List <TrainParant> loclc = new List <TrainParant>(); foreach (var it in Args) { if (it is Locomotive) { loc.Add((Locomotive)it); loclc.Add(it); } else if (it is EngineCoach) { lc.Add((EngineCoach)it); loclc.Add(it); } } if (loc.Count + lc.Count == 0) // 0일 경우 { throw new WrongTrainDataException("기관차의 수가 0 입니다."); } long carry = 0; foreach (var it in loc) { carry += it.Carrying; } foreach (var it in lc) { carry += it.Locomotive.Carrying; } List <Coach> car = new List <Coach>(); foreach (var it in Args) { if (it is Coach) { car.Add((Coach)it); } } if (car.Count > carry) //최대 연결 허용량보다 연결된게 더 높으면 { throw new WrongTrainDataException("기관차가 최대한 연결할 수 있는 객차 수보다 더 많은 객차가 연결 되었습니다."); } if (loc.Count + lc.Count > 1) // 1보다 크면 { int k = 0; if (loclc[0] is Locomotive) { k = (int)((loclc[0] as Locomotive).Rank); } else if (loclc[1] is EngineCoach) { k = (int)((loclc[0] as EngineCoach).Locomotive.Rank); } foreach (var it in loclc) { if (it is Locomotive) { if ((int)((it as Locomotive).Rank) != k) { throw new WrongTrainDataException("기관차의 수가 1 보다 클 경우 기관차들의 등급이 같아야 합니다."); } } else if (it is EngineCoach) { if ((int)((it as EngineCoach).Locomotive.Rank) != k) { throw new WrongTrainDataException("기관차의 수가 1 보다 클 경우 기관차들의 등급이 같아야 합니다."); } } } } } catch (Exception e) { Environment.ReportError(e, AccessManager.AccessKey); } }