diff --git a/MainForm.cs b/MainForm.cs index 1780b0f..d5df581 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -830,118 +830,4 @@ namespace NBTExplorer #endregion } - - internal class SearchState - { - public DataNode RootNode { get; set; } - public string SearchName { get; set; } - public string SearchValue { get; set; } - - public IEnumerator State { get; set; } - - public Action DiscoverCallback { get; set; } - public Action ProgressCallback { get; set; } - public Action CollapseCallback { get; set; } - public Action EndCallback { get; set; } - } - - internal class SearchWorker - { - private ContainerControl _sender; - private SearchState _state; - private bool _cancel; - private object _lock; - - public SearchWorker (SearchState state, ContainerControl sender) - { - _state = state; - _sender = sender; - _lock = new object(); - } - - public void Cancel () - { - lock (_lock) { - _cancel = true; - } - } - - public void Run () - { - if (_state.State == null) - _state.State = FindNode(_state.RootNode).GetEnumerator(); - - if (!_state.State.MoveNext()) - InvokeEndCallback(); - } - - private IEnumerable FindNode (DataNode node) - { - lock (_lock) { - if (_cancel) - yield break; - } - - if (node == null) - yield break; - - bool searchExpanded = false; - if (!node.IsExpanded) { - node.Expand(); - searchExpanded = true; - } - - TagDataNode tagNode = node as TagDataNode; - if (tagNode != null) { - bool mName = _state.SearchName == null; - bool mValue = _state.SearchValue == null; - - if (_state.SearchName != null) { - string tagName = node.NodeName; - if (tagName != null) - mName = tagName.Contains(_state.SearchName); - } - if (_state.SearchValue != null) { - string tagValue = node.NodeDisplay; - if (tagValue != null) - mValue = tagValue.Contains(_state.SearchValue); - } - - if (mName && mValue) { - InvokeDiscoverCallback(node); - yield return node; - } - } - - foreach (DataNode sub in node.Nodes) { - foreach (DataNode s in FindNode(sub)) - yield return s; - } - - if (searchExpanded) { - if (!node.IsModified) { - node.Collapse(); - InvokeCollapseCallback(node); - } - } - } - - private void InvokeDiscoverCallback (DataNode node) - { - if (_sender != null && _state.DiscoverCallback != null) - _sender.BeginInvoke(_state.DiscoverCallback, new object[] { node }); - } - - private void InvokeCollapseCallback (DataNode node) - { - if (_sender != null && _state.CollapseCallback != null) - _sender.BeginInvoke(_state.CollapseCallback, new object[] { node }); - } - - private void InvokeEndCallback () - { - if (_sender != null && _state.EndCallback != null) - _sender.BeginInvoke(_state.EndCallback); - } - } } diff --git a/Model/TagByteArrayDataNode.cs b/Model/TagByteArrayDataNode.cs new file mode 100644 index 0000000..e700f4f --- /dev/null +++ b/Model/TagByteArrayDataNode.cs @@ -0,0 +1,26 @@ +using Substrate.Nbt; + +namespace NBTExplorer.Model +{ + public class TagByteArrayDataNode : TagDataNode + { + public TagByteArrayDataNode (TagNodeByteArray tag) + : base(tag) + { } + + protected new TagNodeByteArray Tag + { + get { return base.Tag as TagNodeByteArray; } + } + + public override bool EditNode () + { + return EditByteHexValue(Tag); + } + + public override string NodeDisplay + { + get { return NodeDisplayPrefix + Tag.Data.Length + " bytes"; } + } + } +} diff --git a/Model/TagByteDataNode.cs b/Model/TagByteDataNode.cs new file mode 100644 index 0000000..cfc803a --- /dev/null +++ b/Model/TagByteDataNode.cs @@ -0,0 +1,26 @@ +using Substrate.Nbt; + +namespace NBTExplorer.Model +{ + public class TagByteDataNode : TagDataNode + { + public TagByteDataNode (TagNodeByte tag) + : base(tag) + { } + + protected new TagNodeByte Tag + { + get { return base.Tag as TagNodeByte; } + } + + public override bool EditNode () + { + return EditScalarValue(Tag); + } + + public override string NodeDisplay + { + get { return NodeDisplayPrefix + unchecked((sbyte)Tag.Data).ToString(); } + } + } +} diff --git a/Model/TagDataNode.cs b/Model/TagDataNode.cs index 6efaa07..b85a0c2 100644 --- a/Model/TagDataNode.cs +++ b/Model/TagDataNode.cs @@ -297,147 +297,4 @@ namespace NBTExplorer.Model return false; } } - - public class TagByteDataNode : TagDataNode - { - public TagByteDataNode (TagNodeByte tag) - : base(tag) - { } - - protected new TagNodeByte Tag - { - get { return base.Tag as TagNodeByte; } - } - - public override bool EditNode () - { - return EditScalarValue(Tag); - } - - public override string NodeDisplay - { - get { return NodeDisplayPrefix + unchecked((sbyte)Tag.Data).ToString(); } - } - } - - public class TagShortDataNode : TagDataNode - { - public TagShortDataNode (TagNodeShort tag) - : base(tag) - { } - - public override bool EditNode () - { - return EditScalarValue(Tag); - } - } - - public class TagIntDataNode : TagDataNode - { - public TagIntDataNode (TagNodeInt tag) - : base(tag) - { } - - public override bool EditNode () - { - return EditScalarValue(Tag); - } - } - - public class TagLongDataNode : TagDataNode - { - public TagLongDataNode (TagNodeLong tag) - : base(tag) - { } - - public override bool EditNode () - { - return EditScalarValue(Tag); - } - } - - public class TagFloatDataNode : TagDataNode - { - public TagFloatDataNode (TagNodeFloat tag) - : base(tag) - { } - - public override bool EditNode () - { - return EditScalarValue(Tag); - } - } - - public class TagDoubleDataNode : TagDataNode - { - public TagDoubleDataNode (TagNodeDouble tag) - : base(tag) - { } - - public override bool EditNode () - { - return EditScalarValue(Tag); - } - } - - public class TagStringDataNode : TagDataNode - { - public TagStringDataNode (TagNodeString tag) - : base(tag) - { } - - public override bool EditNode () - { - return EditStringValue(Tag); - } - - public override string NodeDisplay - { - get { return NodeDisplayPrefix + Tag.ToString().Replace('\n', (char)0x00B6); } - } - } - - public class TagByteArrayDataNode : TagDataNode - { - public TagByteArrayDataNode (TagNodeByteArray tag) - : base(tag) - { } - - protected new TagNodeByteArray Tag - { - get { return base.Tag as TagNodeByteArray; } - } - - public override bool EditNode () - { - return EditByteHexValue(Tag); - } - - public override string NodeDisplay - { - get { return NodeDisplayPrefix + Tag.Data.Length + " bytes"; } - } - } - - public class TagIntArrayDataNode : TagDataNode - { - public TagIntArrayDataNode (TagNodeIntArray tag) - : base(tag) - { } - - protected new TagNodeIntArray Tag - { - get { return base.Tag as TagNodeIntArray; } - } - - public override bool EditNode () - { - return EditIntHexValue(Tag); - } - - public override string NodeDisplay - { - get { return NodeDisplayPrefix + Tag.Data.Length + " integers"; } - } - } } diff --git a/Model/TagDoubleDataNode.cs b/Model/TagDoubleDataNode.cs new file mode 100644 index 0000000..e1fd990 --- /dev/null +++ b/Model/TagDoubleDataNode.cs @@ -0,0 +1,16 @@ +using Substrate.Nbt; + +namespace NBTExplorer.Model +{ + public class TagDoubleDataNode : TagDataNode + { + public TagDoubleDataNode (TagNodeDouble tag) + : base(tag) + { } + + public override bool EditNode () + { + return EditScalarValue(Tag); + } + } +} diff --git a/Model/TagFloatDataNode.cs b/Model/TagFloatDataNode.cs new file mode 100644 index 0000000..30c8946 --- /dev/null +++ b/Model/TagFloatDataNode.cs @@ -0,0 +1,16 @@ +using Substrate.Nbt; + +namespace NBTExplorer.Model +{ + public class TagFloatDataNode : TagDataNode + { + public TagFloatDataNode (TagNodeFloat tag) + : base(tag) + { } + + public override bool EditNode () + { + return EditScalarValue(Tag); + } + } +} diff --git a/Model/TagIntArrayDataNode.cs b/Model/TagIntArrayDataNode.cs new file mode 100644 index 0000000..0e59c28 --- /dev/null +++ b/Model/TagIntArrayDataNode.cs @@ -0,0 +1,26 @@ +using Substrate.Nbt; + +namespace NBTExplorer.Model +{ + public class TagIntArrayDataNode : TagDataNode + { + public TagIntArrayDataNode (TagNodeIntArray tag) + : base(tag) + { } + + protected new TagNodeIntArray Tag + { + get { return base.Tag as TagNodeIntArray; } + } + + public override bool EditNode () + { + return EditIntHexValue(Tag); + } + + public override string NodeDisplay + { + get { return NodeDisplayPrefix + Tag.Data.Length + " integers"; } + } + } +} diff --git a/Model/TagIntDataNode.cs b/Model/TagIntDataNode.cs new file mode 100644 index 0000000..2cda6c3 --- /dev/null +++ b/Model/TagIntDataNode.cs @@ -0,0 +1,16 @@ +using Substrate.Nbt; + +namespace NBTExplorer.Model +{ + public class TagIntDataNode : TagDataNode + { + public TagIntDataNode (TagNodeInt tag) + : base(tag) + { } + + public override bool EditNode () + { + return EditScalarValue(Tag); + } + } +} diff --git a/Model/TagLongDataNode.cs b/Model/TagLongDataNode.cs new file mode 100644 index 0000000..3051802 --- /dev/null +++ b/Model/TagLongDataNode.cs @@ -0,0 +1,16 @@ +using Substrate.Nbt; + +namespace NBTExplorer.Model +{ + public class TagLongDataNode : TagDataNode + { + public TagLongDataNode (TagNodeLong tag) + : base(tag) + { } + + public override bool EditNode () + { + return EditScalarValue(Tag); + } + } +} diff --git a/Model/TagShortDataNode.cs b/Model/TagShortDataNode.cs new file mode 100644 index 0000000..beff379 --- /dev/null +++ b/Model/TagShortDataNode.cs @@ -0,0 +1,16 @@ +using Substrate.Nbt; + +namespace NBTExplorer.Model +{ + public class TagShortDataNode : TagDataNode + { + public TagShortDataNode (TagNodeShort tag) + : base(tag) + { } + + public override bool EditNode () + { + return EditScalarValue(Tag); + } + } +} diff --git a/Model/TagStringDataNode.cs b/Model/TagStringDataNode.cs new file mode 100644 index 0000000..1624073 --- /dev/null +++ b/Model/TagStringDataNode.cs @@ -0,0 +1,21 @@ +using Substrate.Nbt; + +namespace NBTExplorer.Model +{ + public class TagStringDataNode : TagDataNode + { + public TagStringDataNode (TagNodeString tag) + : base(tag) + { } + + public override bool EditNode () + { + return EditStringValue(Tag); + } + + public override string NodeDisplay + { + get { return NodeDisplayPrefix + Tag.ToString().Replace('\n', (char)0x00B6); } + } + } +} diff --git a/NBTExplorer.csproj b/NBTExplorer.csproj index d503765..49c84e3 100644 --- a/NBTExplorer.csproj +++ b/NBTExplorer.csproj @@ -98,12 +98,12 @@ Find.cs - + Form - - + + Form1.cs - + Form @@ -172,10 +172,10 @@ Find.cs - + Form1.cs Designer - + EditHex.cs diff --git a/SearchWorker.cs b/SearchWorker.cs new file mode 100644 index 0000000..b337fe7 --- /dev/null +++ b/SearchWorker.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Windows.Forms; +using NBTExplorer.Model; + +namespace NBTExplorer +{ + internal class SearchState + { + public DataNode RootNode { get; set; } + public string SearchName { get; set; } + public string SearchValue { get; set; } + + public IEnumerator State { get; set; } + + public Action DiscoverCallback { get; set; } + public Action ProgressCallback { get; set; } + public Action CollapseCallback { get; set; } + public Action EndCallback { get; set; } + } + + internal class SearchWorker + { + private ContainerControl _sender; + private SearchState _state; + private bool _cancel; + private object _lock; + + public SearchWorker (SearchState state, ContainerControl sender) + { + _state = state; + _sender = sender; + _lock = new object(); + } + + public void Cancel () + { + lock (_lock) { + _cancel = true; + } + } + + public void Run () + { + if (_state.State == null) + _state.State = FindNode(_state.RootNode).GetEnumerator(); + + if (!_state.State.MoveNext()) + InvokeEndCallback(); + } + + private IEnumerable FindNode (DataNode node) + { + lock (_lock) { + if (_cancel) + yield break; + } + + if (node == null) + yield break; + + bool searchExpanded = false; + if (!node.IsExpanded) { + node.Expand(); + searchExpanded = true; + } + + TagDataNode tagNode = node as TagDataNode; + if (tagNode != null) { + bool mName = _state.SearchName == null; + bool mValue = _state.SearchValue == null; + + if (_state.SearchName != null) { + string tagName = node.NodeName; + if (tagName != null) + mName = tagName.Contains(_state.SearchName); + } + if (_state.SearchValue != null) { + string tagValue = node.NodeDisplay; + if (tagValue != null) + mValue = tagValue.Contains(_state.SearchValue); + } + + if (mName && mValue) { + InvokeDiscoverCallback(node); + yield return node; + } + } + + foreach (DataNode sub in node.Nodes) { + foreach (DataNode s in FindNode(sub)) + yield return s; + } + + if (searchExpanded) { + if (!node.IsModified) { + node.Collapse(); + InvokeCollapseCallback(node); + } + } + } + + private void InvokeDiscoverCallback (DataNode node) + { + if (_sender != null && _state.DiscoverCallback != null) + _sender.BeginInvoke(_state.DiscoverCallback, new object[] { node }); + } + + private void InvokeCollapseCallback (DataNode node) + { + if (_sender != null && _state.CollapseCallback != null) + _sender.BeginInvoke(_state.CollapseCallback, new object[] { node }); + } + + private void InvokeEndCallback () + { + if (_sender != null && _state.EndCallback != null) + _sender.BeginInvoke(_state.EndCallback); + } + } +}