1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-23 22:55:37 +01:00

Rename DiscordService to be a bit more future-proof. Fix flickering when showing community link.

This commit is contained in:
Nick Blakely 2020-08-16 03:08:49 -07:00
parent bb376e1f63
commit 6d46fa2875
12 changed files with 79 additions and 65 deletions

View File

@ -0,0 +1,9 @@
namespace Giants.DataContract.V1
{
public class CommunityStatus
{
public string CommunityAppName { get; set; }
public string CommunityAppUri { get; set; }
}
}

View File

@ -1,7 +0,0 @@
namespace Giants.DataContract.V1
{
public class DiscordStatus
{
public string DiscordUri { get; set; }
}
}

View File

@ -34,7 +34,7 @@
this.btnPlay = new Giants.Launcher.ImageButton(); this.btnPlay = new Giants.Launcher.ImageButton();
this.updateProgressBar = new System.Windows.Forms.ProgressBar(); this.updateProgressBar = new System.Windows.Forms.ProgressBar();
this.txtProgress = new System.Windows.Forms.Label(); this.txtProgress = new System.Windows.Forms.Label();
this.DiscordLabel = new System.Windows.Forms.LinkLabel(); this.CommunityLabel = new System.Windows.Forms.LinkLabel();
((System.ComponentModel.ISupportInitialize)(this.btnExit)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.btnExit)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.btnOptions)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.btnOptions)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.btnPlay)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.btnPlay)).BeginInit();
@ -108,19 +108,21 @@
this.txtProgress.Text = "ProgressText"; this.txtProgress.Text = "ProgressText";
this.txtProgress.Visible = false; this.txtProgress.Visible = false;
// //
// DiscordLabel // CommunityLabel
// //
this.DiscordLabel.AutoSize = true; this.CommunityLabel.ActiveLinkColor = System.Drawing.Color.DodgerBlue;
this.DiscordLabel.BackColor = System.Drawing.Color.Transparent; this.CommunityLabel.AutoSize = true;
this.DiscordLabel.LinkColor = System.Drawing.Color.Aquamarine; this.CommunityLabel.BackColor = System.Drawing.Color.Transparent;
this.DiscordLabel.Location = new System.Drawing.Point(12, 9); this.CommunityLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.DiscordLabel.Name = "DiscordLabel"; this.CommunityLabel.LinkColor = System.Drawing.Color.DodgerBlue;
this.DiscordLabel.Size = new System.Drawing.Size(69, 13); this.CommunityLabel.Location = new System.Drawing.Point(12, 9);
this.DiscordLabel.TabIndex = 12; this.CommunityLabel.Name = "CommunityLabel";
this.DiscordLabel.TabStop = true; this.CommunityLabel.Size = new System.Drawing.Size(112, 17);
this.DiscordLabel.Text = "DiscordLabel"; this.CommunityLabel.TabIndex = 12;
this.DiscordLabel.Visible = false; this.CommunityLabel.TabStop = true;
this.DiscordLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.DiscordLabel_LinkClicked); this.CommunityLabel.Text = "CommunityLabel";
this.CommunityLabel.Visible = false;
this.CommunityLabel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.DiscordLabel_LinkClicked);
// //
// LauncherForm // LauncherForm
// //
@ -131,7 +133,7 @@
this.CancelButton = this.btnExit; this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(800, 500); this.ClientSize = new System.Drawing.Size(800, 500);
this.ControlBox = false; this.ControlBox = false;
this.Controls.Add(this.DiscordLabel); this.Controls.Add(this.CommunityLabel);
this.Controls.Add(this.txtProgress); this.Controls.Add(this.txtProgress);
this.Controls.Add(this.updateProgressBar); this.Controls.Add(this.updateProgressBar);
this.Controls.Add(this.btnExit); this.Controls.Add(this.btnExit);
@ -161,7 +163,7 @@
private ImageButton btnExit; private ImageButton btnExit;
private System.Windows.Forms.ProgressBar updateProgressBar; private System.Windows.Forms.ProgressBar updateProgressBar;
private System.Windows.Forms.Label txtProgress; private System.Windows.Forms.Label txtProgress;
private System.Windows.Forms.LinkLabel DiscordLabel; private System.Windows.Forms.LinkLabel CommunityLabel;
} }
} }

View File

@ -24,16 +24,18 @@ namespace Giants.Launcher
private readonly HttpClient httpClient; private readonly HttpClient httpClient;
private readonly VersionClient versionHttpClient; private readonly VersionClient versionHttpClient;
private readonly DiscordClient discordHttpClient; private readonly CommunityClient communityHttpClient;
private string commandLine = String.Empty; private string commandLine = String.Empty;
private string gamePath = null; private string gamePath = null;
private Updater updater; private Updater updater;
private string discordUri; private string communityAppUri;
public LauncherForm() public LauncherForm()
{ {
this.InitializeComponent(); this.InitializeComponent();
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
// Set window title // Set window title
this.Text = GameName; this.Text = GameName;
@ -42,8 +44,8 @@ namespace Giants.Launcher
this.versionHttpClient = new VersionClient(this.httpClient); this.versionHttpClient = new VersionClient(this.httpClient);
this.versionHttpClient.BaseUrl = BaseUrl; this.versionHttpClient.BaseUrl = BaseUrl;
this.discordHttpClient = new DiscordClient(this.httpClient); this.communityHttpClient = new CommunityClient(this.httpClient);
this.discordHttpClient.BaseUrl = BaseUrl; this.communityHttpClient.BaseUrl = BaseUrl;
} }
private void btnExit_Click(object sender, EventArgs e) private void btnExit_Click(object sender, EventArgs e)
@ -138,11 +140,11 @@ namespace Giants.Launcher
{ {
try try
{ {
var status = await this.discordHttpClient.GetDiscordStatusAsync(); var status = await this.communityHttpClient.GetDiscordStatusAsync();
this.discordUri = status.DiscordUri; this.communityAppUri = status.CommunityAppUri;
this.DiscordLabel.Text = Resources.DiscordLabel; this.CommunityLabel.Text = string.Format(Resources.CommunityLabel, status.CommunityAppName);
this.DiscordLabel.Visible = true; this.CommunityLabel.Visible = true;
} }
catch (Exception) catch (Exception)
{ {
@ -273,19 +275,19 @@ namespace Giants.Launcher
private void DiscordLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) private void DiscordLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{ {
if (string.IsNullOrEmpty(this.discordUri)) if (string.IsNullOrEmpty(this.communityAppUri))
{ {
return; return;
} }
var uri = new Uri(this.discordUri); var uri = new Uri(this.communityAppUri);
if (uri.Scheme != "https") if (uri.Scheme != "https")
{ {
// For security, reject any non-HTTPS or local file system URIs // For security, reject any non-HTTPS or local file system URIs
return; return;
} }
Process.Start(this.discordUri); Process.Start(this.communityAppUri);
} }
} }
} }

View File

@ -80,11 +80,11 @@ namespace Giants.Launcher {
} }
/// <summary> /// <summary>
/// Looks up a localized string similar to Join the community on Discord!. /// Looks up a localized string similar to Join the community on {0}!.
/// </summary> /// </summary>
internal static string DiscordLabel { internal static string CommunityLabel {
get { get {
return ResourceManager.GetString("DiscordLabel", resourceCulture); return ResourceManager.GetString("CommunityLabel", resourceCulture);
} }
} }

View File

@ -199,7 +199,7 @@
<data name="OptionSamples" xml:space="preserve"> <data name="OptionSamples" xml:space="preserve">
<value>{0} Samples</value> <value>{0} Samples</value>
</data> </data>
<data name="DiscordLabel" xml:space="preserve"> <data name="CommunityLabel" xml:space="preserve">
<value>Join the community on Discord!</value> <value>Join the community on {0}!</value>
</data> </data>
</root> </root>

View File

@ -17,7 +17,7 @@
services.AddSingleton<IMemoryCache, MemoryCache>(); services.AddSingleton<IMemoryCache, MemoryCache>();
services.AddSingleton<IUpdaterStore, CosmosDbUpdaterStore>(); services.AddSingleton<IUpdaterStore, CosmosDbUpdaterStore>();
services.AddSingleton<IUpdaterService, UpdaterService>(); services.AddSingleton<IUpdaterService, UpdaterService>();
services.AddSingleton<IDiscordService, DiscordService>(); services.AddSingleton<ICommunityService, CommunityService>();
services.AddHostedService<InitializerService>(); services.AddHostedService<InitializerService>();
services.AddHostedService<ServerRegistryCleanupService>(); services.AddHostedService<ServerRegistryCleanupService>();

View File

@ -2,11 +2,11 @@
{ {
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
public class DiscordService : IDiscordService public class CommunityService : ICommunityService
{ {
private readonly IConfiguration configuration; private readonly IConfiguration configuration;
public DiscordService(IConfiguration configuration) public CommunityService(IConfiguration configuration)
{ {
this.configuration = configuration; this.configuration = configuration;
} }

View File

@ -1,6 +1,6 @@
namespace Giants.Services namespace Giants.Services
{ {
public interface IDiscordService public interface ICommunityService
{ {
string GetDiscordUri(); string GetDiscordUri();
} }

View File

@ -342,13 +342,13 @@ namespace Giants.WebApi.Clients
} }
[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.7.0.0 (NJsonSchema v10.1.24.0 (Newtonsoft.Json v11.0.0.0))")] [System.CodeDom.Compiler.GeneratedCode("NSwag", "13.7.0.0 (NJsonSchema v10.1.24.0 (Newtonsoft.Json v11.0.0.0))")]
public partial class DiscordClient public partial class CommunityClient
{ {
private string _baseUrl = "https://localhost:44304"; private string _baseUrl = "https://localhost:44304";
private System.Net.Http.HttpClient _httpClient; private System.Net.Http.HttpClient _httpClient;
private System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings; private System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings;
public DiscordClient(System.Net.Http.HttpClient httpClient) public CommunityClient(System.Net.Http.HttpClient httpClient)
{ {
_httpClient = httpClient; _httpClient = httpClient;
_settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings); _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings);
@ -375,17 +375,17 @@ namespace Giants.WebApi.Clients
partial void ProcessResponse(System.Net.Http.HttpClient client, System.Net.Http.HttpResponseMessage response); partial void ProcessResponse(System.Net.Http.HttpClient client, System.Net.Http.HttpResponseMessage response);
/// <exception cref="ApiException">A server side error occurred.</exception> /// <exception cref="ApiException">A server side error occurred.</exception>
public System.Threading.Tasks.Task<DiscordStatus> GetDiscordStatusAsync() public System.Threading.Tasks.Task<CommunityStatus> GetDiscordStatusAsync()
{ {
return GetDiscordStatusAsync(System.Threading.CancellationToken.None); return GetDiscordStatusAsync(System.Threading.CancellationToken.None);
} }
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="ApiException">A server side error occurred.</exception> /// <exception cref="ApiException">A server side error occurred.</exception>
public async System.Threading.Tasks.Task<DiscordStatus> GetDiscordStatusAsync(System.Threading.CancellationToken cancellationToken) public async System.Threading.Tasks.Task<CommunityStatus> GetDiscordStatusAsync(System.Threading.CancellationToken cancellationToken)
{ {
var urlBuilder_ = new System.Text.StringBuilder(); var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/Discord"); urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/Community");
var client_ = _httpClient; var client_ = _httpClient;
try try
@ -415,7 +415,7 @@ namespace Giants.WebApi.Clients
var status_ = (int)response_.StatusCode; var status_ = (int)response_.StatusCode;
if (status_ == 200) if (status_ == 200)
{ {
var objectResponse_ = await ReadObjectResponseAsync<DiscordStatus>(response_, headers_).ConfigureAwait(false); var objectResponse_ = await ReadObjectResponseAsync<CommunityStatus>(response_, headers_).ConfigureAwait(false);
if (objectResponse_.Object == null) if (objectResponse_.Object == null)
{ {
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
@ -857,10 +857,13 @@ namespace Giants.WebApi.Clients
} }
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v11.0.0.0)")] [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v11.0.0.0)")]
public partial class DiscordStatus public partial class CommunityStatus
{ {
[Newtonsoft.Json.JsonProperty("discordUri", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] [Newtonsoft.Json.JsonProperty("communityAppName", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string DiscordUri { get; set; } public string CommunityAppName { get; set; }
[Newtonsoft.Json.JsonProperty("communityAppUri", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string CommunityAppUri { get; set; }
} }

View File

@ -68,19 +68,19 @@
} }
} }
}, },
"/api/Discord": { "/api/Community": {
"get": { "get": {
"tags": [ "tags": [
"Discord" "Community"
], ],
"operationId": "Discord_GetDiscordStatus", "operationId": "Community_GetDiscordStatus",
"responses": { "responses": {
"200": { "200": {
"description": "", "description": "",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"$ref": "#/components/schemas/DiscordStatus" "$ref": "#/components/schemas/CommunityStatus"
} }
} }
} }
@ -281,11 +281,15 @@
} }
} }
}, },
"DiscordStatus": { "CommunityStatus": {
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"discordUri": { "communityAppName": {
"type": "string",
"nullable": true
},
"communityAppUri": {
"type": "string", "type": "string",
"nullable": true "nullable": true
} }

View File

@ -6,22 +6,23 @@
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class DiscordController : ControllerBase public class CommunityController : ControllerBase
{ {
private readonly IDiscordService discordService; private readonly ICommunityService discordService;
public DiscordController( public CommunityController(
IDiscordService discordService) ICommunityService communityService)
{ {
this.discordService = discordService; this.discordService = communityService;
} }
[HttpGet] [HttpGet]
public DiscordStatus GetDiscordStatus() public CommunityStatus GetDiscordStatus()
{ {
return new DiscordStatus return new CommunityStatus
{ {
DiscordUri = this.discordService.GetDiscordUri() CommunityAppName = "Discord",
CommunityAppUri = this.discordService.GetDiscordUri()
}; };
} }
} }