add: godot editor patcher

This commit is contained in:
h z
2025-02-21 05:05:44 +00:00
commit 0b37bae704
7 changed files with 236 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
/VersionInfo.props

View File

@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="VersionInfo.props" Condition="Exists('VersionInfo.props')" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>$(PoloniumSdkVersion)</Version>
<SelfContained>true</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<OutputPath>bin/</OutputPath>
<PublishDir>$(OutputPath)publish/</PublishDir>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.6" />
</ItemGroup>
<Target Name="PostPublish" AfterTargets="Publish">
<PropertyGroup>
<AppDir>$(PublishDir)AppDir/</AppDir>
<AppImageToolPath>$(ProjectDir)appimagetool</AppImageToolPath>
<AppName>Polonium.Godot.Patcher</AppName>
</PropertyGroup>
<Exec Command="mkdir -p $(AppDir)usr/bin $(AppDir)usr/lib $(AppDir)usr/share/applications $(AppDir)usr/share/icons/hicolor/256x256/apps" />
<Exec Command="cp $(PublishDir)$(AppName) $(AppDir)usr/bin/" />
<Exec Command="cp $(PublishDir)*.dll $(AppDir)usr/lib/ || true" />
<Exec Command="cp $(PublishDir)*.so $(AppDir)usr/lib/ || true" />
<Exec Command="echo '#!/bin/bash' > $(AppDir)AppRun" />
<Exec Command="echo 'export LD_LIBRARY_PATH=$APPDIR/usr/lib:$LD_LIBRARY_PATH' >> $(AppDir)AppRun" />
<Exec Command="echo 'exec $APPDIR/usr/bin/$(AppName) $@' >> $(AppDir)AppRun" />
<Exec Command="chmod +x $(AppDir)AppRun" />
<Exec Command="echo '[Desktop Entry]' > $(AppDir)usr/share/applications/$(AppName).desktop" />
<Exec Command="echo 'Name=$(AppName)' >> $(AppDir)usr/share/applications/$(AppName).desktop" />
<Exec Command="echo 'Exec=AppRun' >> $(AppDir)usr/share/applications/$(AppName).desktop" />
<Exec Command="echo 'Type=Application' >> $(AppDir)usr/share/applications/$(AppName).desktop" />
<Exec Command="echo 'Categories=Utility;' >> $(AppDir)usr/share/applications/$(AppName).desktop" />
<Exec Command="echo 'Icon=$(AppName)' >> $(AppDir)/usr/share/applications/$(AppName).desktop" />
<Exec Command="cp -f $(AppName).png $(AppDir)" />
<Exec Command="cp $(AppDir)/usr/share/applications/$(AppName).desktop $(AppDir)/" />
<Exec Command="$(AppImageToolPath) $(ProjectDir)$(AppDir) $(ProjectDir)$(PublishDir)$(AppName).AppImage" />
</Target>
</Project>

BIN
Polonium.Godot.Patcher.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@@ -0,0 +1,16 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polonium.Godot.Patcher", "Polonium.Godot.Patcher.csproj", "{85B54825-8E92-4C32-ADCE-5CD713F972D7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{85B54825-8E92-4C32-ADCE-5CD713F972D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85B54825-8E92-4C32-ADCE-5CD713F972D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85B54825-8E92-4C32-ADCE-5CD713F972D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85B54825-8E92-4C32-ADCE-5CD713F972D7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

169
Program.cs Normal file
View File

@@ -0,0 +1,169 @@
using System.Reflection;
using Mono.Cecil;
using Mono.Cecil.Cil;
public class Program
{
public static void Main(string[] args)
{
string dllPath = ".";
if (args.Length != 0)
{
Console.WriteLine(args[0]);
dllPath = args[0];
}
if (dllPath.EndsWith("/"))
dllPath = $"{dllPath}GodotTools.ProjectEditor.dll";
else if (!dllPath.EndsWith("GodotTools.ProjectEditor.dll"))
dllPath = $"{dllPath}/GodotTools.ProjectEditor.dll";
if (!File.Exists(dllPath))
{
Console.WriteLine(dllPath);
Console.WriteLine("GodotTools.ProjectEditor.dll not found");
return;
}
string backupPath = $"{dllPath}.bak";
if (!File.Exists(backupPath))
File.Copy(dllPath, backupPath);
string? version = Assembly.GetEntryAssembly()?
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
.InformationalVersion;
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(dllPath, new ReaderParameters { });
TypeDefinition targetType = assembly.MainModule.Types.First(t => t.Name == "ProjectUtils");
MethodDefinition method = targetType.Methods.First(m => m.Name == "EnsureGodotSdkIsUpToDate");
TypeReference? stringType = assembly.MainModule.ImportReference(typeof(string));
VariableDefinition? root = method.Body.Variables.FirstOrDefault(t => t.VariableType.FullName == "Microsoft.Build.Construction.ProjectRootElement");
TypeDefinition msBuildProjectTypeDef = assembly.MainModule.GetType("GodotTools.ProjectEditor.MSBuildProject");
TypeDefinition projectGeneratorTypeDef = assembly.MainModule.GetType("GodotTools.ProjectEditor.ProjectGenerator");
MethodDefinition? msBuildProjectGetRoot = msBuildProjectTypeDef.Methods
.FirstOrDefault(m => m.Name == "get_Root");
MethodDefinition? projectGeneratorGetGodotSdk = projectGeneratorTypeDef.Methods
.FirstOrDefault(m => m.Name == "get_GodotSdkAttrValue");
MethodDefinition? msBuildProjectSetHasUnsavedChanges = msBuildProjectTypeDef.Methods
.FirstOrDefault(m => m.Name == "set_HasUnsavedChanges");
MethodReference? preGetSdk = method.Body.Instructions
.Select(i => i.Operand)
.OfType<MethodReference>()
.FirstOrDefault(m => m.Name == "get_Sdk");
MethodReference? preSetSdk = method.Body.Instructions
.Select(i => i.Operand)
.OfType<MethodReference>()
.FirstOrDefault(m => m.Name == "set_Sdk");
foreach (Instruction instruction in method.Body.Instructions)
{
}
method.Body.Instructions.Clear();
method.Body.ExceptionHandlers.Clear();
method.Body.Variables.Clear();
ILProcessor il = method.Body.GetILProcessor();
//VariableDefinition root = new VariableDefinition(projectRootElementTypeDef);
method.Body.Variables.Add(root);
VariableDefinition godotSdk = new VariableDefinition(stringType);
method.Body.Variables.Add(godotSdk);
method.Body.InitLocals = true;
il.Append(il.Create(OpCodes.Ldarg_0));
il.Append(il.Create(OpCodes.Callvirt, msBuildProjectGetRoot));
il.Append(il.Create(OpCodes.Stloc_0));
il.Append(il.Create(OpCodes.Call, projectGeneratorGetGodotSdk));
il.Append(il.Create(OpCodes.Stloc_1));
il.Append(il.Create(OpCodes.Ldloc_0));
il.Append(il.Create(OpCodes.Callvirt, preGetSdk));
il.Append(il.Create(OpCodes.Call, method.Module
.ImportReference(
typeof(string).GetMethod("IsNullOrEmpty", [typeof(string)]))
)
);
Instruction setSdk = il.Create(OpCodes.Nop);
il.Append(il.Create(OpCodes.Brtrue_S, setSdk));
il.Append(il.Create(OpCodes.Ldloc_0));
il.Append(il.Create(OpCodes.Callvirt, preGetSdk));
il.Append(il.Create(OpCodes.Callvirt, method.Module
.ImportReference(typeof(string).GetMethod("Trim", Type.EmptyTypes)))
);
il.Append(il.Create(OpCodes.Ldloc_1));
il.Append(il.Create(OpCodes.Ldc_I4_5));
il.Append(il.Create(OpCodes.Callvirt, method.Module
.ImportReference(
typeof(string).GetMethod("Equals", [typeof(string), typeof(StringComparison)]))
)
);
Instruction notMatch = il.Create(OpCodes.Nop);
il.Append(il.Create(OpCodes.Brfalse_S, notMatch));
il.Append(il.Create(OpCodes.Ret));
il.Append(notMatch);
il.Append(il.Create(OpCodes.Ldloc_0));
il.Append(il.Create(OpCodes.Callvirt, preGetSdk));
il.Append(il.Create(OpCodes.Callvirt, method.Module
.ImportReference(typeof(string).GetMethod("Trim", Type.EmptyTypes)))
);
il.Append(il.Create(OpCodes.Ldstr, $"Polonium.Sdk/{version}"));
il.Append(il.Create(OpCodes.Ldc_I4_5));
il.Append(il.Create(OpCodes.Callvirt, method.Module
.ImportReference(
typeof(string).GetMethod("Equals", [typeof(string), typeof(StringComparison)]))
)
);
il.Append(il.Create(OpCodes.Brfalse_S, setSdk));
il.Append(il.Create(OpCodes.Ret));
il.Append(setSdk);
il.Append(il.Create(OpCodes.Ldloc_0));
il.Append(il.Create(OpCodes.Ldloc_1));
il.Append(il.Create(OpCodes.Callvirt, preSetSdk));
il.Append(il.Create(OpCodes.Ldarg_0));
il.Append(il.Create(OpCodes.Ldc_I4_1));
il.Append(il.Create(OpCodes.Callvirt, msBuildProjectSetHasUnsavedChanges));
il.Append(il.Create(OpCodes.Ret));
assembly.Write($"{dllPath}.2.dll");
File.Replace($"{dllPath}.2.dll", dllPath, null);
}
private static string GetAppImagePath()
{
try
{
string exePath = "/proc/self/exe";
if (File.Exists(exePath))
{
return Path.GetFullPath(new FileInfo(exePath).FullName);
}
}
catch (Exception ex)
{
return "";
}
return "";
}
}

BIN
appimagetool Executable file

Binary file not shown.

2
publish Normal file
View File

@@ -0,0 +1,2 @@
#!/bin/bash
dotnet publish -c Release -r linux-x64