using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using ABI.CCK.Components; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.Events; using UnityEngine.SceneManagement; using Random = System.Random; namespace ABI.CCK.Scripts.Editor { public class CCK_BuildUtility { public static PreAvatarBundleEvent PreAvatarBundleEvent = new PreAvatarBundleEvent(); public static PrePropBundleEvent PrePropBundleEvent = new PrePropBundleEvent(); public static string upload_id = ""; public static async Task BuildAndUploadAvatar(GameObject avatarObject) { //GameObject avatarCopy = null; var origInfo = avatarObject.GetComponent(); /*try { avatarCopy = GameObject.Instantiate(avatarObject); PrefabUtility.UnpackPrefabInstance(avatarCopy, PrefabUnpackMode.Completely, InteractionMode.UserAction); Debug.Log("[CCK:BuildUtility] To prevent problems, the prefab has been unpacked. Your game object is no longer linked to the prefab instance."); } catch { Debug.Log("[CCK:BuildUtility] Object is not a prefab. No need to unpack."); }*/ //CVRAssetInfo info = avatarCopy.GetComponent(); if (string.IsNullOrEmpty(origInfo.objectId)) { #if UNITY_EDITOR APIConnection.Initialize(EditorPrefs.GetString("m_ABI_Username"), EditorPrefs.GetString("m_ABI_Key")); #endif APIConnection.BaseResponse response = await APIConnection.MakeRequest("cck/generate/avatar", put: true); if (response != null && response.Data != null) { origInfo.objectId = response.Data.Id.ToString(); } else { Debug.LogError($"[CCK:BuildUtility] New Guid could not be generated"); } } Random rnd = new Random(); origInfo.randomNum = rnd.Next(11111111, 99999999).ToString(); EditorUtility.SetDirty(origInfo); try { PrefabUtility.ApplyPrefabInstance(avatarObject, InteractionMode.UserAction); } catch { Debug.Log("[CCK:BuildUtility] Object is not a prefab. No need to Apply To Instance."); } PreAvatarBundleEvent.Invoke(avatarObject); EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene()); if (!Application.unityVersion.Contains("2021")) PrefabUtility.SaveAsPrefabAsset(avatarObject, "Assets/ABI.CCK/Resources/Cache/_CVRAvatar.prefab"); else PrefabUtility.SaveAsPrefabAsset(avatarObject, $"Assets/ABI.CCK/Resources/Cache/CVRAvatar_{origInfo.objectId}_{origInfo.randomNum}.prefab"); //GameObject.DestroyImmediate(avatarCopy); EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene()); AssetBundleBuild assetBundleBuild = new AssetBundleBuild(); if (!Application.unityVersion.Contains("2021")) assetBundleBuild.assetNames = new[] {"Assets/ABI.CCK/Resources/Cache/_CVRAvatar.prefab"}; else assetBundleBuild.assetNames = new[] {$"Assets/ABI.CCK/Resources/Cache/CVRAvatar_{origInfo.objectId}_{origInfo.randomNum}.prefab"}; upload_id = origInfo.objectId; EditorPrefs.SetString("m_ABI_uploadId", upload_id); EditorPrefs.SetString("m_ABI_uploadRand", origInfo.randomNum); assetBundleBuild.assetBundleName = $"cvravatar_{origInfo.objectId}_{origInfo.randomNum}.cvravatar"; BuildPipeline.BuildAssetBundles(Application.persistentDataPath, new[] {assetBundleBuild}, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget); AssetDatabase.Refresh(); EditorPrefs.SetBool("m_ABI_isBuilding", true); EditorApplication.isPlaying = true; } public static async Task BuildAndUploadSpawnable(GameObject s) { GameObject sCopy = null; var origInfo = s.GetComponent(); var spawnable = s.GetComponent(); spawnable.spawnableType = CVRSpawnable.SpawnableType.StandaloneSpawnable; if (string.IsNullOrEmpty(origInfo.objectId)) { #if UNITY_EDITOR APIConnection.Initialize(EditorPrefs.GetString("m_ABI_Username"), EditorPrefs.GetString("m_ABI_Key")); #endif APIConnection.BaseResponse response = await APIConnection.MakeRequest("cck/generate/spawnable", put: true); if (response != null && response.Data != null) { origInfo.objectId = response.Data.Id.ToString(); } else { Debug.LogError($"[CCK:BuildUtility] New Guid could not be generated"); } } Random rnd = new Random(); origInfo.randomNum = rnd.Next(11111111, 99999999).ToString(); EditorUtility.SetDirty(origInfo); PrePropBundleEvent.Invoke(s); try { sCopy = GameObject.Instantiate(s); PrefabUtility.UnpackPrefabInstance(sCopy, PrefabUnpackMode.Completely, InteractionMode.UserAction); Debug.Log("[CCK:BuildUtility] To prevent problems, the prefab has been unpacked. Your game object is no longer linked to the prefab instance."); } catch { Debug.Log("[CCK:BuildUtility] Object is not a prefab. No need to unpack."); } CVRAssetInfo info = sCopy.GetComponent(); try { PrefabUtility.ApplyPrefabInstance(s, InteractionMode.UserAction); } catch { Debug.Log("[CCK:BuildUtility] Object is not a prefab. No need to Apply To Instance."); } EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene()); if (!Application.unityVersion.Contains("2021")) PrefabUtility.SaveAsPrefabAsset(sCopy, "Assets/ABI.CCK/Resources/Cache/_CVRSpawnable.prefab"); else PrefabUtility.SaveAsPrefabAsset(sCopy, $"Assets/ABI.CCK/Resources/Cache/CVRSpawnable_{origInfo.objectId}_{origInfo.randomNum}.prefab"); GameObject.DestroyImmediate(sCopy); EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene()); AssetBundleBuild assetBundleBuild = new AssetBundleBuild(); if (!Application.unityVersion.Contains("2021")) assetBundleBuild.assetNames = new[] {"Assets/ABI.CCK/Resources/Cache/_CVRSpawnable.prefab"}; else assetBundleBuild.assetNames = new[] {$"Assets/ABI.CCK/Resources/Cache/CVRSpawnable_{origInfo.objectId}_{origInfo.randomNum}.prefab"}; upload_id = origInfo.objectId; EditorPrefs.SetString("m_ABI_uploadId", upload_id); EditorPrefs.SetString("m_ABI_uploadRand", origInfo.randomNum); assetBundleBuild.assetBundleName = $"cvrspawnable_{origInfo.objectId}_{origInfo.randomNum}.cvrprop"; BuildPipeline.BuildAssetBundles(Application.persistentDataPath, new[] {assetBundleBuild}, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget); AssetDatabase.Refresh(); EditorPrefs.SetBool("m_ABI_isBuilding", true); EditorApplication.isPlaying = true; } public static async Task BuildAndUploadMapAsset(Scene scene, GameObject descriptor) { SetupNetworkUUIDs(); CVRAssetInfo info = descriptor.GetComponent(); if (string.IsNullOrEmpty(info.objectId)) { #if UNITY_EDITOR APIConnection.Initialize(EditorPrefs.GetString("m_ABI_Username"), EditorPrefs.GetString("m_ABI_Key")); #endif APIConnection.BaseResponse response = await APIConnection.MakeRequest("cck/generate/world", put: true); if (response != null && response.Data != null) { info.objectId = response.Data.Id.ToString(); EditorUtility.SetDirty(info); } else { Debug.LogError($"[CCK:BuildUtility] New Guid could not be generated"); } } EditorSceneManager.MarkSceneDirty(scene); EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene()); PrefabUtility.SaveAsPrefabAsset(descriptor, "Assets/ABI.CCK/Resources/Cache/_CVRWorld.prefab"); AssetBundleBuild assetBundleBuild = new AssetBundleBuild(); assetBundleBuild.assetNames = new[] {scene.path}; assetBundleBuild.assetBundleName = "bundle.cvrworld"; BuildPipeline.BuildAssetBundles(Application.persistentDataPath, new[] {assetBundleBuild}, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget); AssetDatabase.Refresh(); EditorPrefs.SetBool("m_ABI_isBuilding", true); EditorApplication.isPlaying = true; } public static void SetupNetworkUUIDs() { CVRInteractable[] interactables = Resources.FindObjectsOfTypeAll(); CVRObjectSync[] objectSyncs = Resources.FindObjectsOfTypeAll(); CVRVideoPlayer[] videoPlayers = Resources.FindObjectsOfTypeAll(); CVRSpawnable[] spawnables = Resources.FindObjectsOfTypeAll(); GameInstanceController[] gameInstances = Resources.FindObjectsOfTypeAll(); GunController[] gunControllers = Resources.FindObjectsOfTypeAll(); List UsedGuids = new List(); foreach (var interactable in interactables) { foreach (var action in interactable.actions) { string guid; do { guid = Guid.NewGuid().ToString(); } while (!string.IsNullOrEmpty(UsedGuids.Find(match => match == guid))); UsedGuids.Add(guid); action.guid = guid; } } foreach (var objectSync in objectSyncs) { string guid; do { guid = Guid.NewGuid().ToString(); } while (!string.IsNullOrEmpty(UsedGuids.Find(match => match == guid))); UsedGuids.Add(guid); var newserializedObject = new SerializedObject(objectSync); newserializedObject.Update(); SerializedProperty _guidProperty = newserializedObject.FindProperty("guid"); _guidProperty.stringValue = guid; newserializedObject.ApplyModifiedProperties(); } foreach (var player in videoPlayers) { Guid res; if (player.playerId == null || !Guid.TryParse(player.playerId, out res)) { string guid; do { guid = Guid.NewGuid().ToString(); } while (!string.IsNullOrEmpty(UsedGuids.Find(match => match == guid))); UsedGuids.Add(guid); player.playerId = guid; } } foreach (var spawnable in spawnables) { string guid; do { guid = Guid.NewGuid().ToString(); } while (!string.IsNullOrEmpty(UsedGuids.Find(match => match == guid))); if (spawnable.preGeneratedInstanceId == "") { UsedGuids.Add(guid); spawnable.preGeneratedInstanceId = "ws~" + guid; } else { UsedGuids.Add(spawnable.preGeneratedInstanceId); } spawnable.spawnableType = CVRSpawnable.SpawnableType.WorldSpawnable; } int i = 0; foreach (GameInstanceController gameInstance in gameInstances) { if (gameInstance.teams.Count == 0) { var team = new Team(); team.name = "Team"; team.color = Color.red; team.playerLimit = 16; team.index = i; gameInstance.teams.Add(team); i++; } else { foreach (var team in gameInstance.teams) { team.index = i; i++; } } } foreach (var gunController in gunControllers) { string guid; do { guid = Guid.NewGuid().ToString(); } while (!string.IsNullOrEmpty(UsedGuids.Find(match => match == guid))); UsedGuids.Add(guid); var newserializedObject = new SerializedObject(gunController); newserializedObject.Update(); SerializedProperty _guidProperty = newserializedObject.FindProperty("referenceID"); _guidProperty.stringValue = guid; newserializedObject.ApplyModifiedProperties(); } } } [System.Serializable] public class PreAvatarBundleEvent : UnityEvent { } [System.Serializable] public class PrePropBundleEvent : UnityEvent { } }