using System.IO; using UnityEditor; using Ubisoft.Hotel.Package.Editor; namespace Ubisoft.Hotel.PackageManager.Sample { public class UseCasesSampleManager { private const string SAMPLE_NAME = "properties_use_cases"; private static string m_unitySrcSampleRootPath; [MenuItem("Hotel/PackageManagement/PackageManager/Install sample")] public static void Menu_PreProcess() { OnLoad(); } [InitializeOnLoadMethod] private static void OnLoad() { // Install the sample in consumer's project string unitySrcPath = $"{UnitySrcSampleRootPath}/Hotel/Editor/Scripts"; string unityDstPath = "Assets/Hotel/Editor/Scripts"; string platformSrcPath = HtAssetDatabase.UnityPathToPlatformPath(unitySrcPath, true); if (Directory.Exists(platformSrcPath)) { string msg = "Installing this sample will rearrange some assets in the host project. It is strongly recommended to install it in a fresh new project.\n\n"; msg += $"Do you want to continue with the installation?"; bool confirmation = EditorUtility.DisplayDialog("Hotel PackageManager properties use cases sample", msg, "Yes", "No"); if (confirmation) { InstallSample(unitySrcPath, unityDstPath); } else { // If the user declines the installation then the whole sample folder is deleted HtAssetDatabase.DeleteFolder(UnitySrcSampleRootPath, true); } } } private static string UnitySrcSampleRootPath { get { if (m_unitySrcSampleRootPath == null) { string packageJson = File.ReadAllText(HtAssetUtility.GetAssetInPackagePath(Editor.PackageManager.PACKAGE_NAME, "package.json")); CustomPackageInfo packageInfo = CustomPackageInfo.Parse(packageJson); m_unitySrcSampleRootPath = $"Assets/Samples/{packageInfo.displayName}/{packageInfo.version}/properties_use_cases"; } return m_unitySrcSampleRootPath; } } private static void InstallSample(string unitySrcRootPath, string unityDstRootPath) { Debug.Log("InstallSample"); string platformSrcPath = HtAssetDatabase.UnityPathToPlatformPath(unitySrcRootPath, true); string platformDstPath = HtAssetDatabase.UnityPathToPlatformPath(unityDstRootPath, true); // Delete Assets/Hotel/Editor/Scripts if (Directory.Exists(platformDstPath)) { Directory.Delete(platformDstPath, true); } // Move from sample to Assets/Hotel/Editor/Scripts Directory.Move(platformSrcPath, platformDstPath); // Delete Scripts folder in sample so there will be no more attempts to install it again HtAssetDatabase.DeleteFolder(unitySrcRootPath); // Rename from .txt to .cs string unitySrcPath = $"{unityDstRootPath}/{Editor.AppSpaceBuildSuiteProvider.CONSUMER_PACKAGE_SUITE_PROVIDER_FILENAME_NO_EXT}.txt"; string unityDstPath = $"{unityDstRootPath}/{Editor.AppSpaceBuildSuiteProvider.CONSUMER_PACKAGE_SUITE_PROVIDER_FILENAME_NO_EXT}.cs"; File.Move(HtAssetDatabase.UnityPathToPlatformPath(unitySrcPath), HtAssetDatabase.UnityPathToPlatformPath(unityDstPath)); unitySrcPath = $"{unityDstRootPath}/{Editor.AppSpaceBuildSuiteProvider.CONSUMER_ASMDEF_FILENAME_NO_EXT}.txt"; unityDstPath = $"{unityDstRootPath}/{Editor.AppSpaceBuildSuiteProvider.CONSUMER_ASMDEF_FILENAME_NO_EXT}.asmdef"; File.Move(HtAssetDatabase.UnityPathToPlatformPath(unitySrcPath), HtAssetDatabase.UnityPathToPlatformPath(unityDstPath)); // Delete current hotelSettings.asset so it's regenerated with the new ConsumerPackageSuiteProvider.cs Editor.PackageManager.DeleteHotelSettings(); // Copy scenes to Assets unitySrcPath = $"{UnitySrcSampleRootPath}/Scenes"; unityDstPath = $"Assets/{SAMPLE_NAME}"; HtAssetDatabase.DeleteFolder(unityDstPath); HtAssetDatabase.CreateFolder(unityDstPath); unityDstPath += $"/Scenes"; platformSrcPath = HtAssetDatabase.UnityPathToPlatformPath(unitySrcPath, true); platformDstPath = HtAssetDatabase.UnityPathToPlatformPath(unityDstPath, true); Directory.Move(platformSrcPath, platformDstPath); AssetDatabase.Refresh(); } } }