Example #1
0
        public void TestConstruction()
        {
            TestObject1 obj1 = new TestObject1("some value");

            Variant v1 = new Variant(obj1);
            Assert.AreEqual(v1.Type, Variant.EnumType.Object);

            TestObject1 obj2 = v1.AsObject<TestObject1>();
            Assert.AreEqual(obj1, obj2);

            Assert.Throws<VariantException>(delegate { v1.AsObject<TestObject2>(); });
            Assert.Throws<VariantException>(delegate { v1.AsObject<VariantObjectProxy>(); });

            VariantObjectProxy obj3 = new VariantObjectProxy(obj1.Class);
            obj3.Inflate(obj2.Deflate(), obj2.Version);

            Assert.AreEqual(obj3.Class, "TestObject1");
            Assert.AreEqual(obj3.Version, 1);
            Assert.AreEqual(obj3.Deflate().Type, Variant.EnumType.Dictionary);

            Variant v2 = new Variant(obj3);

            Assert.IsInstanceOf<VariantObjectProxy>(v2.AsObject());

            Assert.Throws<VariantException>(delegate { v1.AsObject<TestObject2>(); });

            TestObject1 obj5 = v2.AsObject<TestObject1>();
            Assert.AreEqual(obj5.Class, "TestObject1");
            Assert.AreEqual(obj5.Version, 1);

            Assert.IsInstanceOf<TestObject1>(v2.AsObject());
        }
Example #2
0
        public void TestConstruction()
        {
            TestObject1 obj1 = new TestObject1("some value");

            Variant v1 = new Variant(obj1);

            Assert.AreEqual(v1.Type, Variant.EnumType.Object);

            TestObject1 obj2 = v1.AsObject <TestObject1>();

            Assert.AreEqual(obj1, obj2);

            Assert.Throws <VariantException>(delegate { v1.AsObject <TestObject2>(); });
            Assert.Throws <VariantException>(delegate { v1.AsObject <VariantObjectProxy>(); });

            VariantObjectProxy obj3 = new VariantObjectProxy(obj1.Class);

            obj3.Inflate(obj2.Deflate(), obj2.Version);

            Assert.AreEqual(obj3.Class, "TestObject1");
            Assert.AreEqual(obj3.Version, 1);
            Assert.AreEqual(obj3.Deflate().Type, Variant.EnumType.Dictionary);

            Variant v2 = new Variant(obj3);

            Assert.IsInstanceOf <VariantObjectProxy>(v2.AsObject());

            Assert.Throws <VariantException>(delegate { v1.AsObject <TestObject2>(); });

            TestObject1 obj5 = v2.AsObject <TestObject1>();

            Assert.AreEqual(obj5.Class, "TestObject1");
            Assert.AreEqual(obj5.Version, 1);

            Assert.IsInstanceOf <TestObject1>(v2.AsObject());
        }
Example #3
0
        private static string Format(Variant value)
        {
            object o = value.AsObject();

            if (o == null)
            {
                return("null");
            }
            else if (o is string)
            {
                return('"' + ((string)o) + '"');
            }
            else if (o is Amf3Object)
            {
                var sb = new System.Text.StringBuilder();
                var ao = (Amf3Object)o;
                sb.AppendFormat("[{0} ", ao.ClassDef.Name);
                for (int i = 0; i < ao.ClassDef.Properties.Length; i++)
                {
                    sb.AppendFormat("{0}:{1} ", ao.ClassDef.Properties[i], Format(ao.Values[i]));
                }
                sb.AppendFormat("]");
                return(sb.ToString());
            }
            else if (o is _root.Vector <int> )
            {
                var a = o as _root.Vector <int>;
                return("(int)[" + a.ToString() + "]");
            }
            else if (o is _root.Vector <uint> )
            {
                var a = o as _root.Vector <uint>;
                return("(uint)[" + a.ToString() + "]");
            }
            else if (o is _root.Vector <double> )
            {
                var a = o as _root.Vector <double>;
                return("(number)[" + a.ToString() + "]");
            }
            else if (o is double)
            {
                return("(number)" + o.ToString());
            }
            else
            {
                return(o.ToString());
            }
        }
Example #4
0
        public void TestObject()
        {
            TestObject1 o1 = new TestObject1("some value");

            Variant v1 = new Variant(o1);

            string  xml = XmlWriter.ToString(v1);
            Variant v2  = XmlReader.FromString(xml);

            Assert.AreEqual(v2.Type, Variant.EnumType.Object);

            TestObject1 o2 = v2.AsObject <TestObject1>();

            Assert.AreEqual(o1.Class, o2.Class);
            Assert.AreEqual(o1.Version, o2.Version);
            Assert.IsTrue(o1.Deflate().Equals(o2.Deflate()));
        }
Example #5
0
        public void TestObject()
        {
            TestObject1 o1 = new TestObject1("some value");

            Variant v1 = new Variant(o1);

            byte[] bytes = BinaryWriter.ToBytes(v1);

            Variant v2 = BinaryReader.FromBytes(bytes);

            Assert.AreEqual(v2.Type, Variant.EnumType.Object);

            TestObject1 o2 = v2.AsObject <TestObject1>();

            Assert.AreEqual(o1.Class, o2.Class);
            Assert.AreEqual(o1.Version, o2.Version);
            Assert.IsTrue(o1.Deflate().Equals(o2.Deflate()));
        }
Example #6
0
        public void TestObjectBuffer()
        {
            TestObject1 o1 = new TestObject1("some value");

            Variant v1 = new Variant(o1);

            var bufferSize = BinaryWriter.GetByteCount(v1);

            byte[] bytes = new byte[bufferSize];
            BinaryWriter.GetBytes(v1, BinaryMode.Default, bytes, 0);

            Variant v2 = BinaryReader.FromBytes(bytes);

            Assert.AreEqual(v2.Type, Variant.EnumType.Object);

            TestObject1 o2 = v2.AsObject <TestObject1>();

            Assert.AreEqual(o1.Class, o2.Class);
            Assert.AreEqual(o1.Version, o2.Version);
            Assert.IsTrue(o1.Deflate().Equals(o2.Deflate()));
        }
        /// <exception cref="VariantException"></exception>
        public static object[] VariantArrayToJavaObjectArray(Variant[] @params, Type[] types)
        {
            if (types.Length == 0)
            {
                return(null);
            }
            // 元々引数不要
            if (@params.Length < types.Length)
            {
                return(null);
            }
            // パラメータが少ない
            int count = types.Length;

            object[] ret = new object[count];
            for (int i = 0; i < count; i++)
            {
                Type    type  = types[i];
                Variant param = @params[i];
                if (type.GetTypeInfo().IsPrimitive)
                {
                    // プリミティブタイプの场合
                    if (type == typeof(int))
                    {
                        ret[i] = Extensions.ValueOf(param.AsInteger());
                    }
                    else if (type == typeof(double))
                    {
                        ret[i] = (param.AsDouble());
                    }
                    else if (type == typeof(bool))
                    {
                        ret[i] = Extensions.ValueOf(param.AsInteger() != 0);
                    }
                    else if (type == typeof(float))
                    {
                        ret[i] = ((float)param.AsDouble());
                    }
                    else if (type == typeof(long))
                    {
                        ret[i] = Extensions.ValueOf(param.AsInteger());
                    }
                    else if (type == typeof(char))
                    {
                        ret[i] = ((char)param.AsInteger());
                    }
                    else if (type == typeof(byte))
                    {
                        ret[i] = (unchecked ((byte)param.AsInteger()));
                    }
                    else if (type == typeof(short))
                    {
                        ret[i] = ((short)param.AsInteger());
                    }
                    else
                    {
                        // may be Void.TYPE
                        ret[i] = null;
                    }
                }
                else if (type == typeof(string))
                {
                    ret[i] = param.AsString();
                }
                else if (type == typeof(ByteBuffer))
                {
                    ret[i] = param.AsOctet();
                }
                else if (type == typeof(Variant))
                {
                    ret[i] = param;
                }
                else if (type == typeof(VariantClosure))
                {
                    ret[i] = param.AsObjectClosure();
                }
                else if (type == typeof(Dispatch2))
                {
                    ret[i] = param.AsObject();
                }
                else if (type == param.ToJavaObject().GetType())
                {
                    ret[i] = param.ToJavaObject();
                }
                else
                {
                    // その他 のクラス
                    ret[i] = null;
                }
            }
            return(ret);
        }
 /// <exception cref="VariantException"></exception>
 public static object VariantToJavaObject(Variant param, Type type)
 {
     if (type.GetTypeInfo().IsPrimitive)
     {
         // プリミティブタイプの场合
         if (type == typeof(int))
         {
             return(Extensions.ValueOf(param.AsInteger()));
         }
         if (type == typeof(double))
         {
             return(param.AsDouble());
         }
         if (type == typeof(bool))
         {
             return(Extensions.ValueOf(param.AsInteger() != 0));
         }
         if (type == typeof(float))
         {
             return((float)param.AsDouble());
         }
         if (type == typeof(long))
         {
             return(Extensions.ValueOf(param.AsInteger()));
         }
         if (type == typeof(char))
         {
             return((char)param.AsInteger());
         }
         if (type == typeof(byte))
         {
             return(unchecked ((byte)param.AsInteger()));
         }
         if (type == typeof(short))
         {
             return((short)param.AsInteger());
         }
         // may be Void.TYPE
         return(null);
     }
     if (type == typeof(string))
     {
         return(param.AsString());
     }
     if (type == typeof(ByteBuffer))
     {
         return(param.AsOctet());
     }
     if (type == typeof(Variant))
     {
         return(param);
     }
     if (type == typeof(VariantClosure))
     {
         return(param.AsObjectClosure());
     }
     if (type == typeof(Dispatch2))
     {
         return(param.AsObject());
     }
     if (type == param.ToJavaObject().GetType())
     {
         return(param.ToJavaObject());
     }
     // その他 のクラス
     return(null);
 }
Example #9
0
        public static void Parse(Stream stream, TextWriter output)
        {
            Amf3Parser parser = new Amf3Parser(stream);

            parser.OverrideSerializer = new Amf3Object.Serializer();

            int time      = 0;
            int enterTime = 0;

            while (stream.Position < stream.Length)
            {
                Variant v = new Variant();
                parser.ReadNextObject(ref v);
                if (!v.IsDefined)
                {
                    break;
                }

                var amfObj = v.AsObject() as Amf3Object;
                if (amfObj == null)
                {
                    break;
                }

                output.Write("{0:D8}: ", time);

                switch (amfObj.ClassDef.Name)
                {
                case ".value":
                {
                    output.WriteLine("WriteValue({0}, {1});",
                                     Format(amfObj["name"]),
                                     Format(amfObj["value"])
                                     );
                    break;
                }

                case ".span":
                {
                    time += amfObj["delta"].AsInt();
                    output.WriteLine("WriteSpan({0}, {1}, {2});",
                                     Format(amfObj["name"]),
                                     amfObj["span"],
                                     amfObj["delta"]
                                     );

                    // handle end of frame
                    string name = amfObj["name"].AsString();
                    if (name == ".exit")
                    {
                        int span   = amfObj["span"].AsInt();
                        int deltas = time - enterTime;
                        output.WriteLine("// frame deltas:{0} span:{1} diff:{2}", deltas, span, deltas - span);
                    }

                    break;
                }

                case ".spanValue":
                {
                    time += amfObj["delta"].AsInt();
                    output.WriteLine("WriteSpanValue({0}, {1}, {2}, {3});",
                                     Format(amfObj["name"]),
                                     amfObj["span"],
                                     amfObj["delta"],
                                     Format(amfObj["value"])
                                     );
                    break;
                }

                case ".time":
                {
                    time += amfObj["delta"].AsInt();
                    output.WriteLine("WriteTime({0}, {1});",
                                     Format(amfObj["name"]),
                                     amfObj["delta"]
                                     );

                    // handle start of frame
                    string name = amfObj["name"].AsString();
                    if (name == ".enter")
                    {
                        enterTime = time;
                    }
                }
                break;

                default:
                    output.WriteLine(Format(v));
                    break;
                }
            }
        }
Example #10
0
        private void WriteVariant(Variant value)
        {
            switch (value.Kind)
            {
                case VariantKind.Null:
                    _writer.Write((byte)EncodingKind.Null);
                    break;

                case VariantKind.Boolean:
                    _writer.Write((byte)(value.AsBoolean() ? EncodingKind.Boolean_True : EncodingKind.Boolean_False));
                    break;

                case VariantKind.Byte:
                    _writer.Write((byte)EncodingKind.UInt8);
                    _writer.Write(value.AsByte());
                    break;

                case VariantKind.SByte:
                    _writer.Write((byte)EncodingKind.Int8);
                    _writer.Write(value.AsSByte());
                    break;

                case VariantKind.Int16:
                    _writer.Write((byte)EncodingKind.Int16);
                    _writer.Write(value.AsInt16());
                    break;

                case VariantKind.UInt16:
                    _writer.Write((byte)EncodingKind.UInt16);
                    _writer.Write(value.AsUInt16());
                    break;

                case VariantKind.Int32:
                    {
                        var v = value.AsInt32();
                        if (v >= 0 && v <= 10)
                        {
                            _writer.Write((byte)((int)EncodingKind.Int32_0 + v));
                        }
                        else if (v >= 0 && v < byte.MaxValue)
                        {
                            _writer.Write((byte)EncodingKind.Int32_1Byte);
                            _writer.Write((byte)v);
                        }
                        else if (v >= 0 && v < ushort.MaxValue)
                        {
                            _writer.Write((byte)EncodingKind.Int32_2Bytes);
                            _writer.Write((ushort)v);
                        }
                        else
                        {
                            _writer.Write((byte)EncodingKind.Int32);
                            _writer.Write(v);
                        }
                    }
                    break;

                case VariantKind.UInt32:
                    {
                        var v = value.AsUInt32();
                        if (v >= 0 && v <= 10)
                        {
                            _writer.Write((byte)((int)EncodingKind.UInt32_0 + v));
                        }
                        else if (v >= 0 && v < byte.MaxValue)
                        {
                            _writer.Write((byte)EncodingKind.UInt32_1Byte);
                            _writer.Write((byte)v);
                        }
                        else if (v >= 0 && v < ushort.MaxValue)
                        {
                            _writer.Write((byte)EncodingKind.UInt32_2Bytes);
                            _writer.Write((ushort)v);
                        }
                        else
                        {
                            _writer.Write((byte)EncodingKind.UInt32);
                            _writer.Write(v);
                        }
                    }
                    break;

                case VariantKind.Int64:
                    _writer.Write((byte)EncodingKind.Int64);
                    _writer.Write(value.AsInt64());
                    break;

                case VariantKind.UInt64:
                    _writer.Write((byte)EncodingKind.UInt64);
                    _writer.Write(value.AsUInt64());
                    break;

                case VariantKind.Decimal:
                    _writer.Write((byte)EncodingKind.Decimal);
                    _writer.Write(value.AsDecimal());
                    break;

                case VariantKind.Float4:
                    _writer.Write((byte)EncodingKind.Float4);
                    _writer.Write(value.AsSingle());
                    break;

                case VariantKind.Float8:
                    _writer.Write((byte)EncodingKind.Float8);
                    _writer.Write(value.AsDouble());
                    break;

                case VariantKind.Char:
                    _writer.Write((byte)EncodingKind.Char);
                    _writer.Write((ushort)value.AsChar());  // written as ushort because BinaryWriter fails on chars that are unicode surrogates
                    break;

                case VariantKind.String:
                    WriteStringValue(value.AsString());
                    break;

                case VariantKind.BoxedEnum:
                    var e = value.AsBoxedEnum();
                    WriteBoxedEnum(e, e.GetType());
                    break;

                case VariantKind.DateTime:
                    _writer.Write((byte)EncodingKind.DateTime);
                    _writer.Write(value.AsDateTime().ToBinary());
                    break;

                case VariantKind.Type:
                    WriteType(value.AsType());
                    break;

                case VariantKind.Array:
                    WriteArray(value.AsArray());
                    break;

                case VariantKind.Object:
                    WriteObject(value.AsObject());
                    break;
            }
        }
Example #11
0
        /// <exception cref="TjsException"></exception>
        /// <exception cref="VariantException"></exception>
        public virtual void AssignStructure(Dispatch2 dsp, AList <Dispatch2> stack)
        {
            // assign structured data from dsp
            ArrayNI arrayni = (ArrayNI)dsp.GetNativeInstance(ArrayClass.ClassID);

            if (arrayni != null)
            {
                // copy from array
                stack.AddItem(dsp);
                try
                {
                    mItems.Clear();
                    int count = arrayni.mItems.Count;
                    for (int i = 0; i < count; i++)
                    {
                        Variant v = arrayni.mItems[i];
                        if (v.IsObject())
                        {
                            // object
                            Dispatch2 dsp1 = v.AsObject();
                            // determin dsp's object type
                            //DictionaryNI dicni = null;
                            //ArrayNI arrayni1 = null;
                            if (dsp1 != null && dsp1.GetNativeInstance(DictionaryClass.ClassID) != null)
                            {
                                //dicni = (DictionaryNI)ni.mValue;
                                // dictionary
                                bool objrec = false;
                                int  scount = stack.Count;
                                for (int j = 0; j < scount; j++)
                                {
                                    Dispatch2 d = stack[j];
                                    if (d == dsp1)
                                    {
                                        // object recursion detected
                                        objrec = true;
                                        break;
                                    }
                                }
                                if (objrec)
                                {
                                    mItems.AddItem(new Variant());
                                }
                                else
                                {
                                    // becomes null
                                    Dispatch2 newobj = Tjs.CreateDictionaryObject();
                                    mItems.AddItem(new Variant(newobj, newobj));
                                    DictionaryNI newni;
                                    if ((newni = (DictionaryNI)newobj.GetNativeInstance(DictionaryClass.ClassID)) !=
                                        null)
                                    {
                                        newni.AssignStructure(dsp1, stack);
                                    }
                                }
                            }
                            else
                            {
                                if (dsp1 != null && dsp1.GetNativeInstance(ArrayClass.ClassID) != null)
                                {
                                    // array
                                    bool objrec = false;
                                    int  scount = stack.Count;
                                    for (int j = 0; j < scount; j++)
                                    {
                                        Dispatch2 d = stack[j];
                                        if (d == dsp1)
                                        {
                                            // object recursion detected
                                            objrec = true;
                                            break;
                                        }
                                    }
                                    if (objrec)
                                    {
                                        mItems.AddItem(new Variant());
                                    }
                                    else
                                    {
                                        // becomes null
                                        Dispatch2 newobj = Tjs.CreateArrayObject();
                                        mItems.AddItem(new Variant(newobj, newobj));
                                        ArrayNI newni;
                                        if ((newni = (ArrayNI)newobj.GetNativeInstance(ArrayClass.ClassID)) != null)
                                        {
                                            newni.AssignStructure(dsp1, stack);
                                        }
                                    }
                                }
                                else
                                {
                                    // other object types
                                    mItems.AddItem(v);
                                }
                            }
                        }
                        else
                        {
                            // others
                            mItems.AddItem(v);
                        }
                    }
                }
                finally
                {
                    stack.Remove(stack.Count - 1);
                }
            }
            else
            {
                throw new TjsException(Error.SpecifyDicOrArray);
            }
        }
 /// <exception cref="VariantException"></exception>
 /// <exception cref="TjsException"></exception>
 public virtual bool Callback(string name, int flags, Variant value)
 {
     if ((flags & Interface.HIDDENMEMBER) != 0)
     {
         return(true);
     }
     if (value.IsObject())
     {
         // object
         Dispatch2 dsp = value.AsObject();
         // determin dsp's object type
         Variant val;
         if (dsp != null)
         {
             if (dsp.GetNativeInstance(DictionaryClass.ClassID) != null)
             {
                 // dictionary
                 bool objrec = false;
                 int  count  = mStack.Count;
                 for (int i = 0; i < count; i++)
                 {
                     Dispatch2 v = mStack[i];
                     if (v == dsp)
                     {
                         // object recursion detected
                         objrec = true;
                         break;
                     }
                 }
                 val = new Variant();
                 if (objrec)
                 {
                     val.SetObject(null);
                 }
                 else
                 {
                     // becomes null
                     Dispatch2 newobj = Tjs.CreateDictionaryObject();
                     val.SetObject(newobj, newobj);
                     DictionaryNI newni;
                     if ((newni = (DictionaryNI)newobj.GetNativeInstance(DictionaryClass.ClassID)) !=
                         null)
                     {
                         newni.AssignStructure(dsp, mStack);
                     }
                 }
             }
             else
             {
                 if (dsp.GetNativeInstance(ArrayClass.ClassID) != null)
                 {
                     // array
                     bool objrec = false;
                     int  count  = mStack.Count;
                     for (int i = 0; i < count; i++)
                     {
                         Dispatch2 v = mStack[i];
                         if (v == dsp)
                         {
                             // object recursion detected
                             objrec = true;
                             break;
                         }
                     }
                     val = new Variant();
                     if (objrec)
                     {
                         val.SetObject(null);
                     }
                     else
                     {
                         // becomes null
                         Dispatch2 newobj = Tjs.CreateArrayObject();
                         val.SetObject(newobj, newobj);
                         ArrayNI newni;
                         if ((newni = (ArrayNI)newobj.GetNativeInstance(ArrayClass.ClassID)) != null)
                         {
                             newni.AssignStructure(dsp, mStack);
                         }
                     }
                 }
                 else
                 {
                     val = value;
                 }
             }
         }
         else
         {
             // other object types
             val = value;
         }
         mDest.PropSet(Interface.MEMBERENSURE | Interface.IGNOREPROP, name, val, mDest);
     }
     else
     {
         // other types
         mDest.PropSet(Interface.MEMBERENSURE | Interface.IGNOREPROP, name, value, mDest);
     }
     return(true);
 }