Пример #1
0
        public static VMObject Deserialize(byte[] data)
        {
            MemoryStream mstream = new MemoryStream();

            mstream.Write(data, 0, data.Length);
            mstream.Position = 0;
            BinaryReader mreader = new BinaryReader(mstream);
            double       typeid  = mreader.ReadDouble();
            //TODO: Decode the function variables
            int len = mreader.ReadInt32();
            Dictionary <int, FunctionDeclaration> tempdecl = new Dictionary <int, FunctionDeclaration>();

            for (int i = 0; i < len; i++)
            {
                FunctionDeclaration mdec = new FunctionDeclaration();
                int key = mreader.ReadInt32();

                int    serlen  = mreader.ReadInt32();
                byte[] serdata = mreader.ReadBytes(serlen);
                if (serdata[0] == 0)
                {
                    //Native method descriptor. TODO: Populate method info
                }
                else
                {
                    //XVAR method descriptor

                    byte[] xvardescriptor = new byte[serdata.Length - 1];
                    Buffer.BlockCopy(serdata, 1, xvardescriptor, 9, xvardescriptor.Length);
                    mdec.function = xvardescriptor;
                }
                tempdecl.Add(key, mdec);
            }
            //NExt::
            byte[] finaldata = new byte[mstream.Length - mstream.Position];
            mstream.Read(finaldata, 0, finaldata.Length);

            VMObject retval = (VMObject)types[typeid].GetConstructor(new Type[] { typeof(byte[]) }).Invoke(new object[] { finaldata });

            retval.functions = tempdecl;
            MethodInfo[] nativemethods = retval.GetType().GetMethods();
            foreach (KeyValuePair <int, FunctionDeclaration> e in retval.functions)
            {
                if (e.Value.function == null)
                {
                    //Hmm. Must be a somewhat 'inquisitive' native.
                    e.Value.nativeMethod = nativemethods[e.Key];
                }
            }
            return(retval);
        }