From a76a289d11086dd99d345390e58a43b66b053470 Mon Sep 17 00:00:00 2001
From: Mic Bowman
Date: Tue, 31 Jul 2012 10:45:37 -0700
Subject: Adds support to ScriptModuleComms for region modules to export
constants to the script engine.
---
.../Framework/Interfaces/IScriptModuleComms.cs | 4 +++
.../ScriptModuleComms/ScriptModuleCommsModule.cs | 33 ++++++++++++++++++
.../Shared/CodeTools/CSCodeGenerator.cs | 39 ++++++++++++++++++++--
3 files changed, 74 insertions(+), 2 deletions(-)
diff --git a/OpenSim/Region/Framework/Interfaces/IScriptModuleComms.cs b/OpenSim/Region/Framework/Interfaces/IScriptModuleComms.cs
index bfe1e8d..ed71a95 100644
--- a/OpenSim/Region/Framework/Interfaces/IScriptModuleComms.cs
+++ b/OpenSim/Region/Framework/Interfaces/IScriptModuleComms.cs
@@ -67,6 +67,10 @@ namespace OpenSim.Region.Framework.Interfaces
///
void DispatchReply(UUID scriptId, int code, string text, string key);
+ /// For constants
+ void RegisterConstant(string cname, object value);
+ object LookupModConstant(string cname);
+
// For use ONLY by the script API
void RaiseEvent(UUID script, string id, string module, string command, string key);
}
diff --git a/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs b/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs
index 74a85e2..705a847 100644
--- a/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs
+++ b/OpenSim/Region/OptionalModules/Scripting/ScriptModuleComms/ScriptModuleCommsModule.cs
@@ -46,6 +46,8 @@ namespace OpenSim.Region.OptionalModules.Scripting.ScriptModuleComms
private static readonly ILog m_log =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+ private Dictionary m_constants = new Dictionary();
+
#region ScriptInvocation
protected class ScriptInvocationData
{
@@ -269,6 +271,37 @@ namespace OpenSim.Region.OptionalModules.Scripting.ScriptModuleComms
Delegate fn = LookupScriptInvocation(fname);
return fn.DynamicInvoke(olist.ToArray());
}
+
+ ///
+ /// Operation to for a region module to register a constant to be used
+ /// by the script engine
+ ///
+ public void RegisterConstant(string cname, object value)
+ {
+ m_log.DebugFormat("[MODULE COMMANDS] register constant <{0}> with value {1}",cname,value.ToString());
+ lock (m_constants)
+ {
+ m_constants.Add(cname,value);
+ }
+ }
+
+ ///
+ /// Operation to check for a registered constant
+ ///
+ public object LookupModConstant(string cname)
+ {
+ // m_log.DebugFormat("[MODULE COMMANDS] lookup constant <{0}>",cname);
+
+ lock (m_constants)
+ {
+ object value = null;
+ if (m_constants.TryGetValue(cname,out value))
+ return value;
+ }
+
+ return null;
+ }
+
#endregion
}
diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs
index b24f016..97dd0f6 100644
--- a/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs
@@ -38,7 +38,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
{
public class CSCodeGenerator : ICodeConverter
{
-// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private SYMBOL m_astRoot = null;
private Dictionary, KeyValuePair> m_positionMap;
@@ -255,7 +255,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
else if (s is IdentDotExpression)
retstr += Generate(CheckName(((IdentDotExpression) s).Name) + "." + ((IdentDotExpression) s).Member, s);
else if (s is IdentExpression)
- retstr += Generate(CheckName(((IdentExpression) s).Name), s);
+ retstr += GenerateIdentifier(((IdentExpression) s).Name, s);
else if (s is IDENT)
retstr += Generate(CheckName(((TOKEN) s).yytext), s);
else
@@ -868,6 +868,41 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
}
///
+ /// Generates the code for an identifier
+ ///
+ /// The symbol name
+ /// The Symbol node.
+ /// String containing C# code for identifier reference.
+ private string GenerateIdentifier(string id, SYMBOL s)
+ {
+ if (m_comms != null)
+ {
+ object value = m_comms.LookupModConstant(id);
+ if (value != null)
+ {
+ string retval = null;
+ if (value is int)
+ retval = ((int)value).ToString();
+ else if (value is float)
+ retval = String.Format("new LSL_Types.LSLFloat({0})",((float)value).ToString());
+ else if (value is string)
+ retval = String.Format("new LSL_Types.LSLString(\"{0}\")",((string)value));
+ else if (value is OpenMetaverse.UUID)
+ retval = String.Format("new LSL_Types.key(\"{0}\")",((OpenMetaverse.UUID)value).ToString());
+ else if (value is OpenMetaverse.Vector3)
+ retval = String.Format("new LSL_Types.Vector3(\"{0}\")",((OpenMetaverse.Vector3)value).ToString());
+ else if (value is OpenMetaverse.Quaternion)
+ retval = String.Format("new LSL_Types.Quaternion(\"{0}\")",((OpenMetaverse.Quaternion)value).ToString());
+ else retval = id;
+
+ return Generate(retval, s);
+ }
+ }
+
+ return Generate(CheckName(id), s);
+ }
+
+ ///
/// Generates the code for a FunctionCall node.
///
/// The FunctionCall node.
--
cgit v1.1