mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Fix constant session cache reload from disk
Disk session cache was constantly reloaded (if enabled) + Add additional debug messages / update error messages See #101
This commit is contained in:
parent
1180c06b1f
commit
e4d93041fa
1 changed files with 11 additions and 15 deletions
|
|
@ -11,16 +11,13 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handle sessions caching and storage.
|
/// Handle sessions caching and storage.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
||||||
public static class SessionCache
|
public static class SessionCache
|
||||||
{
|
{
|
||||||
private const string SessionCacheFile = "SessionCache.db";
|
private const string SessionCacheFile = "SessionCache.db";
|
||||||
|
|
||||||
private static Dictionary<string, SessionToken> sessions = new Dictionary<string, SessionToken>();
|
private static Dictionary<string, SessionToken> sessions = new Dictionary<string, SessionToken>();
|
||||||
private static FileSystemWatcher cachemonitor = new FileSystemWatcher();
|
private static FileSystemWatcher cachemonitor = new FileSystemWatcher();
|
||||||
private static Timer updatetimer = new Timer(100);
|
private static Timer updatetimer = new Timer(100);
|
||||||
private static List<KeyValuePair<string, SessionToken>> pendingadds = new List<KeyValuePair<string, SessionToken>>();
|
private static List<KeyValuePair<string, SessionToken>> pendingadds = new List<KeyValuePair<string, SessionToken>>();
|
||||||
|
|
||||||
private static BinaryFormatter formatter = new BinaryFormatter();
|
private static BinaryFormatter formatter = new BinaryFormatter();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -28,7 +25,6 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="login">User login used with Minecraft.net</param>
|
/// <param name="login">User login used with Minecraft.net</param>
|
||||||
/// <returns>TRUE if session is available</returns>
|
/// <returns>TRUE if session is available</returns>
|
||||||
|
|
||||||
public static bool Contains(string login)
|
public static bool Contains(string login)
|
||||||
{
|
{
|
||||||
return sessions.ContainsKey(login);
|
return sessions.ContainsKey(login);
|
||||||
|
|
@ -39,7 +35,6 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="login">User login used with Minecraft.net</param>
|
/// <param name="login">User login used with Minecraft.net</param>
|
||||||
/// <param name="session">User session token used with Minecraft.net</param>
|
/// <param name="session">User session token used with Minecraft.net</param>
|
||||||
|
|
||||||
public static void Store(string login, SessionToken session)
|
public static void Store(string login, SessionToken session)
|
||||||
{
|
{
|
||||||
if (Contains(login))
|
if (Contains(login))
|
||||||
|
|
@ -66,7 +61,6 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="login">User login used with Minecraft.net</param>
|
/// <param name="login">User login used with Minecraft.net</param>
|
||||||
/// <returns>SessionToken for given login</returns>
|
/// <returns>SessionToken for given login</returns>
|
||||||
|
|
||||||
public static SessionToken Get(string login)
|
public static SessionToken Get(string login)
|
||||||
{
|
{
|
||||||
return sessions[login];
|
return sessions[login];
|
||||||
|
|
@ -76,7 +70,6 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
/// Initialize cache monitoring to keep cache updated with external changes.
|
/// Initialize cache monitoring to keep cache updated with external changes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>TRUE if session tokens are seeded from file</returns>
|
/// <returns>TRUE if session tokens are seeded from file</returns>
|
||||||
|
|
||||||
public static bool InitializeDiskCache()
|
public static bool InitializeDiskCache()
|
||||||
{
|
{
|
||||||
cachemonitor.Path = AppDomain.CurrentDomain.BaseDirectory;
|
cachemonitor.Path = AppDomain.CurrentDomain.BaseDirectory;
|
||||||
|
|
@ -96,7 +89,6 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender">Sender</param>
|
/// <param name="sender">Sender</param>
|
||||||
/// <param name="e">Event data</param>
|
/// <param name="e">Event data</param>
|
||||||
|
|
||||||
private static void OnChanged(object sender, FileSystemEventArgs e)
|
private static void OnChanged(object sender, FileSystemEventArgs e)
|
||||||
{
|
{
|
||||||
updatetimer.Stop();
|
updatetimer.Stop();
|
||||||
|
|
@ -108,9 +100,9 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender">Sender</param>
|
/// <param name="sender">Sender</param>
|
||||||
/// <param name="e">Event data</param>
|
/// <param name="e">Event data</param>
|
||||||
|
|
||||||
private static void HandlePending(object sender, ElapsedEventArgs e)
|
private static void HandlePending(object sender, ElapsedEventArgs e)
|
||||||
{
|
{
|
||||||
|
updatetimer.Stop();
|
||||||
LoadFromDisk();
|
LoadFromDisk();
|
||||||
|
|
||||||
foreach(KeyValuePair<string, SessionToken> pending in pendingadds.ToArray())
|
foreach(KeyValuePair<string, SessionToken> pending in pendingadds.ToArray())
|
||||||
|
|
@ -124,9 +116,11 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
/// Reads cache file and loads SessionTokens into SessionCache.
|
/// Reads cache file and loads SessionTokens into SessionCache.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>True if data is successfully loaded</returns>
|
/// <returns>True if data is successfully loaded</returns>
|
||||||
|
|
||||||
private static bool LoadFromDisk()
|
private static bool LoadFromDisk()
|
||||||
{
|
{
|
||||||
|
if (Settings.DebugMessages)
|
||||||
|
ConsoleIO.WriteLineFormatted("§8Updating session cache from disk");
|
||||||
|
|
||||||
if (File.Exists(SessionCacheFile))
|
if (File.Exists(SessionCacheFile))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
@ -139,11 +133,11 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Error reading cached sessions from disk: " + ex.Message);
|
ConsoleIO.WriteLineFormatted("§8Failed to read session cache from disk: " + ex.Message);
|
||||||
}
|
}
|
||||||
catch (SerializationException)
|
catch (SerializationException ex2)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Malformed sessions from cache file ");
|
ConsoleIO.WriteLineFormatted("§8Got malformed data while reading session cache from disk: " + ex2.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -152,9 +146,11 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Saves SessionToken's from SessionCache into cache file.
|
/// Saves SessionToken's from SessionCache into cache file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
||||||
private static void SaveToDisk()
|
private static void SaveToDisk()
|
||||||
{
|
{
|
||||||
|
if (Settings.DebugMessages)
|
||||||
|
ConsoleIO.WriteLineFormatted("§8Saving session cache to disk");
|
||||||
|
|
||||||
bool fileexists = File.Exists(SessionCacheFile);
|
bool fileexists = File.Exists(SessionCacheFile);
|
||||||
IOException lastEx = null;
|
IOException lastEx = null;
|
||||||
int attempt = 1;
|
int attempt = 1;
|
||||||
|
|
@ -187,7 +183,7 @@ namespace MinecraftClient.Protocol.SessionCache
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("Error writing cached sessions to disk" + (lastEx != null ? ": " + lastEx.Message : ""));
|
ConsoleIO.WriteLineFormatted("§8Failed to write session cache to disk" + (lastEx != null ? ": " + lastEx.Message : ""));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue