Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GameTester.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Runtime.ExceptionServices;
5 using System.Threading.Tasks;
6 #if SILICONSTUDIO_PLATFORM_IOS
7 using MonoTouch.UIKit;
8 #endif
9 using SiliconStudio.Core;
10 using SiliconStudio.Core.Diagnostics;
11 using SiliconStudio.Paradox.Games;
12 
13 namespace SiliconStudio.Paradox.Graphics.Regression
14 {
15  public class GameTester
16  {
17  public readonly static Logger Logger = GlobalLogger.GetLogger("GameTester");
18 
19 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
20  public static void RunGameTest(GameBase game)
21  {
22  using (game)
23  {
24  game.Run();
25  }
26  }
27 #elif SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
28  public static void RunGameTest(GameBase game)
29  {
30  throw new NotImplementedException();
31  }
32 #elif SILICONSTUDIO_PLATFORM_IOS || SILICONSTUDIO_PLATFORM_ANDROID
33  public static void RunGameTest(GameBase game)
34  {
35  // Prepare finish callback
36  var tcs = new TaskCompletionSource<bool>();
37  EventHandler<EventArgs> gameFinishedCallback = (sender, e) =>
38  {
39  // Notify waiter that game has exited
40  Logger.Info("Game finished.");
41  tcs.TrySetResult(true);
42  };
43 
44  EventHandler<GameUnhandledExceptionEventArgs> exceptionhandler = (sender, e) =>
45  {
46  Logger.Info("Game finished with exception ={0}.", e);
47  tcs.TrySetException((Exception)e.ExceptionObject);
48  };
49 
50  // Transmit data to activity
51  // TODO: Avoid static with string intent + Dictionary?
52  try
53  {
54  game.UnhandledException += exceptionhandler;
55 
56  Logger.Info(@"Starting activity");
57 
58 #if SILICONSTUDIO_PLATFORM_IOS
59  game.Exiting += gameFinishedCallback;
60 
61  UIApplication.SharedApplication.InvokeOnMainThread(() =>
62  {
63  var window = UIApplication.SharedApplication.KeyWindow;
64  var rootNavigationController = (UINavigationController)window.RootViewController;
65 
66  // create the paradox game view
67  var bounds = UIScreen.MainScreen.Bounds;
68  var paradoxGameView = new Starter.ParadoxApplicationDelegate.iOSParadoxView(bounds) { ContentScaleFactor = UIScreen.MainScreen.Scale };
69 
70  // create the view controller used to display the paradox game
71  var paradoxGameController = new iOSGameTestController(game) { View = paradoxGameView };
72 
73  // create the game context
74  var gameContext = new GameContext(window, paradoxGameView, paradoxGameController);
75 
76  // push view
77  rootNavigationController.PushViewController(gameContext.GameViewController, false);
78 
79  // launch the game
80  game.Run(gameContext);
81  });
82 #elif SILICONSTUDIO_PLATFORM_ANDROID
83  // Start activity
84  AndroidGameTestActivity.Game = game;
85  AndroidGameTestActivity.Destroyed += gameFinishedCallback;
86  PlatformAndroid.Context.StartActivity(typeof (AndroidGameTestActivity));
87 #endif
88 
89  // Wait for completion of task
90  // TODO: Should we put a timeout and issue a Game.Exit() in main thread if too long?
91  tcs.Task.Wait();
92 
93  Logger.Info(@"Activity ended");
94  }
95  catch (AggregateException e)
96  {
97  // Unwrap aggregate exceptions
98  if (e.InnerExceptions.Count == 1)
99  ExceptionDispatchInfo.Capture(e.InnerException).Throw();
100  }
101  finally
102  {
103 #if SILICONSTUDIO_PLATFORM_IOS
104  // iOS Cleanup
105  UIApplication.SharedApplication.InvokeOnMainThread(() =>
106  {
107  var window = UIApplication.SharedApplication.KeyWindow;
108  var rootNavigationController = (UINavigationController)window.RootViewController;
109 
110  rootNavigationController.PopViewControllerAnimated(false);
111  });
112 #elif SILICONSTUDIO_PLATFORM_ANDROID
113  AndroidGameTestActivity.Game = null;
114  AndroidGameTestActivity.Destroyed -= gameFinishedCallback;
115 #endif
116 
117  // Cleanup
118  game.Exiting -= gameFinishedCallback;
119  game.UnhandledException -= exceptionhandler;
120  }
121  }
122 #endif
123  }
124 }
Base implementation for ILogger.
Definition: Logger.cs:10
Contains context used to render the game (Control for WinForm, a DrawingSurface for WP8...
Definition: GameContext.cs:31