Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDanOak committed Apr 6, 2024
2 parents bfb805e + a46aa52 commit f831994
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 49 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ If your local version is older than the most recent version according to the cli
* Ability to launch the game directly from the patcher
* UI fits the theme of the game
* Ignores the downloaded config so it doesn't overwrite the config you have locally
* Installs the game if not already installed

![New Version Available](./docs/img/new_version.png)

Expand All @@ -31,6 +32,5 @@ If your local version is older than the most recent version according to the cli
* Optional: Save this in some sort of config so it wouldn't need to be reset on every load
* Checkbox for auto-launching the application after patch or if patching is not necessary
* Optional: Save this in some sort of config so it wouldn't need to be reset on every load
* Install the game if it is not already there
* Embed News?
* Tests???
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows8.0</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -12,8 +12,10 @@
<Company>OakTech</Company>
<Product>Endless Online Patcher</Product>
<PackageIcon>eo-patcher-icon.png</PackageIcon>
<AssemblyVersion>0.0.0.4</AssemblyVersion>
<FileVersion>0.0.0.4</FileVersion>
<AssemblyVersion>0.0.0.5</AssemblyVersion>
<FileVersion>0.0.0.5</FileVersion>
<AssemblyName>eopatcher</AssemblyName>
<StartupObject>EndlessOnlinePatcher.Desktop.Program</StartupObject>
</PropertyGroup>

<ItemGroup>
Expand Down
23 changes: 22 additions & 1 deletion src/EndlessOnlinePatcher.Desktop/Main.Designer.cs

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

60 changes: 44 additions & 16 deletions src/EndlessOnlinePatcher.Desktop/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ private async void Main_Shown(object sender, EventArgs e)
}
else
{
lblMessage.Text = $"A new version of the client is available (v{_localVersion} -> v{_remoteVersion})";
lblMessage.Text = $"A new version of the client is available{Environment.NewLine}(v{_localVersion} -> v{_remoteVersion})";
pbxPatch.Visible = true;
pbxSkip.Visible = true;
}
pbxExit.Visible = true;
}
Expand Down Expand Up @@ -100,7 +101,7 @@ private void pbxLaunch_MouseLeave(object sender, EventArgs e)

private async void pbxLaunch_Click(object sender, EventArgs e)
{
await Windows.StartEO();
await Core.Windows.StartEO();
Close();
}

Expand All @@ -119,27 +120,42 @@ private void pbxExit_MouseLeave(object sender, EventArgs e)
pbxExit.Image = Resources.eo_exit;
}

private void pbxSkip_MouseEnter(object sender, EventArgs e)
{
pbxSkip.Image = Resources.skip_hover;
}

private void pbxSkip_MouseLeave(object sender, EventArgs e)
{
pbxSkip.Image = Resources.skip;
}

delegate void SetPatchTextCallback(string text);
private void SetPatchText(string text)
{
if (lblMessage.InvokeRequired)
{

SetPatchTextCallback d = new SetPatchTextCallback(SetPatchText);
Invoke(d, new object[] { text });
return;
}

lblMessage.Text = text;
}

private async void pbxPatch_MouseClick(object sender, MouseEventArgs e)
{
if (_patching) return;

_patching = true;
pbxSkip.Visible = false;
pbxPatch.Image = Resources.eo_patching;
var status = () => "Downloading Latest Patch";
using var patcher = new Patcher(new Progress<int>(x =>
{
if (x >= 99 && status().Contains("Extracting"))
{
// weird multithreading bug because of this I think
lblMessage.Text = "Patch applied! You are now on the latest version and can launch the game";
return;
}
lblMessage.Text = $"{status()}... {x}%.";
}), _downloadLink);

using var patcher = new Patcher(_downloadLink, SetPatchText);

await patcher.Patch(_remoteVersion!);
status = () => "Extracting and Applying Latest Patch";
patcher.ApplyPatch(_remoteVersion!);

_patching = false;
pbxPatch.Visible = false;
pbxLaunch.Visible = true;
Expand All @@ -158,11 +174,13 @@ private void pbxExit_MouseDown(object sender, MouseEventArgs e)

private void pbxPatch_MouseDown(object sender, MouseEventArgs e)
{
if (_patching) return;
_sndClickDown.Play();
}

private void pbxPatch_MouseUp(object sender, MouseEventArgs e)
{
if (_patching) return;
_sndClickUp.Play();
}

Expand All @@ -185,4 +203,14 @@ private void pbxLogout_MouseUp(object sender, MouseEventArgs e)
{
_sndClickUp.Play();
}

private void pbxSkip_MouseDown(object sender, MouseEventArgs e)
{
_sndClickDown.Play();
}

private void pbxSkip_MouseUp(object sender, MouseEventArgs e)
{
_sndClickUp.Play();
}
}
20 changes: 20 additions & 0 deletions src/EndlessOnlinePatcher.Desktop/Properties/Resources.Designer.cs

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

6 changes: 6 additions & 0 deletions src/EndlessOnlinePatcher.Desktop/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@
<data name="eo_popup" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\eo-popup.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="skip" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\skip.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="skip_hover" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\skip-hover.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="text_window" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\text-window.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/EndlessOnlinePatcher/Core/IPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
internal interface IPatcher
{
public Task Patch(FileVersion version);
public void ApplyPatch(FileVersion version);
}
72 changes: 48 additions & 24 deletions src/EndlessOnlinePatcher/Core/Patcher.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,78 @@
using EndlessOnlinePatcher.Extensions;
using System.Diagnostics;
using System.IO.Compression;
using System.Numerics;
using System.Text.RegularExpressions;

namespace EndlessOnlinePatcher.Core;

public sealed class Patcher : IPatcher, IDisposable
{
private IProgress<int> _progress { get; }
private string _link { get; }

public Patcher(IProgress<int> progress, string downloadLink)
private Action<string> _setPatchTextCallback;

public Patcher(string downloadLink, Action<string> setPatchTextCallback)
{
_progress = progress;
_link = downloadLink;
_setPatchTextCallback = setPatchTextCallback;
}

public async Task Patch(FileVersion version)
{
Clean();
using var httpClient = new HttpClient();
using var fileStream = new FileStream("patch.zip", FileMode.Create, FileAccess.Write);
await httpClient.DownloadAsync(_link, fileStream, _progress);
fileStream.Close();
await DownloadPatch();
await ApplyPatch(version);
}

private string GetDirectoryFrom(string filePath)
private async Task DownloadPatch()
{
var regex = new Regex("(.*)\\\\");
return regex.Match(filePath).Value;
var progress = new Progress<int>(x => _setPatchTextCallback($"Downloading... {x}%"));
using var httpClient = new HttpClient();
using var fileStream = new FileStream("patch.zip", FileMode.Create, FileAccess.Write);
await httpClient.DownloadAsync(_link, fileStream, progress);
fileStream.Close();
}

public void ApplyPatch(FileVersion version)
private async Task ApplyPatch(FileVersion version)
{
var localDirectory = EndlessOnlineDirectory.Get().FullName;
var patchFolder = $"patch-{version}/";

ZipFile.ExtractToDirectory("patch.zip", patchFolder);
var allExceptConfig = Directory.EnumerateFiles(patchFolder, "*", SearchOption.AllDirectories)
var patchFiles = Directory.EnumerateFiles(patchFolder, "*", SearchOption.AllDirectories)
.Select(x => x.Remove(0, patchFolder.Length))
.Where(x => !x.StartsWith("config"));
.ToList();

var i = 0;
var localDirectory = EndlessOnlineDirectory.Get().FullName;
foreach (var file in allExceptConfig)
{
var relativeDirectory = $"{localDirectory}/{GetDirectoryFrom(file)}";
if (!Directory.Exists(relativeDirectory))
Directory.CreateDirectory(relativeDirectory);
var completed = 0;

File.Copy($"{patchFolder}/{file}", $"{localDirectory}/{file}", true);
i++;
_progress.Report(i / allExceptConfig.Count() * 100);
}
var extractTasks = patchFiles
.Select(file => Task.Run(() =>
{
var relativeDirectory = $"{localDirectory}/{GetDirectoryFrom(file)}";
if (!Directory.Exists(relativeDirectory))
Directory.CreateDirectory(relativeDirectory);
File.Copy($"{patchFolder}/{file}", $"{localDirectory}/{file}", true);
completed++;
var percent = 100f * completed / patchFiles.Count;
#if DEBUG
Debug.WriteLine($"Copying {file} to {localDirectory}/{file} {completed}/{patchFiles.Count} {percent}%");
#endif
_setPatchTextCallback($"Extracting... {(int)percent}%");
}));

await Task.WhenAll(extractTasks);

_setPatchTextCallback($"Patch applied! You are now on the latest version v{version}. Enjoy!");
}

private static string GetDirectoryFrom(string filePath)
{
var regex = new Regex("(.*)\\\\");
return regex.Match(filePath).Value;
}

private static void Clean()
Expand Down
3 changes: 1 addition & 2 deletions src/EndlessOnlinePatcher/Core/Windows.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace EndlessOnlinePatcher.Core;

Expand Down
2 changes: 1 addition & 1 deletion src/EndlessOnlinePatcher/EndlessOnlinePatcher.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down

0 comments on commit f831994

Please sign in to comment.