mirror of
https://github.com/netchx/netch.git
synced 2026-03-18 18:13:21 +08:00
重写模式保存
This commit is contained in:
@@ -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;
|
||||
/// <summary>
|
||||
/// 被编辑的模式
|
||||
/// </summary>
|
||||
private readonly Models.Mode _mode;
|
||||
|
||||
//被编辑的模式
|
||||
public readonly Models.Mode EditingMode;
|
||||
|
||||
public bool IsEdited { get; private set; } = false;
|
||||
/// <summary>
|
||||
/// 是否被编辑过
|
||||
/// </summary>
|
||||
public bool Edited { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 编辑模式
|
||||
@@ -24,19 +27,25 @@ namespace Netch.Forms.Mode
|
||||
/// <param name="mode">模式</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -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<string>());
|
||||
|
||||
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<string>());
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ namespace Netch.Models
|
||||
public string Remark;
|
||||
|
||||
/// <summary>
|
||||
/// 文件相对路径
|
||||
/// 文件相对路径(必须是存在的文件)
|
||||
/// </summary>
|
||||
public string RelativePath;
|
||||
|
||||
@@ -85,50 +85,6 @@ namespace Netch.Models
|
||||
return fileString;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入模式文件
|
||||
/// </summary>
|
||||
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());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除模式文件
|
||||
/// </summary>
|
||||
public void DeleteFile()
|
||||
{
|
||||
var fullPath = Path.Combine(Modes.ModeDirectory, RelativePath);
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
File.Delete(fullPath);
|
||||
}
|
||||
}
|
||||
|
||||
public string TypeToString()
|
||||
{
|
||||
return Type switch
|
||||
|
||||
@@ -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);
|
||||
|
||||
/// <summary>
|
||||
/// 从模式文件夹读取模式并为 <see cref="Forms.MainForm.ModeComboBox"/> 绑定数据
|
||||
/// </summary>
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user