public IEnumerable <byte> Encode() { var encodedScripts = Scripts.Encode(); var encodedStrings = Strings.ConvertAll(s => FF8String.Encode(s)); uint subSections = 3; uint headerLength = (subSections + 1) * 4; uint aiLength = (uint)encodedScripts.Count(); uint textIndexLength = (uint)Strings.Count * 2; uint textLength = (uint)encodedStrings.Sum(s => s.Count()); uint totalLength = headerLength + aiLength + textIndexLength + textLength; totalLength += 4 - (totalLength % 4); uint aiOffset = headerLength; uint textIndexOffset = aiOffset + aiLength; uint textOffset = textIndexOffset + textIndexLength; var result = new byte[totalLength]; using (var stream = new MemoryStream(result)) using (var writer = new BinaryWriter(stream)) { writer.Write(subSections); writer.Write(aiOffset); writer.Write(textIndexOffset); writer.Write(textOffset); writer.Write(encodedScripts); ushort offset = 0; for (int i = 0; i < encodedStrings.Count; i++) { writer.Write(offset); offset += (ushort)encodedStrings[i].Count(); } for (int i = 0; i < encodedStrings.Count; i++) { writer.Write(encodedStrings[i]); } } return(result); }