예제 #1
0
        /// <summary>
        /// Function to save the editor file.
        /// </summary>
        /// <param name="path">Path to the new file.</param>
        /// <param name="plugIn">The plug-in used to save the file.</param>
        public static void Save(string path, FileWriterPlugIn plugIn)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentException(APIResources.GOREDIT_ERR_PARAMETER_MUST_NOT_BE_EMPTY, "path");
            }

            // We don't have a writer plug-in, at this point, that's not good.
            if (plugIn == null)
            {
                throw new IOException(string.Format(APIResources.GOREDIT_ERR_NO_WRITER_PLUGIN, path));
            }

            // Write the meta data file to the file system.
            EditorMetaDataFile.Save();

            // Write the file.
            if (!plugIn.Save(path))
            {
                return;
            }

            Filename = Path.GetFileName(path);
            FilePath = path;

            // Remove all changed items.
            FileChanged = false;
        }
예제 #2
0
        /// <summary>
        /// Function to save the current content.
        /// </summary>
        /// <param name="persistMetaData">[Optional] TRUE to persist the meta data for the file, FALSE to leave it.</param>
        public static void Save(bool persistMetaData = true)
        {
            if ((Current == null) ||
                (ContentFile == null))
            {
                return;
            }

            // Write the content out to the scratch file system.
            using (var contentStream = ContentFile.OpenStream(true))
            {
                Current.Persist(contentStream);
            }

            // Save any metadata.
            if (!persistMetaData)
            {
                return;
            }

            EditorMetaDataFile.Save();

            if (ContentSaved == null)
            {
                return;
            }

            ContentSaved();
        }
예제 #3
0
        /// <summary>
        /// Function to create a content object instance.
        /// </summary>
        /// <param name="plugIn">The plug-in to use when creating the content.</param>
        /// <param name="settings">Settings to pass to the content.</param>
        /// <param name="editorFile">Editor file that holds the content.</param>
        /// <param name="recordDefaults">TRUE to record the default property values for the content, FALSE to treat as new property values.</param>
        /// <returns>The new content object.</returns>
        private static ContentObject CreateContentObjectInstance(ContentPlugIn plugIn, ContentSettings settings, EditorFile editorFile, bool recordDefaults)
        {
            ContentObject content = plugIn.CreateContentObject(settings);

            content.OnCloseCurrent      = CloseCurrentContent;
            content.OnRenameContent     = RenameCurrentContent;
            content.OnPropertyRefreshed = ContentPropertyRefreshed;
            content.OnChanged           = ContentPropertyChanged;
            content.OnReload            = contentItem =>
            {
                if ((Current == null) ||
                    (contentItem != Current))
                {
                    return;
                }

                Load(editorFile, ContentFile, plugIn, true);
            };
            content.OnCommit = contentItem =>
            {
                if ((Current == null) ||
                    (contentItem != Current))
                {
                    return;
                }

                // Save the content and its metadata.
                Save();
            };

            if ((content.HasProperties) &&
                (recordDefaults))
            {
                content.SetDefaults();
            }

            content.ImageEditor = plugIn.GetRegisteredImageEditor();

            if ((content.ImageEditor != null) &&
                (string.IsNullOrWhiteSpace(PlugIns.DefaultImageEditorPlugIn)))
            {
                PlugIns.DefaultImageEditorPlugIn = content.ImageEditor.Name;
            }

            content.EditorFile = editorFile;

            if (content.EditorFile != null)
            {
                // Indicate that this content is linked to another piece of content.
                content.HasOwner = EditorMetaDataFile.HasFileLinks(editorFile);
            }

            return(content);
        }
예제 #4
0
        /// <summary>
        /// Function to open the editor file.
        /// </summary>
        /// <param name="path">Path to the editor file.</param>
        public static void Open(string path)
        {
            var packFileSystem = new GorgonFileSystem();

            FileChanged = false;

            // Add the new file system as a mount point.
            var plugIns = from plugIn in Gorgon.PlugIns
                          where plugIn is GorgonFileSystemProviderPlugIn &&
                          !PlugIns.IsDisabled(plugIn)
                          select plugIn;

            foreach (var plugIn in plugIns)
            {
                packFileSystem.Providers.LoadProvider(plugIn.Name);
            }

            if (!packFileSystem.Providers.Any(item => item.CanReadFile(path)))
            {
                throw new FileLoadException(string.Format(APIResources.GOREDIT_ERR_NO_READ_PROVIDERS,
                                                          Path.GetFileName(path)));
            }

            packFileSystem.Mount(path);

            try
            {
                // Remove our previous scratch data.
                ResetFile();

                // At this point we should have a clean scratch area, so all files will exist in the packed file.
                // Unpack the file structure so we can work with it.
                var directories = packFileSystem.FindDirectories("*", true);
                var files       = packFileSystem.FindFiles("*", true);

                // Create our directories.
                foreach (var directory in directories)
                {
                    ScratchArea.ScratchFiles.CreateDirectory(directory.FullPath);
                }

                // Copy our files.
                foreach (var file in files)
                {
                    using (var inputStream = packFileSystem.OpenStream(file, false))
                    {
                        using (var outputStream = ScratchArea.ScratchFiles.OpenStream(file.FullPath, true))
                        {
                            inputStream.CopyTo(outputStream);
                        }
                    }
                }

                FilePath = string.Empty;
                Filename = Path.GetFileName(path);

                // Load the meta data if it exists.
                EditorMetaDataFile.Load();

                // If we can't write the file, then leave the editor file path as blank.
                // If the file path is blank, then the Save As function will be triggered if we attempt to save so we
                // can save it in a format that we DO understand.  This is of course assuming we have any plug-ins loaded
                // that will allow us to save.
                if (GetWriterPlugIn(path) != null)
                {
                    FilePath = path;
                }
            }
            catch
            {
                // We have a problem, reset whatever changes we've made.
                ResetFile();
                throw;
            }
            finally
            {
                // At this point we don't need this file system any more.  We'll
                // be using our scratch system instead.
                packFileSystem.Clear();
            }
        }
예제 #5
0
 /// <summary>
 /// Function to reset the file data.
 /// </summary>
 private static void ResetFile()
 {
     ScratchArea.DestroyScratchArea();
     ScratchArea.InitializeScratch();
     EditorMetaDataFile.Reset();
 }