1
0
mirror of synced 2024-12-11 23:36:01 +01:00
Switch-Toolbox/Switch_Toolbox_Library/Forms/Archive/ArchiveFilePanel.cs

226 lines
7.0 KiB
C#
Raw Normal View History

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;
namespace Switch_Toolbox.Library.Forms
{
//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;
private bool _IsLoaded = false;
2019-06-10 02:16:11 +02:00
public ArchiveFilePanel()
{
InitializeComponent();
ReloadEditors();
_IsLoaded = true;
}
2019-06-10 02:16:11 +02:00
public void LoadFile(ArchiveFileInfo archiveFileInfo) {
ArchiveFileInfo = archiveFileInfo;
}
private void ReloadEditors()
{
stComboBox1.Items.Clear();
2019-06-18 22:27:36 +02:00
stComboBox1.Items.Add("Properties");
stComboBox1.Items.Add("Hex Editor");
stComboBox1.Items.Add("File Editor");
stComboBox1.Items.Add("Text Editor");
2019-06-18 22:27:36 +02:00
if (Runtime.ObjectEditor.EditorDiplayIndex < stComboBox1.Items.Count)
stComboBox1.SelectedIndex = Runtime.ObjectEditor.EditorDiplayIndex;
else
stComboBox1.SelectedIndex = 0;
}
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 22:27:36 +02:00
UpdatePropertiesView();
else if (GetEditor() == 1)
2019-06-18 22:27:36 +02:00
UpdateHexView();
else if (GetEditor() == 2)
2019-06-18 22:27:36 +02:00
UpdateFileEditor();
else if (GetEditor() == 3)
UpdateTextView();
}
2019-06-14 02:05:05 +02:00
private void UpdateFileEditor()
{
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)
{
AddControl(new STUserControl());
return;
}
2019-06-17 02:40:32 +02:00
SetEditorForm(File);
//If the format isn't active we can just dispose it
if (ArchiveFileInfo.FileFormat == null)
File.Unload();
2019-06-14 02:05:05 +02:00
}
private bool CheckActiveType(Type type)
{
return stPanel1.Controls.Count > 0 && stPanel1.Controls[0].GetType() != type;
}
2019-06-17 02:40:32 +02:00
public void SetEditorForm(IFileFormat fileFormat)
2019-06-14 02:05:05 +02:00
{
if (fileFormat == null)
2019-06-17 02:40:32 +02:00
AddControl(new STUserControl() { Dock = DockStyle.Fill });
if (fileFormat is TreeNodeFile)
{
var Editor = ((TreeNodeFile)fileFormat).GetEditor();
var ActiveEditor = GetActiveEditor(Editor.GetType());
2019-06-17 02:48:17 +02:00
if (ActiveEditor == null)
AddControl(Editor);
else
Editor = ActiveEditor;
((TreeNodeFile)fileFormat).FillEditor(Editor);
}
2019-06-14 02:05:05 +02: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");
System.Reflection.MethodInfo methodFill = objectType.GetMethod("FillEditor");
var Editor = (UserControl)method.Invoke(fileFormat, new object[0]);
var ActiveEditor = GetActiveEditor(Editor.GetType());
2019-06-17 02:48:17 +02:00
if (ActiveEditor == null)
AddControl(Editor);
else
Editor = ActiveEditor;
methodFill.Invoke(fileFormat, new object[1] { Editor });
2019-06-14 02:05:05 +02: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 22:30:06 +02:00
if (File != null && IsConvertableText(File.GetType()))
{
editor.FillEditor(((IConvertableTextFormat)File).ConvertToString());
if (((IConvertableTextFormat)File).TextFileType == TextFileType.Yaml)
editor.IsYAML = true;
}
else
editor.FillEditor(ArchiveFileInfo.FileData);
}
private void NotifyFormatSwitched()
{
}
private void SaveTextFormat()
{
}
private bool IsConvertableText(Type type)
{
return typeof(IConvertableTextFormat).IsAssignableFrom(type);
}
2019-06-18 22:27:36 +02: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);
}
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 22:27:36 +02:00
byte[] Data = ArchiveFileInfo.FileData;
//Only load a certain about of bytes to prevent memory dispose issues
editor.LoadData(Utils.SubArray(Data, 0, 3000));
}
public void AddControl(Control control)
{
2019-06-17 02:40:32 +02:00
foreach (var child in stPanel1.Controls)
if (child is STUserControl)
((STUserControl)child).OnControlClosing();
stPanel1.Controls.Clear();
stPanel1.Controls.Add(control);
}
private void stComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
2019-06-18 22:27:36 +02:00
if (_IsLoaded && stComboBox1.SelectedIndex != -1)
{
Runtime.ObjectEditor.EditorDiplayIndex = stComboBox1.SelectedIndex;
UpdateEditor();
2019-06-18 22:27:36 +02:00
}
}
}
}