Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 43 additions & 33 deletions patcher/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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\";
Copy link
Member

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.

Copy link
Author

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 :P

Copy link
Member

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.



logListView.Columns[0].Width = logListView.Width - 4 - SystemInformation.VerticalScrollBarWidth;
Timer timer = new Timer
Expand All @@ -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();
Expand All @@ -91,6 +80,26 @@ public MainForm()
{
CurrentSettings = new Settings();
}
try
{
var fileContent = File.ReadAllText($@"{configLocation}\Servers.json", Encoding.UTF8);
Copy link
Member

Choose a reason for hiding this comment

The 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);
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

return gameServers;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion patcher/ServerListEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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\";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Member

Choose a reason for hiding this comment

The 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 server.json.

var serverString = JsonConvert.SerializeObject(GameServers);
File.WriteAllText("Servers.json", serverString, Encoding.UTF8);
File.WriteAllText(@$"{folder}Servers.json", serverString, Encoding.UTF8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

this.DialogResult = DialogResult.OK;
this.Close();
}
Expand Down