Skip to content

Commit

Permalink
gzip the bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneWizard08 committed Sep 10, 2024
1 parent 8e82af7 commit a821fb1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
5 changes: 3 additions & 2 deletions ConfigurableWarning/ConfigurableWarning.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
</ItemGroup>

<ItemGroup>
<None Remove="contentsettings.bundle"/>
<EmbeddedResource Include="contentsettings.bundle"/>
<None Remove="contentsettings.bundle" />
<None Remove="contentsettings.bundle.gz" />
<EmbeddedResource Include="contentsettings.bundle.gz" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO.Compression;
using UnityEngine;
using Object = UnityEngine.Object;

Expand All @@ -9,8 +10,8 @@ namespace ContentSettings.Internal;
/// Contains the assets used by the settings system.
/// </summary>
internal static class SettingsAssets {
internal const string BundlePath = "contentsettings.bundle";
internal const string BundlePath = "contentsettings.bundle.gz";

/// <summary>
/// Gets the settings navigation prefab.
/// </summary>
Expand Down Expand Up @@ -41,13 +42,12 @@ internal static class SettingsAssets {
/// </summary>
internal static GameObject SettingsBoolInputPrefab { get; private set; } = null!;

private static Dictionary<string, AssetBundle> AssetBundles { get; } = new ();
private static Dictionary<string, AssetBundle> AssetBundles { get; } = new();

/// <summary>
/// Loads the assets used by the settings system.
/// </summary>
internal static void LoadAssets()
{
internal static void LoadAssets() {
SettingsNavigationPrefab = LoadAsset<GameObject>(
BundlePath,
"Assets/ContentSettings/SettingsNavigation.prefab");
Expand Down Expand Up @@ -82,35 +82,32 @@ internal static void LoadAssets()
/// <returns>The loaded asset.</returns>
/// <exception cref="System.Exception">Thrown if the asset bundle or asset could not be loaded.</exception>
private static T LoadAsset<T>(string bundleName, string assetName)
where T : Object
{
where T : Object {
ContentSettings.Logger.LogDebug($"Loading asset '{assetName}' from asset bundle '{bundleName}'.");

if (AssetBundles.TryGetValue(bundleName, out var bundle))
{
if (AssetBundles.TryGetValue(bundleName, out var bundle)) {
return bundle.LoadAsset<T>(assetName);
}

var assetBundleStream = typeof(ContentSettings)
.Assembly
.GetManifestResourceStream(typeof(ConfigurableWarning.ConfigurableWarning).Namespace + "." + bundleName);

if (assetBundleStream == null)
{
if (assetBundleStream == null) {
throw new Exception($"Failed to load asset bundle '{bundleName}' from embedded resource.");
}

using (assetBundleStream)
{
var assetBundle = AssetBundle.LoadFromStream(assetBundleStream);
if (assetBundle == null)
{
using (var gzStream = new GZipStream(assetBundleStream, CompressionMode.Decompress)) {
var assetBundle = AssetBundle.LoadFromStream(gzStream);

if (assetBundle == null) {
throw new Exception($"Failed to load asset bundle '{bundleName}' from stream.");
}

var asset = assetBundle.LoadAsset<T>(assetName);
if (asset == null)
{

if (asset == null) {
throw new Exception($"Failed to load asset '{assetName}' from asset bundle '{bundleName}'.");
}

Expand Down
Binary file added ConfigurableWarning/contentsettings.bundle.gz
Binary file not shown.

0 comments on commit a821fb1

Please sign in to comment.