Skip to content

Commit

Permalink
Code improvements for splash screen and version handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpmartell committed Aug 8, 2024
1 parent 39e1c70 commit f3b0582
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 56 deletions.
32 changes: 32 additions & 0 deletions LittleWarGameClient/FontManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using LittleWarGameClient.Properties;
using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static nud2dlib.Windows.Forms.Win32;

namespace LittleWarGameClient
{
internal class FontManager
{
private static FontFamily? gameFont;
internal static FontFamily lwgFont
{
get
{
if (gameFont == null)
{
string font_filename = "lwgFont.ttf";
if (!File.Exists(font_filename))
File.WriteAllBytes(font_filename, Resources.LcdSolidFont);
PrivateFontCollection pfc = new();
pfc.AddFontFile(font_filename);
gameFont = pfc.Families[0];
}
return gameFont;
}
}
}
}
3 changes: 1 addition & 2 deletions LittleWarGameClient/GameForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ internal GameForm()
{
PreInitWeb();
InitializeComponent();
if (Program.LWG_FONT != null)
loadingText.Font = new Font(Program.LWG_FONT, 48F, FontStyle.Regular, GraphicsUnit.Point);
loadingText.Font = new Font(FontManager.lwgFont, 48F, FontStyle.Regular, GraphicsUnit.Point);
settings = new Settings();
audioMngr = new AudioManager(Text);
kbHandler = new KeyboardHandler(settings);
Expand Down
2 changes: 1 addition & 1 deletion LittleWarGameClient/KeyboardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public bool OnPreKeyEvent(IWebBrowser webView, IBrowser browser, KeyType type, i

public bool OnKeyEvent(IWebBrowser webView, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
{
return true;
return false;
}
}
}
3 changes: 1 addition & 2 deletions LittleWarGameClient/OverlayForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ internal OverlayForm()
overlayMessages = new BDictionary<string, Notification>();
IsActivated = false;
InitializeComponent();
if (Program.LWG_FONT != null)
Font = new Font(Program.LWG_FONT, 21.75F, FontStyle.Regular, GraphicsUnit.Point);
Font = new Font(FontManager.lwgFont, 21.75F, FontStyle.Regular, GraphicsUnit.Point);

try
{
Expand Down
15 changes: 4 additions & 11 deletions LittleWarGameClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,25 @@ internal static class Program
private static CallBackPtr callBackPtr = Callback;
private static List<WinStruct> _WinStructList = new List<WinStruct>();

internal static FontFamily? LWG_FONT;
internal static bool IsDoubleInstance = false;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
(new Thread(() =>
{
string font_filename = "lwgFont.ttf";
if (!File.Exists(font_filename))
File.WriteAllBytes(font_filename, Resources.LcdSolidFont);
PrivateFontCollection pfc = new();
pfc.AddFontFile(font_filename);
LWG_FONT = pfc.Families[0];
Application.Run(SplashScreen.Instance);
SplashScreen.Instance.ShowDialog();
})).Start();
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "Global\\LittleWarGameClient", out createdNew))
{
if (createdNew)
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(OverlayForm.Instance);
}
else
Expand Down
2 changes: 2 additions & 0 deletions LittleWarGameClient/SplashScreen.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions LittleWarGameClient/SplashScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ internal static SplashScreen Instance
public SplashScreen()
{
InitializeComponent();
if (Program.LWG_FONT != null)
{
splashText.Font = new Font(Program.LWG_FONT, 24F, FontStyle.Regular, GraphicsUnit.Point);
splashText.ForeColor = Color.White;
}
splashText.Font = new Font(FontManager.lwgFont, 24F, FontStyle.Regular, GraphicsUnit.Point);
}

internal void InvokeUI(Action a)
Expand All @@ -50,7 +46,7 @@ private void SplashScreen_Load(object sender, EventArgs e)
{
if (Program.IsDoubleInstance)
{
Application.Exit();
Close();
}
}
}
Expand Down
77 changes: 77 additions & 0 deletions LittleWarGameClient/SplashScreen.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,81 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAEAICAAAAEAIACoEAAAFgAAACgAAAAgAAAAQAAAAAEAIAAAAAAAABAAAAAAAAAAAAAAAAAAAAAA
AAD////////////////9/f3//////9XS0f85LSb/QDQt/z8zLP8/Myz/QDQt/zksJf/Sz83//////2xj
Xv81KCH/QTUu/z4yK/9CNjD/MCMb/5mTj////////Pv7////////////////////////////////////
//////////////////////////////39/f//////1dLR/zotJv9ANC3/PjIs/z4yLf9ANC3/OSwl/9LP
zf//////bGNe/zUoIf9DNy//QTUt/0Q3MP8wIxv/mZOP///////8+/v/////////////////////////
/////////////////////////////////////////f39///////U0tH/Oy4l/zkvL/8OD0D/ERI//z4z
Lv85LSX/0s/N//////9uZF//LSMc/4lrW//Ak33/cFlN/ygdFv+blJD///////z7+///////////////
///////////////////////////////////////////////////9/f3//////9TS0f88LyT/OS8v/wYJ
Q/8KDEL/PzMt/zstJP/Sz83//////25kX/8sIhv/lHNi/9Sjif94XlL/JhwV/5uUkP///////Pv7////
//////////////////////////////////////////////////////////////z8/P//////ysrU/woL
OP8TFUb/GCJq/xcgZv8SE0H/DxA+/09HTP9kWlP/ST03/zYsJv+Qb1//zp6G/3ZdUP8nHBX/m5SQ////
///8+/v//////////////////////////////////////////////////////////////////Pz9////
///Jytb/AAE7/wkNSf8cJ3L/GSRu/wYJRP8DBkL/LyYp/zwvIP88MCn/Miok/5BvX//Soon/d15R/yYc
Ff+blJD///////z7+////////////////////////////////////////////////////////Pz9////
//+xscL/PD1k/z5Bbv8VHmX/GSJq/yIvg/8hLoH/FyFn/xcgZ/8YGUj/EhI9/2BLTf98YVH/i2tc/6J7
af9oUkf/KR4X/5uUkP///////Pv7////////////////////////////////////////////////////
///7+/z//////5eYrP8AACb/CQxF/xsnc/8cKHT/JTKK/yQxiP8bJ3P/HChz/woPS/8AAD3/dVlZ/6N9
Y/+Qbl3/lHFg/2RPRP8qHxj/mpSQ///////8+/v/////////////////////////////////////////
/////////////52ds/8rLVn/LzNo/xUdZP8aI2v/Ii6C/yMvhf8rOZn/KjiX/yIug/8iLoP/GCFo/xQd
Yv8qJ1H/LiZJ/1RESv9wWUz/U0Q8/y0gGf+ak4////////z7+///////////////////////////////
////////////////////////goKf/wAAJ/8PFFX/HCl1/xwodv8lMor/JjON/y89ov8uPKD/JTKL/yUy
iv8dKHb/HCh0/wkPTv8AAD7/OTFB/11OQv9LPjn/LiEa/5qTj////////Pv7////////////////////
//////////////////////////////////+Gh6T/AAAy/xceYP8iL4P/Ii+D/yo4l/8rOJj/Ljyh/y47
oP8qOJf/KjiY/yMvhP8iLoL/ERVT/wQHQP81LDv/UUE2/0c6NP8vIhv/mJGN///////6+fn/////////
/////////////////////////////////////////////4WHo/8AAC//Fx9i/yc0jv8mM43/Lz2j/y89
o/8uPKH/Ljyh/y89o/8vPaT/JjSO/yY0jf8QFlb/AwdB/yQgL/81LCL/PDIs/y4hGv+YkY3///////z7
+///////////////////////////////////////////////////////hYej/wAALv8ZIWf/Kzqa/yo3
lv8uPKH/Ljyh/y48of8uPKH/Ljyh/y48of8qN5b/KzmZ/xIYWf8AAj3/WEZQ/49vW/9kUEb/QzYv/3No
Y/+WkI3/wb27///////9/f3///////////////////////////////////////////+Fh6P/AAAt/xwj
bP8yQar/Ljyh/y48of8uPKH/Ljyh/y48of8uPKH/Ljyh/y48of8xQKj/FRpd/wAAOf+EZmv/3KeK/4Bm
Wf9NQjz/Rzky/x4QCf+Gfnr///////z8/P///////////////////////////////////////////4WH
o/8AAC3/GyJr/y8+pf8tO57/Ljyh/y48of8uPKH/Ljyh/y48of8uPKH/LTue/y8+pP8UGlz/AAA6/4Nn
bf/SpIn/on5t/5d1Zf9vWU3/LiQd/5CJhP//////+vn5////////////////////////////////////
////////hYej/wAALP8dJG//NESv/zA/pv8vPaP/Lz2j/y89o/8vPaP/Lz2j/y89o/8wP6b/M0Ou/xUb
X/8AADj/jHB2/9yvlf/BlYH/z5+G/4VoWv8mHBb/k4yH///////8/Pz/////////////////////////
//////////////////+Gh6P/AAAw/xUaWf8eJnP/ICh4/y05lv8sOZX/LDmV/yw5lf8tOZb/LTqX/x0m
dv8aInD/DxNP/wAAOv+LbnT/3K6U/7ySfv/GmID/f2RV/y4kHv9pX1n/nJWR/7q1sv///////Pv7////
/////////////////////////////4OEo/8AADf/BwlD/wAAMf8ECEb/FiiI/xYnhv8WJ4b/FieG/xUm
hf8VJ4f/CA5O/wUGPP8LDUj/AAJE/4pudf/crpT/vJJ+/8aYgP9+YlP/Ny0n/zktJv8UCQL/bWVh////
///8/Pz/////////////////////////////////kY+d/wkGJv8/NU7/Z1Jm/2ZVbv94bpX/d22U/3Rr
kv90a5L/em+X/390m/8fHj//AAIj/wgJKf8AAB//iWxp/9msk/+/lYH/zJ+H/4JmWP8vJiH/YVBF/450
Y/+TgXf/pqGd/724tf///////Pv7//////////////////////+knZf/KB0R/4VqWP/ruJz/4bKW//DE
pP/yxqX//9Cu///Rr//93rv//+fD/zszK/8AAAD/BgUE/wAAAP+WdWX/7rui/9eqlP/ouaH/kXRl/yUe
Gv+Ib2D//9i9/6mKeP8OBAD/XVRP///////9/f3//////////////////////6GbmP8kGRT/e2JX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v/RpY//wpeB/8iagv+DZ1n/KB4X/46G
gv//////w7+8/0tCO/98bGL/4bqj/8imkf9CNS3/ST44/+/t7f//////oJqW/yAVD/98Ylb/37CY/9aq
k//mvaX/6L+m//bLsv/2y7L/9suy//bLsv/2y7L/9suy//bLsv/3zLP/68Gp/+W9pf/Xq5X/4bCY/4tv
YP8gFhD/j4eC///////5+Pj//////8fDwf8wIxv/PjIr/zgsJf9GOzT/7u3s//////+po6D/Ny0o/4Fp
Xv/NoIr/xp2I/9OumP/VsJn/4ruk/+K7pP/iu6T/4ruk/+K7pP/iu6T/4ruk/+O8pf/Yspz/066Y/8ef
iv/OoYv/jHJk/zguKf+Zko7///////r6+v//////zsvJ/0Q4Mf9RRj//S0A6/1hOSP/v7u7///////f3
9///////qqSh/zQmHv9ENi//QTQs/0E0LP9CNC3/QjQt/0I0Lf9CNC3/QjQt/0I0Lf9CNC3/QjQt/0E0
LP9BNCz/RDcw/zMkHP+Wjor///////b19f////////7+///////8+/v/9PPz//X09P/19PT/9vX1//7+
/v///////Pz8//////+zrqz/OzAp/0tAOv9IPTf/SD03/0g9Nv9IPTb/SD02/0g9Nv9IPTb/SD02/0g9
Nv9IPTb/SD03/0g9N/9LQTv/Oi4n/56Ylf///////Pz7////////////////////////////////////
///////////////////+/v7///////f39//t7Oz/7u3t/+7t7f/u7e3/7u3t/+7t7f/u7e3/7u3t/+7t
7f/u7e3/7u3t/+7t7f/u7e3/7u3t/+7t7f/t7Oz/9vX1///////+/v7///////////////////////7+
/v/+/v7//v7+//7+/v//////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
</value>
</data>
</root>
47 changes: 16 additions & 31 deletions LittleWarGameClient/VersionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ internal Version? LatestVersion
}
}
}



public event EventHandler LatestVersionObtained;

public VersionHandler(Settings s)
Expand All @@ -44,14 +41,18 @@ public VersionHandler(Settings s)
CurrentVersion = new Version(0,0,0);
#endif
LatestVersionObtained += CheckForUpdate;
PerformCheck();
(new Thread(() =>
{
PerformCheck();
})).Start();
}

private async void PerformCheck()
{
if (CanCheckForUpdate())
{
LatestVersion = await TryGetLatestVersionAsync();
OverlayForm.Instance.AddOverlayMessage("updateCheck", new Notification("Checking for updates..."));
LatestVersion = await GetLatestGitHubVersion();
}
}

Expand Down Expand Up @@ -89,39 +90,23 @@ private bool CanCheckForUpdate()
return false;
return true;
}

private async Task<Version?> TryGetLatestVersionAsync()
private async Task<Version?> GetLatestGitHubVersion(int retries=3)
{
try
{
return await TimeoutAfter(GetLatestGitHubVersion(), TimeSpan.FromSeconds(10.0));
}
catch (Exception)
if (retries < 1)
{
OverlayForm.Instance.AddOverlayMessage("updateCheck", new Notification("Network Error: Could not obtain check for updates"));
OverlayForm.Instance.AddOverlayMessage("updateError", new Notification("Network Error: Could not check for newer versions"));
return null;
}
}
private async Task<Version> GetLatestGitHubVersion()
{
var client = new GitHubClient(new ProductHeaderValue("LWGClient"));
var release = await client.Repository.Release.GetLatest("ivanpmartell", "LittleWarGameClient");
return new Version(release.TagName.Substring(1));
}

private async Task<TResult> TimeoutAfter<TResult>(Task<TResult> task, TimeSpan timeout)
{
using var cts = new CancellationTokenSource();
var timeoutTask = Task<TResult>.Delay(timeout, cts.Token);
var completedTask = await Task<TResult>.WhenAny(task, timeoutTask).ConfigureAwait(false);
if (completedTask == task)
try
{
cts.Cancel();
return await task.ConfigureAwait(false);
var client = new GitHubClient(new ProductHeaderValue("LWGClient"));
var release = await client.Repository.Release.GetLatest("ivanpmartell", "LittleWarGameClient");
return new Version(release.TagName.Substring(1));
}
else
catch (Exception)
{
throw new TimeoutException($"Task timed out after {timeout}");
Thread.Sleep(2000);
return await GetLatestGitHubVersion(retries-1);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ This project is completely open source. Feel free to fork and make pull requests

# Important

If updating from versions below 0.2.0:
If updating from versions below 0.3.0:

Versions 0.2.0+ are not compatible with previous versions. Extract to an empty folder. Do not simply replace files with updated files.
Versions 0.3.0+ are not compatible with previous versions. Extract to an empty folder. Do not simply replace files with updated files.
Unfortunately this means that saved data from previous version cannot be carried over. Make sure to backup your data, e.g. downloaded replays

# Download

Grab the `lwg_client.zip` file from the latest release. Click [here](https://github.com/ivanpmartell/LittleWarGameClient/releases/latest) for easier access to the download.
Grab the `lwg_clientx64.zip` file from the latest release. Click [here](https://github.com/ivanpmartell/LittleWarGameClient/releases/latest) for easier access to the download.

0 comments on commit f3b0582

Please sign in to comment.