From 1c231ee89703caa7c7f5a04082f06ed4d40f3b8b Mon Sep 17 00:00:00 2001
From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com>
Date: Sat, 29 Aug 2020 12:06:32 +0800
Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E6=A8=A1=E5=BC=8F=E4=BF=9D?=
=?UTF-8?q?=E5=AD=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Netch/Forms/Mode/Process.cs | 174 ++++++++++++++----------------------
Netch/Models/Mode.cs | 46 +---------
Netch/Utils/Modes.cs | 42 +++++++--
3 files changed, 102 insertions(+), 160 deletions(-)
diff --git a/Netch/Forms/Mode/Process.cs b/Netch/Forms/Mode/Process.cs
index 2299cef6..662c7461 100644
--- a/Netch/Forms/Mode/Process.cs
+++ b/Netch/Forms/Mode/Process.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -10,13 +11,15 @@ namespace Netch.Forms.Mode
{
public partial class Process : Form
{
- //用于判断当前窗口是否为编辑模式
- public readonly bool IsEditing;
+ ///
+ /// 被编辑的模式
+ ///
+ private readonly Models.Mode _mode;
- //被编辑的模式
- public readonly Models.Mode EditingMode;
-
- public bool IsEdited { get; private set; } = false;
+ ///
+ /// 是否被编辑过
+ ///
+ public bool Edited { get; private set; }
///
/// 编辑模式
@@ -24,19 +27,25 @@ namespace Netch.Forms.Mode
/// 模式
public Process(Models.Mode mode)
{
+ if (mode.Type != 0)
+ {
+ throw new Exception("请传入进程模式");
+ }
+
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
- EditingMode = mode;
Text = "Edit Process Mode";
- //循环填充已有规则
- mode.Rule.ForEach(i => RuleListBox.Items.Add(i));
+ this._mode = mode;
+ RuleListBox.Items.AddRange(mode.Rule.ToArray());
+
+ #region 禁用文件名更改
- IsEditing = true;
RemarkTextBox.TextChanged -= RemarkTextBox_TextChanged;
- FilenameTextBox.Enabled = false;
- FilenameLabel.Enabled = false;
- UseCustomFilenameBox.Enabled = false;
+ FilenameTextBox.Enabled =
+ UseCustomFilenameBox.Enabled = false;
+
+ #endregion
FilenameTextBox.Text = mode.FileName;
RemarkTextBox.Text = mode.Remark;
@@ -47,11 +56,7 @@ namespace Netch.Forms.Mode
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
- EditingMode = null;
-
FilenameTextBox.Enabled = false;
- FilenameLabel.Enabled = false;
- IsEditing = false;
}
///
@@ -91,7 +96,7 @@ namespace Netch.Forms.Mode
if (FileChildInfo.Name.EndsWith(".exe") && !RuleListBox.Items.Contains(FileChildInfo.Name))
{
RuleListBox.Items.Add(FileChildInfo.Name);
- IsEdited = true;
+ Edited = true;
}
}
}
@@ -133,7 +138,7 @@ namespace Netch.Forms.Mode
{
if (RuleListBox.SelectedIndex == -1) return;
RuleListBox.Items.RemoveAt(RuleListBox.SelectedIndex);
- IsEdited = true;
+ Edited = true;
}
private async void AddButton_Click(object sender, EventArgs e)
@@ -153,7 +158,7 @@ namespace Netch.Forms.Mode
RuleListBox.Items.Add(process);
}
- IsEdited = true;
+ Edited = true;
RuleListBox.SelectedIndex = RuleListBox.Items.IndexOf(process);
ProcessNameTextBox.Text = string.Empty;
}
@@ -176,105 +181,58 @@ namespace Netch.Forms.Mode
public void ControlButton_Click(object sender, EventArgs e)
{
- if (IsEditing)
+ if (RuleListBox.Items.Count == 0)
{
- // 编辑模式
- if (RuleListBox.Items.Count != 0)
- {
- EditingMode.BypassChina = false;
- EditingMode.FileName = FilenameTextBox.Text;
- EditingMode.Type = 0;
- EditingMode.Remark = RemarkTextBox.Text;
- EditingMode.Rule.Clear();
+ MessageBoxX.Show(i18N.Translate("Unable to add empty rule"));
+ return;
+ }
- var text = $"# {RemarkTextBox.Text}, 0\r\n";
- foreach (var item in RuleListBox.Items)
- {
- var process = item as string;
- EditingMode.Rule.Add(process);
- text += process + "\r\n";
- }
+ if (string.IsNullOrWhiteSpace(RemarkTextBox.Text))
+ {
+ MessageBoxX.Show(i18N.Translate("Please enter a mode remark"));
+ return;
+ }
- text = text.Substring(0, text.Length - 2);
+ if (string.IsNullOrWhiteSpace(FilenameTextBox.Text))
+ {
+ MessageBoxX.Show(i18N.Translate("Please enter a mode filename"));
+ return;
+ }
- if (!Directory.Exists("mode"))
- {
- Directory.CreateDirectory("mode");
- }
+ if (_mode != null)
+ {
+ _mode.Remark = RemarkTextBox.Text;
+ _mode.Rule.Clear();
+ _mode.Rule.AddRange(RuleListBox.Items.Cast());
- File.WriteAllText(Path.Combine("mode", FilenameTextBox.Text) + ".txt", text);
-
- MessageBoxX.Show(i18N.Translate("Mode updated successfully"));
-
- IsEdited = false;
- Close();
- }
- else
- {
- MessageBoxX.Show(i18N.Translate("Unable to add empty rule"));
- }
+ Modes.WriteFile(_mode);
+ Edited = false;
+ MessageBoxX.Show(i18N.Translate("Mode updated successfully"));
}
else
{
- Configuration.Save();
- if (!string.IsNullOrWhiteSpace(RemarkTextBox.Text))
+ var fullName = Modes.GetFullPath(FilenameTextBox.Text + ".txt");
+ if (File.Exists(fullName))
{
- if (string.IsNullOrWhiteSpace(FilenameTextBox.Text))
- {
- MessageBoxX.Show(i18N.Translate("Please enter a mode filename"));
- return;
- }
-
- var modeFilename = Path.Combine("mode", FilenameTextBox.Text);
- // 如果文件已存在,返回
- if (File.Exists(modeFilename + ".txt"))
- {
- MessageBoxX.Show(i18N.Translate("File already exists.\n Please Change the filename"));
- return;
- }
-
- if (RuleListBox.Items.Count != 0)
- {
- var mode = new Models.Mode
- {
- BypassChina = false,
- FileName = FilenameTextBox.Text,
- Type = 0,
- Remark = RemarkTextBox.Text
- };
-
- var text = $"# {RemarkTextBox.Text}, 0\r\n";
- foreach (var item in RuleListBox.Items)
- {
- var process = item as string;
- mode.Rule.Add(process);
- text += process + "\r\n";
- }
-
- text = text.Substring(0, text.Length - 2);
-
- if (!Directory.Exists("mode"))
- {
- Directory.CreateDirectory("mode");
- }
-
- File.WriteAllText(modeFilename + ".txt", text);
-
- MessageBoxX.Show(i18N.Translate("Mode added successfully"));
-
- Modes.Add(mode);
- Close();
- }
- else
- {
- MessageBoxX.Show(i18N.Translate("Unable to add empty rule"));
- }
+ MessageBoxX.Show(i18N.Translate("File already exists.\n Please Change the filename"));
+ return;
}
- else
+
+ var mode = new Models.Mode
{
- MessageBoxX.Show(i18N.Translate("Please enter a mode remark"));
- }
+ BypassChina = false,
+ FileName = FilenameTextBox.Text,
+ Type = 0,
+ Remark = RemarkTextBox.Text
+ };
+ mode.Rule.AddRange(RuleListBox.Items.Cast());
+
+ Modes.WriteFile(mode);
+ Modes.Add(mode);
+ MessageBoxX.Show(i18N.Translate("Mode added successfully"));
}
+
+ Close();
}
private async void RemarkTextBox_TextChanged(object sender, EventArgs e)
@@ -297,7 +255,7 @@ namespace Netch.Forms.Mode
private void UseCustomFilenameBox_CheckedChanged(object sender, EventArgs e)
{
- FilenameTextBox.Enabled = FilenameLabel.Enabled = ((CheckBox) sender).Checked;
+ FilenameTextBox.Enabled = UseCustomFilenameBox.Checked;
}
}
}
\ No newline at end of file
diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs
index e1d91f4b..60d21300 100644
--- a/Netch/Models/Mode.cs
+++ b/Netch/Models/Mode.cs
@@ -14,7 +14,7 @@ namespace Netch.Models
public string Remark;
///
- /// 文件相对路径
+ /// 文件相对路径(必须是存在的文件)
///
public string RelativePath;
@@ -85,50 +85,6 @@ namespace Netch.Models
return fileString;
}
- ///
- /// 写入模式文件
- ///
- public void ToFile(string Dir)
- {
- if (!System.IO.Directory.Exists(Dir))
- {
- System.IO.Directory.CreateDirectory(Dir);
- }
-
- var NewPath = System.IO.Path.Combine(Dir, FileName);
- if (System.IO.File.Exists(NewPath + ".txt"))
- {
- // 重命名该模式文件名
- NewPath += "_";
-
- while (System.IO.File.Exists(NewPath + ".txt"))
- {
- // 循环重命名该模式文件名,直至不重名
- NewPath += "_";
- }
- }
-
- FileName = System.IO.Path.GetFileName(NewPath);
-
- // 加上文件名后缀
- NewPath += ".txt";
-
- // 写入到模式文件里
- System.IO.File.WriteAllText(NewPath, ToFileString());
- }
-
- ///
- /// 删除模式文件
- ///
- public void DeleteFile()
- {
- var fullPath = Path.Combine(Modes.ModeDirectory, RelativePath);
- if (File.Exists(fullPath))
- {
- File.Delete(fullPath);
- }
- }
-
public string TypeToString()
{
return Type switch
diff --git a/Netch/Utils/Modes.cs b/Netch/Utils/Modes.cs
index e37944d2..6f0752c3 100644
--- a/Netch/Utils/Modes.cs
+++ b/Netch/Utils/Modes.cs
@@ -12,6 +12,10 @@ namespace Netch.Utils
public static readonly string ModeDirectory = Path.Combine(Global.NetchDir, $"{MODE_DIR}\\");
+ public static string GetRelativePath(string fullName) => fullName.Substring(ModeDirectory.Length);
+ public static string GetFullPath(string relativeName) => Path.Combine(ModeDirectory, relativeName);
+ public static string GetFullPath(Mode mode) => Path.Combine(ModeDirectory, mode.RelativePath);
+
///
/// 从模式文件夹读取模式并为 绑定数据
///
@@ -43,15 +47,15 @@ namespace Netch.Utils
Sort();
}
- private static void LoadModeFile(string path)
+ private static void LoadModeFile(string fullName)
{
var mode = new Mode
{
- FileName = Path.GetFileNameWithoutExtension(path),
- RelativePath = path.Substring(ModeDirectory.Length)
+ FileName = Path.GetFileNameWithoutExtension(fullName),
+ RelativePath = GetRelativePath(fullName)
};
- var content = File.ReadAllLines(path);
+ var content = File.ReadAllLines(fullName);
if (content.Length == 0) return;
for (var i = 0; i < content.Length; i++)
@@ -67,8 +71,8 @@ namespace Netch.Utils
if ((tmp = splited.ElementAtOrDefault(0)) != null)
mode.Remark = i18N.Translate(tmp);
- if ((tmp = splited.ElementAtOrDefault(1)) != null)
- mode.Type = int.Parse(tmp);
+ tmp = splited.ElementAtOrDefault(1);
+ mode.Type = tmp != null ? int.Parse(tmp) : 0;
if ((tmp = splited.ElementAtOrDefault(2)) != null)
mode.BypassChina = int.Parse(tmp) == 1;
@@ -88,6 +92,25 @@ namespace Netch.Utils
Global.Modes.Add(mode);
}
+ public static void WriteFile(Mode mode)
+ {
+ if (!Directory.Exists(ModeDirectory))
+ {
+ Directory.CreateDirectory(ModeDirectory);
+ }
+
+ var fullName = GetFullPath(mode.RelativePath ?? mode.FileName + ".txt");
+
+ if (mode.RelativePath == null && File.Exists(fullName))
+ {
+ throw new Exception("新建模式的文件名已存在,请贡献者检查代码");
+ }
+
+ // 写入到模式文件里
+ File.WriteAllText(fullName, mode.ToFileString());
+ mode.RelativePath = GetRelativePath(fullName);
+ }
+
private static void Sort()
{
Global.Modes.Sort((a, b) => string.Compare(a.Remark, b.Remark, StringComparison.Ordinal));
@@ -102,7 +125,12 @@ namespace Netch.Utils
public static void Delete(Mode mode)
{
- mode.DeleteFile();
+ var fullName = GetFullPath(mode);
+ if (File.Exists(fullName))
+ {
+ File.Delete(fullName);
+ }
+
Global.Modes.Remove(mode);
Global.MainForm.InitMode();
}