Refactor project management and enhance architecture

Added a new static class `AssetsPath` for asset management.
Added a new icon file (`icon-256.ico`) for UI representation.
Added new package references to enhance functionality.
Added internals visibility attributes for better testing.
Added a new `EngineEditorViewModel` class for MVVM support.
Added a new `GameObject` class for component management.
Added a new `BitSet` class for efficient bit manipulation.
Added various utility classes to support the new entity system.

Changed the `ID` property in `ProjectInfo` to internal.
Changed the `AddProjectAsync` method to return the created `ProjectInfo`.
Changed the connection string retrieval method to use the new `Command` constant.
Changed the `DataPath` class to use `readonly` fields for folder paths.
Changed the `ActivationHandler` class to use new `DataPath` constants.
Changed the `OpenProjectPage` layout and interaction for better UI.

Updated the target framework to a newer version for compatibility.
Updated the `ProjectService` to use new constants from `DataPath`.
Updated the `World` class to improve entity management.

Refactored the `ProjectRepository` class to encapsulate SQL commands.
Refactored the `Transform` class to use properties for better encapsulation.
This commit is contained in:
2025-04-05 16:07:53 +09:00
parent 62fe30ff2b
commit 7cd881b7d4
44 changed files with 1672 additions and 247 deletions

View File

@@ -6,17 +6,22 @@ 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 class Command
{
public const string CONNECTION_STRING = "Data Source={0}\\projects.db;Version=3;";
public 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);";
public const string SELECT_PROJECT_STRING = "SELECT * FROM Projects";
public const string INSERT_PROJECT_STRING = "INSERT INTO Projects (Name, Path, EngineVersion, LastOpened) VALUES (@Name, @Path, @EngineVersion, @LastOpened);";
public const string REMOVE_PROJECT_STRING = "DELETE FROM Projects WHERE ID = @ID;";
public const string UPDATE_PROJECT_STRING = "UPDATE Projects SET Name = @Name, Path = @Path, EngineVersion = @EngineVersion, LastOpened = @LastOpened WHERE ID = @ID;";
}
private static string GetConnectionString() => string.Format(_CONNECTION_STRING, DataPath.ApplicationDataFolder);
private static string GetConnectionString() => string.Format(Command.CONNECTION_STRING, DataPath.APPLICATION_DATA_FOLDER);
private static async Task EnsureTableCreatedAsync(SQLiteConnection connection)
{
using var createCommand = connection.CreateCommand();
createCommand.CommandText = _CREATE_PROJECT_TABLE_STRING;
createCommand.CommandText = Command.CREATE_PROJECT_TABLE_STRING;
await createCommand.ExecuteNonQueryAsync();
}
@@ -28,13 +33,14 @@ internal static class ProjectRepository
await EnsureTableCreatedAsync(connection);
using var command = connection.CreateCommand();
command.CommandText = _SELECT_PROJECT_STRING;
command.CommandText = Command.SELECT_PROJECT_STRING;
using var reader = command.ExecuteReader();
while (await reader.ReadAsync())
{
var project = new ProjectInfo
{
ID = reader.GetInt32(0),
Name = reader.GetString(1),
Path = reader.GetString(2),
EngineVersion = new Version(reader.GetString(3)),
@@ -53,12 +59,43 @@ internal static class ProjectRepository
await EnsureTableCreatedAsync(connection);
using var command = connection.CreateCommand();
command.CommandText = _INSERT_PROJECT_STRING;
command.CommandText = Command.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();
}
public static async Task RemoveProjectAsync(ProjectInfo project)
{
using var connection = new SQLiteConnection(GetConnectionString());
await connection.OpenAsync();
using var command = connection.CreateCommand();
command.CommandText = Command.REMOVE_PROJECT_STRING;
command.Parameters.AddWithValue("@ID", project.ID);
await command.ExecuteNonQueryAsync();
}
public static async Task UpdateProjectAsync(ProjectInfo project)
{
using var connection = new SQLiteConnection(GetConnectionString());
await connection.OpenAsync();
using var command = connection.CreateCommand();
command.CommandText = Command.UPDATE_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);
command.Parameters.AddWithValue("@ID", project.ID); // Ensure the ID parameter is added
await command.ExecuteNonQueryAsync();
}
}

View File

@@ -7,7 +7,7 @@ public class ProjectInfo
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID
{
get; set;
get; internal set;
}
public required string Name

View File

@@ -0,0 +1,8 @@
namespace Ghost.Data.Resources;
public static class AssetsPath
{
public const string ASSETS_FOLDER = "Assets";
public readonly static string AppIconPath = Path.Combine(AppContext.BaseDirectory, $"{ASSETS_FOLDER}/Icon-256.ico");
}

View File

@@ -4,6 +4,6 @@ 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");
public readonly static string APPLICATION_DATA_FOLDER = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ENGINE_DATA_FOLDER_NAME);
public readonly static string PROJECT_TEMPLATES_FOLDER = Path.Combine(APPLICATION_DATA_FOLDER, "ProjectTemplates");
}

View File

@@ -8,19 +8,18 @@ namespace Ghost.Data.Services;
public class ProjectService
{
private const string _TEMPLATE_CONTENT_FILE = "content.zip";
private const string _ASSETS_FOLDER = "Assets";
private const string _TEMPLATE_CONTENT_FILE = "content.zip";
public async IAsyncEnumerable<(string path, TemplateInfo info)> GetProjectTemplatesAsync()
{
var templatesFolder = DataPath.ProjectTemplatesFolder;
var templatesFolder = DataPath.PROJECT_TEMPLATES_FOLDER;
if (!Directory.Exists(templatesFolder))
{
yield break;
}
var templates = Directory.GetFiles(DataPath.ProjectTemplatesFolder, "template.json", SearchOption.AllDirectories);
var templates = Directory.GetFiles(DataPath.PROJECT_TEMPLATES_FOLDER, "template.json", SearchOption.AllDirectories);
foreach (var templatePath in templates)
{
var fileStream = File.OpenRead(templatePath);
@@ -52,6 +51,11 @@ public class ProjectService
});
}
public IAsyncEnumerable<ProjectInfo> LoadAllProjectAsync()
{
return ProjectRepository.LoadProjectsAsync();
}
public async Task<string> CreateProjectAsync(string projectName, string projectDirectory, string templatePath)
{
var projectPath = Path.Combine(projectDirectory, projectName);
@@ -70,19 +74,27 @@ public class ProjectService
return ProjectRepository.AddProjectAsync(project);
}
public Task AddProjectAsync(string name, string path, Version version)
public async Task<ProjectInfo> AddProjectAsync(string name, string path, Version version)
{
return ProjectRepository.AddProjectAsync(new ProjectInfo
var project = new ProjectInfo
{
Name = name,
Path = path,
EngineVersion = version,
LastOpened = DateTime.Now
});
};
await ProjectRepository.AddProjectAsync(project);
return project;
}
public IAsyncEnumerable<ProjectInfo> LoadProjectAsync()
public Task RemoveProjectAsync(ProjectInfo project)
{
return ProjectRepository.LoadProjectsAsync();
return ProjectRepository.RemoveProjectAsync(project);
}
public Task UpdateProjectAsync(ProjectInfo project)
{
return ProjectRepository.UpdateProjectAsync(project);
}
}