-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
224 lines (179 loc) · 6.67 KB
/
Program.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
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
using Dat;
using Microsoft.EntityFrameworkCore;
using OpenLoco.Common;
using OpenLoco.Dat.FileParsing;
using OpenLoco.Definitions;
using OpenLoco.Definitions.Database;
using OpenLoco.Definitions.SourceData;
using System.Text.Json;
using System.Text.Json.Serialization;
using var db = Seed();
Console.WriteLine("done");
Console.ReadLine();
static LocoDb Seed()
{
var builder = new DbContextOptionsBuilder<LocoDb>();
const string connectionString = "Data Source=Q:\\Games\\Locomotion\\Server\\loco-dev.db";
_ = builder.UseSqlite(connectionString);
var db = new LocoDb(builder.Options);
// Note: The database must exist before this script works
Console.WriteLine($"Database path: {connectionString}");
const bool seed = true;
const bool DeleteExisting = true;
if (seed)
{
SeedDb(db, DeleteExisting);
}
return db;
}
static void SeedDb(LocoDb db, bool deleteExisting)
{
if (deleteExisting)
{
Console.WriteLine("Clearing database");
_ = db.Users.ExecuteDelete();
_ = db.Roles.ExecuteDelete();
_ = db.Objects.ExecuteDelete();
_ = db.Tags.ExecuteDelete();
_ = db.Modpacks.ExecuteDelete();
_ = db.Authors.ExecuteDelete();
_ = db.Licences.ExecuteDelete();
_ = db.SaveChanges(); // not necessary since ExecuteDelete auto-saves
}
const string ObjDirectory = "Q:\\Games\\Locomotion\\Server\\Objects";
var allDatFiles = SawyerStreamUtils.GetDatFilesInDirectory(ObjDirectory);
Console.WriteLine("Seeding");
var jsonOptions = new JsonSerializerOptions() { WriteIndented = true, Converters = { new JsonStringEnumConverter() }, };
// ...
if (!db.Roles.Any())
{
Console.WriteLine("Seeding Roles");
var roles = JsonSerializer.Deserialize<IEnumerable<string>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\roles.json"), jsonOptions);
if (roles != null)
{
db.AddRange(roles.Select(x => new TblRole() { Name = x }));
_ = db.SaveChanges();
}
}
// ...
Console.WriteLine("Seeding Users");
var users = JsonSerializer.Deserialize<IEnumerable<UserJsonRecord>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\users.json"), jsonOptions);
if (users != null)
{
db.AddRange(users.Select(x => new TblUser()
{
Name = x.Name,
DisplayName = x.DisplayName,
Author = db.Authors.SingleOrDefault(a => a.Name == x.Author),
Roles = [.. db.Roles.Where(r => x.Roles.Contains(r.Name))],
PasswordHashed = x.PasswordHashed,
PasswordSalt = x.PasswordSalt
}));
_ = db.SaveChanges();
}
// ...
if (!db.Authors.Any())
{
Console.WriteLine("Seeding Authors");
var authors = JsonSerializer.Deserialize<IEnumerable<string>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\authors.json"), jsonOptions);
if (authors != null)
{
db.AddRange(authors.Select(x => new TblUser() { Name = x }));
_ = db.SaveChanges();
}
}
// ...
if (!db.Tags.Any())
{
Console.WriteLine("Seeding Tags");
var tags = JsonSerializer.Deserialize<IEnumerable<string>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\tags.json"), jsonOptions);
if (tags != null)
{
db.AddRange(tags.Select(x => new TblTag() { Name = x }));
_ = db.SaveChanges();
}
}
// ...
if (!db.Licences.Any())
{
Console.WriteLine("Seeding Licences");
var licences = JsonSerializer.Deserialize<IEnumerable<LicenceJsonRecord>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\licences.json"), jsonOptions);
if (licences != null)
{
db.AddRange(licences.Select(x => new TblLicence() { Name = x.Name, Text = x.Text }));
_ = db.SaveChanges();
}
}
// ...
if (!db.Modpacks.Any())
{
Console.WriteLine("Seeding Modpacks");
var modpacks = JsonSerializer.Deserialize<IEnumerable<ModpackJsonRecord>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\modpacks.json"), jsonOptions);
if (modpacks != null)
{
db.AddRange(modpacks.Select(x => new TblModpack() { Name = x.Name, Author = x.Author == null ? null : db.Authors.Single(a => a.Name == x.Author) }));
_ = db.SaveChanges();
}
}
// ...
if (!db.Objects.Any())
{
var fileArr = allDatFiles.ToArray();
Console.WriteLine($"Seeding {fileArr.Length} Objects");
var progress = new Progress<float>();
var index = ObjectIndex.LoadOrCreateIndex(ObjDirectory);
var objectMetadata = JsonSerializer.Deserialize<IEnumerable<ObjectMetadata>>(File.ReadAllText("Q:\\Games\\Locomotion\\Server\\objectMetadata.json"), jsonOptions);
var objectMetadataDict = objectMetadata!.ToDictionary(x => (x.ObjectName, x.Checksum), x => x);
var gameReleaseDate = new DateTimeOffset(2004, 09, 07, 0, 0, 0, TimeSpan.Zero);
foreach (var objIndex in index.Objects.DistinctBy(x => (x.ObjectName, x.Checksum)))
{
var metadataKey = (objIndex.ObjectName, objIndex.Checksum);
if (!objectMetadataDict.TryGetValue(metadataKey, out var meta))
{ }
var fullPath = Path.Combine(ObjDirectory, objIndex.Filename);
var creationTime = objIndex.IsVanilla ? gameReleaseDate : File.GetLastWriteTimeUtc(fullPath); // this is the "Modified" time as shown in Windows
var authors = meta?.Authors == null ? null : db.Authors.Where(x => meta.Authors.Contains(x.Name)).ToList();
var tags = meta?.Tags == null ? null : db.Tags.Where(x => meta.Tags.Contains(x.Name)).ToList();
var modpacks = meta?.Modpacks == null ? null : db.Modpacks.Where(x => meta.Modpacks.Contains(x.Name)).ToList();
var licence = meta?.Licence == null ? null : db.Licences.Where(x => x.Name == meta.Licence).First();
var tblLocoObject = new TblLocoObject()
{
Name = $"{objIndex.ObjectName}_{objIndex.Checksum}", // same as server upload name
PathOnDisk = objIndex.Filename.Replace('\\', '/'), // make the path linux-compatible
OriginalName = objIndex.ObjectName,
OriginalChecksum = objIndex.Checksum,
IsVanilla = objIndex.IsVanilla,
ObjectType = objIndex.ObjectType,
VehicleType = objIndex.VehicleType,
Description = meta?.Description,
Authors = authors ?? [],
CreationDate = creationTime,
LastEditDate = null,
UploadDate = DateTimeOffset.Now,
Tags = tags ?? [],
Modpacks = modpacks ?? [],
Availability = ObjectAvailability.NewGames,
Licence = licence,
};
_ = db.Add(tblLocoObject);
}
_ = db.SaveChanges();
}
Console.WriteLine("Finished seeding");
}
static Dictionary<(string ObjectName, string Checksum), GlenDbData2> LoadMetadata(string metadataFile)
{
if (!File.Exists(metadataFile))
{
return [];
}
var text = File.ReadAllText(metadataFile);
var metadata = JsonSerializer.Deserialize<GlenDBSchema2>(text).data;
var kvList = metadata.GroupBy(x => (x.ObjectName, uint32_t_LittleToBigEndian(x.Checksum)));
return kvList.ToDictionary(x => x.Key, x => x.First());
}
static string? uint32_t_LittleToBigEndian(string input)
{
var r = new string(input.Chunk(2).Reverse().SelectMany(x => x).ToArray());
return Convert.ToUInt32(r, 16).ToString();
}