private void btnConvert_Click(object sender, System.EventArgs e) { if (dlgFileOpen.ShowDialog(this) == DialogResult.OK) { foreach (string filename in dlgFileOpen.FileNames) { object obj = LoadFile(filename); if (obj != null) { if (typeof(STSerializableObject).IsInstanceOfType(obj) || typeof(HighScoreRecord[]).IsInstanceOfType(obj)) { if (typeof(STSerializableObject).IsInstanceOfType(obj)) { obj = ((STSerializableObject)obj).Serialize(); } else { obj = STSerializableObject.ArrayToArrayList((HighScoreRecord[])obj); } SaveFile(filename, obj); } else { Log(UnrecognizedFile); } } } } }
public static HighScoreRecord[] GetHighScores(System.Windows.Forms.IWin32Window owner) { HighScoreRecord[] highScores = new HighScoreRecord[3]; object obj = LoadFile(Consts.HighScoreFile, true, owner); if (obj != null) { highScores = (HighScoreRecord[])STSerializableObject.ArrayListToArray((ArrayList)obj, "HighScoreRecord"); } return(highScores); }
public static ArrayList ArrayToArrayList(STSerializableObject[] array) { ArrayList list = null; if (array != null) { list = new ArrayList(); foreach (STSerializableObject obj in array) list.Add(obj == null ? null : obj.Serialize()); } return list; }
/* * Types currently supported: * CrewMember * Gadget * HighScoreRecord * Shield * StarSystem * Weapon * * If an array of a type not listed is converted using ArrayToArrayList, the type * needs to be added here. */ public static STSerializableObject[] ArrayListToArray(ArrayList list, string type) { STSerializableObject[] array = null; if (list != null) { switch (type) { case "CrewMember": array = new CrewMember[list.Count]; break; case "Gadget": array = new Gadget[list.Count]; break; case "HighScoreRecord": array = new HighScoreRecord[list.Count]; break; case "Shield": array = new Shield[list.Count]; break; case "StarSystem": array = new StarSystem[list.Count]; break; case "Weapon": array = new Weapon[list.Count]; break; } for (int index = 0; index < list.Count; index++) { Hashtable hash = (Hashtable)list[index]; STSerializableObject obj = null; if (hash != null) { switch (type) { case "CrewMember": obj = new CrewMember(hash); break; case "Gadget": obj = new Gadget(hash); break; case "HighScoreRecord": obj = new HighScoreRecord(hash); break; case "Shield": obj = new Shield(hash); break; case "StarSystem": obj = new StarSystem(hash); break; case "Weapon": obj = new Weapon(hash); break; } } array[index] = obj; } } return(array); }