ReadAsBinary() 공개 메소드

Populate this instance from the given Stream as a binary KeyValue.
public ReadAsBinary ( Stream input ) : bool
input Stream The input to read from.
리턴 bool
예제 #1
0
            internal GuestPassListCallback(MsgClientUpdateGuestPassesList msg, Stream payload)
            {
                Result = msg.Result;
                CountGuestPassesToGive   = msg.CountGuestPassesToGive;
                CountGuestPassesToRedeem = msg.CountGuestPassesToRedeem;

                GuestPasses = new List <KeyValue>();
                for (int i = 0; i < CountGuestPassesToGive + CountGuestPassesToRedeem; i++)
                {
                    var kv = new KeyValue();
                    kv.ReadAsBinary(payload);
                    GuestPasses.Add(kv.Children[0]);
                }
            }
예제 #2
0
                internal App(CMsgClientAppInfoResponse.App app, AppInfoStatus status)
                {
                    Status       = status;
                    AppID        = app.app_id;
                    ChangeNumber = app.change_number;
                    Sections     = new Dictionary <EAppInfoSection, KeyValue>();

                    foreach (var section in app.sections)
                    {
                        KeyValue kv = new KeyValue();
                        using (MemoryStream ms = new MemoryStream(section.section_kv))
                            kv.ReadAsBinary(ms);

                        Sections.Add(( EAppInfoSection )section.section_id, kv);
                    }
                }
예제 #3
0
                internal Package(CMsgClientPackageInfoResponse.Package pack, Package.PackageStatus status)
                {
                    Status = status;

                    PackageID    = pack.package_id;
                    ChangeNumber = pack.change_number;
                    Hash         = pack.sha;

                    Data = new KeyValue();

                    using (var ms = new MemoryStream(pack.buffer))
                        using (var br = new BinaryReader(ms))
                        {
                            br.ReadUInt32(); // unknown uint at the beginning of the buffer
                            Data.ReadAsBinary(ms);
                        }
                }
예제 #4
0
        static KeyValue LoadFromFile(string path, bool asBinary)
        {
            if (File.Exists(path) == false)
            {
                return(null);
            }

            try
            {
                using (var input = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var kv = new KeyValue();

                    if (asBinary)
                    {
                        if (kv.ReadAsBinary(input) == false)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        if (kv.ReadAsText(input) == false)
                        {
                            return(null);
                        }
                    }

                    return(kv);
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
예제 #5
0
            internal GuestPassListCallback( MsgClientUpdateGuestPassesList msg, Stream payload )
#endif
            {
                Result = msg.Result;
                CountGuestPassesToGive = msg.CountGuestPassesToGive;
                CountGuestPassesToRedeem = msg.CountGuestPassesToRedeem;

                GuestPasses = new List<KeyValue>();
                for ( int i = 0; i < CountGuestPassesToGive + CountGuestPassesToRedeem; i++ )
                {
                    var kv = new KeyValue();
                    kv.ReadAsBinary( payload );
                    GuestPasses.Add( kv.Children[0] );
                }
            }
예제 #6
0
                internal Package( CMsgClientPackageInfoResponse.Package pack, Package.PackageStatus status )
                {
                    Status = status;

                    PackageID = pack.package_id;
                    ChangeNumber = pack.change_number;
                    Hash = pack.sha;

                    Data = new KeyValue();

                    using ( var ms = new MemoryStream( pack.buffer ) )
                    using ( var br = new BinaryReader( ms ) )
                    {
                        br.ReadUInt32(); // unknown uint at the beginning of the buffer
                        Data.ReadAsBinary( ms );
                    }

                    if ( Data.Children != null )
                    {
                        Data = Data.Children.FirstOrDefault() ?? KeyValue.Invalid;
                    }
                }
예제 #7
0
                internal App( CMsgClientAppInfoResponse.App app, AppInfoStatus status )
                {
                    Status = status;
                    AppID = app.app_id;
                    ChangeNumber = app.change_number;
                    Sections = new Dictionary<EAppInfoSection, KeyValue>();

                    foreach ( var section in app.sections )
                    {
                        KeyValue kv = new KeyValue();

                        using ( MemoryStream ms = new MemoryStream( section.section_kv ) )
                            kv.ReadAsBinary( ms );

                        if ( kv.Children != null )
                        {
                            Sections.Add( ( EAppInfoSection )section.section_id, kv.Children.FirstOrDefault() ?? KeyValue.Invalid );
                        }
                    }
                }
예제 #8
0
        /// <summary>
        /// Populate this instance from the given <see cref="Stream"/> as a binary <see cref="KeyValue"/>.
        /// </summary>
        /// <param name="input">The input <see cref="Stream"/> to read from.</param>
        /// <returns><c>true</c> if the read was successful; otherwise, <c>false</c>.</returns>
        public bool ReadAsBinary(Stream input)
        {
            this.Children = new List <KeyValue>();

            while (true)
            {
                var type = ( Type )input.ReadByte();

                if (type == Type.End)
                {
                    break;
                }

                var current = new KeyValue();
                current.Name = input.ReadNullTermString(Encoding.UTF8);

                try
                {
                    switch (type)
                    {
                    case Type.None:
                    {
                        current.ReadAsBinary(input);
                        break;
                    }

                    case Type.String:
                    {
                        current.Value = input.ReadNullTermString(Encoding.UTF8);
                        break;
                    }

                    case Type.WideString:
                    {
                        throw new InvalidDataException("wstring is unsupported");
                    }

                    case Type.Int32:
                    case Type.Color:
                    case Type.Pointer:
                    {
                        current.Value = Convert.ToString(input.ReadInt32());
                        break;
                    }

                    case Type.UInt64:
                    {
                        current.Value = Convert.ToString(input.ReadUInt64());
                        break;
                    }

                    case Type.Float32:
                    {
                        current.Value = Convert.ToString(input.ReadFloat());
                        break;
                    }

                    default:
                    {
                        throw new InvalidDataException("Unknown KV type encountered.");
                    }
                    }
                }
                catch (InvalidDataException ex)
                {
                    throw new InvalidDataException(string.Format("An exception ocurred while reading KV '{0}'", current.Name), ex);
                }

                this.Children.Add(current);
            }

            return(input.Position == input.Length);
        }
예제 #9
0
        public void KeyValuesReadsBinary_ReadAsBinary_PreservesBuggyBehavior()
        {
            var binary = Utils.DecodeHexString( TestObjectHex );
            var kv = new KeyValue();
            bool success;
            using ( var ms = new MemoryStream( binary ) )
            {
                success = kv.ReadAsBinary( ms );
                Assert.Equal( ms.Length, ms.Position );
            }

            Assert.True( success, "Should have read test object." );
            Assert.Null( kv.Name );
            Assert.Equal( 1, kv.Children.Count );

            var actualKv = kv.Children[ 0 ];
            Assert.Equal( "TestObject", actualKv.Name );
            Assert.Equal( 1, actualKv.Children.Count );
            Assert.Equal( "key", actualKv.Children[0].Name );
            Assert.Equal( "value", actualKv.Children[0].Value );
        }
예제 #10
0
        /// <summary>
        /// Populate this instance from the given <see cref="Stream"/> as a binary <see cref="KeyValue"/>.
        /// </summary>
        /// <param name="input">The input <see cref="Stream"/> to read from.</param>
        /// <returns><c>true</c> if the read was successful; otherwise, <c>false</c>.</returns>
        public bool ReadAsBinary( Stream input )
        {
            this.Children = new List<KeyValue>();

            while ( true )
            {

                var type = ( Type )input.ReadByte();

                if ( type == Type.End )
                {
                    break;
                }

                var current = new KeyValue();
                current.Name = input.ReadNullTermString( Encoding.UTF8 );

                try
                {
                    switch ( type )
                    {
                        case Type.None:
                            {
                                current.ReadAsBinary( input );
                                break;
                            }

                        case Type.String:
                            {
                                current.Value = input.ReadNullTermString( Encoding.UTF8 );
                                break;
                            }

                        case Type.WideString:
                            {
                                throw new InvalidDataException( "wstring is unsupported" );
                            }

                        case Type.Int32:
                        case Type.Color:
                        case Type.Pointer:
                            {
                                current.Value = Convert.ToString( input.ReadInt32() );
                                break;
                            }

                        case Type.UInt64:
                            {
                                current.Value = Convert.ToString( input.ReadUInt64() );
                                break;
                            }

                        case Type.Float32:
                            {
                                current.Value = Convert.ToString( input.ReadFloat() );
                                break;
                            }

                        default:
                            {
                                throw new InvalidDataException( "Unknown KV type encountered." );
                            }
                    }
                }
                catch ( InvalidDataException ex )
                {
                    throw new InvalidDataException( string.Format( "An exception ocurred while reading KV '{0}'", current.Name ), ex );
                }

                this.Children.Add( current );
            }

            return input.Position == input.Length;
        }
예제 #11
0
        static KeyValue LoadFromFile( string path, bool asBinary )
        {
            if ( File.Exists( path ) == false )
            {
                return null;
            }

            try
            {
                using ( var input = File.Open( path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ) )
                {
                    var kv = new KeyValue();

                    if ( asBinary )
                    {
                        if ( kv.ReadAsBinary( input ) == false )
                        {
                            return null;
                        }
                    }
                    else
                    {
                        if ( kv.ReadAsText( input ) == false )
                        {
                            return null;
                        }
                    }

                    return kv;
                }
            }
            catch ( Exception )
            {
                return null;
            }
        }
예제 #12
0
		void DisplayDataAsBinaryKeyValues(object sender, EventArgs e)
		{
			SetAsRadioSelected(sender);

			var data = (byte[])value;
			var kv = new KeyValue();
			bool didRead;
			using (var ms = new MemoryStream(data))
			{
				try
				{
					didRead = kv.ReadAsBinary(ms);
				}
				catch (InvalidDataException)
				{
					didRead = false;
				}
			}

			if (!didRead)
			{
				SetValueForDisplay("Not a valid KeyValues object!");
			}
			else
			{
				var firstChild = kv.Children[0]; // Due to bug in KeyValues parser.
				SetValueForDisplay(null, childNodes: new[] { new TreeNodeObjectExplorer(firstChild.Name, firstChild).TreeNode });
			}

			TreeNode.ExpandAll();
		}