-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
208 lines (183 loc) · 7.41 KB
/
MainWindow.xaml.cs
File metadata and controls
208 lines (183 loc) · 7.41 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using Microsoft.Win32;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
namespace Trinity_Configs_Migrator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//TCMNav.SelectedItem = tcm_tab_about;
}
private void titleBarBtnMinimize_Click(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void titleBarBtnExit_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void BtnOldPath_Click(object sender, RoutedEventArgs e)
{
// Configure open file dialog box
var dialog = new OpenFileDialog();
dialog.FileName = "Authserver or Worldserver Config"; // Default file name
dialog.DefaultExt = ".conf"; // Default file extension
dialog.Filter = "Text documents (.conf)|*.conf"; // Filter files by extension
// Show open file dialog box
bool? result = dialog.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
string filename = dialog.FileName;
info_old_path.Text = filename;
}
}
private void BrtnNewPath_Click(object sender, RoutedEventArgs e)
{
// Configure open file dialog box
var dialog = new OpenFileDialog();
dialog.FileName = "Authserver or Worldserver Config"; // Default file name
dialog.DefaultExt = ".conf"; // Default file extension
dialog.Filter = "Text documents (.conf)|*.conf"; // Filter files by extension
// Show open file dialog box
bool? result = dialog.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
string filename = dialog.FileName;
info_new_path.Text = filename;
}
}
private void BtnMigrate_Click(object sender, RoutedEventArgs e)
{
migration_info.Text = "Working on migration documents..";
Task.Delay(1000);
// get whole text from old file
string old_config_content = string.Empty;
try
{
// Open the text file using a stream reader.
using (var sr = new StreamReader(info_old_path.Text))
{
// Read the stream as a string, and write the string to the console.
old_config_content = sr.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//get whole text from new file
string new_config_content = string.Empty;
try
{
// Open the text file using a stream reader.
using (var sr = new StreamReader(info_new_path.Text))
{
// Read the stream as a string, and write the string to the console.
new_config_content = sr.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
int counter = 0;
// get old config file reference
string old_config_ref = string.Empty;
foreach (string line in old_config_content.Split('\n'))
{
if (line.StartsWith("["))
{
old_config_ref = line;
}
}
// get new config file reference
string new_config_ref = string.Empty;
foreach (string line in new_config_content.Split('\n'))
{
if (line.StartsWith("["))
{
new_config_ref = line;
}
}
if (old_config_ref != new_config_ref)
{
migration_info.Foreground = new SolidColorBrush(Colors.DarkRed);
migration_info.Text = "You are trying to migrate settings from 2 different config references, for example transfering authserver settings to worldserver.conf";
}
else
{
// Read the file and display it line by line.
foreach (string oldline in old_config_content.Split('\n'))
{
// if line doesn't start with "#", or not null or not empty then definetely found a variable
if (!oldline.StartsWith("[") && !oldline.StartsWith("#") && !string.IsNullOrEmpty(oldline) && !string.IsNullOrWhiteSpace(oldline))
{
// get varialbe name
string s_old_var_name = string.Empty;
int charLocation = oldline.IndexOf("=", StringComparison.Ordinal);
if (charLocation > 0)
{
s_old_var_name = oldline.Substring(0, charLocation);
}
string s_old_var_value = oldline.Substring(oldline.LastIndexOf('=') + 1);
// check if variable exists in the new config file
if (!new_config_content.Contains(s_old_var_name))
continue;
int lineNumber = 0;
foreach (string lineN in new_config_content.Split('\n'))
{
lineNumber++;
if (lineN.Contains(s_old_var_name) && !lineN.StartsWith("#") && lineN != oldline)
{
LineChanger(oldline.Replace("\n", "").Replace("\r", ""), info_new_path.Text, lineNumber);
//MessageBox.Show($"transfering value for {s_old_var_name} to new config at line number [{lineNumber}]");
counter++;
break;
}
}
}
}
if (counter > 0)
{
migration_info.Foreground = new SolidColorBrush(Colors.DarkGreen);
migration_info.Text = $"{counter} settings transfered.";
}
else
{
migration_info.Foreground = new SolidColorBrush(Colors.DarkOrange);
migration_info.Text = $"{counter} settings transfered, nothing different.";
}
}
}
static void LineChanger(string newText, string fileName, int line_to_edit)
{
int currentLineNumber = 0;
string[] lines = File.ReadAllLines(fileName);
foreach (string line in lines)
{
if (currentLineNumber == line_to_edit)
{
lines[currentLineNumber -1] = newText;
break;
}
currentLineNumber++;
}
File.WriteAllLines(fileName, lines);
}
}
}