Skip to content

Commit 78a567f

Browse files
committed
add database exporter program
1 parent f39f382 commit 78a567f

File tree

9 files changed

+108
-11
lines changed

9 files changed

+108
-11
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\Definitions\Definitions.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

DatabaseExporter/Program.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using OpenLoco.Definitions.Database;
3+
using OpenLoco.Definitions.SourceData;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
var builder = new DbContextOptionsBuilder<LocoDb>();
8+
const string connectionString = "Data Source=Q:\\Games\\Locomotion\\Database\\loco.db";
9+
_ = builder.UseSqlite(connectionString);
10+
var db = new LocoDb(builder.Options);
11+
12+
var jsonOptions = new JsonSerializerOptions() { WriteIndented = true, Converters = { new JsonStringEnumConverter() }, };
13+
14+
Console.WriteLine("loading");
15+
16+
var authors = JsonSerializer.Serialize<IEnumerable<string>>(db.Authors.Select(a => a.Name).ToList().Order(), jsonOptions);
17+
var tags = JsonSerializer.Serialize<IEnumerable<string>>(db.Tags.Select(t => t.Name).ToList().Order(), jsonOptions);
18+
var licences = JsonSerializer.Serialize<IEnumerable<LicenceJsonRecord>>(db.Licences.Select(l => new LicenceJsonRecord(l.Name, l.Text)).ToList().OrderBy(l => l.Name), jsonOptions);
19+
var modpacks = JsonSerializer.Serialize<IEnumerable<ModpackJsonRecord>>(db.Modpacks.Select(m => new ModpackJsonRecord(m.Name, m.Author)).ToList().OrderBy(m => m.Name), jsonOptions);
20+
21+
var objs = new List<ObjectMetadata>();
22+
23+
foreach (var o in db.Objects
24+
.Include(l => l.Licence)
25+
.Select(x => new ExpandedTblLocoObject(x, x.Authors, x.Tags, x.Modpacks))
26+
.ToList()
27+
.OrderBy(x => x.Object.Name))
28+
{
29+
var obj = new ObjectMetadata(
30+
o.Object.OriginalName,
31+
o.Object.OriginalChecksum,
32+
o.Object.Description,
33+
o.Authors.Select(a => a.Name).ToList(),
34+
o.Tags.Select(t => t.Name).ToList(),
35+
o.Modpacks.Select(m => m.Name).ToList(),
36+
o.Object.Licence?.Name);
37+
objs.Add(obj);
38+
}
39+
40+
var objects = JsonSerializer.Serialize<IEnumerable<ObjectMetadata>>(objs, jsonOptions);
41+
42+
Console.WriteLine("writing");
43+
44+
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\authors.json", authors);
45+
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\tags.json", tags);
46+
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\licences.json", licences);
47+
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\modpacks.json", modpacks);
48+
File.WriteAllText("Q:\\Games\\Locomotion\\Database\\objects.json", objects);
49+
50+
Console.WriteLine("done");

DatabaseSeeder/Program.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
using OpenLoco.Dat.FileParsing;
55
using OpenLoco.Definitions;
66
using OpenLoco.Definitions.Database;
7+
using OpenLoco.Definitions.SourceData;
78
using System.Text.Json;
89
using System.Text.Json.Serialization;
910

10-
using var db = ExampleRun();
11+
using var db = Seed();
1112

1213
Console.WriteLine("done");
1314
Console.ReadLine();
1415

15-
static LocoDb ExampleRun()
16+
static LocoDb Seed()
1617
{
1718
var builder = new DbContextOptionsBuilder<LocoDb>();
1819
const string connectionString = "Data Source=Q:\\Games\\Locomotion\\Server\\loco-dev.db";
@@ -159,8 +160,8 @@ static void SeedDb(LocoDb db, bool deleteExisting)
159160
};
160161

161162
_ = db.Add(tblLocoObject);
162-
163163
}
164+
164165
_ = db.SaveChanges();
165166
}
166167

@@ -185,9 +186,3 @@ static void SeedDb(LocoDb db, bool deleteExisting)
185186
var r = new string(input.Chunk(2).Reverse().SelectMany(x => x).ToArray());
186187
return Convert.ToUInt32(r, 16).ToString();
187188
}
188-
189-
record LicenceJsonRecord(string Name, string Text);
190-
191-
record ModpackJsonRecord(string Name, string? Author);
192-
193-
record ObjectMetadata(string ObjectName, uint Checksum, string Description, List<string> Authors, List<string> Tags, List<string> Modpacks, string? Licence);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using OpenLoco.Definitions.Database;
2+
3+
namespace OpenLoco.Definitions.SourceData
4+
{
5+
// this must be done because eager-loading related many-to-many data in entity framework is recursive and cannot be turned off...
6+
public record ExpandedTblLocoObject(TblLocoObject Object, ICollection<TblAuthor> Authors, ICollection<TblTag> Tags, ICollection<TblModpack> Modpacks);
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace OpenLoco.Definitions.SourceData
2+
{
3+
public record LicenceJsonRecord(string Name, string Text);
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using OpenLoco.Definitions.Database;
2+
3+
namespace OpenLoco.Definitions.SourceData
4+
{
5+
public record ModpackJsonRecord(string Name, string? Author)
6+
{
7+
public ModpackJsonRecord(string Name, TblAuthor? author) : this(Name, author?.Name)
8+
{ }
9+
}
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace OpenLoco.Definitions.SourceData
2+
{
3+
public record ObjectMetadata(string ObjectName, uint Checksum, string? Description, List<string> Authors, List<string> Tags, List<string> Modpacks, string? Licence);
4+
}

ObjectService/Server.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
using OpenLoco.Definitions;
99
using OpenLoco.Definitions.Database;
1010
using OpenLoco.Definitions.DTO;
11+
using OpenLoco.Definitions.SourceData;
1112
using OpenLoco.Definitions.Web;
1213

1314
namespace OpenLoco.ObjectService
1415
{
15-
// this must be done because eager-loading related many-to-many data in entity framework is recursive and cannot be turned off...
16-
internal record ExpandedTblLocoObject(TblLocoObject Object, ICollection<TblAuthor> Authors, ICollection<TblTag> Tags, ICollection<TblModpack> Modpacks);
1716

1817
public class ObjectServiceSettings
1918
{

OpenLocoObjectEditor.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DatabaseSeeder", "DatabaseS
3939
EndProject
4040
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Definitions", "Definitions\Definitions.csproj", "{16B6E331-F893-483A-AECC-A3B4E87CE34C}"
4141
EndProject
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DatabaseExporter", "DatabaseExporter\DatabaseExporter.csproj", "{A5FD546A-471B-45AC-9037-7D02A2A17E23}"
43+
EndProject
4244
Global
4345
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4446
Debug|Any CPU = Debug|Any CPU
@@ -145,6 +147,18 @@ Global
145147
{16B6E331-F893-483A-AECC-A3B4E87CE34C}.Release|x64.Build.0 = Release|Any CPU
146148
{16B6E331-F893-483A-AECC-A3B4E87CE34C}.Release|x86.ActiveCfg = Release|Any CPU
147149
{16B6E331-F893-483A-AECC-A3B4E87CE34C}.Release|x86.Build.0 = Release|Any CPU
150+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
151+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Debug|Any CPU.Build.0 = Debug|Any CPU
152+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Debug|x64.ActiveCfg = Debug|Any CPU
153+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Debug|x64.Build.0 = Debug|Any CPU
154+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Debug|x86.ActiveCfg = Debug|Any CPU
155+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Debug|x86.Build.0 = Debug|Any CPU
156+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Release|Any CPU.ActiveCfg = Release|Any CPU
157+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Release|Any CPU.Build.0 = Release|Any CPU
158+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Release|x64.ActiveCfg = Release|Any CPU
159+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Release|x64.Build.0 = Release|Any CPU
160+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Release|x86.ActiveCfg = Release|Any CPU
161+
{A5FD546A-471B-45AC-9037-7D02A2A17E23}.Release|x86.Build.0 = Release|Any CPU
148162
EndGlobalSection
149163
GlobalSection(SolutionProperties) = preSolution
150164
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)