public void GetSetDefaultGetOnly() { var config = new Ini.Config(); var section = new Ini.Section("Test"); config.Add(section); string SettingName = "Preset"; string SettingName2 = "ArrayPreset"; int oldVal = 5; int newVal = 7; section[SettingName].Int = oldVal; int mods = config.Changes; var setting = section.GetOrSet(SettingName, newVal); Assert.AreEqual(mods, config.Changes); Assert.AreEqual(oldVal, setting.Int); section[SettingName2].IntArray = new int[] { 1, 2, 3 }; mods = config.Changes; var setting2 = section.GetOrSet(SettingName2, new int[] { 7, 8, 9 }); Assert.AreEqual(mods, config.Changes); // no changes var array = setting2.IntArray; Assert.AreEqual(1, array[0]); Assert.AreEqual(2, array[1]); Assert.AreEqual(3, array[2]); Assert.AreEqual("{ 1, 2, 3 }", new ExposedSetting(setting2).ExposedEscapedValue); }
public void Initialization() { var config = new Ini.Config(); var section = new Ini.Section("Test"); config.Add(section); section.Add(new Ini.Setting() { Name = "Key" }); // empty value var lines = config.GetLines(); foreach (var line in lines) { Debug.WriteLine(line); } Assert.AreEqual(3, lines.Length); Assert.AreEqual("[Test]\n", lines[0]); Assert.AreEqual(2, config.Changes); }
public void GetSetDefaultDoesSet() { var config = new Ini.Config(); var section = new Ini.Section("Test"); config.Add(section); int newVal = 5; int mods = config.Changes; var setVal = section.GetOrSet("NotSet", newVal); Assert.AreEqual(mods + 2, config.Changes); Assert.AreEqual(newVal, setVal.Int); var setVal2 = section.GetOrSet("NotSetArray", new int[] { 1, 2, 3 }); Assert.AreEqual(mods + 4, config.Changes); var array = setVal2.IntArray; Assert.AreEqual(1, array[0]); Assert.AreEqual(2, array[1]); Assert.AreEqual(3, array[2]); Assert.AreEqual("{ 1, 2, 3 }", new ExposedSetting(setVal2).ExposedEscapedValue); Assert.AreEqual(5, config.Changes); }
public void SettingEnumerator() { var section = new Ini.Section("TestSection"); section.Add(new Ini.Setting() { Name = "Test1", Int = 5 }); section.Add(new Ini.Setting() { Name = "Test2", Int = 72 }); var results = new List <string>(); var resultInts = new List <string>(); foreach (Ini.Setting setting in section) { results.Add(setting.Name); resultInts.Add(setting.Value); } Assert.AreEqual("Test1", results[0]); Assert.AreEqual("5", resultInts[0]); Assert.AreEqual("Test2", results[1]); Assert.AreEqual("72", resultInts[1]); }
public void SaveIni(Ini ini) { for (int i = 0; i < m_alsUnitGroups.Count; i++) { Ini.Section sec = new Ini.Section("UnitGroup " + i); ((UnitGroup)m_alsUnitGroups[i]).AddIniProperties(sec); ini.Add(sec); } }
public void StoreValue() { try { var section = new Ini.Section("Test"); var key0 = new Ini.Setting { Name = "Key0", Value = "#\"bob\"", Comment = "string" }; Debug.WriteLine(key0.Value); Assert.AreEqual("#\"bob\"", key0.Value); var key1 = new Ini.Setting { Name = "Key1", Comment = "float" }; key1.Set(0.5f); Debug.WriteLine(key1.Value); Assert.AreEqual("0.5", key1.Value); var intarray = new Ini.ExposedSetting { Name = "IntArray", Comment = "ints" }; intarray.SetArray(new[] { 1, 2f, 3 }); Debug.WriteLine(intarray.ExposedEscapedValue); Assert.AreEqual("{ 1, 2, 3 }", intarray.ExposedEscapedValue); var strarray = new Ini.ExposedSetting { Name = "StringArray", Comment = "strings" }; strarray.SetArray(new[] { "abc", "xyz" }); Debug.WriteLine(strarray.ExposedEscapedValue); Assert.AreEqual("{ abc, xyz }", strarray.ExposedEscapedValue); var badarray = new Ini.ExposedSetting { Name = "BadArray", Comment = "bad strings" }; badarray.SetArray(new[] { "a#b#c", "x\"y\"z", "\"doop\"#", "good", " spaced", "#bad", "#\"test\"" }); Debug.WriteLine(badarray.ExposedEscapedValue); Assert.AreEqual("{ \"a#b#c\", \"x\\\"y\\\"z\", \"\\\"doop\\\"#\", good, \" spaced\", \"#bad\", \"#\\\"test\\\"\" }", badarray.ExposedEscapedValue); var quotedArray = new Ini.Setting { Name = "test", Value = "kakka\"bob\"" }; section.Add(key0); section.Add(key1); section.Add(intarray); section.Add(strarray); } catch (Exception ex) { Assert.Fail(ex.Message); } }
public static UnitGroup FromIniSection(Ini.Section sec) { UnitGroup ug = new UnitGroup(sec["Name"].Value); ug.Side = (Side)int.Parse(sec["Side"].Value); ug.Aggressiveness = (Aggressiveness)int.Parse(sec["Aggressiveness"].Value); ug.LoopForever = (int.Parse(sec["LoopForever"].Value) != 0); ug.CreateAtLevelLoad = (int.Parse(sec["CreateAtLevelLoad"].Value) != 0); ug.RandomGroup = (int.Parse(sec["RandomGroup"].Value) != 0); ug.Spawn = (int.Parse(sec["Spawn"].Value) != 0); ug.ReplaceDestroyedGroup = (int.Parse(sec["ReplaceGroup"].Value) != 0); if (sec["SpawnArea"] != null) { ug.SpawnArea = CaTypeArea.GetAreaNameFromIndex(int.Parse(sec["SpawnArea"].Value)); } ug.Health = int.Parse(sec["Health"].Value); // Units string strUTC = null; if (sec["Units"] != null) { strUTC = sec["Units"].Value; } if (strUTC != null) { Regex re = new Regex(@"^(?<count>\d+),(?<end>.*)$"); Match m = re.Match(strUTC); string strT = m.Groups["end"].Value; while (strT.Length != 0) { UnitTypeAndCount utc = new UnitTypeAndCount(); strT = utc.FromSaveString(strT); ug.UnitTypeAndCounts.Add(utc); re = new Regex(@"^\s*,(?<end>.*)$"); m = re.Match(strT); strT = m.Groups["end"].Value; } } // UnitGroup actions foreach (Ini.Property prop in sec.Properties) { if (prop.Name != "A") { continue; } CaBase cab = UnitGroupActionLoader.LoadIni(prop.Value); ug.Actions.Add(cab); } return(ug); }
public void LoadIni(Ini ini) { for (int index = 0; true; index++) { Ini.Section sec = ini["UnitGroup " + index]; if (sec == null) { break; } UnitGroup ug = UnitGroup.FromIniSection(sec); AddUnitGroup(ug); } }
public void AddIniProperties(Ini.Section sec) { // Save conditions & actions foreach (CaBase cab in m_alsConditions) { if (!(cab is CommentCondition)) { sec.Add(new Ini.Property("C", cab.ToSaveString())); } } foreach (CaBase cab in m_alsActions) { if (!(cab is CommentTriggerAction)) { sec.Add(new Ini.Property("A", cab.ToSaveString())); } } }
public void EmptyArray() { var section = new Ini.Section("Test"); string intSettingName = "IntArray"; string stringSettingName = "StringArray"; string nullSettingName = "NullArray"; section[intSettingName].IntArray = new int[] { }; section[stringSettingName].Array = new string[] { }; Assert.Throws <ArgumentNullException>(delegate { section[nullSettingName].Array = null; }); //var nullArray = section[nullSettingName].Array; var stringArray = section[stringSettingName].StringArray; var intArray = section[intSettingName].IntArray; //Assert.AreEqual(null, nullArray?.Length ?? null); Assert.AreEqual(0, intArray.Length); Assert.AreEqual(0, stringArray.Length); }
public void NameChange() { var config = new Ini.Config(); var section = new Ini.Section("Test"); config.Add(section); var setting = new Ini.Setting() { Name = "Name" }; section.Add(setting); int mods = config.Changes; section.Name = "tseT"; Assert.AreEqual(mods + 1, config.Changes); setting.Name = "emaN"; Assert.AreEqual(mods + 2, config.Changes); }
public static string ImportExportMixMap(string strFile, LevelDoc lvld) { // Load ini Ini ini = new Ini(strFile); // Get name of level String strName = strFile; Ini.Section secBasic = ini["Basic"]; if (secBasic != null) { Ini.Property propName = secBasic["Name"]; if (propName != null) { strName = propName.Value; } else { Ini.Property propBrief = secBasic["Brief"]; if (propBrief != null) { strName = propBrief.Value; } } } // Get theater Ini.Section secMap = ini["MAP"]; if (secMap == null) { MessageBox.Show("Could not load " + strFile); return(null); } Theater theater; switch (secMap["Theater"].Value) { case "DESERT": theater = Theater.Desert; break; case "TEMPERATE": theater = Theater.Temperate; break; case "WINTER": theater = Theater.Winter; break; default: MessageBox.Show("Could not load " + strFile); return(null); } // Get bounds & invert int xLeft = Int32.Parse(secMap["X"].Value); int yTop = Int32.Parse(secMap["Y"].Value); int cxWidth = Int32.Parse(secMap["Width"].Value); int cyHeight = Int32.Parse(secMap["Height"].Value); Rectangle rcBounds = new Rectangle(64 - (xLeft + cxWidth), yTop, cxWidth, cyHeight); // We're ready to go lvld.Title = strName; //fixme //lvld.TileCollectionFileName = theater.ToString() + ".tc"; lvld.Bounds = rcBounds; // Load up MixTemplate[] amixt = MixSuck.LoadTemplates(theater); MixSuck.ImportTemplates(amixt, lvld.GetTemplateDoc()); Stream stm = (Stream) new FileStream(strFile.ToLower().Replace(".ini", ".bin"), FileMode.Open, FileAccess.Read, FileShare.Read); MixMap map = MixSuck.LoadMap(stm, amixt); stm.Close(); map.ImportMap(lvld); //fixme //lvld.SetBackgroundTemplate(lvld.GetTemplateDoc()FindTemplate(0)); // Save string strFileLvld = strName + "_" + Path.GetFileName(strFile).Replace(".ini", ".ld"); lvld.SaveAs(strFileLvld); // Also save a scaled image for reference TemplateDoc tmpd = lvld.GetTemplateDoc(); Bitmap bm = lvld.GetMapBitmap(tmpd.TileSize, tmpd, true); int cx; int cy; if (bm.Width > bm.Height) { cx = 128; cy = bm.Height * 128 / bm.Width; } else { cx = bm.Width * 128 / bm.Height; cy = 128; } bm = (Bitmap)bm.GetThumbnailImage(cx, cy, null, IntPtr.Zero); bm.Save(strFileLvld.Replace(".ld", ".png"), ImageFormat.Png); bm.Dispose(); // Save a full 24x24 original map image for reference map.SaveMapBitmap("cc_" + strFileLvld.Replace(".ld", ".png")); return(strFileLvld); }
public void AddIniProperties(Ini.Section sec) { // Save Name sec.Add(new Ini.Property("Name", Name)); // Save Side sec.Add(new Ini.Property("Side", "k" + m_side.ToString())); // Save Aggressiveness sec.Add(new Ini.Property("Aggressiveness", "knAggressiveness" + m_aggr.ToString())); // Save flags sec.Add(new Ini.Property("LoopForever", m_fLoopForever ? "1" : "0")); sec.Add(new Ini.Property("CreateAtLevelLoad", m_fCreateAtLevelLoad ? "1" : "0")); sec.Add(new Ini.Property("RandomGroup", m_fRandomGroup ? "1" : "0")); sec.Add(new Ini.Property("Spawn", m_fSpawn ? "1" : "0")); sec.Add(new Ini.Property("ReplaceGroup", m_fReplaceDestroyedGroup ? "1" : "0")); // Save SpawnArea int nSpawnArea = CaTypeArea.GetArea(m_strSpawnArea); if (nSpawnArea != -1) { sec.Add(new Ini.Property("SpawnArea", nSpawnArea.ToString())); } // Save Health sec.Add(new Ini.Property("Health", Health.ToString())); // Save unit list if (m_alsUnitTypeAndCounts.Count > 0) { // Write total # of units int cTotalUnits = 0; foreach (UnitTypeAndCount utc in m_alsUnitTypeAndCounts) { cTotalUnits += utc.c; } string str = cTotalUnits.ToString(); // Write unit/count pairs foreach (UnitTypeAndCount utc in m_alsUnitTypeAndCounts) { str += "," + utc.ToSaveString(); } sec.Add(new Ini.Property("Units", str)); } // Save actions foreach (CaBase cab in m_alsActions) { if (!(cab is CommentUnitGroupAction)) { sec.Add(new Ini.Property("A", cab.ToSaveString())); } } }
public void LoadIni(Ini ini) { Hashtable map = new Hashtable(); Trigger tgrCurrent = null; Ini.Section sec = ini["Triggers"]; foreach (Ini.Property prop in sec.Properties) { if (prop.Name == "Count") { continue; } if (prop.Name == "T") { tgrCurrent = new Trigger(); int nfSides = 0; foreach (string key in prop.Value.Split(',')) { Side side = (Side)int.Parse(key.Split(':')[0]); nfSides |= SideToMask(side); map.Add(key, tgrCurrent); } tgrCurrent.Sides = nfSides; m_alsTriggers.Add(tgrCurrent); continue; } if (prop.Name == "C") { tgrCurrent.Conditions.Add(TriggerConditionLoader.LoadIni(prop.Value)); continue; } if (prop.Name == "A") { tgrCurrent.Actions.Add(TriggerActionLoader.LoadIni(prop.Value)); continue; } } // Add the triggers for each side in proper order for (int side = 0; side < m_aalsSideTriggers.Length; side++) { int index = 0; while (true) { bool fFound = false; foreach (string key in map.Keys) { int sideT = int.Parse(key.Split(':')[0]); if (sideT != side) { continue; } int indexT = int.Parse(key.Split(':')[1]); if (indexT != index) { continue; } fFound = true; m_aalsSideTriggers[side].Add(map[key]); } if (!fFound) { break; } index = index + 1; } } // Go through all the triggers and search for demo check trigger. // There should be only one, but check them all. ArrayList alsRemove = new ArrayList(); foreach (Trigger tgr in m_alsTriggers) { foreach (CaBase cab in tgr.Conditions) { if (cab.GetType() == typeof(TestPvarCondition)) { TestPvarCondition cdn = (TestPvarCondition)cab; if (cdn.GetVariableString() == "$demo") { alsRemove.Add(tgr); break; } } } } foreach (Trigger tgr in alsRemove) { RemoveTrigger(tgr); } // Triggers have been modified SetModified(); }
public Ini.Section GetIniSection(bool fDemoCheckTrigger) { // If asked create a trigger causes mission failure if running on // demo version side1 bool fModifiedSave = m_fModified; Trigger tgrDemo = new Trigger(); if (fDemoCheckTrigger) { // condition: persistent variable $demo is exactly 1 // action: end mission: lose // Condition tgrDemo.Sides = SideToMask(Side.side1); TestPvarCondition cdn = new TestPvarCondition(); cdn.Active = true; CaTypeText catText = (CaTypeText)cdn.GetTypes()[0]; catText.Text = "$demo"; CaTypeQualifiedNumber catQualNum = (CaTypeQualifiedNumber)cdn.GetTypes()[1]; catQualNum.Qualifier = Qualifier.Exactly; catQualNum.Value = 1; tgrDemo.Conditions.Add(cdn); // Action EndMissionTriggerAction acn = new EndMissionTriggerAction(); acn.Active = true; CaTypeWinLose catWinLose = (CaTypeWinLose)acn.GetTypes()[0]; catWinLose.Result = WinLoseType.Lose; tgrDemo.Actions.Add(acn); // Add this trigger temporarily // Move it up to first place AddTrigger(tgrDemo); while (MoveUpTrigger(Side.side1, tgrDemo) != -1) { ; } } // Save triggers Ini.Section sec = new Ini.Section("Triggers"); sec.Add(new Ini.Property("Count", m_alsTriggers.Count.ToString())); foreach (Trigger tgr in m_alsTriggers) { // Calc per side indexes string strT = ""; for (int n = 0; n < m_aalsSideTriggers.Length; n++) { ArrayList als = (ArrayList)m_aalsSideTriggers[n]; int j = als.IndexOf(tgr); if (j != -1) { if (strT != "") { strT += ","; } string strType = "k" + ((Side)n).ToString(); strT += strType + ":" + j.ToString(); } } sec.Add(new Ini.Property("T", strT)); // Save trigger contents tgr.AddIniProperties(sec); } // Restore order if (fDemoCheckTrigger) { m_fModified = fModifiedSave; RemoveTrigger(tgrDemo); } return(sec); }
public void RecursiveSaveAndLoad(int repeats) { if (repeats < 2) { repeats = 2; } if (repeats > 30) { repeats = 30; } var config = new Ini.Config(); var section1Name = "Number Tests"; var section2Name = "String Tests"; var section1Var1Name = "IntArray"; var section1Var2Name = "DoubleValue"; var section2Var1Name = "StringArray"; var section2Var2Name = "String"; var s1var1value = new int[] { 1, 2, 3 }; var s1var1valueEscaped = "{ 1, 2, 3 }"; var s1var2value = 0.5d; var s1var2valueFormatted = "0.5"; var s2var1value = new string[] { "abc", "xyz" }; var s2var1valueEscaped = "{ abc, xyz }"; var s2var2value = "Bad\"#"; var s2var2valueEscaped = "\"Bad\\\"#\""; var section1 = new Ini.Section(section1Name); section1.Add(new Ini.Setting() { Name = section1Var1Name, IntArray = s1var1value }); section1.Add(new Ini.Setting() { Name = section1Var2Name, Double = s1var2value }); config.Add(section1); var section2 = new Ini.Section(section2Name); section2.Add(new Ini.Setting() { Name = section2Var1Name, Array = s2var1value }); section2.Add(new Ini.Setting() { Name = section2Var2Name, Value = s2var2value }); config.Add(section2); const string incrName = "Incrementor"; const string rollName = "Roller"; var section3 = new Section(incrName); section3.Add(new Setting() { Name = rollName, Int = 0 }); config.Add(section3); var data = config.GetLines(); for (int i = 0; i < repeats; i++) { config = Ini.Config.FromData(data); // read written config Assert.AreEqual(3, config.ItemCount); var s1 = config.Get(section1Name); var s2 = config.Get(section2Name); Assert.IsNotNull(s1); Assert.IsNotNull(s2); var s1v1 = s1.Get(section1Var1Name); var s1v2 = s1.Get(section1Var2Name); var s2v1 = s2.Get(section2Var1Name); var s2v2 = s2.Get(section2Var2Name); Assert.IsNotNull(s1v1); Assert.IsNotNull(s1v2); Assert.IsNotNull(s2v1); Assert.IsNotNull(s2v2); Assert.AreEqual(s1var1valueEscaped, new ExposedSetting(s1v1).ExposedEscapedValue); Assert.AreEqual(s1var2valueFormatted, s1v2.Value); Assert.AreEqual(s2var1valueEscaped, new ExposedSetting(s2v1).ExposedEscapedValue); Assert.AreEqual(s2var2valueEscaped, new ExposedSetting(s2v2).ExposedEscapedValue); int num = config[incrName][rollName].Int; Assert.AreEqual(i, num); config[incrName][rollName].Int = num + 1; data = config.GetLines(); // re-write config } }