Exemplo n.º 1
0
        private Amf3ClassDef ReadAmf3ClassDef(Amf3Object.Flags flags)
        {
            bool   externalizable = ((flags & Amf3Object.Flags.Externalizable) != 0);
            bool   dynamic        = ((flags & Amf3Object.Flags.Dynamic) != 0);
            string name           = ReadString();

            if (externalizable && dynamic)
            {
                throw new InvalidOperationException("Serialized objects cannot be both dynamic and externalizable");
            }

            List <string> properties = new List <string>();

            int members = ((int)flags) >> 4;

            for (int i = 0; i < members; i++)
            {
                properties.Add(ReadString());
            }

            Amf3ClassDef classDef = new Amf3ClassDef(name, properties.ToArray(), dynamic, externalizable);

            // lookup serializer to use for this class definition
            classDef.Serializer = GetSerializerFromAlias(classDef.Name);

            traitTable.Add(classDef);
            return(classDef);
        }
Exemplo n.º 2
0
        public void TypelessWrite(Amf3ClassDef classDef)
        {
            // have we written this class before with this writer?
            if (classDef.mWriter == this)
            {
                // use the cached id in the class definition
                TypelessWrite((classDef.mId << 2) | 1);
                return;
            }

            int index;

            if (classDefTable.TryGetValue(classDef, out index))
            {
                // store class id inside of class def for this writer
                classDef.mWriter = this;
                classDef.mId     = index;

                TypelessWrite((index << 2) | 1);
            }
            else
            {
                // store class id inside of class def for this writer
                classDef.mWriter = this;
                classDef.mId     = classDefTable.Count;

                // store class reference in lookup table
                classDefTable[classDef] = classDef.mId;

                Amf3Object.Flags flags = Amf3Object.Flags.Inline | Amf3Object.Flags.InlineClassDef;

                if (classDef.Externalizable)
                {
                    flags |= Amf3Object.Flags.Externalizable;
                }
                if (classDef.Dynamic)
                {
                    flags |= Amf3Object.Flags.Dynamic;
                }

                TypelessWrite((int)flags | (classDef.Properties.Length << 4));

                TypelessWrite(classDef.Name);

                foreach (string i in classDef.Properties)
                {
                    TypelessWrite(i);
                }
            }
        }
Exemplo n.º 3
0
        public object ReadAmf3Object()
        {
            Amf3Object.Flags flags = (Amf3Object.Flags)ReadInteger();

            if ((flags & Amf3Object.Flags.Inline) == 0)
            {
                return(GetTableEntry(objectTable, ((int)flags) >> 1));
            }

            Amf3ClassDef classDef;

            if ((flags & Amf3Object.Flags.InlineClassDef) == 0)
            {
                classDef = GetTableEntry(traitTable, ((int)flags) >> 2);
            }
            else
            {
                classDef = ReadAmf3ClassDef(flags);
            }

            // get the serializer to use (start with the override serializer)
            IAmf3Serializer serializer = OverrideSerializer;

            if (serializer == null)
            {
                // no override? get serializer from class definition
                serializer = classDef.Serializer;
                if (serializer == null)
                {
                    // no serializer for class definition? use default serializer
                    serializer = DefaultSerializer;
                }
            }

            // fast path the Amf3Object serializer
            if (serializer is Amf3Object.Serializer)
            {
                var obj = new Amf3Object(classDef);

                // add to object table
                objectTable.Add(obj);

                // read all properties into object
                int count = classDef.Properties.Length;
                for (int i = 0; i < count; i++)
                {
                    ReadNextObject(ref obj.Values[i]);
                }

                // read dynamic properties
                if (classDef.Dynamic)
                {
                    string key = ReadString();
                    while (key != "")
                    {
                        var value = ReadNextObject();
                        obj.SetPropertyValueAsObject(key, value);
                        key = ReadString();
                    }
                }
                return(obj);
            }
            else
            {
                // create object instance using serializer
                object obj = serializer.NewInstance(classDef);

                // add to object table
                objectTable.Add(obj);

                // begin property remapping
                var reader = classDef.CreatePropertyReader();
                reader.BeginRead(this);
                // read object using serializer
                serializer.ReadObject(reader, obj);
                // end property remapping
                classDef.ReleasePropertyReader(reader);

                // read dynamic properties
                if (classDef.Dynamic)
                {
                    var    dc  = obj as PlayScript.IDynamicClass;
                    string key = ReadString();
                    while (key != "")
                    {
                        var value = ReadNextObject();
                        if (dc != null)
                        {
                            dc.__SetDynamicValue(key, value);
                        }
                        key = ReadString();
                    }
                }
                return(obj);
            }
        }
Exemplo n.º 4
0
        public Amf3Object ReadAmf3Object()
        {
            Amf3Object.Flags flags = (Amf3Object.Flags)ReadInteger();

            if ((flags & Amf3Object.Flags.Inline) == 0)
            {
                return((Amf3Object)GetTableEntry(objectTable, ((int)flags) >> 1));
            }

            Amf3ClassDef classDef;

            if ((flags & Amf3Object.Flags.InlineClassDef) == 0)
            {
                classDef = GetTableEntry(traitTable, ((int)flags) >> 2);
            }
            else
            {
                bool   externalizable = ((flags & Amf3Object.Flags.Externalizable) != 0);
                bool   dynamic        = ((flags & Amf3Object.Flags.Dynamic) != 0);
                string name           = ReadString();

                if (externalizable && dynamic)
                {
                    throw new InvalidOperationException("Serialized objects cannot be both dynamic and externalizable");
                }

                List <string> properties = new List <string>();

                int members = ((int)flags) >> 4;

                for (int i = 0; i < members; i++)
                {
                    properties.Add(ReadString());
                }

                classDef = new Amf3ClassDef(name, properties, dynamic, externalizable);
                traitTable.Add(classDef);
            }

            Amf3Object obj = new Amf3Object(classDef);

            objectTable.Add(obj);

            if (classDef.Externalizable)
            {
                obj.Properties["inner"] = ReadNextObject();
                return(obj);
            }

            foreach (string i in classDef.Properties)
            {
                obj.Properties[i] = ReadNextObject();
            }

            if (classDef.Dynamic)
            {
                string key = ReadString();
                while (key != "")
                {
                    obj.Properties[key] = ReadNextObject();
                    key = ReadString();
                }
            }

            return(obj);
        }
        public void TypelessWrite(Amf3Object obj)
        {
            if (CheckObjectTable(obj))
            {
                return;
            }

            int index;

            if (classDefTable.TryGetValue(obj.ClassDef, out index))
            {
                TypelessWrite((index << 2) | 1);
            }
            else
            {
                classDefTable[obj.ClassDef] = classDefTable.Count;

                Amf3Object.Flags flags = Amf3Object.Flags.Inline | Amf3Object.Flags.InlineClassDef;

                if (obj.ClassDef.Externalizable)
                {
                    flags |= Amf3Object.Flags.Externalizable;
                }
                if (obj.ClassDef.Dynamic)
                {
                    flags |= Amf3Object.Flags.Dynamic;
                }

                TypelessWrite((int)flags | (obj.ClassDef.Properties.Count << 4));

                TypelessWrite(obj.ClassDef.Name);

                foreach (string i in obj.ClassDef.Properties)
                {
                    TypelessWrite(i);
                }
            }

            objectTable[obj] = objectTable.Count;

            if (obj.ClassDef.Externalizable)
            {
                Write(obj["inner"]);
                return;
            }

            foreach (string i in obj.ClassDef.Properties)
            {
                Write(obj[i]);
            }

            if (obj.ClassDef.Dynamic)
            {
                var dynamicProperties =
                    from i in obj.Properties
                    where !obj.ClassDef.Properties.Contains(i.Key)
                    select i;

                foreach (var i in dynamicProperties)
                {
                    TypelessWrite(i.Key);
                    Write(i.Value);
                }

                TypelessWrite("");
            }
        }