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

Support custom loading screen music and images #80

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
174 changes: 131 additions & 43 deletions Mapify/Map/MapLifeCycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
using Random = UnityEngine.Random;

namespace Mapify.Map
{
Expand All @@ -29,8 +30,7 @@ public static class MapLifeCycle
public static Action OnCleanup;

private static bool isMapLoaded;
private static List<AssetBundle> assets_assetBundles;
private static AssetBundle scenes;
private static List<AssetBundle> loadedAssetBundles;
private static string originalRailwayScenePath;
private static string originalGameContentScenePath;
private static int scenesToLoad;
Expand All @@ -51,13 +51,59 @@ public static IEnumerator LoadMap(BasicMapInfo basicMapInfo)
yield return null;

// Load asset bundles
loadedAssetBundles = new List<AssetBundle>();
string mapDir = Maps.GetDirectory(basicMapInfo);
string[] assets_assetBundlePaths = Maps.GetMapAssets(Names.ASSETS_ASSET_BUNDLES_PREFIX+"*", mapDir);
assets_assetBundles = new List<AssetBundle>(assets_assetBundlePaths.Length);

foreach (var ass in assets_assetBundlePaths)
// Register mapinfo
Mapify.LogDebug(() => $"Loading AssetBundle '{Names.MAP_INFO_ASSET_BUNDLE}'");
AssetBundleCreateRequest mapInfoRequest = AssetBundle.LoadFromFileAsync(Maps.GetMapAsset(Names.MAP_INFO_ASSET_BUNDLE, mapDir));
do
{
loadingInfo.UpdateLoadingStatus(loadingMapLogMsg, Mathf.RoundToInt(mapInfoRequest.progress * 100));
yield return null;
} while (!mapInfoRequest.isDone);

MapInfo mapInfo = null;
if (mapInfoRequest.assetBundle is null)
{
// Warning and not Error because this occurs if the map is built with an older version of Mapify, and then its not a problem
Debug.LogWarning("Failed to load the mapinfo bundle");
}
else
{
loadedAssetBundles.Add(mapInfoRequest.assetBundle);
var idk = mapInfoRequest.assetBundle.LoadAllAssets();
foreach (var aaa in idk)
{
if (aaa is MapInfo info)
{
mapInfo = info;
break;
}
}

if (mapInfo is null)
{
Mapify.LogError($"Failed to find {nameof(MapInfo)}!");
SceneSwitcher.SwitchToScene(DVScenes.MainMenu);
yield break;
}

//LoadingScreenImages will be null if the map was built with an older version of Mapify
if (mapInfo.LoadingScreenImages != null && mapInfo.LoadingScreenImages.Length > 0)
{
ShowLoadingScreenImage(mapInfo);
}

PlayLoadingScreenMusic(mapInfo.LoadingScreenMusic);
ShowLoadingScreenLogo(mapInfo.LoadingScreenLogo);
}

string[] miscAssets_bundlePaths = Maps.GetMapAssets(Names.MISC_ASSETS_ASSET_BUNDLES_PREFIX+"*", mapDir);

foreach (var bundlePath in miscAssets_bundlePaths)
{
var assetFileName = Path.GetFileName(ass);
var assetFileName = Path.GetFileName(bundlePath);

if (assetFileName.EndsWith(".manifest")) { continue; }

Expand All @@ -70,7 +116,14 @@ public static IEnumerator LoadMap(BasicMapInfo basicMapInfo)
yield return null;
} while (!assetsReq.isDone);

assets_assetBundles.Add(assetsReq.assetBundle);
loadedAssetBundles.Add(assetsReq.assetBundle);

// in maps exported with older versions of Mapify the mapInfo is in the misc assets assetbundle
if (mapInfo is null)
{
mapInfo = assetsReq.assetBundle.LoadAllAssets<MapInfo>()[0];
Maps.RegisterLoadedMap(mapInfo);
}
}

Mapify.LogDebug(() => $"Loading AssetBundle '{Names.SCENES_ASSET_BUNDLE}'");
Expand All @@ -82,24 +135,7 @@ public static IEnumerator LoadMap(BasicMapInfo basicMapInfo)
yield return null;
} while (!scenesReq.isDone);

scenes = scenesReq.assetBundle;

// Register mapinfo
Mapify.LogDebug(() => $"Loading AssetBundle '{Names.MAP_INFO_ASSET_BUNDLE}'");
AssetBundleCreateRequest mapInfoRequest = AssetBundle.LoadFromFileAsync(Maps.GetMapAsset(Names.MAP_INFO_ASSET_BUNDLE, mapDir));
do
{
loadingInfo.UpdateLoadingStatus(loadingMapLogMsg, Mathf.RoundToInt(mapInfoRequest.progress * 100));
yield return null;
} while (!mapInfoRequest.isDone);

var mapInfo = mapInfoRequest.assetBundle.LoadAllAssets<MapInfo>()[0];
if (mapInfo is null)
{
Debug.LogError($"Failed to find {nameof(MapInfo)}!");
SceneSwitcher.SwitchToScene(DVScenes.MainMenu);
yield break;
}
loadedAssetBundles.Add(scenesReq.assetBundle);

Maps.RegisterLoadedMap(mapInfo);

Expand Down Expand Up @@ -140,7 +176,21 @@ public static IEnumerator LoadMap(BasicMapInfo basicMapInfo)
Mapify.Log("Vanilla scenes unloaded");
MonoBehaviourDisablerPatch.EnableAll();

// Set LevelInfo
SetLevelInfo(mapInfo);
SetupStreamer(wsi.gameObject, mapInfo);

InitializeLists();
WorldStreamingInit_Awake_Patch.CanInitialize = true;

foreach (VanillaAsset nonInstantiatableAsset in Enum.GetValues(typeof(VanillaAsset)).Cast<VanillaAsset>().Where(e => !AssetCopier.InstantiatableAssets.Contains(e)))
Mapify.LogError($"VanillaAsset {nonInstantiatableAsset} wasn't set in the AssetCopier! You MUST fix this!");

// Auto-save won't work without this line.
SaveGameManager.Instance.disableAutosave = false;
}

private static void SetLevelInfo(MapInfo mapInfo)
{
LevelInfo levelInfo = SingletonBehaviour<LevelInfo>.Instance;
levelInfo.terrainSize = mapInfo.terrainSize;
levelInfo.waterLevel = mapInfo.waterLevel;
Expand All @@ -152,17 +202,62 @@ public static IEnumerator LoadMap(BasicMapInfo basicMapInfo)
levelInfo.newCareerSpawnRotation = mapInfo.defaultSpawnRotation;
levelInfo.enforceBoundary = true;
levelInfo.worldBoundaryMargin = mapInfo.worldBoundaryMargin;
}

SetupStreamer(wsi.gameObject, mapInfo);
private static void ShowLoadingScreenImage(MapInfo mapInfo)
{
Mapify.Log("Showing custom loading screen image");

InitializeLists();
WorldStreamingInit_Awake_Patch.CanInitialize = true;
var randomInt = Random.Range(0, mapInfo.LoadingScreenImages.Length);
var customImage = mapInfo.LoadingScreenImages[randomInt];

foreach (VanillaAsset nonInstantiatableAsset in Enum.GetValues(typeof(VanillaAsset)).Cast<VanillaAsset>().Where(e => !AssetCopier.InstantiatableAssets.Contains(e)))
Mapify.LogError($"VanillaAsset {nonInstantiatableAsset} wasn't set in the AssetCopier! You MUST fix this!");
var canvasGameObject = Object.FindObjectsOfType<GameObject>().FirstOrDefault(gameObject => gameObject.name.Contains("LoadImage_Background_"));
if (canvasGameObject is null)
{
Mapify.LogError("cant find canvasGameObject");
return;
}

// Auto-save won't work without this line.
SaveGameManager.Instance.disableAutosave = false;
// set the image
canvasGameObject.GetComponent<CanvasRenderer>().SetTexture(customImage);
}

private static void ShowLoadingScreenLogo(Texture2D loadingScreenLogo)
{
if (loadingScreenLogo == null)
{
Mapify.LogDebug(() => "Skipping loading screen logo, it is null");
return;
}

Mapify.Log("Showing custom loading screen logo");

var canvasGameObject = Object.FindObjectsOfType<GameObject>().FirstOrDefault(gameObject => gameObject.name == "LogoImage_alignedByHand");
if (canvasGameObject is null)
{
Mapify.LogError("cant find canvasGameObject");
return;
}

// set the image
canvasGameObject.GetComponent<CanvasRenderer>().SetTexture(loadingScreenLogo);
}

private static void PlayLoadingScreenMusic(AudioClip loadingScreenMusic)
{
if (loadingScreenMusic == null) return;

Mapify.Log("Playing custom loading screen music");
var mainMenuMusicSource = GameObject.Find("Audio Source - main menu music")?.GetComponent<AudioSource>();
if (mainMenuMusicSource == null)
{
Mapify.LogError(nameof(PlayLoadingScreenMusic)+": can't find audio source");
return;
}

mainMenuMusicSource.Pause();
mainMenuMusicSource.clip = loadingScreenMusic;
mainMenuMusicSource.Play();
}

private static void SetupStreamer(GameObject parent, MapInfo mapInfo)
Expand Down Expand Up @@ -245,30 +340,23 @@ private static void InitializeLists()
private static void Cleanup()
{
OnCleanup();
Maps.UnreigsterLoadedMap();
Maps.UnregisterLoadedMap();
SceneManager.sceneLoaded -= OnSceneLoad;
WorldStreamingInit_Awake_Patch.CanInitialize = false;
AssetCopier.Cleanup();
originalRailwayScenePath = null;
originalGameContentScenePath = null;
scenesToLoad = 0;

foreach (AssetBundle bundle in assets_assetBundles)
foreach (AssetBundle bundle in loadedAssetBundles)
{
if (bundle != null)
{
bundle.Unload(true);
}
}

assets_assetBundles = null;

if (scenes != null)
{
scenes.Unload(true);
scenes = null;
}

loadedAssetBundles = null;
isMapLoaded = false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion Mapify/Map/Maps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Mapify.Map
{
public static class Maps
{
//the base DV map
public static readonly BasicMapInfo DEFAULT_MAP_INFO = new BasicMapInfo(Names.DEFAULT_MAP_NAME, $"{BuildInfo.BUILD_VERSION_MAJOR}", null);

public static Action OnMapsUpdated;
Expand Down Expand Up @@ -118,7 +119,7 @@ public static void RegisterLoadedMap(MapInfo mapInfo)
LoadedMap = mapInfo;
}

public static void UnreigsterLoadedMap()
public static void UnregisterLoadedMap()
{
IsDefaultMap = true;
LoadedMap = null;
Expand Down
2 changes: 2 additions & 0 deletions Mapify/Mapify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@
<Reference Include="Unity.TextMeshPro" />
<Reference Include="UnityEngine"/>
<Reference Include="UnityEngine.AssetBundleModule"/>
<Reference Include="UnityEngine.AudioModule"/>
<Reference Include="UnityEngine.CoreModule"/>
<Reference Include="UnityEngine.ImageConversionModule"/>
<Reference Include="UnityEngine.IMGUIModule"/>
<Reference Include="UnityEngine.JSONSerializeModule"/>
<Reference Include="UnityEngine.PhysicsModule"/>
<Reference Include="UnityEngine.TerrainModule"/>
<Reference Include="UnityEngine.UI" />
<Reference Include="UnityEngine.UIModule" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
Expand Down
4 changes: 1 addition & 3 deletions Mapify/SceneInitializers/GameContent/VanillaAssetSetup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using Mapify.Editor;
using Mapify.Editor.Utils;
using Mapify.Editor;
using Mapify.Utils;
using UnityEngine;

Expand Down
Loading