Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 8c34aa2

Browse files
authored
Merge pull request #29 from js6pak/embedded-assetbundle
Load assetbundle from EmbeddedResource
2 parents 190467f + 4a1c54f commit 8c34aa2

File tree

9 files changed

+66
-18
lines changed

9 files changed

+66
-18
lines changed

resources/Older Unity bundle/THIS BUNDLE IS FOR UNITY 5.6.1 AND OLDER.txt

Whitespace-only changes.

src/Config/ModConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ModConfig
1111
public static ModConfig Instance;
1212

1313
internal static readonly IniDataParser _parser = new IniDataParser();
14-
internal const string INI_PATH = ExplorerCore.EXPLORER_FOLDER + @"\config.ini";
14+
internal static readonly string INI_PATH = Path.Combine(ExplorerCore.EXPLORER_FOLDER, "config.ini");
1515

1616
static ModConfig()
1717
{

src/ExplorerCore.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using BepInEx;
34
using UnityEngine;
45
using UnityEngine.SceneManagement;
56
using UnityExplorer.Config;
@@ -19,7 +20,12 @@ public class ExplorerCore
1920
public const string VERSION = "3.1.4";
2021
public const string AUTHOR = "Sinai";
2122
public const string GUID = "com.sinai.unityexplorer";
23+
24+
#if ML
2225
public const string EXPLORER_FOLDER = @"Mods\UnityExplorer";
26+
#elif BIE
27+
public static string EXPLORER_FOLDER = Path.Combine(Paths.ConfigPath, "UnityExplorer");
28+
#endif
2329

2430
public static ExplorerCore Instance { get; private set; }
2531

src/Helpers/Texture2DHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public static Texture2D Copy(Texture2D orig, Rect rect)
7070

7171
pixels = orig.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
7272

73-
var _newTex = new Texture2D((int)rect.width, (int)rect.height);
73+
// use full constructor for better compatibility
74+
var _newTex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGBA32, Texture.GenerateAllMips, false, IntPtr.Zero);
7475
_newTex.SetPixels(pixels);
7576

7677
return _newTex;
File renamed without changes.
File renamed without changes.

src/UI/UIManager.cs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,33 +88,62 @@ public static void Update()
8888
}
8989
}
9090

91-
private static void LoadBundle()
91+
private static byte[] ReadFully(this Stream input)
9292
{
93-
var bundlePath = ExplorerCore.EXPLORER_FOLDER + @"\explorerui.bundle";
94-
if (File.Exists(bundlePath))
93+
using (var ms = new MemoryStream())
9594
{
96-
var bundle = AssetBundle.LoadFromFile(bundlePath);
95+
input.CopyTo(ms);
96+
return ms.ToArray();
97+
}
98+
}
99+
100+
private static AssetBundle LoadExplorerUi(string id)
101+
{
102+
return AssetBundle.LoadFromMemory(ReadFully(typeof(ExplorerCore).Assembly.GetManifestResourceStream($"UnityExplorer.Resources.explorerui.{id}.bundle")));
103+
}
97104

98-
BackupShader = bundle.LoadAsset<Shader>("DefaultUI");
105+
private static void LoadBundle()
106+
{
107+
AssetBundle bundle = null;
108+
109+
try
110+
{
111+
bundle = LoadExplorerUi("modern");
112+
}
113+
catch
114+
{
115+
ExplorerCore.Log("Failed to load modern ExplorerUI Bundle, falling back to legacy");
99116

100-
// Fix for games which don't ship with 'UI/Default' shader.
101-
if (Graphic.defaultGraphicMaterial.shader?.name != "UI/Default")
117+
try
102118
{
103-
ExplorerCore.Log("This game does not ship with the 'UI/Default' shader, using manual Default Shader...");
104-
Graphic.defaultGraphicMaterial.shader = BackupShader;
119+
bundle = LoadExplorerUi("legacy");
105120
}
121+
catch
122+
{
123+
// ignored
124+
}
125+
}
106126

107-
ResizeCursor = bundle.LoadAsset<Sprite>("cursor");
127+
if (bundle == null)
128+
{
129+
ExplorerCore.LogWarning("Could not load the ExplorerUI Bundle!");
130+
return;
131+
}
108132

109-
ConsoleFont = bundle.LoadAsset<Font>("CONSOLA");
133+
BackupShader = bundle.LoadAsset<Shader>("DefaultUI");
110134

111-
ExplorerCore.Log("Loaded UI bundle");
112-
}
113-
else
135+
// Fix for games which don't ship with 'UI/Default' shader.
136+
if (Graphic.defaultGraphicMaterial.shader?.name != "UI/Default")
114137
{
115-
ExplorerCore.LogWarning("Could not find the ExplorerUI Bundle! It should exist at '" + bundlePath + "'");
116-
return;
138+
ExplorerCore.Log("This game does not ship with the 'UI/Default' shader, using manual Default Shader...");
139+
Graphic.defaultGraphicMaterial.shader = BackupShader;
117140
}
141+
142+
ResizeCursor = bundle.LoadAsset<Sprite>("cursor");
143+
144+
ConsoleFont = bundle.LoadAsset<Font>("CONSOLA");
145+
146+
ExplorerCore.Log("Loaded UI bundle");
118147
}
119148

120149
private static GameObject CreateRootCanvas()

src/UnityExplorer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@
293293
<Compile Include="Unstrip\SceneUnstrip.cs" />
294294
<Compile Include="Properties\AssemblyInfo.cs" />
295295
<Compile Include="UI\UIFactory.cs" />
296+
<EmbeddedResource Include="Resources\*" />
296297
</ItemGroup>
297298
<ItemGroup>
298299
<None Include="ILRepack.targets" />

src/Unstrip/AssetBundleUnstrip.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public static AssetBundle LoadFromFile(string path)
2424

2525
return new AssetBundle(ptr);
2626
}
27+
28+
private delegate IntPtr d_LoadFromMemory(IntPtr binary, uint crc);
29+
30+
public static AssetBundle LoadFromMemory(byte[] binary, uint crc = 0)
31+
{
32+
var iCall = ICallHelper.GetICall<d_LoadFromMemory>("UnityEngine.AssetBundle::LoadFromMemory_Internal");
33+
34+
var ptr = iCall(((Il2CppStructArray<byte>) binary).Pointer, crc);
35+
36+
return new AssetBundle(ptr);
37+
}
2738

2839
// ~~~~~~~~~~~~ Instance ~~~~~~~~~~~~
2940

0 commit comments

Comments
 (0)