From 7271e02f6f32bcbb64d0b8b2d8565d1d94c342ab Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 22 Sep 2015 06:42:17 +1000 Subject: [PATCH] Persist key bindings, fixes #81. --- ClassicalSharp/ClassicalSharp.csproj | 1 + ClassicalSharp/Game/Game.InputHandling.cs | 60 +++++++++++++++++++---- ClassicalSharp/Utils/Options.cs | 58 ++++++++++++++++++++++ 3 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 ClassicalSharp/Utils/Options.cs diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index da638e75b..32dc667ae 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -168,6 +168,7 @@ + diff --git a/ClassicalSharp/Game/Game.InputHandling.cs b/ClassicalSharp/Game/Game.InputHandling.cs index 659ae4f86..ae0b32b9a 100644 --- a/ClassicalSharp/Game/Game.InputHandling.cs +++ b/ClassicalSharp/Game/Game.InputHandling.cs @@ -1,7 +1,8 @@ -using System; -using ClassicalSharp.Particles; -using OpenTK; -using OpenTK.Input; +using System; +using System.IO; +using ClassicalSharp.Particles; +using OpenTK; +using OpenTK.Input; namespace ClassicalSharp { @@ -195,15 +196,15 @@ namespace ClassicalSharp { public enum KeyMapping { Forward, Back, Left, Right, Jump, Respawn, SetSpawn, OpenChat, SendChat, PauseOrExit, OpenInventory, Screenshot, Fullscreen, VSync, - ThirdPersonCamera, ViewDistance, Fly, Speed, NoClip, FlyUp, - FlyDown, PlayerList, HideGui, + ThirdPersonCamera, ViewDistance, Fly, Speed, NoClip, FlyUp, + FlyDown, PlayerList, HideGui, } public class KeyMap { public Key this[KeyMapping key] { get { return Keys[(int)key]; } - set { Keys[(int)key] = value; } + set { Keys[(int)key] = value; SaveKeyBindings(); } } Key[] Keys; @@ -229,18 +230,59 @@ namespace ClassicalSharp { Keys = new Key[] { Key.W, Key.S, Key.A, Key.D, Key.Space, Key.R, Key.Y, Key.T, Key.Enter, Key.Escape, Key.B, Key.F12, Key.F11, Key.F7, - Key.F5, Key.F, Key.Z, Key.ShiftLeft, Key.X, Key.Q, + Key.F5, Key.F, Key.Z, Key.ShiftLeft, Key.X, Key.Q, Key.E, Key.Tab, Key.F1 }; #else Keys = new Key[23]; Keys[0] = Key.W; Keys[1] = Key.S; Keys[2] = Key.A; Keys[3] = Key.D; Keys[4] = Key.Space; Keys[5] = Key.R; Keys[6] = Key.Y; Keys[7] = Key.T; - Keys[8] = Key.Enter; Keys[9] = Key.Escape; Keys[10] = Key.B; + Keys[8] = Key.Enter; Keys[9] = Key.Escape; Keys[10] = Key.B; Keys[11] = Key.F12; Keys[12] = Key.F11; Keys[13] = Key.F7; Keys[14] = Key.F5; Keys[15] = Key.F; Keys[16] = Key.Z; Keys[17] = Key.ShiftLeft; Keys[18] = Key.X; Keys[19] = Key.Q; Keys[20] = Key.E; Keys[21] = Key.Tab; Keys[22] = Key.F1; #endif + LoadKeyBindings(); + } + + void LoadKeyBindings() { + try { + Options.Load(); + } catch( IOException ) { + Utils.LogWarning( "Unable to load options.txt" ); + return; + } + string[] names = KeyMapping.GetNames( typeof( KeyMapping ) ); + for( int i = 0; i < names.Length; i++ ) { + string key = "key-" + names[i]; + string value = Options.Get( key ); + if( value == null ) { + Options.Set( key, Keys[i].ToString() ); + continue; + } + + Key mapping; + try { + mapping = (Key)Enum.Parse( typeof( Key ), value, true ); + } catch( ArgumentException ) { + Options.Set( key, Keys[i].ToString() ); + continue; + } + if( !IsReservedKey( mapping ) ) + Keys[i] = mapping; + } + } + + void SaveKeyBindings() { + string[] names = KeyMapping.GetNames( typeof( KeyMapping ) ); + for( int i = 0; i < names.Length; i++ ) { + Options.Set( "key-" + names[i], Keys[i].ToString() ); + } + try { + Options.Save(); + } catch( IOException ) { + Utils.LogWarning( "Unable to sace options.txt" ); + } } } } \ No newline at end of file diff --git a/ClassicalSharp/Utils/Options.cs b/ClassicalSharp/Utils/Options.cs new file mode 100644 index 000000000..b390edc83 --- /dev/null +++ b/ClassicalSharp/Utils/Options.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace ClassicalSharp { + + public static class Options { + + static Dictionary OptionsSet = new Dictionary(); + + public static string Get( string key ) { + string value; + return OptionsSet.TryGetValue( key, out value ) ? value : null; + } + + public static void Set( string key, string value ) { + OptionsSet[key] = value; + } + + public const string OptionsFile = "options.txt"; + + public static void Load() { + try { + using( StreamReader reader = new StreamReader( OptionsFile, false ) ) + LoadFrom( reader ); + } catch( FileNotFoundException ) { + + } + } + + static void LoadFrom( StreamReader reader ) { + string line; + while( ( line = reader.ReadLine() ) != null ) { + if( line.Length == 0 && line[0] == '#' ) continue; + + int separatorIndex = line.IndexOf( '=' ); + if( separatorIndex <= 0 ) continue; + string key = line.Substring( 0, separatorIndex ); + + separatorIndex++; + if( separatorIndex == line.Length ) continue; + string value = line.Substring( separatorIndex, line.Length - separatorIndex ); + OptionsSet[key] = value; + } + } + + public static void Save() { + using( StreamWriter writer = new StreamWriter( OptionsFile ) ) { + foreach( KeyValuePair pair in OptionsSet ) { + writer.Write( pair.Key ); + writer.Write( '=' ); + writer.Write( pair.Value ); + writer.WriteLine(); + } + } + } + } +}