diff --git a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptConsts.cs b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptConsts.cs index eb754ef865..413b9fc2ad 100644 --- a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptConsts.cs +++ b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptConsts.cs @@ -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 Init() { - Dictionary sc = new Dictionary(); + Dictionary sc = new(); // For every event code, define XMREVENTCODE_ and XMREVENTMASKn_ 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 sc, FieldInfo[] allFields) { - List ucfs = new List(allFields.Length); + List 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 sc, IEnumerator 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 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; } diff --git a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptVarDict.cs b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptVarDict.cs index 662a09fc60..4e00c8f118 100644 --- a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptVarDict.cs +++ b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptVarDict.cs @@ -106,7 +106,7 @@ namespace OpenSim.Region.ScriptEngine.Yengine private bool isFrozen = false; private bool locals; - private Dictionary> master = new Dictionary>(); + private Dictionary> 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 typedic; - if(!master.TryGetValue(var.name.val, out typedic)) + if(!master.TryGetValue(var.name.val, out Dictionary typedic)) { typedic = new Dictionary(); 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)) diff --git a/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs b/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs index 59a68f6bf9..40f9774b27 100644 --- a/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs +++ b/OpenSim/Region/ScriptEngine/YEngine/XMREngine.cs @@ -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 apiCtxTypes = new Dictionary(); + ApiManager am = new(); + Dictionary 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_ 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 consts = comms.GetConstants(); - foreach(KeyValuePair kvp in consts) + //only do this once since constants are static + if(doSuper) { - try + // Add constants to list of built-in constants. + Dictionary consts = comms.GetConstants(); + foreach(KeyValuePair 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();