Example #1
0
        private System.Collections.IEnumerable async_ChangeDataFormat(ProcessingBigTaskDialog.WorkerInterface state)
        {
            state.StateDesc = "DualityApp Data"; yield return(null);

            DualityApp.LoadAppData();
            DualityApp.LoadUserData();
            state.Progress += 0.05f; yield return(null);

            DualityApp.SaveAppData();
            DualityApp.SaveUserData();
            state.Progress += 0.05f; yield return(null);

            // Special case: Current Scene in sandbox mode
            if (Sandbox.IsActive && !string.IsNullOrEmpty(Scene.CurrentPath))
            {
                // Because changes we'll do will be discarded when leaving the sandbox we'll need to
                // do it the hard way - manually load an save the file.
                state.StateDesc = "Current Scene"; yield return(null);

                Scene curScene = Resource.Load <Scene>(Scene.CurrentPath, null, false);
                if (curScene != null)
                {
                    curScene.Save(null, false);
                }
            }

            var           loadedContent = ContentProvider.GetLoadedContent <Resource>();
            List <string> resFiles      = Resource.GetResourceFiles();

            foreach (string file in resFiles)
            {
                if (Sandbox.IsActive && file == Scene.CurrentPath)
                {
                    continue;                                                                // Skip current Scene in Sandbox
                }
                state.StateDesc = file; yield return(null);

                // Wasn't loaded before? Unload it later to keep the memory footprint small.
                bool wasLoaded = loadedContent.Any(r => r.Path == file);

                if (wasLoaded)
                {
                    // Retrieve already loaded content
                    var cr = ContentProvider.RequestContent(file);
                    state.Progress += 0.45f / resFiles.Count; yield return(null);

                    // Perform rename and flag unsaved / modified
                    cr.Res.Save();
                    state.Progress += 0.45f / resFiles.Count; yield return(null);
                }
                else
                {
                    // Load content without initializing it
                    Resource res = Resource.Load <Resource>(file, null, false);
                    state.Progress += 0.45f / resFiles.Count; yield return(null);

                    // Perform rename and save it without making it globally available
                    res.Save(null, false);
                    state.Progress += 0.45f / resFiles.Count; yield return(null);
                }
            }
        }
Example #2
0
        private static System.Collections.IEnumerable async_RenameContentRefs(ProcessingBigTaskDialog.WorkerInterface state)
        {
            var renameData   = state.Data as List <ResourceRenamedEventArgs>;
            int totalCounter = 0;
            int fileCounter  = 0;

            // Rename in static application data
            state.StateDesc = "DualityApp Data"; yield return(null);

            DualityApp.LoadAppData();
            DualityApp.LoadUserData();
            state.Progress += 0.04f; yield return(null);

            totalCounter   += async_RenameContentRefs_Perform(DualityApp.AppData, renameData);
            totalCounter   += async_RenameContentRefs_Perform(DualityApp.UserData, renameData);
            state.Progress += 0.02f; yield return(null);

            DualityApp.SaveAppData();
            DualityApp.SaveUserData();
            state.Progress += 0.04f; yield return(null);

            // Special case: Current Scene in sandbox mode
            if (Sandbox.IsActive)
            {
                // Because changes we'll do will be discarded when leaving the sandbox we'll need to
                // do it the hard way - manually load an save the file.
                state.StateDesc = "Current Scene"; yield return(null);

                Scene curScene = Resource.Load <Scene>(Scene.CurrentPath, null, false);
                fileCounter   = async_RenameContentRefs_Perform(curScene, renameData);
                totalCounter += fileCounter;
                if (fileCounter > 0)
                {
                    curScene.Save(Scene.CurrentPath, false);
                }
            }
            // Special case: Current Scene NOT in sandbox mode, but still unsaved
            else if (Scene.Current.IsRuntimeResource)
            {
                state.StateDesc = "Current Scene"; yield return(null);

                fileCounter = async_RenameContentRefs_Perform(Scene.Current, renameData);
                if (fileCounter > 0)
                {
                    DualityEditorApp.NotifyObjPropChanged(null, new ObjectSelection(Scene.Current.AllObjects));
                }
                totalCounter += fileCounter;
            }

            // Rename in actual content
            var             targetResTypes = renameData.Any(e => e.IsDirectory) ? null : renameData.Select(e => e.ContentType).ToArray();
            var             loadedContent  = ContentProvider.GetLoadedContent <Resource>();
            var             reloadContent  = new List <IContentRef>();
            List <string>   resFiles       = Resource.GetResourceFiles();
            List <Resource> modifiedRes    = new List <Resource>();

            foreach (string file in resFiles)
            {
                // Early-out, if this kind of Resource isn't able to reference the renamed Resource
                if (targetResTypes != null)
                {
                    Type resType = Resource.GetTypeByFileName(file);

                    if (resType == null)
                    {
                        Log.Editor.WriteWarning("Could not determine Resource type for File '{0}' using file name only. Skipping it during rename.", file);
                        continue;
                    }

                    bool canReferenceRes = false;
                    foreach (Type targetType in targetResTypes)
                    {
                        if (ReflectionHelper.CanReferenceResource(resType, targetType))
                        {
                            canReferenceRes = true;
                            break;
                        }
                    }
                    if (!canReferenceRes)
                    {
                        state.Progress += 0.9f / resFiles.Count;
                        continue;
                    }
                }

                // Set displayed name
                state.StateDesc = file; yield return(null);

                // Wasn't loaded before? Unload it later to keep the memory footprint small.
                bool wasLoaded = loadedContent.Any(r => r.Path == file);
                // Keep in mind that this operation is performed while Duality content was
                // in an inconsistent state. Loading Resources now may lead to wrong data.
                // Because the ContentRefs might be wrong right now.

                if (wasLoaded)
                {
                    // Retrieve already loaded content
                    IContentRef cr = ContentProvider.RequestContent(file);
                    state.Progress += 0.45f / resFiles.Count; yield return(null);

                    // Perform rename and flag unsaved / modified
                    fileCounter = async_RenameContentRefs_Perform(cr.Res, renameData);
                    if (fileCounter > 0)
                    {
                        modifiedRes.Add(cr.Res);
                    }
                }
                else
                {
                    // Load content without initializing it
                    Resource res = Resource.Load <Resource>(file, null, false);
                    state.Progress += 0.45f / resFiles.Count; yield return(null);

                    // Perform rename and save it without making it globally available
                    fileCounter = async_RenameContentRefs_Perform(res, renameData);
                    if (fileCounter > 0)
                    {
                        res.Save(null, false);
                    }
                }

                totalCounter   += fileCounter;
                state.Progress += 0.45f / resFiles.Count; yield return(null);
            }

            // Notify the editor about modified Resources
            if (modifiedRes.Count > 0)
            {
                DualityEditorApp.NotifyObjPropChanged(null, new ObjectSelection(modifiedRes));
            }
        }