1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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;
}
}
|