Skip to content

Commit 8c27976

Browse files
SQL v0
1 parent 979f05b commit 8c27976

File tree

10 files changed

+319
-0
lines changed

10 files changed

+319
-0
lines changed

Diff for: AssetEditor.sln

+17
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TwUiEditor", "TwUiEditor",
8484
EndProject
8585
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Editors.Twui", "Editors\TwuiEditor\Editor.Twui\Editors.Twui.csproj", "{FB35CFEB-811F-41E6-BBAD-0312954324BF}"
8686
EndProject
87+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DatabaseEditor", "DatabaseEditor", "{5A4DFCA4-6884-49D2-BA24-A9C8676A0C77}"
88+
EndProject
89+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Editors.DatabaseEditor", "Editors\DatabaseEditor\Editors.DatabaseEditor\Editors.DatabaseEditor.csproj", "{BEE61E8C-B25D-4982-9C42-6131AED41B48}"
90+
EndProject
91+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utility.DatabaseSchemaGenerator", "Editors\DatabaseEditor\Utility.DatabaseSchemaGenerator\Utility.DatabaseSchemaGenerator.csproj", "{0298ED36-AE77-4CC3-9A46-40D700A3C225}"
92+
EndProject
8793
Global
8894
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8995
Debug|Any CPU = Debug|Any CPU
@@ -202,6 +208,14 @@ Global
202208
{FB35CFEB-811F-41E6-BBAD-0312954324BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
203209
{FB35CFEB-811F-41E6-BBAD-0312954324BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
204210
{FB35CFEB-811F-41E6-BBAD-0312954324BF}.Release|Any CPU.Build.0 = Release|Any CPU
211+
{BEE61E8C-B25D-4982-9C42-6131AED41B48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
212+
{BEE61E8C-B25D-4982-9C42-6131AED41B48}.Debug|Any CPU.Build.0 = Debug|Any CPU
213+
{BEE61E8C-B25D-4982-9C42-6131AED41B48}.Release|Any CPU.ActiveCfg = Release|Any CPU
214+
{BEE61E8C-B25D-4982-9C42-6131AED41B48}.Release|Any CPU.Build.0 = Release|Any CPU
215+
{0298ED36-AE77-4CC3-9A46-40D700A3C225}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
216+
{0298ED36-AE77-4CC3-9A46-40D700A3C225}.Debug|Any CPU.Build.0 = Debug|Any CPU
217+
{0298ED36-AE77-4CC3-9A46-40D700A3C225}.Release|Any CPU.ActiveCfg = Release|Any CPU
218+
{0298ED36-AE77-4CC3-9A46-40D700A3C225}.Release|Any CPU.Build.0 = Release|Any CPU
205219
EndGlobalSection
206220
GlobalSection(SolutionProperties) = preSolution
207221
HideSolutionNode = FALSE
@@ -240,6 +254,9 @@ Global
240254
{AE3B8059-D6E0-4AFD-BB00-9E28CE0933A1} = {1A9F8CC6-CFE9-4559-A969-20FB9CC2A07F}
241255
{9A22B0C7-FB6E-419E-A87D-4E86F4A2952C} = {07AC615B-A8FC-4E1A-BDD5-BC11452429A0}
242256
{FB35CFEB-811F-41E6-BBAD-0312954324BF} = {9A22B0C7-FB6E-419E-A87D-4E86F4A2952C}
257+
{5A4DFCA4-6884-49D2-BA24-A9C8676A0C77} = {07AC615B-A8FC-4E1A-BDD5-BC11452429A0}
258+
{BEE61E8C-B25D-4982-9C42-6131AED41B48} = {5A4DFCA4-6884-49D2-BA24-A9C8676A0C77}
259+
{0298ED36-AE77-4CC3-9A46-40D700A3C225} = {5A4DFCA4-6884-49D2-BA24-A9C8676A0C77}
243260
EndGlobalSection
244261
GlobalSection(ExtensibilityGlobals) = postSolution
245262
SolutionGuid = {AB5968F3-98ED-4AFF-98EA-0DBEDCACADF2}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0-windows</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\..\Shared\SharedCore\Shared.Core.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<Folder Include="GeneratedDatabase\" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Nodes;
3+
using Shared.Core.Settings;
4+
5+
namespace Editors.DatabaseEditor.FileFormats
6+
{
7+
public static class DbScehmaParser
8+
{
9+
public static DbSchema CreateFromRpfmJson(string systemFilePath, GameTypeEnum game)
10+
{
11+
var dbSchema = new DbSchema() { Game = game };
12+
13+
var jsonContent = File.ReadAllText(systemFilePath);
14+
var jsonNode = JsonSerializer.Deserialize<JsonNode>(jsonContent);
15+
16+
var definitions = jsonNode["definitions"] as JsonObject;
17+
foreach (var definition in definitions)
18+
{
19+
var tableName = definition.Key;
20+
21+
var tableVersions = definition.Value as JsonArray;
22+
foreach (var tableVersion in tableVersions)
23+
{
24+
var schemaVersion = tableVersion["version"].GetValue<int>();
25+
var schemaFields = tableVersion["fields"] as JsonArray;
26+
27+
var dbTableSchema = new DBTableSchema()
28+
{
29+
Name = tableName,
30+
Version = schemaVersion
31+
};
32+
33+
foreach (var schemaField in schemaFields)
34+
{
35+
var name = schemaField["name"].GetValue<string>();
36+
var field_type = schemaField["field_type"].GetValue<string>();
37+
var isKey = schemaField["is_key"].GetValue<bool>();
38+
39+
var dbColoumnSchema = new DbColoumnSchema()
40+
{
41+
DataType = field_type,
42+
IsKey = isKey,
43+
Name = name,
44+
Description = ""
45+
};
46+
47+
dbTableSchema.Coloumns.Add(dbColoumnSchema);
48+
}
49+
50+
dbSchema.TableSchemas.Add(dbTableSchema);
51+
}
52+
}
53+
54+
return dbSchema;
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Diagnostics;
2+
using Shared.Core.Settings;
3+
4+
namespace Editors.DatabaseEditor.FileFormats
5+
{
6+
public class DbSchema
7+
{
8+
public required GameTypeEnum Game { get; set; }
9+
public List<DBTableSchema> TableSchemas { get; set; } = [];
10+
}
11+
12+
[DebuggerDisplay("{Name} : {Version}")]
13+
public class DBTableSchema
14+
{
15+
public required int Version { get; set; }
16+
public required string Name { get; set; }
17+
public List<DbColoumnSchema> Coloumns { get; set; } = [];
18+
}
19+
20+
21+
[DebuggerDisplay("{Name} : {DataType}")]
22+
public class DbColoumnSchema
23+
{
24+
public required string Name { get; set; }
25+
public required string DataType { get; set; }
26+
public required bool IsKey { get; set; }
27+
public string Description { get; set; }
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Shared.Core.PackFiles;
3+
using Shared.Core.PackFiles.Models;
4+
5+
namespace Utility.DatabaseSchemaGenerator.Examples
6+
{
7+
8+
9+
10+
public class ExampleDb : DbContext
11+
{
12+
13+
public static string GetDbPath()
14+
{
15+
var folder = Environment.SpecialFolder.LocalApplicationData;
16+
var path = Environment.GetFolderPath(folder);
17+
return System.IO.Path.Join(path, "blogging.db"); //C:\Users\ole_k\AppData\Local
18+
}
19+
20+
IPackFileService _packFileService;
21+
22+
public DbSet<battle_skeleton_parts> battle_skeleton_parts_table { get; set; }
23+
24+
public ExampleDb(DbContextOptions<ExampleDb> options) : base(options)
25+
{
26+
27+
}
28+
29+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
30+
{
31+
var path = GetDbPath();
32+
optionsBuilder.UseSqlite($"Data Source={path}");
33+
}
34+
35+
public void Deserialize(IPackFileService packFileService)
36+
{
37+
Load(battle_skeleton_parts_table, battle_skeleton_parts.TableName, battle_skeleton_parts.Deserialize);
38+
}
39+
40+
void Load<T>(DbSet<T> values, string tableName, Func<PackFile, List<T>> loader) where T : class
41+
{
42+
var file = _packFileService.FindFile($"Tables\\db\\{tableName}\\table_"); // Get all in folder
43+
if (file != null)
44+
{
45+
var fileContent = loader(file);
46+
values.AddRange(fileContent);
47+
}
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Shared.Core.ByteParsing;
3+
using Shared.Core.PackFiles.Models;
4+
5+
namespace Utility.DatabaseSchemaGenerator.Examples
6+
{
7+
public class battle_skeleton_parts // Remove _tables
8+
{
9+
// --- Auto generate
10+
[Key] public string variant_name { get; set; }
11+
public string skeleton { get; set; }
12+
public string root_joint { get; set; }
13+
// ---
14+
15+
public static string TableName => "battle_skeleton_parts_table"; //
16+
public static int TableVersion => 0; //
17+
18+
static battle_skeleton_parts Create(ByteChunk byteChunk)
19+
{
20+
var instance = new battle_skeleton_parts();
21+
instance.variant_name = byteChunk.ReadString();
22+
instance.skeleton = byteChunk.ReadString();
23+
instance.root_joint = byteChunk.ReadOptionalString();
24+
return instance;
25+
}
26+
27+
public static List<battle_skeleton_parts> Deserialize(PackFile dbFile)
28+
{
29+
var ouput = new List<battle_skeleton_parts>();
30+
var byteChunk = dbFile.DataSource.ReadDataAsChunk();
31+
var numItems = byteChunk.ReadUInt32();
32+
var version = byteChunk.ReadUInt32();
33+
34+
if (version != TableVersion)
35+
throw new Exception($"Failed to deserialize {TableName}. Version mismatch. Expected {TableVersion}, Actual {version}");
36+
37+
for (var i = 0; i < numItems; i++)
38+
{
39+
var instance = Create(byteChunk);
40+
ouput.Add(instance);
41+
}
42+
43+
var bytesLeft = byteChunk.BytesLeft;
44+
if (bytesLeft != 0)
45+
throw new Exception($"Failed to deserialize {TableName}. {bytesLeft} bytes left after deserialization");
46+
47+
return ouput;
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Editors.DatabaseEditor.FileFormats;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.Configuration;
4+
using Shared.Core.PackFiles;
5+
using Shared.Core.Settings;
6+
using Utility.DatabaseSchemaGenerator.Examples;
7+
8+
namespace Utility.DatabaseSchemaGenerator
9+
{
10+
internal class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
//GenerateSchema();
15+
CreateExampleDb();
16+
}
17+
18+
static void GenerateSchema()
19+
{
20+
var dbSchema = DbScehmaParser.CreateFromRpfmJson(@"C:\Users\ole_k\Downloads\schema_wh3.json", GameTypeEnum.Warhammer3);
21+
22+
// Delete old files
23+
// Get table
24+
// Generate
25+
}
26+
27+
public static ExampleDb CreateDbContext()
28+
{
29+
var configurationBuilder = new ConfigurationBuilder()
30+
.SetBasePath(Directory.GetCurrentDirectory())
31+
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
32+
33+
IConfigurationRoot configuration = configurationBuilder.Build();
34+
string connectionString = ExampleDb.GetDbPath();
35+
36+
var optionsBuilder = new DbContextOptionsBuilder<ExampleDb>()
37+
.UseSqlite(connectionString);
38+
39+
return new ExampleDb(optionsBuilder.Options);
40+
}
41+
42+
static void CreateExampleDb()
43+
{
44+
var settings = new ApplicationSettingsService(GameTypeEnum.Warhammer3);
45+
var gameInfo = new GameInformationFactory();
46+
47+
var containerLoader = new PackFileContainerLoader(settings, gameInfo);
48+
var gameFiles = containerLoader.LoadAllCaFiles(GameTypeEnum.Warhammer3);
49+
50+
var pfs = new PackFileService(null);
51+
pfs.AddContainer(gameFiles);
52+
53+
using (ExampleDb sc = CreateDbContext())
54+
{
55+
56+
//https://marketplace.visualstudio.com/items?itemName=ErikEJ.SQLServerCompactSQLiteToolbox&ssr=false#overview
57+
// dotnet tool install --global dotnet-ef
58+
//dotnet add package Microsoft.EntityFrameworkCore.Design*/
59+
60+
//var m = sc.Database.GetAppliedMigrations();
61+
sc.Database.EnsureCreated();
62+
63+
sc.Deserialize(pfs);
64+
sc.SaveChanges();
65+
}
66+
}
67+
}
68+
}

Diff for: Editors/DatabaseEditor/Utility.DatabaseSchemaGenerator/Templates/TableTamplate.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0-windows</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\..\..\Shared\SharedCore\Shared.Core.csproj" />
21+
<ProjectReference Include="..\Editors.DatabaseEditor\Editors.DatabaseEditor.csproj" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<Folder Include="Templates\" />
26+
</ItemGroup>
27+
28+
29+
30+
</Project>

Diff for: Shared/SharedCore/ByteParsing/ByteChunk.cs

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public void Read<T>(SpesificByteParser<T> parser, out T value, out string error)
140140

141141
public string ReadStringAscii() => Read(ByteParsers.StringAscii);
142142
public string ReadString() => Read(ByteParsers.String);
143+
public string ReadOptionalString() => Read(ByteParsers.OptString);
143144
public int ReadInt32() => Read(ByteParsers.Int32);
144145
public uint ReadUInt32() => Read(ByteParsers.UInt32);
145146
public long ReadInt64() => Read(ByteParsers.Int64);

0 commit comments

Comments
 (0)