Skip to content

Instantly share code, notes, and snippets.

@DylanLukes
Created August 25, 2010 04:35
Show Gist options
  • Save DylanLukes/548862 to your computer and use it in GitHub Desktop.
Save DylanLukes/548862 to your computer and use it in GitHub Desktop.
-(NBTag*)parseTag:(BOOL)named type:(int)type{
// If this tag is named, read a byte, otherwise, use the provided value.
int tagType = named?[self readByte]:type;
// If this is a TAG_End, go no further.
if (tagType == TAG_End) return [NBTEnd newTag];
// Read the name (if the tag is named)
// Assigning a nil value to the tag as its name will mark it as unnamed
// (see NBTag.m)
NSString* tagName = named?[self readPString]:nil;
// Create a tag (NBTag* could represent any of its subclass)
NBTag* parsedTag;
switch (tagType) {
case TAG_Byte:
parsedTag = [NBTByte newTagWithName:tagName];
[(NBTByte*)parsedTag setTagValue:[self readByte]];
break;
case TAG_Short:
parsedTag = [NBTShort newTagWithName:tagName];
[(NBTShort*)parsedTag setTagValue:[self readShort]];
break;
case TAG_Int:
parsedTag = [NBTInt newTagWithName:tagName];
[(NBTInt*)parsedTag setTagValue:[self readInt]];
break;
case TAG_Long:
parsedTag = [NBTLong newTagWithName:tagName];
[(NBTLong*)parsedTag setTagValue:[self readLong]];
break;
case TAG_Float:
parsedTag = [NBTFloat newTagWithName:tagName];
[(NBTFloat*)parsedTag setTagValue:[self readFloat]];
break;
case TAG_Double:
parsedTag = [NBTDouble newTagWithName:tagName];
[(NBTDouble*)parsedTag setTagValue:[self readDouble]];
break;
case TAG_String:
parsedTag = [NBTString newTagWithName:tagName];
[(NBTString*)parsedTag setTagValue:[self readPString]];
break;
case TAG_Byte_Array:
parsedTag = [NBTByteArray newTagWithName:tagName];
NSUInteger datalen = [self readInt];
[(NBTByteArray*)parsedTag setTagData:[self readBytes:datalen]];
break;
case TAG_List:
parsedTag = [NBTList newTagWithName:tagName];
[(NBTList*)parsedTag setChildType:[self readByte]];
int32_t childCount = [self readInt];
for(int i = 0; i < childCount; i++){
[(NBTList*)parsedTag addChild:[self parseUnnamedTag:[(NBTList*)parsedTag childType]]];
}
break;
case TAG_Compound:
parsedTag = [NBTCompound newTagWithName:tagName];
for(;;){
NBTag* childTag = [self parseNamedTag];
if ([childTag isKindOfClass:[NBTEnd class]]) break;
// Prevent the code from blowing up too much if there's an error.
[(NBTCompound*)parsedTag setTag:childTag forKey:[childTag name]];
}
break;
default:
parsedTag = nil;
break;
}
//NSLog(@"%@", parsedTag);
return parsedTag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment