public static TTC_File Parse(string path, bool writeXml) { TTC_File file = Parse(File.ReadAllBytes(path)); if (writeXml) { YAXSerializer serializer = new YAXSerializer(typeof(TTC_File)); serializer.SerializeToFile(file, path + ".xml"); } return(file); }
public static TTC_File Parse(byte[] bytes) { if (BitConverter.ToUInt32(bytes, 0) != TTC_SIGNATURE) { throw new InvalidDataException("TTC signature not found!"); } TTC_File ttc = new TTC_File(); int count = BitConverter.ToInt32(bytes, 8); int offset = BitConverter.ToInt32(bytes, 12) + 16; //Parse ttc entries for (int i = 0; i < count; i++) { TTC_Entry entry = new TTC_Entry(); entry.CmsID = BitConverter.ToInt32(bytes, offset + (16 * i) + 12); int numLists = BitConverter.ToInt32(bytes, offset + (16 * i) + 0); int dataStart = BitConverter.ToInt32(bytes, offset + (16 * i) + 4) + offset; int startIndex = BitConverter.ToInt32(bytes, offset + (16 * i) + 8); for (int a = 0; a < numLists; a++) { TTC_EventList eventList = new TTC_EventList(); eventList.Type = (TtcEventListType)BitConverter.ToInt32(bytes, dataStart + (16 * (startIndex + a)) + 12); int numEvents = BitConverter.ToInt32(bytes, dataStart + (16 * (startIndex + a)) + 0); int eventDataStart = BitConverter.ToInt32(bytes, dataStart + (16 * (startIndex + a)) + 4) + offset; int eventStartIndex = BitConverter.ToInt32(bytes, dataStart + (16 * (startIndex + a)) + 8); for (int b = 0; b < numEvents; b++) { TTC_Event _event = new TTC_Event(); _event.Costume = BitConverter.ToInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 4); _event.Transformation = BitConverter.ToInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 8); _event.Condition = (TtcEventCondition)BitConverter.ToInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 16); int nameIdx = BitConverter.ToInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 20); if (nameIdx != -1 && nameIdx < offset) { _event.Name = StringEx.GetString(bytes, nameIdx + 16, false); } //validation if (BitConverter.ToInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 0) != entry.CmsID) { throw new InvalidDataException("Warning: CMS ID mismatch!"); } if (BitConverter.ToInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 12) != (int)eventList.Type) { throw new InvalidDataException("Warning: EventList Type mismatch!"); } if (BitConverter.ToUInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 24) != 0xFFFFFFFF) { throw new InvalidDataException("Warning: unk_24 is not 0xFFFFFFFF!"); } if (BitConverter.ToUInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 28) != 0xFFFFFFFF) { throw new InvalidDataException("Warning: unk_28 is not 0xFFFFFFFF!"); } if (BitConverter.ToUInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 32) != 0xFFFFFFFF) { throw new InvalidDataException("Warning: unk_32 is not 0xFFFFFFFF!"); } if (BitConverter.ToUInt32(bytes, eventDataStart + (40 * (eventStartIndex + b)) + 36) != 0xFFFFFFFF) { throw new InvalidDataException("Warning: unk_36 is not 0xFFFFFFFF!"); } eventList.Events.Add(_event); } entry.EventLists.Add(eventList); } ttc.Entries.Add(entry); } return(ttc); }