Esempio n. 1
0
 public void AddMod(IHasModSettings mod)
 {
     foreach (var setting in mod.Settings)
     {
         AddSetting(setting);
     }
 }
Esempio n. 2
0
 public static bool HasSavedDataPresent(IHasModSettings mod)
 {
     if (modSettings == null)
     {
         return(false);
     }
     return(modSettings.ContainsKey(mod.UniqueID));
 }
Esempio n. 3
0
        public static void RemoveSettings(IHasModSettings mod)
        {
            if (modSettings.ContainsKey(mod.UniqueID))
            {
                modSettings.Remove(mod.UniqueID);
            }

            JsonHelper.Serialize(modSettings, settingsFile);
        }
Esempio n. 4
0
 private static bool TryGetSavedBinaryValue <T>(IHasModSettings mod, IModSetting <T> setting, out T value) where T : IEquatable <T>
 {
     if (deserializedBinary.TryGetValue(mod.FactorioVersion, out var settings))
     {
         return(settings.TryGetValue(setting, out value));
     }
     else
     {
         value = default(T);
         return(false);
     }
 }
Esempio n. 5
0
            public IList <IModSetting> ToSettings(IHasModSettings owner)
            {
                var result = new List <IModSetting>(data.Count);

                foreach (var dict in data)
                {
                    if (TryConvert(dict, owner, out var settingInfo))
                    {
                        result.Add(settingInfo);
                    }
                }
                return(result);
            }
Esempio n. 6
0
 private static void AddSettings(IHasModSettings mod)
 {
     if (mod.HasSettings && mod.Override)
     {
         var exportTemplate = new ModSettingsExportTemplate(mod);
         modSettings[mod.UniqueID] = exportTemplate;
     }
     else
     {
         if (modSettings.ContainsKey(mod.UniqueID))
         {
             modSettings.Remove(mod.UniqueID);
         }
     }
 }
Esempio n. 7
0
        public IList <IModSetting> GetSettings(ModCollection parentCollection, IHasModSettings owner)
        {
            const string mainFileName = "settings.lua";

            if (settings == null)
            {
                var script = new Script();

                var data    = new SettingsData();
                var luaData = UserData.Create(data);
                script.Globals.Set("data", luaData);

                var modsTable = new Table(script);
                foreach (var mod in parentCollection.Where(m => m.FactorioVersion == InfoFile.FactorioVersion))
                {
                    modsTable.Set(DynValue.NewString(mod.Name), DynValue.True);
                }
                script.Globals.Set("mods", DynValue.NewTable(modsTable));

                if (isFile)
                {
                    using (var archive = ZipFile.OpenRead(file.FullName))
                    {
                        script.Options.ScriptLoader = new ArchiveSettingsScriptLoader(LoadSettingsFileFromArchive, archive, parentCollection, InfoFile)
                        {
                            ModulePaths = new[] { "?.lua" }
                        };
                        string mainFile = LoadSettingsFileFromArchive(archive, mainFileName);
                        script.DoString(mainFile);
                    }
                }
                else
                {
                    script.Options.ScriptLoader = new DirectorySettingsScriptLoader(LoadSettingsFileFromDirectory, parentCollection, InfoFile)
                    {
                        ModulePaths = new[] { "?.lua" }
                    };
                    string mainFile = LoadSettingsFileFromDirectory(mainFileName);
                    script.DoString(mainFile);
                }

                settings = data.ToSettings(owner);
            }

            return(settings);
        }
Esempio n. 8
0
        public static bool TryGetSavedValue <T>(IHasModSettings mod, IModSetting <T> setting, out T value) where T : IEquatable <T>
        {
            if (mod.UseBinaryFileOverride && TryGetSavedBinaryValue(mod, setting, out value))
            {
                return(true);
            }

            bool result = modSettings.TryGetValue(mod.UniqueID, out var settings);

            if (result)
            {
                return(settings.TryGetValue(setting, out value));
            }
            else
            {
                value = default(T);
                return(false);
            }
        }
Esempio n. 9
0
        public IModSetting ToSetting(IHasModSettings owner)
        {
            switch (Type)
            {
            case SettingType.Boolean:
                return(new BooleanModSetting(owner, Name, LoadTime, Ordering, DefaultValue.GetBoolean()));

            case SettingType.Integer:
                if ((AllowedValues == null) || (AllowedValues.Length == 0))
                {
                    return(new IntegerModSetting(owner, Name, LoadTime, Ordering, DefaultValue.GetInteger(), MinValue?.GetInteger() ?? long.MinValue, MaxValue?.GetInteger() ?? long.MaxValue));
                }
                else
                {
                    return(new IntegerListModSetting(owner, Name, LoadTime, Ordering, DefaultValue.GetInteger(), AllowedValues.Select(value => value.GetInteger())));
                }

            case SettingType.FloatingPoint:
                if ((AllowedValues == null) || (AllowedValues.Length == 0))
                {
                    return(new FloatingPointModSetting(owner, Name, LoadTime, Ordering, DefaultValue.GetFloatingPoint(), MinValue?.GetFloatingPoint() ?? decimal.MinValue, MaxValue?.GetFloatingPoint() ?? decimal.MaxValue));
                }
                else
                {
                    return(new FloatingPointListModSetting(owner, Name, LoadTime, Ordering, DefaultValue.GetFloatingPoint(), AllowedValues.Select(value => value.GetFloatingPoint())));
                }

            case SettingType.String:
                if ((AllowedValues == null) || (AllowedValues.Length == 0))
                {
                    return(new StringModSetting(owner, Name, LoadTime, Ordering, DefaultValue.GetString(), AllowEmptyValue));
                }
                else
                {
                    return(new StringListModSetting(owner, Name, LoadTime, Ordering, DefaultValue.GetString(), AllowedValues.Select(value => value.GetString())));
                }
            }

            return(null);
        }
Esempio n. 10
0
        public ModSettingsExportTemplate(IHasModSettings mod)
            : this()
        {
            foreach (var setting in mod.Settings)
            {
                var template = setting.CreateValueTemplate();
                switch (setting.LoadTime)
                {
                case LoadTime.Startup:
                    StartupTemplates[setting.Name] = template;
                    break;

                case LoadTime.RuntimeGlobal:
                    RuntimeGlobalTemplates[setting.Name] = template;
                    break;

                case LoadTime.RuntimeUser:
                    RuntimeUserTemplates[setting.Name] = template;
                    break;
                }
            }
        }
Esempio n. 11
0
 public ModSettingsExportTemplate(IHasModSettings mod)
     : this()
 {
     AddMod(mod);
 }
Esempio n. 12
0
 public FloatingPointListModSetting(IHasModSettings owner, string name, LoadTime loadTime, string ordering, decimal defaultValue, IEnumerable <decimal> allowedValues)
     : base(owner, name, loadTime, ordering, defaultValue, allowedValues)
 {
 }
 public FloatingPointModSetting(IHasModSettings owner, string name, LoadTime loadTime, string ordering, double defaultValue, double minValue, double maxValue)
     : base(owner, name, loadTime, ordering, defaultValue, minValue, maxValue)
 {
 }
Esempio n. 14
0
 public static void SaveSettings(IHasModSettings mod)
 {
     AddSettings(mod);
     JsonHelper.Serialize(modSettings, settingsFile);
 }
Esempio n. 15
0
            private bool TryConvert(Dictionary <string, DynValue> dict, IHasModSettings owner, out IModSetting result)
            {
                result = null;

                if (!dict.TryGetValue("type", out var typeDyn))
                {
                    return(false);
                }
                if ((typeDyn.Type != DataType.String) || !settingTypeMapping.TryGetValue(typeDyn.String, out var type))
                {
                    return(false);
                }

                if (!dict.TryGetValue("name", out var nameDyn))
                {
                    return(false);
                }
                if (nameDyn.Type != DataType.String)
                {
                    return(false);
                }
                string name = nameDyn.String;

                if (!dict.TryGetValue("setting_type", out var loadTimeDyn))
                {
                    return(false);
                }
                if ((loadTimeDyn.Type != DataType.String) || !loadTimeMapping.TryGetValue(loadTimeDyn.String, out var loadTime))
                {
                    return(false);
                }

                string ordering = string.Empty;

                if (dict.TryGetValue("order", out var orderingDyn) && (orderingDyn.Type == DataType.String))
                {
                    ordering = orderingDyn.String;
                }

                bool hasDefaultValue = dict.TryGetValue("default_value", out var defaultValueDyn);

                bool isList = dict.TryGetValue("allowed_values", out var allowedValuesDyn);

                if (isList && (allowedValuesDyn.Type != DataType.Table))
                {
                    return(false);
                }

                if (isList)
                {
                    var allowedValuesListDyn = allowedValuesDyn.Table.Values;

                    switch (type)
                    {
                    case SettingType.Boolean:
                        return(false);

                    case SettingType.Integer:
                        if (hasDefaultValue && (defaultValueDyn.Type != DataType.Number))
                        {
                            return(false);
                        }
                        if (!TryConvertNumberTable(allowedValuesListDyn, out var allowedIntValues))
                        {
                            return(false);
                        }
                        result = new IntegerListModSetting(owner, name, loadTime, ordering, hasDefaultValue ? (long)defaultValueDyn.Number : 0, allowedIntValues.Select(value => (long)value));
                        break;

                    case SettingType.FloatingPoint:
                        if (hasDefaultValue && (defaultValueDyn.Type != DataType.Number))
                        {
                            return(false);
                        }
                        if (!TryConvertNumberTable(allowedValuesListDyn, out var allowedFloatValues))
                        {
                            return(false);
                        }
                        result = new FloatingPointListModSetting(owner, name, loadTime, ordering, hasDefaultValue ? (decimal)defaultValueDyn.Number : 0, allowedFloatValues);
                        break;

                    case SettingType.String:
                        if (hasDefaultValue && (defaultValueDyn.Type != DataType.String))
                        {
                            return(false);
                        }
                        if (!TryConvertStringTable(allowedValuesListDyn, out var allowedStringValues))
                        {
                            return(false);
                        }
                        result = new StringListModSetting(owner, name, loadTime, ordering, hasDefaultValue ? defaultValueDyn.String : string.Empty, allowedStringValues);
                        break;
                    }
                }
                else
                {
                    bool hasMinValue = dict.TryGetValue("minimum_value", out var minValueDyn);
                    if (hasMinValue && (minValueDyn.Type != DataType.Number))
                    {
                        return(false);
                    }

                    bool hasMaxValue = dict.TryGetValue("maximum_value", out var maxValueDyn);
                    if (hasMaxValue && (maxValueDyn.Type != DataType.Number))
                    {
                        return(false);
                    }

                    switch (type)
                    {
                    case SettingType.Boolean:
                        if (hasDefaultValue && (defaultValueDyn.Type != DataType.Boolean))
                        {
                            return(false);
                        }
                        result = new BooleanModSetting(owner, name, loadTime, ordering, hasDefaultValue ? defaultValueDyn.Boolean : false);
                        break;

                    case SettingType.Integer:
                        if (hasDefaultValue && (defaultValueDyn.Type != DataType.Number))
                        {
                            return(false);
                        }
                        result = new IntegerModSetting(owner, name, loadTime, ordering, hasDefaultValue ? (long)defaultValueDyn.Number : 0, hasMinValue ? (long)minValueDyn.Number : long.MinValue, hasMaxValue ? (long)maxValueDyn.Number : long.MaxValue);
                        break;

                    case SettingType.FloatingPoint:
                        if (hasDefaultValue && (defaultValueDyn.Type != DataType.Number))
                        {
                            return(false);
                        }
                        result = new FloatingPointModSetting(owner, name, loadTime, ordering, hasDefaultValue ? (decimal)defaultValueDyn.Number : 0, hasMinValue ? (decimal)minValueDyn.Number : decimal.MinValue, hasMaxValue ? (decimal)maxValueDyn.Number : decimal.MaxValue);
                        break;

                    case SettingType.String:
                        if (hasDefaultValue && (defaultValueDyn.Type != DataType.String))
                        {
                            return(false);
                        }
                        bool allowEmptyValue = false;
                        if (dict.TryGetValue("allow_blank", out var allowEmptyValueDyn) && (allowEmptyValueDyn.Type == DataType.Boolean))
                        {
                            allowEmptyValue = allowEmptyValueDyn.Boolean;
                        }
                        result = new StringModSetting(owner, name, loadTime, ordering, hasDefaultValue ? defaultValueDyn.String : string.Empty, allowEmptyValue);
                        break;
                    }
                }

                return(true);
            }
Esempio n. 16
0
 public int Compare(IHasModSettings x, IHasModSettings y)
 {
     return(string.Compare(x.DisplayName, y.DisplayName, StringComparison.InvariantCultureIgnoreCase));
 }
Esempio n. 17
0
 public BooleanModSetting(IHasModSettings owner, string name, LoadTime loadTime, string ordering, bool defaultValue)
     : base(owner, name, loadTime, ordering, defaultValue)
 {
 }
Esempio n. 18
0
 public IntegerListModSetting(IHasModSettings owner, string name, LoadTime loadTime, string ordering, long defaultValue, IEnumerable <long> allowedValues)
     : base(owner, name, loadTime, ordering, defaultValue, allowedValues)
 {
 }
Esempio n. 19
0
 public IntegerModSetting(IHasModSettings owner, string name, LoadTime loadTime, string ordering, long defaultValue, long minValue, long maxValue)
     : base(owner, name, loadTime, ordering, defaultValue, minValue, maxValue)
 {
 }