Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small opts #236

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions BenchmarkTest/BenchmarkTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UndertaleModTool\UndertaleModLib\UndertaleModLib.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions BenchmarkTest/DecompileBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using BenchmarkDotNet.Attributes;
using UndertaleModLib;
using UndertaleModLib.Decompiler;
using UndertaleModLib.Models;

namespace BenchmarkTest;

[MemoryDiagnoser]
public class DecompileBenchmark
{
public UndertaleData data;
public GlobalDecompileContext decompileContext;

[GlobalSetup]
public void GlobalSetup()
{
using (FileStream fs = new FileInfo(@"/home/narr/Dokumente/am2r 1.5.5/assets/game.unx_older").OpenRead())
{
data = UndertaleIO.Read(fs);
}
decompileContext = new GlobalDecompileContext(data, false);
}

[Benchmark]
public void AppendVariableToEveryEntry()
{
void AppendGML(UndertaleCode code, string appendedText)
{
var codeText = Decompiler.Decompile(code, decompileContext);
codeText = codeText + appendedText + "\n";
code.ReplaceGML(codeText, data);
}

foreach (UndertaleCode codeentry in data.Code.SkipLast((int)(data.Code.Count * 0.99)))
{
if (codeentry.Name.Content == "gml_Object_oRm_a5c11lock_Collision_267") continue;

AppendGML(codeentry, "var this_variable_is_not_used_and_thus_completely_uselesss = 1; if (!this_variable_is_not_used_and_thus_completely_uselesss) this_variable_is_not_used_and_thus_completely_uselesss = 1");
}
}
}
7 changes: 7 additions & 0 deletions BenchmarkTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// See https://aka.ms/new-console-template for more information

using BenchmarkDotNet.Running;
using BenchmarkTest;

Console.WriteLine("Hello, World!");
BenchmarkRunner.Run<DecompileBenchmark>();
51 changes: 44 additions & 7 deletions YAMS-LIB/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ public static string GetGMLCode(this UndertaleCode code)

public static void ReplaceGMLInCode(this UndertaleCode code, string textToReplace, string replacementText, bool ignoreErrors = false)
{
var codeText = Decompiler.Decompile(code, decompileContext);
string codeText;

if (Patcher.CodeCache.TryGetValue(code, out string? value))
{
codeText = value;
}
else
{
codeText = Decompiler.Decompile(code, decompileContext);
Patcher.CodeCache.Add(code, codeText);
}

if (!codeText.Contains(textToReplace))
{
if (ignoreErrors)
Expand All @@ -34,26 +45,52 @@ public static void ReplaceGMLInCode(this UndertaleCode code, string textToReplac
throw new ApplicationException($"The text \"{textToReplace}\" was not found in \"{code.Name.Content}\"!");
}
codeText = codeText.Replace(textToReplace, replacementText);
code.ReplaceGML(codeText, gmData);
Patcher.CodeCache[code] = codeText;
}

public static void PrependGMLInCode(this UndertaleCode code, string prependedText)
{
var codeText = Decompiler.Decompile(code, decompileContext);
string codeText;
if (Patcher.CodeCache.TryGetValue(code, out string? value))
{
codeText = value;
}
else
{
codeText = Decompiler.Decompile(code, decompileContext);
Patcher.CodeCache.Add(code, codeText);
}
codeText = prependedText + "\n" + codeText;
code.ReplaceGML(codeText, gmData);
Patcher.CodeCache[code] = codeText;
}

public static void AppendGMLInCode(this UndertaleCode code, string appendedText)
{
var codeText = Decompiler.Decompile(code, decompileContext);
string codeText;
if (Patcher.CodeCache.TryGetValue(code, out string? value))
{
codeText = value;
}
else
{
codeText = Decompiler.Decompile(code, decompileContext);
Patcher.CodeCache.Add(code, codeText);
}
codeText = codeText + appendedText + "\n";
code.ReplaceGML(codeText, gmData);
Patcher.CodeCache[code] = codeText;
}

public static void SubstituteGMLCode(this UndertaleCode code, string newGMLCode)
{
code.ReplaceGML(newGMLCode, gmData);
Patcher.CodeCache[code] = newGMLCode;
}

public static void FlushCode()
{
foreach ((var codeName, var codeText) in Patcher.CodeCache)
{
codeName.ReplaceGML(codeText, gmData);
}
}

public static UndertaleRoom.Tile CreateRoomTile(int x, int y, int depth, UndertaleBackground tileset, uint sourceX, uint sourceY, uint width = 16, uint height = 16, uint? id = null)
Expand Down
18 changes: 16 additions & 2 deletions YAMS-LIB/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class Patcher
internal static GlobalDecompileContext? decompileContext;
internal static bool isHorde = false;

internal static Dictionary<UndertaleCode, string> CodeCache = new Dictionary<UndertaleCode, string>(1024);

private static string CreateVersionString()
{
Assembly assembly = Assembly.GetExecutingAssembly();
Expand Down Expand Up @@ -49,13 +51,17 @@ public static void Main(string am2rPath, string outputAm2rPath, string jsonPath)

SeedObject? seedObject = JsonSerializer.Deserialize<SeedObject>(File.ReadAllText(jsonPath));

// Read 1.5.x data
gmData = new UndertaleData();
Stopwatch sw = new Stopwatch();
sw.Start();

// Read 1.5.x data
using (FileStream fs = new FileInfo(am2rPath).OpenRead())
{
gmData = UndertaleIO.Read(fs);
}
sw.Stop();
var afterRead = sw.Elapsed;
sw.Start();

Console.WriteLine("Read data file.");
decompileContext = new GlobalDecompileContext(gmData, false);
Expand Down Expand Up @@ -628,10 +634,18 @@ public static void Main(string am2rPath, string outputAm2rPath, string jsonPath)
Multiworld.Apply(gmData, decompileContext, seedObject);
AddBossMWTracking.Apply(gmData, decompileContext, seedObject);


// Write back to disk
ExtensionMethods.FlushCode();
sw.Stop();
var beforeWrite = sw.Elapsed;
sw.Start();
using (FileStream fs = new FileInfo(outputAm2rPath).OpenWrite())
{
UndertaleIO.Write(fs, gmData, Console.WriteLine);
}
sw.Stop();
Console.WriteLine($"Total Time: {sw.Elapsed}");
Console.WriteLine($"Patching Time Only: {beforeWrite-afterRead}");
}
}
56 changes: 28 additions & 28 deletions YAMS-LIB/patches/DecoupleItemsFromLocations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
gmData.Code.ByName(code).ReplaceGMLInCode("global.item[0]", "global.hasBombs");
}

UndertaleGameObject? elderSeptogg = gmData.GameObjects.ByName("oElderSeptogg");
const string elderSeptoggName = "oElderSeptogg";
foreach (var gameObject in new[]
{
gmData.Rooms.ByName("rm_a0h11").GameObjects.First(go => go.X == 480 && go.Y == 768 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a0h25").GameObjects.First(go => go.X == 120 && go.Y == 816 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a0h25").GameObjects.First(go => go.X == 168 && go.Y == 256 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a0h29").GameObjects.First(go => go.X == 384 && go.Y == 312 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a1h05").GameObjects.First(go => go.X == 1184 && go.Y == 832 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a3h04").GameObjects.First(go => go.X == 528 && go.Y == 1344 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a3h04").GameObjects.First(go => go.X == 1728 && go.Y == 1248 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a3a07").GameObjects.First(go => go.X == 112 && go.Y == 240 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a3b02").GameObjects.First(go => go.X == 192 && go.Y == 896 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a3b08").GameObjects.First(go => go.X == 224 && go.Y == 352 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a0h17").GameObjects.First(go => go.X == 96 && go.Y == 352 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a4b02a").GameObjects.First(go => go.X == 120 && go.Y == 816 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a4b10").GameObjects.First(go => go.X == 144 && go.Y == 624 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a4b10").GameObjects.First(go => go.X == 512 && go.Y == 256 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a4b11").GameObjects.First(go => go.X == 224 && go.Y == 2288 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a5c13").GameObjects.First(go => go.X == 96 && go.Y == 704 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a5c14").GameObjects.First(go => go.X == 1056 && go.Y == 288 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a5c17").GameObjects.First(go => go.X == 192 && go.Y == 288 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a5c18").GameObjects.First(go => go.X == 128 && go.Y == 192 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a5c18").GameObjects.First(go => go.X == 480 && go.Y == 192 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a5c21").GameObjects.First(go => go.X == 160 && go.Y == 384 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a5c21").GameObjects.First(go => go.X == 96 && go.Y == 560 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content),
gmData.Rooms.ByName("rm_a0h11").GameObjects.First(go => go.X == 480 && go.Y == 768 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a0h25").GameObjects.First(go => go.X == 120 && go.Y == 816 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a0h25").GameObjects.First(go => go.X == 168 && go.Y == 256 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a0h29").GameObjects.First(go => go.X == 384 && go.Y == 312 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a1h05").GameObjects.First(go => go.X == 1184 && go.Y == 832 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a3h04").GameObjects.First(go => go.X == 528 && go.Y == 1344 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a3h04").GameObjects.First(go => go.X == 1728 && go.Y == 1248 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a3a07").GameObjects.First(go => go.X == 112 && go.Y == 240 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a3b02").GameObjects.First(go => go.X == 192 && go.Y == 896 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a3b08").GameObjects.First(go => go.X == 224 && go.Y == 352 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a0h17").GameObjects.First(go => go.X == 96 && go.Y == 352 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a4b02a").GameObjects.First(go => go.X == 120 && go.Y == 816 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a4b10").GameObjects.First(go => go.X == 144 && go.Y == 624 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a4b10").GameObjects.First(go => go.X == 512 && go.Y == 256 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a4b11").GameObjects.First(go => go.X == 224 && go.Y == 2288 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a5c13").GameObjects.First(go => go.X == 96 && go.Y == 704 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a5c14").GameObjects.First(go => go.X == 1056 && go.Y == 288 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a5c17").GameObjects.First(go => go.X == 192 && go.Y == 288 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a5c18").GameObjects.First(go => go.X == 128 && go.Y == 192 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a5c18").GameObjects.First(go => go.X == 480 && go.Y == 192 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a5c21").GameObjects.First(go => go.X == 160 && go.Y == 384 && go.ObjectDefinition.Name.Content == elderSeptoggName),
gmData.Rooms.ByName("rm_a5c21").GameObjects.First(go => go.X == 96 && go.Y == 560 && go.ObjectDefinition.Name.Content == elderSeptoggName),
})
{
gameObject.CreationCode.ReplaceGMLInCode("global.item[0]", "global.hasBombs");
Expand All @@ -77,7 +77,7 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
subscreenMenuStep.ReplaceGMLInCode("global.item[2] == 0", "!global.hasSpiderball");
foreach (UndertaleCode code in gmData.Code.Where(c =>
(c.Name.Content.StartsWith("gml_Script_scr_septoggs_") && c.Name.Content.Contains('2')) ||
c.Name.Content == gmData.Rooms.ByName("rm_a0h25").GameObjects.First(go => go.X == 120 && go.Y == 816 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content).CreationCode.Name.Content)
c.Name.Content == gmData.Rooms.ByName("rm_a0h25").GameObjects.First(go => go.X == 120 && go.Y == 816 && go.ObjectDefinition.Name.Content == elderSeptoggName).CreationCode.Name.Content)
)
{
code.ReplaceGMLInCode("global.item[2]", "global.hasSpiderball");
Expand All @@ -95,7 +95,7 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
foreach (UndertaleCode? code in gmData.Code.Where(c =>
(c.Name.Content.StartsWith("gml_Script_scr_septoggs_") && c.Name.Content.Contains('4')) ||
c.Name.Content == "gml_Room_rm_a3b08_Create" ||
c == gmData.Rooms.ByName("rm_a5c17").GameObjects.First(go => go.X == 192 && go.Y == 288 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content).CreationCode)
c == gmData.Rooms.ByName("rm_a5c17").GameObjects.First(go => go.X == 192 && go.Y == 288 && go.ObjectDefinition.Name.Content == elderSeptoggName).CreationCode)
)
{
code.ReplaceGMLInCode("global.item[4]", "global.hasHijump");
Expand All @@ -120,7 +120,7 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
foreach (UndertaleCode? code in gmData.Code.Where(c =>
(c.Name.Content.StartsWith("gml_Script_scr_septoggs_") && c.Name.Content.Contains('6')) ||
gmData.Rooms.ByName("rm_a5a03").GameObjects.Where(go => go.X is >= 96 and <= 112 && go.Y is >= 240 and <= 288 && go.ObjectDefinition.Name.Content == "oBlockStep").Select(go => go.CreationCode).Contains(c) ||
c == gmData.Rooms.ByName("rm_a0h25").GameObjects.First(go => go.X == 120 && go.Y == 816 && go.ObjectDefinition.Name.Content == elderSeptogg.Name.Content).CreationCode)
c == gmData.Rooms.ByName("rm_a0h25").GameObjects.First(go => go.X == 120 && go.Y == 816 && go.ObjectDefinition.Name.Content == elderSeptoggName).CreationCode)
)
{
code.ReplaceGMLInCode("global.item[6]", "global.hasSpacejump");
Expand Down Expand Up @@ -210,7 +210,7 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
}
""");
subscreenMenuStep.ReplaceGMLInCode("""
if (global.curropt == 7 && (!global.hasIbeam))
if (global.curropt == 7 && !global.hasIbeam)
global.curropt += 1
""", """
if (global.curropt == 7 && (!global.hasIbeam))
Expand All @@ -219,7 +219,7 @@ public static void Apply(UndertaleData gmData, GlobalDecompileContext decompileC
global.curropt += 1
""");
subscreenMenuStep.ReplaceGMLInCode("""
if (global.curropt == 7 && (!global.hasIbeam))
if (global.curropt == 7 && !global.hasIbeam)
global.curropt -= 1
""", """
if (global.curropt == 8 && (!global.hasMorph))
Expand Down
Loading
Loading