Skip to content

Commit

Permalink
checking for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
BadRyuner committed Feb 25, 2022
1 parent 6edcbde commit 33dc61a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
85 changes: 83 additions & 2 deletions PulsarInjector/Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
Expand Down Expand Up @@ -198,16 +199,18 @@ public static void InstallModLoader(string targetAssemblyPath)

public static void CopyAssemblies(string targetAssemblyDir)
{
string PulsarModLoaderDll = CheckForUpdates(typeof(PulsarModLoader.PulsarMod).Assembly.Location);

/* Copy important assemblies to target assembly's directory */
string sourceDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string[] copyables = new string[] {
typeof(PulsarModLoader.PulsarMod).Assembly.Location,
PulsarModLoaderDll,
Path.Combine(sourceDir, "0Harmony.dll")
};

foreach (string sourcePath in copyables)
{
string destPath = Path.Combine(targetAssemblyDir, Path.GetFileName(sourcePath));
string destPath = Path.Combine(targetAssemblyDir, Path.GetFileName(sourcePath).Replace("_updated.dll", ".dll"));
Logger.Info($"Copying {Path.GetFileName(destPath)} to {Path.GetDirectoryName(destPath)}");
try
{
Expand All @@ -220,5 +223,83 @@ public static void CopyAssemblies(string targetAssemblyDir)
}
};
}

public static string CheckForUpdates(string CurrentPMLDll)
{
string version = System.Diagnostics.FileVersionInfo.GetVersionInfo(CurrentPMLDll).FileVersion;
bool useOtherDll = false;

if (File.Exists(CurrentPMLDll.Replace(".dll", "_updated.dll")))
{
string UpdatedPMLDll = CurrentPMLDll.Replace(".dll", "_updated.dll");
string UpdatedVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(UpdatedPMLDll).FileVersion;
short[] versionAsNum = version.Split('.').Select(s => short.Parse(s)).ToArray();
short[] UpdatedVersionAsNum = UpdatedVersion.Split('.').Select(s => short.Parse(s)).ToArray();

for (byte i = 0; i < 4; i++)
if (UpdatedVersionAsNum[i] > versionAsNum[i])
{
CurrentPMLDll = UpdatedPMLDll;
version = UpdatedPMLDll;
useOtherDll = true;
break;
}
else if (UpdatedVersionAsNum[i] < versionAsNum[i])
{
break;
}
}

Logger.Info("=== Updates ===");
Logger.Info("Check for a newer version of PML?");
Logger.Info("(Y/N)");

if (Console.ReadLine().ToUpper() == "N")
return CurrentPMLDll;

using (var web = new System.Net.WebClient())
{
web.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36");

string[] info = web.DownloadString("https://api.github.com/repos/PULSAR-Modders/pulsar-mod-loader/releases/latest").Split('\n');
string versionFromInfo = info.First(i => i.Contains("tag_name"))
.Replace(@" ""tag_name"": """, string.Empty)
.Replace(@""",", string.Empty); // for example: returns "0.10.4"

if (version.StartsWith(versionFromInfo))
return CurrentPMLDll;

Logger.Info($"New update available! Download {versionFromInfo}?");
Logger.Info("(Y/N)");

if (Console.ReadLine().ToUpper() == "N")
return CurrentPMLDll;

string downloadLink = info.First(i => i.Contains("https://github.com/PULSAR-Modders/pulsar-mod-loader/releases/download") && i.Contains(".dll"))
.Replace(@" ""browser_download_url"": """, string.Empty).Replace(@"""", string.Empty);
string zipPath = CurrentPMLDll.Replace(".dll", ".zip");
File.WriteAllBytes(zipPath, web.DownloadData(downloadLink));

string newDllPath = useOtherDll ? CurrentPMLDll : CurrentPMLDll.Replace(".dll", "_updated.dll");

using (var zipfile = Pathfinding.Ionic.Zip.ZipFile.Read(zipPath))
{
var dll = zipfile.First(z => z.FileName.EndsWith("PulsarModLoader.dll"));
List<byte> bytes = new List<byte>();
using (var reader = dll.OpenReader())
{
while (reader.Position != reader.Length)
bytes.Add((byte)reader.ReadByte());
}
File.WriteAllBytes(newDllPath, bytes.ToArray());
}

File.Delete(zipPath);

Logger.Info("Successfully updated!");

return newDllPath;
}
}
}
}
3 changes: 3 additions & 0 deletions PulsarInjector/PulsarInjector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
<Reference Include="Mono.Cecil.Rocks, Version=0.11.3.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.11.3\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
</Reference>
<Reference Include="Pathfinding.Ionic.Zip.Reduced">
<HintPath>..\lib\Pathfinding.Ionic.Zip.Reduced.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation" />
Expand Down
23 changes: 23 additions & 0 deletions PulsarModLoader/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace PulsarModLoader
{
public class ModManager
{
public static bool IsOldVersion;

public delegate void ModLoaded(string name, PulsarMod mod);
public delegate void ModUnloaded(PulsarMod mod);
public delegate void AllModsLoaded();
Expand Down Expand Up @@ -52,6 +54,27 @@ public ModManager()

// Force Photon's static constructor to run so patching its methods doesn't fail
RuntimeHelpers.RunClassConstructor(typeof(PhotonNetwork).TypeHandle);

IsOldVersion = false;

#if !DEBUG
try
{
using (var web = new System.Net.WebClient())
{
web.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36");

string[] info = web.DownloadString("https://api.github.com/repos/PULSAR-Modders/pulsar-mod-loader/releases/latest").Split('\n');
string versionFromInfo = info.First(i => i.Contains("tag_name"))
.Replace(@" ""tag_name"": """, string.Empty)
.Replace(@""",", string.Empty);

if (!PMLVersionInfo.FileVersion.StartsWith(versionFromInfo))
IsOldVersion = true;
}
}
catch { }
#endif
}

public PulsarMod GetMod(string name)
Expand Down
2 changes: 1 addition & 1 deletion PulsarModLoader/Patches/GameVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class GameVersion
static void Prefix(ref string Obj3)
{
if (Obj3.Contains("v"))
Obj3 += $"\nPML {PMLVersion}";
Obj3 += $" PML {PMLVersion}{(ModManager.IsOldVersion ? " (OLD)" : string.Empty)}";
}

static void Postfix(string __result) => Version = __result;
Expand Down

0 comments on commit 33dc61a

Please sign in to comment.