|
| 1 | +/* |
| 2 | + * PROJECT: Cult of the Lamb Multiplayer Mod |
| 3 | + * LICENSE: MIT (https://spdx.org/licenses/MIT) |
| 4 | + * PURPOSE: Assets management |
| 5 | + * COPYRIGHT: Copyright 2025 GeoB99 <geobman1999@gmail.com> |
| 6 | + */ |
| 7 | + |
| 8 | +/* IMPORTS ********************************************************************/ |
| 9 | + |
| 10 | +using COTLMP; |
| 11 | +using COTLMP.Debug; |
| 12 | +using HarmonyLib; |
| 13 | +using BepInEx; |
| 14 | +using MMTools; |
| 15 | +using System; |
| 16 | +using System.IO; |
| 17 | +using System.Reflection; |
| 18 | +using UnityEngine; |
| 19 | +using UnityEngine.SceneManagement; |
| 20 | +using UnityEngine.UI; |
| 21 | + |
| 22 | +/* CLASSES & CODE *************************************************************/ |
| 23 | + |
| 24 | +/* |
| 25 | + * @brief |
| 26 | + * Contains the classes and code for the Unity Assets management. |
| 27 | + * |
| 28 | + * @class Assets |
| 29 | + * The class for Scenes API management. |
| 30 | + */ |
| 31 | +namespace COTLMP.Api |
| 32 | +{ |
| 33 | + internal static class Assets |
| 34 | + { |
| 35 | + /* |
| 36 | + * @brief |
| 37 | + * Opens an assets bundle file and loads it into memory. |
| 38 | + * |
| 39 | + * @param[in] BundleName |
| 40 | + * A string that points to the name of the bundle file to be |
| 41 | + * opened. |
| 42 | + * |
| 43 | + * @returns |
| 44 | + * Returns the asset bundle object to the caller. NULL is returned |
| 45 | + * if said scene asset couldn't be found within the Assets folder. |
| 46 | + */ |
| 47 | + public static AssetBundle OpenAssetBundleFile(string BundleName) |
| 48 | + { |
| 49 | + AssetBundle Bundle; |
| 50 | + string AssetsLocation; |
| 51 | + string AbsolutePath; |
| 52 | + |
| 53 | + /* Sanity check -- Make sure the Assets folder of the mod exists */ |
| 54 | + AssetsLocation = Path.Combine(Plugin.CotlmpPathLocation, "Assets"); |
| 55 | + COTLMP.Debug.Assertions.Assert(Directory.Exists(AssetsLocation), true, "Assets folder missing", "The Assets folder is either missing or corrupt, please re-install COTLMP!"); |
| 56 | + |
| 57 | + /* Now build the absolute path to the UI assets bundle */ |
| 58 | + AbsolutePath = Path.Combine(AssetsLocation, BundleName); |
| 59 | + COTLMP.Debug.PrintLogger.Print(DebugLevel.MESSAGE_LEVEL, DebugComponent.ASSETS_MANAGEMENT_COMPONENT, $"Absolute Path -> {AbsolutePath}"); |
| 60 | + |
| 61 | + /* And load it from the absolute path */ |
| 62 | + Bundle = AssetBundle.LoadFromFile(AbsolutePath); |
| 63 | + if (Bundle == null) |
| 64 | + { |
| 65 | + COTLMP.Debug.PrintLogger.Print(DebugLevel.ERROR_LEVEL, DebugComponent.ASSETS_MANAGEMENT_COMPONENT, |
| 66 | + $"Failed to load the bundle file -- {BundleName}!"); |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + return Bundle; |
| 71 | + } |
| 72 | + |
| 73 | + /* |
| 74 | + * @brief |
| 75 | + * Loads a scene and shows it to the screen, given its name. |
| 76 | + * |
| 77 | + * @param[in] SceneName |
| 78 | + * A string to the name of the scene of which to be loaded. |
| 79 | + * |
| 80 | + * @param[in] ActionToTakeCallback |
| 81 | + * An action callback provided by the caller. The method executes |
| 82 | + * this callback if the scene name is "Main Menu" of which it |
| 83 | + * performs a specific action depending on the pointed callback. |
| 84 | + * This parameter is optional and ignored for every other scene name. |
| 85 | + * |
| 86 | + */ |
| 87 | + public static void ShowScene(string SceneName, Action ActionToTakeCallback) |
| 88 | + { |
| 89 | + /* |
| 90 | + * The caller asked to load the main menu scene, use MMTools for that |
| 91 | + * as the main menu scene is built-in COTL natively. |
| 92 | + */ |
| 93 | + if (SceneName == "Main Menu") |
| 94 | + { |
| 95 | + MMTools.MMTransition.Play(MMTransition.TransitionType.LoadAndFadeOut, |
| 96 | + MMTransition.Effect.BlackFade, |
| 97 | + SceneName, |
| 98 | + 1, |
| 99 | + null, |
| 100 | + ActionToTakeCallback); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + /* Otherwise use the scene manager to load whatever scene */ |
| 105 | + SceneManager.LoadScene(SceneName, LoadSceneMode.Single); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/* EOF */ |
0 commit comments