public static TournamentXPSettings GetFromFile(string path)
        {
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));
            }

            JsonSerializerSettings serializerSettings = new JsonSerializerSettings
            {
                Formatting = Formatting.Indented
            };
            TournamentXPSettings settings = new TournamentXPSettings();

            if (File.Exists(System.IO.Path.GetFullPath(path)))
            {
                var settingsjson = File.ReadAllText(path);
                settings = JsonConvert.DeserializeObject <TournamentXPSettings>(settingsjson);
                //write new file to get latest updates
                File.WriteAllText(path, settingsjson);
            }
            else
            {
                var settingsjson = JsonConvert.SerializeObject(settings, serializerSettings);
                File.WriteAllText(path, settingsjson);
            }
            return(settings);
        }
Exemple #2
0
        public TournamentXPSettings GetSettings()
        {
            //var config = new MapperConfiguration(cfg => cfg.CreateMap<TournamentXPSettingsModLib, TournamentXPSettings>());
            //var mapper = new Mapper(config);
            //TournamentXPSettings dto = mapper.Map<TournamentXPSettings>(Instance);
            TournamentXPSettings dto = new TournamentXPSettings();

            PropertyInfo[] propertiesML = typeof(TournamentsXPSettingsModLib).GetProperties();
            foreach (PropertyInfo pTXP in typeof(TournamentXPSettings).GetProperties())
            {
                foreach (PropertyInfo pML in propertiesML)
                {
                    try
                    {
                        if (pTXP.Name == pML.Name && pML.Name != "Instance")
                        {
                            pTXP.SetValue(dto, pML.GetValue(TournamentsXPSettingsModLib.Instance));
                            break;
                        }
                    }
                    catch
                    {
                        //ErrorLog.Log("Error in assigning ModLib property to TXPSettings: " + pTXP.Name + "\n" + ex.ToStringFull());
                    }
                }
            }
            return(dto);
        }
        public static bool LoadSettings(string configPath)
        {
            //Load Settings
            try
            {
                var  overridefile     = System.IO.Path.Combine(configPath, "override.txt");
                bool overrideSettings = false;
                bool forceDebug       = false;


                if (File.Exists(overridefile))
                {
                    string overridetext = File.ReadAllText(overridefile);
                    if (overridetext.IndexOf("force local settings", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        overrideSettings = true;
                    }
                    if (overridetext.IndexOf("force debug on", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        forceDebug = true;
                    }
                    if (overridetext.IndexOf("force disabled on", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        return(false);
                    }
                }
                else
                {
                    File.WriteAllText(overridefile, "");
                }

                bool modLibLoaded = false;

                if (!modLibLoaded)
                {
                    string configFilePath         = System.IO.Path.Combine(configPath, "tournamentxpsettings.json");
                    TournamentXPSettings settings = GetFromFile(configFilePath);

                    if (forceDebug)
                    {
                        settings.DebugMode = true;
                    }
#if DEBUG
                    settings.DebugMode = true;
#endif
                    TournamentXPSettings.SetSettings(settings);
                }
            }
            catch
            {
                // ErrorLog.Log("Error Loading Settings\n" + ex.ToStringFull());
                return(false);
            }
            return(true);
        }
        public static void WriteSettings(string path, TournamentXPSettings settings)
        {
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));
            }
            JsonSerializerSettings serializerSettings = new JsonSerializerSettings
            {
                Formatting = Formatting.Indented
            };
            var settingsjson = JsonConvert.SerializeObject(settings, serializerSettings);

            File.WriteAllText(path, settingsjson);
        }
Exemple #5
0
        public static float GetRenownValue(this TournamentXPSettings settings, CharacterObject character)
        {
            var worth = 0f;

            if (character.IsHero)
            {
                worth += settings.RenownPerHeroProperty[(int)RenownHeroTier.HeroBase];
                var hero = character.HeroObject;
                if (hero != null)
                {
                    if (hero.IsNoble)
                    {
                        worth += settings.RenownPerHeroProperty[(int)RenownHeroTier.IsNoble];
                    }
                    if (hero.IsNotable)
                    {
                        worth += settings.RenownPerHeroProperty[(int)RenownHeroTier.IsNotable];
                    }
                    if (hero.IsCommander)
                    {
                        worth += settings.RenownPerHeroProperty[(int)RenownHeroTier.IsCommander];
                    }
                    if (hero.IsMinorFactionHero)
                    {
                        worth += settings.RenownPerHeroProperty[(int)RenownHeroTier.IsMinorFactionHero];
                    }
                    if (hero.IsFactionLeader)
                    {
                        if (hero.MapFaction.IsKingdomFaction)
                        {
                            worth += settings.RenownPerHeroProperty[(int)RenownHeroTier.IsMajorFactionLeader];
                        }

                        if (hero.MapFaction.IsMinorFaction)
                        {
                            worth += settings.RenownPerHeroProperty[(int)RenownHeroTier.IsMinorFactionHero];
                        }
                    }
                }
            }
            else
            {
                worth += settings.RenownPerTroopTier[character.Tier];
            }
            return(worth);
        }
        public static bool GetModLibSettings(bool forceDebug = false, bool forceMenu = false)
        {
            var modnames = Utilities.GetModulesNames().ToList();

            if (modnames.Contains("Bannerlord.MBOptionScreen"))
            {
                if (forceMenu)
                {
                    //Reenable ModLib settings menu option
                    Module.CurrentModule.AddInitialStateOption(new InitialStateOption("ModOptionsMenu", new TextObject("ModLib Options"), 9990, () =>
                    {
                        ScreenManager.PushScreen(new ModOptionsGauntletScreen());
                    }, false));
                }
            }

            try
            {
                FileDatabase.Initialise(SettingsHelper.ModuleFolderName);
                TournamentXPSettingsModLib settings = FileDatabase.Get <TournamentXPSettingsModLib>(TournamentXPSettingsModLib.InstanceID);
                if (settings == null)
                {
                    settings = new TournamentXPSettingsModLib();
                }
                SettingsDatabase.RegisterSettings(settings);
                if (forceDebug)
                {
                    settings.DebugMode = true;
                }
                TournamentXPSettings.SetSettings(settings.GetSettings());
            }
            catch (Exception ex)
            {
                ErrorLog.Log("TournamentsXPanded failed to initialize settings data.\n\n" + ex.ToStringFull());
                return(false);
            }
            return(true);
        }
        public static bool GetSettings()
        {
            //Load Settings
            try
            {
                var  overridefile     = System.IO.Path.Combine(TaleWorlds.Engine.Utilities.GetConfigsPath(), ModuleFolderName, "override.txt");
                bool overrideSettings = false;
                bool forceDebug       = false;
                bool forceMenu        = false;
                if (File.Exists(overridefile))
                {
                    string overridetext = File.ReadAllText(overridefile);
                    if (overridetext.IndexOf("force local settings", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        overrideSettings = true;
                    }
                    if (overridetext.IndexOf("force debug on", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        forceDebug = true;
                    }
                    if (overridetext.IndexOf("force modlib on", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        forceMenu = true;
                    }
                }

                var  modnames     = Utilities.GetModulesNames().ToList();
                bool modLibLoaded = false;
                if (modnames.Contains("ModLib") && !overrideSettings)
                {
                    //    modLibLoaded =  SettingsHelperModLib.GetModLibSettings(forceDebug, forceMenu);

                    var modlibsettingsdll = System.IO.Path.Combine(TaleWorlds.Engine.Utilities.GetBasePath(), "Modules", ModuleFolderName, "bin", "Win64_Shipping_Client", "TournamentsXPanded.Settings.ModLib.dll");
                    modlibsettingsdll = System.IO.Path.GetFullPath(modlibsettingsdll);
                    Assembly assembly = Assembly.LoadFile(modlibsettingsdll);
                    Type     settingsHelperModLibType = assembly.GetType("TournamentsXPanded.Settings.SettingsHelperModLib");

                    Type TournamentXPSettingsModLib = assembly.GetType("TournamentsXPanded.Settings.SettingsHelperModLib.TournamentXPSettingsModLib");

                    TournamentXPSettings osettings = (TournamentXPSettings)settingsHelperModLibType.GetMethod("GetModLibSettings", BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly).Invoke(null, new object[] { forceDebug, forceMenu });
                    if (osettings != null)
                    {
                        TournamentXPSettings.SetSettings(osettings);
                        modLibLoaded = true;
                    }
                }
                if (!modLibLoaded)
                {
                    TournamentXPSettings settings = new TournamentXPSettings();
                    string configPath             = System.IO.Path.Combine(TaleWorlds.Engine.Utilities.GetConfigsPath(), ModuleFolderName, "tournamentxpsettings.json");
                    if (File.Exists(configPath))
                    {
                        var settingsjson = File.ReadAllText(configPath);
                        settings = JsonConvert.DeserializeObject <TournamentXPSettings>(settingsjson);
                    }
                    else
                    {
                        JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
                        serializerSettings.Formatting = Formatting.Indented;

                        var settingsjson = JsonConvert.SerializeObject(settings, serializerSettings);
                        File.WriteAllText(configPath, settingsjson);
                    }

                    if (forceDebug)
                    {
                        settings.DebugMode = true;
                    }
                    TournamentXPSettings.SetSettings(settings);
                }
            }
            catch (Exception ex)
            {
                ErrorLog.Log("Error Loading Settings\n" + ex.ToStringFull());
                return(false);
            }
            return(true);
        }
Exemple #8
0
 public static void SetSettings(TournamentXPSettings settings)
 {
     _instance = settings;
 }
Exemple #9
0
        public static List <ItemObject.ItemTypeEnum> GetActivePrizeTypes(this TournamentXPSettings settings)
        {
            List <ItemObject.ItemTypeEnum> validTypes = new List <ItemObject.ItemTypeEnum>();

            if (settings.EnableItemType_BodyArmor)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.BodyArmor);
            }
            if (settings.EnableItemType_Bow)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.Bow);
            }
            if (settings.EnableItemType_Cape)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.Cape);
            }
            if (settings.EnableItemType_Crossbow)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.Crossbow);
            }
            if (settings.EnableItemType_HandArmor)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.HandArmor);
            }
            if (settings.EnableItemType_HeadArmor)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.HeadArmor);
            }
            if (settings.EnableItemType_Horse)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.Horse);
            }
            if (settings.EnableItemType_HorseHarness)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.HorseHarness);
            }
            if (settings.EnableItemType_LegArmor)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.LegArmor);
            }
            if (settings.EnableItemType_OneHandedWeapon)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.OneHandedWeapon);
            }
            if (settings.EnableItemType_Polearm)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.Polearm);
            }
            if (settings.EnableItemType_Shield)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.Shield);
            }
            if (settings.EnableItemType_Thrown)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.Thrown);
            }
            if (settings.EnableItemType_TwoHandedWeapon)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.TwoHandedWeapon);
            }
            if (settings.EnableItemType_Arrow)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.Arrows);
            }
            if (settings.EnableItemType_Bolt)
            {
                validTypes.Add(ItemObject.ItemTypeEnum.Bolts);
            }
            return(validTypes);
        }
        public void SetProperty(SettingPropertyAttribute attr, PropertyInfo pInfo, TournamentXPSettings settings)
        {
            _pInfo    = pInfo;
            _settings = settings;


            string text = attr.DisplayName;

            if (text.Trim().StartsWith("{"))
            {
                var id = LocalizedTextManager.GetStringId(text);
                text = LocalizedTextManager.GetTranslatedText(languageId, id);
            }
            lblName.Text = text;
            if (!string.IsNullOrWhiteSpace(attr.HintText))
            {
                string text2 = attr.HintText;
                if (text2.Trim().StartsWith("{"))
                {
                    var id = LocalizedTextManager.GetStringId(text2);
                    text2 = LocalizedTextManager.GetTranslatedText(languageId, id);
                }
                ToolTip tt = new ToolTip();
                tt.IsBalloon = true;
                tt.SetToolTip(lblName, text2);
            }

            checkBox1.Visible  = false;
            textBox1.Visible   = false;
            textBox1.TextAlign = HorizontalAlignment.Right;
            trackBar1.Visible  = false;
            trackBar2.Visible  = false;

            switch (pInfo.PropertyType.Name)
            {
            case "Boolean":
                checkBox1.Visible = true;
                checkBox1.Checked = (bool)pInfo.GetValue(settings);
                break;

            case "Int32":
            case "Int64":
            case "Integer":
                textBox1.Visible  = true;
                textBox1.Text     = ((int)pInfo.GetValue(settings)).ToString();
                trackBar1.Visible = true;
                trackBar1.Minimum = (int)attr.MinValue;
                trackBar1.Maximum = (int)attr.MaxValue;
                trackBar1.Value   = ((int)pInfo.GetValue(settings));
                break;

            case "Single":
                textBox1.Visible  = true;
                textBox1.Text     = ((float)pInfo.GetValue(settings)).ToString();
                trackBar2.Visible = true;
                trackBar2.Minimum = attr.MinValue;
                trackBar2.Maximum = attr.MaxValue;
                trackBar2.Value   = ((float)pInfo.GetValue(settings));
                break;

            default:
                break;
            }
        }
Exemple #11
0
        public void LoadConfig(TournamentXPSettings settings)
        {
            _settings = settings;
            pnlSettings.Controls.Clear();
            settingprops = new List <XPSettingControlBase>();
            //var groups = new List<SettingPropertyGroup>();
            propList = (from p in typeof(TournamentXPSettings).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                        let propAttr = p.GetCustomAttribute <SettingPropertyAttribute>(true)
                                       let groupAttr = p.GetCustomAttribute <SettingPropertyGroupAttribute>(true)
                                                       where propAttr != null
                                                       let groupAttrToAdd = groupAttr ?? null
                                                                            select new ConfigInfo {
                attribute = propAttr, propertyInfo = p, pgroup = groupAttrToAdd
            }).ToList();


            List <string> descriptions = new List <string>();
            List <string> hints        = new List <string>();
            List <string> groups       = new List <string>();


            List <FlowLayoutPanel> groupPanels = new List <FlowLayoutPanel>();

            groupPanels.Add(new FlowLayoutPanel()
            {
                Name = "Default"
            });

            var groupcount = 0;

            foreach (var pr in propList)
            {
                FlowLayoutPanel pnl = groupPanels.Where(x => x.Name == "Default").First();
                if (pr.pgroup != null)
                {
                    pnl = groupPanels.Where(x => x.Name == pr.pgroup.GroupName).FirstOrDefault();
                    if (pnl == null)
                    {
                        pnl = new FlowLayoutPanel()
                        {
                            Name = pr.pgroup.GroupName
                        };
                        pnl.AutoScroll   = false;
                        pnl.AutoSize     = true;
                        pnl.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                        pnl.BorderStyle  = BorderStyle.FixedSingle;

                        string text = pr.pgroup.GroupName;
                        if (text.Trim().StartsWith("{"))
                        {
                            var id = LocalizedTextManager.GetStringId(text);
                            text = LocalizedTextManager.GetTranslatedText(ddlLanguage.SelectedItem.ToString(), id);
                        }

                        pnl.Controls.Add(new Label()
                        {
                            AutoSize = true, MinimumSize = new Size(700, 50), Text = text, Font = new System.Drawing.Font("Arial Narrow", 22F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)))
                        });
                        groupPanels.Add(pnl);
                        groupcount++;
                        groups.Add(pr.pgroup.GroupName);
                    }
                }

                var uc = new XPSettingControlBase(ddlLanguage.SelectedItem.ToString());
                uc.Anchor = AnchorStyles.Top;
                uc.SetProperty(pr.attribute, pr.propertyInfo, _settings);
                pnl.Controls.Add(uc);
                settingprops.Add(uc);
#if DEBUG
                descriptions.Add(pr.attribute.DisplayName);
                if (!string.IsNullOrWhiteSpace(pr.attribute.HintText))
                {
                    hints.Add(pr.attribute.HintText);
                }
#endif
            }

            foreach (var pnl in groupPanels)
            {
                if (pnl.Controls.Count > 0)
                {
                    pnlSettings.Controls.Add(pnl);
                }
            }

            pnlSettings.Refresh();
#if DEBUG
            using (var sw = new System.IO.StreamWriter("E:\\texts.json"))
            {
                var count = 0;
                foreach (var s in groups)
                {
                    count++;
                    string id = ("000" + count.ToString());
                    id = id.Substring(id.Length - 4);
                    sw.WriteLine("<string id=\"txpg" + id + "\" text=\"" + s + "\" />");
                }
                foreach (var s in descriptions)
                {
                    count++;
                    string id = ("000" + count.ToString());
                    id = id.Substring(id.Length - 4);
                    sw.WriteLine("<string id=\"txpd" + id + "\" text=\"" + s + "\" />");
                }
                foreach (var s in hints)
                {
                    count++;
                    string id = ("000" + count.ToString());
                    id = id.Substring(id.Length - 4);
                    sw.WriteLine("<string id=\"txph" + id + "\" text=\"" + s + "\" />");
                }
            }
#endif
        }
Exemple #12
0
        public static bool LoadSettings()
        {
            //Load Settings
            try
            {
                var  overridefile     = System.IO.Path.Combine(TaleWorlds.Engine.Utilities.GetConfigsPath(), ModuleFolderName, "override.txt");
                bool overrideSettings = false;
                bool forceDebug       = false;
                bool forceMenu        = false;
                if (File.Exists(overridefile))
                {
                    string overridetext = File.ReadAllText(overridefile);
                    if (overridetext.IndexOf("force local settings", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        overrideSettings = true;
                    }
                    if (overridetext.IndexOf("force debug on", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        forceDebug = true;
                    }
                }
                else
                {
                    File.WriteAllText(overridefile, "");
                }
                var  modnames     = Utilities.GetModulesNames().ToList();
                bool modLibLoaded = false;
                if (
                    (modnames.Contains("ModLib")
                     //  || modnames.Contains("Bannerlord.MBOptionScreen")
                    ) && !overrideSettings)
                {
                    Type     settingsHelperModLibType = null;
                    Assembly modlibassembly           = null;
                    Assembly modlibSettingsAssembly   = null;

                    try
                    {
                        modlibassembly = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.GetName().Name == "ModLib").FirstOrDefault();
                    }
                    catch { }

                    //try
                    //{
                    //    if (!AppDomain.CurrentDomain.GetAssemblies().Select(x => x.GetName().Name).ToList().Contains("ModLib"))
                    //    {
                    //        var modlibdllpath = System.IO.Path.Combine(TaleWorlds.Engine.Utilities.GetBasePath(), "Modules", "Bannerlord.MBOptionScreen", "bin", "Win64_Shipping_Client", "ModLib.dll");
                    //        modlibdllpath = System.IO.Path.GetFullPath(modlibdllpath);
                    //        if (File.Exists(modlibdllpath))
                    //            modlibassembly = Assembly.LoadFile(modlibdllpath);
                    //    }
                    //}
                    //catch (Exception ex)
                    //{
                    //    ErrorLog.Log("Error Loading ModLib Assembly\n" + ex.ToStringFull());
                    //}

                    if (modlibassembly != null)
                    {
                        var modlibsettingsdll = System.IO.Path.Combine(TaleWorlds.Engine.Utilities.GetBasePath(), "Modules", ModuleFolderName, "bin", "Win64_Shipping_Client", "TournamentsXPanded.Settings.ModLib.dll");
                        modlibsettingsdll        = System.IO.Path.GetFullPath(modlibsettingsdll);
                        modlibSettingsAssembly   = Assembly.LoadFile(modlibsettingsdll);
                        settingsHelperModLibType = modlibSettingsAssembly.GetType("TournamentsXPanded.Settings.SettingsHelperModLib");
                    }

                    if (settingsHelperModLibType != null)
                    {
                        try
                        {
                            TournamentXPSettings osettings = (TournamentXPSettings)settingsHelperModLibType.GetMethod("GetModLibSettings", BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly).Invoke(null, new object[] { forceDebug });
                            if (osettings != null)
                            {
                                TournamentXPSettings.SetSettings(osettings);
                                modLibLoaded = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            ErrorLog.Log("Error getting ModLib settings\n" + ex.ToStringFull());
                        }
                    }
                }
                if (!modLibLoaded)
                {
                    JsonSerializerSettings serializerSettings = new JsonSerializerSettings
                    {
                        Formatting = Formatting.Indented
                    };
                    TournamentXPSettings settings = new TournamentXPSettings();
                    string configPath             = System.IO.Path.Combine(TaleWorlds.Engine.Utilities.GetConfigsPath(), ModuleFolderName, "tournamentxpsettings.json");
                    if (File.Exists(configPath))
                    {
                        var settingsjson = File.ReadAllText(configPath);
                        settings = JsonConvert.DeserializeObject <TournamentXPSettings>(settingsjson);
                        //write new file to get latest updates
                        File.WriteAllText(configPath, settingsjson);
                    }
                    else
                    {
                        var settingsjson = JsonConvert.SerializeObject(settings, serializerSettings);
                        File.WriteAllText(configPath, settingsjson);
                    }

                    if (forceDebug)
                    {
                        settings.DebugMode = true;
                    }

                    TournamentXPSettings.SetSettings(settings);
                }
            }
            catch (Exception ex)
            {
                ErrorLog.Log("Error Loading Settings\n" + ex.ToStringFull());
                return(false);
            }
            return(true);
        }