static XMBFile from_text(string filepath) { var file = new XMBFile(); using (var reader = File.OpenText(filepath)) { List <XMBEntry> tmp = new List <XMBEntry>(); int index = 0; while (!reader.EndOfStream) { var line = reader.ReadLine().Trim(); if (string.IsNullOrEmpty(line)) { continue; } var entry = new XMBEntry(); entry.Name = line.TrimEnd('{'); entry.ParentIndex = -1; entry.Index = index; if (line.EndsWith("{") | reader.ReadLine().EndsWith("{")) { bool endScope = false; while (!endScope) { line = reader.ReadLine().Trim(); if (string.IsNullOrEmpty(line) | line.EndsWith("{")) { continue; } if (line.Contains('=')) { entry.Expressions.Add(line.Trim()); } else if (line.EndsWith("}")) { endScope = true; continue; } else { var child = parse_entry(reader, line, ref index); child.ParentIndex = (short)entry.Index; entry.Children.Add(child); } } } file.Entries.Add(entry); } } return(file); }
static XMBEntry parse_entry(StreamReader reader, string name, ref int index) { index++; int index2 = -1; // children indexes are relative to parent var ret = new XMBEntry(); ret.Index = index; ret.Name = name; bool endScope = false; while (!endScope) { var line = reader.ReadLine().Trim(); if (string.IsNullOrEmpty(line) | line.EndsWith("{")) { continue; } if (line.Contains('=')) { ret.Expressions.Add(line.Trim()); } else if (line.EndsWith("}")) { endScope = true; continue; } else { var child = parse_entry(reader, line, ref index2); child.ParentIndex = (short)ret.Index; ret.Children.Add(child); } } return(ret); }
static void parseXMB(string filename) { List <XMBEntry> temp = new List <XMBEntry>(); using (var stream = File.Open(filename, FileMode.Open)) { using (var reader = new BinaryReader(stream)) { stream.Seek(4, SeekOrigin.Begin); count1 = reader.ReadBint32(); count2 = reader.ReadBint32(); count3 = reader.ReadBint32(); count4 = reader.ReadBint32(); strOffsets = reader.ReadBint32(); entriesTable = reader.ReadBint32(); fieldsTable = reader.ReadBint32(); extraEntry = reader.ReadBint32(); strTable1 = reader.ReadBint32(); strTable2 = reader.ReadBint32(); for (int i = 0; i < count3; i++) { stream.Seek(strOffsets + i * 4, SeekOrigin.Begin); var stroff = reader.ReadBint32(); stream.Seek(strTable1 + stroff, SeekOrigin.Begin); strings1.Add(reader.ReadStringNT()); } for (int i = 0; i < count1; i++) { stream.Seek(entriesTable + i * 0x10, SeekOrigin.Begin); var entry = new XMBEntry(); entry.NameOffset = reader.ReadBint32(); entry.NumProperties = reader.ReadBuint16(); entry.NumChildren = reader.ReadBuint16(); entry.FirstPropertyIndex = reader.ReadBuint16(); entry.unk1 = reader.ReadBuint16(); entry.ParentIndex = reader.ReadBint16(); entry.unk2 = reader.ReadBuint16(); stream.Seek(strTable1 + entry.NameOffset, SeekOrigin.Begin); entry.Name = reader.ReadStringNT(); temp.Add(entry); } for (int i = 0; i < count2; i++) { stream.Seek(fieldsTable + i * 8, SeekOrigin.Begin); var stroff1 = reader.ReadBint32(); var stroff2 = reader.ReadBint32(); var str = ""; stream.Seek(strTable1 + stroff1, SeekOrigin.Begin); str += $"{reader.ReadStringNT()} = "; stream.Seek(strTable2 + stroff2, SeekOrigin.Begin); str += $"{reader.ReadStringNT()}"; expressions.Add(str); } for (int x = 0; x < temp.Count; x++) { var entry = temp[x]; if (entry.NumProperties > 0) { for (int i = 0; i < entry.NumProperties; i++) { entry.Expressions.Add(expressions[entry.FirstPropertyIndex + i]); } } if (entry.ParentIndex != -1) { for (int i = 0; i < temp[entry.ParentIndex + i].NumChildren; i++) { entry.depth = temp[entry.ParentIndex + i].depth + 1; // for indent stuff and things temp[entry.ParentIndex + i].Children.Add(entry); } } else { entries.Add(entry); } } } } }