Skip to content

Commit

Permalink
Add config save data, so you don't have to keep searching for folders…
Browse files Browse the repository at this point in the history
… game
  • Loading branch information
akbaryahya committed Sep 25, 2022
1 parent 472f2b8 commit f1d8c41
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 169 deletions.
8 changes: 4 additions & 4 deletions Extra/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace YuukiPS_Launcher.Extra
{
public class Discord
{
public DiscordRpcClient client;
public DiscordRpcClient? client;

public void Ready(string appid = "1023479009335582830")
{
Expand All @@ -19,12 +19,12 @@ public void Ready(string appid = "1023479009335582830")
//Subscribe to events
client.OnReady += (sender, e) =>
{
Console.WriteLine("Received Ready from user {0}", e.User.Username);
Console.WriteLine("Discord: Received Ready from user {0}", e.User.Username);
UpdateStatus("Getting ready", "Wait");
};
client.OnPresenceUpdate += (sender, e) =>
client.OnError += (sender, e) =>
{
Console.WriteLine("Received Update! {0}", e.Presence);
Console.WriteLine("Discord: Error Update {0}", e.Message);
};

//Connect to the RPC
Expand Down
12 changes: 12 additions & 0 deletions Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 86 additions & 14 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Win32;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Management;
using System.Net.NetworkInformation;
Expand All @@ -9,6 +10,7 @@ namespace YuukiPS_Launcher
{
public partial class Main : Form
{
Config configdata = new Config();

// Main Function
private ProxyController? proxy;
Expand All @@ -24,17 +26,91 @@ public partial class Main : Form
private static string DataConfig = Path.Combine(CurrentlyPath, "data");
private static string Modfolder = Path.Combine(CurrentlyPath, "mod");

// Config default
string VersionGame = "";
string WatchFile = "";
bool IsGameRun = false;
bool DoneCheck = true;
int GameChannel = 0;
int GameMetode = 1;
public string ConfigPath = Path.Combine(DataConfig, "config.json");

// Stats default
public string WatchFile = "";
public bool IsGameRun = false;
public bool DoneCheck = true;
public string VersionGame = "";
public int GameChannel = 0;
public int GameMetode = 1;

// Extra
Extra.Discord discord = new Extra.Discord();

public void LoadConfig()
{
// Create missing folder
Directory.CreateDirectory(DataConfig);
Directory.CreateDirectory(Modfolder);

if (File.Exists(ConfigPath))
{
string data = File.ReadAllText(ConfigPath);
try
{
configdata = JsonConvert.DeserializeObject<Config>(data);
if (configdata != null)
{
// load config
Set_LA_GameFolder.Text = configdata.Game_Path;
GetHost.Text = configdata.Hostdefault;
GetPort.Text = configdata.ProxyPort.ToString();

checkModeOnline.Checked = configdata.MetodeOnline;
CheckProxyUseHTTPS.Checked = configdata.HostHTTPS;
Extra_AkebiGC.Checked = configdata.extra.Akebi;

Console.WriteLine("load config...");
}
else
{
Console.WriteLine("No config load...");
}
}
catch (Exception ex)
{
Console.WriteLine("Error load config: " + ex.Message);
}
}
else
{
Console.WriteLine("No Config file found...");
}

// Check Game Version
CheckVersionGame();
}
public void SaveConfig()
{
try
{
// save config
configdata.Game_Path = Set_LA_GameFolder.Text;
configdata.Hostdefault = GetHost.Text;

int myInt;
bool isValid = int.TryParse(GetPort.Text, out myInt);
if (isValid)
{
configdata.ProxyPort = myInt;
}

configdata.MetodeOnline = checkModeOnline.Checked;
configdata.HostHTTPS = CheckProxyUseHTTPS.Checked;
configdata.extra.Akebi = Extra_AkebiGC.Checked;

File.WriteAllText(ConfigPath, JsonConvert.SerializeObject(configdata));

Console.WriteLine("Done save config...");
}
catch (Exception ex)
{
Console.WriteLine("Error save config: " + ex.Message);
}
}

public Main()
{
InitializeComponent();
Expand All @@ -45,19 +121,15 @@ private void Main_Load(object sender, EventArgs e)
{
Console.WriteLine("Loading....");

// Create missing folder
Directory.CreateDirectory(DataConfig);
Directory.CreateDirectory(Modfolder);
// Load config and check version game
LoadConfig();

// Before starting make sure proxy is turned off
CheckProxy(true);

// Check Update
CheckUpdate();

// Check Game Version
CheckVersionGame();

// Server List
GetServerList();
UpdateServerListTimer();
Expand Down Expand Up @@ -1444,7 +1516,7 @@ private void tabPage5_Click(object sender, EventArgs e)

private void Set_LA_Save_Click(object sender, EventArgs e)
{

SaveConfig();
}

[Obsolete]
Expand Down
149 changes: 1 addition & 148 deletions Tool.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.DotNet.PlatformAbstractions;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace YuukiPS_Launcher
{
Expand All @@ -24,71 +22,6 @@ public static string CalcMemoryMensurableUnit(double bytes)
return result;
}

public const string UNIX_PID_REGX = @"\w+\s+(\d+).*";
public const string WIND_PID_REGX = @".*\s+(\d+)";
public static void findAndKillProcessRuningOn(string port)
{
List<string> pidList = new List<string>();
List<string> list = new List<string>();
switch (getOSName())
{
case Platform.Linux:
list = findUnixProcess();
list = filterProcessListBy(processList: list, filter: ":" + port);

foreach (string pidString in list)
{
string pid = getPidFrom(pidString: pidString, pattern: UNIX_PID_REGX);

if (!String.IsNullOrEmpty(pid))
{
pidList.Add(pid);
}
}
break;

case Platform.Windows:
list = findWindowsProcess();
list = filterProcessListBy(processList: list, filter: ":" + port);

foreach (string pidString in list)
{
string pid = getPidFrom(pidString: pidString, pattern: WIND_PID_REGX);

if (!String.IsNullOrEmpty(pid))
{
pidList.Add(pid);
}
}
break;
default:
Console.WriteLine("No match found");
break;
}

foreach (string pid in pidList)
{
killProcesBy(pidString: pid);
}
}

public static Platform getOSName()
{
string os = System.Environment.OSVersion.VersionString;
Console.WriteLine("OS = {0}", os);

if (os != null && os.ToLower().Contains("unix"))
{
Console.WriteLine("UNXI machine");
return Platform.Linux;
}
else
{
Console.WriteLine("Windows machine");
return Platform.Windows;
}
}

public static void killProcesBy(string pidString)
{
int pid = -1;
Expand All @@ -105,86 +38,6 @@ public static void killProcesBy(string pidString)

}

public static List<String> findUnixProcess()
{
ProcessStartInfo processStart = new ProcessStartInfo();
processStart.FileName = "bash";
processStart.Arguments = "-c lsof -i";

processStart.RedirectStandardOutput = true;
processStart.UseShellExecute = false;
processStart.CreateNoWindow = true;

Process process = new Process();
process.StartInfo = processStart;
process.Start();

String outstr = process.StandardOutput.ReadToEnd();

return splitByLineBreak(outstr);
}

public static List<String> findWindowsProcess()
{
ProcessStartInfo processStart = new ProcessStartInfo();
processStart.FileName = "netstat.exe";
processStart.Arguments = "-aon";

processStart.RedirectStandardOutput = true;
processStart.UseShellExecute = false;
processStart.CreateNoWindow = true;

Process process = new Process();
process.StartInfo = processStart;
process.Start();

String outstr = process.StandardOutput.ReadToEnd();

return splitByLineBreak(outstr);
}

public static List<string> splitByLineBreak(string processLines)
{
List<string> processList = new List<string>();

if (processLines != null)
{
string[] list = processLines.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
processList.AddRange(collection: list);
}

return processList;
}

public static List<String> filterProcessListBy(List<String> processList,
String filter)
{

if (processList == null)
{
return new List<string>();
}

if (filter == null)
{
return processList;
}

return processList.FindAll(i => i != null && i.ToLower().Contains(filter.ToLower()));
}

public static String getPidFrom(String pidString, String pattern)
{
MatchCollection matches = Regex.Matches(pidString, pattern);

if (matches != null && matches.Count > 0)
{
return matches[0].Groups[1].Value;
}

return "";
}

public static void Logger(string message, ConsoleColor c = ConsoleColor.White)
{
Console.ForegroundColor = c;
Expand Down
5 changes: 2 additions & 3 deletions YuukiPS Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

<ApplicationManifest>Properties\app.manifest</ApplicationManifest>

<AssemblyVersion>2022.9.25.0720</AssemblyVersion>
<FileVersion>2022.9.25.0720</FileVersion>
<AssemblyVersion>2022.9.25.1542</AssemblyVersion>
<FileVersion>2022.9.25.1542</FileVersion>

<EnableWindowsTargeting>true</EnableWindowsTargeting>
<AssemblyName>YuukiPS</AssemblyName>
Expand Down Expand Up @@ -47,7 +47,6 @@
<ItemGroup>
<PackageReference Include="DiscordRichPresence" Version="1.0.175" />
<PackageReference Include="Downloader" Version="2.4.1" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="108.0.2" />
<PackageReference Include="System.Management" Version="6.0.0" />
Expand Down
Loading

0 comments on commit f1d8c41

Please sign in to comment.