Skip to content

Commit

Permalink
Organized the project better
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpmartell committed Aug 29, 2024
1 parent 4c22142 commit 2b7ac7c
Show file tree
Hide file tree
Showing 28 changed files with 117 additions and 107 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
1 change: 1 addition & 0 deletions LittleWarGameClient/GameForm.Designer.cs

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

91 changes: 51 additions & 40 deletions LittleWarGameClient/GameForm.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Windows.Forms;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection;
using System.Data;
using System.Diagnostics;
using NAudio.CoreAudioApi;
using nud2dlib.Windows.Forms;
using nud2dlib;
using CefSharp;
using CefSharp.Handler;
using CefSharp.WinForms;
using CefSharp.DevTools.Debugger;
using LittleWarGameClient.Handlers;
using LittleWarGameClient.Helpers;
using LittleWarGameClient.Interceptors;

namespace LittleWarGameClient
{
Expand All @@ -31,12 +20,14 @@ internal static GameForm Instance
}

internal const string baseUrl = @"https://littlewargame.com/play";
private readonly Settings settings;
private readonly SettingsHandler settings;
private readonly KeyboardHandler kbHandler;
private readonly VersionHandler vHandler;
private readonly AudioManager audioMngr;
private readonly AudioHandler audioMngr;
private FormWindowState PreviousWindowState;

internal int requestCallCounter = 0;
private int requestCallWhereLoadingFinished = -1;
private bool wasSmallWindow = false;
private bool gameHasLoaded = false;
private bool mouseLocked;
Expand All @@ -45,9 +36,9 @@ internal GameForm()
{
PreInitWeb();
InitializeComponent();
loadingText.Font = new Font(FontManager.lwgFont, 48F, FontStyle.Regular, GraphicsUnit.Point);
settings = new Settings();
audioMngr = new AudioManager(Text);
loadingText.Font = new Font(FontHandler.lwgFont, 48F, FontStyle.Regular, GraphicsUnit.Point);
settings = new SettingsHandler();
audioMngr = new AudioHandler(Text);
kbHandler = new KeyboardHandler(settings);
vHandler = new VersionHandler(settings);
InitScreen();
Expand Down Expand Up @@ -195,26 +186,6 @@ private void GameForm_Load(object sender, EventArgs e)
Activate();
}

private void webView_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (e.IsLoading)
{
InvokeUI(() =>
{
loadingPanel.Visible = true;
loadingTimer.Enabled = true;
gameHasLoaded = false;
});
}
else // has loaded
{
var addonJS = System.IO.File.ReadAllText("js/addons.js");
webBrowser.ExecuteScriptAsync(addonJS);
ElementMessage.CallJSFunc(webBrowser, "init.function", $"\"{vHandler.CurrentVersion}\", {settings.GetMouseLock().ToString().ToLower()}, {settings.GetVolume()}");
kbHandler.InitHotkeyNames((ChromiumWebBrowser)sender, settings);
}
}

internal void InvokeUI(Action a)
{
if (formInstance != null && formInstance.InvokeRequired)
Expand Down Expand Up @@ -289,7 +260,6 @@ internal void AddonsLoadedPostLogic()
ForceResizeGameWindows();
loadingPanel.Visible = false;
loadingTimer.Enabled = false;
loadingText.Text = "Reconnecting";
}

internal void ChangeVolume(float value)
Expand All @@ -303,5 +273,46 @@ internal async void VolumeChangePostLogic(float value)
settings.SetVolume(value);
await settings.SaveAsync();
}

private void webView_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (e.IsLoading)
{
InvokeUI(() =>
{
loaderImage.Visible = true;
loadingPanel.Visible = true;
loadingText.Text = "Loading";
loadingText.Enabled = true;
loadingTimer.Enabled = true;
gameHasLoaded = false;
});
}
else // has loaded
{
if (requestCallWhereLoadingFinished < requestCallCounter)
{
requestCallWhereLoadingFinished = requestCallCounter;
var addonJS = System.IO.File.ReadAllText("js/addons.js");
webBrowser.ExecuteScriptAsync(addonJS);
ElementMessage.CallJSFunc(webBrowser, "init.function", $"\"{vHandler.CurrentVersion}\", {settings.GetMouseLock().ToString().ToLower()}, {settings.GetVolume()}");
kbHandler.InitHotkeyNames((ChromiumWebBrowser)sender, settings);
}
}
}

private void webView_LoadError(object sender, LoadErrorEventArgs e)
{
InvokeUI(() =>
{
loaderImage.Visible = false;
loadingText.Text = "ERROR";
loadingText.Enabled = false;
loadingText.Visible = true;
loadingTimer.Enabled = false;
gameHasLoaded = false;
});
OverlayForm.Instance.AddOverlayMessage("loadError", new Notification("Error: Website could not be loaded"));
}
}
}
4 changes: 2 additions & 2 deletions LittleWarGameClient/GameForm.resx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Microsoft ResX Schema
Version 2.0
Expand Down Expand Up @@ -48,7 +48,7 @@
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
using System.Text;
using System.Threading.Tasks;

namespace LittleWarGameClient
namespace LittleWarGameClient.Handlers
{
internal class AudioManager : IAudioSessionEventsHandler
internal class AudioHandler : IAudioSessionEventsHandler
{
private MMDevice? mainDevice;
private AudioSessionControl? currentSession;
private readonly string formTitle;

public AudioManager(string formTitle)
public AudioHandler(string formTitle)
{
this.formTitle = formTitle;
var etor = new MMDeviceEnumerator();
Expand Down Expand Up @@ -59,7 +59,7 @@ private void UnregisterFromSession(AudioSessionControl? session)
}
}

private void ChangeTextAndIcon(AudioSessionControl session)
private void ChangeTextAndIcon(AudioSessionControl session)
{
if (session.IsSystemSoundsSession)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using System.Threading.Tasks;
using static nud2dlib.Windows.Forms.Win32;

namespace LittleWarGameClient
namespace LittleWarGameClient.Handlers
{
internal class FontManager
internal class FontHandler
{
private static FontFamily? gameFont;
internal static FontFamily lwgFont
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
using System.Reflection;
using CefSharp.WinForms;
using CefSharp;
using LittleWarGameClient.Helpers;

namespace LittleWarGameClient
namespace LittleWarGameClient.Handlers
{
internal class KeyboardHandler : IKeyboardHandler
{
private readonly Dictionary<Keys, MethodInfo?> hotKeys = new Dictionary<Keys, MethodInfo?>();
internal bool hasHangingAltKey = false;

internal KeyboardHandler(Settings settings)
internal KeyboardHandler(SettingsHandler settings)
{
InitHotkeys(settings);
}

private void InitHotkeys(Settings settings)
private void InitHotkeys(SettingsHandler settings)
{
Type type = typeof(Settings);
Type type = typeof(SettingsHandler);
foreach (var methodInfo in type.GetMethods().Where(p => Attribute.IsDefined(p, typeof(Hotkey))))
{
object[] attribute = methodInfo.GetCustomAttributes(typeof(Hotkey), true);
Expand All @@ -28,7 +29,7 @@ private void InitHotkeys(Settings settings)
Hotkey hotkey = (Hotkey)attribute[0];
if (hotkey.FuncToCall != null)
{
Type thisType = this.GetType();
Type thisType = GetType();
funcToCall = thisType.GetMethod(hotkey.FuncToCall, BindingFlags.NonPublic | BindingFlags.Instance);
}
}
Expand All @@ -38,9 +39,9 @@ private void InitHotkeys(Settings settings)
}
}

internal void InitHotkeyNames(ChromiumWebBrowser sender, Settings settings)
internal void InitHotkeyNames(ChromiumWebBrowser sender, SettingsHandler settings)
{
Type type = typeof(Settings);
Type type = typeof(SettingsHandler);
foreach (var methodInfo in type.GetMethods().Where(p => Attribute.IsDefined(p, typeof(Hotkey))))
{
object[] attribute = methodInfo.GetCustomAttributes(typeof(Hotkey), true);
Expand Down Expand Up @@ -71,12 +72,12 @@ private void OptionsMenuHotkeyFunc(ChromiumWebBrowser sender)

private void ChatHistoryHotkeyFunc(ChromiumWebBrowser sender)
{
ElementMessage.CallJSFunc((ChromiumWebBrowser)sender, "toggleChat");
ElementMessage.CallJSFunc(sender, "toggleChat");
}

private void FriendsHotkeyFunc(ChromiumWebBrowser sender)
{
ElementMessage.CallJSFunc((ChromiumWebBrowser)sender, "toggleFriends");
ElementMessage.CallJSFunc(sender, "toggleFriends");
}

public bool OnPreKeyEvent(IWebBrowser webView, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
Expand Down Expand Up @@ -117,6 +118,7 @@ public bool OnKeyEvent(IWebBrowser webView, IBrowser browser, KeyType type, int
{
if (isSystemKey && type == KeyType.Char)
return true;
//Matches Alt+F16 key press related to Alt-Tab fix for game
if (windowsKeyCode == 0x7F && modifiers == CefEventFlags.AltDown)
hasHangingAltKey = false;
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using IniFile;
using LittleWarGameClient.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace LittleWarGameClient
namespace LittleWarGameClient.Handlers
{
internal class Settings
internal class SettingsHandler
{
private const string fileName = "Settings.ini";
private const int defaultWidth = 1280;
Expand All @@ -25,7 +26,7 @@ internal class Settings
private readonly Ini settings;
private readonly SettingsHelper helper;

public Settings()
public SettingsHandler()
{
if (!File.Exists(fileName))
settings = CreateDefaultIniFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
using System.Text;
using System.Threading.Tasks;

namespace LittleWarGameClient
namespace LittleWarGameClient.Handlers
{
internal class VersionHandler
{
readonly Settings settings;
readonly SettingsHandler settings;
internal Version CurrentVersion { get; private set; }
private Version? latestVersion;
internal Version? LatestVersion
Expand All @@ -32,19 +32,19 @@ internal Version? LatestVersion
}
public event EventHandler LatestVersionObtained;

public VersionHandler(Settings s)
public VersionHandler(SettingsHandler s)
{
settings = s;
var productVersion = System.Windows.Forms.Application.ProductVersion.Split('+').First();
CurrentVersion = new Version(productVersion);
#if DEBUG
CurrentVersion = new Version(0,0,0);
CurrentVersion = new Version(0, 0, 0);
#endif
LatestVersionObtained += CheckForUpdate;
(new Thread(() =>
new Thread(() =>
{
PerformCheck();
})).Start();
}).Start();
}

private async void PerformCheck()
Expand Down Expand Up @@ -90,7 +90,7 @@ private bool CanCheckForUpdate()
return false;
return true;
}
private async Task<Version?> GetLatestGitHubVersion(int retries=3)
private async Task<Version?> GetLatestGitHubVersion(int retries = 3)
{
if (retries < 1)
{
Expand All @@ -106,7 +106,7 @@ private bool CanCheckForUpdate()
catch (Exception)
{
Thread.Sleep(2000);
return await GetLatestGitHubVersion(retries-1);
return await GetLatestGitHubVersion(retries - 1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Text;
using System.Threading.Tasks;

namespace LittleWarGameClient
namespace LittleWarGameClient.Helpers
{
public static class Extensions
public static class PanelHelper
{
public static void SetDoubleBuffered(this Panel panel)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text;
using System.Threading.Tasks;

namespace LittleWarGameClient
namespace LittleWarGameClient.Helpers
{
internal class SettingsHelper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace LittleWarGameClient
namespace LittleWarGameClient.Helpers
{
internal class ElementMessage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using CefSharp;
using CefSharp.Handler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LittleWarGameClient
namespace LittleWarGameClient.Interceptors
{
internal class ContextMenuInterceptor : ContextMenuHandler
{
Expand Down
Loading

0 comments on commit 2b7ac7c

Please sign in to comment.