diff options
Diffstat (limited to 'Client/Assembly-CSharp/StoreMenu.cs')
-rw-r--r-- | Client/Assembly-CSharp/StoreMenu.cs | 409 |
1 files changed, 409 insertions, 0 deletions
diff --git a/Client/Assembly-CSharp/StoreMenu.cs b/Client/Assembly-CSharp/StoreMenu.cs new file mode 100644 index 0000000..a67399d --- /dev/null +++ b/Client/Assembly-CSharp/StoreMenu.cs @@ -0,0 +1,409 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using PowerTools; +using UnityEngine; +using UnityEngine.Purchasing; +using UnityEngine.Purchasing.Extension; + +public class StoreMenu : MonoBehaviour, IStoreListener +{ + public PurchaseStates PurchaseState { get; private set; } + + public SpriteRenderer HatSlot; + + public SpriteRenderer SkinSlot; + + public SpriteAnim PetSlot; + + public TextRenderer ItemName; + + public SpriteRenderer PurchaseBackground; + + public TextRenderer PriceText; + + public PurchaseButton PurchasablePrefab; + + public SpriteRenderer HortLinePrefab; + + public TextRenderer LoadingText; + + public TextRenderer RestorePurchasesButton; + + public GameObject RestorePurchasesObj; + + public SpriteRenderer BannerPrefab; + + public Sprite HatBanner; + + public Sprite SkinsBanner; + + public Sprite HolidayBanner; + + public Sprite PetsBanner; + + public SpriteRenderer TopArrow; + + public SpriteRenderer BottomArrow; + + public const string BoughtAdsProductId = "bought_ads"; + + private IStoreController controller; + + private IExtensionProvider extensions; + + public Scroller Scroller; + + public Vector2 StartPositionVertical; + + public FloatRange XRange = new FloatRange(-1f, 1f); + + public int NumPerRow = 4; + + private PurchaseButton CurrentButton; + + private List<GameObject> AllObjects = new List<GameObject>(); + + private const float NormalHeight = -0.45f; + + private const float BoxHeight = -0.75f; + + public void Start() + { + this.PetSlot.gameObject.SetActive(false); + SteamPurchasingModule steamPurchasingModule = new SteamPurchasingModule(this); + foreach (PetBehaviour petBehaviour in DestroyableSingleton<HatManager>.Instance.AllPets) + { + if (petBehaviour.SteamId != 0U) + { + steamPurchasingModule.IdTranslator.Add(petBehaviour.ProdId, petBehaviour.SteamId); + } + } + ConfigurationBuilder configurationBuilder = ConfigurationBuilder.Instance(steamPurchasingModule, Array.Empty<IPurchasingModule>()); + foreach (PetBehaviour petBehaviour2 in DestroyableSingleton<HatManager>.Instance.AllPets) + { + if (!petBehaviour2.Free) + { + configurationBuilder.AddProduct(petBehaviour2.ProdId, ProductType.NonConsumable); + } + } + UnityPurchasing.Initialize(this, configurationBuilder); + this.PurchaseBackground.color = new Color(0.5f, 0.5f, 0.5f, 1f); + this.PriceText.Color = new Color(0.8f, 0.8f, 0.8f, 1f); + this.PriceText.Text = ""; + } + + public void Update() + { + this.TopArrow.enabled = !this.Scroller.AtTop; + this.BottomArrow.enabled = !this.Scroller.AtBottom; + } + + public void RestorePurchases() + { + } + + private void DestroySliderObjects() + { + for (int i = 0; i < this.AllObjects.Count; i++) + { + UnityEngine.Object.Destroy(this.AllObjects[i]); + } + this.AllObjects.Clear(); + } + + private void FinishRestoring() + { + this.ShowAllButtons(); + this.RestorePurchasesButton.Text = "Purchases Restored"; + } + + public void SetProduct(PurchaseButton button) + { + if (this.PurchaseState == PurchaseStates.Started) + { + return; + } + this.CurrentButton = button; + if (this.CurrentButton.Product is HatBehaviour) + { + HatBehaviour hatBehaviour = (HatBehaviour)this.CurrentButton.Product; + this.HatSlot.gameObject.SetActive(true); + this.SkinSlot.gameObject.SetActive(false); + this.PetSlot.gameObject.SetActive(false); + PlayerControl.SetHatImage(hatBehaviour, this.HatSlot); + this.ItemName.Text = (string.IsNullOrWhiteSpace(hatBehaviour.StoreName) ? hatBehaviour.name : hatBehaviour.StoreName); + if (hatBehaviour.RelatedSkin) + { + TextRenderer itemName = this.ItemName; + itemName.Text += " (Includes skin!)"; + this.SkinSlot.gameObject.SetActive(true); + PlayerControl.SetSkinImage(hatBehaviour.RelatedSkin, this.SkinSlot); + } + } + else if (this.CurrentButton.Product is SkinData) + { + SkinData skinData = (SkinData)this.CurrentButton.Product; + this.SkinSlot.gameObject.SetActive(true); + this.HatSlot.gameObject.SetActive(true); + this.PetSlot.gameObject.SetActive(false); + PlayerControl.SetHatImage(skinData.RelatedHat, this.HatSlot); + PlayerControl.SetSkinImage(skinData, this.SkinSlot); + this.ItemName.Text = (string.IsNullOrWhiteSpace(skinData.StoreName) ? skinData.name : skinData.StoreName); + } + else if (this.CurrentButton.Product is PetBehaviour) + { + PetBehaviour petBehaviour = (PetBehaviour)this.CurrentButton.Product; + this.SkinSlot.gameObject.SetActive(false); + this.HatSlot.gameObject.SetActive(false); + this.PetSlot.gameObject.SetActive(true); + SpriteRenderer component = this.PetSlot.GetComponent<SpriteRenderer>(); + component.material = new Material(petBehaviour.rend.sharedMaterial); + PlayerControl.SetPlayerMaterialColors((int)SaveManager.BodyColor, component); + this.PetSlot.Play(petBehaviour.idleClip, 1f); + this.ItemName.Text = (string.IsNullOrWhiteSpace(petBehaviour.StoreName) ? petBehaviour.name : petBehaviour.StoreName); + } + else + { + this.HatSlot.gameObject.SetActive(false); + this.SkinSlot.gameObject.SetActive(false); + this.PetSlot.gameObject.SetActive(false); + this.ItemName.Text = "Remove All Ads"; + } + if (button.Purchased) + { + this.PurchaseBackground.color = new Color(0.5f, 0.5f, 0.5f, 1f); + this.PriceText.Color = new Color(0.8f, 0.8f, 0.8f, 1f); + this.PriceText.Text = "Owned"; + return; + } + this.PurchaseBackground.color = Color.white; + this.PriceText.Color = Color.white; + this.PriceText.Text = button.Price; + } + + public void BuyProduct() + { + if (!this.CurrentButton || this.CurrentButton.Purchased || this.PurchaseState == PurchaseStates.Started) + { + return; + } + base.StartCoroutine(this.WaitForPurchaseAds(this.CurrentButton)); + } + + public IEnumerator WaitForPurchaseAds(PurchaseButton button) + { + this.PurchaseState = PurchaseStates.Started; + this.controller.InitiatePurchase(button.ProductId); + while (this.PurchaseState == PurchaseStates.Started) + { + yield return null; + } + if (this.PurchaseState == PurchaseStates.Success) + { + foreach (PurchaseButton purchaseButton in from p in this.AllObjects + select p.GetComponent<PurchaseButton>() into h + where h && h.ProductId == button.ProductId + select h) + { + purchaseButton.SetPurchased(); + } + } + this.SetProduct(button); + yield break; + } + + public void Close() + { + HatsTab hatsTab = UnityEngine.Object.FindObjectOfType<HatsTab>(); + if (hatsTab) + { + hatsTab.OnDisable(); + hatsTab.OnEnable(); + } + base.gameObject.SetActive(false); + } + + private void ShowAllButtons() + { + this.DestroySliderObjects(); + string text = ""; + try + { + text = "Couldn't fetch products"; + foreach (Product product in this.controller.products.all) + { + if (product != null && product.hasReceipt) + { + try + { + SaveManager.SetPurchased(product.definition.id); + } + catch + { + } + } + } + Vector3 vector = this.StartPositionVertical; + UnityEngine.Object.Destroy(this.RestorePurchasesObj); + text = "Couldn't fetch products"; + text = "Couldn't fetch pet data"; + vector.y += -0.375f; + PetBehaviour[] array = (from h in DestroyableSingleton<HatManager>.Instance.AllPets + where !h.Free + select h into p + orderby p.StoreName + select p).ToArray<PetBehaviour>(); + vector = this.InsertBanner(vector, this.PetsBanner); + Vector3 position = vector; + Product[] all; + Product[] allProducts = all; + IBuyable[] hats = array; + vector = this.InsertHatsFromList(position, allProducts, hats); + text = "Couldn't finalize menu"; + this.Scroller.YBounds.max = Mathf.Max(0f, -vector.y - 2.5f); + this.LoadingText.gameObject.SetActive(false); + } + catch (Exception ex) + { + Debug.Log(string.Concat(new object[] + { + "Exception: ", + text, + ": ", + ex + })); + this.DestroySliderObjects(); + this.LoadingText.gameObject.SetActive(true); + this.LoadingText.Text = "Loading Failed:\r\n" + text; + } + } + + private Vector3 InsertHortLine(Vector3 position) + { + position.x = 1.2f; + SpriteRenderer spriteRenderer = UnityEngine.Object.Instantiate<SpriteRenderer>(this.HortLinePrefab, this.Scroller.Inner); + spriteRenderer.transform.localPosition = position; + spriteRenderer.gameObject.SetActive(true); + position.y += -0.33749998f; + return position; + } + + private Vector3 InsertHatsFromList(Vector3 position, Product[] allProducts, IBuyable[] hats) + { + int num = 0; + for (int i = 0; i < hats.Length; i++) + { + IBuyable item = hats[i]; + Product product = allProducts.FirstOrDefault((Product p) => item.ProdId == p.definition.id); + if (product != null && product.definition != null && product.availableToPurchase) + { + int num2 = num % this.NumPerRow; + position.x = this.StartPositionVertical.x + this.XRange.Lerp((float)num2 / ((float)this.NumPerRow - 1f)); + if (num2 == 0 && num > 1) + { + position.y += -0.75f; + } + this.InsertProduct(position, product, item); + num++; + } + } + position.y += -0.75f; + return position; + } + + private void InsertProduct(Vector3 position, Product product, IBuyable item) + { + PurchaseButton purchaseButton = UnityEngine.Object.Instantiate<PurchaseButton>(this.PurchasablePrefab, this.Scroller.Inner); + this.AllObjects.Add(purchaseButton.gameObject); + purchaseButton.transform.localPosition = position; + purchaseButton.Parent = this; + PurchaseButton purchaseButton2 = purchaseButton; + string id = product.definition.id; + ProductMetadata metadata = product.metadata; + string name; + if (metadata == null) + { + name = null; + } + else + { + string localizedTitle = metadata.localizedTitle; + name = ((localizedTitle != null) ? localizedTitle.Replace("(Among Us)", "") : null); + } + ProductMetadata metadata2 = product.metadata; + purchaseButton2.SetItem(item, id, name, (metadata2 != null) ? metadata2.localizedPriceString : null, product.hasReceipt || SaveManager.GetPurchase(product.definition.id)); + } + + private Vector3 InsertBanner(Vector3 position, Sprite s) + { + position.x = this.StartPositionVertical.x; + SpriteRenderer spriteRenderer = UnityEngine.Object.Instantiate<SpriteRenderer>(this.BannerPrefab, this.Scroller.Inner); + spriteRenderer.sprite = s; + spriteRenderer.transform.localPosition = position; + position.y += -spriteRenderer.sprite.bounds.size.y; + this.AllObjects.Add(spriteRenderer.gameObject); + return position; + } + + public void OnInitialized(IStoreController controller, IExtensionProvider extensions) + { + this.controller = controller; + this.extensions = extensions; + if (this.controller == null || this.controller.products == null) + { + this.LoadingText.Text = "Product controller\r\nfailed to load"; + return; + } + this.ShowAllButtons(); + } + + public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e) + { + this.PurchaseState = PurchaseStates.Success; + SaveManager.SetPurchased(e.purchasedProduct.definition.id); + return PurchaseProcessingResult.Complete; + } + + public void OnInitializeFailed(InitializationFailureReason error) + { + this.RestorePurchasesObj.SetActive(false); + this.LoadingText.gameObject.SetActive(true); + if (error == InitializationFailureReason.NoProductsAvailable) + { + this.LoadingText.Text = "Coming Soon!"; + return; + } + if (error == InitializationFailureReason.PurchasingUnavailable) + { + this.LoadingText.Text = "Loading Failed:\r\nSteam must be running and logged in to view products."; + return; + } + this.LoadingText.Text = "Loading Failed:\r\n" + error; + } + + public void OnPurchaseFailed(Product i, PurchaseFailureReason error) + { + if (error == PurchaseFailureReason.ProductUnavailable) + { + this.DestroySliderObjects(); + this.LoadingText.gameObject.SetActive(true); + this.LoadingText.Text = "Coming Soon!"; + } + else if (error == PurchaseFailureReason.PurchasingUnavailable) + { + this.DestroySliderObjects(); + this.LoadingText.gameObject.SetActive(true); + this.LoadingText.Text = "Steam overlay is required for in-game purchasing. You can still buy and install DLC in Steam."; + } + else + { + this.DestroySliderObjects(); + this.LoadingText.gameObject.SetActive(true); + this.LoadingText.Text = "Loading Failed:\r\n" + error; + } + Debug.LogError("Failed: " + error); + this.PurchaseState = PurchaseStates.Fail; + } +} |