/// <summary> /// Instantiates a platform from an assetbundle. /// </summary> /// <param name="bundle">An AssetBundle containing a CustomPlatform</param> /// <param name="parent">The <see cref="Transform"/> under which this <paramref name="bundle"/> will be instantiated</param> /// <returns></returns> private static CustomPlatform LoadPlatform(AssetBundle bundle, Transform parent) { GameObject platformPrefab = bundle.LoadAsset <GameObject>("_CustomPlatform"); if (platformPrefab == null) { return(null); } GameObject newPlatform = UnityEngine.Object.Instantiate(platformPrefab.gameObject); try { foreach (AudioListener al in FindAll <AudioListener>(newPlatform)) { UnityEngine.Object.DestroyImmediate(al); } } catch (ComponentNotFoundException) { } newPlatform.transform.parent = parent; bundle.Unload(false); // Collect author and name CustomPlatform customPlatform = newPlatform.GetComponent <CustomPlatform>(); if (customPlatform == null) { // Check for old platform global::CustomPlatform legacyPlatform = newPlatform.GetComponent <global::CustomPlatform>(); if (legacyPlatform != null) { // Replace legacyplatform component with up to date one customPlatform = newPlatform.AddComponent <CustomPlatform>(); customPlatform.platName = legacyPlatform.platName; customPlatform.platAuthor = legacyPlatform.platAuthor; customPlatform.hideDefaultPlatform = true; // Remove old platform data GameObject.Destroy(legacyPlatform); } else { // no customplatform component, abort GameObject.Destroy(newPlatform); return(null); } } newPlatform.name = customPlatform.platName + " by " + customPlatform.platAuthor; if (customPlatform.icon == null) { customPlatform.icon = Resources.FindObjectsOfTypeAll <Sprite>().Where(x => x.name == "FeetIcon").FirstOrDefault(); } newPlatform.SetActive(false); return(customPlatform); }
/// <summary> /// Instantiate a platform from an assetbundle. /// </summary> /// <param name="bundle">An AssetBundle containing a CustomPlatform</param> /// <returns></returns> private CustomPlatform LoadPlatform(AssetBundle bundle, Transform parent) { GameObject platformPrefab = bundle.LoadAsset <GameObject>("_CustomPlatform"); if (platformPrefab == null) { Plugin.logger.Info("Assetbundle didnt contain a Custom Platform"); return(null); } GameObject newPlatform = GameObject.Instantiate(platformPrefab.gameObject); newPlatform.transform.parent = parent; bundle.Unload(false); // Collect author and name CustomPlatform customPlatform = newPlatform.GetComponent <CustomPlatform>(); if (customPlatform == null) { // Check for old platform global::CustomPlatform legacyPlatform = newPlatform.GetComponent <global::CustomPlatform>(); if (legacyPlatform != null) { Plugin.logger.Info("legacy version of customPlatform detected, updating"); // Replace legacyplatform component with up to date one customPlatform = newPlatform.AddComponent <CustomPlatform>(); customPlatform.platName = legacyPlatform.platName; customPlatform.platAuthor = legacyPlatform.platAuthor; customPlatform.hideDefaultPlatform = true; // Remove old platform data GameObject.Destroy(legacyPlatform); } else { // no customplatform component, abort Plugin.logger.Info("Loaded object had no customplatform attached, skipping"); GameObject.Destroy(newPlatform); return(null); } } newPlatform.name = customPlatform.platName + " by " + customPlatform.platAuthor; if (customPlatform.icon == null) { customPlatform.icon = Resources.FindObjectsOfTypeAll <Sprite>().Where(x => x.name == "FeetIcon").FirstOrDefault(); } AddManagers(newPlatform); newPlatform.gameObject.SetActive(false); return(customPlatform); }
/// <summary> /// Asynchronously loads a <see cref="CustomPlatform"/> from a specified file path /// </summary> internal IEnumerator <AsyncOperation> LoadFromFileAsync(string fullPath, Action <CustomPlatform, string> callback) { if (!File.Exists(fullPath)) { throw new FileNotFoundException("File could not be found", fullPath); } using FileStream fileStream = File.OpenRead(fullPath); AssetBundleCreateRequest assetBundleCreateRequest = AssetBundle.LoadFromStreamAsync(fileStream); yield return(assetBundleCreateRequest); if (!assetBundleCreateRequest.isDone || !assetBundleCreateRequest.assetBundle) { throw new FileLoadException("File coulnd not be loaded", fullPath); } AssetBundleRequest platformAssetBundleRequest = assetBundleCreateRequest.assetBundle.LoadAssetAsync("_CustomPlatform"); yield return(platformAssetBundleRequest); if (!platformAssetBundleRequest.isDone || !platformAssetBundleRequest.asset) { assetBundleCreateRequest.assetBundle.Unload(true); throw new FileLoadException("File coulnd not be loaded", fullPath); } assetBundleCreateRequest.assetBundle.Unload(false); GameObject platformPrefab = (GameObject)platformAssetBundleRequest.asset; foreach (AudioListener al in platformPrefab.GetComponentsInChildren <AudioListener>()) { GameObject.DestroyImmediate(al); } CustomPlatform customPlatform = platformPrefab.GetComponent <CustomPlatform>(); if (customPlatform == null) { // Check for old platform global::CustomPlatform legacyPlatform = platformPrefab.GetComponent <global::CustomPlatform>(); if (legacyPlatform != null) { // Replace legacyplatform component with up to date one customPlatform = platformPrefab.AddComponent <CustomPlatform>(); customPlatform.platName = legacyPlatform.platName; customPlatform.platAuthor = legacyPlatform.platAuthor; customPlatform.hideDefaultPlatform = true; // Remove old platform data GameObject.Destroy(legacyPlatform); } else { // no customplatform component, abort GameObject.Destroy(platformPrefab); yield break; } } customPlatform.name = customPlatform.platName + " by " + customPlatform.platAuthor; customPlatform.fullPath = fullPath; using MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(fileStream); customPlatform.platHash = BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant(); MaterialSwapper.ReplaceMaterials(customPlatform.gameObject); callback(customPlatform, fullPath); GameObject.Destroy(platformPrefab); yield break; }