[gbrainy] Convert Mono.CSharp dependency into a service and implement also the support for Mono > 2.10
- From: Jordi Mas <jmas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gbrainy] Convert Mono.CSharp dependency into a service and implement also the support for Mono > 2.10
- Date: Sun, 27 Mar 2011 13:16:12 +0000 (UTC)
commit 7ca22a20ac9c85533a7503863fd8ea89e56aa08b
Author: Jordi Mas <jmas softcatala org>
Date: Sun Mar 27 15:17:21 2011 +0200
Convert Mono.CSharp dependency into a service and implement also the support for Mono > 2.10
configure.ac | 9 +
src/Clients/Classical/gbrainy.cs | 5 +-
src/Clients/WebForms/Global.asax.cs | 5 +-
src/Core/Core.csproj | 9 +-
src/Core/Libraries/CSharpCompiler.cs | 102 ++++++++++++
.../CSharpCompilerStatic.cs} | 166 ++++++-------------
.../Libraries/CSharpCompilerStaticDomainProxy.cs | 70 ++++++++
src/Core/Libraries/TranslationsCatalog.cs | 4 +-
src/Core/Main/Xml/GameXml.cs | 67 ++++++--
src/Core/Makefile.am | 8 +-
src/Core/Services/DefaultServices.cs | 60 +++++++
src/Core/Services/ServiceLocator.cs | 21 ++-
tests/TestSupport/UnitTestSupport.cs | 5 +-
tools/GamesStatistics.cs | 4 +-
14 files changed, 385 insertions(+), 150 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 9df44a1..03ed5fe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -41,6 +41,14 @@ else
AC_MSG_RESULT([found])
fi
+if ! pkg-config --atleast-version=2.11 mono; then
+ CSC_DEFINES="$CSC_DEFINES -d:CSHARP_STATIC"
+ csharp_static="Yes"
+else
+ csharp_static="No"
+fi
+
+
dnl -- check for various mono assemblies that we need.
needed_assemblies="Mono.Posix Mono.Cairo Mono.CSharp"
@@ -211,3 +219,4 @@ echo "GNOME enabled: ${enable_gnome}"
echo "Mono-addins: ${enable_addins_sharp}"
echo "NUnit: ${enable_tests}"
echo "Debug enabled: ${enable_debug}"
+echo "Mono.CSharp static: ${csharp_static}"
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index 5167c9e..e1c8225 100644
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -28,6 +28,7 @@ using System.Reflection;
using gbrainy.Core.Main;
using gbrainy.Core.Platform;
using gbrainy.Core.Services;
+using gbrainy.Core.Libraries;
using gbrainy.Clients.Classical.Dialogs;
using gbrainy.Clients.Classical.Widgets;
@@ -670,9 +671,7 @@ namespace gbrainy.Clients.Classical
static void InitCoreLibraries ()
{
- // Register services
- ServiceLocator.Instance.RegisterService <ITranslations> (new TranslationsCatalog ());
- ServiceLocator.Instance.RegisterService <IConfiguration> (new MemoryConfiguration ());
+ new DefaultServices ().RegisterServices ();
// Configuration
ServiceLocator.Instance.GetService <IConfiguration> ().Set (ConfigurationKeys.GamesDefinitions, Defines.DATA_DIR);
diff --git a/src/Clients/WebForms/Global.asax.cs b/src/Clients/WebForms/Global.asax.cs
index 109b6f2..8a2384c 100644
--- a/src/Clients/WebForms/Global.asax.cs
+++ b/src/Clients/WebForms/Global.asax.cs
@@ -41,8 +41,11 @@ namespace gbrainy.Clients.WebForms
}
// Register services
+ DefaultServices services = new DefaultServices ();
+ services.RemoveService <ITranslations> ();
+ services.RegisterServices ();
+
ServiceLocator.Instance.RegisterService <ITranslations> (new TranslationsWeb ());
- ServiceLocator.Instance.RegisterService <IConfiguration> (new MemoryConfiguration ());
ThemeManager.ConfigPath = Defines.THEMES_DIR;
// Configuration
diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj
index 8eb87ff..1c6aff5 100644
--- a/src/Core/Core.csproj
+++ b/src/Core/Core.csproj
@@ -9,14 +9,13 @@
<OutputType>Library</OutputType>
<RootNamespace>Core</RootNamespace>
<AssemblyName>gbrainy.Core.dll</AssemblyName>
- <BaseDirectory>.</BaseDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\Debug</OutputPath>
- <DefineConstants>DEBUG</DefineConstants>
+ <DefineConstants>DEBUG,CSHARP_STATIC</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
@@ -93,7 +92,6 @@
<Compile Include="Libraries\SVGImage.cs" />
<Compile Include="Libraries\GetText.cs" />
<Compile Include="Platform\Unix.cs" />
- <Compile Include="Main\Xml\CodeEvaluation.cs" />
<Compile Include="Main\GameAnswerCheckAttributes.cs" />
<Compile Include="Main\GameDifficulty.cs" />
<Compile Include="Main\Xml\OptionDrawingObject.cs" />
@@ -114,6 +112,11 @@
<Compile Include="Main\GameAnswer.cs" />
<Compile Include="Main\GameAnswerEventArgs.cs" />
<Compile Include="AssemblyInfo.cs" />
+ <Compile Include="Libraries\CSharpCompilerStaticDomainProxy.cs" />
+ <Compile Include="Libraries\CSharpCompilerStatic.cs" />
+ <Compile Include="Libraries\CSharpCompiler.cs" />
+ <Compile Include="Services\DefaultServices.cs" />
+ <Compile Include="Services\ICSharpCompiler.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
diff --git a/src/Core/Libraries/CSharpCompiler.cs b/src/Core/Libraries/CSharpCompiler.cs
new file mode 100644
index 0000000..0d1f812
--- /dev/null
+++ b/src/Core/Libraries/CSharpCompiler.cs
@@ -0,0 +1,102 @@
+/*
+ * 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.
+ */
+
+#if !CSHARP_STATIC
+
+using System;
+using System.Text;
+using System.Text.RegularExpressions;
+
+using Mono.CSharp;
+using System.Reflection;
+using gbrainy.Core.Services;
+
+namespace gbrainy.Core.Libraries
+{
+ public class NullReportPrinter : ReportPrinter
+ {
+ public NullReportPrinter ()
+ {
+
+ }
+ }
+
+ // This encapsulates Mono.CSharp > 2.10
+ public class CSharpCompiler : ICSharpCompiler
+ {
+ Evaluator evaluator;
+
+ public CSharpCompiler ()
+ {
+ CompilerSettings settings = new CompilerSettings ();
+ Report report = new Report (new NullReportPrinter ());
+
+ evaluator = new Evaluator (settings, report);
+ }
+
+ public void EvaluateCode (string c)
+ {
+ evaluator.Evaluate (c);
+ }
+
+ public string GetAllVariables ()
+ {
+ return evaluator.GetVars ();
+ }
+
+ public string GetVariableValue (string _var)
+ {
+ const string exp = "([a-z0-9._%+-]+) ([a-z0-9._%+-]+) (=) ([0-9]+)";
+ Match match;
+ int idx, cur, newline_len;
+ string line;
+ string vars;
+
+ vars = GetAllVariables ();
+ Regex regex = new Regex (exp, RegexOptions.IgnoreCase);
+
+ newline_len = System.Environment.NewLine.Length;
+ cur = 0;
+
+ do
+ {
+ // Process a line
+ idx = vars.IndexOf (System.Environment.NewLine, cur);
+ if (idx == -1) idx = vars.Length;
+
+ line = vars.Substring (cur, idx - cur);
+ cur = idx + newline_len;
+ match = regex.Match (line);
+
+ // "int num = 2";
+ // group 1 -> int, group 2 -> num, group 3 -> =, group 4 -> 2
+ if (match.Groups.Count == 5)
+ {
+ if (match.Groups[2].Value == _var)
+ return match.Groups[4].Value;
+ }
+
+ } while (cur < vars.Length);
+
+ return string.Empty;
+ }
+ }
+}
+
+#endif
diff --git a/src/Core/Main/Xml/CodeEvaluation.cs b/src/Core/Libraries/CSharpCompilerStatic.cs
similarity index 66%
rename from src/Core/Main/Xml/CodeEvaluation.cs
rename to src/Core/Libraries/CSharpCompilerStatic.cs
index ca433c1..eff280e 100644
--- a/src/Core/Main/Xml/CodeEvaluation.cs
+++ b/src/Core/Libraries/CSharpCompilerStatic.cs
@@ -17,6 +17,8 @@
* Boston, MA 02111-1307, USA.
*/
+#if CSHARP_STATIC
+
using System;
using System.Text;
using System.Text.RegularExpressions;
@@ -25,122 +27,94 @@ using Mono.CSharp;
using System.Reflection;
using gbrainy.Core.Services;
-namespace gbrainy.Core.Main.Xml
+namespace gbrainy.Core.Libraries
{
- // This class proxys data from one domain to another
- public class CrossDomainProxy : MarshalByRefObject
- {
- string code;
-
- public void SetCode (string c)
- {
- code = c;
- }
-
- public string GetVars ()
- {
- return Evaluator.GetVars ();
- }
-
- public void EvaluateVariables ()
- {
- string eval;
-
- try
- {
- // Using's for the variables section
- // We need to evaluate either declarations (like using) or expression/statements separately
- eval = "using System;\n";
- Mono.CSharp.Evaluator.Run (eval);
-
- // Infrastructure for the user available
- eval = "Random random = new Random ();\n";
-
- // As Mono 2.4.4 this call is killing in terms of memory leaking
- Mono.CSharp.Evaluator.Run (eval + code);
- }
-
- catch (Exception e)
- {
- Console.WriteLine ("GameXml. Error in games.xml: {0} when evaluating variable definition [{1}]", e.Message, code);
- }
- }
- }
-
- // Code evaluation functions
- public static class CodeEvaluation
+ // This encapsulates Mono.CSharp previous to Mono 2.10 when it was a static class
+ public class CSharpCompilerStatic : ICSharpCompiler
{
static bool? monofix_needed;
static string stored_vars;
static int unload_domain = 0;
static AppDomain tempDomain;
const int maximum_uses = 5;
-
- static public void EvaluateVariables (string c)
+
+ public void EvaluateCode (string c)
{
if (tempDomain == null)
tempDomain = AppDomain.CreateDomain ("MonoCSharpDomain");
-
+
// Load the Mono Compiler service in a separate domain then
// we can recycle it to reduce memory consumption
//
// After Mono 2.12 this is no longer need
- // http://tirania.org/blog/archive/2011/Feb-24.html
- IConfiguration config = ServiceLocator.Instance.GetService <IConfiguration> ();
+ // http://tirania.org/blog/archive/2011/Feb-24.html
+ IConfiguration config = ServiceLocator.Instance.GetService <IConfiguration> ();
string asm_dir = config.Get <string> (ConfigurationKeys.AssembliesDir);
string full_name = System.IO.Path.Combine (asm_dir, "gbrainy.Core.dll");
AssemblyName aname = AssemblyName.GetAssemblyName (full_name);
Assembly asem = tempDomain.Load (aname);
-
- CrossDomainProxy proxy = (CrossDomainProxy) tempDomain.CreateInstanceAndUnwrap(asem.FullName,
- typeof (CrossDomainProxy).FullName);
-
+
+ CSharpCompilerStaticDomainProxy proxy = (CSharpCompilerStaticDomainProxy) tempDomain.CreateInstanceAndUnwrap (asem.FullName,
+ typeof (CSharpCompilerStaticDomainProxy).FullName);
+
proxy.SetCode (c);
tempDomain.DoCallBack (proxy.EvaluateVariables);
stored_vars = proxy.GetVars ();
-
-
+ stored_vars = FixGetVars (stored_vars);
+
if (unload_domain > maximum_uses)
- {
+ {
AppDomain.Unload (tempDomain);
unload_domain = 0;
tempDomain = null;
}
- else
+ else
unload_domain++;
}
-
- static public string ReplaceVariables (string str)
+ public string GetAllVariables ()
{
- const string exp = "\\[[a-z_]+\\]+";
- string var, vars, var_value;
- Regex regex;
- Match match;
+ return stored_vars;
+ }
- if (String.IsNullOrEmpty (str) ||
- String.IsNullOrEmpty (stored_vars))
- return str;
+ public string GetVariableValue (string _var)
+ {
+ const string exp = "([a-z0-9._%+-]+) ([a-z0-9._%+-]+) (=) ([0-9]+)";
+ Match match;
+ int idx, cur, newline_len;
+ string line;
+ string vars;
- regex = new Regex (exp, RegexOptions.IgnoreCase);
- match = regex.Match (str);
+ vars = GetAllVariables ();
+ Regex regex = new Regex (exp, RegexOptions.IgnoreCase);
- vars = stored_vars;
- vars = FixGetVars (vars);
+ newline_len = System.Environment.NewLine.Length;
+ cur = 0;
- while (String.IsNullOrEmpty (match.Value) == false)
+ do
{
- var = match.Value.Substring (1, match.Value.Length - 2);
- var_value = GetVarValue (vars, var);
+ // Process a line
+ idx = vars.IndexOf (System.Environment.NewLine, cur);
+ if (idx == -1) idx = vars.Length;
- if (String.IsNullOrEmpty (var_value) == false)
- str = str.Replace (match.Value, var_value);
+ line = vars.Substring (cur, idx - cur);
+ cur = idx + newline_len;
+ match = regex.Match (line);
- match = match.NextMatch ();
- }
- return str;
+ // "int num = 2";
+ // group 1 -> int, group 2 -> num, group 3 -> =, group 4 -> 2
+ if (match.Groups.Count == 5)
+ {
+ if (match.Groups[2].Value == _var)
+ return match.Groups[4].Value;
+ }
+
+ } while (cur < vars.Length);
+
+ return string.Empty;
}
- // Before Mono 2.6 (rev. 156533) there is no line separator between vars
+
+ // Before Mono 2.6 (rev. 156533) there is no line separator between variables
static string FixGetVars (string str)
{
if (monofix_needed == null)
@@ -190,40 +164,6 @@ namespace gbrainy.Core.Main.Xml
output.Append (str.Substring (cur, str.Length - cur));
return output.ToString ();
}
-
- static string GetVarValue (string vars, string _var)
- {
- const string exp = "([a-z0-9._%+-]+) ([a-z0-9._%+-]+) (=) ([0-9]+)";
- Match match;
- int idx, cur, newline_len;
- string line;
-
- Regex regex = new Regex (exp, RegexOptions.IgnoreCase);
-
- newline_len = System.Environment.NewLine.Length;
- cur = 0;
-
- do
- {
- // Process a line
- idx = vars.IndexOf (System.Environment.NewLine, cur);
- if (idx == -1) idx = vars.Length;
-
- line = vars.Substring (cur, idx - cur);
- cur = idx + newline_len;
- match = regex.Match (line);
-
- // "int num = 2";
- // group 1 -> int, group 2 -> num, group 3 -> =, group 4 -> 2
- if (match.Groups.Count == 5)
- {
- if (match.Groups[2].Value == _var)
- return match.Groups[4].Value;
- }
-
- } while (cur < vars.Length);
-
- return string.Empty;
- }
}
}
+#endif
diff --git a/src/Core/Libraries/CSharpCompilerStaticDomainProxy.cs b/src/Core/Libraries/CSharpCompilerStaticDomainProxy.cs
new file mode 100644
index 0000000..e31150f
--- /dev/null
+++ b/src/Core/Libraries/CSharpCompilerStaticDomainProxy.cs
@@ -0,0 +1,70 @@
+/*
+ * 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.
+ */
+
+#if CSHARP_STATIC
+
+using System;
+using Mono.CSharp;
+
+using gbrainy.Core.Services;
+
+namespace gbrainy.Core.Libraries
+{
+ // This class proxys data from one domain to another
+ internal class CSharpCompilerStaticDomainProxy : MarshalByRefObject
+ {
+ string code;
+
+ public void SetCode (string c)
+ {
+ code = c;
+ }
+
+ public string GetVars ()
+ {
+ return Evaluator.GetVars ();
+ }
+
+ public void EvaluateVariables ()
+ {
+ string eval;
+
+ try
+ {
+ // Using's for the variables section
+ // We need to evaluate either declarations (like using) or expression/statements separately
+ eval = "using System;\n";
+ Mono.CSharp.Evaluator.Run (eval);
+
+ // Infrastructure for the user available
+ eval = "Random random = new Random ();\n";
+
+ // As Mono 2.4.4 this call is killing in terms of memory leaking
+ Mono.CSharp.Evaluator.Run (eval + code);
+ }
+
+ catch (Exception e)
+ {
+ Console.WriteLine ("GameXml. Error in games.xml: {0} when evaluating variable definition [{1}]", e.Message, code);
+ }
+ }
+ }
+}
+
+#endif
diff --git a/src/Core/Libraries/TranslationsCatalog.cs b/src/Core/Libraries/TranslationsCatalog.cs
index 04ee810..efc8cec 100644
--- a/src/Core/Libraries/TranslationsCatalog.cs
+++ b/src/Core/Libraries/TranslationsCatalog.cs
@@ -19,7 +19,9 @@
using Mono.Unix;
-namespace gbrainy.Core.Services
+using gbrainy.Core.Services;
+
+namespace gbrainy.Core.Libraries
{
public class TranslationsCatalog : ITranslations
{
diff --git a/src/Core/Main/Xml/GameXml.cs b/src/Core/Main/Xml/GameXml.cs
index dca2ca9..0954dbf 100644
--- a/src/Core/Main/Xml/GameXml.cs
+++ b/src/Core/Main/Xml/GameXml.cs
@@ -20,6 +20,7 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Text.RegularExpressions;
using gbrainy.Core.Toolkit;
using gbrainy.Core.Services;
@@ -54,17 +55,9 @@ namespace gbrainy.Core.Main.Xml
GameXmlDefinition game;
string question, answer, rationale, answer_show;
List <OptionDrawingObject> options;
+ ICSharpCompiler compiler;
- void SetAnswerCorrectShow ()
- {
-
- if (String.IsNullOrEmpty (answer_show))
- return;
-
- Answer.CorrectShow = answer_show;
- }
-
static public List <GameXmlDefinition> Definitions {
set {
games = value;
@@ -100,6 +93,14 @@ namespace gbrainy.Core.Main.Xml
}
}
+ void SetAnswerCorrectShow ()
+ {
+ if (String.IsNullOrEmpty (answer_show))
+ return;
+
+ Answer.CorrectShow = answer_show;
+ }
+
void SetCheckExpression ()
{
string expression;
@@ -136,6 +137,8 @@ namespace gbrainy.Core.Main.Xml
bool variants;
LocalizableString localizable_question, localizable_rationale;
+ compiler = ServiceLocator.Instance.GetService <ICSharpCompiler> ();
+
variants = game.Variants.Count > 0;
SetCheckAttributes ();
@@ -156,16 +159,15 @@ namespace gbrainy.Core.Main.Xml
if (String.IsNullOrEmpty (variables) == false)
{
- // Evaluate code
- CodeEvaluation.EvaluateVariables (variables);
+ compiler.EvaluateCode (variables);
try {
if (String.IsNullOrEmpty (localizable_question.Value) == false)
- localizable_question.ValueComputed = Int32.Parse (CodeEvaluation.ReplaceVariables (localizable_question.Value));
+ localizable_question.ValueComputed = Int32.Parse (ReplaceVariables (localizable_question.Value));
if (localizable_rationale != null && String.IsNullOrEmpty (localizable_rationale.Value) == false)
- localizable_rationale.ValueComputed = Int32.Parse (CodeEvaluation.ReplaceVariables (localizable_rationale.Value));
+ localizable_rationale.ValueComputed = Int32.Parse (ReplaceVariables (localizable_rationale.Value));
}
catch (Exception e)
{
@@ -195,10 +197,10 @@ namespace gbrainy.Core.Main.Xml
if (String.IsNullOrEmpty (variables) == false)
{
- question = CodeEvaluation.ReplaceVariables (question);
- rationale = CodeEvaluation.ReplaceVariables (rationale);
- answer = CodeEvaluation.ReplaceVariables (answer);
- answer_show = CodeEvaluation.ReplaceVariables (answer_show);
+ question = ReplaceVariables (question);
+ rationale = ReplaceVariables (rationale);
+ answer = ReplaceVariables (answer);
+ answer_show = ReplaceVariables (answer_show);
}
if (options != null && options.Count > 0)
@@ -417,7 +419,7 @@ namespace gbrainy.Core.Main.Xml
TextDrawingObject draw_string = draw_object as TextDrawingObject;
text = CatalogGetString (draw_string.Text);
- text = CodeEvaluation.ReplaceVariables (text);
+ text = ReplaceVariables (text);
switch (draw_string.Size) {
case TextDrawingObject.Sizes.Small:
@@ -510,5 +512,34 @@ namespace gbrainy.Core.Main.Xml
return ServiceLocator.Instance.GetService <ITranslations> ().GetPluralString (localizable.String, localizable.PluralString, localizable.ValueComputed);
}
+
+ // Replace compiler service variables
+ string ReplaceVariables (string str)
+ {
+ const string exp = "\\[[a-z_]+\\]+";
+ string var, var_value, all_vars;
+ Regex regex;
+ Match match;
+
+ all_vars = compiler.GetAllVariables ();
+ if (String.IsNullOrEmpty (str) ||
+ String.IsNullOrEmpty (all_vars))
+ return str;
+
+ regex = new Regex (exp, RegexOptions.IgnoreCase);
+ match = regex.Match (str);
+
+ while (String.IsNullOrEmpty (match.Value) == false)
+ {
+ var = match.Value.Substring (1, match.Value.Length - 2);
+ var_value = compiler.GetVariableValue (var);
+
+ if (String.IsNullOrEmpty (var_value) == false)
+ str = str.Replace (match.Value, var_value);
+
+ match = match.NextMatch ();
+ }
+ return str;
+ }
}
}
diff --git a/src/Core/Makefile.am b/src/Core/Makefile.am
index 67e19aa..e7258a2 100644
--- a/src/Core/Makefile.am
+++ b/src/Core/Makefile.am
@@ -22,7 +22,6 @@ CSDISTFILES = \
$(srcdir)/Main/GameSessionHistoryExtended.cs \
$(srcdir)/Main/GameTypes.cs \
$(srcdir)/Main/GameTips.cs \
- $(srcdir)/Main/Xml/CodeEvaluation.cs \
$(srcdir)/Main/Xml/DrawingObject.cs \
$(srcdir)/Main/Xml/ImageDrawingObject.cs \
$(srcdir)/Main/Xml/LocalizableString.cs \
@@ -70,7 +69,12 @@ CSDISTFILES = \
$(srcdir)/Services/ITranslations.cs \
$(srcdir)/Libraries/TranslationsCatalog.cs \
$(srcdir)/Services/IConfiguration.cs \
- $(srcdir)/Services/MemoryConfiguration.cs
+ $(srcdir)/Services/MemoryConfiguration.cs \
+ $(srcdir)/Services/DefaultServices.cs \
+ $(srcdir)/Services/ICSharpCompiler.cs \
+ $(srcdir)/Libraries/CSharpCompilerStatic.cs \
+ $(srcdir)/Libraries/CSharpCompilerStaticDomainProxy.cs \
+ $(srcdir)/Libraries/CSharpCompiler.cs
CSFILES = $(CSDISTFILES) \
Main/Defines.cs \
diff --git a/src/Core/Services/DefaultServices.cs b/src/Core/Services/DefaultServices.cs
new file mode 100644
index 0000000..cf09506
--- /dev/null
+++ b/src/Core/Services/DefaultServices.cs
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2011 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 System.Collections.Generic;
+
+using gbrainy.Core.Libraries;
+
+namespace gbrainy.Core.Services
+{
+ // This is a utility class to init default services
+ public class DefaultServices
+ {
+ Dictionary <Type, IService> services;
+
+ public DefaultServices ()
+ {
+ services = new Dictionary <Type, IService> ();
+
+ // Default services
+ services.Add (typeof (ITranslations), new TranslationsCatalog ());
+ services.Add (typeof (IConfiguration), new MemoryConfiguration ());
+
+#if CSHARP_STATIC
+ services.Add (typeof (ICSharpCompiler), new CSharpCompilerStatic ());
+#else
+ services.Add (typeof (ICSharpCompiler), new CSharpCompiler ());
+#endif
+ }
+
+ public void RemoveService <T> () where T : class, IService
+ {
+ services.Remove (typeof (T));
+ }
+
+ public void RegisterServices ()
+ {
+ foreach (Type t in services.Keys)
+ {
+ ServiceLocator.Instance.RegisterService (t, services[t]);
+ }
+ }
+ }
+}
diff --git a/src/Core/Services/ServiceLocator.cs b/src/Core/Services/ServiceLocator.cs
index 5f3a339..d858673 100644
--- a/src/Core/Services/ServiceLocator.cs
+++ b/src/Core/Services/ServiceLocator.cs
@@ -24,13 +24,13 @@ namespace gbrainy.Core.Services
{
public class ServiceLocator
{
- Dictionary <Type, object> services;
+ Dictionary <Type, IService> services;
static ServiceLocator instance = new ServiceLocator ();
static readonly object sync = new object ();
public ServiceLocator ()
{
- services = new Dictionary <Type, object> ();
+ services = new Dictionary <Type, IService> ();
}
public static ServiceLocator Instance {
@@ -39,9 +39,24 @@ namespace gbrainy.Core.Services
}
}
+ public void RegisterService (Type t, IService service)
+ {
+ lock (sync)
+ {
+ if (services.ContainsKey (t) == false)
+ {
+ services.Add (t, service);
+ }
+ else
+ {
+ services[t] = service;
+ }
+ }
+ }
+
public void RegisterService <T> (T service) where T : class, IService
{
- Type t = typeof (T);
+ Type t = typeof (T);
lock (sync)
{
diff --git a/tests/TestSupport/UnitTestSupport.cs b/tests/TestSupport/UnitTestSupport.cs
index a861138..cb61cd7 100644
--- a/tests/TestSupport/UnitTestSupport.cs
+++ b/tests/TestSupport/UnitTestSupport.cs
@@ -22,6 +22,7 @@ using NUnit.Framework;
using gbrainy.Core.Main;
using gbrainy.Core.Services;
+using gbrainy.Core.Libraries;
namespace gbrainyTest
{
@@ -29,9 +30,7 @@ namespace gbrainyTest
{
public void RegisterDefaultServices ()
{
- // Register services
- ServiceLocator.Instance.RegisterService <ITranslations> (new TranslationsCatalog ());
- ServiceLocator.Instance.RegisterService <IConfiguration> (new MemoryConfiguration ());
+ new DefaultServices ().RegisterServices ();
string mono_path = Environment.GetEnvironmentVariable ("MONO_PATH");
diff --git a/tools/GamesStatistics.cs b/tools/GamesStatistics.cs
index 5134f9b..f14ed01 100644
--- a/tools/GamesStatistics.cs
+++ b/tools/GamesStatistics.cs
@@ -41,9 +41,7 @@ public class GamesStatistics
static void InitCoreLibraries ()
{
- // Register services
- ServiceLocator.Instance.RegisterService <ITranslations> (new TranslationsCatalog ());
- ServiceLocator.Instance.RegisterService <IConfiguration> (new MemoryConfiguration ());
+ new DefaultServices ().RegisterServices ();
// Configuration
ServiceLocator.Instance.GetService <IConfiguration> ().Set (ConfigurationKeys.GamesDefinitions, Defines.DATA_DIR);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]