Persist key bindings, fixes #81.

This commit is contained in:
UnknownShadow200 2015-09-22 06:42:17 +10:00
parent 8a076102f4
commit 7271e02f6f
3 changed files with 110 additions and 9 deletions

View File

@ -168,6 +168,7 @@
<Compile Include="Utils\Camera.cs" />
<Compile Include="Utils\FastBitmap.cs" />
<Compile Include="Utils\FastColour.cs" />
<Compile Include="Utils\Options.cs" />
<Compile Include="Utils\TextureRectangle.cs" />
<Compile Include="Utils\StringBuffer.cs" />
<Compile Include="Utils\Utils.cs" />

View File

@ -1,4 +1,5 @@
using System;
using System.IO;
using ClassicalSharp.Particles;
using OpenTK;
using OpenTK.Input;
@ -203,7 +204,7 @@ namespace ClassicalSharp {
public Key this[KeyMapping key] {
get { return Keys[(int)key]; }
set { Keys[(int)key] = value; }
set { Keys[(int)key] = value; SaveKeyBindings(); }
}
Key[] Keys;
@ -241,6 +242,47 @@ namespace ClassicalSharp {
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" );
}
}
}
}

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace ClassicalSharp {
public static class Options {
static Dictionary<string, string> OptionsSet = new Dictionary<string, string>();
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<string, string> pair in OptionsSet ) {
writer.Write( pair.Key );
writer.Write( '=' );
writer.Write( pair.Value );
writer.WriteLine();
}
}
}
}
}