From e633408665d06f0c7ca7003845b4dac549e8c0e5 Mon Sep 17 00:00:00 2001 From: Lev Rusanov <30170278+JDM170@users.noreply.github.com> Date: Thu, 27 Feb 2025 22:18:41 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Реализовано сохранение и восстановление бекапа * Обновлен RegexHandler Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com> --- MainForm.Designer.cs | 12 +++++++----- MainForm.cs | 38 ++++++++++++++++++++++++++++++++------ RegexHandler.cs | 17 ++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 1b7a3d3..10d454b 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -487,7 +487,7 @@ this.btn_unlock_garages.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.btn_unlock_garages.Location = new System.Drawing.Point(12, 458); this.btn_unlock_garages.Name = "btn_unlock_garages"; - this.btn_unlock_garages.Size = new System.Drawing.Size(115, 34); + this.btn_unlock_garages.Size = new System.Drawing.Size(105, 34); this.btn_unlock_garages.TabIndex = 20; this.btn_unlock_garages.Text = "Unlock garages"; this.btn_unlock_garages.UseVisualStyleBackColor = true; @@ -496,23 +496,25 @@ // this.btn_recover_backup.Enabled = false; this.btn_recover_backup.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); - this.btn_recover_backup.Location = new System.Drawing.Point(133, 458); + this.btn_recover_backup.Location = new System.Drawing.Point(131, 458); this.btn_recover_backup.Name = "btn_recover_backup"; - this.btn_recover_backup.Size = new System.Drawing.Size(116, 34); + this.btn_recover_backup.Size = new System.Drawing.Size(105, 34); this.btn_recover_backup.TabIndex = 21; this.btn_recover_backup.Text = "Recover backup"; this.btn_recover_backup.UseVisualStyleBackColor = true; + this.btn_recover_backup.Click += new System.EventHandler(this.btn_recover_backup_Click); // // btn_apply_changes // this.btn_apply_changes.Enabled = false; this.btn_apply_changes.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); - this.btn_apply_changes.Location = new System.Drawing.Point(254, 458); + this.btn_apply_changes.Location = new System.Drawing.Point(250, 458); this.btn_apply_changes.Name = "btn_apply_changes"; - this.btn_apply_changes.Size = new System.Drawing.Size(101, 34); + this.btn_apply_changes.Size = new System.Drawing.Size(105, 34); this.btn_apply_changes.TabIndex = 22; this.btn_apply_changes.Text = "Apply changes"; this.btn_apply_changes.UseVisualStyleBackColor = true; + this.btn_apply_changes.Click += new System.EventHandler(this.btn_apply_changes_Click); // // MainForm // diff --git a/MainForm.cs b/MainForm.cs index b89e05b..b6ab207 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; -using System.Text.RegularExpressions; using System.Windows.Forms; namespace SaveWizard_rewritten @@ -40,8 +39,8 @@ namespace SaveWizard_rewritten private class WindowText { - private CheckBox Check { get; set; } - private string Str { get; set; } + private readonly CheckBox Check; + private readonly string Str; public WindowText(CheckBox _check, string _str) { @@ -80,11 +79,13 @@ namespace SaveWizard_rewritten private void GetFileData(string filename) { - string file_data = File.ReadAllText(filename); - RegexHandler.FillLines(Regex.Split(file_data, "\n").ToList()); - + RegexHandler.FillLines(File.ReadAllLines(filename).ToList()); + foreach (KeyValuePair item in items) + { item.Key.Text = RegexHandler.GetValue(RegexHandler.SearchLine(item.Value.GetString())); + item.Value.GetCheck().Checked = true; + } if (RegexHandler.SearchLine("company.volatile.stokes.calais") != 0) selected_game = "ets2"; @@ -216,5 +217,30 @@ namespace SaveWizard_rewritten opened_file_path = ofd.FileName; } } + + private void btn_recover_backup_Click(object sender, EventArgs e) + { + string backup_path = opened_file_path + ".swbak"; + if (!File.Exists(backup_path)) + { + MessageBox.Show("Backup not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + File.WriteAllLines(opened_file_path, File.ReadAllLines(backup_path)); + File.Delete(backup_path); + MessageBox.Show("Backup successfully recovered.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); + GetFileData(opened_file_path); + } + + private void btn_apply_changes_Click(object sender, EventArgs e) + { + foreach (KeyValuePair item in items) + if (!item.Value.GetCheck().Checked) + RegexHandler.SetValue(RegexHandler.SearchLine(item.Value.GetString()), item.Key.Text); + File.WriteAllLines(opened_file_path + ".swbak", RegexHandler.GetBackup()); + File.WriteAllLines(opened_file_path, RegexHandler.GetAllLines()); + MessageBox.Show("Changes successfully applied.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); + GetFileData(opened_file_path); + } } } diff --git a/RegexHandler.cs b/RegexHandler.cs index 4c433c2..b6d96e4 100644 --- a/RegexHandler.cs +++ b/RegexHandler.cs @@ -7,10 +7,16 @@ namespace SaveWizard_rewritten static class RegexHandler { private static List lines; + private static List backup; public static void FillLines(List _lines) { - lines = _lines; + if (_lines == null) + return; + lines = new List(); + lines.AddRange(_lines); + backup = new List(); + backup.AddRange(_lines); } public static string GetLine(int index) @@ -23,6 +29,11 @@ namespace SaveWizard_rewritten return lines; } + public static List GetBackup() + { + return backup; + } + public static int SearchLine(string term, int start = 0, string cancel = "this_string_must_not_exist") { if (lines[start].Contains(term)) @@ -64,7 +75,6 @@ namespace SaveWizard_rewritten public static void SetValue(int line, string value) { - //string name = Regex.Match(lines[line], "(.+):").Value; string name = Regex.Match(lines[line], "(.+):").Groups[1].ToString(); lines[line] = $"{name}: {value}"; } @@ -76,7 +86,6 @@ namespace SaveWizard_rewritten public static int GetArrayLength(int line) { - //return Convert.ToInt32(Regex.Match(lines[line], ": ([0-9]+)$").Value); return Convert.ToInt32(GetValue(line)); } @@ -101,7 +110,6 @@ namespace SaveWizard_rewritten { List items = new List(); for (int i = 0; i < GetArrayLength(line); i++) - //items.Add(Regex.Match(lines[line + i + 1], ": (.+)$").Value); items.Add(Regex.Match(lines[line + i + 1], ": (.+)$").Groups[1].ToString()); if (items.Count == 0) return null; @@ -110,7 +118,6 @@ namespace SaveWizard_rewritten public static void AddArrayValue(int line, string value) { - //string name = Regex.Match(lines[line], "(.+):").Value; string name = Regex.Match(lines[line], "(.+):").Groups[1].ToString(); int length = GetArrayLength(line); lines[line] = $"{name}: {length + 1}";