Skip to content

Commit

Permalink
Can now override js to add custom functionality. Mainly hotkeys.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpmartell committed Jul 24, 2024
1 parent 5e6b017 commit 70dac57
Show file tree
Hide file tree
Showing 6 changed files with 69,631 additions and 20 deletions.
20 changes: 16 additions & 4 deletions LittleWarGameClient/AddOns.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
);
},

fakeClick: function (anchorObj) {
if (anchorObj.click) {
anchorObj.click()
fakeClick: function (element) {
if (element.click) {
element.click()
} else if (document.createEvent) {
var event = new MouseEvent('click', {
'view': window
});
anchorObj.dispatchEvent(evt);
element.dispatchEvent(evt);
}
},

Expand Down Expand Up @@ -148,6 +148,8 @@ addons.init = {
function(clientVersion, mouseLock, volume) {
this.handleConnectionError();
this.addExitButton();
this.resizeInfoWindow();
this.moveLoadingText();
this.changeQuitButtonText();
this.addVolumeSlider(volume);
this.addClientVersion(clientVersion);
Expand All @@ -161,6 +163,16 @@ addons.init = {
this.jsInitComplete();
},

moveLoadingText: function () {
var loadingText = document.getElementById("loadingText");
loadingText.style.cssText = "top: 50px;";
},

resizeInfoWindow: function () {
var infoWindow = document.getElementById("playerInfoWindow");
infoWindow.style.cssText += "top: 50px; height: 580px;";
},

handleConnectionError: function () {
var connectionErrorWindow = document.getElementById("NoConnectionWindow");
if (connectionErrorWindow != null) {
Expand Down
3 changes: 2 additions & 1 deletion LittleWarGameClient/Form1.Designer.cs

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

28 changes: 23 additions & 5 deletions LittleWarGameClient/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace LittleWarGameClient
{
internal partial class Form1 : Form
{
private const string baseUrl = @"https://littlewargame.com/play";
private readonly Settings settings;
private readonly Fullscreen fullScreen;
private readonly KeyboardHandler kbHandler;
Expand Down Expand Up @@ -44,7 +45,7 @@ private async void InitWebView()
var path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
CoreWebView2Environment env = await CoreWebView2Environment.CreateAsync(null, Path.Join(path, "data"), new CoreWebView2EnvironmentOptions());
await webView.EnsureCoreWebView2Async(env);
webView.Source = new Uri("https://littlewargame.com/play", UriKind.Absolute);
webView.Source = new Uri(baseUrl, UriKind.Absolute);
webView.CoreWebView2.Profile.DefaultDownloadFolderPath = Path.Join(path, "downloads");
webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
Expand All @@ -53,12 +54,10 @@ private async void InitWebView()

private void webView_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
var addOnJS = System.IO.File.ReadAllText("AddOns.js");
webView.CoreWebView2.ExecuteScriptAsync(addOnJS);
var addonJS = System.IO.File.ReadAllText("addons.js");
webView.CoreWebView2.ExecuteScriptAsync(addonJS);
ElementMessage.CallJSFunc(webView, "init.function", $"\"{vHandler.CurrentVersion}\", {settings.GetMouseLock().ToString().ToLower()}, {settings.GetVolume()}");
kbHandler.InitHotkeyNames(settings);
gameHasLoaded = true;
ResizeGameWindows();
}

private void webView_WebMessageReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e)
Expand All @@ -84,6 +83,8 @@ private void webView_WebMessageReceived(object sender, Microsoft.Web.WebView2.Co
CaptureCursor();
break;
case ButtonType.InitComplete:
gameHasLoaded = true;
ResizeGameWindows();
loadingPanel.Visible = false;
loadingTimer.Enabled = false;
loadingText.Text = "Reconnecting";
Expand Down Expand Up @@ -139,6 +140,7 @@ private void Form1_ResizeEnd(object sender, EventArgs e)

private void Form1_Resize(object sender, EventArgs e)
{
mainImage.Top = this.Height / 4;
CaptureCursor();
ResizeGameWindows();
}
Expand Down Expand Up @@ -175,8 +177,24 @@ private void loadingTimer_Tick(object sender, EventArgs e)

private void webView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.Script);
webView.CoreWebView2.WebResourceRequested +=
delegate (object? sender, CoreWebView2WebResourceRequestedEventArgs args)
{
if (args.Request.Uri == $"{baseUrl}/js/lwg-5.0.0.js")
{
try
{
FileStream fs = File.Open("override/lwg-5.0.0.js", FileMode.Open);
CoreWebView2WebResourceResponse response = webView.CoreWebView2.Environment.CreateWebResourceResponse(fs, 200, "OK", "Content-Type: text/javascript");
args.Response = response;
}
catch { }
}
};
loadingPanel.Visible = true;
loadingTimer.Enabled = true;
gameHasLoaded = false;
}
}
}
6 changes: 5 additions & 1 deletion LittleWarGameClient/LittleWarGameClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@

<ItemGroup>
<None Remove="AddOns.js" />
<None Remove="override\lwg-5.0.0.js" />
</ItemGroup>

<ItemGroup>
<Content Include="AddOns.js">
<Content Include="addons.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="logo.ico" />
<Content Include="override\lwg-5.0.0.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 2 additions & 9 deletions LittleWarGameClient/VersionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,18 @@ private async void TryGetLatestVersionAsync()
private async Task<Version> GetLatestGitHubVersion()
{
var client = new GitHubClient(new ProductHeaderValue("LWGClient"));
IReadOnlyList<Release> releases = await client.Repository.Release.GetAll("ivanpmartell", "LittleWarGameClient");
return new Version(releases[0].TagName.Substring(1));
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)
{
// We need to be able to cancel the "timeout" task, so create a token source
using var cts = new CancellationTokenSource();
// Create the timeout task (don't await it)
var timeoutTask = Task<TResult>.Delay(timeout, cts.Token);

// Run the task and timeout in parallel, return the Task that completes first
var completedTask = await Task<TResult>.WhenAny(task, timeoutTask).ConfigureAwait(false);

if (completedTask == task)
{
// Cancel the "timeout" task so we don't leak a Timer
cts.Cancel();
// await the task to bubble up any errors etc
return await task.ConfigureAwait(false);
}
else
Expand Down
Loading

0 comments on commit 70dac57

Please sign in to comment.