Пример #1
0
        public static EnumerableNumericSettingsVM Factory <TItem, TWrapper>(string memberName, object?defaultVal, TWrapper prototype)
            where TWrapper : BasicSettingsVM <TItem>, new()
        {
            EnumerableNumericSettingsVM ret = null !;
            Func <JsonElement, IBasicSettingsNodeVM> import = new Func <JsonElement, IBasicSettingsNodeVM>((elem) =>
            {
                return(new ListElementWrapperVM <TItem, TWrapper>(
                           prototype.Get(elem)));
            });

            ret = new EnumerableNumericSettingsVM(
                memberName,
                import,
                (list) =>
            {
                list.Add(new ListElementWrapperVM <TItem, TWrapper>(prototype.GetDefault())
                {
                    IsSelected = true
                });
            });
            if (defaultVal is IEnumerable <TItem> items)
            {
                ret.Values.SetTo(items.Select(x =>
                {
                    return(new ListElementWrapperVM <TItem, TWrapper>(x));
                }));
            }
            return(ret);
        }
Пример #2
0
        public static SettingsNodeVM MemberFactory(SettingsParameters param, string memberName, Type targetType, object?defaultVal)
        {
            switch (targetType.Name)
            {
            case "Boolean":
                return(new BoolSettingsVM(memberName, defaultVal));

            case "SByte":
                return(new Int8SettingsVM(memberName, defaultVal));

            case "Int16":
                return(new Int16SettingsVM(memberName, defaultVal));

            case "Int32":
                return(new Int32SettingsVM(memberName, defaultVal));

            case "Int64":
                return(new Int64SettingsVM(memberName, defaultVal));

            case "Byte":
                return(new UInt8SettingsVM(memberName, defaultVal));

            case "UInt16":
                return(new UInt16SettingsVM(memberName, defaultVal));

            case "UInt32":
                return(new UInt32SettingsVM(memberName, defaultVal));

            case "UInt64":
                return(new UInt64SettingsVM(memberName, defaultVal));

            case "Double":
                return(new DoubleSettingsVM(memberName, defaultVal));

            case "Single":
                return(new FloatSettingsVM(memberName, defaultVal));

            case "Decimal":
                return(new DecimalSettingsVM(memberName, defaultVal));

            case "ModKey":
                return(new ModKeySettingsVM(param.DetectedLoadOrder.Transform(x => x.Listing.ModKey), memberName, defaultVal));

            case "FormKey":
                return(new FormKeySettingsVM(memberName, defaultVal));

            case "Array`1":
            case "List`1":
            case "IEnumerable`1":
            {
                var firstGen = targetType.GenericTypeArguments[0];
                switch (firstGen.Name)
                {
                case "SByte":
                    return(EnumerableNumericSettingsVM.Factory <sbyte, Int8SettingsVM>(memberName, defaultVal, new Int8SettingsVM()));

                case "Int16":
                    return(EnumerableNumericSettingsVM.Factory <short, Int16SettingsVM>(memberName, defaultVal, new Int16SettingsVM()));

                case "Int32":
                    return(EnumerableNumericSettingsVM.Factory <int, Int32SettingsVM>(memberName, defaultVal, new Int32SettingsVM()));

                case "Int64":
                    return(EnumerableNumericSettingsVM.Factory <long, Int64SettingsVM>(memberName, defaultVal, new Int64SettingsVM()));

                case "Byte":
                    return(EnumerableNumericSettingsVM.Factory <byte, UInt8SettingsVM>(memberName, defaultVal, new UInt8SettingsVM()));

                case "UInt16":
                    return(EnumerableNumericSettingsVM.Factory <ushort, UInt16SettingsVM>(memberName, defaultVal, new UInt16SettingsVM()));

                case "UInt32":
                    return(EnumerableNumericSettingsVM.Factory <uint, UInt32SettingsVM>(memberName, defaultVal, new UInt32SettingsVM()));

                case "UInt64":
                    return(EnumerableNumericSettingsVM.Factory <ulong, UInt64SettingsVM>(memberName, defaultVal, new UInt64SettingsVM()));

                case "Double":
                    return(EnumerableNumericSettingsVM.Factory <double, DoubleSettingsVM>(memberName, defaultVal, new DoubleSettingsVM()));

                case "Single":
                    return(EnumerableNumericSettingsVM.Factory <float, FloatSettingsVM>(memberName, defaultVal, new FloatSettingsVM()));

                case "Decimal":
                    return(EnumerableNumericSettingsVM.Factory <decimal, DecimalSettingsVM>(memberName, defaultVal, new DecimalSettingsVM()));

                case "ModKey":
                    return(EnumerableModKeySettingsVM.Factory(param, memberName, defaultVal));

                case "FormKey":
                    return(EnumerableFormKeySettingsVM.Factory(memberName, defaultVal));

                default:
                {
                    if (firstGen.Name.Contains("FormLink") &&
                        firstGen.IsGenericType &&
                        firstGen.GenericTypeArguments.Length == 1)
                    {
                        var formLinkGen = firstGen.GenericTypeArguments[0];
                        if (!LoquiRegistration.TryGetRegister(formLinkGen, out var regis))
                        {
                            throw new ArgumentException($"Can't create a formlink control for type: {formLinkGen}");
                        }
                        return(EnumerableFormLinkSettingsVM.Factory(param, memberName, regis.GetterType, defaultVal));
                    }
                    var foundType = param.Assembly.GetType(firstGen.FullName !);
                    if (foundType != null)
                    {
                        return(new EnumerableObjectSettingsVM(param, memberName, foundType));
                    }
                }
                    return(new UnknownSettingsVM(memberName));
                }
            }

            case "HashSet`1":
            {
                var firstGen = targetType.GenericTypeArguments[0];
                switch (firstGen.Name)
                {
                case "ModKey":
                    return(EnumerableModKeySettingsVM.Factory(param, memberName, defaultVal));

                case "FormKey":
                    return(EnumerableFormKeySettingsVM.Factory(memberName, defaultVal));

                default:
                {
                    if (firstGen.Name.Contains("FormLink") &&
                        firstGen.IsGenericType &&
                        firstGen.GenericTypeArguments.Length == 1)
                    {
                        var formLinkGen = firstGen.GenericTypeArguments[0];
                        if (!LoquiRegistration.TryGetRegister(formLinkGen, out var regis))
                        {
                            throw new ArgumentException($"Can't create a formlink control for type: {formLinkGen}");
                        }
                        return(EnumerableFormLinkSettingsVM.Factory(param, memberName, regis.GetterType, defaultVal));
                    }
                    var foundType = param.Assembly.GetType(firstGen.FullName !);
                    if (foundType != null)
                    {
                        return(new EnumerableObjectSettingsVM(param, memberName, foundType));
                    }
                }
                    return(new UnknownSettingsVM(memberName));
                }
            }

            default:
            {
                if (targetType.Name.Contains("FormLink") &&
                    targetType.IsGenericType &&
                    targetType.GenericTypeArguments.Length == 1)
                {
                    return(new FormLinkSettingsVM(param.LinkCache, memberName, targetType));
                }
                var foundType = param.Assembly.GetType(targetType.FullName !);
                if (foundType != null)
                {
                    return(new ObjectSettingsVM(param, memberName, foundType));
                }
            }
                return(new UnknownSettingsVM(memberName));
            }
        }