[gbrainy] Initial command line support



commit 1ae1d833691f5a229d7f63816dde9b86a27eecc2
Author: Jordi Mas <jmas softcatala org>
Date:   Thu Jun 24 19:28:07 2010 +0200

    Initial command line support

 po/POTFILES.in                       |    1 +
 src/Clients/Classical/CommandLine.cs |  175 ++++++++++++++++++++++++++++++++++
 src/Clients/Classical/Makefile.am    |    1 +
 src/Clients/Classical/gbrainy.cs     |  100 +++++++++++++------
 4 files changed, 246 insertions(+), 31 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 76adcdc..64ecdd5 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -23,6 +23,7 @@ src/Core/Main/Verbal/AnalogiesPairOfWordsOptions.cs
 src/Core/Main/Verbal/AnalogiesQuestionAnswer.cs
 src/Core/Main/Verbal/AnalogiesFactory.cs
 src/Clients/Classical/gbrainy.cs
+src/Clients/Classical/CommandLine.cs
 src/Clients/Classical/Dialogs/AboutDialog.cs
 src/Clients/Classical/Dialogs/CustomGameDialog.cs
 src/Clients/Classical/Dialogs/PlayerHistoryDialog.cs
diff --git a/src/Clients/Classical/CommandLine.cs b/src/Clients/Classical/CommandLine.cs
new file mode 100644
index 0000000..45ed0df
--- /dev/null
+++ b/src/Clients/Classical/CommandLine.cs
@@ -0,0 +1,175 @@
+/*
+ * Copyright (C) 2010 Jordi Mas i Hernàndez <jmas softcatala org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+using System;
+using Mono.Unix;
+using System.Collections.Generic;
+using System.Reflection;
+
+using gbrainy.Core.Main;
+
+namespace gbrainy.Clients.Classical
+{
+	public class CommandLine
+	{
+		string [] args;
+		int [] play_list;
+		bool cont_execution;
+
+		public CommandLine (string [] args)
+		{
+			this.args = args;
+		}
+
+		public bool Continue {
+			get { return cont_execution; }
+		}
+
+		public int [] PlayList {
+			get { return play_list; }
+		}
+
+		public void Parse ()
+		{
+			cont_execution = true;
+
+			for (int idx = 0; idx < args.Length; idx++)
+			{
+				switch (args [idx]) {
+				case "--customgame":
+					string [] names = args [idx+1].Split (',');
+
+					for (int i = 0; i < names.Length; i++)
+						names[i] = names[i].Trim ();
+
+					BuildPlayList (names);
+					break;
+				case "--gamelist":
+					GameList ();
+					cont_execution = false;
+					break;
+				case "--version":
+					Version ();
+					cont_execution = false;
+					break;
+				case "--versions":
+					Versions ();
+					cont_execution = false;
+					break;
+				case "--help":
+				case "--usage":
+					Usage ();
+					cont_execution = false;
+					break;
+				default:
+					break;
+				}
+			}
+		}
+
+		static void GameList ()
+		{
+			GameManager.GameLocator [] games;
+			GameManager gm = new GameManager ();
+			gm.GameType = GameSession.Types.AllGames;
+			games = gm.AvailableGames;
+
+			Console.WriteLine (Catalog.GetString ("List of available games"));
+
+			for (int i = 0; i < games.Length; i++)
+			{
+				if (games[i].IsGame == false)
+					continue;
+
+				Game game = (Game) Activator.CreateInstance (games[i].TypeOf, true);
+				game.Variant = games[i].Variant;
+				Console.WriteLine (" {0}", game.Name);
+			}
+		}
+
+		void BuildPlayList (string [] names)
+		{
+			Dictionary <string, int> dictionary;
+			GameManager.GameLocator [] games;
+			GameManager gm = new GameManager ();
+			gm.GameType = GameSession.Types.AllGames;
+			games = gm.AvailableGames;
+
+			// Create a hash to map from game name to locator
+			dictionary = new Dictionary <string, int> (games.Length);
+			for (int i = 0; i < games.Length; i++)
+			{
+				if (games[i].IsGame == false)
+					continue;
+
+				Game game = (Game) Activator.CreateInstance (games[i].TypeOf, true);
+				game.Variant = games[i].Variant;
+				dictionary.Add (game.Name, i);
+			}
+
+			List <int> list = new List <int> (names.Length);
+
+			for (int i = 0; i < names.Length; i++)
+			{
+				try
+				{
+					list.Add (dictionary [names [i]]);
+				}
+				catch (KeyNotFoundException e)
+				{
+					Console.WriteLine ("gbrainy. Game [{0}] not found", names [i]);
+				}
+			}
+
+			play_list = list.ToArray ();
+		}
+
+		static void Usage ()
+		{
+			string usage =
+			        Catalog.GetString (
+			                "Usage: gbrainy [options]\n" +
+			                "  --version\t\t\tPrint version information.\n" +
+			                "  --help\t\t\tPrint this usage message.\n" +
+			                "  --gamelist\t\t\tShows the list of available games\n" +
+			                "  --customgame [game1, gameN]\tSpecifies a list of games to play during a custom game\n" +
+			                "  --versions \t\t\tShow dependencies\n");
+
+			Console.WriteLine (usage);
+		}
+
+		static void Version ()
+		{
+			Console.WriteLine ("gbrainy " + Defines.VERSION);
+		}
+
+		static void Versions ()
+		{
+			Version ();
+			Console.WriteLine ("Mono .NET Version: " + Environment.Version.ToString());
+			Console.WriteLine (String.Format("{0}Assembly Version Information:", Environment.NewLine));
+
+			foreach(Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
+			{
+				AssemblyName name = asm.GetName();
+				Console.WriteLine ("\t" + name.Name + " (" + name.Version.ToString () + ")");
+			}
+		}
+	}
+}
diff --git a/src/Clients/Classical/Makefile.am b/src/Clients/Classical/Makefile.am
index 6ff1d7c..f28c6d1 100644
--- a/src/Clients/Classical/Makefile.am
+++ b/src/Clients/Classical/Makefile.am
@@ -4,6 +4,7 @@ WRAPPER = gbrainy
 TARGET_CFG = ../../gbrainy.exe.config
 
 GBRAINY_CSDISTFILES =					\
+	$(srcdir)/CommandLine.cs 			\
 	$(srcdir)/gbrainy.cs 				\
 	$(srcdir)/GtkSynchronize.cs 			\
 	$(srcdir)/SimpleLabel.cs			\
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index d6287da..36b5cbb 100644
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -70,25 +70,46 @@ namespace gbrainy.Clients.Classical
 		bool margins = false;
 		double offset_x, offset_y;
 		int drawing_square;
+		GameSession.Types initial_session;
 
-		public GtkClient (string [] args, params object [] props)
-		: base ("gbrainy", Defines.VERSION, Modules.UI,  args, props)
+		public GtkClient ()
+		: base ("gbrainy", Defines.VERSION, Modules.UI, new string [0])
 		{
-			bool show_toolbar;
-
 			Catalog.Init ("gbrainy", Defines.GNOME_LOCALE_DIR);
 			Unix.FixLocaleInfo ();
 
-			GtkBeans.Builder builder = new GtkBeans.Builder ("gbrainy.ui");
-			builder.Autoconnect (this);
+			Initialize ();
+		}
 
-			BuildToolBar ();
+		public GameSession Session {
+			get { return session; }
+		}
+
+		public GameSession.Types InitialSessionType {
+			get { return initial_session; }
+			set { initial_session = value; }
+		}
+
+		void Initialize ()
+		{
 			session = new GameSession ();
 			session.DrawRequest += SessionDrawRequest;
 			session.UpdateUIElement += SessionUpdateUIElement;
 			session.SynchronizingObject = new GtkSynchronize ();
-
 			session.Difficulty = (Game.Difficulty) Preferences.GetIntValue (Preferences.DifficultyKey);
+
+			BuildUI ();
+		}
+
+		void BuildUI ()
+		{
+			bool show_toolbar;
+
+			GtkBeans.Builder builder = new GtkBeans.Builder ("gbrainy.ui");
+			builder.Autoconnect (this);
+
+			BuildToolBar ();
+
 			drawing_area = new DrawingArea ();
 			drawing_area.ExposeEvent += OnDrawingAreaExposeEvent;
 			GameSensitiveUI ();
@@ -110,10 +131,10 @@ namespace gbrainy.Clients.Classical
 			solution_vbox.Add (solution_label);
 
 			EventBox eb = new EventBox (); // Provides a window for drawing area windowless widget
-			
+
 			eb.Events = Gdk.EventMask.PointerMotionMask;
 			drawing_vbox.Add (eb);
-	
+
 			eb.Add (drawing_area);
 
 			eb.MotionNotifyEvent += OnMouseMotionEvent;
@@ -140,6 +161,12 @@ namespace gbrainy.Clients.Classical
 			ActiveInputControls (false);
 		}
 
+		public void ProcessDefaults ()
+		{
+			if (InitialSessionType != GameSession.Types.None)
+				OnNewGame (InitialSessionType);
+		}
+
 		// Gamesession has requested a question refresh
 		public void SessionUpdateUIElement (object o, UpdateUIStateEventArgs args)
 		{
@@ -172,9 +199,9 @@ namespace gbrainy.Clients.Classical
 			Cairo.Context cc = Gdk.CairoHelper.Create (args.Window);
 			CairoContextEx cr = new CairoContextEx (cc.Handle, drawing_area);
 
-			// We want a square drawing area for the puzzles then the figures are shown as designed. 
+			// We want a square drawing area for the puzzles then the figures are shown as designed.
 			// For example, squares are squares. This also makes sure that proportions are kept when resizing
-			drawing_square = Math.Min (w, h);	
+			drawing_square = Math.Min (w, h);
 
 			if (drawing_square < w)
 				offset_x = (w - drawing_square) / 2;
@@ -193,7 +220,7 @@ namespace gbrainy.Clients.Classical
 			((IDisposable)cc).Dispose();
 			((IDisposable)cr).Dispose();
 		}
-		
+
 		void OnMouseMotionEvent (object o, MotionNotifyEventArgs ev_args)
 		{
 			SendMouseEvent (ev_args.Event.X, ev_args.Event.Y, MouseEventType.Move);
@@ -214,7 +241,7 @@ namespace gbrainy.Clients.Classical
 			x = ev_x - offset_x;
 			y = ev_y - offset_y;
 
-			if (x < 0 || y < 0 || x > drawing_square || y > drawing_square) 
+			if (x < 0 || y < 0 || x > drawing_square || y > drawing_square)
 				return;
 
 			x =  x / drawing_square;
@@ -292,7 +319,7 @@ namespace gbrainy.Clients.Classical
 		}
 
 		void UpdateSolution (string solution)
-		{		
+		{
 			solution_label.Text = solution;
 		}
 
@@ -418,7 +445,7 @@ namespace gbrainy.Clients.Classical
 			OnAnswerButtonClicked (this, EventArgs.Empty);
 			session.CurrentGame.AnswerEvent -= OnAnswerFromGame; // User can only answer once
 		}
-	
+
 		void OnMenuAbout (object sender, EventArgs args)
 		{
 			AboutDialog about = new AboutDialog ();
@@ -427,9 +454,9 @@ namespace gbrainy.Clients.Classical
 
 		void OnMenuHelp (object sender, EventArgs args)
 		{
-			Unix.ShowUri (null, "ghelp:gbrainy", 
+			Unix.ShowUri (null, "ghelp:gbrainy",
 				Gdk.EventHelper.GetTime (new Gdk.Event(IntPtr.Zero)));
-		}	
+		}
 
 		void OnAnswerButtonClicked (object sender, EventArgs args)
 		{
@@ -437,7 +464,7 @@ namespace gbrainy.Clients.Classical
 
 			if (session.CurrentGame == null)
 				return;
-	
+
 			if (session.ScoreGame (answer_entry.Text) == true)
 				answer = "<span color='#00A000'>" + Catalog.GetString ("Congratulations.") + "</span>";
 			else
@@ -452,17 +479,17 @@ namespace gbrainy.Clients.Classical
 			ActiveInputControls (true);
 			next_button.GrabFocus ();
 			drawing_area.QueueDraw ();
-		}		
+		}
 
 		void OnQuit (object sender, EventArgs args)
 		{
-			Quit ();	
-		}	
+			Quit ();
+		}
 
 		void OnDeleteWindow (object sender, DeleteEventArgs args)
 		{
-			Quit ();	
-		}	
+			Quit ();
+		}
 
 		void OnNextButtonClicked (object sender, EventArgs args)
 		{
@@ -553,7 +580,7 @@ namespace gbrainy.Clients.Classical
 		void OnEndGame (object sender, EventArgs args)
 		{
 			session.End ();
-	
+
 			UpdateSolution (String.Empty);
 			UpdateQuestion (String.Empty);
 			UpdateStatusBar ();
@@ -617,12 +644,12 @@ namespace gbrainy.Clients.Classical
 			dialog = new PlayerHistoryDialog (session.PlayerHistory);
 			dialog.Run ();
 			dialog.Destroy ();
-		}	
+		}
 
 		private void AddIcon (IconFactory stock, string stockid, string resource)
 		{
 			Gtk.IconSet iconset = stock.Lookup (stockid);
-		
+
 			if (iconset != null)
 				return;
 
@@ -631,7 +658,7 @@ namespace gbrainy.Clients.Classical
 			IconSource source = new IconSource ();
 			source.Pixbuf = img;
 			iconset.AddSource (source);
-			stock.Add (stockid, iconset);		
+			stock.Add (stockid, iconset);
 		}
 
 		void OnFullscreen (object sender, EventArgs args)
@@ -653,14 +680,25 @@ namespace gbrainy.Clients.Classical
 			Process.Start ("http://live.gnome.org/gbrainy/Extending";);
 		}
 
-		public static void Main (string [] args) 
+		public static void Main (string [] args)
 		{
 			try {
 				Unix.SetProcessName ("gbrainy");
 			} catch {}
 
-			GtkClient gui = new GtkClient (args);
-			gui.Run ();	
+			CommandLine line = new CommandLine (args);
+			line.Parse ();
+
+			if (line.Continue == false)
+				return;
+
+			GtkClient app = new GtkClient ();
+			if (line.PlayList != null) {
+				app.Session.GameManager.PlayList = line.PlayList;
+				app.InitialSessionType = GameSession.Types.Custom;
+			}
+			app.ProcessDefaults ();
+			app.Run ();
 		}
 	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]