Skip to content

Commit 6b38cee

Browse files
authored
Fix failing to initialize if game's Data folder is just 'Data' (#975)
Any Unity game can have its _Data folder renamed to just Data and continue working just fine.
1 parent 5a32424 commit 6b38cee

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

BepInEx.Core/Paths.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.IO;
1+
using System.IO;
22
using System.Linq;
33
using System.Reflection;
44
using MonoMod.Utils;
@@ -106,9 +106,23 @@ public static void SetExecutablePath(string executablePath,
106106
? Utility.ParentDirectory(executablePath, 4)
107107
: Path.GetDirectoryName(executablePath);
108108

109-
GameDataPath = managedPath != null && gameDataRelativeToManaged
110-
? Path.GetDirectoryName(managedPath)
111-
: Path.Combine(GameRootPath, $"{ProcessName}_Data");
109+
if (managedPath != null && gameDataRelativeToManaged)
110+
{
111+
GameDataPath = Path.GetDirectoryName(managedPath);
112+
}
113+
else
114+
{
115+
// According to some experiments, Unity checks whether globalgamemanagers/data.unity3d exists in the data folder before picking it.
116+
// 'ProcessName_Data' folder is checked first, then if that fails 'Data' folder is checked. If neither is valid, the player crashes.
117+
// A simple Directory.Exists check is accurate enough while being less likely to break in case these conditions change.
118+
GameDataPath = Path.Combine(GameRootPath, $"{ProcessName}_Data");
119+
if (!Directory.Exists(GameDataPath))
120+
GameDataPath = Path.Combine(GameRootPath, "Data");
121+
}
122+
123+
if (string.IsNullOrEmpty(GameDataPath) || !Directory.Exists(GameDataPath))
124+
throw new DirectoryNotFoundException("Failed to extract valid GameDataPath from executablePath: " + executablePath);
125+
112126
ManagedPath = managedPath ?? Path.Combine(GameDataPath, "Managed");
113127
BepInExRootPath = bepinRootPath ?? Path.Combine(GameRootPath, "BepInEx");
114128
ConfigPath = Path.Combine(BepInExRootPath, "config");

Runtimes/Unity/BepInEx.Unity.IL2CPP/Il2CppInteropManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ private static void DownloadUnityAssemblies()
281281
{
282282
Logger.LogMessage("Running Cpp2IL to generate dummy assemblies");
283283

284-
var metadataPath = Path.Combine(Paths.GameRootPath,
285-
$"{Paths.ProcessName}_Data",
284+
var metadataPath = Path.Combine(Paths.GameDataPath,
286285
"il2cpp_data",
287286
"Metadata",
288287
"global-metadata.dat");

0 commit comments

Comments
 (0)