Esempio n. 1
0
        public 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.Length;
            uint textIndexLength = (uint)Strings.Count * 2;
            uint textLength      = (uint)encodedStrings.Sum(s => s.Length);
            uint totalLength     = headerLength + aiLength + textIndexLength + textLength;

            totalLength += 4 - (totalLength % 4);

            uint aiOffset        = headerLength;
            uint textIndexOffset = aiOffset + aiLength;
            uint textOffset      = textIndexOffset + textIndexLength;

            byte[] 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].Length;
                    }

                    for (int i = 0; i < encodedStrings.Count; i++)
                    {
                        writer.Write(encodedStrings[i]);
                    }
                }

            return(result);
        }
        public void EncodingTest(byte opcode, short[] args)
        {
            // construct a script with a populated init sub-script & run it through the encoder
            var script = new BattleScript();

            script.Init.Insert(0, new Instruction(Instruction.OpCodes[opcode], args));
            script = new BattleScript(script.Encode());

            // added instruction should still be intact
            Assert.NotNull(script.Init);
            Assert.NotEmpty(script.Init);
            Assert.Equal(opcode, script.Init[0].Op.Code);
            Assert.Equal(args.Length, script.Init[0].Args.Length);
            for (int i = 0; i < args.Length; i++)
            {
                Assert.Equal(args[i], script.Init[0].Args[i]);
            }
            Assert.Equal(0, script.Init[1].Op.Code);

            // everything else should be the same as the empty script
            Assert.NotNull(script.Execute);
            Assert.NotEmpty(script.Execute);
            Assert.Equal(0, script.Execute[0].Op.Code);

            Assert.NotNull(script.Counter);
            Assert.NotEmpty(script.Counter);
            Assert.Equal(0, script.Counter[0].Op.Code);

            Assert.NotNull(script.Death);
            Assert.NotEmpty(script.Death);
            Assert.Equal(0, script.Death[0].Op.Code);

            Assert.NotNull(script.PreCounter);
            Assert.NotEmpty(script.PreCounter);
            Assert.Equal(0, script.PreCounter[0].Op.Code);
        }