From ff276b4877d0fab565b0fbf63a0a716f63bc1e5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sat, 6 Jan 2024 16:32:15 +0100 Subject: [PATCH] Fetch the player name from itch.io --- OpenRA.Mods.Common/ItchIntegration.cs | 89 +++++++++++++++++++ .../Widgets/Logic/IntroductionPromptLogic.cs | 4 + packaging/.itch.toml | 21 +++++ 3 files changed, 114 insertions(+) create mode 100644 OpenRA.Mods.Common/ItchIntegration.cs diff --git a/OpenRA.Mods.Common/ItchIntegration.cs b/OpenRA.Mods.Common/ItchIntegration.cs new file mode 100644 index 0000000000..c6ffbea379 --- /dev/null +++ b/OpenRA.Mods.Common/ItchIntegration.cs @@ -0,0 +1,89 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Newtonsoft.Json; +using OpenRA.Support; + +namespace OpenRA.Mods.Common +{ + public class ItchIntegration : IGlobalModData + { + class User + { + [JsonProperty("url")] + public string Url { get; set; } + + [JsonProperty("gamer")] + public bool Gamer { get; set; } + + [JsonProperty("id")] + public int Id { get; set; } + + [JsonProperty("press_user")] + public bool PressUser { get; set; } + + [JsonProperty("developer")] + public bool Developer { get; set; } + + [JsonProperty("username")] + public string Username { get; set; } + + [JsonProperty("display_name")] + public string DisplayName { get; set; } + } + + class Root + { + public User User { get; set; } + } + + public void GetPlayerName(Action callback) + { + Task.Run(async () => + { + User user = null; + + var apiKey = Environment.GetEnvironmentVariable("ITCHIO_API_KEY", EnvironmentVariableTarget.Process); + if (!string.IsNullOrEmpty(apiKey)) + { + try + { + var client = HttpClientFactory.Create(); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); + var httpResponseMessage = await client.GetAsync("https://itch.io/api/1/jwt/me"); + httpResponseMessage.EnsureSuccessStatusCode(); + var result = await httpResponseMessage.Content.ReadAsStringAsync(); + user = JsonConvert.DeserializeObject(result).User; + } + catch (Exception e) + { + Log.Write("debug", "Failed to query player name from itch.io API."); + Log.Write("debug", e); + } + } + + var name = ""; + if (user != null) + { + if (string.IsNullOrEmpty(user.DisplayName)) + name = user.Username; + else + name = user.DisplayName; + } + + Game.RunAfterTick(() => callback?.Invoke(name)); + }); + } + } +} diff --git a/OpenRA.Mods.Common/Widgets/Logic/IntroductionPromptLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/IntroductionPromptLogic.cs index c16f8a77c1..5bbc7371cb 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/IntroductionPromptLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/IntroductionPromptLogic.cs @@ -49,6 +49,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic var nameTextfield = widget.Get("PLAYERNAME"); nameTextfield.IsDisabled = () => worldRenderer.World.Type != WorldType.Shellmap; nameTextfield.Text = Settings.SanitizedPlayerName(ps.Name); + + var itchIntegration = modData.Manifest.Get(); + itchIntegration.GetPlayerName(name => nameTextfield.Text = Settings.SanitizedPlayerName(name)); + nameTextfield.OnLoseFocus = () => { if (escPressed) diff --git a/packaging/.itch.toml b/packaging/.itch.toml index 96417d4a5f..ba8f157140 100644 --- a/packaging/.itch.toml +++ b/packaging/.itch.toml @@ -2,13 +2,34 @@ os = "windows" name = "Red Alert" path = "RedAlert.exe" +scope = "profile:me" [[actions]] os = "windows" name = "Dune 2000" path = "Dune2000.exe" +scope = "profile:me" [[actions]] os = "windows" name = "Tiberian Dawn" path = "TiberianDawn.exe" +scope = "profile:me" + +[[actions]] +os = "linux" +name = "Red Alert" +path = "OpenRA-Red-Alert-x86_64.AppImage" +scope = "profile:me" + +[[actions]] +os = "linux" +name = "Dune 2000" +path = "OpenRA-Dune-2000-x86_64.AppImage" +scope = "profile:me" + +[[actions]] +os = "linux" +name = "Tiberian Dawn" +path = "OpenRA-Tiberian-Dawn-x86_64.AppImage" +scope = "profile:me"