-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.cs
More file actions
241 lines (214 loc) · 8.28 KB
/
DataBase.cs
File metadata and controls
241 lines (214 loc) · 8.28 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using Microsoft.Data.Sqlite;
using System.IO;
using Il2CppScheduleOne.Persistence;
using Il2CppScheduleOne.GameTime;
namespace PaxDrops
{
/// <summary>
/// Manages persistent storage of scheduled drops using a single Sqlite database.
/// IL2CPP port using System.Data.Sqlite for CrossOver compatibility.
/// </summary>
public static class DataBase
{
private const string DbDir = "Mods/PaxDrops/Data";
private const string DbPath = "Mods/PaxDrops/Data/drops.db";
private static SqliteConnection? _conn;
public class DropRecord
{
public int Day;
public List<string> Items;
public int DropHour;
public string DropTime;
public string Org;
public string CreatedTime;
public string Type;
public string Location;
public DropRecord()
{
Items = new List<string>();
}
}
public static readonly Dictionary<int, DropRecord> PendingDrops = new Dictionary<int, DropRecord>();
public static void Init()
{
try
{
Directory.CreateDirectory(DbDir);
bool fresh = !File.Exists(DbPath);
_conn = new SqliteConnection($"Data Source={DbPath}");
_conn.Open();
Logger.Msg($"[DataBase] 🔗 Database connection opened. Fresh: {fresh}");
if (fresh) CreateSchema();
LoadPendingDrops();
Logger.Msg("[DataBase] ✅ Initialized and loaded.");
}
catch (Exception ex)
{
Logger.Error("[DataBase] ❌ Failed to initialize database.");
Logger.Exception(ex);
}
}
private static void CreateSchema()
{
try
{
string sql = @"
CREATE TABLE IF NOT EXISTS drops (
id INTEGER PRIMARY KEY AUTOINCREMENT,
org TEXT,
createdTime TEXT,
dropTime TEXT,
hour INTEGER,
items TEXT,
meta TEXT
);";
using (var cmd = _conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
Logger.Msg("[DataBase] 📁 Schema created.");
}
catch (Exception ex)
{
Logger.Error("[DataBase] ❌ Failed to create schema.");
Logger.Exception(ex);
}
}
public static void SaveDrop(int day, List<string> items, int hour, string meta = "manual")
{
try
{
if (_conn == null)
{
Logger.Error("[DataBase] ❌ Database connection is null. Cannot save drop.");
return;
}
if (items == null)
{
Logger.Error("[DataBase] ❌ Items list is null. Cannot save drop.");
return;
}
string now = DateTime.UtcNow.ToString("s");
string dropTime = TimeSpan.FromMinutes(hour).ToString(@"hh\:mm");
// Get organization name from the load manager
string org = "Unknown";
try
{
var loadManager = Il2CppScheduleOne.Persistence.LoadManager.Instance;
if (loadManager?.ActiveSaveInfo != null)
{
org = loadManager.ActiveSaveInfo.OrganisationName ?? "Unknown";
}
}
catch (Exception ex)
{
Logger.Warn($"[DataBase] ⚠️ Could not get organization name: {ex.Message}");
}
string itemList = string.Join(",", items);
string sql = "INSERT INTO drops (org, createdTime, dropTime, hour, items, meta) VALUES (@org, @created, @drop, @hour, @items, @meta);";
using (var cmd = _conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@org", org);
cmd.Parameters.AddWithValue("@created", now);
cmd.Parameters.AddWithValue("@drop", dropTime);
cmd.Parameters.AddWithValue("@hour", hour);
cmd.Parameters.AddWithValue("@items", itemList);
cmd.Parameters.AddWithValue("@meta", meta);
cmd.ExecuteNonQuery();
}
Logger.Msg($"[DataBase] 💾 Drop saved for day {day} @ {hour} with {items.Count} items ({meta})");
var record = new DropRecord
{
Day = day,
Items = items,
DropHour = hour,
DropTime = dropTime,
Org = org,
CreatedTime = now,
Type = meta,
Location = ""
};
PendingDrops[day] = record;
}
catch (Exception ex)
{
Logger.Error("[DataBase] ❌ Failed to save drop.");
Logger.Exception(ex);
}
}
private static void LoadPendingDrops()
{
try
{
// Get current day for context
int today = 1;
try
{
var timeManager = Il2CppScheduleOne.GameTime.TimeManager.Instance;
if (timeManager != null)
{
today = timeManager.ElapsedDays;
}
}
catch (Exception ex)
{
Logger.Warn($"[DataBase] ⚠️ Could not get current day: {ex.Message}");
}
string sql = "SELECT * FROM drops;";
using (var cmd = _conn.CreateCommand())
{
cmd.CommandText = sql;
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int day = today; // fallback
int.TryParse(reader["hour"]?.ToString(), out int hour);
string dropTime = reader["dropTime"]?.ToString();
string itemsRaw = reader["items"]?.ToString();
string org = reader["org"]?.ToString();
string created = reader["createdTime"]?.ToString();
string meta = reader["meta"]?.ToString();
if (string.IsNullOrWhiteSpace(itemsRaw)) continue;
var record = new DropRecord
{
Day = today,
DropHour = hour,
DropTime = dropTime,
Items = new List<string>(itemsRaw.Split(',')),
Org = org,
CreatedTime = created,
Type = meta,
Location = ""
};
PendingDrops[day] = record;
}
}
}
Logger.Msg($"[DataBase] 📦 Loaded {PendingDrops.Count} pending drops.");
}
catch (Exception ex)
{
Logger.Error("[DataBase] ❌ Failed to load pending drops.");
Logger.Exception(ex);
}
}
public static void Shutdown()
{
try
{
_conn?.Close();
Logger.Msg("[DataBase] 🔒 DB connection closed.");
}
catch (Exception ex)
{
Logger.Error("[DataBase] ❌ Error during shutdown.");
Logger.Exception(ex);
}
}
}
}