예제 #1
0
        /// <summary>
        ///     Attempts to load settings from file
        /// </summary>
        /// <returns>A GlocalSettings</returns>
        public static GlobalSettings Load(string path = null)
        {
            path = path ?? DefaultSettingsPath;
            var settings = new GlobalSettings();

            try
            {
                var hasSettings = File.Exists(path);

                var tempPath = GetTempSettingsPath(path);

                var recoverFromCrash = !hasSettings && File.Exists(tempPath);
                if (hasSettings || recoverFromCrash)
                {
                    if (recoverFromCrash)
                    {
                        File.Move(tempPath, path);
                    }

                    XElement root = XElement.Load(path);
                    settings.WowVersion             = root.Element("WowVersion").Value;
                    settings.AutoStart              = GetElementValue <bool>(root.Element("AutoStart"));
                    settings.WowDelay               = GetElementValue <int>(root.Element("WowDelay"));
                    settings.HBDelay                = GetElementValue(root.Element("HBDelay"), 10);
                    settings.LoginDelay             = GetElementValue(root.Element("LoginDelay"), 3);
                    settings.UseDarkStyle           = GetElementValue(root.Element("UseDarkStyle"), true);
                    settings.CheckRealmStatus       = GetElementValue(root.Element("CheckRealmStatus"), false);
                    settings.CheckHbResponsiveness  = GetElementValue(root.Element("CheckHbResponsiveness"), true);
                    settings.CheckWowResponsiveness = GetElementValue(root.Element("CheckWowResponsiveness"), true);
                    settings.AutoUpdateHB           = GetElementValue(root.Element("AutoUpdateHB"), true);
                    settings.MinimizeHbOnStart      = GetElementValue(root.Element("MinimizeHbOnStart"), false);
                    settings.AutoAcceptTosEula      = GetElementValue(root.Element("AutoAcceptTosEula"), false);
                    settings.SetGameWindowTitle     = GetElementValue(root.Element("SetGameWindowTitle"), true);
                    settings.GameWindowTitle        = GetElementValue(root.Element("GameWindowTitle"), "{name} - {pid}");

                    settings.GameStateOffset = GetElementValue(root.Element("GameStateOffset"), 0u);
                    // settings.FrameScriptExecuteOffset = GetElementValue(root.Element("FrameScriptExecuteOffset"), 0u);
                    settings.FocusedWidgetOffset = GetElementValue(root.Element("FocusedWidgetOffset"), 0u);
                    settings.LuaStateOffset      = GetElementValue(root.Element("LuaStateOffset"), 0u);
                    //settings.LastHardwareEventOffset = GetElementValue(root.Element("LastHardwareEventOffset"), 0u);
                    settings.GlueStateOffset = GetElementValue(root.Element("GlueStateOffset"), 0u);

                    XElement characterProfilesElement = root.Element("CharacterProfiles");
                    foreach (XElement profileElement in characterProfilesElement.Elements("CharacterProfile"))
                    {
                        var      profile         = new CharacterProfile();
                        XElement settingsElement = profileElement.Element("Settings");
                        profile.Settings.ProfileName = GetElementValue <string>(settingsElement.Element("ProfileName"));
                        profile.Settings.IsEnabled   = GetElementValue <bool>(settingsElement.Element("IsEnabled"));
                        XElement wowSettingsElement = settingsElement.Element("WowSettings");

                        // Wow Settings
                        if (wowSettingsElement != null)
                        {
                            profile.Settings.WowSettings.LoginData                    = GetElementValue <string>(wowSettingsElement.Element("LoginData"));
                            profile.Settings.WowSettings.PasswordData                 = GetElementValue <string>(wowSettingsElement.Element("PasswordData"));
                            profile.Settings.WowSettings.AcountName                   = GetElementValue <string>(wowSettingsElement.Element("AcountName"));
                            profile.Settings.WowSettings.CharacterName                = GetElementValue <string>(wowSettingsElement.Element("CharacterName"));
                            profile.Settings.WowSettings.ServerName                   = GetElementValue <string>(wowSettingsElement.Element("ServerName"));
                            profile.Settings.WowSettings.AuthenticatorSerialData      = GetElementValue <string>(wowSettingsElement.Element("AuthenticatorSerialData"));
                            profile.Settings.WowSettings.AuthenticatorRestoreCodeData = GetElementValue <string>(wowSettingsElement.Element("AuthenticatorRestoreCodeData"));
                            profile.Settings.WowSettings.Region          = GetElementValue <WowSettings.WowRegion>(wowSettingsElement.Element("Region"));
                            profile.Settings.WowSettings.WowPath         = GetElementValue <string>(wowSettingsElement.Element("WowPath"));
                            profile.Settings.WowSettings.WowArgs         = GetElementValue <string>(wowSettingsElement.Element("WowArgs"));
                            profile.Settings.WowSettings.WowWindowWidth  = GetElementValue <int>(wowSettingsElement.Element("WowWindowWidth"));
                            profile.Settings.WowSettings.WowWindowHeight = GetElementValue <int>(wowSettingsElement.Element("WowWindowHeight"));
                            profile.Settings.WowSettings.WowWindowX      = GetElementValue <int>(wowSettingsElement.Element("WowWindowX"));
                            profile.Settings.WowSettings.WowWindowY      = GetElementValue <int>(wowSettingsElement.Element("WowWindowY"));
                        }
                        XElement hbSettingsElement = settingsElement.Element("HonorbuddySettings");
                        // Honorbuddy Settings
                        if (hbSettingsElement != null)
                        {
                            profile.Settings.HonorbuddySettings.HonorbuddyKeyData = GetElementValue <string>(hbSettingsElement.Element("HonorbuddyKeyData"));
                            profile.Settings.HonorbuddySettings.CustomClass       = GetElementValue <string>(hbSettingsElement.Element("CustomClass"));
                            profile.Settings.HonorbuddySettings.BotBase           = GetElementValue <string>(hbSettingsElement.Element("BotBase"));
                            profile.Settings.HonorbuddySettings.HonorbuddyProfile = GetElementValue <string>(hbSettingsElement.Element("HonorbuddyProfile"));
                            profile.Settings.HonorbuddySettings.HonorbuddyPath    = GetElementValue <string>(hbSettingsElement.Element("HonorbuddyPath"));
                            profile.Settings.HonorbuddySettings.UseHBBeta         = GetElementValue <bool>(hbSettingsElement.Element("UseHBBeta"));
                        }
                        XElement tasksElement = profileElement.Element("Tasks");
                        // Load the Task list.
                        foreach (XElement taskElement in tasksElement.Elements())
                        {
                            Type taskType = Type.GetType("HighVoltz.HBRelog.Tasks." + taskElement.Name);
                            if (taskType != null)
                            {
                                var task = (BMTask)Activator.CreateInstance(taskType);
                                task.SetProfile(profile);
                                // Dictionary of property Names and the corresponding PropertyInfo
                                Dictionary <string, PropertyInfo> propertyDict =
                                    task.GetType()
                                    .GetProperties()
                                    .Where(pi => pi.GetCustomAttributesData().All(cad => cad.Constructor.DeclaringType != typeof(XmlIgnoreAttribute)))
                                    .ToDictionary(k => k.Name);

                                foreach (XAttribute attr in taskElement.Attributes())
                                {
                                    string propKey = attr.Name.ToString();
                                    if (propertyDict.ContainsKey(propKey))
                                    {
                                        // if property is an enum then use Enum.Parse.. otherwise use Convert.ChangeValue
                                        object val = typeof(Enum).IsAssignableFrom(propertyDict[propKey].PropertyType)
                                                         ? Enum.Parse(propertyDict[propKey].PropertyType, attr.Value)
                                                         : Convert.ChangeType(attr.Value, propertyDict[propKey].PropertyType);
                                        propertyDict[propKey].SetValue(task, val, null);
                                    }
                                    else
                                    {
                                        Log.Err("{0} does not have a property called {1}", taskElement.Name, attr.Name);
                                    }
                                }
                                profile.Tasks.Add(task);
                            }
                            else
                            {
                                Log.Err("{0} is not a known task type", taskElement.Name);
                            }
                        }
                        settings.CharacterProfiles.Add(profile);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Err(ex.ToString());
            }
            return(settings);
        }
예제 #2
0
 static GlobalSettings()
 {
     Instance = new GlobalSettings();
     Instance.Load(GetSettingsPath());
 }