update CCK to 3.10, fixing unity 2021 crash :)

This commit is contained in:
Crispy 2024-08-03 22:24:42 +02:00
parent 48a978fa2a
commit d11e0fb3a9
492 changed files with 2165204 additions and 437687 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using ABI.CCK.Components;
using UnityEditor;
@ -8,221 +9,160 @@ using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.XR;
using Object = UnityEngine.Object;
using Random = System.Random;
namespace ABI.CCK.Scripts.Editor
{
public class CCK_BuildUtility
{
#region Variables
public static PreAvatarBundleEvent PreAvatarBundleEvent = new PreAvatarBundleEvent();
public static PrePropBundleEvent PrePropBundleEvent = new PrePropBundleEvent();
public static string upload_id = "";
public static PreWorldBundleEvent PreWorldBundleEvent = new PreWorldBundleEvent();
public static string upload_id = "";
#endregion
#region Avatar Upload
public static async Task BuildAndUploadAvatar(GameObject avatarObject)
{
//GameObject avatarCopy = null;
var origInfo = avatarObject.GetComponent<CVRAssetInfo>();
ClearLog();
EnforceVRSetting();
/*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<CVRAssetInfo>();
if (string.IsNullOrEmpty(origInfo.objectId))
{
#if UNITY_EDITOR
APIConnection.Initialize(EditorPrefs.GetString("m_ABI_Username"), EditorPrefs.GetString("m_ABI_Key"));
#endif
APIConnection.BaseResponse<APIConnection.GenerateResponse> response = await APIConnection.MakeRequest<APIConnection.GenerateResponse>("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();
CVRAssetInfo assetInfo = avatarObject.GetComponent<CVRAssetInfo>();
EditorUtility.SetDirty(origInfo);
try
await InitializeAPIAndSetObjectId(assetInfo, "cck/generate/avatar");
SetAssetInfoDirty(assetInfo);
GameObject instantiatedAvatar = InstantiateAndUnpackPrefab(avatarObject);
if (!HandlePreBuildEvent(PreAvatarBundleEvent.Invoke, instantiatedAvatar))
{
PrefabUtility.ApplyPrefabInstance(avatarObject, InteractionMode.UserAction);
}
catch
{
Debug.Log("[CCK:BuildUtility] Object is not a prefab. No need to Apply To Instance.");
Object.DestroyImmediate(instantiatedAvatar);
return;
}
PreAvatarBundleEvent.Invoke(avatarObject);
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
CCK_Tools.CleanEditorOnlyGameObjects(instantiatedAvatar);
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());
PrefabUtility.SaveAsPrefabAsset(instantiatedAvatar,
!Application.unityVersion.Contains("2021")
? "Assets/ABI.CCK/Resources/Cache/_CVRAvatar.prefab"
: $"Assets/ABI.CCK/Resources/Cache/CVRAvatar_{assetInfo.objectId}_{assetInfo.randomNum}.prefab");
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"};
assetBundleBuild.assetNames = !Application.unityVersion.Contains("2021")
? new[] { "Assets/ABI.CCK/Resources/Cache/_CVRAvatar.prefab" }
: new[]
{
$"Assets/ABI.CCK/Resources/Cache/CVRAvatar_{assetInfo.objectId}_{assetInfo.randomNum}.prefab"
};
upload_id = origInfo.objectId;
upload_id = assetInfo.objectId;
EditorPrefs.SetString("m_ABI_uploadId", upload_id);
EditorPrefs.SetString("m_ABI_uploadRand", origInfo.randomNum);
EditorPrefs.SetString("m_ABI_uploadRand", assetInfo.randomNum);
assetBundleBuild.assetBundleName = $"cvravatar_{origInfo.objectId}_{origInfo.randomNum}.cvravatar";
assetBundleBuild.assetBundleName = $"cvravatar_{assetInfo.objectId}_{assetInfo.randomNum}.cvravatar";
BuildPipeline.BuildAssetBundles(Application.persistentDataPath, new[] {assetBundleBuild},
BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh();
Object.DestroyImmediate(instantiatedAvatar);
if (!File.Exists($"{Application.persistentDataPath}/cvravatar_{assetInfo.objectId}_{assetInfo.randomNum}.cvravatar"))
{
Debug.LogError("Error during bundling\nThere has been an error during the bundling process. Please check your console for errors.");
EditorUtility.DisplayDialog("Error during bundling", "There has been an error during the bundling process. Please check your console for errors.", "OK");
return;
}
AssetDatabase.Refresh();
EditorPrefs.SetBool("m_ABI_isBuilding", true);
EditorApplication.isPlaying = true;
}
public static async Task BuildAndUploadSpawnable(GameObject s)
#endregion
#region Spawnable Upload
public static async Task BuildAndUploadSpawnable(GameObject spawnableObject)
{
GameObject sCopy = null;
var origInfo = s.GetComponent<CVRAssetInfo>();
var spawnable = s.GetComponent<CVRSpawnable>();
ClearLog();
EnforceVRSetting();
CVRAssetInfo assetInfo = spawnableObject.GetComponent<CVRAssetInfo>();
CVRSpawnable spawnable = spawnableObject.GetComponent<CVRSpawnable>();
spawnable.spawnableType = CVRSpawnable.SpawnableType.StandaloneSpawnable;
EditorUtility.SetDirty(spawnable);
if (string.IsNullOrEmpty(origInfo.objectId))
{
#if UNITY_EDITOR
APIConnection.Initialize(EditorPrefs.GetString("m_ABI_Username"), EditorPrefs.GetString("m_ABI_Key"));
#endif
APIConnection.BaseResponse<APIConnection.GenerateResponse> response = await APIConnection.MakeRequest<APIConnection.GenerateResponse>("cck/generate/spawnable", put: true);
await InitializeAPIAndSetObjectId(assetInfo, "cck/generate/spawnable");
SetAssetInfoDirty(assetInfo);
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
GameObject instantiatedSpawnable = InstantiateAndUnpackPrefab(spawnableObject);
if (!HandlePreBuildEvent(PrePropBundleEvent.Invoke, instantiatedSpawnable))
{
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.");
Object.DestroyImmediate(instantiatedSpawnable);
return;
}
CCK_Tools.CleanEditorOnlyGameObjects(instantiatedSpawnable);
CVRAssetInfo info = sCopy.GetComponent<CVRAssetInfo>();
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());
PrefabUtility.SaveAsPrefabAsset(instantiatedSpawnable,
!Application.unityVersion.Contains("2021")
? "Assets/ABI.CCK/Resources/Cache/_CVRSpawnable.prefab"
: $"Assets/ABI.CCK/Resources/Cache/CVRSpawnable_{assetInfo.objectId}_{assetInfo.randomNum}.prefab");
AssetBundleBuild assetBundleBuild = new AssetBundleBuild();
assetBundleBuild.assetNames = !Application.unityVersion.Contains("2021")
? new[] { "Assets/ABI.CCK/Resources/Cache/_CVRSpawnable.prefab" }
: new[]
{
$"Assets/ABI.CCK/Resources/Cache/CVRSpawnable_{assetInfo.objectId}_{assetInfo.randomNum}.prefab"
};
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;
upload_id = assetInfo.objectId;
EditorPrefs.SetString("m_ABI_uploadId", upload_id);
EditorPrefs.SetString("m_ABI_uploadRand", origInfo.randomNum);
EditorPrefs.SetString("m_ABI_uploadRand", assetInfo.randomNum);
assetBundleBuild.assetBundleName = $"cvrspawnable_{origInfo.objectId}_{origInfo.randomNum}.cvrprop";
assetBundleBuild.assetBundleName = $"cvrspawnable_{assetInfo.objectId}_{assetInfo.randomNum}.cvrprop";
BuildPipeline.BuildAssetBundles(Application.persistentDataPath, new[] {assetBundleBuild},
BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh();
Object.DestroyImmediate(instantiatedSpawnable);
if (!File.Exists($"{Application.persistentDataPath}/cvrspawnable_{assetInfo.objectId}_{assetInfo.randomNum}.cvrprop"))
{
Debug.LogError("Error during bundling\nThere has been an error during the bundling process. Please check your console for errors.");
EditorUtility.DisplayDialog("Error during bundling", "There has been an error during the bundling process. Please check your console for errors.", "OK");
return;
}
AssetDatabase.Refresh();
EditorPrefs.SetBool("m_ABI_isBuilding", true);
EditorApplication.isPlaying = true;
}
#endregion
#region World Upload
public static async Task BuildAndUploadMapAsset(Scene scene, GameObject descriptor)
{
ClearLog();
EnforceVRSetting();
SetupNetworkUUIDs();
CVRAssetInfo info = descriptor.GetComponent<CVRAssetInfo>();
CVRAssetInfo assetInfo = descriptor.GetComponent<CVRAssetInfo>();
if (string.IsNullOrEmpty(info.objectId))
{
#if UNITY_EDITOR
APIConnection.Initialize(EditorPrefs.GetString("m_ABI_Username"), EditorPrefs.GetString("m_ABI_Key"));
#endif
APIConnection.BaseResponse<APIConnection.GenerateResponse> response = await APIConnection.MakeRequest<APIConnection.GenerateResponse>("cck/generate/world", put: true);
await InitializeAPIAndSetObjectId(assetInfo, "cck/generate/world");
SetAssetInfoDirty(assetInfo);
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());
if (!HandlePreBuildEvent(PreWorldBundleEvent.Invoke, scene))
return;
PrefabUtility.SaveAsPrefabAsset(descriptor, "Assets/ABI.CCK/Resources/Cache/_CVRWorld.prefab");
@ -233,13 +173,90 @@ namespace ABI.CCK.Scripts.Editor
BuildPipeline.BuildAssetBundles(Application.persistentDataPath, new[] {assetBundleBuild},
BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh();
if (!File.Exists($"{Application.persistentDataPath}/bundle.cvrworld"))
{
Debug.LogError("Error during bundling\nThere has been an error during the bundling process. Please check your console for errors.");
EditorUtility.DisplayDialog("Error during bundling", "There has been an error during the bundling process. Please check your console for errors.", "OK");
return;
}
AssetDatabase.Refresh();
EditorPrefs.SetBool("m_ABI_isBuilding", true);
EditorApplication.isPlaying = true;
}
public static void SetupNetworkUUIDs()
#endregion
#region Private Methods
private static async Task InitializeAPIAndSetObjectId(CVRAssetInfo assetInfo, string apiEndpoint)
{
if (string.IsNullOrEmpty(assetInfo.objectId))
{
#if UNITY_EDITOR
APIConnection.Initialize(EditorPrefs.GetString("m_ABI_Username"), EditorPrefs.GetString("m_ABI_Key"));
#endif
APIConnection.BaseResponse<APIConnection.GenerateResponse> response = await APIConnection.MakeRequest<APIConnection.GenerateResponse>(apiEndpoint, put: true);
if (response != null && response.Data != null)
assetInfo.objectId = response.Data.Id.ToString();
else
Debug.LogError($"[CCK:BuildUtility] New Guid could not be generated");
}
Random rnd = new Random();
assetInfo.randomNum = rnd.Next(11111111, 99999999).ToString();
}
private static void SetAssetInfoDirty(Component assetInfo)
{
EditorUtility.SetDirty(assetInfo);
EditorSceneManager.MarkSceneDirty(assetInfo.gameObject.scene);
EditorSceneManager.SaveScene(assetInfo.gameObject.scene);
}
private static GameObject InstantiateAndUnpackPrefab(GameObject original)
{
GameObject instantiated = Object.Instantiate(original);
if (PrefabUtility.IsPartOfNonAssetPrefabInstance(instantiated) && PrefabUtility.IsOutermostPrefabInstanceRoot(instantiated))
PrefabUtility.UnpackPrefabInstance(instantiated, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
// Why would you do this?
if (instantiated.CompareTag("EditorOnly"))
instantiated.tag = "Untagged";
return instantiated;
}
private static bool HandlePreBuildEvent(Action<GameObject> preBuildEvent, GameObject instantiatedObject)
{
try
{
preBuildEvent.Invoke(instantiatedObject);
return true;
}
catch (Exception ex)
{
Debug.LogError($"[CCK:BuildUtility] Error occurred during PreBuildEvent: {ex.Message}");
return false;
}
}
private static bool HandlePreBuildEvent(Action<Scene> preBuildEvent, Scene scene)
{
try
{
preBuildEvent.Invoke(scene);
return true;
}
catch (Exception ex)
{
Debug.LogError($"[CCK:BuildUtility] Error occurred during PreBuildEvent: {ex.Message}");
return false;
}
}
private static void SetupNetworkUUIDs()
{
CVRInteractable[] interactables = Resources.FindObjectsOfTypeAll<CVRInteractable>();
CVRObjectSync[] objectSyncs = Resources.FindObjectsOfTypeAll<CVRObjectSync>();
@ -360,17 +377,41 @@ namespace ABI.CCK.Scripts.Editor
newserializedObject.ApplyModifiedProperties();
}
}
}
[System.Serializable]
public class PreAvatarBundleEvent : UnityEvent<GameObject>
{
private static void ClearLog()
{
var assembly = Assembly.GetAssembly(typeof(UnityEditor.Editor));
var type = assembly.GetType("UnityEditor.LogEntries");
var method = type.GetMethod("Clear");
method.Invoke(new object(), null);
}
private static void EnforceVRSetting()
{
#if UNITY_2021_1_OR_NEWER
PlayerSettings.virtualRealitySupported = true;
PlayerSettings.stereoRenderingPath = StereoRenderingPath.Instancing;
XRSettings.enabled = false;
#else
PlayerSettings.virtualRealitySupported = true;
PlayerSettings.SetVirtualRealitySDKs(BuildTargetGroup.Standalone, new string[] { "None", "Oculus", "OpenVR", "MockHMD" });
PlayerSettings.stereoRenderingPath = StereoRenderingPath.SinglePass;
#endif
}
#endregion
}
#region PreBundleEvents
[Serializable]
public class PreAvatarBundleEvent : UnityEvent<GameObject> { }
[System.Serializable]
public class PrePropBundleEvent : UnityEvent<GameObject>
{
}
}
[Serializable]
public class PrePropBundleEvent : UnityEvent<GameObject> { }
[Serializable]
public class PreWorldBundleEvent : UnityEvent<Scene> { }
#endregion
}