This project is read-only.
using System;
#if ANDROID
using Android.Content.PM;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Provider;
using Android.Views;
using Android.Widget;
using LicenseVerificationLibrary;
using LicenseVerificationLibrary.Obfuscator;
using LicenseVerificationLibrary.Policy;
using Uri = Android.Net.Uri;
using Microsoft.Xna.Framework;
#endif
#if IPHONE || IOS
using MonoTouch.Foundation;
using MonoTouch.UIKit;
#endif
using cocos2d;

namespace MyFirstGame
{
#if WINDOWS || XBOX || PSM || PSS
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using (Game1 game = new Game1())
            {
                game.Run();
            }
        }
    }
#endif
#if IPHONE || IOS
	[Register ("AppDelegate")]
	class Program : UIApplicationDelegate 
	{
		private Game1 game;

		public override void FinishedLaunching (UIApplication app)
		{
			// Fun begins..
			game = new Game1();
			game.Run();
		}
		
		// This is the main entry point of the application.
		static void Main (string[] args)
		{
			// if you want to use a different Application Delegate class from "AppDelegate"
			// you can specify it here.
			UIApplication.Main (args, null, "AppDelegate");
		}
	}
#endif
#if ANDROID
    [Activity(
        Label = "My Game",
               AlwaysRetainTaskState = true,
               Icon = "@drawable/ic_launcher",
               Theme = "@style/Theme.NoTitleBar",
               ScreenOrientation = Android.Content.PM.ScreenOrientation.Landscape,
               LaunchMode = Android.Content.PM.LaunchMode.SingleInstance,
        MainLauncher = true,
        ConfigurationChanges =  ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden)
    ]
#if OUYA
    [IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryLauncher, "ouya.intent.category.GAME" })]
#endif
    public class MyGame : AndroidGameActivity, ILicenseCheckerCallback
    {
        /// <summary>
        /// Your Base 64 public key
        /// </summary>
        private const string Base64PublicKey = "Get This From Google";

        // Generate your own 20 random bytes, and put them here.
        private static readonly byte[] Salt = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        private LicenseChecker checker;
        
        public MyGame()
        {
        }
        protected override void OnRestoreInstanceState(Bundle savedInstanceState)
        {
            base.OnRestoreInstanceState(savedInstanceState);
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
#if OUYA
            Ouya.Console.Api.OuyaFacade.Instance.Init(this, "..."); // Your UUID dev ID
#endif
            RequestWindowFeature(WindowFeatures.IndeterminateProgress);
            SetContentView(Resource.Layout.Main);
            #region License Check
            // Try to use more data here. ANDROID_ID is a single point of attack.
            string deviceId = Settings.Secure.GetString(ContentResolver, Settings.Secure.AndroidId);

            // Construct the LicenseChecker with a policy.
#if !DEBUG
            var obfuscator = new AesObfuscator(Salt, this.PackageName, deviceId);
            var policy = new ServerManagedPolicy(this, obfuscator);
            this.checker = new LicenseChecker(this, policy, Base64PublicKey);

            this.DoCheck();
#endif
            #endregion
            Game1.Activity = this;
            var game = new Game1();

            var frameLayout = new FrameLayout(this);
            frameLayout.AddView(game.Window);
            this.SetContentView(frameLayout);

            //SetContentView(game.Window);
            game.Run(GameRunBehavior.Asynchronous);
        }
        #region License Check Support
        protected override Dialog OnCreateDialog(int id)
        {
            bool retry = id == 1;
            
            EventHandler<DialogClickEventArgs> eventHandler = delegate
                {
                    if (retry)
                    {
                        this.DoCheck();
                    }
                    else
                    {
                        Uri uri = Uri.Parse("http://market.android.com/details?id=" + this.PackageName);
                        var marketIntent = new Intent(Intent.ActionView, uri);
                        this.StartActivity(marketIntent);
                    }
                };

            var message = retry ? Resource.String.unlicensed_dialog_retry_body : Resource.String.unlicensed_dialog_body;
            var ok = retry ? Resource.String.retry_button : Resource.String.buy_button;

            return new AlertDialog.Builder(this) // builder
                .SetTitle(Resource.String.unlicensed_dialog_title) // title
                .SetMessage(message) // message
                .SetPositiveButton(ok, eventHandler) // ok
                .SetNegativeButton(Resource.String.quit_button, delegate { Finish(); }) // cancel
                .Create(); // create dialog now
        }

        private void DoCheck()
        {
            this.SetProgressBarIndeterminateVisibility(true);
            this.checker.CheckAccess(this);
        }

        private void DisplayResult(string result)
        {
            this.RunOnUiThread(
                delegate
                    {
                        this.SetProgressBarIndeterminateVisibility(false);
                    });
        }

        private void DisplayDialog(bool showRetry)
        {
            this.RunOnUiThread(
                delegate
                    {
                        SetProgressBarIndeterminateVisibility(false);
                        ShowDialog(showRetry ? 1 : 0);
                    });
        }

        /// <summary>
        /// Should allow user access. 
        /// </summary>
        /// <param name="response"></param>
        public void Allow(PolicyServerResponse response)
        {
            if (this.IsFinishing)
            {
                // Don't update UI if Activity is finishing.
                return;
            }
            
            this.DisplayResult(this.GetString(Resource.String.allow));
        }

        /// <summary>
        /// Should not allow access.
        /// <br />
        /// In most cases, the app should assume the user has access unless it 
        /// encounters this. If it does, the app should inform the user of 
        /// their unlicensed ways and then either shut down the app or limit 
        /// the user to a restricted set of features.
        /// <br />
        /// In this example, we show a dialog that takes the user to Play.
        /// If the reason for the lack of license is that the service is
        /// unavailable or there is another problem, we display a
        /// retry button on the dialog and a different message. 
        /// </summary>
        /// <param name="response"></param>
        public void DontAllow(PolicyServerResponse response)
        {
            if (this.IsFinishing)
            {
                // Don't update UI if Activity is finishing.
                return;
            }

            this.DisplayResult(this.GetString(Resource.String.dont_allow));
            this.DisplayDialog(response == PolicyServerResponse.Retry);
        }

        /// <summary>
        /// This is a polite way of saying the developer made a mistake
        /// while setting up or calling the license checker library.
        /// Please examine the error code and fix the error.
        /// </summary>
        /// <param name="errorCode"></param>
        public void ApplicationError(CallbackErrorCode errorCode)
        {
            if (this.IsFinishing)
            {
                // Don't update UI if Activity is finishing.
                return;
            }
            
            var errorString = this.GetString(Resource.String.application_error);
            this.DisplayResult(string.Format(errorString, errorCode));
        }
        
        #endregion
        protected override void OnDestroy()
        {
            base.OnDestroy();
            if (this.checker != null)
            {
                this.checker.OnDestroy();
            }
        }
        protected override void OnPause()
        {
           base.OnPause();
           CCLog.Log("Activity: OnPause was called"); // Called when the power button is pressed.
        }

        protected override void OnResume()
        {
            base.OnResume();
        }
    }
#endif
}


Last edited Jan 12, 2013 at 6:47 AM by totallyevil, version 1

Comments

No comments yet.