Splitting up files

This commit is contained in:
Justin Aquadro 2012-09-01 20:08:54 -04:00
parent 3e7e05bda9
commit 05bf9d6193
13 changed files with 306 additions and 263 deletions

View file

@ -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<DataNode> State { get; set; }
public Action<DataNode> DiscoverCallback { get; set; }
public Action<DataNode> ProgressCallback { get; set; }
public Action<DataNode> 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<DataNode> 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);
}
}
}

View file

@ -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"; }
}
}
}

26
Model/TagByteDataNode.cs Normal file
View file

@ -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(); }
}
}
}

View file

@ -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"; }
}
}
}

View file

@ -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);
}
}
}

16
Model/TagFloatDataNode.cs Normal file
View file

@ -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);
}
}
}

View file

@ -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"; }
}
}
}

16
Model/TagIntDataNode.cs Normal file
View file

@ -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);
}
}
}

16
Model/TagLongDataNode.cs Normal file
View file

@ -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);
}
}
}

16
Model/TagShortDataNode.cs Normal file
View file

@ -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);
}
}
}

View file

@ -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); }
}
}
}

View file

@ -98,12 +98,12 @@
<Compile Include="Forms\Find.Designer.cs">
<DependentUpon>Find.cs</DependentUpon>
</Compile>
<Compile Include="Form1.cs">
<None Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
</None>
<None Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
</None>
<Compile Include="Forms\EditHex.cs">
<SubType>Form</SubType>
</Compile>
@ -172,10 +172,10 @@
<EmbeddedResource Include="Forms\Find.resx">
<DependentUpon>Find.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Form1.resx">
<None Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
</None>
<EmbeddedResource Include="Forms\EditHex.resx">
<DependentUpon>EditHex.cs</DependentUpon>
</EmbeddedResource>

121
SearchWorker.cs Normal file
View file

@ -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<DataNode> State { get; set; }
public Action<DataNode> DiscoverCallback { get; set; }
public Action<DataNode> ProgressCallback { get; set; }
public Action<DataNode> 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<DataNode> 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);
}
}
}