public static bool ReadTexturePatterns(ThingType thing, ClientFeatures features, BinaryReader reader) { bool patternZEnabled = (features & ClientFeatures.PatternZ) == ClientFeatures.PatternZ; bool extendedEnabled = (features & ClientFeatures.Extended) == ClientFeatures.Extended; bool frameDurationsEnabled = (features & ClientFeatures.FrameDurations) == ClientFeatures.FrameDurations; bool frameGroupsEnabled = (features & ClientFeatures.FrameGroups) == ClientFeatures.FrameGroups; byte groupCount = 1; if (frameGroupsEnabled && thing.Category == ThingCategory.Outfit) { groupCount = reader.ReadByte(); } for (byte k = 0; k < groupCount; k++) { FrameGroupType groupType = FrameGroupType.Default; if (frameGroupsEnabled && thing.Category == ThingCategory.Outfit) { groupType = (FrameGroupType)reader.ReadByte(); } FrameGroup group = new FrameGroup(); group.Width = reader.ReadByte(); group.Height = reader.ReadByte(); if (group.Width > 1 || group.Height > 1) { group.ExactSize = reader.ReadByte(); } else { group.ExactSize = Sprite.DefaultSize; } group.Layers = reader.ReadByte(); group.PatternX = reader.ReadByte(); group.PatternY = reader.ReadByte(); group.PatternZ = patternZEnabled ? reader.ReadByte() : (byte)1; group.Frames = reader.ReadByte(); if (frameDurationsEnabled && group.Frames > 1) { group.IsAnimation = true; group.AnimationMode = (AnimationMode)reader.ReadByte(); group.LoopCount = reader.ReadInt32(); group.StartFrame = reader.ReadSByte(); group.FrameDurations = new FrameDuration[group.Frames]; for (int i = 0; i < group.Frames; i++) { uint minimum = reader.ReadUInt32(); uint maximum = reader.ReadUInt32(); group.FrameDurations[i] = new FrameDuration(minimum, maximum); } } int totalSprites = group.GetTotalSprites(); if (totalSprites > 4096) { throw new Exception("A thing type has more than 4096 sprites."); } group.SpriteIDs = new uint[totalSprites]; if (extendedEnabled) { for (int i = 0; i < totalSprites; i++) { group.SpriteIDs[i] = reader.ReadUInt32(); } } else { for (int i = 0; i < totalSprites; i++) { group.SpriteIDs[i] = reader.ReadUInt16(); } } thing.SetFrameGroup(groupType, group); } return true; }
private static ThingData DecodeV1(BinaryReader reader) { reader.BaseStream.Position = 0; Console.WriteLine(reader.ReadUInt16()); ushort nameLength = reader.ReadUInt16(); byte[] buffer = reader.ReadBytes(nameLength); string categoryStr = Encoding.UTF8.GetString(buffer, 0, buffer.Length); ThingCategory category = ThingCategory.Invalid; switch (categoryStr) { case "item": category = ThingCategory.Item; break; case "outfit": category = ThingCategory.Outfit; break; case "effect": category = ThingCategory.Effect; break; case "missile": category = ThingCategory.Missile; break; } ThingType thing = new ThingType(category); if (!ThingTypeSerializer.ReadProperties(thing, DatFormat.Format_1010, reader)) { return null; } FrameGroup group = new FrameGroup(); group.Width = reader.ReadByte(); group.Height = reader.ReadByte(); if (group.Width > 1 || group.Height > 1) { group.ExactSize = reader.ReadByte(); } else { group.ExactSize = Sprite.DefaultSize; } group.Layers = reader.ReadByte(); group.PatternX = reader.ReadByte(); group.PatternY = reader.ReadByte(); group.PatternZ = reader.ReadByte(); group.Frames = reader.ReadByte(); if (group.Frames > 1) { group.IsAnimation = true; group.AnimationMode = AnimationMode.Asynchronous; group.LoopCount = 0; group.StartFrame = 0; group.FrameDurations = new FrameDuration[group.Frames]; for (byte i = 0; i < group.Frames; i++) { group.FrameDurations[i] = new FrameDuration(category); } } int totalSprites = group.GetTotalSprites(); if (totalSprites > 4096) { throw new Exception("The ThingData has more than 4096 sprites."); } group.SpriteIDs = new uint[totalSprites]; SpriteGroup spriteGroup = new SpriteGroup(); Sprite[] sprites = new Sprite[totalSprites]; for (int i = 0; i < totalSprites; i++) { uint spriteID = reader.ReadUInt32(); group.SpriteIDs[i] = spriteID; uint dataSize = reader.ReadUInt32(); if (dataSize > Sprite.PixelsDataSize) { throw new Exception("Invalid sprite data size."); } byte[] pixels = reader.ReadBytes((int)dataSize); Sprite sprite = new Sprite(spriteID, true); sprite.SetPixelsARGB(pixels); sprites[i] = sprite; } thing.SetFrameGroup(FrameGroupType.Default, group); spriteGroup.Add(FrameGroupType.Default, sprites); return new ThingData(thing, spriteGroup); }