diff --git a/Program.cs b/Program.cs index 46e5f3b8..a9d5ac43 100644 --- a/Program.cs +++ b/Program.cs @@ -3,7 +3,6 @@ // This work is licensed under the terms of the MIT license. // For a copy, see . -using CefSharp.DevTools.Runtime; using System; using System.Windows.Forms; @@ -37,10 +36,10 @@ namespace VRCX Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); + SQLite.Instance.Init(); VRCXStorage.Load(); CpuMonitor.Instance.Init(); Discord.Instance.Init(); - SQLite.Instance.Init(); WebApi.Instance.Init(); LogWatcher.Instance.Init(); @@ -52,10 +51,11 @@ namespace VRCX LogWatcher.Instance.Exit(); WebApi.Instance.Exit(); - SQLite.Instance.Exit(); + Discord.Instance.Exit(); CpuMonitor.Instance.Exit(); VRCXStorage.Save(); + SQLite.Instance.Exit(); } } } diff --git a/SQLite.cs b/SQLite.cs index 60648c42..1c5db36b 100644 --- a/SQLite.cs +++ b/SQLite.cs @@ -78,6 +78,40 @@ namespace VRCX callback.Dispose(); } + public void Execute(Action callback, string sql, IDictionary args = null) + { + m_ConnectionLock.EnterReadLock(); + try + { + using (var command = new SQLiteCommand(sql, m_Connection)) + { + if (args != null) + { + foreach (var arg in args) + { + command.Parameters.Add(new SQLiteParameter(arg.Key, arg.Value)); + } + } + using (var reader = command.ExecuteReader()) + { + while (reader.Read() == true) + { + var values = new object[reader.FieldCount]; + reader.GetValues(values); + callback(values); + } + } + } + } + catch + { + } + finally + { + m_ConnectionLock.ExitReadLock(); + } + } + public int ExecuteNonQuery(string sql, IDictionary args = null) { int result = -1; @@ -97,9 +131,6 @@ namespace VRCX result = command.ExecuteNonQuery(); } } - catch - { - } finally { m_ConnectionLock.ExitWriteLock(); diff --git a/WebApi.cs b/WebApi.cs index b036c606..a817599d 100644 --- a/WebApi.cs +++ b/WebApi.cs @@ -10,7 +10,6 @@ namespace VRCX public class WebApi { public static readonly WebApi Instance; - private readonly string _cookieFilePath; private CookieContainer _cookieContainer; static WebApi() @@ -21,31 +20,46 @@ namespace VRCX public WebApi() { - _cookieFilePath = Path.Combine(Program.BaseDirectory, "cookies.dat"); _cookieContainer = new CookieContainer(); } internal void Init() { - try - { - using (var file = File.Open(_cookieFilePath, FileMode.Open, FileAccess.Read)) + SQLite.Instance.ExecuteNonQuery("CREATE TABLE IF NOT EXISTS `cookies` (`key` TEXT PRIMARY KEY, `value` TEXT)"); + SQLite.Instance.Execute((values) => { - _cookieContainer = (CookieContainer)new BinaryFormatter().Deserialize(file); + try + { + using (var stream = new MemoryStream(Convert.FromBase64String((string)values[0]))) + { + _cookieContainer = (CookieContainer)new BinaryFormatter().Deserialize(stream); + } + } + catch + { + } + }, + "SELECT `value` FROM `cookies` WHERE `key` = @key", + new Dictionary() { + {"@key", "default"} } - } - catch - { - } + ); } internal void Exit() { try { - using (var file = File.Open(_cookieFilePath, FileMode.Create, FileAccess.Write)) + using (var memoryStream = new MemoryStream()) { - new BinaryFormatter().Serialize(file, _cookieContainer); + new BinaryFormatter().Serialize(memoryStream, _cookieContainer); + SQLite.Instance.ExecuteNonQuery( + "INSERT OR REPLACE INTO `cookies` (`key`, `value`) VALUES (@key, @value)", + new Dictionary() { + {"@key", "default"}, + {"@value", Convert.ToBase64String(memoryStream.ToArray())} + } + ); } } catch