Skip to content

Commit

Permalink
feat: add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DerStimmler committed Jan 29, 2025
1 parent d6061fb commit bf43f78
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 36 deletions.
4 changes: 3 additions & 1 deletion CrossLaunch/CrossLaunch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference>
<PackageReference Include="H.InputSimulator" Version="1.5.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0"/>
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Silk.NET.Input" Version="2.22.0"/>
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>
Expand Down
30 changes: 18 additions & 12 deletions CrossLaunch/Features/Epic/EgDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net.Http.Json;
using System.Threading.Tasks;
using CrossLaunch.Utils;
using Serilog;

namespace CrossLaunch.Features.Epic;

Expand All @@ -19,18 +20,23 @@ public EgDataService()

public async Task<EgDataItem?> FindGame(string @namespace, string gameId)
{
var response = await _http.GetAsync($"/sandboxes/{@namespace}/items");

if (!response.IsSuccessStatusCode)
try
{
var response = await _http.GetAsync($"/sandboxes/{@namespace}/items");

if (!response.IsSuccessStatusCode)
return null;

var items = await response.Content.ReadFromJsonAsync<List<EgDataItem>>(
CustomJsonSerializerContext.Default.ListEgDataItem
);

return items?.FirstOrDefault(item => item.Id == gameId);
}
catch (Exception e)
{
Log.Error(e, "Couldn't load epic game {Namespace}, {GameId} from egdata", @namespace, gameId);
return null;

var items = await response.Content.ReadFromJsonAsync<List<EgDataItem>>(
CustomJsonSerializerContext.Default.ListEgDataItem
);

if (items is null)
return null;

return items.FirstOrDefault(item => item.Id == gameId);
}
}
}
8 changes: 4 additions & 4 deletions CrossLaunch/Features/Epic/EpicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using CrossLaunch.Utils;
using Serilog;

namespace CrossLaunch.Features.Epic;

Expand All @@ -29,10 +30,7 @@ public async Task<List<EpicGame>> GetGames()
{
var gameInfo = await _egDataService.FindGame(manifest.CatalogNamespace, manifest.CatalogItemId);

if (gameInfo is null)
continue;

var imageUri = gameInfo.KeyImages.FirstOrDefault()?.Url ?? "https://placehold.co/600x400";
var imageUri = gameInfo?.KeyImages.FirstOrDefault()?.Url ?? "https://placehold.co/600x400";

var game = new EpicGame(manifest, new Uri(imageUri));

Expand All @@ -47,6 +45,8 @@ private static async Task<List<EpicManifest>> GetManifests(CancellationToken ct)
var programDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var epicManifestsFolder = Path.Combine(programDataFolder, "Epic", "EpicGamesLauncher", "Data", "Manifests");

Log.Information("Getting Epic Manifests from {ManifestPath}", epicManifestsFolder);

var manifestFiles = Directory.GetFiles(epicManifestsFolder, "*.item");

var manifests = new List<EpicManifest>();
Expand Down
67 changes: 48 additions & 19 deletions CrossLaunch/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using CrossLaunch.Features.Gamepad;
using Serilog;
using Silk.NET.Input;
using Silk.NET.Windowing;

Expand All @@ -18,35 +20,62 @@ internal class Program
public static void Main(string[] args)
{
var cts = new CancellationTokenSource();
var options = WindowOptions.Default;
options.IsVisible = false; // We don't need this window visible
var silkWindow = Window.Create(options);

silkWindow.Load += () =>
ConfigureLogging();

try
{
GamepadHandler.HandleGamepadInput(silkWindow.CreateInput(), cts.Token);
};
var options = WindowOptions.Default;
options.IsVisible = false; // We don't need this window visible
var silkWindow = Window.Create(options);

Task.Run(() => silkWindow.Run(), cts.Token);
silkWindow.Load += () =>
{
GamepadHandler.HandleGamepadInput(silkWindow.CreateInput(), cts.Token);
};

BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(
args,
lifetime =>
{
lifetime.ShutdownRequested += OnShutdownRequested;
return;
Task.Run(() => silkWindow.Run(), cts.Token);

void OnShutdownRequested(object? sender, ShutdownRequestedEventArgs e)
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(
args,
lifetime =>
{
cts.Cancel();
lifetime.ShutdownRequested += OnShutdownRequested;
return;

void OnShutdownRequested(object? sender, ShutdownRequestedEventArgs e)
{
cts.Cancel();
}
}
}
);
);
}
catch (Exception e)
{
Log.Fatal(e, "Something very bad happened");
}
finally
{
Log.CloseAndFlush();
}
}

private static void ConfigureLogging()
{
var logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"CrossLaunch",
"log.txt"
);

using var log = new LoggerConfiguration().WriteTo.Console().WriteTo.File(logPath).CreateLogger();

Log.Logger = log;
}

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
private static AppBuilder BuildAvaloniaApp()
{
return AppBuilder.Configure<App>().UsePlatformDetect().WithInterFont().LogToTrace();
}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Prefer a standalone setup? CrossLaunch works independently of Steam too, offerin

- Epic Games Launcher

## Troubleshooting

Log files are in `%LOCALAPPDATA%/CrossLaunch`.

## Development

1. Restore dotnet dependencies `dotnet restore`
Expand Down

0 comments on commit bf43f78

Please sign in to comment.