-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Serverlist editor to the Patcher #527
base: master
Are you sure you want to change the base?
Changes from 1 commit
ca39b48
ecf5aed
629ce36
9a9f474
9103eae
ee57e29
a64450a
4cd6ab7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ public partial class MainForm : Form, ILoggingProvider | |
{ | ||
private static MainForm instance; | ||
private GameServer[] gameServers; | ||
private readonly string configLocation; | ||
|
||
public static MainForm GetInstance() | ||
{ | ||
|
@@ -54,26 +55,14 @@ private void ToggleTheme(bool darkModeEnabled) | |
public MainForm() | ||
{ | ||
InitializeComponent(); | ||
|
||
// Try to get ServerList | ||
try | ||
{ | ||
var fileContent = File.ReadAllText("Servers.json", Encoding.UTF8); | ||
gameServers = JsonConvert.DeserializeObject<GameServer[]>(fileContent); | ||
if (gameServers == null || gameServers.Length == 0) | ||
{ | ||
gameServers = CreateDefautServerList(); | ||
} | ||
string appData = Environment.GetFolderPath(Environment | ||
.SpecialFolder | ||
.ApplicationData); | ||
|
||
} | ||
catch ( FileNotFoundException ex) | ||
{ | ||
gameServers = CreateDefautServerList(); | ||
} | ||
catch (JsonSerializationException ex) | ||
{ | ||
gameServers = CreateDefautServerList(); | ||
} | ||
configLocation = $@"{appData}\PeacockProject\"; | ||
|
||
|
||
logListView.Columns[0].Width = logListView.Width - 4 - SystemInformation.VerticalScrollBarWidth; | ||
Timer timer = new Timer | ||
|
@@ -82,7 +71,7 @@ public MainForm() | |
}; | ||
timer.Tick += (sender, args) => MemoryPatcher.PatchAllProcesses(this, CurrentSettings.patchOptions); | ||
timer.Enabled = true; | ||
|
||
gameServers = new GameServer[0]; | ||
try | ||
{ | ||
CurrentSettings = Settings.GetFromFile(); | ||
|
@@ -91,6 +80,26 @@ public MainForm() | |
{ | ||
CurrentSettings = new Settings(); | ||
} | ||
try | ||
{ | ||
var fileContent = File.ReadAllText($@"{configLocation}\Servers.json", Encoding.UTF8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
gameServers = JsonConvert.DeserializeObject<GameServer[]>(fileContent); | ||
if (gameServers == null || gameServers.Length == 0) | ||
{ | ||
gameServers = CreateDefautServerList(); | ||
} | ||
|
||
} | ||
catch (FileNotFoundException ex) | ||
{ | ||
gameServers = CreateDefautServerList(); | ||
} | ||
catch (JsonSerializationException ex) | ||
{ | ||
gameServers = CreateDefautServerList(); | ||
} | ||
SetSelectedServerHostname(serverUrlComboBox.Text); | ||
UpdateServerUrlCombobox(); | ||
updateTrayDomains(); | ||
|
||
ToggleTheme(CurrentSettings.darkModeEnabled); | ||
|
@@ -107,13 +116,14 @@ public MainForm() | |
} | ||
} | ||
|
||
private static GameServer[] CreateDefautServerList() | ||
private GameServer[] CreateDefautServerList() | ||
{ | ||
GameServer[] gameServers = new GameServer[2]; | ||
gameServers[0] = new GameServer { ServerName = "IOI Official", ServerAddress = "config.hitman.io" }; | ||
gameServers[1] = new GameServer { ServerName = "Peacock Local", ServerAddress = "127.0.0.1" }; | ||
var serverList = JsonConvert.SerializeObject(gameServers); | ||
File.WriteAllText("Servers.json", serverList, Encoding.UTF8); | ||
|
||
File.WriteAllText($@"{configLocation}Servers.json", serverList, Encoding.UTF8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
return gameServers; | ||
} | ||
|
||
|
@@ -143,9 +153,9 @@ private void RePatchButton_Click(object sender, EventArgs e) | |
|
||
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) | ||
{ | ||
if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\PeacockProject")) | ||
if (!Directory.Exists(configLocation)) | ||
{ | ||
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\PeacockProject"); | ||
Directory.CreateDirectory(configLocation); | ||
} | ||
|
||
CurrentSettings.SaveToFile(); | ||
|
@@ -223,12 +233,16 @@ private Settings CurrentSettings | |
_currentSettings = value; | ||
|
||
SetSelectedServerHostname(value.patchOptions.CustomConfigDomain); | ||
UpdateServerUrlCombobox(); | ||
} | ||
} | ||
|
||
serverUrlComboBox.Items.Clear(); | ||
foreach(var gameserver in gameServers) | ||
{ | ||
serverUrlComboBox.Items.Add(gameserver.ServerName); | ||
} | ||
private void UpdateServerUrlCombobox() | ||
{ | ||
serverUrlComboBox.Items.Clear(); | ||
foreach (var gameserver in gameServers) | ||
{ | ||
serverUrlComboBox.Items.Add(gameserver.ServerName); | ||
} | ||
} | ||
|
||
|
@@ -333,11 +347,7 @@ private void serverList_Click(object sender, EventArgs e) | |
if (result == DialogResult.OK) | ||
{ | ||
gameServers = serverListEditor.GameServers; | ||
serverUrlComboBox.Items.Clear(); | ||
foreach (var gameserver in gameServers) | ||
{ | ||
serverUrlComboBox.Items.Add(gameserver.ServerName); | ||
} | ||
UpdateServerUrlCombobox(); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,8 +43,13 @@ private void saveButton_Click(object sender, EventArgs e) | |
list.Add(new GameServer { ServerName = item.Cells[0].Value.ToString(), ServerAddress = item.Cells[1].Value.ToString() }); | ||
} | ||
GameServers = list.ToArray(); | ||
string appData = Environment.GetFolderPath(Environment | ||
.SpecialFolder | ||
.ApplicationData); | ||
|
||
var folder = $@"{appData}\PeacockProject\"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also feel there should just be a single static readonly in a file with the path to the configuration and |
||
var serverString = JsonConvert.SerializeObject(GameServers); | ||
File.WriteAllText("Servers.json", serverString, Encoding.UTF8); | ||
File.WriteAllText(@$"{folder}Servers.json", serverString, Encoding.UTF8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
this.DialogResult = DialogResult.OK; | ||
this.Close(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please always use
Path.Combine
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had stolen this line from
Settings.cs
:PThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, there are quite a few weird things in the code that make my hands itch. But if we make sure newly added stuff to be correct, the rest will follow, haha.