Skip to content

Commit

Permalink
mod : Launcher is now also used for game servers
Browse files Browse the repository at this point in the history
  • Loading branch information
namidaka authored and namidaka committed Oct 10, 2023
1 parent 581c3df commit 7b356e0
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions src/Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,32 @@
using Gameloop.Vdf;
using Gameloop.Vdf.Linq;
using Microsoft.Win32;
using static System.Net.WebRequestMethods;
using File = System.IO.File;

Console.WriteLine("Make sure Steam/Epic Games/Xbox is running and that Bannerlord is up-to-date");
(bool isServer,bool isBeta, string path) = IsServerLauncher(args);
if (isServer)
{
if (path == null)
{
Console.WriteLine("invalid path");
return;
}

try
{
await UpdateCrpgAsync(path, isBeta, isServer);
return;
}
catch (Exception e)
{
Console.WriteLine("Could not update cRPG Server.");
Console.WriteLine(e);
Console.Read();
return;
}
}

var bannerlordInstallation = ResolveBannerlordInstallation();
if (bannerlordInstallation == null)
Expand Down Expand Up @@ -38,6 +62,7 @@
Arguments = bannerlordInstallation.ProgramArguments ?? string.Empty,
UseShellExecute = true,
});
Console.WriteLine("Make sure Steam/Epic Games/Xbox is running and that Bannerlord is up-to-date");

static GameInstallationInfo? ResolveBannerlordInstallation()
{
Expand Down Expand Up @@ -162,17 +187,30 @@
return null;
}

static async Task UpdateCrpgAsync(string bannerlordPath)
static async Task UpdateCrpgAsync(string bannerlordPath, bool isBeta = false,bool isServer = false)
{
string crpgPath = Path.Combine(bannerlordPath, "Modules/cRPG");
string tagPath = Path.Combine(crpgPath, "Tag.txt");
string? tag = File.Exists(tagPath) ? File.ReadAllText(tagPath) : null;
string websiteUrl = "https://c-rpg.eu/";
string fileName = "crpg.zip";
if (isBeta)
{
websiteUrl = "https://namidaka.fr/";
crpgPath = Path.Combine(bannerlordPath, "Modules/cRPG_Beta");
}

if (isServer)
{
fileName = "cRPGServer.zip";
}

string crpgUrl = websiteUrl + fileName;
using HttpClient httpClient = new(new SocketsHttpHandler
{
AllowAutoRedirect = true,
});
HttpRequestMessage req = new(HttpMethod.Get, "https://c-rpg.eu/cRPG.zip");
HttpRequestMessage req = new(HttpMethod.Get, crpgUrl);
if (tag != null)
{
req.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(tag));
Expand Down Expand Up @@ -238,4 +276,39 @@ static async Task CopyToWithProgressBarAsync(
Console.WriteLine();
}

static (bool isServer,bool isBeta, string path) IsServerLauncher(string[] args)
{
bool isServer = false;
string path = string.Empty;
bool isBeta = false;
for (int i = 0; i < args.Length; i++)
{
switch (args[i].ToLower()) // Using ToLower for case-insensitive comparison
{
case "-server":
isServer = true;
break;
case "-path":
if (i + 1 < args.Length) // Ensure there's another argument after -path
{
path = args[++i].Trim('"');
}
else
{
Console.WriteLine("Error: Expected a path after -path argument.");
}

break;
case "-beta":
isBeta = true;
break;
default:
Console.WriteLine($"Warning: Unknown argument {args[i]}");
break;
}
}

return (isServer, isBeta, path);
}

record GameInstallationInfo(string InstallationPath, string Program, string? ProgramArguments, string? ProgramWorkingDirectory);

0 comments on commit 7b356e0

Please sign in to comment.