Skip to content

Commit

Permalink
Merge pull request #58 from PenguLoader/dev
Browse files Browse the repository at this point in the history
Final v1.0.2
  • Loading branch information
nomi-san committed Apr 18, 2023
2 parents 61643bf + 93c34b3 commit eed95c5
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 642 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,4 @@ FodyWeavers.xsd

bin/*
packages/
d3d9/src/renderer/extension.h
*.g.h
8 changes: 4 additions & 4 deletions core/core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
</Link>
<PreBuildEvent>
<Command>cl bin2c.c /link /out:$(Configuration)\bin2c.exe
$(Configuration)\bin2c.exe src/renderer/extension.js src/renderer/extension.h _ext_code</Command>
$(Configuration)\bin2c.exe src/renderer/extension.js src/renderer/extension.g.h _ext_code</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -177,7 +177,7 @@ $(Configuration)\bin2c.exe src/renderer/extension.js src/renderer/extension.h _e
</Link>
<PreBuildEvent>
<Command>cl bin2c.c /link /out:"$(Platform)\$(Configuration)\bin2c.exe"
"$(Platform)\$(Configuration)\bin2c.exe" src/renderer/extension.js src/renderer/extension.h _ext_code</Command>
"$(Platform)\$(Configuration)\bin2c.exe" src/renderer/extension.js src/renderer/extension.g.h _ext_code</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand Down Expand Up @@ -210,7 +210,7 @@ $(Configuration)\bin2c.exe src/renderer/extension.js src/renderer/extension.h _e
</Link>
<PreBuildEvent>
<Command>cl bin2c.c /link /out:$(Configuration)\bin2c.exe
$(Configuration)\bin2c.exe src/renderer/extension.js src/renderer/extension.h _ext_code</Command>
$(Configuration)\bin2c.exe src/renderer/extension.js src/renderer/extension.g.h _ext_code</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down Expand Up @@ -239,7 +239,7 @@ $(Configuration)\bin2c.exe src/renderer/extension.js src/renderer/extension.h _e
</Link>
<PreBuildEvent>
<Command>cl bin2c.c /link /out:"$(Platform)\$(Configuration)\bin2c.exe"
"$(Platform)\$(Configuration)\bin2c.exe" src/renderer/extension.js src/renderer/extension.h _ext_code</Command>
"$(Platform)\$(Configuration)\bin2c.exe" src/renderer/extension.js src/renderer/extension.g.h _ext_code</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
493 changes: 0 additions & 493 deletions core/src/renderer/extension.h

This file was deleted.

2 changes: 1 addition & 1 deletion core/src/renderer/renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "include/capi/cef_app_capi.h"
#include "include/capi/cef_v8_capi.h"

#include "extension.h"
#include "extension.g.h"

// RENDERER PROCESS ONLY.

Expand Down
266 changes: 127 additions & 139 deletions loader/Main/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,139 @@ namespace PenguLoader.Main
{
static class Updater
{
private const string ApiUrl = "https://api.github.com/repos/{0}/releases/latest";
private const string DownloadUrl = "https://github.com/{0}/releases/latest";
private const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" +
static string ApiUrl => $"https://api.github.com/repos/{Program.GithubRepo}/releases/latest";
static string DownloadUrl => $"https://github.com/{Program.GithubRepo}/releases/latest";

const string USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" +
" AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";

class Update
{
public string Version;
public string Changes;
public string DownloadUrl;
}

static Updater()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
}

public static async Task CheckUpdate()
public static async void CheckUpdate()
{
var update = await FetchUpdate();
if (update == null) return;

await ShowProgressDialog(update);
var dialog = new ProgressDialog()
{
WindowTitle = Program.Name + " v" + update.Version,
ShowTimeRemaining = false,
ProgressBarStyle = ProgressBarStyle.MarqueeProgressBar
};

var cancel = false;
var percent = 0;
var message = "Downloading...";

dialog.DoWork += (s, e) =>
{
while (percent < 100)
{
if (cancel || dialog.CancellationPending)
{
e.Cancel = true;
return;
}
dialog.ReportProgress(percent, "Downloading update...", message);
Thread.Sleep(100);
}
Thread.Sleep(100);
dialog.ReportProgress(100, "Updating...", "Done.");
};

MainWindow.Instance.Hide();
dialog.Show();

try
{
var updateDir = Path.Combine(Directory.GetCurrentDirectory(), ".update");

var tempFile = Path.GetTempFileName();
await DownloadFile(update.DownloadUrl, tempFile, (downloaded, total, percent_) =>
{
percent = percent_;
message = String.Format("{0:0.##} / {1:0.##} MB received.",
(double)downloaded / 1024 / 1024,
(double)total / 1024 / 1024);
});

Utils.DeletePath(updateDir, true);
ZipFile.ExtractToDirectory(tempFile, updateDir);
Utils.DeletePath(tempFile);

while (Module.IsLoaded())
{
MessageBox.Show("Please close your League of Legends Client to apply update.",
Program.Name, MessageBoxButton.OK, MessageBoxImage.Information);
}

ApplyUpdate();
Environment.Exit(0);
}
catch
{
cancel = true;

MainWindow.Instance.Show();
MessageBox.Show(MainWindow.Instance,
"Failed to download update. Please try downloading the update on GitHub releases page.",
Program.Name, MessageBoxButton.OK, MessageBoxImage.Warning);

Utils.OpenLink(DownloadUrl);
}
finally
{
dialog.Dispose();
}
}

private static async Task<Update> FetchUpdate()
static async Task<Update> FetchUpdate()
{
try
{
var apiUrl = string.Format(ApiUrl, Program.GithubRepo);
var json = await DownloadString(apiUrl);
var remoteVersion = ExtractVersion(json);
var localVersion = new System.Version(Version.VERSION);
var json = await DownloadString(ApiUrl);
var match = new Regex("\"tag_name\":\\s+\"(.*)\"").Match(json);

if (remoteVersion.CompareTo(localVersion) > 0)
if (match.Success && match.Groups.Count > 1)
{
return new Update
var vtag = match.Groups[1].Value.ToLower();
if (vtag.StartsWith("v"))
vtag = vtag.Substring(1);

var remote = new System.Version(vtag);
var local = new System.Version(Version.VERSION);

if (remote.CompareTo(local) > 0)
{
Version = remoteVersion.ToString(),
Changes = ExtractChanges(json),
DownloadUrl = ExtractDownloadUrl(json)
};
string changes = "";
match = new Regex("\"body\":\\s+\"(.*)\"").Match(json);
if (match.Success && match.Groups.Count > 1)
changes = Regex.Unescape(match.Groups[1].Value);

string downloadUrl = "";
match = new Regex("\"browser_download_url\":\\s+\"(.*(\\d+\\.?)(?:-stable)?\\.zip)\"").Match(json);
if (match.Success && match.Groups.Count > 1)
downloadUrl = Regex.Unescape(match.Groups[1].Value);

return new Update
{
Version = vtag,
Changes = changes,
DownloadUrl = downloadUrl
};
}
}

return null;
Expand All @@ -61,20 +159,25 @@ private static async Task<Update> FetchUpdate()
}
}

private static async Task<string> DownloadString(string url)
static async Task<string> DownloadString(string url)
{
using (var client = new WebClient())
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.UserAgent = USER_AGENT;

using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
client.Headers.Add(HttpRequestHeader.UserAgent, UserAgent);
return await client.DownloadStringTaskAsync(url);
return await reader.ReadToEndAsync();
}
}

private static async Task DownloadFile(string url, string path, Action<long, long, int> onProgress)
static async Task DownloadFile(string url, string path, Action<long, long, int> onProgress)
{
using (var client = new WebClient())
using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.UserAgent, UserAgent);
client.Headers.Add("User-Agent", USER_AGENT);
client.DownloadProgressChanged += (s, e) =>
{
onProgress.Invoke(e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);
Expand All @@ -84,7 +187,7 @@ private static async Task DownloadFile(string url, string path, Action<long, lon
}
}

private static void ApplyUpdate()
static void ApplyUpdate()
{
var exe = AppDomain.CurrentDomain.FriendlyName;
var dir = Directory.GetCurrentDirectory();
Expand All @@ -107,120 +210,5 @@ private static void ApplyUpdate()
WindowStyle = ProcessWindowStyle.Hidden
});
}

private static System.Version ExtractVersion(string json)
{
var match = new Regex("\"tag_name\":\\s+\"(.*)\"").Match(json);
return match.Success && match.Groups.Count > 1
? new System.Version(match.Groups[1].Value.TrimStart('v').ToLower())
: null;
}

private static string ExtractChanges(string json)
{
var match = new Regex("\"body\":\\s+\"(.*)\"").Match(json);
return match.Success && match.Groups.Count > 1
? Regex.Unescape(match.Groups[1].Value)
: string.Empty;
}

private static string ExtractDownloadUrl(string json)
{
var match = new Regex("\"browser_download_url\":\\s+\"(.*(\\d+\\.?)(?:-stable)?\\.zip)\"").Match(json);
return match.Success && match.Groups.Count > 1
? Regex.Unescape(match.Groups[1].Value)
: string.Empty;
}
private static async Task ShowProgressDialog(Update update)
{
var dialog = new ProgressDialog()
{
WindowTitle = $"{Program.Name} v{update.Version}",
ShowTimeRemaining = false,
ProgressBarStyle = ProgressBarStyle.MarqueeProgressBar
};

var cancellationTokenSource = new CancellationTokenSource();
IProgress<DownloadProgress> progress = new Progress<DownloadProgress>(p => dialog.ReportProgress(p.Percent, $"Downloading update...",
$"{p.DownloadedMb:0.##} / {p.TotalMb:0.##} MB received."));

var tcs = new TaskCompletionSource<bool>();

dialog.DoWork += async (s, e) =>
{
try
{
var tempFile = Path.GetTempFileName();
var updateDir = Path.Combine(Directory.GetCurrentDirectory(), ".update");
await DownloadFile(update.DownloadUrl, tempFile, (downloaded, total, percent) =>
{
if (cancellationTokenSource.Token.IsCancellationRequested)
{
cancellationTokenSource.Cancel();
return;
}
progress.Report(new DownloadProgress(downloaded, total, percent));
});
Utils.DeletePath(updateDir, true);
ZipFile.ExtractToDirectory(tempFile, updateDir);
Utils.DeletePath(tempFile);
while (Module.IsLoaded())
{
MessageBox.Show("Please close your League of Legends Client to apply the update.",
Program.Name, MessageBoxButton.OK, MessageBoxImage.Information);
}
ApplyUpdate();
Environment.Exit(0);
}
catch
{
if (!cancellationTokenSource.Token.IsCancellationRequested)
{
cancellationTokenSource.Cancel();
MainWindow.Instance.Show();
MessageBox.Show(MainWindow.Instance,
"Failed to download update. Please try downloading the update on GitHub releases page.",
Program.Name, MessageBoxButton.OK, MessageBoxImage.Warning);
Utils.OpenLink(string.Format(DownloadUrl, Program.GithubRepo));
}
}
finally
{
tcs.SetResult(true);
}
};

MainWindow.Instance.Hide();
dialog.Show(MainWindow.Instance);

await tcs.Task;
}

private class Update
{
public string Version;
public string Changes;
public string DownloadUrl;
}

private class DownloadProgress
{
public DownloadProgress(long downloaded, long total, int percent)
{
DownloadedMb = (double)downloaded / 1024 / 1024;
TotalMb = (double)total / 1024 / 1024;
Percent = percent;
}

public double DownloadedMb { get; }
public double TotalMb { get; }
public int Percent { get; }
}
}
}
2 changes: 1 addition & 1 deletion loader/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
Icon="ExternalLink" Width="14" Margin="5,0,0,-1" />
</InlineUIContainer></TextBlock>
</Button>
<ui:ToggleSwitch x:Name="btnActivate" OffContent="OFF" OnContent="READY" Cursor="Hand" Margin="268,162,0,25" RenderTransformOrigin="0.5,0.5" Width="128" Height="31">
<ui:ToggleSwitch x:Name="btnActivate" OffContent="OFF" OnContent="READY" Cursor="Hand" Focusable="False" Margin="268,162,0,25" RenderTransformOrigin="0.5,0.5" Width="128" Height="31">
<ui:ToggleSwitch.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1.4" ScaleY="1.4"/>
Expand Down
Loading

0 comments on commit eed95c5

Please sign in to comment.