示例#1
0
        private void OpenFormDialog(IFileFormat fileFormat)
        {
            UserControl form = GetEditorForm(fileFormat);

            form.Text = (((IFileFormat)fileFormat).FileName);

            var parentForm = LibraryGUI.GetActiveForm();

            GenericEditorForm editorForm = new GenericEditorForm(true, form);

            editorForm.FormClosing += (sender, e) => FormClosing(sender, e, fileFormat);
            if (editorForm.ShowDialog() == DialogResult.OK)
            {
                if (fileFormat.CanSave)
                {
                    var mem = new System.IO.MemoryStream();
                    fileFormat.Save(mem);
                    ArchiveFileInfo.FileData = mem.ToArray();
                    UpdateEditor();
                }
            }
        }
        /// <summary>
        /// Saves the <see cref="IFileFormat"/> as a file from the given <param name="FileName">
        /// </summary>
        /// <param name="IFileFormat">The format instance of the file being saved</param>
        /// <param name="FileName">The name of the file</param>
        /// <param name="Alignment">The Alignment used for compression. Used for Yaz0 compression type. </param>
        /// <param name="EnableDialog">Toggle for showing compression dialog</param>
        /// <returns></returns>
        public static void SaveFileFormat(IFileFormat FileFormat, string FileName, bool EnableDialog = true, string DetailsLog = "")
        {
            //These always get created on loading a file,however not on creating a new file
            if (FileFormat.IFileInfo == null)
                throw new System.NotImplementedException("Make sure to impliment a IFileInfo instance if a format is being created!");

            Cursor.Current = Cursors.WaitCursor;
            FileFormat.FilePath = FileName;

            string compressionLog = "";
            if (FileFormat.IFileInfo.FileIsCompressed || FileFormat.IFileInfo.InArchive
                || Path.GetExtension(FileName) == ".szs" || Path.GetExtension(FileName) == ".sbfres")
            {
                //Todo find more optmial way to handle memory with files in archives
                //Also make compression require streams
                var mem = new System.IO.MemoryStream();
                FileFormat.Save(mem);
                mem =  new System.IO.MemoryStream(mem.ToArray());

                FileFormat.IFileInfo.DecompressedSize = (uint)mem.Length;

                var finalStream = CompressFileFormat(
                    FileFormat.IFileInfo.FileCompression,
                    mem,
                    FileFormat.IFileInfo.FileIsCompressed,
                    FileFormat.IFileInfo.Alignment,
                    FileName,
                    EnableDialog);

                compressionLog = finalStream.Item2;
                Stream compressionStream = finalStream.Item1;

                FileFormat.IFileInfo.CompressedSize = (uint)compressionStream.Length;
                compressionStream.ExportToFile(FileName);

                DetailsLog += "\n" + SatisfyFileTables(FileFormat, FileName, compressionStream,
                                    FileFormat.IFileInfo.DecompressedSize,
                                    FileFormat.IFileInfo.CompressedSize,
                                    FileFormat.IFileInfo.FileIsCompressed);

                compressionStream.Flush();
                compressionStream.Close();
            }
            else
            {
                //Check if a stream is active and the file is beinng saved to the same opened file
                if (FileFormat is ISaveOpenedFileStream && FileFormat.FilePath == FileName && File.Exists(FileName))
                {
                    string savedPath = Path.GetDirectoryName(FileName);
                    string tempPath = Path.Combine(savedPath, "tempST.bin");

                    //Save a temporary file first to not disturb the opened file
                    using (var fileStream = new FileStream(tempPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        FileFormat.Save(fileStream);
                        FileFormat.Unload();

                        //After saving is done remove the existing file
                        File.Delete(FileName);

                        //Now move and rename our temp file to the new file path
                        File.Move(tempPath, FileName);

                        FileFormat.Load(File.OpenRead(FileName));

                        var activeForm = LibraryGUI.GetActiveForm();
                        if (activeForm != null && activeForm is ObjectEditor)
                            ((ObjectEditor)activeForm).ReloadArchiveFile(FileFormat);
                    }
                }
                else
                {
                    using (var fileStream = new FileStream(FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        FileFormat.Save(fileStream);
                    }
                }
            }

            if (compressionLog != string.Empty)
                MessageBox.Show($"File has been saved to {FileName}. Compressed time: {compressionLog}", "Save Notification");
            else
                MessageBox.Show($"File has been saved to {FileName}", "Save Notification");

             //   STSaveLogDialog.Show($"File has been saved to {FileName}", "Save Notification", DetailsLog);
            Cursor.Current = Cursors.Default;
        }
示例#3
0
        public void OpenFile(string FileName, bool InActiveEditor = false)
        {
            if (File.Exists(FileName))
            {
                SaveRecentFile(FileName);
            }

            object file = STFileLoader.OpenFileFormat(FileName);

            if (file == null) //File might not be supported so return
            {
                return;
            }

            Type objectType = file.GetType();

            bool HasEditorActive = false;

            foreach (var inter in objectType.GetInterfaces())
            {
                if (inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IEditor <>))
                {
                    MethodInfo method     = objectType.GetMethod("OpenForm");
                    MethodInfo methodFill = objectType.GetMethod("FillEditor");
                    var        control    = (UserControl)method.Invoke(file, new object[0]);
                    methodFill.Invoke(file, new object[1] {
                        control
                    });
                    var form = new GenericEditorForm(false, control);
                    TabDupeIndex   = 0;
                    form.Text      = CheckTabDupes(((IFileFormat)file).FileName);
                    form.MdiParent = this;
                    form.Show();

                    HasEditorActive = true;
                }
                else if (inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IEditorForm <>))
                {
                    MethodInfo method = objectType.GetMethod("OpenForm");
                    var        form   = (Form)method.Invoke(file, new object[0]);
                    TabDupeIndex = 0;
                    form.Text    = CheckTabDupes(((IFileFormat)file).FileName);
                    form.Show();

                    HasEditorActive = true;
                }
            }

            bool IsTreeNode    = file is TreeNode;
            bool IsArchiveFile = file is IArchiveFile;

            if (!IsTreeNode && !IsArchiveFile || HasEditorActive)
            {
                SetFormatSettings(GetActiveIFileFormat());
                return;
            }

            //ObjectEditor is for treenode or archive file types. Editors will be on the right side, treenodes on the left
            SetFormatSettings((IFileFormat)file);

            //Check for active object editors
            Form editor = (Form)LibraryGUI.GetActiveForm();

            bool useActiveEditor = false;

            if (editor != null && editor is ObjectEditor)
            {
                //If any are active and we want it to be a new tab then create an instance of one
                if (InActiveEditor || ((ObjectEditor)editor).AddFilesToActiveEditor)
                {
                    useActiveEditor = true;
                }
            }

            bool IsEditorActive = editor != null;

            if (!useActiveEditor || !IsEditorActive)
            {
                editor           = new ObjectEditor(((IFileFormat)file));
                editor.MdiParent = this;
                editor.Text      = CheckTabDupes(((IFileFormat)file).FileName);
                editor.Show();

                ((ObjectEditor)editor).SelectFirstNode();

                if (file is TreeNodeFile)
                {
                    ((TreeNodeFile)file).OnAfterAdded();
                }
            }
            else
            {
                if (IsArchiveFile)
                {
                    ((ObjectEditor)editor).AddIArchiveFile((IFileFormat)file);
                }
                else
                {
                    AddObjectEditorFile(((TreeNode)file), (ObjectEditor)editor, false);
                }
            }

            SetFormatSettings(GetActiveIFileFormat());
        }