diff options
Diffstat (limited to 'Client/Assembly-CSharp/SteamPurchasingModule.cs')
-rw-r--r-- | Client/Assembly-CSharp/SteamPurchasingModule.cs | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/Client/Assembly-CSharp/SteamPurchasingModule.cs b/Client/Assembly-CSharp/SteamPurchasingModule.cs new file mode 100644 index 0000000..b3d80f1 --- /dev/null +++ b/Client/Assembly-CSharp/SteamPurchasingModule.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using Steamworks; +using UnityEngine; +using UnityEngine.Purchasing; +using UnityEngine.Purchasing.Extension; + +internal class SteamPurchasingModule : IPurchasingModule, IStore +{ + public const string Name = "Steam"; + + public Dictionary<string, uint> IdTranslator = new Dictionary<string, uint>(StringComparer.OrdinalIgnoreCase); + + private IStoreCallback storeCallback; + + private Callback<GameOverlayActivated_t> overlayCallback; + + private bool steamOverlayOpen; + + private StoreMenu parent; + + public SteamPurchasingModule(StoreMenu parent) + { + this.parent = parent; + } + + public void Configure(IPurchasingBinder binder) + { + binder.RegisterStore("Steam", this); + } + + public void FinishTransaction(ProductDefinition product, string transactionId) + { + } + + public void Initialize(IStoreCallback callback) + { + this.storeCallback = callback; + if (!SteamManager.Initialized || !SteamUtils.IsOverlayEnabled()) + { + this.storeCallback.OnSetupFailed(InitializationFailureReason.PurchasingUnavailable); + } + this.overlayCallback = Callback<GameOverlayActivated_t>.Create(new Callback<GameOverlayActivated_t>.DispatchDelegate(this.HandleOverlayActivate)); + } + + private void HandleOverlayActivate(GameOverlayActivated_t param) + { + this.steamOverlayOpen = (param.m_bActive > 0); + } + + public void Purchase(ProductDefinition product, string developerPayload) + { + if (!SteamUtils.IsOverlayEnabled()) + { + this.storeCallback.OnPurchaseFailed(new PurchaseFailureDescription(product.storeSpecificId, PurchaseFailureReason.PurchasingUnavailable, "Steam overlay is disabled, but required for in-game purchasing.")); + return; + } + uint value; + if (this.IdTranslator.TryGetValue(product.id, out value)) + { + AppId_t appId_t = new AppId_t(value); + SteamFriends.ActivateGameOverlayToStore(appId_t, EOverlayToStoreFlag.k_EOverlayToStoreFlag_AddToCartAndShow); + this.parent.StartCoroutine(this.WaitForDlcPurchase(product, appId_t)); + return; + } + this.storeCallback.OnPurchaseFailed(new PurchaseFailureDescription(product.storeSpecificId, PurchaseFailureReason.ProductUnavailable, "Couldn't find Product Id for " + product.id)); + } + + private IEnumerator WaitForDlcPurchase(ProductDefinition product, AppId_t appId) + { + while (!this.steamOverlayOpen) + { + SteamAPI.RunCallbacks(); + yield return null; + } + while (this.steamOverlayOpen) + { + SteamAPI.RunCallbacks(); + yield return null; + } + ulong num; + while (SteamApps.GetDlcDownloadProgress(appId, out num, out num)) + { + yield return null; + } + if (SteamApps.BIsDlcInstalled(appId)) + { + this.storeCallback.OnPurchaseSucceeded(product.id, "FakeReceipt", UnityEngine.Random.value.ToString()); + } + else + { + this.storeCallback.OnPurchaseFailed(new PurchaseFailureDescription(product.id, PurchaseFailureReason.UserCancelled, "Steam overlay closed without purchase completing")); + } + yield break; + } + + public void RetrieveProducts(ReadOnlyCollection<ProductDefinition> products) + { + if (!SteamManager.Initialized) + { + return; + } + List<ProductDescription> list = new List<ProductDescription>(products.Count); + for (int i = 0; i < products.Count; i++) + { + ProductDefinition productDefinition = products[i]; + uint value; + if (this.IdTranslator.TryGetValue(productDefinition.id, out value)) + { + bool flag = SteamApps.BIsDlcInstalled(new AppId_t(value)); + list.Add(new ProductDescription(productDefinition.id, new ProductMetadata("$2.99", null, null, "USD", 1m), flag ? "Bought" : null, null)); + } + } + this.storeCallback.OnProductsRetrieved(list); + } +} |