2019-06-10 00:03:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2019-07-16 21:35:21 +00:00
|
|
|
|
namespace Toolbox.Library.Forms
|
2019-06-10 00:03:56 +00:00
|
|
|
|
{
|
|
|
|
|
//The panel when a file in an archive is clicked on
|
|
|
|
|
//Configures the editor view, ie Hex, Editor, Text Edtior, etc
|
|
|
|
|
public partial class ArchiveFilePanel : UserControl
|
|
|
|
|
{
|
|
|
|
|
ArchiveFileInfo ArchiveFileInfo;
|
2019-08-29 19:45:32 +00:00
|
|
|
|
IArchiveFile ArchiveFile;
|
2019-06-10 00:03:56 +00:00
|
|
|
|
|
|
|
|
|
private bool _IsLoaded = false;
|
|
|
|
|
|
2019-06-10 00:16:11 +00:00
|
|
|
|
public ArchiveFilePanel()
|
2019-06-10 00:03:56 +00:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
ReloadEditors();
|
|
|
|
|
_IsLoaded = true;
|
2019-07-17 20:14:55 +00:00
|
|
|
|
saveBtn.Visible = false;
|
2019-06-10 00:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 19:45:32 +00:00
|
|
|
|
public void LoadFile(ArchiveFileInfo archiveFileInfo, IArchiveFile archiveFile)
|
|
|
|
|
{
|
|
|
|
|
ArchiveFileInfo = archiveFileInfo;
|
|
|
|
|
ArchiveFile = archiveFile;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 20:14:55 +00:00
|
|
|
|
public void LoadFile(ArchiveFileInfo archiveFileInfo)
|
|
|
|
|
{
|
2019-06-10 00:16:11 +00:00
|
|
|
|
ArchiveFileInfo = archiveFileInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 00:03:56 +00:00
|
|
|
|
private void ReloadEditors()
|
|
|
|
|
{
|
|
|
|
|
stComboBox1.Items.Clear();
|
2019-06-18 20:27:36 +00:00
|
|
|
|
stComboBox1.Items.Add("Properties");
|
2019-06-10 00:03:56 +00:00
|
|
|
|
stComboBox1.Items.Add("Hex Editor");
|
|
|
|
|
stComboBox1.Items.Add("File Editor");
|
|
|
|
|
stComboBox1.Items.Add("Text Editor");
|
2019-06-18 20:27:36 +00:00
|
|
|
|
|
|
|
|
|
if (Runtime.ObjectEditor.EditorDiplayIndex < stComboBox1.Items.Count)
|
|
|
|
|
stComboBox1.SelectedIndex = Runtime.ObjectEditor.EditorDiplayIndex;
|
|
|
|
|
else
|
|
|
|
|
stComboBox1.SelectedIndex = 0;
|
2019-06-10 00:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetEditor(int Index) { stComboBox1.SelectedIndex = Index; }
|
|
|
|
|
public int GetEditor() { return stComboBox1.SelectedIndex; }
|
|
|
|
|
|
|
|
|
|
public UserControl GetActiveEditor(Type type)
|
|
|
|
|
{
|
|
|
|
|
foreach (var control in stPanel1.Controls)
|
|
|
|
|
{
|
|
|
|
|
if (control.GetType() == type)
|
|
|
|
|
return (UserControl)control;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateEditor()
|
|
|
|
|
{
|
|
|
|
|
if (GetEditor() == 0)
|
2019-06-18 20:27:36 +00:00
|
|
|
|
UpdatePropertiesView();
|
2019-06-22 14:40:08 +00:00
|
|
|
|
else if (GetEditor() == 1)
|
2019-06-18 20:27:36 +00:00
|
|
|
|
UpdateHexView();
|
2019-06-22 14:40:08 +00:00
|
|
|
|
else if (GetEditor() == 2)
|
2019-06-18 20:27:36 +00:00
|
|
|
|
UpdateFileEditor();
|
2019-06-22 14:40:08 +00:00
|
|
|
|
else if (GetEditor() == 3)
|
2019-06-10 00:03:56 +00:00
|
|
|
|
UpdateTextView();
|
2019-07-17 20:14:55 +00:00
|
|
|
|
|
|
|
|
|
if (GetEditor() == 2 || GetEditor() == 3)
|
|
|
|
|
saveBtn.Visible = true;
|
|
|
|
|
else
|
|
|
|
|
saveBtn.Visible = false;
|
2019-06-10 00:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:05:05 +00:00
|
|
|
|
private void UpdateFileEditor()
|
|
|
|
|
{
|
2019-06-22 14:40:08 +00:00
|
|
|
|
var File = ArchiveFileInfo.FileFormat;
|
|
|
|
|
if (File == null) //If the file is not open yet, try temporarily for a preview
|
|
|
|
|
File = ArchiveFileInfo.OpenFile();
|
|
|
|
|
|
|
|
|
|
//If the file is still null, just add a basic control and return
|
|
|
|
|
if (File == null)
|
2019-06-16 23:35:46 +00:00
|
|
|
|
{
|
|
|
|
|
AddControl(new STUserControl());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 19:45:32 +00:00
|
|
|
|
if (File.IFileInfo != null && ArchiveFile != null)
|
|
|
|
|
File.IFileInfo.ArchiveParent = ArchiveFile;
|
|
|
|
|
|
2019-07-18 20:52:46 +00:00
|
|
|
|
ArchiveFileInfo.FileFormat = File;
|
2019-06-17 00:40:32 +00:00
|
|
|
|
SetEditorForm(File);
|
2019-06-14 00:05:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckActiveType(Type type)
|
|
|
|
|
{
|
|
|
|
|
return stPanel1.Controls.Count > 0 && stPanel1.Controls[0].GetType() != type;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 00:40:32 +00:00
|
|
|
|
public void SetEditorForm(IFileFormat fileFormat)
|
2019-06-14 00:05:05 +00:00
|
|
|
|
{
|
2019-06-14 21:23:02 +00:00
|
|
|
|
if (fileFormat == null)
|
2019-06-16 23:35:46 +00:00
|
|
|
|
{
|
2019-09-13 23:42:16 +00:00
|
|
|
|
AddControl(new STUserControl() { Dock = DockStyle.Fill });
|
2019-08-26 20:46:41 +00:00
|
|
|
|
return;
|
2019-06-16 23:35:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 00:05:05 +00:00
|
|
|
|
Type objectType = fileFormat.GetType();
|
|
|
|
|
foreach (var inter in objectType.GetInterfaces())
|
|
|
|
|
{
|
|
|
|
|
if (inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IEditor<>))
|
|
|
|
|
{
|
|
|
|
|
System.Reflection.MethodInfo method = objectType.GetMethod("OpenForm");
|
2019-06-17 23:19:27 +00:00
|
|
|
|
System.Reflection.MethodInfo methodFill = objectType.GetMethod("FillEditor");
|
|
|
|
|
|
2019-06-16 23:35:46 +00:00
|
|
|
|
var Editor = (UserControl)method.Invoke(fileFormat, new object[0]);
|
|
|
|
|
var ActiveEditor = GetActiveEditor(Editor.GetType());
|
2019-06-17 00:48:17 +00:00
|
|
|
|
if (ActiveEditor == null)
|
|
|
|
|
AddControl(Editor);
|
|
|
|
|
else
|
2019-06-16 23:35:46 +00:00
|
|
|
|
Editor = ActiveEditor;
|
2019-06-17 23:19:27 +00:00
|
|
|
|
|
|
|
|
|
methodFill.Invoke(fileFormat, new object[1] { Editor });
|
2019-09-13 23:42:16 +00:00
|
|
|
|
return;
|
2019-06-14 00:05:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-13 23:42:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fileFormat is STGenericTexture)
|
|
|
|
|
{
|
|
|
|
|
var Editor = ((STGenericTexture)fileFormat).GetEditor();
|
|
|
|
|
var ActiveEditor = GetActiveEditor(Editor.GetType());
|
|
|
|
|
if (ActiveEditor == null)
|
|
|
|
|
AddControl(Editor);
|
|
|
|
|
else
|
|
|
|
|
Editor = ActiveEditor;
|
|
|
|
|
|
|
|
|
|
((STGenericTexture)fileFormat).FillEditor(Editor);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fileFormat is TreeNodeFile)
|
|
|
|
|
{
|
|
|
|
|
var Editor = ((TreeNodeFile)fileFormat).GetEditor();
|
|
|
|
|
var ActiveEditor = GetActiveEditor(Editor.GetType());
|
|
|
|
|
if (ActiveEditor == null)
|
|
|
|
|
AddControl(Editor);
|
|
|
|
|
else
|
|
|
|
|
Editor = ActiveEditor;
|
|
|
|
|
|
|
|
|
|
((TreeNodeFile)fileFormat).FillEditor(Editor);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-06-14 00:05:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 00:03:56 +00:00
|
|
|
|
private void UpdateTextView()
|
|
|
|
|
{
|
|
|
|
|
TextEditor editor = (TextEditor)GetActiveEditor(typeof(TextEditor));
|
|
|
|
|
if (editor == null)
|
|
|
|
|
{
|
|
|
|
|
editor = new TextEditor();
|
|
|
|
|
editor.Dock = DockStyle.Fill;
|
|
|
|
|
AddControl(editor);
|
|
|
|
|
}
|
|
|
|
|
editor.Text = Text;
|
|
|
|
|
|
|
|
|
|
var File = ArchiveFileInfo.FileFormat;
|
|
|
|
|
if (File == null)
|
|
|
|
|
File = ArchiveFileInfo.OpenFile();
|
|
|
|
|
|
2019-06-10 20:30:06 +00:00
|
|
|
|
if (File != null && IsConvertableText(File.GetType()))
|
2019-06-10 00:03:56 +00:00
|
|
|
|
{
|
|
|
|
|
editor.FillEditor(((IConvertableTextFormat)File).ConvertToString());
|
|
|
|
|
|
|
|
|
|
if (((IConvertableTextFormat)File).TextFileType == TextFileType.Yaml)
|
|
|
|
|
editor.IsYAML = true;
|
|
|
|
|
}
|
2019-08-26 20:46:41 +00:00
|
|
|
|
else if (ArchiveFileInfo.FileDataStream != null)
|
|
|
|
|
editor.FillEditor(ArchiveFileInfo.FileDataStream);
|
|
|
|
|
else if (ArchiveFileInfo.FileData != null)
|
2019-06-10 00:03:56 +00:00
|
|
|
|
editor.FillEditor(ArchiveFileInfo.FileData);
|
2019-07-18 20:52:46 +00:00
|
|
|
|
|
|
|
|
|
ArchiveFileInfo.FileFormat = File;
|
2019-06-10 00:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 18:56:19 +00:00
|
|
|
|
private void NotifyFormatSwitched()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveTextFormat()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 00:03:56 +00:00
|
|
|
|
private bool IsConvertableText(Type type)
|
|
|
|
|
{
|
2019-06-24 19:45:54 +00:00
|
|
|
|
return typeof(IConvertableTextFormat).IsAssignableFrom(type);
|
2019-06-10 00:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-18 20:27:36 +00:00
|
|
|
|
private void UpdatePropertiesView()
|
|
|
|
|
{
|
|
|
|
|
STPropertyGrid editor = (STPropertyGrid)GetActiveEditor(typeof(STPropertyGrid));
|
|
|
|
|
if (editor == null)
|
|
|
|
|
{
|
|
|
|
|
editor = new STPropertyGrid();
|
|
|
|
|
editor.Dock = DockStyle.Fill;
|
|
|
|
|
AddControl(editor);
|
|
|
|
|
}
|
|
|
|
|
editor.Text = Text;
|
|
|
|
|
editor.LoadProperty(ArchiveFileInfo.DisplayProperties);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 00:03:56 +00:00
|
|
|
|
private void UpdateHexView()
|
|
|
|
|
{
|
|
|
|
|
HexEditor editor = (HexEditor)GetActiveEditor(typeof(HexEditor));
|
|
|
|
|
if (editor == null)
|
|
|
|
|
{
|
|
|
|
|
editor = new HexEditor();
|
|
|
|
|
editor.Dock = DockStyle.Fill;
|
|
|
|
|
AddControl(editor);
|
|
|
|
|
}
|
|
|
|
|
editor.Text = Text;
|
2019-06-18 20:27:36 +00:00
|
|
|
|
byte[] Data = ArchiveFileInfo.FileData;
|
|
|
|
|
|
2019-08-20 20:52:48 +00:00
|
|
|
|
if (ArchiveFileInfo.FileDataStream != null)
|
|
|
|
|
editor.LoadData(ArchiveFileInfo.FileDataStream);
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
//Only load a certain about of bytes to prevent memory dispose issues
|
|
|
|
|
editor.LoadData(Utils.SubArray(Data, 0, 3000));
|
2019-06-10 00:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void AddControl(Control control)
|
|
|
|
|
{
|
2019-06-17 00:40:32 +00:00
|
|
|
|
foreach (var child in stPanel1.Controls)
|
|
|
|
|
if (child is STUserControl)
|
|
|
|
|
((STUserControl)child).OnControlClosing();
|
|
|
|
|
|
2019-06-10 00:03:56 +00:00
|
|
|
|
stPanel1.Controls.Clear();
|
|
|
|
|
stPanel1.Controls.Add(control);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stComboBox1_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2019-06-18 20:27:36 +00:00
|
|
|
|
if (_IsLoaded && stComboBox1.SelectedIndex != -1)
|
|
|
|
|
{
|
|
|
|
|
Runtime.ObjectEditor.EditorDiplayIndex = stComboBox1.SelectedIndex;
|
2019-06-10 00:03:56 +00:00
|
|
|
|
UpdateEditor();
|
2019-06-18 20:27:36 +00:00
|
|
|
|
}
|
2019-06-10 00:03:56 +00:00
|
|
|
|
}
|
2019-07-17 20:14:55 +00:00
|
|
|
|
|
|
|
|
|
private void saveBtn_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
bool IsTextEditor = GetEditor() == 3;
|
|
|
|
|
|
|
|
|
|
var File = ArchiveFileInfo.FileFormat;
|
2019-07-18 20:55:41 +00:00
|
|
|
|
if (IsTextEditor && File != null && IsConvertableText(File.GetType()))
|
2019-07-17 20:14:55 +00:00
|
|
|
|
{
|
2019-07-18 20:55:41 +00:00
|
|
|
|
if (((IConvertableTextFormat)File).CanConvertBack)
|
|
|
|
|
{
|
|
|
|
|
TextEditor editor = (TextEditor)GetActiveEditor(typeof(TextEditor));
|
|
|
|
|
((IConvertableTextFormat)File).ConvertFromString(editor.GetText());
|
2019-07-17 20:14:55 +00:00
|
|
|
|
|
2019-07-18 20:55:41 +00:00
|
|
|
|
ArchiveFileInfo.SaveFileFormat();
|
|
|
|
|
MessageBox.Show($"Saved {File.FileName} to archive!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show($"File format does not support converting back from type: {((IConvertableTextFormat)File).TextFileType}!");
|
|
|
|
|
}
|
2019-07-17 20:14:55 +00:00
|
|
|
|
}
|
|
|
|
|
else if (File != null && File.CanSave)
|
|
|
|
|
{
|
|
|
|
|
ArchiveFileInfo.SaveFileFormat();
|
|
|
|
|
MessageBox.Show($"Saved {File.FileName} to archive!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
MessageBox.Show($"File format does not support saving!");
|
|
|
|
|
}
|
2019-06-10 00:03:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|