Skip to content

Commit

Permalink
Log and try-catch extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
cybershard committed Sep 17, 2021
1 parent afcba22 commit 778bde3
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 24 deletions.
6 changes: 3 additions & 3 deletions ClientLauncher/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public static class Context

public static string BepInExGithubOrg => "NuclearPowered";
public static string BepInExGithubRepo => "BepInEx";

#if PRIVATE
public static string BucketUrl => "https://polusgg-mod-client-private.nyc3.digitaloceanspaces.com";
#elif PUBLIC
public static string BucketUrl => "https://launcher.asset.polus.gg";
#endif

//TODO: hardcode value later

public static string CosmeticsUrl => "http://cosmetics.service.polus.gg:2219";
public static string AccountServerUrl => "https://account.polus.gg";

Expand All @@ -49,4 +49,4 @@ public static class Context

public static Configuration Configuration { get; set; } = new();
}
}
}
2 changes: 1 addition & 1 deletion ClientLauncher/Extensions/GameInstallExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static async ValueTask CopyTo(this GameInstall install, string newInstall
if (Directory.Exists(filePath) && Path.GetFileName(filePath) != "Among Us_Data")
continue;

var rebasedPath = filePath.Replace(basePath, "").TrimStart('/');
var rebasedPath = filePath.Replace(basePath, "").TrimStart('/').TrimStart('\\');
rebasedPath = Path.Join(newInstallPath, rebasedPath);

var baseDir = Path.GetDirectoryName(rebasedPath);
Expand Down
4 changes: 2 additions & 2 deletions ClientLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public static void Main(string[] args)
Console.Write($"Error initializing Steam API: {e.Message}\n{e.StackTrace}");
}

LoggingService.ClearLog();

CreateConfiguration();
LoggingService.CreateLogFile();

LoggingService.LogError(new GameInstall().ModPackageManifestJson);
LoggingService.Log(new GameInstall().ModPackageManifestJson);

BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);

Expand Down
2 changes: 1 addition & 1 deletion ClientLauncher/Services/Api/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<MemoryStream> DownloadFileAsync(string downloadUrl, string? na

public async Task<ModPackageManifest> GetModPackageManifestAsync(string version)
{
LoggingService.LogError($"{Context.BucketUrl}/{version}/{ModPackageManifest.ManifestFileName}");
LoggingService.Log($"{Context.BucketUrl}/{version}/{ModPackageManifest.ManifestFileName}");
var manifest = await _client.GetFromJsonAsync<ModPackageManifest>($"{Context.BucketUrl}/{version}/{ModPackageManifest.ManifestFileName}");
if (manifest is null)
throw new InvalidOperationException($"Download manifest.json was not found for version {version}");
Expand Down
18 changes: 17 additions & 1 deletion ClientLauncher/Services/DownloadService.BepInEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,23 @@ public static async ValueTask DownloadBepInExAsync(GameInstall install)
Directory.CreateDirectory(install.BepInExFolder);

using var zip = new ZipArchive(zipStream);
zip.ExtractToDirectory(install.Location, true);
foreach (ZipArchiveEntry entry in zip.Entries)
{
var zipEntryPath = Path.GetFullPath(Path.Combine(install.Location, entry.FullName));
try
{

var directoryName = Path.GetDirectoryName(zipEntryPath);
if (directoryName is not null)
Directory.CreateDirectory(directoryName);
entry.ExtractToFile(zipEntryPath, true);
}
catch (Exception e)
{
LoggingService.Log($"Couldn't extract file {entry.FullName} to path {zipEntryPath}: {e.Message}\n{e.StackTrace}");
}
}


await File.WriteAllTextAsync(install.BepInExVersionFile, latest.Id.ToString());
}
Expand Down
23 changes: 12 additions & 11 deletions ClientLauncher/Services/LoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@

namespace ClientLauncher.Services
{
public static class LoggingService {
public static string LogFile => Path.Combine(Context.DataPath, "launcher.log");
public static void ClearLog() {
if (File.Exists(LogFile))
{
File.Delete(LogFile);
}
}
public static void LogError(string contents)
public static class LoggingService
{
private static string LogFile => Path.Combine(Context.DataPath, "launcher.log");

public static void Log(string contents)
{
try
{
Console.WriteLine(contents);
File.AppendAllText(LogFile, $"{contents}\n\n");
}
catch (Exception) { /* ignored */ }
}
}

public static void CreateLogFile()
{
File.WriteAllText(LogFile, $"--- Log file created at {DateTime.Now:G} ---\n");
}
}
}
}
10 changes: 5 additions & 5 deletions ClientLauncher/ViewModels/LandingPage/LandingPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private async Task InstallGameAsync()
catch (Exception e)
{
IsInstallProgressing = false;
LoggingService.LogError($"Couldn't setup modded install directory: {e.Message}\n{e.StackTrace}");
LoggingService.Log($"Couldn't setup modded install directory: {e.Message}\n{e.StackTrace}");
await WarnDialog.Handle("Couldn't setup modded install directory.");
return;
}
Expand All @@ -187,7 +187,7 @@ private async Task InstallGameAsync()
catch (Exception e)
{
IsInstallProgressing = false;
LoggingService.LogError($"Couldn't install BepInEx: {e.Message}\n{e.StackTrace}");
LoggingService.Log($"Couldn't install BepInEx: {e.Message}\n{e.StackTrace}");
await WarnDialog.Handle("Couldn't install BepInEx.");
return;
}
Expand All @@ -199,7 +199,7 @@ private async Task InstallGameAsync()
catch (Exception e)
{
IsInstallProgressing = false;
LoggingService.LogError($"Exception installing Polus.gg mod: {e.Message}\n{e.StackTrace}");
LoggingService.Log($"Exception installing Polus.gg mod: {e.Message}\n{e.StackTrace}");
await WarnDialog.Handle($"Couldn't install Polusgg mod.");
return;
}
Expand All @@ -211,14 +211,14 @@ private async Task InstallGameAsync()
}
catch (Exception e)
{
LoggingService.LogError($"Exception while launching game: {e.Message}\n{e.StackTrace}");
LoggingService.Log($"Exception while launching game: {e.Message}\n{e.StackTrace}");
await WarnDialog.Handle($"Caught exception when launching game.");
}

}
catch (Exception e)
{
LoggingService.LogError($"Exception while launching game: {e.Message}\n{e.StackTrace}");
LoggingService.Log($"Exception while launching game: {e.Message}\n{e.StackTrace}");
await WarnDialog.Handle($"Unknown error when launching game.");
}
finally
Expand Down

0 comments on commit 778bde3

Please sign in to comment.