Skip to content

Commit 85f2f38

Browse files
committed
Added Class that loads the current configuration for the Wildlife apps from Github.
1 parent c902119 commit 85f2f38

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Text.RegularExpressions;
2+
using GamesOnWhales;
3+
using Tomlyn;
4+
using Tomlyn.Model;
5+
using WolfLeash.Extensions;
6+
7+
namespace WolfLeash.Components.Classes;
8+
9+
public partial class DefaultAppLoader
10+
{
11+
private const string ConfigBaseAddress =
12+
"https://raw.githubusercontent.com/games-on-whales/gow/refs/heads/master/apps/{0}/assets/wolf.config.toml";
13+
14+
private static readonly string[] DefaultApps =
15+
["es-de", "firefox", "heroic-games-launcher", "kodi", "lutris", "pegasus", "prismlauncher", "retroarch", "steam", "xfce"];
16+
17+
private static string? _defaultRenderDevice;
18+
private static string DefaultRenderDevice => _defaultRenderDevice ??= GetDefaultRenderDevice();
19+
20+
private readonly HttpClient _httpClient;
21+
22+
public DefaultAppLoader()
23+
{
24+
_httpClient = new HttpClient();
25+
}
26+
27+
public Task<List<GamesOnWhales.App>> GetApps() => GetApps(CancellationToken.None);
28+
public async Task<List<GamesOnWhales.App>> GetApps(CancellationToken cancellationToken)
29+
{
30+
var apps = new List<GamesOnWhales.App>();
31+
foreach (var appName in DefaultApps)
32+
{
33+
apps.Add(await GetApp(appName, cancellationToken));
34+
}
35+
return apps;
36+
}
37+
38+
public Task<string> GetAppConfigAsync(string appName) => GetAppConfigAsync(appName, CancellationToken.None);
39+
public async Task<string> GetAppConfigAsync(string appName, CancellationToken cancellationToken)
40+
{
41+
var url = string.Format(ConfigBaseAddress, appName);
42+
var response = await _httpClient.GetAsync(url, cancellationToken);
43+
var body = await response.Content.ReadAsStringAsync(cancellationToken);
44+
return body;
45+
}
46+
47+
public Task<GamesOnWhales.App> GetApp(string appName) => GetApp(appName, CancellationToken.None);
48+
public async Task<GamesOnWhales.App> GetApp(string appName, CancellationToken cancellationToken)
49+
{
50+
var cfg = await GetAppConfigAsync(appName, cancellationToken);
51+
var model = ((TomlTableArray)Toml.ToModel(cfg)["apps"])[0];
52+
var runner = (TomlTable)model["runner"];
53+
var app = new GamesOnWhales.App()
54+
{
55+
Id = Guid.NewGuid().ToBase64(),
56+
Title = (string)model["title"],
57+
Icon_png_path = (string)model["icon_png_path"],
58+
Start_audio_server = true,
59+
Start_virtual_compositor = true,
60+
Support_hdr = false,
61+
H264_gst_pipeline = "",
62+
Hevc_gst_pipeline = "",
63+
Av1_gst_pipeline = "",
64+
Opus_gst_pipeline = "",
65+
Render_node = DefaultRenderDevice,
66+
67+
Runner = new Wolf__config__AppDocker__tagged()
68+
{
69+
Name = (string)runner["name"],
70+
Image = (string)runner["image"],
71+
Base_create_json = (string)runner["base_create_json"],
72+
Devices = ((TomlArray)runner["devices"]).Select(t => (string)t!).ToList(),
73+
Env = ((TomlArray)runner["env"]).Select(t => (string)t!).ToList(),
74+
Mounts = ((TomlArray)runner["mounts"]).Select(t => (string)t!).ToList(),
75+
Ports = ((TomlArray)runner["ports"]).Select(t => (string)t!).ToList(),
76+
Type = "docker"
77+
}
78+
};
79+
return app;
80+
}
81+
82+
private static string GetDefaultRenderDevice()
83+
{
84+
if (!Path.Exists("/dev/dri/"))
85+
{
86+
return "/dev/dri/renderD128";
87+
}
88+
89+
var renderDevices = Directory.GetFiles("/dev/dri/")
90+
.Where(f => RendererRegex().IsMatch(f))
91+
.ToList();
92+
93+
return renderDevices.Count > 0 ? renderDevices[0] : "/dev/dri/renderD128";
94+
}
95+
96+
[GeneratedRegex(".*?/renderD[0-9]+")]
97+
private static partial Regex RendererRegex();
98+
}

WolfLeash/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
builder.Services.AddLogging(configure => configure.AddConsole());
2020
builder.Services.AddTransient<ColorGenerator>();
2121
builder.Services.AddWolfApi<Api>();
22+
builder.Services.AddSingleton<DefaultAppLoader>();
2223

2324
var migrationPatcher = new DatabasePreMigrationPatches();
2425
migrationPatcher.Execute();

0 commit comments

Comments
 (0)