Avoid dictionary usage.

This commit is contained in:
UnknownShadow200 2016-09-11 11:14:54 +10:00
parent a8f7e2b0a7
commit 8be566b75d
3 changed files with 52 additions and 56 deletions

View file

@ -329,10 +329,9 @@ namespace ClassicalSharp {
Graphics.DeleteTexture( ref CloudsTex );
Graphics.Dispose();
if( Options.HasChanged ) {
Options.Load();
Options.Save();
}
if( Options.OptionsChanged.Count == 0 ) return;
Options.Load();
Options.Save();
}
internal bool CanPick( byte block ) {

View file

@ -78,50 +78,49 @@ namespace ClassicalSharp.Hotkeys {
return false;
}
const string prefix = "hotkey-";
public void LoadSavedHotkeys() {
foreach( var pair in Options.OptionsSet ) {
if( Utils.CaselessStarts( pair.Key, "hotkey-" ) ) {
// First retrieve the parts from
const int startIndex = 7;
int keySplit = pair.Key.IndexOf( '&', startIndex );
if( keySplit < 0 ) {
Utils.LogDebug( "Hotkey {0} has an invalid key", pair.Key );
continue;
}
string strKey = pair.Key.Substring( startIndex, keySplit - startIndex );
string strFlags = pair.Key.Substring( keySplit + 1, pair.Key.Length - keySplit - 1 );
int valueSplit = pair.Value.IndexOf( '&', 0 );
if( valueSplit < 0 ) {
Utils.LogDebug( "Hotkey {0} has an invalid value", pair.Key );
continue;
}
string strMoreInput = pair.Value.Substring( 0, valueSplit - 0 );
string strText = pair.Value.Substring( valueSplit + 1, pair.Value.Length - valueSplit - 1 );
// Then try to parse the key and value
Key key; byte flags; bool moreInput;
if( !Utils.TryParseEnum( strKey, Key.Unknown, out key ) ||
!Byte.TryParse( strFlags, out flags ) ||
!Boolean.TryParse( strMoreInput, out moreInput ) ||
strText.Length == 0 ) {
Utils.LogDebug( "Hotkey {0} has invalid arguments", pair.Key );
continue;
}
AddHotkey( key, flags, strText, moreInput );
if( !Utils.CaselessStarts( pair.Key, prefix ) ) continue;
int keySplit = pair.Key.IndexOf( '&', prefix.Length );
if( keySplit < 0 ) {
Utils.LogDebug( "Hotkey {0} has an invalid key", pair.Key );
continue;
}
string strKey = pair.Key.Substring( prefix.Length, keySplit - prefix.Length );
string strFlags = pair.Key.Substring( keySplit + 1, pair.Key.Length - keySplit - 1 );
int valueSplit = pair.Value.IndexOf( '&', 0 );
if( valueSplit < 0 ) {
Utils.LogDebug( "Hotkey {0} has an invalid value", pair.Key );
continue;
}
string strMoreInput = pair.Value.Substring( 0, valueSplit - 0 );
string strText = pair.Value.Substring( valueSplit + 1, pair.Value.Length - valueSplit - 1 );
// Then try to parse the key and value
Key key; byte flags; bool moreInput;
if( !Utils.TryParseEnum( strKey, Key.Unknown, out key ) ||
!Byte.TryParse( strFlags, out flags ) ||
!Boolean.TryParse( strMoreInput, out moreInput ) ||
strText.Length == 0 ) {
Utils.LogDebug( "Hotkey {0} has invalid arguments", pair.Key );
continue;
}
AddHotkey( key, flags, strText, moreInput );
}
}
public void UserRemovedHotkey( Key baseKey, byte flags ) {
string key = "hotkey-" + baseKey + "&" + flags;
Options.Set( key, null );
Options.Set<string>( key, null );
}
public void UserAddedHotkey( Key baseKey, byte flags, bool moreInput, string text ) {
string key = "hotkey-" + baseKey + "&" + flags;
string value = moreInput + "&" + text;
Options.Set( key, value );
Options.Set<string>( key, value );
}
}

View file

@ -64,10 +64,8 @@ namespace ClassicalSharp {
public static class Options {
public static Dictionary<string, string> OptionsSet = new Dictionary<string, string>();
public static Dictionary<string, bool> OptionsChanged = new Dictionary<string, bool>();
public static bool HasChanged { get { return OptionsChanged.Count > 0; } }
const string OptionsFile = "options.txt";
public static List<string> OptionsChanged = new List<string>();
const string Filename = "options.txt";
static bool TryGetValue( string key, out string value ) {
if( OptionsSet.TryGetValue( key, out value ) ) return true;
@ -122,19 +120,16 @@ namespace ClassicalSharp {
return mapping;
}
public static void Set( string key, string value ) {
key = key.ToLower();
if( value != null )
OptionsSet[key] = value;
else
OptionsSet.Remove( key );
OptionsChanged[key] = true;
}
public static void Set<T>( string key, T value ) {
key = key.ToLower();
OptionsSet[key] = value.ToString();
OptionsChanged[key] = true;
if( value != null ) {
OptionsSet[key] = value.ToString();
} else {
OptionsSet.Remove( key );
}
if( !OptionsChanged.Contains( key ) )
OptionsChanged.Add( key );
}
public static bool Load() {
@ -147,7 +142,7 @@ namespace ClassicalSharp {
Program.CleanupMainDirectory();
try {
string path = Path.Combine( Program.AppDirectory, OptionsFile );
string path = Path.Combine( Program.AppDirectory, Filename );
using( Stream fs = File.OpenRead( path ) )
using( StreamReader reader = new StreamReader( fs, false ) )
LoadFrom( reader );
@ -165,7 +160,7 @@ namespace ClassicalSharp {
// remove all the unchanged options
List<string> toRemove = new List<string>();
foreach( KeyValuePair<string, string> kvp in OptionsSet ) {
if( !OptionsChanged.ContainsKey( kvp.Key ) )
if( !OptionsChanged.Contains( kvp.Key ) )
toRemove.Add( kvp.Key );
}
for( int i = 0; i < toRemove.Count; i++ )
@ -181,17 +176,20 @@ namespace ClassicalSharp {
sepIndex++;
if( sepIndex == line.Length ) continue;
string value = line.Substring( sepIndex, line.Length - sepIndex );
if( !OptionsChanged.ContainsKey( key ) )
if( !OptionsChanged.Contains( key ) )
OptionsSet[key] = value;
}
}
public static bool Save() {
try {
string path = Path.Combine( Program.AppDirectory, OptionsFile );
string path = Path.Combine( Program.AppDirectory, Filename );
using( Stream fs = File.Create( path ) )
using( StreamWriter writer = new StreamWriter( fs ) )
SaveTo( writer );
using( StreamWriter writer = new StreamWriter( fs ) )
{
SaveTo( writer );
}
OptionsChanged.Clear();
return true;
} catch( IOException ex ) {