// Methods /// <summary> /// Initializes MainUnitFile with the given file name. /// </summary> /// <param name="fName">The name of the file</param> public void InitUnitFile(string filePath) { /// We're wrapping most of this method in a try-catch block to catch file exceptions. /// This will allow the app to quickly recover from incorrect file selections try { // First things first, initialize MainUnitFile MainUnitFile = new UnitFile(filePath); MainUnitFile.init(); // Now that we've initialized the file, pull out the names for each unit UnitName tempUName; foreach (UnitFileNode node in MainUnitFile.UnitDir.TheUnits) { tempUName = new UnitName(node.TheUnit, node.Index); // Toss it on the NameList.. UnitEditor.NameList.Add(tempUName); // ...and on the Armies Armies.AddUnit(tempUName); } // Finally, set UnitEditor to the first unit in the list UnitEditor.changeSelection(UnitEditor.NameList[0]); } catch (SUE_InvalidFileException ivfe) { // Send message to user, redo file open. throw ivfe; } }
static int Main(string[] args) { UnitFile theFile; string fName = "UnitTypes.dat"; Unit curUnit; // Try building it with the file name provided theFile = new UnitFile(fName); // Try initializing it try { theFile.init(); Console.Out.WriteLine("The file was read and has {0} units", theFile.UnitCount); List <UnitFactionNode> theFacts = theFile.UnitDir.GetStartingUnits(); Console.Out.WriteLine("______________________________ NEA\tCon\tGPF\tRoT\tCal\tPac\tEU\tRUS"); foreach (UnitFactionNode fact in theFacts) { Console.Out.WriteLine("{0,-30} {1,5} {2,5} {3,5} {4,5} {4,5} {5,5} {6,5} {7,5} {8,5}", fact.Name.Value, fact.FactionCounts[(int)UnitFaction.NEA].Value, fact.FactionCounts[(int)UnitFaction.Con].Value, fact.FactionCounts[(int)UnitFaction.GPF].Value, fact.FactionCounts[(int)UnitFaction.RoT].Value, fact.FactionCounts[(int)UnitFaction.Cal].Value, fact.FactionCounts[(int)UnitFaction.Pac].Value, fact.FactionCounts[(int)UnitFaction.EU].Value, fact.FactionCounts[(int)UnitFaction.Rus].Value); } } catch (SUE_InvalidFileException sueIFE) { Console.Out.WriteLine(sueIFE.Message); } return(0); }