Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Update remove crack patch option
Browse files Browse the repository at this point in the history
  • Loading branch information
tylearymf committed Sep 18, 2022
1 parent 358448f commit cd99e7e
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 25 deletions.
12 changes: 12 additions & 0 deletions Assets/Language_Chinese.resx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ https://www.github.com/tylearymf/UniHacker</value>
<data name="Process_occupy" xml:space="preserve">
<value>{0} 正在运行中,可能会破解失败!</value>
</data>
<data name="Revert_btn" xml:space="preserve">
<value>还 原</value>
</data>
<data name="Revert_Desc" xml:space="preserve">
<value>是否移除破解补丁?</value>
</data>
<data name="Revert_fail" xml:space="preserve">
<value>移除失败!</value>
</data>
<data name="Revert_success" xml:space="preserve">
<value>移除成功!</value>
</data>
<data name="Select" xml:space="preserve">
<value>选择</value>
</data>
Expand Down
12 changes: 12 additions & 0 deletions Assets/Language_English.resx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ https://www.github.com/tylearymf/UniHacker</value>
<data name="Process_occupy" xml:space="preserve">
<value>{0} is running and the patch may fail!</value>
</data>
<data name="Revert_btn" xml:space="preserve">
<value>Revert</value>
</data>
<data name="Revert_Desc" xml:space="preserve">
<value>Do you need to remove the crack patch?</value>
</data>
<data name="Revert_fail" xml:space="preserve">
<value>Remove failed!</value>
</data>
<data name="Revert_success" xml:space="preserve">
<value>Successfully removed!</value>
</data>
<data name="Select" xml:space="preserve">
<value>Select</value>
</data>
Expand Down
43 changes: 36 additions & 7 deletions Patcher/Hub/UnityHubPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public UnityHubPatcher(string filePath) : base(filePath)
{
PatchStatus = PatchStatus.Support;
}

var unityHubPath = RootPath;
unityHubPath = Path.Combine(unityHubPath, "resources");
var exportFolder = Path.Combine(unityHubPath, "app");
var asarBackupPath = Path.Combine(unityHubPath, "app.asar.bak");
if (Directory.Exists(exportFolder) || File.Exists(asarBackupPath))
PatchStatus = PatchStatus.Patched;
}

public async override Task<(bool success, string errorMsg)> ApplyPatch(Action<double> progress)
Expand Down Expand Up @@ -81,10 +88,9 @@ public UnityHubPatcher(string filePath) : base(filePath)

if (File.Exists(asarPath) && !File.Exists(asarBackupPath))
File.Move(asarPath, asarBackupPath);

if (PlatformUtils.IsOSX())
{
await PlatformUtils.MacOSRemoveQuarantine(Directory.GetParent(RootPath)!.FullName);
}
}
else
{
Expand All @@ -94,6 +100,31 @@ public UnityHubPatcher(string filePath) : base(filePath)
return (patchResult, string.Empty);
}

public async override Task<(bool success, string errorMsg)> RemovePatch(Action<double> progress)
{
var unityHubPath = RootPath;
unityHubPath = Path.Combine(unityHubPath, "resources");
var exportFolder = Path.Combine(unityHubPath, "app");
var asarPath = Path.Combine(unityHubPath, "app.asar");
var asarBackupPath = Path.Combine(unityHubPath, "app.asar.bak");

progress(0.2F);
await Task.Delay(200);

if (Directory.Exists(exportFolder))
Directory.Delete(exportFolder, true);

progress(0.7F);
await Task.Delay(200);

if (!File.Exists(asarPath) && File.Exists(asarBackupPath))
File.Move(asarBackupPath, asarPath);

progress(1F);
await Task.Delay(200);

return (true, string.Empty);
}

public static void ReplaceMethodBody(ref string scriptContent, string methodName, string newMethodContent)
{
Expand All @@ -116,9 +147,6 @@ public static bool CopyDirectory(string sourcePath, string destinationPath, bool
var ret = true;
try
{
sourcePath = sourcePath.EndsWith(@"\") ? sourcePath : sourcePath + @"\";
destinationPath = destinationPath.EndsWith(@"\") ? destinationPath : destinationPath + @"\";

if (Directory.Exists(sourcePath))
{
if (!Directory.Exists(destinationPath))
Expand All @@ -127,12 +155,13 @@ public static bool CopyDirectory(string sourcePath, string destinationPath, bool
foreach (var filePath in Directory.GetFiles(sourcePath))
{
var file = new FileInfo(filePath);
file.CopyTo(destinationPath + file.Name, overwrite);
file.CopyTo(Path.Combine(destinationPath, file.Name), overwrite);
}

foreach (var directoryPath in Directory.GetDirectories(sourcePath))
{
var directory = new DirectoryInfo(directoryPath);
if (!CopyDirectory(directoryPath, destinationPath + directory.Name, overwrite))
if (!CopyDirectory(directoryPath, Path.Combine(destinationPath, directory.Name), overwrite))
ret = false;
}
}
Expand Down
1 change: 0 additions & 1 deletion Patcher/Misc/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class Language
{
public const string English = "English";
public const string Chinese = "Chinese";
public const string DefaultLanguage = English;

static ResourceManager? s_ResourceManager;

Expand Down
2 changes: 1 addition & 1 deletion Patcher/Misc/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Patcher(string filePath)
throw new NotImplementedException();
}

public virtual async Task<(bool success, string errorMsg)> RemovePatch()
public virtual async Task<(bool success, string errorMsg)> RemovePatch(Action<double> progress)
{
await Task.Yield();
throw new NotImplementedException();
Expand Down
21 changes: 17 additions & 4 deletions Patcher/Unity/LicensingInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ internal class LicensingInfo
internal static int s_MajorVersion;
internal static int s_MinorVersion;

public static void TryGenerate(int majorVersion, int minorVersion)
static string GetLicensePath(int majorVersion, int minorVersion)
{
s_MajorVersion = majorVersion;
s_MinorVersion = minorVersion;

var commonAppData = string.Empty;
switch (PlatformUtils.GetPlatformType())
{
Expand Down Expand Up @@ -57,6 +54,15 @@ public static void TryGenerate(int majorVersion, int minorVersion)
else
ulfFilePath = Path.Combine(unityLicensingPath, "Unity_lic.ulf");

return ulfFilePath;
}

public static void TryGenerate(int majorVersion, int minorVersion)
{
s_MajorVersion = majorVersion;
s_MinorVersion = minorVersion;

var ulfFilePath = GetLicensePath(majorVersion, minorVersion);
if (File.Exists(ulfFilePath))
File.Delete(ulfFilePath);

Expand Down Expand Up @@ -109,6 +115,13 @@ public static void TryGenerate(int majorVersion, int minorVersion)
contents = contents.Replace("<MachineBindings/>", "<MachineBindings></MachineBindings>");
File.WriteAllText(ulfFilePath, contents);
}

public static void TryRemove(int majorVersion, int minorVersion)
{
var ulfFilePath = GetLicensePath(majorVersion, minorVersion);
if (File.Exists(ulfFilePath))
File.Delete(ulfFilePath);
}
}

[XmlRoot("root", Namespace = "", IsNullable = false)]
Expand Down
33 changes: 32 additions & 1 deletion Patcher/Unity/UnityPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public UnityPatcher(string filePath) : base(filePath)
for (int j = 0; j < bytes.Length; j++)
{
if (bytes[j].HasValue)
fileBytes[patchIndexes![i] + j] = bytes[j].Value;
fileBytes[patchIndexes![i] + j] = bytes[j]!.Value;
}
}

Expand Down Expand Up @@ -124,6 +124,37 @@ public UnityPatcher(string filePath) : base(filePath)

return (true, string.Empty);
}

public async override Task<(bool success, string errorMsg)> RemovePatch(Action<double> progress)
{
if (patchInfo == null || patchIndexes?.Count == 0)
return (false, string.Empty);

progress(0.2F);
await Task.Delay(200);

var fileBakPath = FilePath + ".bak";
if (File.Exists(fileBakPath))
File.Move(fileBakPath, FilePath, true);

progress(0.5F);
await Task.Delay(200);

var licensingFilePath = Path.Combine(RootPath, "Data/Resources/Licensing/Client/Unity.Licensing.Client" + PlatformUtils.GetExtension());
var licensingFileBakPath = licensingFilePath + ".bak";
if (File.Exists(licensingFileBakPath))
File.Move(licensingFileBakPath, licensingFilePath);

progress(0.7F);
await Task.Delay(200);

LicensingInfo.TryRemove(MajorVersion, MinorVersion);

progress(1F);
await Task.Delay(200);

return (true, string.Empty);
}
}
#pragma warning restore CS8618
}
2 changes: 1 addition & 1 deletion UniHacker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<Authors>tylearymf</Authors>
<Company>tylearymf</Company>
<Version>3.6</Version>
<Version>3.7</Version>
<PackageId>com.tylearymf.unihacker</PackageId>
<PackageProjectUrl>https:/www.github.com/tylearymf/unihacker</PackageProjectUrl>
<ApplicationIcon>Assets\avalonia-logo.ico</ApplicationIcon>
Expand Down
1 change: 1 addition & 0 deletions ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class MainWindowViewModel : ViewModelBase
public string Version => Language.GetString(nameof(Version));
public string Status => Language.GetString(nameof(Status));
public string Patch_btn => Language.GetString(nameof(Patch_btn));
public string Revert_btn => Language.GetString(nameof(Revert_btn));
}
}
24 changes: 16 additions & 8 deletions Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:vm="using:UniHacker.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Width="520" Height="250" MaxWidth="520" MaxHeight="250" CanResize="False"
mc:Ignorable="d" Width="520" Height="270" MaxWidth="520" MaxHeight="270" CanResize="False"
x:Class="UniHacker.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico">

Expand All @@ -24,25 +24,25 @@
HorizontalAlignment="Stretch"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
Margin="350,-85,0,50"/>
<TextBlock Text="{Binding Version}" VerticalAlignment="Top" Margin="0,70"/>
Margin="350,-95,0,60"/>
<TextBlock Text="{Binding Version}" VerticalAlignment="Top" Margin="0,75"/>
<TextBox Name="Version"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="70,65,70,0"
Margin="70,70,70,0"
Width="145"
IsReadOnly="True"/>
<TextBlock Text="{Binding Status}" VerticalAlignment="Top" Margin="170,70" HorizontalAlignment="Right" />
<TextBlock Text="{Binding Status}" VerticalAlignment="Top" Margin="170,75" HorizontalAlignment="Right" />
<TextBox Name="Status"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="350,65,0,0"
Margin="350,70,0,0"
Width="145"
IsReadOnly="True"/>
<TextBox Name="RichText"
Text="{Binding Description}"
VerticalAlignment="Bottom"
Margin="0,-125,150,0"
Margin="0,-135,150,0"
TextWrapping="Wrap"
IsReadOnly="True"/>
<Button Name="PatchBtn"
Expand All @@ -52,7 +52,15 @@
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
FontSize="30"
Margin="350,100,0,0" />
Margin="350,110,0,30" />
<Button Name="RevertBtn"
Content="{Binding Revert_btn}"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
FontSize="12"
Margin="350,220,0,0" />
<ProgressBar Name="ProgressBar"
VerticalAlignment="Bottom"
Margin="-10,10,-10,-10"/>
Expand Down
38 changes: 36 additions & 2 deletions Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
using System.Diagnostics;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using MessageBox.Avalonia.Enums;

namespace UniHacker.Views
{
public partial class MainWindow : Window
{
public static MainWindow? Instance { private set; get; }

string? filePath;
Patcher? patcher;

public MainWindow()
Expand Down Expand Up @@ -46,17 +48,20 @@ void InitView()

SelectBtn.Click += SelectBtn_Click;
PatchBtn.Click += PatchBtn_Click;
RevertBtn.Click += RevertBtn_Click;
}

void UpdateFilePath(string filePath)
{
this.filePath = filePath;
patcher = PatchManager.GetPatcher(filePath, PlatformUtils.GetPlatformType());
var status = patcher?.PatchStatus ?? PatchStatus.Unknown;

FilePath.Text = filePath;
Version.Text = patcher?.FileVersion ?? string.Empty;
Status.Text = Language.GetString(status.ToString());
PatchBtn.IsEnabled = status == PatchStatus.Support;
RevertBtn.IsEnabled = status == PatchStatus.Patched;

if (patcher != null)
{
Expand All @@ -77,20 +82,49 @@ async void PatchBtn_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs
if (patcher == null)
return;

PatchBtn.IsEnabled = false;

try
{
PatchBtn.IsEnabled = false;

(bool success, string errorMsg) = await patcher.ApplyPatch(progress => ProgressBar.Value = (int)(progress * 100));
var msg = Language.GetString(success ? "Patch_success" : "Patch_fail");
_ = await MessageBox.Show(string.Format("{0}\n\n{1}", msg, errorMsg));

UpdateFilePath(filePath!);
}
catch (Exception ex)
{
PatchBtn.IsEnabled = true;
_ = await MessageBox.Show(ex.ToString());
}
}

async void RevertBtn_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
if (patcher == null)
return;

var result = await MessageBox.Show(Language.GetString("Revert_Desc"), ButtonEnum.YesNo);
if (result == ButtonResult.Yes)
{
try
{
RevertBtn.IsEnabled = false;

(bool success, string errorMsg) = await patcher.RemovePatch(progress => ProgressBar.Value = (int)(progress * 100));
var msg = Language.GetString(success ? "Revert_success" : "Revert_fail");
_ = await MessageBox.Show(string.Format("{0}\n\n{1}", msg, errorMsg));

UpdateFilePath(filePath!);
}
catch (Exception ex)
{
RevertBtn.IsEnabled = true;
_ = await MessageBox.Show(ex.ToString());
}
}
}

async void SelectBtn_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var dialog = new OpenFileDialog();
Expand Down

0 comments on commit cd99e7e

Please sign in to comment.