private void LoadGroupsToDS(KVItem groups, dsGroups ds) { if (groups.key == "Groups") { foreach (KVItem grp in groups.subItems) { // Create the groups row dsGroups.dtGroupsRow groupRow = _dsGroups.dtGroups.NewdtGroupsRow(); // Set the name groupRow.name = grp.key; // Add the group row ds.dtGroups.AdddtGroupsRow(groupRow); // Get the kv pairs foreach (KVItem subitem in grp.subItems) { // What key is this? switch (subitem.key.ToLower()) { case "flags": groupRow.flags = subitem.value.ToLower(); break; case "immunity": groupRow.immunity = Decimal.Parse(subitem.value); break; case "overrides": foreach (KVItem ovr in subitem.subItems) { // Create the override row and attach it to // the group row dsGroups.dtOverridesRow ovrRow = _dsGroups.dtOverrides.NewdtOverridesRow(); ovrRow.dtGroupsRow = groupRow; // Save the key and value ovrRow.key = ovr.key; ovrRow.value = ovr.value.ToLower(); // Add the row ds.dtOverrides.AdddtOverridesRow(ovrRow); } break; default: break; } } } } else { // User tried to load an invalid group file MessageBox.Show("Invalid admin_groups.cfg.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void saveToolStripMenuItem_Click(object sender, EventArgs e) { // Set the filter if (tcMain.SelectedTab == tpAdmin) { sfdMain.Filter = FILTER_ADMIN; } else { sfdMain.Filter = FILTER_GROUP; } // Prompt the user to save the file if (sfdMain.ShowDialog() == DialogResult.OK) { try { // admins.cfg if (sfdMain.Filter == FILTER_ADMIN) { // Create the root section KVItem admins = new KVItem(); admins.key = "Admins"; foreach (dsAdmins.dtAdminsRow row in _dsAdmins.dtAdmins) { // Create the admin KVItem currentAdmin = new KVItem(row.name, null); // Set the the auth and identity currentAdmin.subItems.Add(new KVItem("auth", row.authType)); currentAdmin.subItems.Add(new KVItem("identity", row.identity)); // Make sure group isn't empty before creating the KV pair if (row.group != null && row.group.Trim() != "") { currentAdmin.subItems.Add(new KVItem("group", row.group)); } // Set the immunity and flags currentAdmin.subItems.Add(new KVItem("immunity", ((int)row.immunity).ToString())); currentAdmin.subItems.Add(new KVItem("flags", row.flags)); // Add the admin admins.subItems.Add(currentAdmin); } // Save the file KVParser.WriteToFile(admins, sfdMain.FileName); } // admin_groups.cfg else if (sfdMain.Filter == FILTER_GROUP) { // Create the root section KVItem groups = new KVItem(); groups.key = "Groups"; // Add all the groups foreach (dsGroups.dtGroupsRow groupRow in _dsGroups.dtGroups) { KVItem currentGroup = new KVItem(); currentGroup.key = groupRow.name; // Set the KVs currentGroup.subItems.Add (new KVItem("flags", groupRow.flags)); currentGroup.subItems.Add (new KVItem("immunity", ((int)groupRow.immunity).ToString())); // Get the override rows DataRow[] overrides = groupRow.GetChildRows(_dsGroups.Relations[0]); // Does this group have overrides? if (overrides.Length > 0) { // Yes, add the overrides section KVItem ovrSection = new KVItem(); ovrSection.key = "Overrides"; // Add each override foreach (DataRow row in overrides) { // Get a typed version of the row dsGroups.dtOverridesRow ovrRow = (dsGroups.dtOverridesRow)row; // Create the KV pair ovrSection.subItems.Add(new KVItem(ovrRow.key, ovrRow.value)); } // Add the override section currentGroup.subItems.Add(ovrSection); } // Add the group groups.subItems.Add(currentGroup); } // Save the file KVParser.WriteToFile(groups, sfdMain.FileName); } } catch (StrongTypingException ex) { if (ex.Message.Contains("name")) { MessageBox.Show("You must enter a name.", "Missing Name", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (ex.Message.Contains("identity")) { MessageBox.Show("You must enter an identity.", "Missing Identity", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (ex.Message.Contains("key")) { MessageBox.Show("You must enter a command / command group.", "Missing Command / Command Group", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { // Let the user know something went wrong ErrorHandler.ShowError(ex); } } }