コード例 #1
0
ファイル: RantFunctions.cs プロジェクト: nicolasmaurice/Rant
 internal static void Load()
 {
     if (Loaded)
     {
         return;
     }
     foreach (var method in typeof(RantFunctions).GetMethods(BindingFlags.Static | BindingFlags.NonPublic))
     {
         if (!method.IsStatic)
         {
             continue;
         }
         var attr = method.GetCustomAttributes(typeof(RantFunctionAttribute), false).FirstOrDefault() as RantFunctionAttribute;
         if (attr == null)
         {
             continue;
         }
         var name     = String.IsNullOrEmpty(attr.Name) ? method.Name.ToLower() : attr.Name;
         var descAttr = method.GetCustomAttributes(typeof(RantDescriptionAttribute), false).FirstOrDefault() as RantDescriptionAttribute;
         var info     = new RantFunctionInfo(name, descAttr?.Description ?? String.Empty, method);
         if (Util.ValidateName(name))
         {
             RegisterFunction(info);
         }
         foreach (var alias in attr.Aliases.Where(Util.ValidateName))
         {
             RegisterAlias(alias, info.Name);
         }
     }
     Loaded = true;
 }
コード例 #2
0
ファイル: RantFunctions.cs プロジェクト: strogonoff/Rant
 private static void RegisterFunction(RantFunctionInfo func)
 {
     RantFunctionGroup group;
     if (!FunctionTable.TryGetValue(func.Name, out group))
         group = FunctionTable[func.Name] = new RantFunctionGroup(func.Name);
     group.Add(func);
 }
コード例 #3
0
        public void Add(RantFunctionInfo func)
        {
            RantFunctionInfo existing;

            if (_functions.TryGetValue(func.Parameters.Length, out existing))
            {
                throw new ArgumentException($"Cannot load function {func} becaue its signature is ambiguous with existing function {existing}.");
            }
            if (_paramsArrayFunc != null)
            {
                if (func.HasParamArray)
                {
                    throw new ArgumentException($"Cannot load function {func} because another function with a parameter array was already loaded.");
                }
                if (func.Parameters.Length >= _paramsArrayFunc.Parameters.Length)
                {
                    throw new ArgumentException($"Cannot load function {func} because its signature is ambiguous with {_paramsArrayFunc}.");
                }
            }

            _functions[func.Parameters.Length] = func;
            if (func.HasParamArray)
            {
                _paramsArrayFunc = func;
            }
        }
コード例 #4
0
        private static void RegisterProperty(Type objectType, RantFunctionInfo info)
        {
            var dictionary = _properties.ContainsKey(objectType) ?
                             _properties[objectType] :
                             new Dictionary <string, RantFunctionInfo>();

            dictionary.Add(info.Name, info);
            _properties[objectType] = dictionary;
        }
コード例 #5
0
ファイル: RantFunctions.cs プロジェクト: nicolasmaurice/Rant
        private static void RegisterFunction(RantFunctionInfo func)
        {
            RantFunctionGroup group;

            if (!FunctionTable.TryGetValue(func.Name, out group))
            {
                group = FunctionTable[func.Name] = new RantFunctionGroup(func.Name);
            }
            group.Add(func);
        }
コード例 #6
0
 private static void RegisterGlobalObject(string name, string property, RantFunctionInfo info)
 {
     if (!_globalObjects.ContainsKey(name))
     {
         _globalObjects[name] = new RichObject(null);
     }
     if (info.TreatAsRichardFunction)
     {
         _globalObjects[name].Values[property] = new RichNativeFunction(null, info.ParamCount - 1, info);
     }
     else
     {
         _globalObjects[name].Values[property] = new RichFunctionInfo(null, info);
     }
 }
コード例 #7
0
        public static void Load()
        {
            if (_loaded)
            {
                return;
            }

            _properties    = new Dictionary <Type, Dictionary <string, RantFunctionInfo> >();
            _globalObjects = new Dictionary <string, RichObject>();

            foreach (var method in typeof(RichardFunctions).GetMethods(BindingFlags.Static | BindingFlags.NonPublic))
            {
                if (!method.IsStatic)
                {
                    continue;
                }
                var attr = method.GetCustomAttributes(typeof(RichardProperty), true).OfType <RichardProperty>().FirstOrDefault();
                if (attr == null)
                {
                    var objectAttr = method.GetCustomAttributes(typeof(RichardGlobalObject), true).OfType <RichardGlobalObject>().FirstOrDefault();
                    if (objectAttr == null)
                    {
                        continue;
                    }
                    var name     = String.IsNullOrEmpty(objectAttr.Property) ? method.Name.ToLower() : objectAttr.Property;
                    var descAttr = method.GetCustomAttributes(typeof(RantDescriptionAttribute), true).OfType <RantDescriptionAttribute>().FirstOrDefault();
                    var info     = new RantFunctionInfo(name, descAttr?.Description ?? String.Empty, method);
                    info.TreatAsRichardFunction = objectAttr.IsFunction;
                    if (Util.ValidateName(name))
                    {
                        RegisterGlobalObject(objectAttr.Name, objectAttr.Property, info);
                    }
                }
                else
                {
                    var name     = String.IsNullOrEmpty(attr.Name) ? method.Name.ToLower() : attr.Name;
                    var descAttr = method.GetCustomAttributes(typeof(RantDescriptionAttribute), true).OfType <RantDescriptionAttribute>().FirstOrDefault();
                    var info     = new RantFunctionInfo(name, descAttr?.Description ?? String.Empty, method);
                    info.TreatAsRichardFunction = attr.IsFunction;
                    if (Util.ValidateName(name))
                    {
                        RegisterProperty(attr.ObjectType, info);
                    }
                }
            }

            _loaded = true;
        }
コード例 #8
0
ファイル: RantFunctionGroup.cs プロジェクト: W-h-a-t-s/Rant
        public void Add(RantFunctionInfo func)
        {
            RantFunctionInfo existing;
            if (_functions.TryGetValue(func.Parameters.Length, out existing))
                throw new ArgumentException($"Cannot load function {func} becaue its signature is ambiguous with existing function {existing}.");
            if (_paramsArrayFunc != null)
            {
                if (func.HasParamArray)
                    throw new ArgumentException($"Cannot load function {func} because another function with a parameter array was already loaded.");
                if (func.Parameters.Length >= _paramsArrayFunc.Parameters.Length)
                    throw new ArgumentException($"Cannot load function {func} because its signature is ambiguous with {_paramsArrayFunc}.");
            }

            _functions[func.Parameters.Length] = func;
            if (func.HasParamArray) _paramsArrayFunc = func;
        }
コード例 #9
0
ファイル: RantFunctions.cs プロジェクト: strogonoff/Rant
 internal static void Load()
 {
     if (Loaded) return;
     foreach (var method in typeof(RantFunctions).GetMethods(BindingFlags.Static | BindingFlags.NonPublic))
     {
         if (!method.IsStatic) continue;
         var attr = method.GetCustomAttributes(typeof(RantFunctionAttribute), false).FirstOrDefault() as RantFunctionAttribute;
         if (attr == null) continue;
         var name = String.IsNullOrEmpty(attr.Name) ? method.Name.ToLower() : attr.Name;
         var descAttr = method.GetCustomAttributes(typeof(RantDescriptionAttribute), false).FirstOrDefault() as RantDescriptionAttribute;
         var info = new RantFunctionInfo(name, descAttr?.Description ?? String.Empty, method);
         if (Util.ValidateName(name)) RegisterFunction(info);
         foreach (var alias in attr.Aliases.Where(Util.ValidateName))
             RegisterAlias(alias, info.Name);
     }
     Loaded = true;
 }