//Save the changes to file and memory private void SaveButton_Click(object sender, EventArgs e) { //validate face key, input santization if (!PublicMethods.faceValid(FaceKeyTextBox.Text.Substring(2))) { MessageBox.Show("Error: Invalid Face Key", "Error"); return; } //create new tuple for changed favorite and insert it into List to be written Tuple <string, string> changedFave = new Tuple <string, string>(NameTextBox.Text, FaceKeyTextBox.Text); faves[index] = changedFave; //write favorites to file. Not perfect solution, but functional enough StreamWriter writer = new StreamWriter(File.Open("favorites.txt", FileMode.Create)); foreach (Tuple <string, string> fave in faves) { writer.WriteLine(fave.Item1 + "\t" + fave.Item2); } writer.Close(); //reopen manage favorites, causes refresh of favorites to memory. Also not idea, but works ManageFavorites mF = new ManageFavorites(faves); mF.Show(); this.Close(); }
//writes the face keys to profiles.dat //index is the row index of the datagridview and newFace is the key to be written public void saveProfiles(int index, string newFace) { //drop the 0x at the beginning newFace = newFace.Substring(2); //check that the key is valid. Input sanization if (PublicMethods.faceValid(newFace)) { string profileFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Mount&Blade Warband\\profiles.dat"; if (!File.Exists(profileFile)) { MessageBox.Show("Profiles.dat has been removed. Reopen warband and try again."); return; } using (BinaryWriter writer = new BinaryWriter(File.Open(profileFile, FileMode.Open))) { //move the binary writer to the position we want to write to writer.Seek((int)facePosition[index], SeekOrigin.Begin); //breakup the facekey and convert to an int format string face1 = newFace.Substring(0, 8); string face2 = newFace.Substring(8, 8); string face3 = newFace.Substring(16, 8); string face4 = newFace.Substring(24, 8); string face5 = newFace.Substring(32, 8); string face6 = newFace.Substring(40, 8); string face7 = newFace.Substring(48, 8); string face8 = newFace.Substring(56, 8); int face1Val = Convert.ToInt32("0x" + face1, 16); int face2Val = Convert.ToInt32("0x" + face2, 16); int face3Val = Convert.ToInt32("0x" + face3, 16); int face4Val = Convert.ToInt32("0x" + face4, 16); int face5Val = Convert.ToInt32("0x" + face5, 16); int face6Val = Convert.ToInt32("0x" + face6, 16); int face7Val = Convert.ToInt32("0x" + face7, 16); int face8Val = Convert.ToInt32("0x" + face8, 16); //write the broken face key to the file writer.Write(face2Val); writer.Write(face1Val); writer.Write(face4Val); writer.Write(face3Val); writer.Write(face6Val); writer.Write(face5Val); writer.Write(face8Val); writer.Write(face7Val); //inform user of completion MessageBox.Show("Done!"); } } else { MessageBox.Show("Error: Invalid Face Code", "Eror"); } }
//saves the favorite to file and memory private void SaveButton_Click(object sender, EventArgs e) { //validate that the key provided is valid, input sanitization if (!PublicMethods.faceValid(FaceKeyTextBox.Text.Substring(2))) { MessageBox.Show("Error: Invalid Face Key", "Error"); return; } //write the key to file StreamWriter writer = new StreamWriter("favorites.txt", true); writer.WriteLine(NameTextBox.Text + "\t" + FaceKeyTextBox.Text, "a"); writer.Close(); //inform user MessageBox.Show(NameTextBox.Text + " added to favorites."); Close(); }