This commit is contained in:
bmgjet 2024-03-08 20:33:05 +13:00
commit 6ec193961b
9 changed files with 306 additions and 0 deletions

Binary file not shown.

25
HarmonyUpgrader.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34112.27
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HarmonyUpgrader", "HarmonyUpgrader\HarmonyUpgrader.csproj", "{C959B646-1189-428B-9AEA-6DAD8DF98FF0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C959B646-1189-428B-9AEA-6DAD8DF98FF0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C959B646-1189-428B-9AEA-6DAD8DF98FF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C959B646-1189-428B-9AEA-6DAD8DF98FF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C959B646-1189-428B-9AEA-6DAD8DF98FF0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1552512E-ECA2-42AC-BE3E-24C5F56A0AF2}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C959B646-1189-428B-9AEA-6DAD8DF98FF0}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>HarmonyUpgrader</RootNamespace>
<AssemblyName>HarmonyUpgrader</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Cecil">
<HintPath>..\..\..\..\..\..\marchstagingbranch\RustDedicated_Data\Managed\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

183
HarmonyUpgrader/Program.cs Normal file
View File

@ -0,0 +1,183 @@
using Mono.Cecil;
using System;
using System.IO;
namespace HarmonyUpgrader
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(string.Concat(new string[]
{
" .-'''-. ",
System.Environment.NewLine,
" ' _ \\ .-''-. ..-'''-. ",
System.Environment.NewLine,
" . __ __ ___ / /` '. \\ _..._ .' .-. ) \\.-'''\\ \\ ",
System.Environment.NewLine,
" .'| | |/ `.' `. . | \\ ' .' '..-. .- / .' / / | | ",
System.Environment.NewLine,
"< | .-,.--. | .-. .-. '| ' | '. .-. .\\ \\ / /(_/ / / __/ / ",
System.Environment.NewLine,
" | | __ | .-. || | | | | |\\ \\ / / | ' ' | \\ \\ / / / / |_ '. ",
System.Environment.NewLine,
" | | .'''-. .:--.'. | | | || | | | | | `. ` ..' / | | | | \\ \\ / / / / `. \\ ",
System.Environment.NewLine,
" | |/.'''. \\ / | \\ | | | | || | | | | | '-...-'` | | | | \\ \\ / / . ' \\ '. ",
System.Environment.NewLine,
" | / | | `\" __ | | | | '- | | | | | | | | | | \\ ` / / / _.-'),.--. , | ",
System.Environment.NewLine,
" | | | | .'.''| | | | |__| |__| |__| | | | | \\ / .' ' _.'.-''// \\ | | ",
System.Environment.NewLine,
" | | | | / / | |_| | | | | | / / / /.-'_.' \\ / / ,' ",
System.Environment.NewLine,
" | '. | '.\\ \\._,\\ '/|_| | | | | |`-' / / _.' `'--'-....--' / ",
System.Environment.NewLine,
" '---' '---'`--' `\" '--' '--' '..' ( _.-' `.. __..-' ",
System.Environment.NewLine,
System.Environment.NewLine,
"Upgrader by bmgjet",
}));
if (args.Length <= 0)
{
Console.WriteLine("File not found, Drop .cs or .dll on this exe");
Console.ReadKey();
return;
}
string InputFile = args[0];
Console.WriteLine("Opening " + InputFile);
if (File.Exists(InputFile))
{
if (InputFile.EndsWith(".dll"))
{
CheckHarmonyDlls(new FileInfo(InputFile));
Console.WriteLine("Dll Upgraded");
Console.ReadKey();
return;
}
else if (InputFile.EndsWith(".cs"))
{
CheckOxidePlugins(new FileInfo(InputFile));
Console.WriteLine(".cs Upgraded");
Console.ReadKey();
return;
}
else
{
Console.WriteLine("Must Be .cs or .dll file");
Console.ReadKey();
return;
}
}
Console.WriteLine("File not found, Drop .cs or .dll on this exe");
Console.ReadKey();
}
#region Functions
private static void CheckHarmonyDlls(FileInfo file)
{
if (file == null) { return; }
ModuleDefinition asm = ModuleDefinition.ReadModule(file.FullName); //Read dll assembly
try
{
//Check version
AssemblyDefinition a = asm.Assembly;
for (int i = a.MainModule.AssemblyReferences.Count - 1; i >= 0; i--)
{
if (a.MainModule.AssemblyReferences[i].Name == "0Harmony" && a.MainModule.AssemblyReferences[i].Version.ToString() == "1.2.0.1")
{
//Change version number
a.MainModule.AssemblyReferences[i].Version = new Version(2, 3, 0, 0);
Console.WriteLine("Found Harmony V1.2 dll " + file.Name + " Creating Back Up As " + file.Name.Replace(".dll", ".V1_2") + " And Upgrading.");
//Update Referece
foreach (var MetaData in asm.GetMemberReferences())
{
if (MetaData.FullName.Contains("HarmonyPatch") || MetaData.FullName.Contains("HarmonyPrefix") || MetaData.FullName.Contains("HarmonyPostfix") || MetaData.FullName.Contains("HarmonyTranspiler"))
{
MetaData.FullName.Replace("Harmony.", "HarmonyLib.");
}
}
//Update Name Spaces
foreach (var moduleDefinition in a.Modules)
{
foreach (var typeDefinition in moduleDefinition.Types)
{
if (typeDefinition.Namespace == "Harmony") { typeDefinition.Namespace = "HarmonyLib"; }
}
foreach (var TypeReferences in moduleDefinition.GetTypeReferences())
{
if (TypeReferences.Namespace == "Harmony") { TypeReferences.Namespace = "HarmonyLib"; }
}
}
asm.Write(file.FullName.Replace(".dll", "modded.dll")); //Save as modded version
asm.Dispose(); //Close opened dll
File.Move(file.FullName, file.FullName.Replace(".dll", ".V1_2")); //Create back up of orignal dll
File.Move(file.FullName.Replace(".dll", "modded.dll"), file.FullName); //Rename modded dll to orignals name
break;
}
}
}
catch
{
//Something went wrong
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Failed To Process: " + file.FullName);
Console.ForegroundColor = ConsoleColor.Magenta;
}
if (asm != null) { asm.Dispose(); } //Close dll
}
private static void CheckOxidePlugins(FileInfo file)
{
if (file == null) { return; }
bool Upgraded = false;
string[] PluginSRC = File.ReadAllLines(file.FullName); //Read all plugin lines
for (int i = PluginSRC.Length - 1; i >= 0; i--) //Check each line and upgrade any harmony init code
{
if (PluginSRC[i] == "using Harmony;")
{
Upgraded = true;
PluginSRC[i] = "using HarmonyLib;";
}
if (PluginSRC[i].Contains("HarmonyInstance"))
{
Upgraded = true;
PluginSRC[i] = PluginSRC[i].Replace("HarmonyInstance", "Harmony");
}
if (PluginSRC[i].Contains("HarmonyInstance.Create"))
{
Upgraded = true;
PluginSRC[i] = PluginSRC[i].Replace("HarmonyInstance.Create", "new HarmonyLib.Harmony");
string[] getname = PluginSRC[i].Split('=');
if (getname.Length == 2)
{
PluginSRC[i] += System.Environment.NewLine + getname[0].TrimEnd() + ".PatchAll();";
}
}
if (PluginSRC[i].Contains("Harmony.Create"))
{
Upgraded = true;
PluginSRC[i] = PluginSRC[i].Replace("Harmony.Create", "new HarmonyLib.Harmony");
string[] getname = PluginSRC[i].Split('=');
if (getname.Length == 2)
{
PluginSRC[i] += System.Environment.NewLine + getname[0].TrimEnd() + ".PatchAll();";
}
}
if (PluginSRC[i].Contains("PatchProcessor"))
{
Upgraded = true;
PluginSRC[i] = "";
}
}
if (Upgraded)
{
Console.WriteLine("Found Oxide Harmony V1.2 plugin " + file.Name + " Saving Backup And Creating Patched Version.");
File.Copy(file.FullName, file.FullName.Replace(".cs", ".old"));
File.WriteAllLines(file.FullName, PluginSRC);
}
}
#endregion
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HarmonyUpgrader")]
[assembly: AssemblyDescription("Upgrades Plugins")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("bmgjet")]
[assembly: AssemblyProduct("HarmonyUpgrader")]
[assembly: AssemblyCopyright("Copyright © bmgjet 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c959b646-1189-428b-9aea-6dad8df98ff0")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Binary file not shown.

Binary file not shown.