mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-18 14:23:51 +02:00
store cookies to sqlite
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
// This work is licensed under the terms of the MIT license.
|
||||
// For a copy, see <https://opensource.org/licenses/MIT>.
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
37
SQLite.cs
37
SQLite.cs
@@ -78,6 +78,40 @@ namespace VRCX
|
||||
callback.Dispose();
|
||||
}
|
||||
|
||||
public void Execute(Action<object[]> callback, string sql, IDictionary<string, object> 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<string, object> args = null)
|
||||
{
|
||||
int result = -1;
|
||||
@@ -97,9 +131,6 @@ namespace VRCX
|
||||
result = command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
m_ConnectionLock.ExitWriteLock();
|
||||
|
||||
38
WebApi.cs
38
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<string, object>() {
|
||||
{"@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<string, object>() {
|
||||
{"@key", "default"},
|
||||
{"@value", Convert.ToBase64String(memoryStream.ToArray())}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
catch
|
||||
|
||||
Reference in New Issue
Block a user