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
|
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);
}
}
|