Example #1
0
        // Load all keybinds.
        public static void LoadBinds()
        {
            foreach (Mod mod in ModLoader.LoadedMods)
            {
                // Check if there are custom keybinds
                string path = Path.Combine(ModLoader.GetModConfigFolder(mod), "keybinds.xml");

                if (!File.Exists(path))
                {
                    SaveModBinds(mod);
                    continue;
                }

                // Load XML
                XmlDocument doc = new XmlDocument();
                doc.Load(path);

                foreach (XmlNode keybind in doc.GetElementsByTagName("Keybind"))
                {
                    XmlNode id       = keybind.SelectSingleNode("ID");
                    XmlNode key      = keybind.SelectSingleNode("Key");
                    XmlNode modifier = keybind.SelectSingleNode("Modifier");

                    // Check if its valid and fetch
                    if (id == null || key == null || modifier == null)
                    {
                        continue;
                    }

                    Keybind bind = Keybind.Keybinds.Find(x => x.Mod == mod && x.ID == id.InnerText);

                    if (bind == null)
                    {
                        continue;
                    }

                    // Set bind
                    try
                    {
                        KeyCode code = (KeyCode)Enum.Parse(typeof(KeyCode), key.InnerText);
                        bind.Key = code;
                    }
                    catch (Exception e)
                    {
                        bind.Key = KeyCode.None;
                        ModConsole.Error(e.Message);
                    }

                    try
                    {
                        KeyCode code = (KeyCode)Enum.Parse(typeof(KeyCode), modifier.InnerText);
                        bind.Modifier = code;
                    }
                    catch (Exception e)
                    {
                        bind.Modifier = KeyCode.None;
                        ModConsole.Error(e.Message);
                    }
                }
            }
        }