From 9e0f3e05165a627ff9ad97eb02f52a45ab71e90f Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Sun, 16 Feb 2020 11:55:47 -0500 Subject: [PATCH] Some byaml improvements Add XML format to byaml text editor. Disable reference nodes in text editor for now till i find a better method to handle them. --- .../FileFormats/Byaml/YamlByamlConverter.cs | 11 ++-- .../FileFormats/Layout/BxlytToGL.cs | 1 - File_Format_Library/GUI/Byaml/ByamlEditor.cs | 52 ++++++++++++++++--- .../Forms/Editors/TextEditor/TextEditor.cs | 9 ++++ 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/File_Format_Library/FileFormats/Byaml/YamlByamlConverter.cs b/File_Format_Library/FileFormats/Byaml/YamlByamlConverter.cs index e1973af2..d0e2d367 100644 --- a/File_Format_Library/FileFormats/Byaml/YamlByamlConverter.cs +++ b/File_Format_Library/FileFormats/Byaml/YamlByamlConverter.cs @@ -180,7 +180,7 @@ namespace FirstPlugin if (node == null) { return new YamlScalarNode("null"); } - else if (NodePaths.ContainsKey(node)) + else if (IsReferenceNode(node)) { if (NodePaths[node].Tag == null) NodePaths[node].Tag = $"!ref{refNodeId++}"; @@ -189,7 +189,7 @@ namespace FirstPlugin else if ((node is IList)) { var yamlNode = new YamlSequenceNode(); - NodePaths.Add(node, yamlNode); + // NodePaths.Add(node, yamlNode); if (!HasEnumerables((IList)node) && ((IList)node).Count < 6) @@ -203,7 +203,7 @@ namespace FirstPlugin else if (node is IDictionary) { var yamlNode = new YamlMappingNode(); - NodePaths.Add(node, yamlNode); + // NodePaths.Add(node, yamlNode); if (!HasEnumerables((IDictionary)node) && ((IDictionary)node).Count < 6) @@ -227,6 +227,11 @@ namespace FirstPlugin } } + private static bool IsReferenceNode(dynamic node) + { + return false; + } + private static bool HasEnumerables(IDictionary node) { foreach (var item in node.Values) diff --git a/File_Format_Library/FileFormats/Layout/BxlytToGL.cs b/File_Format_Library/FileFormats/Layout/BxlytToGL.cs index 110bb479..5db418d5 100644 --- a/File_Format_Library/FileFormats/Layout/BxlytToGL.cs +++ b/File_Format_Library/FileFormats/Layout/BxlytToGL.cs @@ -633,7 +633,6 @@ namespace LayoutBXLYT //Huge thanks to layout studio for the window pane rendering code //https://github.com/Treeki/LayoutStudio/blob/master/layoutgl/widget.cpp - //Note i still need to fix UV coordinates being flips and transformed! public static void DrawWindowPane(BasePane pane, LayoutViewer.Camera2D camera, bool gameWindow, byte effectiveAlpha, Dictionary Textures, bool isSelected) { if (!Runtime.LayoutEditor.DisplayWindowPane) diff --git a/File_Format_Library/GUI/Byaml/ByamlEditor.cs b/File_Format_Library/GUI/Byaml/ByamlEditor.cs index 4d5b59d8..efd5cf1d 100644 --- a/File_Format_Library/GUI/Byaml/ByamlEditor.cs +++ b/File_Format_Library/GUI/Byaml/ByamlEditor.cs @@ -43,12 +43,20 @@ namespace FirstPlugin bool useMuunt = true; + bool IsXML => xmlToolstrip.Checked; + private TextEditor textEditor; + private STToolStipMenuItem xmlToolstrip; + private STToolStipMenuItem yamlToolstrip; public ByamlEditor() { InitializeComponent(); Reload(); + + xmlToolstrip = new STToolStipMenuItem("XML", null, OnFormatChanged); + yamlToolstrip = new STToolStipMenuItem("YAML", null, OnFormatChanged); + yamlToolstrip.Checked = true; } public ByamlEditor(System.Collections.IEnumerable by, bool _pathSupport, ushort _ver, ByteOrder defaultOrder = ByteOrder.LittleEndian, bool IsSaveDialog = false, BYAML byaml = null) @@ -104,9 +112,29 @@ namespace FirstPlugin textEditor.ClearContextMenus(); textEditor.AddContextMenu("Decompile", TextEditorToYaml); textEditor.AddContextMenu("Compile", TextEditorFromYaml); + + var formatMenu = new STToolStripItem("Change Formatting"); + formatMenu.DropDownItems.Add(xmlToolstrip); + formatMenu.DropDownItems.Add(yamlToolstrip); + + textEditor.AddContextMenu(formatMenu, TextEditorFromYaml); + stPanel4.Controls.Add(textEditor); } + private void OnFormatChanged(object sender, EventArgs e) + { + yamlToolstrip.Checked = false; + xmlToolstrip.Checked = false; + + var menu = sender as STToolStipMenuItem; + menu.Checked = true; + + if (textEditor.GetText() != string.Empty) { + UpdateTextEditor(); + } + } + void ParseBymlFirstNode() { TreeNode root = new TreeNode(FileName); @@ -719,15 +747,24 @@ namespace FirstPlugin } } - private void TextEditorToYaml(object sender, EventArgs e) - { - var format = (IConvertableTextFormat)FileFormat; - textEditor.FillEditor(((IConvertableTextFormat)FileFormat).ConvertToString()); + private void TextEditorToYaml(object sender, EventArgs e) { + UpdateTextEditor(); + } - if (format.TextFileType == TextFileType.Xml) + private void UpdateTextEditor() { + textEditor.IsXML = false; + textEditor.IsYAML = false; + + if (IsXML) + { + textEditor.FillEditor(XmlByamlConverter.ToXML(FileFormat.BymlData)); textEditor.IsXML = true; + } else + { + textEditor.FillEditor(YamlByamlConverter.ToYaml(FileFormat.BymlData)); textEditor.IsYAML = true; + } } private void TextEditorFromYaml(object sender, EventArgs e) @@ -739,7 +776,10 @@ namespace FirstPlugin try { if (FileFormat != null) { - FileFormat.ConvertFromString(textEditor.GetText()); + if (IsXML) + FileFormat.BymlData = XmlByamlConverter.FromXML(textEditor.GetText()); + else + FileFormat.BymlData = YamlByamlConverter.FromYaml(textEditor.GetText()); } } catch (Exception ex) diff --git a/Switch_Toolbox_Library/Forms/Editors/TextEditor/TextEditor.cs b/Switch_Toolbox_Library/Forms/Editors/TextEditor/TextEditor.cs index 6086e28c..0dc0ac71 100644 --- a/Switch_Toolbox_Library/Forms/Editors/TextEditor/TextEditor.cs +++ b/Switch_Toolbox_Library/Forms/Editors/TextEditor/TextEditor.cs @@ -196,6 +196,15 @@ namespace Toolbox.Library.Forms private Color BACK_COLOR = Color.FromArgb(30, 30, 30); private Color FORE_COLOR = Color.White; + public void AddContextMenu(STToolStripItem menu, EventHandler handler) + { + foreach (ToolStripItem item in stContextMenuStrip1.Items) + if (item.Text == menu.Text) + return; + + stContextMenuStrip1.Items.Add(menu); + } + public void AddContextMenu(string text, EventHandler handler) { foreach (ToolStripItem item in stContextMenuStrip1.Items)