Пример #1
0
        /// <inheritdoc />
        public IMoValue Get(MoPath key, MoParams parameters)
        {
            if (Functions.TryGetValue(key.Value, out var func))
            {
                return(MoValue.FromObject(func(parameters)));
            }

            return(DoubleValue.Zero);
        }
Пример #2
0
        /// <inheritdoc />
        public override IMoValue Get(object instance)
        {
            var value = _fieldInfo.GetValue(instance);

            return(value is IMoValue moValue ? moValue : MoValue.FromObject(value));
        }
Пример #3
0
        private static void ProcessMethods(IReflect type,
                                           IDictionary <string, Func <object, MoParams, IMoValue> > functions)
        {
            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);

            foreach (var method in methods)
            {
                var functionAttribute = method.GetCustomAttribute <MoFunctionAttribute>();

                if (functionAttribute == null)
                {
                    continue;
                }

                foreach (var name in functionAttribute.Name)
                {
                    if (functions.ContainsKey(name))
                    {
                        Debug.WriteLine($"Duplicate function \'{name}\' in {type.ToString()}");

                        continue;
                    }

                    IMoValue ExecuteMolangFunction(object instance, MoParams mo)
                    {
                        var      methodParams = method.GetParameters();
                        IMoValue value        = DoubleValue.Zero;

                        object[] parameters = new object[methodParams.Length];
                        //List<object> parameters = new List<object>();

                        if (methodParams.Length == 1 && methodParams[0].ParameterType == typeof(MoParams))
                        {
                            parameters[0] = mo;
                            //parameters.Add(mo);
                        }
                        else
                        {
                            for (var index = 0; index < methodParams.Length; index++)
                            {
                                var parameter = methodParams[index];

                                if (!mo.Contains(index))
                                {
                                    if (!parameter.IsOptional)
                                    {
                                        throw new MissingMethodException($"Missing parameter: {parameter.Name}");
                                    }

                                    break;
                                }

                                var t = parameter.ParameterType;

                                if (t == typeof(MoParams))
                                {
                                    parameters[index] = mo;                                     //.Add(mo);
                                }
                                else if (t == typeof(int))
                                {
                                    parameters[index] = mo.GetInt(index);
                                }
                                else if (t == typeof(double))
                                {
                                    parameters[index] = mo.GetDouble(index);
                                }
                                else if (t == typeof(float))
                                {
                                    parameters[index] = (float)mo.GetDouble(index);
                                }
                                else if (t == typeof(string))
                                {
                                    parameters[index] = mo.GetString(index);
                                }
                                else if (typeof(IMoStruct).IsAssignableFrom(t))
                                {
                                    parameters[index] = mo.GetStruct(index);
                                }
                                else if (typeof(MoLangEnvironment).IsAssignableFrom(t))
                                {
                                    parameters[index] = mo.GetEnv(index);
                                }
                                else
                                {
                                    throw new Exception("Unknown parameter type.");
                                }

                                //TODO: Continue.
                            }
                        }

                        var result = method.Invoke(instance, parameters);

                        if (result != null)
                        {
                            if (result is IMoValue moValue)
                            {
                                return(moValue);
                            }

                            return(MoValue.FromObject(result));
                        }

                        return(value);
                    }

                    functions.Add(name, ExecuteMolangFunction);
                }
            }
        }