diff --git a/AppCoinsUnityPlugin.sln b/AppCoinsUnityPlugin.sln index 1d0dea1..de48a0f 100644 --- a/AppCoinsUnityPlugin.sln +++ b/AppCoinsUnityPlugin.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppCoinsUnityPlugin2017", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppCoinsUnityPluginEditor2017", "AppCoinsUnityPluginEditor2017\AppCoinsUnityPluginEditor2017.csproj", "{55211CDF-39D3-490E-BC3C-CA2F20DBE61B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MigrationHelper", "MigrationHelper\MigrationHelper.csproj", "{96FC5FBE-6CDB-4624-98D7-009A15D024FE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -43,6 +45,10 @@ Global {55211CDF-39D3-490E-BC3C-CA2F20DBE61B}.Debug|Any CPU.Build.0 = Debug|Any CPU {55211CDF-39D3-490E-BC3C-CA2F20DBE61B}.Release|Any CPU.ActiveCfg = Release|Any CPU {55211CDF-39D3-490E-BC3C-CA2F20DBE61B}.Release|Any CPU.Build.0 = Release|Any CPU + {96FC5FBE-6CDB-4624-98D7-009A15D024FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {96FC5FBE-6CDB-4624-98D7-009A15D024FE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {96FC5FBE-6CDB-4624-98D7-009A15D024FE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {96FC5FBE-6CDB-4624-98D7-009A15D024FE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution outputpath = ../../AppcoinsUnityPlugin/Appcoins Unity/Assets/AppcoinsUnity/Scripts diff --git a/AppCoinsUnityPlugin2017/AppCoinsUnityPlugin2017.csproj b/AppCoinsUnityPlugin2017/AppCoinsUnityPlugin2017.csproj index 593cc9e..3ac8162 100644 --- a/AppCoinsUnityPlugin2017/AppCoinsUnityPlugin2017.csproj +++ b/AppCoinsUnityPlugin2017/AppCoinsUnityPlugin2017.csproj @@ -34,8 +34,8 @@ - - AppcoinsUnity.cs + + ASFAppcoinsUnity.cs AppcoinsSku.cs @@ -49,12 +49,21 @@ AppcoinsChecks.cs - - AppcoinsUnityEditorMode.cs - MessageHandlerGUI.cs + + AndroidAppcoinsUnityVisitor.cs + + + EditorAppcoinsUnity.cs + + + EditorAppcoinsUnityVisitor.cs + + + IAppcoinsUnityVisitor.cs + \ No newline at end of file diff --git a/AppCoinsUnityPlugin2018/AppCoinsUnityPlugin2018.csproj b/AppCoinsUnityPlugin2018/AppCoinsUnityPlugin2018.csproj index 6a545c7..5f8fd8f 100644 --- a/AppCoinsUnityPlugin2018/AppCoinsUnityPlugin2018.csproj +++ b/AppCoinsUnityPlugin2018/AppCoinsUnityPlugin2018.csproj @@ -7,7 +7,7 @@ Library AppCoinsUnityPlugin AppCoinsUnityPlugin2018 - v2.0 + v3.5 true @@ -37,9 +37,6 @@ AppcoinsPurchaser.cs - - AppcoinsUnity.cs - AppcoinsSku.cs @@ -52,8 +49,20 @@ AppcoinsChecks.cs - - AppcoinsUnityEditorMode.cs + + ASFAppcoinsUnity.cs + + + AndroidAppcoinsUnityVisitor.cs + + + EditorAppcoinsUnity.cs + + + EditorAppcoinsUnityVisitor.cs + + + IAppcoinsUnityVisitor.cs diff --git a/AppCoinsUnityPlugin5_6/AppCoinsUnityPlugin5_6.csproj b/AppCoinsUnityPlugin5_6/AppCoinsUnityPlugin5_6.csproj index 81af26b..d039e32 100644 --- a/AppCoinsUnityPlugin5_6/AppCoinsUnityPlugin5_6.csproj +++ b/AppCoinsUnityPlugin5_6/AppCoinsUnityPlugin5_6.csproj @@ -40,9 +40,6 @@ AppcoinsPurchaser.cs - - AppcoinsUnity.cs - AppcoinsSku.cs @@ -52,12 +49,24 @@ AppcoinsChecks.cs - - AppcoinsUnityEditorMode.cs - MessageHandlerGUI.cs + + AndroidAppcoinsUnityVisitor.cs + + + EditorAppcoinsUnity.cs + + + EditorAppcoinsUnityVisitor.cs + + + IAppcoinsUnityVisitor.cs + + + ASFAppcoinsUnity.cs + \ No newline at end of file diff --git a/AppCoinsUnityPluginEditor2017/AppCoinsUnityPluginEditor2017.csproj b/AppCoinsUnityPluginEditor2017/AppCoinsUnityPluginEditor2017.csproj index da4c1d3..70aaee9 100644 --- a/AppCoinsUnityPluginEditor2017/AppCoinsUnityPluginEditor2017.csproj +++ b/AppCoinsUnityPluginEditor2017/AppCoinsUnityPluginEditor2017.csproj @@ -47,15 +47,15 @@ ..\dependencies\2017\UnityEngine.dll - - ..\dependencies\2017\UnityEditor.dll - ..\AppCoinsUnityPlugin2017\bin\Debug\AppCoinsUnityPlugin2017.dll ..\dependencies\2017\AppCoinsUnitySupport.dll + + ..\dependencies\2017\UnityEditor.dll + @@ -68,6 +68,9 @@ Support\Editor\CustomBuild\CustomBuildUnityExport\CustomBuildUnityExport_2017_OR_LOWER.cs + + Support\Editor\Migration\MigrationHelper.cs + \ No newline at end of file diff --git a/AppCoinsUnityPluginEditor2018/AppCoinsUnityPluginEditor2018.csproj b/AppCoinsUnityPluginEditor2018/AppCoinsUnityPluginEditor2018.csproj index 274708b..ec20c82 100644 --- a/AppCoinsUnityPluginEditor2018/AppCoinsUnityPluginEditor2018.csproj +++ b/AppCoinsUnityPluginEditor2018/AppCoinsUnityPluginEditor2018.csproj @@ -68,8 +68,5 @@ Support\Editor\CustomBuild\CustomBuildUnityExport\CustomBuildUnityExport2018.cs - - - \ No newline at end of file diff --git a/AppCoinsUnityPluginEditor5_6/AppCoinsUnityPluginEditor5_6.csproj b/AppCoinsUnityPluginEditor5_6/AppCoinsUnityPluginEditor5_6.csproj index 22c6949..3dc35bc 100644 --- a/AppCoinsUnityPluginEditor5_6/AppCoinsUnityPluginEditor5_6.csproj +++ b/AppCoinsUnityPluginEditor5_6/AppCoinsUnityPluginEditor5_6.csproj @@ -65,6 +65,9 @@ Support\Editor\CustomBuild\CustomBuildUnityExport\CustomBuildUnityExport_2017_OR_LOWER.cs + + Support\Editor\Migration\MigrationHelper.cs + diff --git a/MigrationHelper/MigrationHelper.csproj b/MigrationHelper/MigrationHelper.csproj new file mode 100644 index 0000000..bd22e50 --- /dev/null +++ b/MigrationHelper/MigrationHelper.csproj @@ -0,0 +1,45 @@ + + + + Debug + AnyCPU + {96FC5FBE-6CDB-4624-98D7-009A15D024FE} + Library + MigrationHelper + MigrationHelper + v3.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + true + bin\Release + prompt + 4 + false + + + + + ..\dependencies\2018\UnityEditor.dll + + + ..\dependencies\2018\UnityEngine.dll + + + + + + Migration\MigrationHelper.cs + + + + \ No newline at end of file diff --git a/MigrationHelper/MyClass.cs b/MigrationHelper/MyClass.cs new file mode 100644 index 0000000..b466349 --- /dev/null +++ b/MigrationHelper/MyClass.cs @@ -0,0 +1,10 @@ +using System; +namespace MigrationHelper +{ + public class MyClass + { + public MyClass() + { + } + } +} diff --git a/MigrationHelper/Properties/AssemblyInfo.cs b/MigrationHelper/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..6adc82e --- /dev/null +++ b/MigrationHelper/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("MigrationHelper")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("${AuthorCopyright}")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/Scripts/ASFAppcoinsUnity.cs b/Scripts/ASFAppcoinsUnity.cs new file mode 100644 index 0000000..a2eb341 --- /dev/null +++ b/Scripts/ASFAppcoinsUnity.cs @@ -0,0 +1,520 @@ +using UnityEngine; + +using System; +using System.Collections.Generic; + +namespace Aptoide.AppcoinsUnity +{ + /// + /// Class that makes the communication between the game and the Platform + /// (where the game is running). + /// + /// + /// Awake + /// Unity's Awake Event. + /// + /// + /// SetupCommunication + /// + /// Setup components of the running Platform side. + /// + /// + /// + /// SetupReceiver + /// + /// Get an instance of the of the running Platform's object communicating + /// with the game. + /// + /// + /// + /// SendExceptionToReceiver + /// + /// Send Exception to the Platform's object communicating with the game. + /// + /// + /// + /// GetWalletAddress + /// Wallet Address. + /// + /// + /// SetupWalletAddress + /// + /// Give the Wallet Address to the Platform's object communicating with the + /// game. + /// + /// + /// + /// GetIAB + /// Check if IAB is enabled. + /// + /// + /// SetupIAB + /// Setup IAB component on the Platform side. + /// + /// + /// AwakeReceiver + /// + /// Game and the Platform's object communicating with the game can start the + /// communication itself. + /// + /// + /// + /// AddSKU + /// Add new SKU to skus' list. + /// + /// + /// MakePurchase + /// Begin transaction to buy a specific SKU. + /// + /// + /// PurchaseSuccess + /// + /// Callback funciton when transaction ended successfully. + /// + /// + /// + /// PurchaseFailure + /// Callback function when transaction failed. + /// + /// + /// GetSKUList + /// Send list with all registerd SKUs. + /// + /// + /// FindSKUById + /// Find specific SKU by its ID. + /// + /// + /// CheckForRepeatedSKU + /// + /// Check if new SKU has the same SKUID of other SKU in skus' list. + /// + /// + /// + /// GetGameObject + /// Get AppcoinsUnity game object. + /// + /// + /// + /// + /// + /// Aptoide.AppcoinsUnity.ASFAppcoinsUnity has 'DontDestroyOnLoad' property, so + /// it can be used at any scene (just include it in the first scene). + /// + /// + /// For now only the Android Platform and Unity Editor have a visitor + /// (IAppcoinsUnityVisitor) to communicate with + /// Aptoide.AppcoinsUnity.ASFAppcoinsUnity. + /// + /// + public class ASFAppcoinsUnity : MonoBehaviour + { + // Visitor to call depending on the platform where the game is running. + private IAppcoinsUnityVisitor appcoinsVisitor; + + // Purchase Object defined by the user. + [Header("Add your purchaser object here")] + public AppcoinsPurchaser purchaserObject; + private AppcoinsPurchaser purchaserObjChoosed; + + // Wallet address where the user want to receive appcoins. + [Header("Your wallet address for receiving Appcoins")] + public string address; + private string addressChoosed; + + // Enable In-App Billing. + [Header("Uncheck to disable Appcoins IAB")] + public bool enableIAB = true; + private bool IABChoosed; + + // Enable POA. + [Header("Uncheck to disable Appcoins ADS(Proof of attention)")] + public bool enablePOA = true; + private bool POAChoosed; + + // Enable test transactions (Ropsten net). + [Header("Enable debug to use testnets e.g Ropsten")] + public bool enableDebug = true; + private bool debugChoosed; + + // List with all registerd skus. + private List skus; + + // User can add SKUs until SetupIAB is called. + private bool canAddSku = true; + + /// + /// Chose IAppcoinsUnityVisitor visitor in terms of which platform is + /// being used to run the game. Initialize skus' list and + /// purchaserObject. + /// + /// + /// Thrown when platform choosed to integrate AppcoinsUnity Plugin and + /// run the game is not supported. + /// + /// + /// + /// Current Platform Supported: Andoroid, Unity Editor. + /// + /// + internal void Awake() + { + if (Application.isEditor) + { + appcoinsVisitor = new EditorAppcoinsUnityVisitor(); + } + + else if (Application.isMobilePlatform && + Application.platform == RuntimePlatform.Android) + { + appcoinsVisitor = new AndroidAppcoinsUnityVisitor(); + } + + else + { + throw new PlatformNotSupportedException(); + } + + skus = new List(); + + // Set up communication with Purchaser + CheckPurchaserObject(); + purchaserObject.Init(this); + + // Give access to ASFAppcoinsUnity prefab at all Scenes + DontDestroyOnLoad(this.gameObject); + + // Initialize private attributes (So even ig the user change the + // public attributes after Awake event, those changes don't + // propagate to the private values + purchaserObjChoosed = purchaserObject; + addressChoosed = address; + IABChoosed = enableIAB; + POAChoosed = enablePOA; + debugChoosed = enableDebug; + } + + /// + /// Unity Start Event. Setup communication with the receiver. + /// + internal void Start() + { + SetupCommunication(); + } + + /// + /// Setup all communication with the Platform's side object (receiver) + /// that will communicate with the game. (This includes setting up + /// receiver, give wallet address and all SKUs in skus list (if IAB is + /// enabled) to receiver. + /// + protected void SetupCommunication() + { + SetupReceiver(); + SetupWalletAddress(); + + if (IABChoosed) + { + SetupIAB(); + } + + // Awake Reveiver only when all Setup's have been done + AwakeReceiver(); + } + + /// + /// Instantiate receiver (Platform's side object to communicate with the + /// game). + /// + private void SetupReceiver() + { + appcoinsVisitor.SetupReceiver(this); + } + + /// + /// Get wallet address. + /// + /// + /// Return wallet address. + /// + /// + /// + /// This wallet address is the the receiver wallet for IAB transactions. + /// + /// + public string GetWalletAddress() + { + return address; + } + + /// + /// Pass wallet address to Platform's object communicting with the game + /// to registered and be the receiver for IAB transactions. + /// + private void SetupWalletAddress() + { + appcoinsVisitor.SetupWalletAddress(this); + } + + /// + /// Check if IAB is enabled. + /// + /// + /// true if IAB is enabled; false otherwise. + /// + public bool GetIAB() + { + return IABChoosed; + } + + /// + /// All SKU's in skus' list are passed to the platform's object + /// communicating with the game to be registered (this is done by + /// calling 'RegisterSKUs' purchaserObject method). + /// + /// + /// + /// Afeter this method being called, no more SKUs can be registered, if + /// so CannotRegisterSKUException will be thrown. + /// + /// + private void SetupIAB() + { + // Setup IAB in the platform being used + appcoinsVisitor.SetupIAB(this); + + // Register all SKU's in products list before setting up IAB + purchaserObject.RegisterSKUs(); + + // No more SKU's can be registered. + canAddSku = false; + } + + /// + /// Check if POA is enabled. + /// + /// + /// true if POA is enabled; false otherwise. + /// + public bool GetPOA() + { + return POAChoosed; + } + + /// + /// Start the communication between the game and the Platform's object + /// that is communicating with the game. + /// + /// + /// + /// This method is called after 'SetupReceiver', + /// 'SetupWalletAddress', and 'SetupIAB' methods. + /// + /// + private void AwakeReceiver() + { + appcoinsVisitor.AwakeReceiver(this); + } + + /// + /// Try to add new SKU to skus' list. If SKU + /// is already in skus' list, or is not well instantiated the repective + /// Exception will be sent. + /// + /// + /// Thrown when is set to null. + /// + /// See class that + /// implements Appcoins SKUs. + /// SKU to add to skus' list. + internal void AddSKU(AppcoinsSKU newSku) + { + if (newSku == null) + { + throw new NullSKUProductException(); + } + + if (!canAddSku) + { + throw new CannotRegisterSKUException(); + } + + // Check if 'newSku' is a valid SKU + newSku.CheckSKU(); + + // Check if 'newSku' have a sku id different from all sku id's + // in skus list + CheckForRepeatedSKU(newSku); + + // Add new sku to skus' list + skus.Add(newSku); + + // Add new sku to the platform being used + appcoinsVisitor.AddSKU(this, newSku); + } + + /// + /// If SKU is in skus' list start transaction + /// flux by passing it to the Platorm' side. + /// + /// SKU to purchase. + /// See class that + /// implements Appcoins SKUs. + /// + /// Thrown when is not in skus' list and can't be + /// added because 'SetupIAB' method has already been called. + /// + /// + /// Thrown when a purchase was tried to be made but 'enableIAB' is set + /// to fasle. + /// + internal void MakePurchase(AppcoinsSKU sku) + { + if (IABChoosed) + { + if (skus.Contains(sku)) + { + appcoinsVisitor.MakePurchase(this, sku); + } + + else + { + throw new SkuIsNotRegistedException(); + } + } + + else + { + throw new IABIsTurnedOffException(); + } + } + + /// + /// Callback function when some transaction ended successfully on the + /// Platform's side. + /// + /// Sku id + /// See class that + /// implements Appcoins SKUs. + /// + /// + /// purchaserObject.PurchaseSucess method is called (if user wants to + /// handle successful transactions). + /// + /// + private void PurchaseSuccess(string skuId) + { + purchaserObject.PurchaseSuccess(FindSKUById(skuId)); + } + + /// + /// Callback function when some transaction failed on the Platform's + /// side. + /// + /// Sku id + /// See class that + /// implements Appcoins SKUs. + /// + /// + /// purchaserObject.PurchaseFailure method is called (if user wants to + /// handle failed transactions). + /// + /// + private void PurchaseFailure(string skuId) + { + purchaserObject.PurchaseFailure(FindSKUById(skuId)); + } + + /// + /// Get skus' list. + /// + /// + /// System.Collections.Generic.List with all registered SKU's. + /// + public List GetSKUList() + { + return new List(skus); + } + + /// + /// Find SKU, in skus list, with a the specific sku id + /// . + /// + /// + /// Return Aptoide.AppcoinsUnity.AppcoinsSKU, sku in skus list with + /// a SKUID as the same as ." + /// + /// See class that + /// implements Appcoins SKUs. + /// Sku id + public AppcoinsSKU FindSKUById(string skuid) + { + return skus.Find(sku => sku.GetSKUId().Equals(skuid)); + } + + /// + /// Check if 'purchaserObject is configurated (not null). + /// + /// + /// Thrown when 'purchaserObject' is null. + /// + private void CheckPurchaserObject() + { + if (purchaserObject == null) + { + throw new PurchaserObjectIsNullException(); + } + } + + /// + /// Check if new SKU have the same SKUID of + /// any SKU in skus' list. + /// + /// + /// Thrown when have the same SKUID of another + /// skus' list element. + /// + /// See class that + /// implements Appcoins SKUs. + /// Appcoins SKU + private void CheckForRepeatedSKU(AppcoinsSKU newSKU) + { + foreach(AppcoinsSKU sku in skus) + { + if (sku.Equals(newSKU)) + { + throw new RepeatedSKUProductException(); + } + } + } + + /// + /// Check if (Wallet address) is valid. + /// If not throw + /// . + /// + /// + /// Thrown when is not valid. + /// + public void CheckWalletAddress() + { + if (address == null || address.Equals("")) + { + throw new WalletAddressIsInvalidException(); + } + } + + /// + /// Get Aptoide.AppcoinsUnity.AppcoinsUnity game object. + /// + /// + /// UnityEngine.GameObject that contains + /// Aptoide.AppcoinsUnity.AppcoinUnity component. + /// + /// See + public GameObject GetGameObject() + { + return gameObject; + } + } +} \ No newline at end of file diff --git a/Scripts/AndroidAppcoinsUnityVisitor.cs b/Scripts/AndroidAppcoinsUnityVisitor.cs new file mode 100644 index 0000000..fcc483d --- /dev/null +++ b/Scripts/AndroidAppcoinsUnityVisitor.cs @@ -0,0 +1,109 @@ +// created by Lukmon Agboola(Codeberg) +// Modified by Aptoide +// Note: do not change anything here as it may break the workings of the plugin +// else you're very sure of what you're doing. +using UnityEngine; + +using System.Collections.Generic; +using System; + +namespace Aptoide.AppcoinsUnity +{ + public class AndroidAppcoinsUnityVisitor : IAppcoinsUnityVisitor + { + // Platform side oject to communicate with. + private AndroidJavaClass androidClass; + + // Instance of andoridClass + private AndroidJavaObject instance + { + get + { + return androidClass.GetStatic("instance"); + } + } + + /// + /// Instantiate java class to be the receiver in an Andorid enviornment. + /// + /// + /// object. + /// + public void SetupReceiver(ASFAppcoinsUnity appcoinsUnity) + { + //get refference to java class + androidClass = + new AndroidJavaClass("com.aptoide.appcoinsunity.UnityAppcoins"); + } + + /// + /// Give wallet address to receiver. + /// + /// + /// object. + /// + public void SetupWalletAddress(ASFAppcoinsUnity appcoinsUnity) + { + //setup wallet address + androidClass.CallStatic("setAddress", + appcoinsUnity.GetWalletAddress() + ); + } + + /// + /// Enable IAB on the receiver's side. + /// + /// + /// object. + /// + public void SetupIAB(ASFAppcoinsUnity appcoinsUnity) + { + //Enable or disable In App Billing + androidClass.CallStatic("enableIAB", appcoinsUnity.GetIAB()); + } + + /// + /// Start receiver lifecycle (Initialize EditorAppcoinsUnity). + /// + /// + /// object. + /// + public void AwakeReceiver(ASFAppcoinsUnity appcoinsUnity) + { + //start sdk + androidClass.CallStatic("start"); + } + + /// + /// Give new SKU to receiver. + /// + /// + /// object. + /// + /// new + /// to give to receiver. + /// + public void AddSKU(ASFAppcoinsUnity appcoinsUnity, AppcoinsSKU newSku) + { + //Add SKU to java class + androidClass.CallStatic("addNewSku", newSku.GetName(), + newSku.GetSKUId(), newSku.GetPrice()); + } + + /// + /// Pass SKU to receiver to start the transaction. + /// + /// + /// object. + /// + /// + /// + /// to be purchased. + /// + public void MakePurchase(ASFAppcoinsUnity appcoinsUnity, + AppcoinsSKU sku) + { + androidClass.CallStatic("makePurchase", sku.GetSKUId()); + } + } +} //namespace Aptoide.AppcoinsUnity diff --git a/Scripts/AppcoinsChecks.cs b/Scripts/AppcoinsChecks.cs index bfc13b6..bbb5932 100644 --- a/Scripts/AppcoinsChecks.cs +++ b/Scripts/AppcoinsChecks.cs @@ -56,11 +56,13 @@ public static void CheckForRepeatedSkuId(List products) { AppcoinsSKU compareProduct = products[j]; - if (currentProduct != null && currentProduct.SKUID.Length == - compareProduct.SKUID.Length + if (currentProduct != null && + currentProduct.GetSKUId().Length == + compareProduct.GetSKUId().Length ) { - if (currentProduct.SKUID.Equals(compareProduct.SKUID)) + if (currentProduct.GetSKUId(). + Equals(compareProduct.GetSKUId())) { throw new RepeatedSKUProductException(); } @@ -69,12 +71,12 @@ public static void CheckForRepeatedSkuId(List products) } } - public static bool CheckPoAActive(AppcoinsUnity a) + public static bool CheckPoAActive(ASFAppcoinsUnity a) { return a.enablePOA == true ? true : false; } - public static bool CheckPurchaserObject(AppcoinsUnity a) + public static bool CheckPurchaserObject(ASFAppcoinsUnity a) { if (a.purchaserObject == null) { @@ -86,13 +88,13 @@ public static bool CheckPurchaserObject(AppcoinsUnity a) internal static bool IgnoreSKU(List products, AppcoinsSKU product) { - if (product == null || product.SKUID.Equals("")) + if (product == null || product.GetSKUId().Equals("")) { return true; } if (products.FindAll( - sku => sku.SKUID.Equals(product.SKUID) + sku => sku.GetSKUId().Equals(product.GetSKUId()) ).Count > 2 ) { diff --git a/Scripts/AppcoinsException.cs b/Scripts/AppcoinsException.cs index da0f4b8..db8df25 100644 --- a/Scripts/AppcoinsException.cs +++ b/Scripts/AppcoinsException.cs @@ -1,146 +1,320 @@ using System; -public class AppcoinsException : Exception +namespace Aptoide.AppcoinsUnity { - public readonly string message; - public AppcoinsException() + public class AppcoinsException : Exception { + public readonly string message; + public AppcoinsException() + { + } + + public AppcoinsException(string mes) : base(mes) + { + message = mes; + } + + public AppcoinsException(string mes, Exception inner) + : base(mes, inner) + { + message = mes; + } } - - public AppcoinsException(string mes) : base(mes) + + public class MoreThanOneAppcoinsPrefabException : AppcoinsException { - message = mes; + const string _message = "It was encoutered more than one AppcoinsUnity" + + "prefab in all open scenes. Please make sure that you only have one." + + "(AppcoinsUnity prefab has the property: 'DontDestroyOnLoad', so you " + + "have access to it at all scenes."; + + public MoreThanOneAppcoinsPrefabException() : base(_message) + { + } + + public MoreThanOneAppcoinsPrefabException(string new_message) + : base(new_message) + { + } + + public MoreThanOneAppcoinsPrefabException(Exception inner) + : base(_message, inner) + { + } + + public MoreThanOneAppcoinsPrefabException(string new_message, + Exception inner) : base(new_message, inner) + { + } } - - public AppcoinsException(string mes, Exception inner) - : base(mes, inner) + + public class NoSKUProductsException : AppcoinsException { - message = mes; + const string _message = "Appcoins Unity prefab has no products available."; + + public NoSKUProductsException() : base(_message) + { + } + + public NoSKUProductsException(string new_message) + : base(new_message) + { + } + + public NoSKUProductsException(Exception inner) + : base(_message, inner) + { + } + + public NoSKUProductsException(string new_message, + Exception inner) : base(new_message, inner) + { + } } -} - -public class MoreThanOneAppcoinsPrefabException : AppcoinsException -{ - const string _message = "It was encoutered more than one AppcoinsUnity" + - "prefab in all open scenes. Please make sure that you only have one." + - "(AppcoinsUnity prefab has the property: 'DontDestroyOnLoad', so you " + - "have access to it at all scenes."; - - public MoreThanOneAppcoinsPrefabException() : base(_message) + + public class NullSKUProductException : AppcoinsException { + private const string _message = "AppcoinsUnity prefab has one or more " + + "null products."; + + public NullSKUProductException() : base(_message) + { + } + + public NullSKUProductException(string new_message) + : base(new_message) + { + } + + public NullSKUProductException(Exception inner) + : base(_message, inner) + { + } + + public NullSKUProductException(string new_message, + Exception inner) : base(new_message, inner) + { + } } - - public MoreThanOneAppcoinsPrefabException(string new_message) - : base(new_message) + + public class RepeatedSKUProductException : AppcoinsException { + const string _message = "AppcoinsUnity prefab has two or more products " + + "with the same SKU Id"; + + public RepeatedSKUProductException() : base(_message) + { + } + + public RepeatedSKUProductException(string new_message) + : base(new_message) + { + } + + public RepeatedSKUProductException(Exception inner) + : base(_message, inner) + { + } + + public RepeatedSKUProductException(string new_message, + Exception inner) : base(new_message, inner) + { + } } - - public MoreThanOneAppcoinsPrefabException(Exception inner) - : base(_message, inner) + + public class InvalidSKUIdException : AppcoinsException { + const string _message = "AppcoinsUnity prefab has one or more products " + + "with an invalid SKU Id"; + + public InvalidSKUIdException() : base(_message) + { + } + + public InvalidSKUIdException(string new_message) + : base(new_message) + { + } + + public InvalidSKUIdException(Exception inner) + : base(_message, inner) + { + } + + public InvalidSKUIdException(string new_message, + Exception inner) : base(new_message, inner) + { + } } - - public MoreThanOneAppcoinsPrefabException(string new_message, - Exception inner) : base(new_message, inner) + + public class InvalidSKUPriceException : AppcoinsException { + const string _message = "SKU price is negative"; + + public InvalidSKUPriceException() : base(_message) + { + } + + public InvalidSKUPriceException(string new_message) + : base(new_message) + { + } + + public InvalidSKUPriceException(Exception inner) + : base(_message, inner) + { + } + + public InvalidSKUPriceException(string new_message, + Exception inner) : base(new_message, inner) + { + } } -} - -public class NoSKUProductsException : AppcoinsException -{ - const string _message = "Appcoins Unity prefab has no products available."; - - public NoSKUProductsException() : base(_message) + + public class PurchaserObjectIsNullException : AppcoinsException { + const string _message = "ASFAppcoinsUnity purchaserObject is not defined"; + + public PurchaserObjectIsNullException() : base(_message) + { + } + + public PurchaserObjectIsNullException(string new_message) + : base(new_message) + { + } + + public PurchaserObjectIsNullException(Exception inner) + : base(_message, inner) + { + } + + public PurchaserObjectIsNullException(string new_message, + Exception inner) : base(new_message, inner) + { + } } - - public NoSKUProductsException(string new_message) - : base(new_message) + + public class IABIsTurnedOffException : AppcoinsException { + const string _message = "Trying to make a purchase when is IAB turned off"; + + public IABIsTurnedOffException() : base(_message) + { + } + + public IABIsTurnedOffException(string new_message) + : base(new_message) + { + } + + public IABIsTurnedOffException(Exception inner) + : base(_message, inner) + { + } + + public IABIsTurnedOffException(string new_message, + Exception inner) : base(new_message, inner) + { + } } - - public NoSKUProductsException(Exception inner) - : base(_message, inner) + + public class SkuIsNotRegistedException : AppcoinsException { + const string _message = "Trying to purchase an unregistered SKU"; + + public SkuIsNotRegistedException() : base(_message) + { + } + + public SkuIsNotRegistedException(string new_message) + : base(new_message) + { + } + + public SkuIsNotRegistedException(Exception inner) + : base(_message, inner) + { + } + + public SkuIsNotRegistedException(string new_message, + Exception inner) : base(new_message, inner) + { + } } - public NoSKUProductsException(string new_message, - Exception inner) : base(new_message, inner) + public class CannotRegisterSKUException : AppcoinsException { - } -} + const string _message = "SKU register time experied (perhaps 'SetupIAB'" + + " method has already been called"; -public class NullSKUProductException : AppcoinsException -{ - private const string _message = "AppcoinsUnity prefab has one or more " + - "null products."; + public CannotRegisterSKUException() : base(_message) + { + } - public NullSKUProductException() : base(_message) - { - } + public CannotRegisterSKUException(string new_message) + : base(new_message) + { + } - public NullSKUProductException(string new_message) - : base(new_message) - { - } + public CannotRegisterSKUException(Exception inner) + : base(_message, inner) + { + } - public NullSKUProductException(Exception inner) - : base(_message, inner) - { + public CannotRegisterSKUException(string new_message, + Exception inner) : base(new_message, inner) + { + } } - public NullSKUProductException(string new_message, - Exception inner) : base(new_message, inner) + public class PlatformNotSupportedException : AppcoinsException { - } -} + const string _message = "Platform choosed to integrate AppcoinsUnity " + + "Plugin is not supported."; -public class RepeatedSKUProductException : AppcoinsException -{ - const string _message = "AppcoinsUnity prefab has two or more products " + - "with the same SKU Id"; + public PlatformNotSupportedException() : base(_message) + { + } - public RepeatedSKUProductException() : base(_message) - { - } + public PlatformNotSupportedException(string new_message) + : base(new_message) + { + } - public RepeatedSKUProductException(string new_message) - : base(new_message) - { - } + public PlatformNotSupportedException(Exception inner) + : base(_message, inner) + { + } - public RepeatedSKUProductException(Exception inner) - : base(_message, inner) - { + public PlatformNotSupportedException(string new_message, + Exception inner) : base(new_message, inner) + { + } } - public RepeatedSKUProductException(string new_message, - Exception inner) : base(new_message, inner) + public class WalletAddressIsInvalidException : AppcoinsException { - } -} + const string _message = "Wallet address at ASFAppcoinsUnity game " + + "object is not valid."; -public class InvalidSKUIdException : AppcoinsException -{ - const string _message = "AppcoinsUnity prefab has one or more products " + - "with an invalid SKU Id"; + public WalletAddressIsInvalidException() : base(_message) + { + } - public InvalidSKUIdException() : base(_message) - { - } + public WalletAddressIsInvalidException(string new_message) + : base(new_message) + { + } - public InvalidSKUIdException(string new_message) - : base(new_message) - { - } + public WalletAddressIsInvalidException(Exception inner) + : base(_message, inner) + { + } - public InvalidSKUIdException(Exception inner) - : base(_message, inner) - { - } - - public InvalidSKUIdException(string new_message, - Exception inner) : base(new_message, inner) - { + public WalletAddressIsInvalidException(string new_message, + Exception inner) : base(new_message, inner) + { + } } -} +} \ No newline at end of file diff --git a/Scripts/AppcoinsPurchaser.cs b/Scripts/AppcoinsPurchaser.cs index e9f3411..8c5a805 100644 --- a/Scripts/AppcoinsPurchaser.cs +++ b/Scripts/AppcoinsPurchaser.cs @@ -8,42 +8,137 @@ namespace Aptoide.AppcoinsUnity { + /// + /// Class that instatiated an AppCoins SKU. + /// + /// + /// Init + /// Get reference to ASFAppcoinsUnity. + /// + /// + /// PurchaseSuccess + /// + /// Callback function when a transaction finished successfully. + /// + /// + /// + /// PurchaseFailure + /// Callback function when a transaction failed. + /// + /// + /// MakePurchase + /// Initiate a purchase. + /// + /// + /// AddSKU + /// + /// Add new SKU to ASFAppcoinsUnity. + /// + /// + /// + /// RegisterSKUs + /// + /// Method to be called by ASFAppcoinsUnity at unity Start Event. + /// + /// + /// + /// + /// + /// + /// AppcoinsSKU can be instatiated without a name. + /// + /// public abstract class AppcoinsPurchaser : MonoBehaviour { + // Reference to ASFAppcoinsUnity Component + ASFAppcoinsUnity appcoinsUnity; - AppcoinsUnity appcoinsUnity; - - public void Init(AppcoinsUnity appcoinsUnityRef) + /// + /// Get reference of ASFAppcoinsUnity Component + /// + /// ASFAppcoinsUnity reference. + public void Init(ASFAppcoinsUnity appcoinsUnityRef) { - //get refference to AppcoinsUnity class + //get reference to ASFAppcoinsUnity class appcoinsUnity = appcoinsUnityRef; } - public virtual void PurchaseTest(string sku) - { - - } - - public virtual void PurchaseSuccess(string sku) + /// + /// Callback function when a transaction finish successfully. + /// + /// SKU purchased. + /// + /// + /// This method is empty and its main purpose is to the user override it + /// with his custom implementation when a SKU is purchased. + /// + /// + public virtual void PurchaseSuccess(AppcoinsSKU sku) { - + // Nothing to do } - public virtual void PurchaseFailure(string sku) + /// + /// Callback function when a transaction fails. + /// + /// SKU tryied to purchase. + /// + /// + /// This method is empty and its main purpose is to the user override it + /// with his custom implementation when a purchase fails. + /// + /// + public virtual void PurchaseFailure(AppcoinsSKU sku) { - + // Nothing to do } - public void MakePurchase(string sku) + /// + /// Request to ASFAppcoinsUnity to initiate a transaction of + /// . + /// + /// + /// . + /// + /// + /// + /// If enableIAB attribute of + /// is false a + /// Exception will be thrown + /// . + /// + /// + /// If is not registered a Exception will be + /// thrown . + /// + /// + public void MakePurchase(AppcoinsSKU sku) { - appcoinsUnity.MakePurchase (sku); + appcoinsUnity.MakePurchase(sku); } - public void AddSKU(AppcoinsSKU newProduct) + /// + /// Register new . + /// + /// New + /// to register. + /// + public void AddSKU(AppcoinsSKU newSku) { - appcoinsUnity.AddSKU(newProduct); + appcoinsUnity.AddSKU(newSku); } + /// + /// Method to be called by + /// to register all + /// SKUs. + /// + /// + /// + /// We highly recommend add all wanted SKUs by calling 'AddSKU' method + /// inside this method. + /// + /// public abstract void RegisterSKUs(); } } //namespace Aptoide.AppcoinsUnity diff --git a/Scripts/AppcoinsSku.cs b/Scripts/AppcoinsSku.cs index 8fff8b4..af1a8ff 100644 --- a/Scripts/AppcoinsSku.cs +++ b/Scripts/AppcoinsSku.cs @@ -1,42 +1,212 @@ //created by Lukmon Agboola(Codeberg) //Modifief by Aptoide +using System; using System.Collections; +using System.Collections.Generic; using UnityEngine; namespace Aptoide.AppcoinsUnity{ + /// + /// Class that instatiated an AppCoins SKU. + /// + /// + /// AppcoinsSkU + /// AppcoinsSKU constructor. + /// + /// + /// GetName + /// Get the name of Appcoins SKU object. + /// + /// + /// GetSKUId + /// Get the ID of Appcoins SKU object. + /// + /// + /// GetPrice + /// Get the price of Appcoins SKU object. + /// + /// + /// CheckSKU + /// + /// Check if Appcoins SKU object is well instatiated. + /// + /// + /// + /// CheckSKUId + /// + /// Check if the ID of Appcoins SKU object is valid. + /// + /// + /// + /// CheckPrice + /// + /// Check if the price of Appcoins SKU object is valid. + /// + /// + /// + /// Equals + /// + /// Check if two Appcoins SKU objects are the same. + /// + /// + /// + /// GetHashCode + /// Get the hash code of Appcoins SKU object. + /// + /// + /// + /// + /// + /// AppcoinsSKU can be instatiated without a name. + /// + /// public class AppcoinsSKU { - public string Name; - public string SKUID; - public double Price; + // SKU name + public string name; - public AppcoinsSKU(string skuid, double price) + // SKU ID + public string skuID; + + // SKU Price + public double price; + + /// + /// Initializes a new instance of the + /// class. + /// + /// SKU ID. + /// SKU price. + public AppcoinsSKU(string skuid, double p) { - //CheckSKUID(skuid); - //CheckPrice(price); + CheckSKUId(skuid); + CheckPrice(p); - SKUID = skuid; - Price = price; + name = ""; + skuID = skuid; + price = p; } - public AppcoinsSKU(string name, string skuid, double price) : - this(skuid, price) + /// + /// Initializes a new instance of the + /// class. + /// + /// SKU name. + /// SKU ID. + /// SKU price. + public AppcoinsSKU(string n, string skuid, double p) : + this(skuid, p) { - Name = name; + name = n; } - //private void CheckSKUID(string skuid) - //{ - // if (skuid == null || skuid.Equals("")) - // { - // throw new InvalidSKUIdException(); - // } - //} + /// + /// Get the SKU name. + /// + /// The SKU name. + public string GetName() + { + return name; + } + + /// + /// Get the SKU ID. + /// + /// The SKU ID. + public string GetSKUId() + { + return skuID; + } + + /// + /// Get the SKU price. + /// + /// The SKU price. + public double GetPrice() + { + return price; + } + + /// + /// Check if the Appcoins SKU is well instatiated (if has a valid ID and + /// a valid price). + /// + public void CheckSKU() + { + CheckSKUId(skuID); + CheckPrice(price); + } - //private void CheckPrice(double price) - //{ - //} + /// + /// Check if SKU ID is valid (If is not null or an empty string). + /// + /// + /// Thrown when SKU ID is null or an empty string. + /// + /// SKU ID. + private void CheckSKUId(string id) + { + if (id == null || id.Equals("")) + { + throw new InvalidSKUIdException(); + } + } + + /// + /// Check if SKU price is valid (Greater or equal than 0). + /// + /// + /// Thrown when SKU price is less than 0. + /// + /// SKU price. + private void CheckPrice(double p) + { + if (p < 0) + { + throw new InvalidSKUPriceException(); + } + } + + /// + /// Determines whether the specified is equal to + /// the current . + /// + /// The to compare with the + /// current . + /// + /// true if the specified is equal to the + /// current + /// (if both have the same SKU ID); + /// otherwise, false. + /// + /// + /// Thrown when is not an Object of + /// . + /// + public override bool Equals(System.Object obj) + { + if (!(obj is AppcoinsSKU otherSKU)) + { + throw new NullSKUProductException(); + } + + return skuID.Equals(otherSKU.GetSKUId()) ? true : false; + } + + /// + /// Serves as a hash function for a + /// object. + /// + /// + /// A hash code for this instance that is suitable for use in hashing + /// algorithms and data structures such as a hash table. + /// + public override int GetHashCode() + { + return 714161071 + + EqualityComparer.Default.GetHashCode(skuID); + } } } //namespace Aptoide.AppcoinsUnity diff --git a/Scripts/AppcoinsUnity.cs b/Scripts/AppcoinsUnity.cs deleted file mode 100644 index b63d28e..0000000 --- a/Scripts/AppcoinsUnity.cs +++ /dev/null @@ -1,178 +0,0 @@ -// created by Lukmon Agboola(Codeberg) -// Modified by Aptoide -// Note: do not change anything here as it may break the workings of the plugin -// else you're very sure of what you're doing. -using UnityEngine; - -using System.Collections.Generic; - -namespace Aptoide.AppcoinsUnity -{ - public class AppcoinsUnity : MonoBehaviour - { - public static string POA = "POA"; - public static string DEBUG = "DEBUG"; - public static string APPCOINS_PREFAB = "APPCOINS_PREFAB"; - - [Header("Your wallet address for receiving Appcoins")] - public string receivingAddress; - [Header("Uncheck to disable Appcoins IAB")] - public bool enableIAB = true; - [Header("Uncheck to disable Appcoins ADS(Proof of attention)")] - public bool enablePOA = true; - [Header("Enable debug to use testnets e.g Ropsten")] - public bool enableDebug = true; - [Header("Add all your products here")] - private List products; - [Header("Add your purchaser object here")] - public AppcoinsPurchaser purchaserObject; - - AndroidJavaClass _class; - AndroidJavaObject instance { get { - return _class.GetStatic("instance"); - } } - - private AppcoinsUnityEditorMode appcoinsEditorMode; - - private void Awake() - { - products = new List(); - - if (purchaserObject != null) - { - purchaserObject.Init(this); - } - - DontDestroyOnLoad(this.gameObject); - } - - // Use this for initialization - void Start() - { - if (!Application.isEditor) - { - //get refference to java class - _class = new AndroidJavaClass("com.aptoide.appcoinsunity." + - "UnityAppcoins"); - - //setup wallet address - _class.CallStatic("setAddress", receivingAddress); - - //Enable or disable In App Billing - _class.CallStatic("enableIAB", enableIAB); - - // MessageHandlerGUI is meant to just run in editor mode - Destroy(GetComponent()); - } - - SetupIAB(); - - if (Application.isEditor) - { - appcoinsEditorMode = - new AppcoinsUnityEditorMode( - this, - GetComponent() - ); - - appcoinsEditorMode.Start(false); - } - - } - - internal void SetupIAB() - { - if (purchaserObject != null) - { - purchaserObject.RegisterSKUs(); - } - - if (!Application.isEditor) - { - //start sdk - _class.CallStatic("start"); - } - } - - public List GetProductList() - { - return products; - } - - internal void AddSKU(AppcoinsSKU newProduct) - { - products.Add(newProduct); - AddSKUToJava(newProduct); - } - - private void AddSKUToJava(AppcoinsSKU newProduct) - { - if (!Application.isEditor) - { - _class.CallStatic("addNewSku", newProduct.Name, - newProduct.SKUID, newProduct.Price); - } - } - - - //method used in making purchase - public void MakePurchase(string skuid) - { - if (!enableIAB) - { - Debug.LogWarning("Tried to make a purchase but enableIAB is " + - "false! Please set it to true on " + - "AppcoinsUnity object before using this " + - "functionality"); - return; - } - - if (Application.isEditor) - { - appcoinsEditorMode.MakePurchase(skuid); - } - - else - { - _class.CallStatic("makePurchase", skuid); - } - } - - //callback on successful purchases - public void PurchaseSuccess(string skuid) - { - if (purchaserObject != null) - { - Debug.Log("Going to call purchaseSuccess on purchaserObject " + - "skuid " + skuid); - - purchaserObject.PurchaseSuccess(skuid); - } - else - { - Debug.Log("purchaserObject is null"); - } - } - - //callback on failed purchases - public void PurchaseFailure(string skuid) - { - if (purchaserObject != null) - { - Debug.Log("Going to call purchaseFailure on purchaserObject " + - "skuid " + skuid); - - purchaserObject.PurchaseFailure(skuid); - } - else - { - Debug.Log("purchaserObject is null"); - } - } - - private AppcoinsSKU FindSKUById(string skuid) - { - return products.Find(sku => sku.SKUID.Equals(skuid)); - } - } -} //namespace Aptoide.AppcoinsUnity diff --git a/Scripts/AppcoinsUnityDecorator.cs b/Scripts/AppcoinsUnityDecorator.cs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Scripts/AppcoinsUnityDecorator.cs @@ -0,0 +1 @@ + diff --git a/Scripts/AppcoinsUnityEditorMode.cs b/Scripts/AppcoinsUnityEditorMode.cs index bbaafa0..7e6392b 100644 --- a/Scripts/AppcoinsUnityEditorMode.cs +++ b/Scripts/AppcoinsUnityEditorMode.cs @@ -11,7 +11,7 @@ namespace Aptoide.AppcoinsUnity public class AppcoinsUnityEditorMode { - AppcoinsUnity appcoinsUnity; + ASFAppcoinsUnity appcoinsUnity; MessageHandlerGUI messHandler; string skuID; @@ -22,7 +22,7 @@ public class AppcoinsUnityEditorMode private const string testFail = "Test Failure"; // Use this for initialization - internal AppcoinsUnityEditorMode(AppcoinsUnity a, MessageHandlerGUI mH) + internal AppcoinsUnityEditorMode(ASFAppcoinsUnity a, MessageHandlerGUI mH) { appcoinsUnity = a; messHandler = mH; @@ -70,7 +70,7 @@ internal void CheckProducts(bool a) try { - AppcoinsChecks.DefaultFullCheck(appcoinsUnity.GetProductList()); + AppcoinsChecks.DefaultFullCheck(appcoinsUnity.GetSKUList()); } catch (Exception e) { diff --git a/Scripts/CreateAppcoinsGameObject.cs b/Scripts/CreateAppcoinsGameObject.cs new file mode 100644 index 0000000..c4068ec --- /dev/null +++ b/Scripts/CreateAppcoinsGameObject.cs @@ -0,0 +1,43 @@ +using UnityEngine; + +namespace Aptoide.AppcoinsUnity +{ + class CreateAppcoinsGameObject : MonoBehaviour + { + [Header("Your wallet address for receiving Appcoins")] + public string receivingAddress; + + [Header("Uncheck to disable Appcoins IAB")] + public bool enableIAB = true; + + [Header("Uncheck to disable Appcoins ADS(Proof of attention)")] + public bool enablePOA = true; + + [Header("Enable debug to use testnets e.g Ropsten")] + public bool enableDebug = true; + + [Header("Add your purchaser object here")] + public AppcoinsPurchaser purchaserObject; + + private void Awake() + { + if (Application.isEditor) + { + gameObject.AddComponent(typeof(EditorAppcoinsUnity)); + GetComponent().Init(receivingAddress, + enableIAB, enablePOA, + enableDebug); + } + + else if(Application.isMobilePlatform && + Application.platform == RuntimePlatform.Android + ) + { + gameObject.AddComponent(typeof(AndroidAppcoinsUnity)); + GetComponent().Init(receivingAddress, + enableIAB, enablePOA, + enableDebug); + } + } + } +} \ No newline at end of file diff --git a/Scripts/EditorAppcoinsUnity.cs b/Scripts/EditorAppcoinsUnity.cs new file mode 100644 index 0000000..c48c675 --- /dev/null +++ b/Scripts/EditorAppcoinsUnity.cs @@ -0,0 +1,95 @@ +using UnityEngine; + +using System; +using System.Collections; + +namespace Aptoide.AppcoinsUnity { + public class EditorAppcoinsUnity : MonoBehaviour + { + MessageHandlerGUI messageHandler; + + private string title = "AppCoins Unity Integration"; + private string ok = "Got it"; + private string testSuc = "Test Success"; + private string testFail = "Test Failure"; + + AppcoinsSKU currentSku; + + internal void Setup() + { + gameObject.AddComponent(typeof(MessageHandlerGUI)); + messageHandler = gameObject.GetComponent(); + } + + internal void Init() + { + // Setup editor window + messageHandler.InitializeWindow(); + + // Show POA Information + if (gameObject.GetComponent().GetPOA()) + { + SetupMessage(title, "POA should have started now.", ok, null); + } + } + + private IEnumerator WaitFor(bool var, bool res) + { + while (!(var == res)) + { + yield return null; + } + + yield break; + } + + internal void SetupMessage(string t, string content, string suc, + string fail) + { + messageHandler.ChangeContent(t, content, suc, fail); + messageHandler.Enable(); + + // Wait until 'ok' button is clicked + StartCoroutine(WaitFor(messageHandler.enabled, true)); + } + + //method used in making purchase + internal void MakePurchase(AppcoinsSKU sku) + { + currentSku = sku; + + string content = "Appcoins Unity: Simulate Purchase of " + + sku.GetName() + " (" + sku.GetSKUId() + ")."; + + messageHandler.prop.AddListener(ProcessPurchase); + // Create window to simulate purchase + SetupMessage(title, content, testSuc, testFail); + } + + private void ProcessPurchase(bool purchaseResult) + { + messageHandler.prop.RemoveAllListeners(); + + if (purchaseResult) + { + string content = "Purchase of " + currentSku.GetName() + " (" + + currentSku.GetSKUId() + ") finished " + + "successfully."; + SetupMessage(title, content, ok, null); + + gameObject.SendMessage("PurchaseSuccess", + currentSku.GetSKUId() + ); + } + + else + { + string content = "Purchase of " + currentSku.GetName() + " (" + + currentSku.GetSKUId() + ") failed."; + SetupMessage(title, content, ok, null); + + gameObject.SendMessage("PurchaseFailure", currentSku.GetSKUId()); + } + } + } +} //namespace Aptoide.AppcoinsUnity \ No newline at end of file diff --git a/Scripts/EditorAppcoinsUnityVisitor.cs b/Scripts/EditorAppcoinsUnityVisitor.cs new file mode 100644 index 0000000..a767ceb --- /dev/null +++ b/Scripts/EditorAppcoinsUnityVisitor.cs @@ -0,0 +1,125 @@ +using System; + +namespace Aptoide.AppcoinsUnity +{ + /// + /// Concrete class to that implements + /// interface. + /// + /// + /// SetupReceiver + /// AppcoinsSKU constructor. + /// + /// + /// SetupWalletAddress + /// Get the name of Appcoins SKU object. + /// + /// + /// SetupIAB + /// Get the ID of Appcoins SKU object. + /// + /// + /// AwakeReceiver + /// Get the price of Appcoins SKU object. + /// + /// + /// AddSKU + /// + /// Check if Appcoins SKU object is well instatiated. + /// + /// + /// + /// MakePurchase + /// + /// Check if the ID of Appcoins SKU object is valid. + /// + /// + /// + /// + public class EditorAppcoinsUnityVisitor : IAppcoinsUnityVisitor + { + // Platform side oject to communicate with. + private EditorAppcoinsUnity editorAppc; + + /// + /// Create EditorAppcoinsUnity Component to test ASF plugin integration. + /// (simulaing purchases). + /// + /// + /// object. + /// + public void SetupReceiver(ASFAppcoinsUnity appcoinsUnity) + { + // Add EditorAppcoinsUnity Component to ASFAppcoinsUnity game object + editorAppc = appcoinsUnity.GetGameObject().AddComponent( + typeof(EditorAppcoinsUnity) + ) as EditorAppcoinsUnity; + + // Set up EditorAppcoinsUnity Component. + editorAppc.Setup(); + } + + /// + /// Give wallet address to receiver. + /// + /// + /// object. + /// + public void SetupWalletAddress(ASFAppcoinsUnity appcoinsUnity) + { + // Nothing to do + } + + /// + /// Enable IAB on the receiver's side. + /// + /// + /// object. + /// + public void SetupIAB(ASFAppcoinsUnity appcoinsUnity) + { + // Nothing to do + } + + /// + /// Start receiver lifecycle (Initialize EditorAppcoinsUnity). + /// + /// + /// object. + /// + public void AwakeReceiver(ASFAppcoinsUnity appcoinsUnity) + { + editorAppc.Init(); + } + + /// + /// Give new SKU to receiver. + /// + /// + /// object. + /// + /// new + /// to give to receiver. + /// + public void AddSKU(ASFAppcoinsUnity appcoinsUnity, AppcoinsSKU newSku) + { + // Nothing to do + } + + /// + /// Pass SKU to receiver to start the transaction. + /// + /// + /// object. + /// + /// + /// + /// to be purchased. + /// + public void MakePurchase(ASFAppcoinsUnity appcoinsUnity, + AppcoinsSKU sku) + { + editorAppc.MakePurchase(sku); + } + } +} \ No newline at end of file diff --git a/Scripts/IAppcoinsUnity.cs b/Scripts/IAppcoinsUnity.cs new file mode 100644 index 0000000..e848246 --- /dev/null +++ b/Scripts/IAppcoinsUnity.cs @@ -0,0 +1,9 @@ +namespace Aptoide.AppcoinsUnity +{ + internal abstract class AppcoinsUnity + { + void SetupAddress(string address); + void SetupIAB(); + void AddSKU(AppcoinsSKU sku); + } +} \ No newline at end of file diff --git a/Scripts/IAppcoinsUnityVisitor.cs b/Scripts/IAppcoinsUnityVisitor.cs new file mode 100644 index 0000000..40c3376 --- /dev/null +++ b/Scripts/IAppcoinsUnityVisitor.cs @@ -0,0 +1,95 @@ +using System; + +namespace Aptoide.AppcoinsUnity +{ + /// + /// Class that instatiated an AppCoins SKU. + /// + /// + /// SetupReceiver + /// AppcoinsSKU constructor. + /// + /// + /// SetupWalletAddress + /// Get the name of Appcoins SKU object. + /// + /// + /// SetupIAB + /// Get the ID of Appcoins SKU object. + /// + /// + /// AwakeReceiver + /// Get the price of Appcoins SKU object. + /// + /// + /// AddSKU + /// + /// Check if Appcoins SKU object is well instatiated. + /// + /// + /// + /// MakePurchase + /// + /// Check if the ID of Appcoins SKU object is valid. + /// + /// + /// + /// + public interface IAppcoinsUnityVisitor + { + /// + /// Instantiate receiver based on choosed Platform to run the game. + /// + /// + /// object. + /// + void SetupReceiver(ASFAppcoinsUnity appcoinsUnity); + + /// + /// Give wallet address to receiver. + /// + /// + /// object. + /// + void SetupWalletAddress(ASFAppcoinsUnity appcoinsUnity); + + /// + /// Enable IAB on the receiver's side. + /// + /// + /// object. + /// + void SetupIAB(ASFAppcoinsUnity appcoinsUnity); + + /// + /// Start receiver lifecycle. + /// + /// + /// object. + /// + void AwakeReceiver(ASFAppcoinsUnity appcoinsUnity); + + /// + /// Give new SKU to receiver. + /// + /// + /// object. + /// + /// new + /// to give to receiver. + /// + void AddSKU(ASFAppcoinsUnity appcoinsUnity, AppcoinsSKU newSku); + + /// + /// Pass SKU to receiver to start the transaction. + /// + /// + /// object. + /// + /// + /// + /// to be purchased. + /// + void MakePurchase(ASFAppcoinsUnity appcoinsUnity, AppcoinsSKU sku); + } +} \ No newline at end of file diff --git a/Scripts/MessageHandlerGUI.cs b/Scripts/MessageHandlerGUI.cs index b90271b..38c65c9 100644 --- a/Scripts/MessageHandlerGUI.cs +++ b/Scripts/MessageHandlerGUI.cs @@ -16,12 +16,12 @@ public class PropagateSelection : UnityEvent {} private float windowHeight; private float windowWidth; - private bool isEnabled = false; + internal bool isEnabled = false; public PropagateSelection prop; Canvas canvas; - private void Awake() + internal void InitializeWindow() { prop = new PropagateSelection(); @@ -139,7 +139,15 @@ public void ChangeContent(string m, string s, string f) success = s; fail = f; } - + + public void ChangeContent(string t, string m, string s, string f) + { + ChangeTitle(t); + message = m; + success = s; + fail = f; + } + public void ChangeTitle(string t) { title = new GUIContent(t); diff --git a/Scripts/MigrationHelper.cs b/Scripts/MigrationHelper.cs new file mode 100644 index 0000000..531a951 --- /dev/null +++ b/Scripts/MigrationHelper.cs @@ -0,0 +1,54 @@ +using UnityEngine; +using UnityEditor; + +using System; +using System.IO; + +[InitializeOnLoad] +public class MigrationHelper +{ + + static MigrationHelper() + { + Debug.Log("MigrationHelper is running"); + + string scriptsPath = Application.dataPath + "/AppcoinsUnity/Scripts"; + string editorScriptsPath = scriptsPath + "/Editor"; + + Debug.Log(scriptsPath); + Debug.Log(editorScriptsPath); + + string[] olderScriptFiles = { "AppcoinsPurchaser", "AppcoinsSku", + "AppcoinsUnity", "BashUtils", + "AppCoinsUnityPluginTests2018", + "AppCoinsUnityPluginTests2017", + "AppCoinsUnityPluginTests5_6", + "AppCoinsUnityPluginEditorMode2018", + "AppCoinsUnityPluginEditorMode5_6", + "AppCoinsUnityPluginEditorMode2017" }; + + string[] olderEditorScriptFiles = { "AppCoinsProductEditor", + "AppcoinsStartup", "CustomBuild", "CustomTree", + "ProductMaker", "appcoins-unity-support-5_6", + "appcoins-unity-support-2017", "AppcoinsUnitySupport2018", + "AppCoinsUnitySupport2017", "AppCoinsUnitySupport2018", + "AppCoinsUnitySupport5_6" }; + + DeleteFiles(scriptsPath, olderScriptFiles); + DeleteFiles(editorScriptsPath, olderEditorScriptFiles); + } + + private static void DeleteFiles(string dirPath, string[] filesToDelete) + { + foreach (string filePath in Directory.GetFiles(dirPath)) + { + string fName = Path.GetFileName(filePath); + Debug.Log(fName); + + if (Array.Exists(filesToDelete, dF => dF.Equals(fName))) + { + File.Delete(filePath); + } + } + } +} diff --git a/Scripts/Support/Editor/CustomBuild/AppcoinsGameObject/ASFAppcoinsGameObject.cs b/Scripts/Support/Editor/CustomBuild/AppcoinsGameObject/ASFAppcoinsGameObject.cs index 68d7f79..b06a8f8 100644 --- a/Scripts/Support/Editor/CustomBuild/AppcoinsGameObject/ASFAppcoinsGameObject.cs +++ b/Scripts/Support/Editor/CustomBuild/AppcoinsGameObject/ASFAppcoinsGameObject.cs @@ -1,26 +1,25 @@ -using UnityEngine; - using System; using Aptoide.AppcoinsUnity; public class ASFAppcoinsGameObject : AppcoinsGameObject { - private AppcoinsUnity asfGameObject; + private ASFAppcoinsUnity asfGameObject; + private const string appcoinsPrefab = "APPCOINS_PREFAB"; private const string appcoinsPOA = "APPCOINS_ENABLE_POA"; private const string appcoinsDebug = "APPCOINS_ENABLE_DEBUG"; - private const string appcoinsPOANewLine = "resValue \"bool\", " + + private const string poaNewLine = "resValue \"bool\", " + "\"APPCOINS_ENABLE_POA\", \"{0}\""; - private const string appcoinsDebugNewLine = "resValue \"bool\", " + + private const string debugNewLine = "resValue \"bool\", " + "\"APPCOINS_ENABLE_DEBUG\", \"{0}\""; - private const string appcoinsNameNewLine = "resValue \"string\", " + + private const string nameNewLine = "resValue \"string\", " + "\"APPCOINS_PREFAB\", \"{0}\""; private void FindAppcoinsGameObject() { - AppcoinsUnity[] foundObjects = (AppcoinsUnity[]) - UnityEngine.Object.FindObjectsOfType(typeof(AppcoinsUnity)); + ASFAppcoinsUnity[] foundObjects = (ASFAppcoinsUnity[]) + UnityEngine.Object.FindObjectsOfType(typeof(ASFAppcoinsUnity)); if (foundObjects.Length == 0) { @@ -34,44 +33,25 @@ public override void CheckAppcoinsGameobject() { FindAppcoinsGameObject(); - // Change Appcoins prefab name in mainTempla.gradle - string newNameLine = - appcoinsNameNewLine.Replace(toReplace, - asfGameObject.gameObject.name); - string newPOALine = - appcoinsPOANewLine.Replace(toReplace, - asfGameObject.enablePOA.ToString() - ); + asfGameObject.CheckWalletAddress(); - string newDebugLine = - appcoinsDebugNewLine.Replace(toReplace, - asfGameObject.enableDebug.ToString() - ); + string[] newLines = { + nameNewLine.Replace(toReplace, asfGameObject.gameObject.name), + poaNewLine.Replace(toReplace, + asfGameObject.enablePOA.ToString().ToLower()), + debugNewLine.Replace(toReplace, + asfGameObject.enableDebug.ToString().ToLower()) + }; - Tools.ChangeLineInFile(mainTemplatePath, mainTemplateVarName, - mainTemplateContainers, newNameLine, numTimes); - Tools.ChangeLineInFile(mainTemplatePath, appcoinsPOA, - mainTemplateContainers, newPOALine, numTimes); - Tools.ChangeLineInFile(mainTemplatePath, appcoinsDebug, - mainTemplateContainers, newDebugLine, numTimes); + // Change Appcoins prefab name in mainTempla.gradle + changeLineInMainTemplate(mainTemplatePath, mainTemplateContainers, + newLines); + } - //// Check Appcoins prefab's products - //try - //{ - // AppcoinsChecks.CheckForRepeatedSkuId(asfGameObject.GetProductList()); - // AppcoinsChecks.CheckSKUs(asfGameObject.GetProductList()); - //} - //catch (NoSKUProductsException e) - //{ - // throw new Exception(e.message); - //} - //catch (NullSKUProductException e) - //{ - // throw new Exception(e.message); - //} - //catch (RepeatedSKUProductException e) - //{ - // throw new Exception(e.message); - //} + private void changeLineInMainTemplate(string filePath, string[] containers, + string[] newLines) + { + Tools.RemoveLineInFileWithSpecString(filePath, newLines); + Tools.AddLinesInFile(filePath, containers, newLines); } } diff --git a/Scripts/Support/Editor/Migration/MigrationHelper.cs b/Scripts/Support/Editor/Migration/MigrationHelper.cs new file mode 100644 index 0000000..d14358a --- /dev/null +++ b/Scripts/Support/Editor/Migration/MigrationHelper.cs @@ -0,0 +1,49 @@ +using UnityEngine; +using UnityEditor; + +using System; +using System.IO; + +[InitializeOnLoad] +public class MigrationHelpere { + + static MigrationHelpere() + { + Debug.Log("MigrationHelper is running"); + + string scriptsPath = Application.dataPath + "/AppcoinsUnity/Scripts"; + string editorScriptsPath = scriptsPath + "/Editor"; + + string[] olderScriptFiles = { "AppcoinsPurchaser", "AppcoinsSku", + "AppcoinsUnity", "BashUtils", + "AppCoinsUnityPluginTests2018", + "AppCoinsUnityPluginTests2017", + "AppCoinsUnityPluginTests5_6", + "AppCoinsUnityPluginEditorMode2018", + "AppCoinsUnityPluginEditorMode5_6", + "AppCoinsUnityPluginEditorMode2017" }; + + string[] olderEditorScriptFiles = { "AppCoinsProductEditor", + "AppcoinsStartup", "CustomBuild", "CustomTree", + "ProductMaker", "appcoins-unity-support-5_6", + "appcoins-unity-support-2017", "AppcoinsUnitySupport2018", + "AppCoinsUnitySupport2017", "AppCoinsUnitySupport2018", + "AppCoinsUnitySupport5_6" }; + + DeleteFiles(scriptsPath, olderScriptFiles); + DeleteFiles(editorScriptsPath, olderEditorScriptFiles); + } + + private static void DeleteFiles(string dirPath, string[] filesToDelete) { + foreach(string filePath in Directory.GetFiles(dirPath)) + { + string fName = Path.GetFileName(filePath); + Debug.Log(fName); + + if (Array.Exists(filesToDelete, dF => dF.Equals(fName))) + { + File.Delete(filePath); + } + } + } +} \ No newline at end of file diff --git a/dependencies/2017/AppCoinsUnitySupport.dll b/dependencies/2017/AppCoinsUnitySupport.dll index 8b9947b..5f91213 100644 Binary files a/dependencies/2017/AppCoinsUnitySupport.dll and b/dependencies/2017/AppCoinsUnitySupport.dll differ diff --git a/dependencies/2018/AppCoinsUnitySupport.dll b/dependencies/2018/AppCoinsUnitySupport.dll index 8b9947b..5f91213 100644 Binary files a/dependencies/2018/AppCoinsUnitySupport.dll and b/dependencies/2018/AppCoinsUnitySupport.dll differ diff --git a/dependencies/5.6/AppCoinsUnitySupport.dll b/dependencies/5.6/AppCoinsUnitySupport.dll index 8b9947b..5f91213 100644 Binary files a/dependencies/5.6/AppCoinsUnitySupport.dll and b/dependencies/5.6/AppCoinsUnitySupport.dll differ