Adds support to ScriptModuleComms for region modules to export

constants to the script engine.
0.7.4.1
Mic Bowman 2012-07-31 10:45:37 -07:00
parent f9a8915cca
commit a76a289d11
3 changed files with 74 additions and 2 deletions

View File

@ -67,6 +67,10 @@ namespace OpenSim.Region.Framework.Interfaces
/// <param name="key"></param> /// <param name="key"></param>
void DispatchReply(UUID scriptId, int code, string text, string key); 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 // For use ONLY by the script API
void RaiseEvent(UUID script, string id, string module, string command, string key); void RaiseEvent(UUID script, string id, string module, string command, string key);
} }

View File

@ -46,6 +46,8 @@ namespace OpenSim.Region.OptionalModules.Scripting.ScriptModuleComms
private static readonly ILog m_log = private static readonly ILog m_log =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private Dictionary<string,object> m_constants = new Dictionary<string,object>();
#region ScriptInvocation #region ScriptInvocation
protected class ScriptInvocationData protected class ScriptInvocationData
{ {
@ -269,6 +271,37 @@ namespace OpenSim.Region.OptionalModules.Scripting.ScriptModuleComms
Delegate fn = LookupScriptInvocation(fname); Delegate fn = LookupScriptInvocation(fname);
return fn.DynamicInvoke(olist.ToArray()); return fn.DynamicInvoke(olist.ToArray());
} }
/// <summary>
/// Operation to for a region module to register a constant to be used
/// by the script engine
/// </summary>
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);
}
}
/// <summary>
/// Operation to check for a registered constant
/// </summary>
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 #endregion
} }

View File

@ -38,7 +38,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
{ {
public class CSCodeGenerator : ICodeConverter 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 SYMBOL m_astRoot = null;
private Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> m_positionMap; private Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> m_positionMap;
@ -255,7 +255,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
else if (s is IdentDotExpression) else if (s is IdentDotExpression)
retstr += Generate(CheckName(((IdentDotExpression) s).Name) + "." + ((IdentDotExpression) s).Member, s); retstr += Generate(CheckName(((IdentDotExpression) s).Name) + "." + ((IdentDotExpression) s).Member, s);
else if (s is IdentExpression) else if (s is IdentExpression)
retstr += Generate(CheckName(((IdentExpression) s).Name), s); retstr += GenerateIdentifier(((IdentExpression) s).Name, s);
else if (s is IDENT) else if (s is IDENT)
retstr += Generate(CheckName(((TOKEN) s).yytext), s); retstr += Generate(CheckName(((TOKEN) s).yytext), s);
else else
@ -867,6 +867,41 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
return retstr; return retstr;
} }
/// <summary>
/// Generates the code for an identifier
/// </summary>
/// <param name="id">The symbol name</param>
/// <param name="s">The Symbol node.</param>
/// <returns>String containing C# code for identifier reference.</returns>
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);
}
/// <summary> /// <summary>
/// Generates the code for a FunctionCall node. /// Generates the code for a FunctionCall node.
/// </summary> /// </summary>