mantis 9078: allow region modules script injected constants to be silent replaced. Since with Yengine, this are compile time globals for all regions, only set them once

This commit is contained in:
UbitUmarov
2023-05-23 18:02:10 +01:00
parent cafbf1c977
commit d0c681d404
3 changed files with 51 additions and 45 deletions

View File

@@ -28,7 +28,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
@@ -54,15 +53,14 @@ namespace OpenSim.Region.ScriptEngine.Yengine
*/
public static ScriptConst Lookup(string name)
{
ScriptConst sc;
if(!scriptConstants.TryGetValue(name, out sc))
sc = null;
return sc;
if(scriptConstants.TryGetValue(name, out ScriptConst sc))
return sc;
return null;
}
private static Dictionary<string, ScriptConst> Init()
{
Dictionary<string, ScriptConst> sc = new Dictionary<string, ScriptConst>();
Dictionary<string, ScriptConst> sc = new();
// For every event code, define XMREVENTCODE_<eventname> and XMREVENTMASKn_<eventname> symbols.
for(int i = 0; i < 64; i++)
@@ -72,12 +70,12 @@ namespace OpenSim.Region.ScriptEngine.Yengine
string s = ((ScriptEventCode)i).ToString();
if((s.Length > 0) && (s[0] >= 'a') && (s[0] <= 'z'))
{
new ScriptConst(sc,
_ = new ScriptConst(sc,
"XMREVENTCODE_" + s,
new CompValuInteger(new TokenTypeInt(null), i));
int n = i / 32 + 1;
int m = 1 << (i % 32);
new ScriptConst(sc,
_ = new ScriptConst(sc,
"XMREVENTMASK" + n + "_" + s,
new CompValuInteger(new TokenTypeInt(null), m));
}
@@ -100,7 +98,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine
// this one accepts only upper-case named fields
public static void AddInterfaceConstants(Dictionary<string, ScriptConst> sc, FieldInfo[] allFields)
{
List<FieldInfo> ucfs = new List<FieldInfo>(allFields.Length);
List<FieldInfo> ucfs = new(allFields.Length);
foreach(FieldInfo f in allFields)
{
string fieldName = f.Name;
@@ -119,8 +117,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine
// this one accepts all fields given to it
public static void AddInterfaceConstants(Dictionary<string, ScriptConst> sc, IEnumerator<FieldInfo> fields)
{
if(sc == null)
sc = scriptConstants;
sc ??= scriptConstants;
for(fields.Reset(); fields.MoveNext();)
{
@@ -167,7 +164,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine
}
// Add to dictionary.
new ScriptConst(sc, constField.Name, cv);
_ = new ScriptConst(sc, constField.Name, cv);
}
}
@@ -179,8 +176,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine
*/
public static ScriptConst AddConstant(string name, object value)
{
CompValu cv = null;
CompValu cv;
if(value is char cvalue)
{
cv = new CompValuChar(new TokenTypeChar(null), cvalue);
@@ -249,10 +245,12 @@ namespace OpenSim.Region.ScriptEngine.Yengine
CompValu z = new CompValuFloat(new TokenTypeFloat(null), ov.Z);
cv = new CompValuVec(new TokenTypeVec(null), x, y, z);
}
if(cv == null)
else
throw new Exception("bad type " + value.GetType().Name);
return new ScriptConst(scriptConstants, name, cv);
var sc = new ScriptConst(name, cv);
scriptConstants[name] = sc;
return sc;
}
/*
@@ -263,7 +261,12 @@ namespace OpenSim.Region.ScriptEngine.Yengine
private ScriptConst(Dictionary<string, ScriptConst> lc, string name, CompValu rVal)
{
this.name = name;
this.rVal = rVal;
lc.Add(name, this);
}
private ScriptConst(string name, CompValu rVal)
{
this.name = name;
this.rVal = rVal;
}

View File

@@ -106,7 +106,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine
private bool isFrozen = false;
private bool locals;
private Dictionary<string, Dictionary<ArgTypes, TDVEntry>> master = new Dictionary<string, Dictionary<ArgTypes, TDVEntry>>();
private Dictionary<string, Dictionary<ArgTypes, TDVEntry>> master = new();
private int count = 0;
private VarDict frozenLocals = null;
@@ -131,16 +131,15 @@ namespace OpenSim.Region.ScriptEngine.Yengine
}
// Make sure we have a sub-dictionary based on the bare name (ie, no signature)
Dictionary<ArgTypes, TDVEntry> typedic;
if(!master.TryGetValue(var.name.val, out typedic))
if(!master.TryGetValue(var.name.val, out Dictionary<ArgTypes, TDVEntry> typedic))
{
typedic = new Dictionary<ArgTypes, TDVEntry>();
master.Add(var.name.val, typedic);
}
// See if there is an entry in the sub-dictionary that matches the argument signature.
// Note that fields have null argument lists.
// Methods always have a non-null argument list, even if only 0 entries long.
// See if there is an entry in the sub-dictionary that matches the argument signature.
// Note that fields have null argument lists.
// Methods always have a non-null argument list, even if only 0 entries long.
ArgTypes types;
types.argTypes = (var.argDecl == null) ? null : KeyTypesToStringTypes(var.argDecl.types);
if(typedic.ContainsKey(types))

View File

@@ -335,8 +335,8 @@ namespace OpenSim.Region.ScriptEngine.Yengine
private void OneTimeLateInitialization()
{
// Build list of defined APIs and their 'this' types and define a field in XMRInstanceSuperType.
ApiManager am = new ApiManager();
Dictionary<string, Type> apiCtxTypes = new Dictionary<string, Type>();
ApiManager am = new();
Dictionary<string, Type> apiCtxTypes = new();
foreach(string api in am.GetApis())
{
m_log.Debug("[YEngine]: adding api " + api);
@@ -346,8 +346,8 @@ namespace OpenSim.Region.ScriptEngine.Yengine
apiCtxType = typeof(XMRLSL_Api);
apiCtxTypes[api] = apiCtxType;
}
if(ScriptCodeGen.xmrInstSuperType == null) // Only create type once!
bool doSuper = ScriptCodeGen.xmrInstSuperType == null;
if (doSuper) // Only create type once!
{
// Start creating type XMRInstanceSuperType that contains a field
// m_ApiManager_<APINAME> that points to the per-instance context
@@ -360,8 +360,10 @@ namespace OpenSim.Region.ScriptEngine.Yengine
// public OSSL_Api m_ApiManager_OSSL; // 'this' value for all os...() functions
// ....
// }
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "XMRInstanceSuperAssembly";
AssemblyName assemblyName = new()
{
Name = "XMRInstanceSuperAssembly"
};
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("XMRInstanceSuperModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("XMRInstanceSuperType", TypeAttributes.Public | TypeAttributes.Class);
@@ -415,7 +417,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine
MethodInfo mi = m.Method;
try
{
CommsCallCodeGen cccg = new CommsCallCodeGen(mi, comms, m_XMRInstanceApiCtxFieldInfos["MOD"]);
CommsCallCodeGen cccg = new(mi, comms, m_XMRInstanceApiCtxFieldInfos["MOD"]);
Verbose("[YEngine]: added comms function " + cccg.fullName);
}
catch(Exception e)
@@ -425,19 +427,23 @@ namespace OpenSim.Region.ScriptEngine.Yengine
}
}
// Add constants to list of built-in constants.
Dictionary<string, object> consts = comms.GetConstants();
foreach(KeyValuePair<string, object> kvp in consts)
//only do this once since constants are static
if(doSuper)
{
try
// Add constants to list of built-in constants.
Dictionary<string, object> consts = comms.GetConstants();
foreach(KeyValuePair<string, object> kvp in consts)
{
ScriptConst sc = ScriptConst.AddConstant(kvp.Key, kvp.Value);
Verbose("[YEngine]: added comms constant " + sc.name);
}
catch(Exception e)
{
m_log.Error("[YEngine]: failed to add comms constant " + kvp.Key);
m_log.Error("[YEngine]: - " + e.Message);
try
{
ScriptConst sc = ScriptConst.AddConstant(kvp.Key, kvp.Value);
Verbose("[YEngine]: added comms constant " + sc.name);
}
catch(Exception e)
{
m_log.Error("[YEngine]: failed to add comms constant " + kvp.Key);
m_log.Error("[YEngine]: - " + e.Message);
}
}
}
}
@@ -487,9 +493,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine
: base(null, false, NameArgSig(mi), RetType(mi))
{
methName = mi.Name;
string modInvokerName = comms.LookupModInvocation(methName);
if(modInvokerName == null)
throw new Exception("cannot find comms method " + methName);
string modInvokerName = comms.LookupModInvocation(methName) ?? throw new Exception("cannot find comms method " + methName);
modInvokerMeth = typeof(MOD_Api).GetMethod(modInvokerName, modInvokerArgTypes);
xmrInstModApiCtxField = apictxfi;
}
@@ -497,7 +501,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine
// script-visible name(argtype,...) signature string
private static string NameArgSig(MethodInfo mi)
{
StringBuilder sb = new StringBuilder();
StringBuilder sb = new();
sb.Append(mi.Name);
sb.Append('(');
ParameterInfo[] mps = mi.GetParameters();