Exemple #1
0
        public int ReadSizeT()
        {
            if (SizeOfSizeT <= 0)
            {
                throw new Exception("sizeof(size_t) is not valid:" + SizeOfSizeT);
            }

            var    bytes = ReadBytes(SizeOfSizeT);
            UInt64 ret;

            switch (SizeOfSizeT)
            {
            case 4:
                ret = BitConverter.ToUInt32(bytes, 0);
                break;

            case 8:
                ret = BitConverter.ToUInt64(bytes, 0);
                break;

            default:
                throw new NotImplementedException();
            }

#if DEBUG_BINARY_READER
            ULDebug.Log("ReadSizeT: " + ret);
#endif

            if (ret > Int32.MaxValue)
            {
                throw new NotImplementedException();
            }

            return((int)ret);
        }
Exemple #2
0
        private void LoadConstants(LuaProto proto)
        {
            var n = LoadInt();

#if DEBUG_UNDUMP
            ULDebug.Log("Load Constants:" + n);
#endif
            proto.K.Clear();
            for (int i = 0; i < n; ++i)
            {
                int t = (int)LoadByte();
#if DEBUG_UNDUMP
                ULDebug.Log("Constant Type:" + t);
#endif
                var v = new StkId();
                switch (t)
                {
                case (int)LuaType.LUA_TNIL:
                    v.V.SetNilValue();
                    proto.K.Add(v);
                    break;

                case (int)LuaType.LUA_TBOOLEAN:
                    v.V.SetBValue(LoadBoolean());
                    proto.K.Add(v);
                    break;

                case (int)LuaType.LUA_TNUMBER:
                    v.V.SetNValue(LoadNumber());
                    proto.K.Add(v);
                    break;

                case (int)LuaType.LUA_TSTRING:
#if DEBUG_UNDUMP
                    ULDebug.Log("LuaType.LUA_TSTRING");
#endif
                    v.V.SetSValue(LoadString());
                    proto.K.Add(v);
                    break;

                default:
                    throw new UndumpException(
                              "LoadConstants unknown type: " + t);
                }
            }

            n = LoadInt();
#if DEBUG_UNDUMP
            ULDebug.Log("Load Functions:" + n);
#endif
            proto.P.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.P.Add(LoadFunction());
            }
        }
Exemple #3
0
        public uint ReadUInt()
        {
            var  bytes = ReadBytes(4);
            uint ret   = BitConverter.ToUInt32(bytes, 0);

#if DEBUG_BINARY_READER
            ULDebug.Log("ReadUInt: " + ret);
#endif
            return(ret);
        }
Exemple #4
0
        public double ReadDouble()
        {
            var    bytes = ReadBytes(8);
            double ret   = BitConverter.ToDouble(bytes, 0);

#if DEBUG_BINARY_READER
            ULDebug.Log("ReadDouble: " + ret);
#endif
            return(ret);
        }
        private LuaUpvalue F_FindUpval(StkId level)
        {
#if DEBUG_FIND_UPVALUE
            ULDebug.Log("[F_FindUpval] >>>>>>>>>>>>>>>>>>>> level:" + level);
#endif

            var node = OpenUpval.First;
            LinkedListNode <LuaUpvalue> prev = null;
            while (node != null)
            {
                var upval = node.Value;
#if DEBUG_FIND_UPVALUE
                ULDebug.Log("[F_FindUpval] >>>>>>>>>>>>>>>>>>>> upval.V:" + upval.V);
#endif
                if (upval.V.Index < level.Index)
                {
                    break;
                }

                var next = node.Next;
                if (upval.V == level)
                {
                    return(upval);
                }

                prev = node;
                node = next;
            }

            // not found: create a new one
            var ret = new LuaUpvalue();
            ret.V = level;
            // ret.Prev = G.UpvalHead;
            // ret.Next = G.UpvalHead.Next;
            // ret.Next.Prev = ret;
            // G.UpvalHead.Next = ret;

            if (prev == null)
            {
                OpenUpval.AddFirst(ret);
            }
            else
            {
                OpenUpval.AddAfter(prev, ret);
            }

#if DEBUG_FIND_UPVALUE
            ULDebug.Log("[F_FindUpval] >>>>>>>>>>>>>>>>>>>> create new one:" + ret.V);
#endif

            return(ret);
        }
Exemple #6
0
        public byte ReadByte()
        {
            var c = LoadInfo.ReadByte();

            if (c == -1)
            {
                throw new UndumpException("truncated");
            }
#if DEBUG_BINARY_READER
            ULDebug.Log("ReadBytes: " + c);
#endif
            return((byte)c);
        }
Exemple #7
0
        private void LoadHeader()
        {
            byte[] header = LoadBytes(4   // Signature
                                      + 8 // version, format version, size of int ... etc
                                      + 6 // Tail
                                      );
            byte v = header[4             /* skip signature */
                            + 4           /* offset of sizeof(size_t) */
                     ];

#if DEBUG_UNDUMP
            ULDebug.Log(string.Format("sizeof(size_t): {0}", v));
#endif
            Reader.SizeOfSizeT = v;
        }
Exemple #8
0
        private void LoadCode(LuaProto proto)
        {
            var n = LoadInt();

#if DEBUG_UNDUMP
            ULDebug.Log("LoadCode n:" + n);
#endif
            proto.Code.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.Code.Add(LoadInstruction());
#if DEBUG_UNDUMP
                ULDebug.Log("Count:" + proto.Code.Count);
                ULDebug.Log("LoadInstruction:" + proto.Code[proto.Code.Count - 1]);
#endif
            }
        }
Exemple #9
0
        private void LoadUpvalues(LuaProto proto)
        {
            var n = LoadInt();

#if DEBUG_UNDUMP
            ULDebug.Log("Load Upvalues:" + n);
#endif
            proto.Upvalues.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.Upvalues.Add(
                    new UpvalDesc()
                {
                    Name    = null,
                    InStack = LoadBoolean(),
                    Index   = (int)LoadByte()
                });
            }
        }
Exemple #10
0
        private LuaProto LoadFunction()
        {
#if DEBUG_UNDUMP
            ULDebug.Log("LoadFunction enter");
#endif

            LuaProto proto = new LuaProto();
            proto.LineDefined     = LoadInt();
            proto.LastLineDefined = LoadInt();
            proto.NumParams       = LoadByte();
            proto.IsVarArg        = LoadBoolean();
            proto.MaxStackSize    = LoadByte();

            LoadCode(proto);
            LoadConstants(proto);
            LoadUpvalues(proto);
            LoadDebug(proto);
            return(proto);
        }
Exemple #11
0
        private void LoadDebug(LuaProto proto)
        {
            int n;

            proto.Source = LoadString();

            // LineInfo
            n = LoadInt();
#if DEBUG_UNDUMP
            ULDebug.Log("Load LineInfo:" + n);
#endif
            proto.LineInfo.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.LineInfo.Add(LoadInt());
            }

            // LocalVar
            n = LoadInt();
#if DEBUG_UNDUMP
            ULDebug.Log("Load LocalVar:" + n);
#endif
            proto.LocVars.Clear();
            for (int i = 0; i < n; ++i)
            {
                proto.LocVars.Add(
                    new LocVar()
                {
                    VarName = LoadString(),
                    StartPc = LoadInt(),
                    EndPc   = LoadInt(),
                });
            }

            // Upvalues' name
            n = LoadInt();
            for (int i = 0; i < n; ++i)
            {
                proto.Upvalues[i].Name = LoadString();
            }
        }
Exemple #12
0
        public string ReadString()
        {
            var n = ReadSizeT();

            if (n == 0)
            {
                return(null);
            }

            var bytes = ReadBytes(n);

            // n=1: removing trailing '\0'
#if !UNITY_3 && !UNITY_4 && !UNITY_5
            string ret = System.Text.Encoding.UTF8.GetString(bytes, 0, n - 1);
#else
            string ret = PlatformTools.BS2String_UTF8(bytes, 0, n - 1);
#endif
#if DEBUG_BINARY_READER
            ULDebug.Log("ReadString n:" + n + " ret:" + ret);
#endif
            return(ret);
        }
Exemple #13
0
        public byte[] ReadBytes(int count)
        {
            byte[] ret = new byte[count];
            for (int i = 0; i < count; ++i)
            {
                var c = LoadInfo.ReadByte();
                if (c == -1)
                {
                    throw new UndumpException("truncated");
                }
                ret[i] = (byte)c;
            }
#if DEBUG_BINARY_READER
            var sb = new System.Text.StringBuilder();
            sb.Append("ReadBytes:");
            for (var i = 0; i < ret.Length; ++i)
            {
                sb.Append(string.Format(" {0:X02}", ret[i]));
            }
            ULDebug.Log(sb.ToString());
#endif
            return(ret);
        }