-
Notifications
You must be signed in to change notification settings - Fork 8
/
Preferences.cs
146 lines (116 loc) · 4.76 KB
/
Preferences.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Squared.Task.Data;
using Squared.Util.Event;
using Squared.Task;
using System.Web.Script.Serialization;
using System.Data.Common;
namespace ShootBlues {
public class PreferenceStore : IDisposable {
public readonly ConnectionWrapper Database;
public readonly EventBus EventBus;
public readonly IManagedScript Script;
protected readonly HashSet<string> DirtyPrefs = new HashSet<string>();
protected readonly Signal Dirty = new Signal();
protected bool _Initialized = false;
protected IFuture _DirtyTask = null;
public PreferenceStore (IManagedScript script, ConnectionWrapper database, EventBus eventBus) {
Script = script;
Database = database;
EventBus = eventBus;
_DirtyTask = Database.Scheduler.Start(
DirtyTask(), TaskExecutionPolicy.RunAsBackgroundTask
);
}
public void Dispose () {
if (_DirtyTask != null)
_DirtyTask.Dispose();
}
protected IEnumerator<object> DirtyTask () {
while (true) {
var prefNames = DirtyPrefs.ToArray();
DirtyPrefs.Clear();
EventBus.Broadcast(this, "Changed", prefNames);
yield return Dirty.Wait();
}
}
public void Flush () {
Dirty.Set();
}
public IEnumerator<object> Set<T> (string prefName, T value) {
if (!_Initialized)
yield return Initialize();
using (var query = Database.BuildQuery(
"replace into prefs (scriptName, prefName, value) values (?, ?, ?)"
)) {
// System.Data.SQLite sucks :|
var param = query.Command.Parameters[2] as DbParameter;
param.DbType = System.Data.DbType.Object;
yield return query.ExecuteNonQuery(Script.Name, prefName, value);
}
DirtyPrefs.Add(prefName);
Flush();
}
public IEnumerator<object> SetMultiple (Dictionary<string, object> toUpdate) {
if (!_Initialized)
yield return Initialize();
using (var xact = Database.CreateTransaction()) {
yield return xact;
using (var query = Database.BuildQuery(
"replace into prefs (scriptName, prefName, value) values (?, ?, ?)"
)) {
// System.Data.SQLite sucks :|
var param = query.Command.Parameters[2] as DbParameter;
param.DbType = System.Data.DbType.Object;
foreach (var kvp in toUpdate) {
yield return query.ExecuteNonQuery(Script.Name, kvp.Key, kvp.Value);
DirtyPrefs.Add(kvp.Key);
}
}
yield return xact.Commit();
}
Flush();
}
public IEnumerator<object> Get<T> (string prefName) {
if (!_Initialized)
yield return Initialize();
using (var query = Database.BuildQuery(
"select value from prefs where scriptName = ? and prefName = ?"
)) {
var fResult = query.ExecuteScalar<T>(Script.Name, prefName);
yield return fResult;
yield return new Result(fResult.Result);
}
}
public IEnumerator<object> GetAll () {
if (!_Initialized)
yield return Initialize();
var dict = new Dictionary<string, object>();
using (var query = Database.BuildQuery(
"select prefName, value from prefs where scriptName = ?"
))
using (var e = query.Execute<PrefEntry>(Script.Name))
while (!e.Disposed) {
yield return e.Fetch();
foreach (var item in e)
dict.Add(item.Key, item.Value);
}
yield return new Result(dict);
}
public IEnumerator<object> GetAllJson () {
var rtc = new RunToCompletion<Dictionary<string, object>>(GetAll());
yield return rtc;
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(rtc.Result);
yield return new Result(json);
}
public virtual IEnumerator<object> Initialize () {
yield return Program.CreateDBTable(
"prefs", @"( scriptName TEXT NOT NULL, prefName TEXT NOT NULL, value VARIANT, PRIMARY KEY ( scriptName, prefName ) )"
);
_Initialized = true;
}
}
}