Refactor activation handling and introduce entity system
Added new `ActivationHandler` class for folder initialization. Added `ProjectService` class for project-related operations. Added `Ghost.Entities` project with entity management classes. Added `EngineEditorWindow` for enhanced user interface. Changed project files to restructure dependencies and remove unused references. Changed `ProjectRepository` to use asynchronous methods for improved performance. Changed data binding in `CreateProjectPage.xaml` and `OpenProjectPage.xaml` to use new data models. Changed `App.xaml.cs` to utilize the new `ActivationHandler` and include additional services. Removed `IActivationHandler` interface and integrated its functionality into `ActivationHandler`. Removed `EditorActivationHandler` as its functionality was merged into `ActivationHandler`. Updated `AssemblyInfo.cs` to include global using directives for entity types. Updated image assets to reflect visual resource changes.
This commit is contained in:
64
Ghost.Data/DataContext/ProjectRepository.cs
Normal file
64
Ghost.Data/DataContext/ProjectRepository.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Ghost.Data.Models;
|
||||
using Ghost.Data.Resources;
|
||||
using System.Data.SQLite;
|
||||
|
||||
namespace Ghost.Data.DataContext;
|
||||
|
||||
internal static class ProjectRepository
|
||||
{
|
||||
private const string _CONNECTION_STRING = "Data Source={0}\\projects.db;Version=3;";
|
||||
private const string _CREATE_PROJECT_TABLE_STRING = "CREATE TABLE IF NOT EXISTS Projects (ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Path TEXT, EngineVersion TEXT, LastOpened DATETIME);";
|
||||
private const string _SELECT_PROJECT_STRING = "SELECT * FROM Projects";
|
||||
private const string _INSERT_PROJECT_STRING = "INSERT INTO Projects (Name, Path, EngineVersion, LastOpened) VALUES (@Name, @Path, @EngineVersion, @LastOpened);";
|
||||
|
||||
private static string GetConnectionString() => string.Format(_CONNECTION_STRING, DataPath.ApplicationDataFolder);
|
||||
|
||||
private static async Task EnsureTableCreatedAsync(SQLiteConnection connection)
|
||||
{
|
||||
using var createCommand = connection.CreateCommand();
|
||||
createCommand.CommandText = _CREATE_PROJECT_TABLE_STRING;
|
||||
await createCommand.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public static async IAsyncEnumerable<ProjectInfo> LoadProjectsAsync()
|
||||
{
|
||||
using var connection = new SQLiteConnection(GetConnectionString());
|
||||
await connection.OpenAsync();
|
||||
|
||||
await EnsureTableCreatedAsync(connection);
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = _SELECT_PROJECT_STRING;
|
||||
|
||||
using var reader = command.ExecuteReader();
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
var project = new ProjectInfo
|
||||
{
|
||||
Name = reader.GetString(1),
|
||||
Path = reader.GetString(2),
|
||||
EngineVersion = new Version(reader.GetString(3)),
|
||||
LastOpened = reader.GetDateTime(4)
|
||||
};
|
||||
|
||||
yield return project;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task AddProjectAsync(ProjectInfo project)
|
||||
{
|
||||
using var connection = new SQLiteConnection(GetConnectionString());
|
||||
await connection.OpenAsync();
|
||||
|
||||
await EnsureTableCreatedAsync(connection);
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = _INSERT_PROJECT_STRING;
|
||||
|
||||
command.Parameters.AddWithValue("@Name", project.Name);
|
||||
command.Parameters.AddWithValue("@Path", project.Path);
|
||||
command.Parameters.AddWithValue("@EngineVersion", project.EngineVersion.ToString());
|
||||
command.Parameters.AddWithValue("@LastOpened", project.LastOpened);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
23
Ghost.Data/Ghost.Data.csproj
Normal file
23
Ghost.Data/Ghost.Data.csproj
Normal file
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<IsAotCompatible>True</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<IsAotCompatible>True</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||
<PackageReference Include="System.Data.SQLite" Version="1.0.119" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="4.7.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
10
Ghost.Data/JsonContext.cs
Normal file
10
Ghost.Data/JsonContext.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Ghost.Data.Models;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ghost.Data;
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(TemplateInfo))]
|
||||
internal partial class JsonContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
32
Ghost.Data/Models/ProjectInfo.cs
Normal file
32
Ghost.Data/Models/ProjectInfo.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Ghost.Data.Models;
|
||||
|
||||
public class ProjectInfo
|
||||
{
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int ID
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public required string Name
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public required string Path
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public required Version EngineVersion
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public required DateTime LastOpened
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
}
|
||||
44
Ghost.Data/Models/TemplateInfo.cs
Normal file
44
Ghost.Data/Models/TemplateInfo.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
namespace Ghost.Data.Models;
|
||||
|
||||
public class TemplateInfo
|
||||
{
|
||||
public required string Name
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public string? Description
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public required Version TemplateVersion
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public required Version EngineVersion
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
}
|
||||
|
||||
public class TemplateData(string templatePath, TemplateInfo info)
|
||||
{
|
||||
private const string _ICON_NAME = "icon.png";
|
||||
private const string _PREVIEW_NAME = "preview.png";
|
||||
|
||||
public string directory = Path.GetDirectoryName(templatePath)!;
|
||||
|
||||
public TemplateInfo Info => info;
|
||||
|
||||
public Uri GetIconURI()
|
||||
{
|
||||
return new Uri(Path.Combine(directory, _ICON_NAME));
|
||||
}
|
||||
|
||||
public Uri GetPreviewURI()
|
||||
{
|
||||
return new Uri(Path.Combine(directory, _PREVIEW_NAME));
|
||||
}
|
||||
}
|
||||
9
Ghost.Data/Resources/DataPath.cs
Normal file
9
Ghost.Data/Resources/DataPath.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Ghost.Data.Resources;
|
||||
|
||||
public class DataPath
|
||||
{
|
||||
public const string ENGINE_DATA_FOLDER_NAME = "GhostEngine";
|
||||
|
||||
public static string ApplicationDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ENGINE_DATA_FOLDER_NAME);
|
||||
public static string ProjectTemplatesFolder = Path.Combine(ApplicationDataFolder, "ProjectTemplates");
|
||||
}
|
||||
88
Ghost.Data/Services/ProjectService.cs
Normal file
88
Ghost.Data/Services/ProjectService.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using Ghost.Data.DataContext;
|
||||
using Ghost.Data.Models;
|
||||
using Ghost.Data.Resources;
|
||||
using System.IO.Compression;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Ghost.Data.Services;
|
||||
|
||||
public class ProjectService
|
||||
{
|
||||
private const string _TEMPLATE_CONTENT_FILE = "content.zip";
|
||||
|
||||
private const string _ASSETS_FOLDER = "Assets";
|
||||
|
||||
public async IAsyncEnumerable<(string path, TemplateInfo info)> GetProjectTemplatesAsync()
|
||||
{
|
||||
var templatesFolder = DataPath.ProjectTemplatesFolder;
|
||||
if (!Directory.Exists(templatesFolder))
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
var templates = Directory.GetFiles(DataPath.ProjectTemplatesFolder, "template.json", SearchOption.AllDirectories);
|
||||
foreach (var templatePath in templates)
|
||||
{
|
||||
var fileStream = File.OpenRead(templatePath);
|
||||
var templateInfo = await JsonSerializer.DeserializeAsync<TemplateInfo>(fileStream, JsonContext.Default.TemplateInfo);
|
||||
if (templateInfo == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return (templatePath, templateInfo);
|
||||
}
|
||||
}
|
||||
|
||||
private Task SetupAssetsFolder(string projectPath, string templatePath)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
var templateContentPath = Path.Combine(templatePath, _TEMPLATE_CONTENT_FILE);
|
||||
var projectAssetsPath = Path.Combine(projectPath, _ASSETS_FOLDER);
|
||||
|
||||
Directory.CreateDirectory(projectAssetsPath);
|
||||
|
||||
if (!File.Exists(templateContentPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ZipFile.ExtractToDirectory(templateContentPath, projectAssetsPath);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<string> CreateProjectAsync(string projectName, string projectDirectory, string templatePath)
|
||||
{
|
||||
var projectPath = Path.Combine(projectDirectory, projectName);
|
||||
if (!Directory.Exists(projectPath))
|
||||
{
|
||||
Directory.CreateDirectory(projectPath);
|
||||
}
|
||||
|
||||
await SetupAssetsFolder(projectPath, templatePath);
|
||||
|
||||
return projectPath;
|
||||
}
|
||||
|
||||
public Task AddProjectAsync(ProjectInfo project)
|
||||
{
|
||||
return ProjectRepository.AddProjectAsync(project);
|
||||
}
|
||||
|
||||
public Task AddProjectAsync(string name, string path, Version version)
|
||||
{
|
||||
return ProjectRepository.AddProjectAsync(new ProjectInfo
|
||||
{
|
||||
Name = name,
|
||||
Path = path,
|
||||
EngineVersion = version,
|
||||
LastOpened = DateTime.Now
|
||||
});
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<ProjectInfo> LoadProjectAsync()
|
||||
{
|
||||
return ProjectRepository.LoadProjectsAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user