Skip to content

Commit

Permalink
The OptimizeBuild method now takes as an input the path to build dire…
Browse files Browse the repository at this point in the history
…ctory instead of the path to a file in that directory

Fixed build with Unity versions 2021 or newer
  • Loading branch information
tool-buddy committed Jun 6, 2022
1 parent 1fad456 commit 63c2153
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
74 changes: 53 additions & 21 deletions Assets/Plugins/FrameRateBooster/Optimizer/Editor/Optimizer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System;
#if UNITY_2021_1_OR_NEWER
#define USE_IPOSTBUILDPLAYERSCRIPTDLLS
#elif UNITY_2019_3_OR_NEWER
#define USE_IIL2CPPPROCESSOR
#endif

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -7,9 +13,11 @@
using Mono.Cecil.Cil;
using UnityEditor;
using UnityEditor.Callbacks;
#if UNITY_2019_3_OR_NEWER
#if USE_IPOSTBUILDPLAYERSCRIPTDLLS || USE_IIL2CPPPROCESSOR
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
#endif
#if USE_IIL2CPPPROCESSOR
using UnityEditor.Il2Cpp;
#endif
using Debug = UnityEngine.Debug;
Expand All @@ -18,20 +26,45 @@ namespace ToolBuddy.FrameRateBooster.Optimizer
{

public class Optimizer
#if UNITY_2019_3_OR_NEWER
#if USE_IPOSTBUILDPLAYERSCRIPTDLLS
: IPostBuildPlayerScriptDLLs
#elif USE_IIL2CPPPROCESSOR
: IIl2CppProcessor
#endif
{
#region Unity callbacks

#if UNITY_2019_3_OR_NEWER
#region Unity callbacks
#if USE_IPOSTBUILDPLAYERSCRIPTDLLS
public int callbackOrder { get; }

public void OnPostBuildPlayerScriptDLLs(BuildReport report)
{
if (PlayerSettings.GetScriptingBackend(report.summary.platformGroup) == ScriptingImplementation.IL2CPP)
{
string pathToBuiltProject = report.files.FirstOrDefault(r => r.path.Contains("StagingArea")).path;

if (string.IsNullOrEmpty(pathToBuiltProject) == false)
{
//Enabling the code line bellow will run the optimization in this case, but builds fail at linking step, and I don't know why, so I am ignoring the optimization.
//OptimizeBuild(report.summary.platform, Path.GetDirectoryName(Path.GetFullPath(pathToBuiltProject)));
Debug.LogWarning("[Frame Rate Booster] FRB can modify IL2CPP builds only on Unity versions between 2019.3 and 2020.3 inclusive.");
}
else
{
Debug.LogWarning("[Frame Rate Booster] Could not find path for StagingArea");
}

Debug.LogWarning("[Frame Rate Booster] Using FRB on IL2CPP can make builds slower. More details at this link: https://forum.curvyeditor.com/thread-861.html");
}
}

#elif USE_IIL2CPPPROCESSOR
public int callbackOrder { get; }

public void OnBeforeConvertRun(BuildReport report, Il2CppBuildPipelineData data)
{
Debug.LogWarning("[Frame Rate Booster] Using FRB on IL2CPP can make builds slower. More details at this link: https://forum.curvyeditor.com/thread-861.html");

OptimizeBuild(report.summary.platform, data.inputDirectory);
OptimizeBuild(report.summary.platform, Path.GetDirectoryName(Path.GetFullPath(data.inputDirectory)));
}
#endif

Expand All @@ -40,17 +73,17 @@ public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProj
{
if (PlayerSettings.GetScriptingBackend(BuildPipeline.GetBuildTargetGroup(target)) == ScriptingImplementation.IL2CPP)
{
#if UNITY_2019_3_OR_NEWER == false
Debug.LogWarning("[Frame Rate Booster] FRB can modify IL2CPP builds only on Unity 2019.3 and above.");
#if USE_IIL2CPPPROCESSOR == false && USE_IPOSTBUILDPLAYERSCRIPTDLLS == false
Debug.LogWarning("[Frame Rate Booster] FRB can modify IL2CPP builds only on Unity versions between 2019.3 and 2020.3 inclusive.");
Debug.LogWarning("[Frame Rate Booster] Using FRB on IL2CPP can make builds slower. More details at this link: https://forum.curvyeditor.com/thread-861.html");
#endif
return;
}
OptimizeBuild(target, pathToBuiltProject);
OptimizeBuild(target, Path.GetDirectoryName(Path.GetFullPath(pathToBuiltProject)));
}
#endregion
#endregion

private static void OptimizeBuild(BuildTarget target, string pathToBuiltProject)
private static void OptimizeBuild(BuildTarget target, string buildDirectoryPath)
{
if (target == BuildTarget.Android)
{
Expand All @@ -75,24 +108,22 @@ private static void OptimizeBuild(BuildTarget target, string pathToBuiltProject)
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

string buildDirectory = Path.GetDirectoryName(Path.GetFullPath(pathToBuiltProject));

string[] allAssembliesPaths = Directory.GetFiles(buildDirectory, "*.dll", SearchOption.AllDirectories);
string[] allAssembliesPaths = Directory.GetFiles(buildDirectoryPath, "*.dll", SearchOption.AllDirectories);

string optimizationsAssemblyPath;
{
const string optimizationsAssemblyName = optimizationInOwnAssembly
? "FrameRateBooster.Optimizations.dll"
: "Assembly-CSharp-firstpass.dll";

if (GetUniqueTargetAssembly(optimizationsAssemblyName, allAssembliesPaths, buildDirectory,
if (GetUniqueTargetAssembly(optimizationsAssemblyName, allAssembliesPaths, buildDirectoryPath,
"the assembly containing the optimizations", out optimizationsAssemblyPath) == false)
return;
}

string targetAssemblyPath;
{
if (GetUniqueTargetAssembly(targetAssemblyName, allAssembliesPaths, buildDirectory, "the assembly to optimize",
if (GetUniqueTargetAssembly(targetAssemblyName, allAssembliesPaths, buildDirectoryPath, "the assembly to optimize",
out targetAssemblyPath) == false)
return;
}
Expand All @@ -116,7 +147,7 @@ private static bool GetUniqueTargetAssembly(string targetAssemblyName, IEnumerab
break;
case 0:
targetAssemblyPath = null;
Debug.LogError(String.Format("[Frame Rate Booster] Couldn't locate recursively {2} {0} in the build folder {1}", targetAssemblyName, buildDirectory, assemblyDescription));
Debug.LogError(String.Format("[Frame Rate Booster] Couldn't locate recursively {2} {0} in the build folder {1}", targetAssemblyName, buildDirectory, assemblyDescription));
return false;
default:
targetAssemblyPath = null;
Expand All @@ -142,9 +173,9 @@ static public int Optimize(string optimizationsAssemblyPath, string targetAssemb

int optimizedMethodsCount = 0;

using (ModuleDefinition optimizedModuleDefinition = ModuleDefinition.ReadModule(optimizationsAssemblyPath, new ReaderParameters() {ReadWrite = trimOptimizationsAssembly}))
using (ModuleDefinition optimizedModuleDefinition = ModuleDefinition.ReadModule(optimizationsAssemblyPath, new ReaderParameters() { ReadWrite = trimOptimizationsAssembly }))
{
using (ModuleDefinition originalModule = ModuleDefinition.ReadModule(targetAssemblyPath, new ReaderParameters() {ReadWrite = true}))
using (ModuleDefinition originalModule = ModuleDefinition.ReadModule(targetAssemblyPath, new ReaderParameters() { ReadWrite = true }))
{
foreach (TypeDefinition optimizedType in optimizedModuleDefinition.Types)
{
Expand Down Expand Up @@ -202,7 +233,7 @@ static public int Optimize(string optimizationsAssemblyPath, string targetAssemb

if (optimizedMethodsCount == 0)
Debug.LogError("[Frame Rate Booster] Couldn't find any method to optimize. This is not supposed to happen. Please report that to the asset creator.");

originalModule.Write();
}

Expand Down Expand Up @@ -245,5 +276,6 @@ static private TypeDefinition GetOriginalTypeIfAny(ModuleDefinition originalModu
{
return originalModule.Types.SingleOrDefault(t => t.Name == optimizedType.Name && t.Namespace == originalNameSpace);
}

}
}
3 changes: 3 additions & 0 deletions Assets/Plugins/FrameRateBooster/ReadMe.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ If you have any questions, feedback or requests, please write to admin@toolbuddy

VERSION HISTORY
===============
1.3.0
Fixed build with Unity versions 2021 or newer
The OptimizeBuild method now takes as an input the path to build directory instead of the path to a file in that directory
1.2.1
Fixed compatibility issue with Multiplayer HLAPI package.
1.2.0
Expand Down

0 comments on commit 63c2153

Please sign in to comment.