Пример #1
0
        /// <summary>
        /// Attempts to load the session corresponding to the given path.
        /// </summary>
        /// <param name="filePath">The path to a solution or package file.</param>
        /// <returns><c>True</c> if the session was successfully loaded, <c>False</c> if an error occurred, <c>Null</c> if the operation was cancelled by user.</returns>
        public async Task <bool?> OpenSession(UFile filePath)
        {
            if (Session != null)
            {
                throw new InvalidOperationException("A session is already open in this instance.");
            }

            if (filePath != null && !File.Exists(filePath))
            {
                MRU.RemoveFile(filePath);
                await ServiceProvider.Get <IDialogService>().MessageBox(string.Format(Tr._p("Message", @"The file '{0}' does not exist."), filePath), MessageBoxButton.OK, MessageBoxImage.Information);

                return(false);
            }

            var sessionResult = new PackageSessionResult();
            var loadedSession = await SessionViewModel.OpenSession(filePath, ServiceProvider, this, sessionResult);

            // Loading has failed
            if (loadedSession == null)
            {
                // Null means the user has cancelled the loading operation.
                return(sessionResult.OperationCancelled ? (bool?)null : false);
            }

            MRU.AddFile(filePath);
            Session = loadedSession;

            InternalSettings.FileDialogLastOpenSessionDirectory.SetValue(new UFile(filePath).GetFullDirectory());
            InternalSettings.Save();
            return(true);
        }
Пример #2
0
        private async Task <PackageSessionResult> SetupResultProgress(IViewUpdater viewUpdater, LoggingScope scope)
        {
            EditorViewModel.LoadingStatus = new LoadingStatus(LoadingStatus.LoadingMode.Indeterminate);
            await viewUpdater.UpdateView();

            // in this result will be any errors from loading the project
            var sessionResult = new PackageSessionResult();

            sessionResult.MessageLogged   += (_, e) => scope.Log(e.Message);
            sessionResult.ProgressChanged += async(_, e) =>
            {
                if (e.HasKnownSteps && e.CurrentStep > 0)
                {
                    var percentage    = EditorViewModel.LoadingStatus.PercentCompleted;
                    var newPercentage = 100 * e.CurrentStep / e.StepCount;
                    EditorViewModel.LoadingStatus.Mode             = LoadingStatus.LoadingMode.Percentage;
                    EditorViewModel.LoadingStatus.PercentCompleted = newPercentage;
                }
                else
                {
                    EditorViewModel.LoadingStatus.Mode = LoadingStatus.LoadingMode.Indeterminate;
                }
                EditorViewModel.LoadingStatus.Message = e.Message;

                await viewUpdater.UpdateView();
            };
            return(sessionResult);
        }
Пример #3
0
        public async Task <LoggerResult> SaveProject()
        {
            if (PackageSession == null)
            {
                throw new InvalidOperationException("Cannot save the project before it was loaded.");
            }

            // TODO: check asset consistency

            // TODO: read more about Stride.Core.Assets.Editor.PackageViewModel
            //// Prepare packages to be saved by setting their dirty flag correctly
            //foreach (var package in LocalPackages)
            //{
            //    package.PreparePackageForSaving();
            //}

            var viewUpdater = Services.GetService <IViewUpdater>();

            PackageSessionResult sessionResult = await SetupResultProgress(viewUpdater, SaveProjectScope);

            // TODO: display a dialog with save progress

            // Force PackageSession.Save to be executed on the thread pool
            // otherwise it would block execution and we want this process to be async
            using (var scope = new TimedScope(SaveProjectScope, TimedScope.Status.Success))
            {
                await Task.Run(() =>
                {
                    try
                    {
                        var saveParameters = PackageSaveParameters.Default();

                        // TODO: read more about Stride.Core.Assets.Editor.AssetViewModel
                        // AllAssets.ForEach(x => x.PrepareSave(sessionResult));
                        PackageSession.Save(sessionResult, saveParameters);
                    }
                    catch (Exception e)
                    {
                        sessionResult.Error(string.Format("There was a problem saving the solution. {0}", e.Message), e);
                        scope.Result = TimedScope.Status.Failure;
                    }
                });

                if (sessionResult.HasErrors)
                {
                    scope.Result = TimedScope.Status.Failure;
                }
            }

            EditorViewModel.LoadingStatus = null;
            await viewUpdater.UpdateView();

            ProjectSaved?.Invoke(sessionResult);

            return(sessionResult);
        }
Пример #4
0
        public async Task <PackageSessionResult> LoadProject(string path)
        {
            var viewUpdater = Services.GetService <IViewUpdater>();

            PackageSessionResult sessionResult = await SetupResultProgress(viewUpdater, LoadProjectScope);

            using (var scope = new TimedScope(LoadProjectScope, TimedScope.Status.Failure))
            {
                // Force PackageSession.Load to be executed on the thread pool
                // otherwise it would block execution and we want this process to be async
                await Task.Run(() =>
                {
                    PackageSession.Load(path, sessionResult);
                }).ConfigureAwait(false);

                PackageSession = sessionResult.Session;

                foreach (var pkg in PackageSession.LocalPackages)
                {
                    pkg.UpdateAssemblyReferences(LoggingScope.Global($"{nameof(Session)}.{nameof(pkg.UpdateAssemblyReferences)}"));
                }

                if (!sessionResult.HasErrors)
                {
                    scope.Result = TimedScope.Status.Success;
                }
            }

            // Create asset browser for package and add it to the viewmodel
            var browser = new AssetBrowserViewModel(PackageSession);
            await Services.GetService <ITabManager>().CreateToolTab(browser);

            EditorViewModel.LoadingStatus = null;
            await viewUpdater.UpdateView();

            ProjectLoaded?.Invoke(sessionResult);

            return(sessionResult);
        }