From 6e98353ce3a1804908d5c6402594a7404a0e6828 Mon Sep 17 00:00:00 2001 From: Ricardo Alcantara Date: Tue, 11 Aug 2020 23:32:33 -0300 Subject: [PATCH] Rewriting Mono Android IAP to match with GDScript Version. --- mono/android_iap/Android IAP with C#.sln | 19 -- ...> Android in-app purchases with C#.csproj} | 10 +- .../Android in-app purchases with C#.sln | 19 ++ .../GodotGooglePlayBilling/BillingResult.cs | 2 +- .../GooglePlayBilling.cs | 100 ++---- .../GooglePlayBillingUtils.cs | 2 +- .../GodotGooglePlayBilling/Purchase.cs | 2 +- .../GodotGooglePlayBilling/PurchasesResult.cs | 2 +- .../GodotGooglePlayBilling/SkuDetails.cs | 2 +- mono/android_iap/Main.cs | 306 ++++++++---------- mono/android_iap/Main.tscn | 215 ------------ mono/android_iap/Properties/AssemblyInfo.cs | 2 +- mono/android_iap/README.md | 18 +- mono/android_iap/main.tscn | 87 +++++ mono/android_iap/project.godot | 19 +- mono/android_iap/screenshots/.gdignore | 1 - mono/android_iap/screenshots/1.jpg | Bin 34762 -> 0 bytes mono/android_iap/screenshots/2.jpg | Bin 30702 -> 0 bytes mono/android_iap/screenshots/3.jpg | Bin 34927 -> 0 bytes mono/android_iap/screenshots/4.jpg | Bin 36176 -> 0 bytes 20 files changed, 289 insertions(+), 517 deletions(-) delete mode 100644 mono/android_iap/Android IAP with C#.sln rename mono/android_iap/{Android IAP with C#.csproj => Android in-app purchases with C#.csproj} (87%) create mode 100644 mono/android_iap/Android in-app purchases with C#.sln delete mode 100644 mono/android_iap/Main.tscn create mode 100644 mono/android_iap/main.tscn delete mode 100644 mono/android_iap/screenshots/.gdignore delete mode 100644 mono/android_iap/screenshots/1.jpg delete mode 100644 mono/android_iap/screenshots/2.jpg delete mode 100644 mono/android_iap/screenshots/3.jpg delete mode 100644 mono/android_iap/screenshots/4.jpg diff --git a/mono/android_iap/Android IAP with C#.sln b/mono/android_iap/Android IAP with C#.sln deleted file mode 100644 index 432ad2ee..00000000 --- a/mono/android_iap/Android IAP with C#.sln +++ /dev/null @@ -1,19 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Android IAP with C#", "Android IAP with C#.csproj", "{FA89D4C3-B45B-49F1-A975-BF059CA82D86}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - ExportDebug|Any CPU = ExportDebug|Any CPU - ExportRelease|Any CPU = ExportRelease|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {FA89D4C3-B45B-49F1-A975-BF059CA82D86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FA89D4C3-B45B-49F1-A975-BF059CA82D86}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FA89D4C3-B45B-49F1-A975-BF059CA82D86}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU - {FA89D4C3-B45B-49F1-A975-BF059CA82D86}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU - {FA89D4C3-B45B-49F1-A975-BF059CA82D86}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU - {FA89D4C3-B45B-49F1-A975-BF059CA82D86}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU - EndGlobalSection -EndGlobal diff --git a/mono/android_iap/Android IAP with C#.csproj b/mono/android_iap/Android in-app purchases with C#.csproj similarity index 87% rename from mono/android_iap/Android IAP with C#.csproj rename to mono/android_iap/Android in-app purchases with C#.csproj index 55d46702..3ec130a4 100644 --- a/mono/android_iap/Android IAP with C#.csproj +++ b/mono/android_iap/Android in-app purchases with C#.csproj @@ -3,11 +3,11 @@ Debug AnyCPU - {FA89D4C3-B45B-49F1-A975-BF059CA82D86} + {480953C3-B1FD-42F6-8A07-51A3F69024D7} Library .mono\temp\bin\$(Configuration) - AndroidIAPwithC - Android IAP with C# + AndroidInAppPurchasesWithCSharp + Android in-app purchases with C# v4.7 1.0.0.0 .mono\temp\obj @@ -47,12 +47,12 @@ All - $(ProjectDir)\.mono\assemblies\$(ApiConfiguration)\GodotSharp.dll False + $(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/GodotSharp.dll - $(ProjectDir)\.mono\assemblies\$(ApiConfiguration)\GodotSharpEditor.dll False + $(ProjectDir)/.mono/assemblies/$(ApiConfiguration)/GodotSharpEditor.dll diff --git a/mono/android_iap/Android in-app purchases with C#.sln b/mono/android_iap/Android in-app purchases with C#.sln new file mode 100644 index 00000000..f35802ba --- /dev/null +++ b/mono/android_iap/Android in-app purchases with C#.sln @@ -0,0 +1,19 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Android in-app purchases with C#", "Android in-app purchases with C#.csproj", "{480953C3-B1FD-42F6-8A07-51A3F69024D7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + ExportDebug|Any CPU = ExportDebug|Any CPU + ExportRelease|Any CPU = ExportRelease|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {480953C3-B1FD-42F6-8A07-51A3F69024D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {480953C3-B1FD-42F6-8A07-51A3F69024D7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {480953C3-B1FD-42F6-8A07-51A3F69024D7}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU + {480953C3-B1FD-42F6-8A07-51A3F69024D7}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU + {480953C3-B1FD-42F6-8A07-51A3F69024D7}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU + {480953C3-B1FD-42F6-8A07-51A3F69024D7}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU + EndGlobalSection +EndGlobal diff --git a/mono/android_iap/GodotGooglePlayBilling/BillingResult.cs b/mono/android_iap/GodotGooglePlayBilling/BillingResult.cs index 48bfb505..85efbc89 100644 --- a/mono/android_iap/GodotGooglePlayBilling/BillingResult.cs +++ b/mono/android_iap/GodotGooglePlayBilling/BillingResult.cs @@ -1,7 +1,7 @@ using Godot; using Godot.Collections; -namespace Android_Iap.GodotGooglePlayBilling +namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling { // https://developer.android.com/reference/com/android/billingclient/api/BillingClient.BillingResponseCode public enum BillingResponseCode diff --git a/mono/android_iap/GodotGooglePlayBilling/GooglePlayBilling.cs b/mono/android_iap/GodotGooglePlayBilling/GooglePlayBilling.cs index 9fd85303..f2c1f3b9 100644 --- a/mono/android_iap/GodotGooglePlayBilling/GooglePlayBilling.cs +++ b/mono/android_iap/GodotGooglePlayBilling/GooglePlayBilling.cs @@ -1,7 +1,7 @@ using Godot.Collections; using Godot; -namespace Android_Iap.GodotGooglePlayBilling +namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling { public enum PurchaseType { @@ -21,18 +21,20 @@ namespace Android_Iap.GodotGooglePlayBilling [Signal] public delegate void PurchaseAcknowledged(string purchaseToken); [Signal] public delegate void PurchaseAcknowledgementError(int code, string message); [Signal] public delegate void PurchaseConsumed(string purchaseToken); - [Signal] public delegate void PurchaseConsumption_error(int code, string message, string purchaseToken); + [Signal] public delegate void PurchaseConsumptionError(int code, string message, string purchaseToken); [Export] public bool AutoReconnect { get; set; } [Export] public bool AutoConnect { get; set; } + public bool IsAvailable { get; private set; } + private Object _payment; public override void _Ready() { if (Engine.HasSingleton("GodotGooglePlayBilling")) { - GD.Print("GodotGooglePlayBilling HasSingleton"); + IsAvailable = true; _payment = Engine.GetSingleton("GodotGooglePlayBilling"); // These are all signals supported by the API // You can drop some of these based on your needs @@ -47,12 +49,10 @@ namespace Android_Iap.GodotGooglePlayBilling _payment.Connect("purchase_acknowledgement_error", this, nameof(OnGodotGooglePlayBilling_purchase_acknowledgement_error)); // Response ID (int), Debug message (string), Purchase token (string) _payment.Connect("purchase_consumed", this, nameof(OnGodotGooglePlayBilling_purchase_consumed)); // Purchase token (string) _payment.Connect("purchase_consumption_error", this, nameof(OnGodotGooglePlayBilling_purchase_consumption_error)); // Response ID (int), Debug message (string), Purchase token (string) - - if (AutoConnect) StartConnection(); } else { - GD.Print("GPB: Android IAP support is not enabled. Make sure you have enabled 'Custom Build' and the GodotGooglePlayBilling plugin in your Android export settings! IAP will not work."); + IsAvailable = false; } } @@ -62,6 +62,14 @@ namespace Android_Iap.GodotGooglePlayBilling public void EndConnection() => _payment?.Call("endConnection"); + public void QuerySkuDetails(string[] querySkuDetails, PurchaseType type) => _payment?.Call("querySkuDetails", querySkuDetails, $"{type}".ToLower()); + + public bool IsReady() => (_payment?.Call("isReady") as bool?) ?? false; + + public void AcknowledgePurchase(string purchaseToken) => _payment?.Call("acknowledgePurchase", purchaseToken); + + public void ConsumePurchase(string purchaseToken) => _payment?.Call("consumePurchase", purchaseToken); + public BillingResult Purchase(string sku) { if (_payment == null) return null; @@ -69,10 +77,6 @@ namespace Android_Iap.GodotGooglePlayBilling return new BillingResult(result); } - public void QuerySkuDetails(string[] querySkuDetails, PurchaseType type) => _payment?.Call("querySkuDetails", querySkuDetails, $"{type}".ToLower()); - - public bool IsReady() => (_payment?.Call("isReady") as bool?) ?? false; - public PurchasesResult QueryPurchases(PurchaseType purchaseType) { if (_payment == null) return null; @@ -80,85 +84,31 @@ namespace Android_Iap.GodotGooglePlayBilling return new PurchasesResult(result); } - public void AcknowledgePurchase(string purchaseToken) => _payment?.Call("acknowledgePurchase", purchaseToken); - - public void ConsumePurchase(string purchaseToken) => _payment?.Call("consumePurchase", purchaseToken); - #endregion #region GodotGooglePlayBilling Signals - private void OnGodotGooglePlayBilling_connected() - { - GD.Print("GodotGooglePlayBilling Connected"); - EmitSignal(nameof(Connected)); - } + private void OnGodotGooglePlayBilling_connected() => EmitSignal(nameof(Connected)); - private async void OnGodotGooglePlayBilling_disconnected() - { - GD.Print("GodotGooglePlayBilling Disconnected"); - EmitSignal(nameof(Disconnected)); + private void OnGodotGooglePlayBilling_disconnected() => EmitSignal(nameof(Disconnected)); - if (AutoReconnect) - { - await ToSignal(GetTree().CreateTimer(10), "timeout"); - StartConnection(); - } - } + private void OnGodotGooglePlayBilling_connect_error(int code, string message) => EmitSignal(nameof(ConnectError), code, message); - private void OnGodotGooglePlayBilling_connect_error(int code, string message) - { - GD.Print($"GodotGooglePlayBilling ConnectError {code}: {message}"); - EmitSignal(nameof(ConnectError), code, message); - } + private void OnGodotGooglePlayBilling_sku_details_query_completed(Array skuDetails) => EmitSignal(nameof(SkuDetailsQueryCompleted), skuDetails); - private void OnGodotGooglePlayBilling_sku_details_query_completed(Array skuDetails) - { - GD.Print($"GodotGooglePlayBilling SkuDetailsQueryCompleted {skuDetails}"); - EmitSignal(nameof(SkuDetailsQueryCompleted), skuDetails); - } + private void OnGodotGooglePlayBilling_sku_details_query_error(int code, string message) => EmitSignal(nameof(SkuDetailsQueryError), code, message); - private void OnGodotGooglePlayBilling_sku_details_query_error(int code, string message) - { - GD.Print($"SkuDetailsQueryError error {code}: {message}"); - EmitSignal(nameof(SkuDetailsQueryError), code, message); - } + private void OnGodotGooglePlayBilling_purchases_updated(Array purchases) => EmitSignal(nameof(PurchasesUpdated), purchases); - private void OnGodotGooglePlayBilling_purchases_updated(Array purchases) - { - GD.Print($"GodotGooglePlayBilling PurchasesUpdated {purchases}"); - EmitSignal(nameof(PurchasesUpdated), purchases); - } + private void OnGodotGooglePlayBilling_purchase_error(int code, string message) => EmitSignal(nameof(PurchaseError), code, message); - private void OnGodotGooglePlayBilling_purchase_error(int code, string message) - { - GD.Print($"GodotGooglePlayBilling PurchaseError {code}: {message}"); - EmitSignal(nameof(PurchaseError), code, message); - } + private void OnGodotGooglePlayBilling_purchase_acknowledged(string purchaseToken) => EmitSignal(nameof(PurchaseAcknowledged), purchaseToken); - private void OnGodotGooglePlayBilling_purchase_acknowledged(string purchaseToken) - { - GD.Print($"GodotGooglePlayBilling PurchaseAcknowledged {purchaseToken}"); - EmitSignal(nameof(PurchaseAcknowledged), purchaseToken); - } + private void OnGodotGooglePlayBilling_purchase_acknowledgement_error(int code, string message) => EmitSignal(nameof(PurchaseAcknowledgementError), code, message); - private void OnGodotGooglePlayBilling_purchase_acknowledgement_error(int code, string message) - { - GD.Print($"GodotGooglePlayBilling PurchaseAcknowledgementError error {code}: {message}"); - EmitSignal(nameof(PurchaseAcknowledgementError), code, message); - } + private void OnGodotGooglePlayBilling_purchase_consumed(string purchaseToken) => EmitSignal(nameof(PurchaseConsumed), purchaseToken); - private void OnGodotGooglePlayBilling_purchase_consumed(string purchaseToken) - { - GD.Print($"GodotGooglePlayBilling PurchaseConsumed successfully: {purchaseToken}"); - EmitSignal(nameof(PurchaseConsumed), purchaseToken); - } - - private void OnGodotGooglePlayBilling_purchase_consumption_error(int code, string message, string purchaseToken) - { - GD.Print($"GodotGooglePlayBilling PurchaseConsumption_error error {code}: {message}, purchase token: {purchaseToken}"); - EmitSignal(nameof(PurchaseConsumption_error), code, message, purchaseToken); - } + private void OnGodotGooglePlayBilling_purchase_consumption_error(int code, string message, string purchaseToken) => EmitSignal(nameof(PurchaseConsumptionError), code, message, purchaseToken); #endregion } diff --git a/mono/android_iap/GodotGooglePlayBilling/GooglePlayBillingUtils.cs b/mono/android_iap/GodotGooglePlayBilling/GooglePlayBillingUtils.cs index 0c9bbca3..e1b34547 100644 --- a/mono/android_iap/GodotGooglePlayBilling/GooglePlayBillingUtils.cs +++ b/mono/android_iap/GodotGooglePlayBilling/GooglePlayBillingUtils.cs @@ -1,7 +1,7 @@ using Godot; using Godot.Collections; -namespace Android_Iap.GodotGooglePlayBilling +namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling { public static class GooglePlayBillingUtils { diff --git a/mono/android_iap/GodotGooglePlayBilling/Purchase.cs b/mono/android_iap/GodotGooglePlayBilling/Purchase.cs index 1ca8ac69..0e57e325 100644 --- a/mono/android_iap/GodotGooglePlayBilling/Purchase.cs +++ b/mono/android_iap/GodotGooglePlayBilling/Purchase.cs @@ -2,7 +2,7 @@ using System; using Godot; using Godot.Collections; -namespace Android_Iap.GodotGooglePlayBilling +namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling { // https://developer.android.com/reference/com/android/billingclient/api/Purchase.PurchaseState public enum PurchaseState diff --git a/mono/android_iap/GodotGooglePlayBilling/PurchasesResult.cs b/mono/android_iap/GodotGooglePlayBilling/PurchasesResult.cs index 427606b9..1650c1a4 100644 --- a/mono/android_iap/GodotGooglePlayBilling/PurchasesResult.cs +++ b/mono/android_iap/GodotGooglePlayBilling/PurchasesResult.cs @@ -1,7 +1,7 @@ using Godot; using Godot.Collections; -namespace Android_Iap.GodotGooglePlayBilling +namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling { public class PurchasesResult : BillingResult { diff --git a/mono/android_iap/GodotGooglePlayBilling/SkuDetails.cs b/mono/android_iap/GodotGooglePlayBilling/SkuDetails.cs index 444c70bb..2ee3abc2 100644 --- a/mono/android_iap/GodotGooglePlayBilling/SkuDetails.cs +++ b/mono/android_iap/GodotGooglePlayBilling/SkuDetails.cs @@ -2,7 +2,7 @@ using System; using Godot; using Godot.Collections; -namespace Android_Iap.GodotGooglePlayBilling +namespace AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling { public class SkuDetails { diff --git a/mono/android_iap/Main.cs b/mono/android_iap/Main.cs index 97a7bbe6..cf05a198 100644 --- a/mono/android_iap/Main.cs +++ b/mono/android_iap/Main.cs @@ -1,170 +1,82 @@ -using Android_Iap.GodotGooglePlayBilling; +using AndroidInAppPurchasesWithCSharp.GodotGooglePlayBilling; using Godot; -using CoreGeneric = System.Collections.Generic; using System.Linq; using System; -namespace Android_Iap +namespace AndroidInAppPurchasesWithCSharp { - /* - test skus - android.test.purchased - android.test.canceled - android.test.refunded - android.test.item_unavailable - */ - public class Main : Node2D + public class Main : Control { - private readonly string[] ArrInAppProductsSKUs = new string[] - { - "android.test.purchased", - "android.test.canceled", - "android.test.refunded", - "android.test.item_unavailable" - }; + const string TestItemSku = "my_in_app_purchase_sku"; + private AcceptDialog _alertDialog; + private Label _label; - private Button _buyPotionButton; - private Label _totalPotionsLabel; + private GooglePlayBilling _payment; - private Panel _panel; - private Label _processLabel; - private Label _thanksLabel; - - private ProgressBar _playerLife; - private StyleBoxFlat _playerLifeStyleBoxFlat; - - private GooglePlayBilling _googlePlayBilling; - private int _totalPotion = 5; - - CoreGeneric.Dictionary _purchases = new CoreGeneric.Dictionary(); + private string _testItemPurchaseToken; public override void _Ready() { - _googlePlayBilling = GetNode("GooglePlayBilling"); + _payment = GetNode("GooglePlayBilling"); + _alertDialog = GetNode("AlertDialog"); + _label = GetNode