Example #1
0
        // P5's NAME.TBL Expandable support
        private static List <NameSection> GetNameSections(string tbl)
        {
            List <NameSection> sections = new List <NameSection>();

            byte[]      tblBytes = File.ReadAllBytes(tbl);
            int         pos      = 0;
            NameSection section;

            // 33 sections
            for (int i = 0; i <= 16; i++)
            {
                section = new NameSection();
                // Get big endian section size
                section.pointersSize = BitConverter.ToInt32(SliceArray(tblBytes, pos, pos + 4).Reverse().ToArray(), 0);

                // Get pointers
                byte[] segment = SliceArray(tblBytes, pos + 4, pos + 4 + section.pointersSize);
                section.pointers = new List <UInt16>();
                for (int j = 0; j < segment.Length; j += 2)
                {
                    section.pointers.Add(BitConverter.ToUInt16(SliceArray(segment, j, j + 2).Reverse().ToArray(), 0));
                }

                // Get to name section
                pos += section.pointersSize + 4;
                if ((pos % 16) != 0)
                {
                    pos += 16 - (pos % 16);
                }

                // Get big endian section size
                section.namesSize = BitConverter.ToInt32(SliceArray(tblBytes, pos, pos + 4).Reverse().ToArray(), 0);

                // Get names
                segment       = SliceArray(tblBytes, pos + 4, pos + 4 + section.namesSize);
                section.names = new List <byte[]>();
                List <byte> name = new List <byte>();
                foreach (var segmentByte in segment)
                {
                    if (segmentByte == (byte)0)
                    {
                        section.names.Add(name.ToArray());
                        name = new List <byte>();
                    }
                    else
                    {
                        name.Add(segmentByte);
                    }
                }

                // Get to next section
                pos += section.namesSize + 4;
                if ((pos % 16) != 0)
                {
                    pos += 16 - (pos % 16);
                }
                sections.Add(section);
            }
            return(sections);
        }
Example #2
0
 /// <summary>
 /// Reads the custom section with the given header.
 /// </summary>
 /// <param name="Header">The section header.</param>
 /// <returns>The parsed section.</returns>
 protected virtual Section ReadCustomSectionPayload(SectionHeader Header)
 {
     if (Header.Name.CustomName == NameSection.CustomName)
     {
         return(NameSection.ReadSectionPayload(Header, this));
     }
     else
     {
         return(new CustomSection(
                    Header.Name.CustomName,
                    ReadBytes((int)Header.PayloadLength)));
     }
 }
Example #3
0
        static void Main()
        {
            // Module can be used to create, read, modify, and write WebAssembly files.

            SourceMapParser parser = new SourceMapParser();
            SourceMap       sourceMap;

            using (FileStream stream = new FileStream(@"../../fac.wasm.map", FileMode.Open))
            {
                sourceMap = parser.ParseSourceMap(new StreamReader(stream));
            }

            var parsedMapping = sourceMap.ParsedMappings;

            var module = Module.ReadFromBinary("../../fac.wasm");

            var nameSection = module.CustomSections.FirstOrDefault(cs => cs.Name == "name");

            var NameSection = new NameSection(nameSection);

            var actorScriptSection = module.CustomSections.FirstOrDefault(cs => cs.Name == "actorScript");

            ActorScript.Meta.actorScriptSection = new ActorScriptSection(actorScriptSection);

            // We now have enough for a usable WASM file, which we could save with module.WriteToBinary().
            // Below, we show how the Compile feature can be used for .NET-based execution.
            // For stream-based compilation, WebAssembly.Compile should be used.
            // var instanceCreator = module.Compile<Sample>();
            var instanceCreator = Compile.FromBinary <Sample>(@"../../fac.wasm");

            // Instances should be wrapped in a "using" block for automatic disposal.
            using (var instance = instanceCreator())
            {
                // FYI, instanceCreator can be used multiple times to create independent instances.
                //Console.WriteLine(instance.Exports.Fac(1));// Binary 0, result 0
            } // Automatically release the WebAssembly instance here.
        }
Example #4
0
            private void _read()
            {
                _sectionType = System.Text.Encoding.GetEncoding("ASCII").GetString(m_io.ReadBytes(4));
                _lenBody     = m_io.ReadU4be();
                if (SectionType != "DATA")
                {
                    switch (SectionType)
                    {
                    case "MAXH": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new MaxhSection(io___raw_body, this, m_root);
                        break;
                    }

                    case "FAMI": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new FamiSection(io___raw_body, this, m_root);
                        break;
                    }

                    case "PTSZ": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new PtszSection(io___raw_body, this, m_root);
                        break;
                    }

                    case "MAXW": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new MaxwSection(io___raw_body, this, m_root);
                        break;
                    }

                    case "SLAN": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new SlanSection(io___raw_body, this, m_root);
                        break;
                    }

                    case "WEIG": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new WeigSection(io___raw_body, this, m_root);
                        break;
                    }

                    case "CHIX": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new ChixSection(io___raw_body, this, m_root);
                        break;
                    }

                    case "DESC": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new DescSection(io___raw_body, this, m_root);
                        break;
                    }

                    case "NAME": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new NameSection(io___raw_body, this, m_root);
                        break;
                    }

                    case "ASCE": {
                        __raw_body = m_io.ReadBytes(LenBody);
                        var io___raw_body = new KaitaiStream(__raw_body);
                        _body = new AsceSection(io___raw_body, this, m_root);
                        break;
                    }

                    default: {
                        _body = m_io.ReadBytes(LenBody);
                        break;
                    }
                    }
                }
            }