Skip to content

Commit

Permalink
Improve KeyBind data handling
Browse files Browse the repository at this point in the history
Credit to @zaklaus for these changes, originally in #1322.
Will keep JSON data for keybinds, so these changes are sensible and helpful. Thankyou @zaklaus. 👍
  • Loading branch information
Interkarma committed Mar 4, 2020
1 parent ca3ed72 commit e0f181a
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions Assets/Scripts/Game/InputManager.cs
Expand Up @@ -38,6 +38,7 @@ public class InputManager : MonoBehaviour

KeyCode[] reservedKeys = new KeyCode[] { KeyCode.Escape, KeyCode.BackQuote };
Dictionary<KeyCode, Actions> actionKeyDict = new Dictionary<KeyCode, Actions>();
Dictionary<KeyCode, string> unknownActions = new Dictionary<KeyCode, string>();
List<Actions> currentActions = new List<Actions>();
List<Actions> previousActions = new List<Actions>();
bool isPaused;
Expand All @@ -64,7 +65,7 @@ public class InputManager : MonoBehaviour
[fsObject("v1")]
public class KeyBindData_v1
{
public Dictionary<KeyCode, Actions> actionKeyBinds;
public Dictionary<KeyCode, string> actionKeyBinds;
}

#endregion
Expand Down Expand Up @@ -187,6 +188,8 @@ public enum Actions

QuickSave,
QuickLoad,

Unknown,
}

#endregion
Expand Down Expand Up @@ -452,7 +455,20 @@ public void SaveKeyBinds()
string path = GetKeyBindsSavePath();

KeyBindData_v1 keyBindsData = new KeyBindData_v1();
keyBindsData.actionKeyBinds = actionKeyDict;
keyBindsData.actionKeyBinds = new Dictionary<KeyCode, string>();

foreach (var item in actionKeyDict)
{
keyBindsData.actionKeyBinds.Add(item.Key, item.Value.ToString());
}

// If unknown actions were detected in this run, make sure we append them back to the settings file, so we won't break
// the newer builds potentially using them.
foreach (var item in unknownActions)
{
keyBindsData.actionKeyBinds.Add(item.Key, item.Value);
}

string json = SaveLoadManager.Serialize(keyBindsData.GetType(), keyBindsData);
File.WriteAllText(path, json);
RaiseSavedKeyBindsEvent();
Expand Down Expand Up @@ -754,12 +770,35 @@ void LoadKeyBinds()
KeyBindData_v1 keyBindsData = SaveLoadManager.Deserialize(typeof(KeyBindData_v1), json) as KeyBindData_v1;
foreach(var item in keyBindsData.actionKeyBinds)
{
if (!actionKeyDict.ContainsKey(item.Key))
actionKeyDict.Add(item.Key, item.Value);
var actionVal = ActionNameToEnum(item.Value);
if (!actionKeyDict.ContainsKey(item.Key) && actionVal != Actions.Unknown)
actionKeyDict.Add(item.Key, actionVal);
else
{
// This action is unknown in this game, make sure we still keep it so once we save the settings, we
// won't discard them.
unknownActions.Add(item.Key, item.Value);
}
}
RaiseLoadedKeyBindsEvent();
}

static Actions ActionNameToEnum(string value)
{
Actions action;

try
{
action = (Actions)Enum.Parse(typeof(Actions), value, true);
return action;
}
catch (ArgumentException)
{
DaggerfallUnity.LogMessage("Unknown key action detected: " + value, true);
return Actions.Unknown;
}
}

#endregion

#region Events
Expand Down

1 comment on commit e0f181a

@zpl-zak
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad I could help!

Please sign in to comment.