Skip to content

Commit

Permalink
Replaced WinForms Browser with WebView2, support for the site rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowberryHN committed Jul 21, 2023
1 parent 9d8bf15 commit 095c2fb
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
working-directory: ./bin/Release
run: |
Copy-Item "..\..\launcher.json" -Destination "."
Compress-Archive -Path .\CardboardLauncher.exe, .\launcher.json, .\Newtonsoft.Json.dll -DestinationPath CardboardLauncher.zip
Compress-Archive -Path .\CardboardLauncher.exe, .\launcher.json, .\Newtonsoft.Json.dll, .\Facepunch.Steamworks.Win64.dll, .\Microsoft.Web.WebView2.Core.dll, .\Microsoft.Web.WebView2.WinForms.dll, .\runtimes -DestinationPath CardboardLauncher.zip
- name: Create Release
id: create_release
uses: actions/create-release@v1
Expand Down
15 changes: 15 additions & 0 deletions CardboardLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -62,6 +64,12 @@
<Reference Include="Facepunch.Steamworks.Win64, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Facepunch.Steamworks.2.3.3\lib\net46\Facepunch.Steamworks.Win64.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.WebView2.Core, Version=1.0.1823.32, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Web.WebView2.1.0.1823.32\lib\net45\Microsoft.Web.WebView2.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.WebView2.WinForms, Version=1.0.1823.32, Culture=neutral, PublicKeyToken=2a8ab48044d2601e, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Web.WebView2.1.0.1823.32\lib\net45\Microsoft.Web.WebView2.WinForms.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -138,4 +146,11 @@
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="packages\Microsoft.Web.WebView2.1.0.1823.32\build\Microsoft.Web.WebView2.targets" Condition="Exists('packages\Microsoft.Web.WebView2.1.0.1823.32\build\Microsoft.Web.WebView2.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\Microsoft.Web.WebView2.1.0.1823.32\build\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Web.WebView2.1.0.1823.32\build\Microsoft.Web.WebView2.targets'))" />
</Target>
</Project>
79 changes: 42 additions & 37 deletions MainForm.Designer.cs

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

109 changes: 70 additions & 39 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Net;
using System.Security.Permissions;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using Newtonsoft.Json;
using Steamworks;

Expand All @@ -26,6 +28,7 @@ public partial class mainForm : Form

private Config config;

private string trusted_url = @"https://harpnetstudios.com";
private string api_url = @"https://harpnetstudios.com/hnid/api/";

private string migratePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My Games", "Project Crimson Alpha");
Expand Down Expand Up @@ -123,10 +126,10 @@ private void CheckVersion()
DisplayMessage(string.Format("Looks like your launcher is out of date!\n\nNew version available: {0}\nYour version: {1}", content, ver.ToString()), "Launcher Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch(WebException e)
catch(WebException)
{
technicalIssues = true;
webLauncher.Visible = false;
technicalIssues = technicalBody.Visible = technicalTitle.Visible = true;
//webLauncher.Visible = false;
playOfflineChkBox.Checked = true;
}
}
Expand Down Expand Up @@ -212,17 +215,13 @@ public mainForm()

webLauncher.Location = new Point(3, 3);
webLauncher.BringToFront();


webLauncher.Document.BackColor = this.BackColor;
#if DEBUG
webLauncher.ScriptErrorsSuppressed = false;
#else
webLauncher.ScriptErrorsSuppressed = true;
#endif

webLauncher.DefaultBackgroundColor = this.BackColor;

LoadConfig();
CheckVersion();

if (args.Length > 1 && args[1] == "--steam") ValidateSteam();
GrabInfo(config.gameToken);
if(config.homeDir == "") config.homeDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My Games", "Carmine Impact Alpha");
Expand All @@ -236,8 +235,6 @@ public mainForm()

pageSelectCombo.SelectedIndex = 0; // HNID

webLauncher.ObjectForScripting = this;
webLauncher.Url = new Uri(config.webUrl);
this.Text = launcherTitle.Text = LauncherInfo.gameName + " Launcher";
playButton.Text = "&Play " + LauncherInfo.gameName;
versionLabel.Text = "Launcher Version " + typeof(Program).Assembly.GetName().Version;
Expand Down Expand Up @@ -282,11 +279,6 @@ public void displayMessage(String message)
{
DisplayMessage(message, "Webpage");
}

public void setScroll(bool scroll)
{
this.webLauncher.ScrollBarsEnabled = scroll;
}

public void setGameToken(string token, bool quiet=true)
{
Expand Down Expand Up @@ -386,40 +378,67 @@ private void versionLabel_DoubleClick(object sender, EventArgs e)
DisplayMessage("Created by Yellowberry.\n\nSpecial thanks to the rest of the HarpNet crew!");
}

private void mainForm_Load(object sender, EventArgs e)
private async void mainForm_Load(object sender, EventArgs e)
{
webLauncher.ObjectForScripting = this;
webLauncher.DefaultBackgroundColor = this.BackColor;

await webLauncher.EnsureCoreWebView2Async();

webLauncher.CoreWebView2.AddWebResourceRequestedFilter($"*", CoreWebView2WebResourceContext.All);

webLauncher.CoreWebView2.WebResourceRequested += webLauncher_AddLauncherHeader;
webLauncher.CoreWebView2.NewWindowRequested += webLauncher_ExternalLink;

webLauncher.CoreWebView2.Settings.IsSwipeNavigationEnabled = false;
webLauncher.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
webLauncher.CoreWebView2.Settings.IsGeneralAutofillEnabled = false;
webLauncher.CoreWebView2.Settings.IsPasswordAutosaveEnabled = false;
webLauncher.CoreWebView2.Settings.AreHostObjectsAllowed = true;
webLauncher.CoreWebView2.Settings.IsBuiltInErrorPageEnabled = false;
webLauncher.CoreWebView2.Settings.IsPinchZoomEnabled = false;
webLauncher.CoreWebView2.Settings.IsReputationCheckingRequired = false;
webLauncher.CoreWebView2.Settings.IsZoomControlEnabled = false;
webLauncher.CoreWebView2.Settings.IsStatusBarEnabled = false;

#if DEBUG
webLauncher.CoreWebView2.Settings.AreDevToolsEnabled = true;
webLauncher.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true;
#else
webLauncher.CoreWebView2.Settings.AreDevToolsEnabled = false;
webLauncher.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
#endif

webLauncher.CoreWebView2.AddHostObjectToScript("form", this);

webLauncher.CoreWebView2.Settings.UserAgent += $" CardboardLauncher/{typeof(Program).Assembly.GetName().Version}";

webLauncher.Source = new Uri(config.webUrl);
}

private void webLauncher_Navigating(object sender, WebBrowserNavigatingEventArgs e)
private void webLauncher_Navigating(object sender, CoreWebView2NavigationStartingEventArgs e)
{
webLauncher.Document.BackColor = this.BackColor;
this.webLauncher.ScrollBarsEnabled = false;
if(e.Url.ToString() == "about:blank") { return; }

string extForce = "#_force";
string safeSite = "https://harpnetstudios.com/";
//this.webLauncher.ScrollBarsEnabled = false;
if(e.Uri.ToString() == "about:blank") { return; }

Console.WriteLine(e.Url.ToString());
if(!e.Url.ToString().StartsWith(safeSite)&&e.Url.ToString()!="about:blank")
Console.WriteLine(e.Uri.ToString());
if(!e.Uri.ToString().StartsWith(trusted_url)&&e.Uri.ToString()!="about:blank")
{
webWarn.BackColor = Color.Red;
technicalTitle.ForeColor = this.BackColor;
}
else
{
webWarn.BackColor = this.BackColor;

if (e.Url.ToString().EndsWith(extForce))
{
//cancel the current event
e.Cancel = true;

//this opens the URL in the user's default browser
Process.Start(e.Url.ToString().Remove(e.Url.ToString().Length - extForce.Length));
}
technicalTitle.ForeColor = Color.FromArgb(255,36,0);
}
}

private void webLauncher_ExternalLink(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
e.Handled = true;
Process.Start(e.Uri.ToString());
}

private void gameTokenBtn_Click(object sender, EventArgs e)
{
GametokenDialog gtDialog = new GametokenDialog();
Expand Down Expand Up @@ -523,9 +542,21 @@ private void playOfflineChkBox_CheckedChanged(object sender, EventArgs e)
}
}

private void webLauncher_Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
private void webLauncher_Loaded(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
if (e.IsSuccess)
{
//((WebView2)sender).ExecuteScriptAsync("document.querySelector('body').style.overflow='hidden'");
}
}

private void webLauncher_AddLauncherHeader(object sender, CoreWebView2WebResourceRequestedEventArgs e)
{
if (!webLauncher.Visible && !technicalIssues) webLauncher.Visible = true;
e.Request.Headers.SetHeader("X-Launcher-Version", typeof(Program).Assembly.GetName().Version.ToString());
if(!string.IsNullOrEmpty(config.gameToken))
{
e.Request.Headers.SetHeader("X-Game-Token", config.gameToken);
}
}
}
}
Loading

0 comments on commit 095c2fb

Please sign in to comment.