comparison RollcallRaceParser.cs @ 6:0509ed2e686a

Re #16 - File loading * Refactor race detail parsing and unit type parsing in to their own classes Re #17 - Unit detail loading * Add a couple of extra attributes to those loaded
author IBBoard <dev@ibboard.co.uk>
date Mon, 26 Jan 2009 20:31:05 +0000
parents
children 35bc86f8c283
comparison
equal deleted inserted replaced
5:8c34e01a11da 6:0509ed2e686a
1 // This file (RollcallRaceParser.cs) is a part of IBBoard.WarFoundry.Plugin.Rollcall library and is copyright 2009 IBBoard.
2 //
3 // The file and the library/program it is in are licensed under the GNU LGPL license. Please see COPYING.LGPL for more information and the full license.
4
5 using System;
6 using IBBoard.CustomMath;
7 using IBBoard.Ini;
8 using IBBoard.Logging;
9 using IBBoard.WarFoundry.API.Objects;
10
11 namespace IBBoard.WarFoundry.Plugin.Rollcall
12 {
13 /// <summary>
14 /// A helper class to construct a base Race object from an INI file
15 /// </summary>
16 public class RollcallRaceParser
17 {
18 public static Race ReadRaceDetails(IniFile file)
19 {
20 string id = null, name = null;
21 id = "Rollcall" + file["Army"]["BitCodeID"].Value;
22 name = file["Army"]["Name"].Value;
23 LogNotifier.Debug(typeof(RollcallRaceParser), "Loading Rollcall race ID "+id);
24 Race race = new Race(id, name, "Rollcall", RollcallFactory.GetFactory());
25 race.GameSystem = RollcallFactory.GetFactory().RollcallSystem;
26 return race;
27 }
28
29 public static void ReadCategories(IniFile file, Race race)
30 {
31 IniSection section = file["Category"];
32
33 foreach (string key in section.Keys)
34 {
35 string valueString = section[key].Value;
36 string[] values = valueString.Split(',');
37
38 if (values.Length == 3)
39 {
40 LogNotifier.Debug(typeof(RollcallRaceParser), "Loading category " + values[0]);
41 Category category = new Category(key, values[0]);
42 int minPercent = NumberParser.ParseAsInt(values[1], 0);
43 int maxPercent = NumberParser.ParseAsInt(values[2], 100);
44 maxPercent = Math.Max(0, Math.Min(100, Math.Max(minPercent, maxPercent)));
45 minPercent = Math.Max(0, Math.Min(100, minPercent));
46 category.MaximumPercentage = maxPercent;
47 category.MinimumPercentage = minPercent;
48 race.AddCategory(category);
49
50 }
51 //Special cases (allies and aliases) need to be handled later
52 }
53 }
54
55 public static void ReadUnitTypeAndEquipmentSections(IniFile file, Race race)
56 {
57 foreach (IniSection section in file)
58 {
59 string sectionName = section.Name;
60
61 if (sectionName == "Army" || sectionName == "Category")
62 {
63 continue;
64 }
65 else if (sectionName.StartsWith("Unit"))
66 {
67 RollcallUnitTypeParser.ReadUnitTypeSection(file, section, race);
68 }
69 else
70 {
71 RollcallUnitTypeParser.ReadEquipmentSection(file, section, race);
72 }
73 }
74 }
75 }
76 }