forked from KirosHan/Palworld-server-protector-DotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configForm.cs
161 lines (134 loc) · 5.17 KB
/
configForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Palworld_server_protector_DotNet
{
public partial class configForm : Form
{
public configForm()
{
InitializeComponent();
}
List<ConfigItem> ParseConfigFile(string content)
{
var configItems = new List<ConfigItem>();
var translator = new ConfigTranslation();
var noter = new Notes();
// 提取OptionSettings部分
var optionSettingsPrefix = "OptionSettings=(";
var startIndex = content.IndexOf(optionSettingsPrefix);
if (startIndex != -1)
{
startIndex += optionSettingsPrefix.Length;
var endIndex = content.IndexOf(')', startIndex);
if (endIndex != -1)
{
var settings = content.Substring(startIndex, endIndex - startIndex);
var lines = settings.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var parts = line.Split('=');
if (parts.Length == 2)
{
configItems.Add(new ConfigItem
{
中文 = translator.GetTranslation(parts[0].Trim()),
配置项 = parts[0].Trim(),
值 = parts[1].Trim(),
备注 = noter.GetNote(parts[0].Trim())
});
}
}
}
}
return configItems;
}
private string ReadFileContent(string filePath)
{
string content = string.Empty;
try
{
content = File.ReadAllText(filePath);
}
catch (IOException ex)
{
// Handle the exception or display an error message
MessageBox.Show($"读取文件失败: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Task.Run(() => Logger.AppendToErrorLog($"ErrorCode:0xB1>>>服务端配置文件读取错误>>>错误信息:{ex.Message}"));
}
return content;
}
private void SaveConfigItemsToFile(string filePath, List<ConfigItem> items)
{
var sb = new StringBuilder();
sb.Append("[/Script/Pal.PalGameWorldSettings]\nOptionSettings=(");
foreach (var item in items)
{
sb.Append($"{item.配置项}={item.值},");
}
// 移除最后一个逗号
if (sb.Length > 0)
{
sb.Length--;
}
sb.Append(")");
try
{
File.WriteAllText(filePath, sb.ToString());
MessageBox.Show("配置文件保存成功!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (IOException ex)
{
MessageBox.Show($"配置文件保存失败: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Task.Run(() => Logger.AppendToErrorLog($"ErrorCode:0xB2>>>服务端配置文件保存错误>>>错误信息:{ex.Message}"));
}
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "配置文件|*.ini";
openFileDialog.Title = "选择配置文件";
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
string filePath = openFileDialog.FileName;
toolStripTextBox1.Text = filePath;
string fileContent = ReadFileContent(filePath);
// 设置数据源
dataGridView.DataSource = ParseConfigFile(fileContent);
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
// 设置其他列为只读
for (int i = 0; i < dataGridView.Columns.Count; i++)
{
if (i != 2)
{
dataGridView.Columns[i].ReadOnly = true;
}
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dataGridView.DataSource is List<ConfigItem> configItems)
{
var filePath = toolStripTextBox1.Text;
if (!string.IsNullOrWhiteSpace(filePath))
{
SaveConfigItemsToFile(filePath, configItems);
}
else
{
MessageBox.Show("No file selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}