Exemple #1
0
        public KeybindDict Clone()
        {
            KeybindDict newCopy = new KeybindDict();

            foreach (KeyValuePair <KeyAction, DualKeyCombo> entry in this)
            {
                newCopy.Add(entry.Key, entry.Value.Clone());
            }
            return(newCopy);
        }
Exemple #2
0
    public void LoadKeybinds()
    {
        Logger.Log("Loading user keybinds", Category.Keybindings);
        // Get the user's saved keybinds from PlayerPrefs
        string jsonKeybinds = PlayerPrefs.GetString("userKeybinds");

        if (jsonKeybinds != "")
        {
            // Check if user has any saved keybinds and deserialize it from JSON
            // If there are any problems then just reset controls to default
            try
            {
                userKeybinds = JsonConvert.DeserializeObject <KeybindDict>(jsonKeybinds);
                // Set keybind texts when loading keybinds
                foreach (var keyValuePair in userKeybinds)
                {
                    UIManager.UpdateKeybindText(keyValuePair.Key, keyValuePair.Value.PrimaryCombo);
                }
            }
            catch (Exception e)
            {
                Logger.LogWarning("Couldn't deserialize userKeybind JSON: " + e, Category.Keybindings);
                ResetKeybinds();
                ModalPanelManager.Instance.Inform("Unable to read saved keybinds.\nThey were either corrupt or outdated, so they have been reset.");
            }

            // Properly updating user keybinds when we add or remove one
            var newHotkeys        = defaultKeybinds.Keys.Except(userKeybinds.Keys);
            var deprecatedHotKeys = userKeybinds.Keys.Except(defaultKeybinds.Keys);

            foreach (KeyAction entry in newHotkeys)
            {
                userKeybinds.Add(entry, defaultKeybinds[entry]);
            }
            foreach (KeyAction entry in deprecatedHotKeys)
            {
                userKeybinds.Remove(entry);
            }
        }
        else
        {
            // Make a new copy of defaultKeybinds and make userKeybinds reference it
            Logger.Log("No saved keybinds found. Using default.", Category.Keybindings);
            ResetKeybinds();
        }
    }