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)) { if (kv.TryReadAsBinary(ms)) { Sections.Add(( EAppInfoSection )section.section_id, kv); } } } }
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)) { // steamclient checks this value == 1 before it attempts to read the KV from the buffer // see: CPackageInfo::UpdateFromBuffer(CSHA const&,uint,CUtlBuffer &) // todo: we've apparently ignored this with zero ill effects, but perhaps we want to respect it? br.ReadUInt32(); Data.TryReadAsBinary(ms); } }
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.TryReadAsBinary(input) == false) { return(null); } } else { if (kv.ReadAsText(input) == false) { return(null); } } return(kv); } } catch (Exception) { return(null); } }
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.TryReadAsBinary( payload ); GuestPasses.Add( kv ); } }
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 ) ) { // steamclient checks this value == 1 before it attempts to read the KV from the buffer // see: CPackageInfo::UpdateFromBuffer(CSHA const&,uint,CUtlBuffer &) // todo: we've apparently ignored this with zero ill effects, but perhaps we want to respect it? br.ReadUInt32(); Data.TryReadAsBinary( ms ); } }
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 ) ) { if ( kv.TryReadAsBinary( ms ) ) { Sections.Add( ( EAppInfoSection )section.section_id, kv ); } } } }
internal PurchaseResponseCallback(JobID jobID, CMsgClientPurchaseResponse msg) { if ((jobID == null) || (msg == null)) { throw new ArgumentNullException(nameof(jobID) + " || " + nameof(msg)); } JobID = jobID; PurchaseResult = (EPurchaseResult) msg.purchase_result_details; if (msg.purchase_receipt_info == null) { return; } KeyValue receiptInfo = new KeyValue(); using (MemoryStream ms = new MemoryStream(msg.purchase_receipt_info)) { if (!receiptInfo.TryReadAsBinary(ms)) { Logging.LogNullError(nameof(ms)); return; } } List<KeyValue> lineItems = receiptInfo["lineitems"].Children; if (lineItems.Count == 0) { return; } Items = new Dictionary<uint, string>(lineItems.Count); foreach (KeyValue lineItem in lineItems) { uint packageID = lineItem["PackageID"].AsUnsignedInteger(); if (packageID == 0) { // Valid, coupons have PackageID of -1 (don't ask me why) packageID = lineItem["ItemAppID"].AsUnsignedInteger(); if (packageID == 0) { Logging.LogNullError(nameof(packageID)); return; } } string gameName = lineItem["ItemDescription"].Value; if (string.IsNullOrEmpty(gameName)) { Logging.LogNullError(nameof(gameName)); return; } gameName = WebUtility.HtmlDecode(gameName); // Apparently steam expects client to decode sent HTML Items[packageID] = WebUtility.HtmlDecode(gameName); } }
public void KeyValuesReadsBinaryWithMultipleChildren() { var hex = "00546573744f626a65637400016b6579310076616c75653100016b6579320076616c756532000808"; var binary = Utils.DecodeHexString( hex ); var kv = new KeyValue(); bool success; using ( var ms = new MemoryStream( binary ) ) { success = kv.TryReadAsBinary( ms ); } Assert.True( success ); Assert.Equal( "TestObject", kv.Name ); Assert.Equal( 2, kv.Children.Count ); Assert.Equal( "key1", kv.Children[ 0 ].Name ); Assert.Equal( "value1", kv.Children[ 0 ].Value ); Assert.Equal( "key2", kv.Children[ 1 ].Name ); Assert.Equal( "value2", kv.Children[ 1 ].Value ); }
public void KeyValuesFailsToReadTruncatedBinary() { // Test every possible truncation boundary we have. for ( int i = 0; i < TestObjectHex.Length; i += 2 ) { var binary = Utils.DecodeHexString( TestObjectHex.Substring( 0, i ) ); var kv = new KeyValue(); bool success; using ( var ms = new MemoryStream( binary ) ) { success = kv.TryReadAsBinary( ms ); Assert.Equal( ms.Length, ms.Position ); } Assert.False( success, "Should not have read test object." ); } }
public void KeyValuesReadsBinaryWithLeftoverData() { var binary = Utils.DecodeHexString( TestObjectHex + Guid.NewGuid().ToString().Replace("-", "") ); var kv = new KeyValue(); bool success; using ( var ms = new MemoryStream( binary ) ) { success = kv.TryReadAsBinary( ms ); Assert.Equal( TestObjectHex.Length / 2, ms.Position ); Assert.Equal( 16, ms.Length - ms.Position ); } Assert.True( success, "Should have read test object." ); Assert.Equal( "TestObject", kv.Name ); Assert.Equal( 1, kv.Children.Count ); Assert.Equal( "key", kv.Children[0].Name ); Assert.Equal( "value", kv.Children[0].Value ); }
public void KeyValues_TryReadAsBinary_ReadsBinary() { var binary = Utils.DecodeHexString( TestObjectHex ); var kv = new KeyValue(); bool success; using ( var ms = new MemoryStream( binary ) ) { success = kv.TryReadAsBinary( ms ); Assert.Equal( ms.Length, ms.Position ); } Assert.True( success, "Should have read test object." ); Assert.Equal( "TestObject", kv.Name ); Assert.Equal( 1, kv.Children.Count ); Assert.Equal( "key", kv.Children[0].Name ); Assert.Equal( "value", kv.Children[0].Value ); }
internal PurchaseResponseCallback(JobID jobID, CMsgClientPurchaseResponse msg) { JobID = jobID; if (msg == null) { return; } Result = (EResult) msg.eresult; PurchaseResult = (EPurchaseResult) msg.purchase_result_details; if (msg.purchase_receipt_info == null) { return; } ReceiptInfo = new KeyValue(); using (MemoryStream ms = new MemoryStream(msg.purchase_receipt_info)) { if (!ReceiptInfo.TryReadAsBinary(ms)) { return; } List<KeyValue> lineItems = ReceiptInfo["lineitems"].Children; Items = new Dictionary<uint, string>(lineItems.Count); foreach (KeyValue lineItem in lineItems) { uint appID = (uint) lineItem["PackageID"].AsUnsignedLong(); string gameName = lineItem["ItemDescription"].AsString(); gameName = WebUtility.UrlDecode(gameName); // Apparently steam expects client to decode sent HTML Items.Add(appID, gameName); } } }
void DisplayDataAsBinaryKeyValues(object sender, EventArgs e) { SetAsRadioSelected(sender); var data = (byte[])value; var kv = new KeyValue(); bool didRead; using (var ms = new MemoryStream(data)) { didRead = kv.TryReadAsBinary(ms); } if (!didRead) { SetValueForDisplay("Not a valid KeyValues object!"); } else { SetValueForDisplay(null, childNodes: new[] { new TreeNodeObjectExplorer(kv.Name, kv).TreeNode }); } TreeNode.ExpandAll(); }
public void KeyValueBinarySerializationIsSymmetric() { var kv = new KeyValue( "MessageObject" ); kv.Children.Add( new KeyValue( "key", "value" ) ); var deserializedKv = new KeyValue(); bool loaded; using ( var ms = new MemoryStream() ) { kv.SaveToStream( ms, asBinary: true ); ms.Seek( 0, SeekOrigin.Begin ); loaded = deserializedKv.TryReadAsBinary( ms ); } Assert.True( loaded ); Assert.Equal( kv.Name, deserializedKv.Name ); Assert.Equal( kv.Children.Count, deserializedKv.Children.Count ); for ( int i = 0; i < kv.Children.Count; i++ ) { var originalChild = kv.Children[ i ]; var deserializedChild = deserializedKv.Children[ i ]; Assert.Equal( originalChild.Name, deserializedChild.Name ); Assert.Equal( originalChild.Value, deserializedChild.Value ); } }
internal PurchaseResponseCallback(JobID jobID, CMsgClientPurchaseResponse msg) { JobID = jobID; if (msg == null) { return; } ReceiptInfo = new KeyValue(); Items = new Dictionary<uint, string>(); Result = (EResult) msg.eresult; PurchaseResult = (EPurchaseResult) msg.purchase_result_details; using (MemoryStream ms = new MemoryStream(msg.purchase_receipt_info)) { if (!ReceiptInfo.TryReadAsBinary(ms)) { return; } foreach (KeyValue lineItem in ReceiptInfo["lineitems"].Children) { Items.Add((uint) lineItem["PackageID"].AsUnsignedLong(), lineItem["ItemDescription"].AsString()); } } }