public TagArchiveObjectViewNodeSource(string name, GameDefinition game, IDocumentManager documentManager, DisplayItems items, DefaultState defaultState, bool loadDependencies) : base(name) { this.game = game; this.items = items; this.loadDependencies = loadDependencies; Library.FileAdded += new EventHandler <LibraryFileActionArgs>(Library_FileAdded); if ((items & DisplayItems.AllExtractedItems) > 0) { AddNodeType(CreateNodeType <ObjectViewRootNodeType>(defaultState)); HLTGroupNodeType groups = CreateNodeType <HLTGroupNodeType>( new DefaultState(Resources.book_open16, Resources.book_blue16, Color.Black, Color.White)); AddNodeType(groups); HLTNodeType htlType = CreateNodeType <HLTNodeType>(Resources.data16); htlType.AddNodeState( new DocumentOpenState(documentManager, game.GameID, TagLocation.Archive, Resources.data_view16, Resources.data_view16, Color.Blue, Color.White)); AddNodeType(htlType); TagNodeType tagType = CreateNodeType <TagNodeType>(Resources.document_up16); tagType.AddNodeState( new DocumentOpenState(documentManager, game.GameID, TagLocation.Archive, Resources.document_view16, Resources.document_view16, Color.Blue, Color.White)); AddNodeType(tagType); } if ((items & DisplayItems.AllUnextractedItems) > 0) { AddNodeType(CreateNodeType <UnextractedObjectViewRootNodeType>(defaultState)); UnextractedHLTGroupNodeType unextractedHLTGroup = CreateNodeType <UnextractedHLTGroupNodeType>( new DefaultState(Resources.book_open16, Resources.book_blue16, Color.Gray, Color.White)); AddNodeType(unextractedHLTGroup); UnextractedHLTNodeType unextractedHLT = CreateNodeType <UnextractedHLTNodeType>( new DefaultState(Resources.data16, Resources.data16, Color.Gray, Color.White)); AddNodeType(unextractedHLT); } }
/// <summary> /// Reads a single tag node in a NBT reader stream. /// </summary> /// <param name="inRead">The binary reader in which the NBT is found.</param> /// <param name="tagType">The type of data to be read.</param> /// <returns>Returns an object that corresponds to its TAG_TYPE (ex: int for TAG_INT, short for TAG_SHORT, etc.)</returns> public static dynamic Read(BinaryReader inRead, TagNodeType tagType) { /* This method will read the payload of a tag node depending on the TAG_TYPE of the node. * * That is why this method returns a "dynamic" object because the final data type of the * node will only be known during run-time. */ try { // read the NBT stream depending on the tagType of the node switch (tagType) { case TagNodeType.TAG_END: { return 0; } case TagNodeType.TAG_BYTE: { return inRead.ReadByte(); } case TagNodeType.TAG_BYTEA: { return inRead.ReadBytes(Read(inRead, TagNodeType.TAG_INT)); } case TagNodeType.TAG_SHORT: { return EndianConverter.SwapInt16(inRead.ReadInt16()); } case TagNodeType.TAG_INT: { return EndianConverter.SwapInt32(inRead.ReadInt32()); } case TagNodeType.TAG_LONG: { return EndianConverter.SwapInt64(inRead.ReadInt64()); } case TagNodeType.TAG_SINGLE: { return EndianConverter.SwapSingle(inRead.ReadSingle()); } case TagNodeType.TAG_DOUBLE: { return EndianConverter.SwapDouble(inRead.ReadDouble()); } case TagNodeType.TAG_STRING: { return Encoding.UTF8.GetString(inRead.ReadBytes(Read(inRead, TagNodeType.TAG_SHORT))); } default: { throw new NotSupportedException("Tag type is not supported by this reader!"); } } } catch (Exception ex) { Console.WriteLine("An error occurred while reading the tag.\n\n"); Console.WriteLine("Exception message: {0}\n\n", ex.Message); Console.WriteLine("Stack trace:\n\n{0}", ex.StackTrace); return null; } }
private INBTTag readTagPlod(TagNodeType type, string name) { /* There are 3 types of nodes in a NBT file, namely TAG_LIST, TAG_COMPOUND and the base TAG_TYPEs. * * The difference between the data types and TAG_LIST and TAG_COMPOUND is both TAG_LIST and TAG_COMPOUND requires * additional reading methodology to effectively read the whole file. * * First, on a TAG_LIST container type, the nodes are sequentially read WITHOUT the name tag because virtually, it is a * custom data type array. * * Unlike TAG_LISTs, a TAG_COMPOUND container type requires the nodes to be read again by readTagHead() for n times * (listing will only stop if it were to see a TAG_END node) because this container type contains heterogeneous * mix of primitive data types. * * Lastly, if it is a base type data node, it will be directly read by the Read(BinaryReader, TagNodeType) method. * * In a nutshell, this method will read the value (payload) of a node depending on the type of the node. */ // check the tag type of the node switch (type) { // type is a TAG_LIST case TagNodeType.TAG_LIST: { // get the common TAG_TYPE of the list byte _tagType = NBTReader.Read(this._bRead, TagNodeType.TAG_BYTE); // then get the total number of items stored in that list int _tagCout = NBTReader.Read(this._bRead, TagNodeType.TAG_INT); // after getting those values, create a TagNodeList (basically a List) that will // hold the succeeding tag values. TagNodeList _assetsList = new TagNodeList(name, (TagNodeType)_tagType); // loop-it according to the total count of the list for (int i = 0; i < _tagCout; i++) { // read the data then immediately add it on the list _assetsList.Add((INBTTag)readTagPlod((TagNodeType)_tagType, "")); } // finally, return _assetsList to the parent method return _assetsList; } // type is a TAG_COMPOUND case TagNodeType.TAG_COMPOUND: { // create a TagNodeList (basically a Dictionary) that will hold the succeeding tag values. TagNodeListNamed _assetsMaps = new TagNodeListNamed(name); // yes, this is an intentional infinite loop >:) do { // read a tag node INBTTag _nodeMap = readTagHead(); // if tag node is not TAG_END, meaning there is more to add if (_nodeMap.Type != TagNodeType.TAG_END) { // add the _nodeMap into the list _assetsMaps.Add(_nodeMap.Name, _nodeMap); } // otherwise else { // break the loop *\o/* break; } } while (true); // return the list containing the newly read nodes to the parent method return _assetsMaps; } // tag is a primitive data type default: { // read the node according to the type of the node (the method Read() will handle the payload processing) return new TagNode(type, name, Read(this._bRead, type)); } } }
/// <summary> /// Creates a new tag node. /// </summary> /// <param name="type">The TAG_TYPE of the node.</param> /// <param name="name">The name of the node.</param> /// <param name="payload">The value (payload) of the node.</param> public TagNode(TagNodeType type, string name, dynamic payload) { this._type = type; this._name = name; this._payload = payload; }
private TagNode(Tag tag, TagNodeType nodeType) { Tag = tag; NodeType = nodeType; }
public static void WritePayload(BinaryWriter bWrite, dynamic payload, TagNodeType type) { switch (type) { case TagNodeType.TAG_END: { bWrite.Write(0); } break; case TagNodeType.TAG_BYTE: { bWrite.Write((byte)payload); } break; case TagNodeType.TAG_BYTEA: { WritePayload(bWrite, ((byte[])payload).Length, TagNodeType.TAG_INT); for (int i = 0; i < ((byte[])payload).Length; i++) { WritePayload(bWrite, ((byte[])payload)[i], TagNodeType.TAG_BYTE); } } break; case TagNodeType.TAG_SHORT: { bWrite.Write(EndianConverter.SwapInt16((short)payload)); } break; case TagNodeType.TAG_INT: { bWrite.Write(EndianConverter.SwapInt32((int)payload)); } break; case TagNodeType.TAG_LONG: { bWrite.Write(EndianConverter.SwapInt64((long)payload)); } break; case TagNodeType.TAG_SINGLE: { bWrite.Write(EndianConverter.SwapSingle((float)payload)); } break; case TagNodeType.TAG_DOUBLE: { bWrite.Write(EndianConverter.SwapDouble((double)payload)); } break; case TagNodeType.TAG_STRING: { WritePayload(bWrite, ((string)payload).Length, TagNodeType.TAG_SHORT); byte[] _outString = Encoding.UTF8.GetBytes((string)payload); for (int i = 0; i < ((string)payload).Length; i++) WritePayload(bWrite, _outString[i], TagNodeType.TAG_BYTE); } break; default: { throw new NotSupportedException("Tag type is not supported by this writer!"); } } }
/// <summary> /// Resets node info. /// </summary> public void Reset() { Type = TagNodeType.Unassigned; Text.Reset(); Event.Reset(); }