forked from Misaki/GhostEngine
Refactor project structure and improve performance
Changed the `ProjectRepository` class to be static for easier usage. Changed `ProjectService` constants to public properties for accessibility. Changed `App.xaml` to consolidate theme resources into `Override.xaml`. Changed `App.xaml.cs` to implement an `AppStateMachine` for better state management. Changed `ConsolePage` and `HierarchyPage` to utilize the new ViewModel structure. Changed `ProjectPage` to use the `ExplorerItem` model for asset display. Changed `Entity` and `EntityManager` to enhance component management with a new `IComponentData` interface. Changed the `Logger` class to introduce structured logging functionality. Changed the system architecture to support dependency management for better organization. Changed the `QueryEnumerable` class to allow for more flexible entity queries. Changed the `TypeHandle` class to improve efficiency in retrieving type handles. Changed the `World` class to support robust world management and multiple worlds. Updated the `Test` class to demonstrate the new entity and component management system.
This commit is contained in:
@@ -1,19 +1,11 @@
|
||||
using Ghost.Data.Models;
|
||||
using System.Data;
|
||||
using Ghost.Data.Resources;
|
||||
using System.Data.SQLite;
|
||||
|
||||
namespace Ghost.Data.Repository;
|
||||
|
||||
internal class ProjectRepository : IDisposable
|
||||
internal static class ProjectRepository
|
||||
{
|
||||
private readonly SQLiteConnection _connection;
|
||||
|
||||
public ProjectRepository(string sourceDirectory)
|
||||
{
|
||||
_connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, sourceDirectory));
|
||||
_connection.Open();
|
||||
}
|
||||
|
||||
private static class Command
|
||||
{
|
||||
public const string CONNECTION_STRING = "Data Source={0}\\projects.db;Version=3;";
|
||||
@@ -24,18 +16,21 @@ internal class ProjectRepository : IDisposable
|
||||
public const string UPDATE_PROJECT_STRING = "UPDATE Projects SET Name = @Name, MetadataPath = @MetadataPath WHERE ID = @ID;";
|
||||
}
|
||||
|
||||
private async Task EnsureTableCreatedAsync()
|
||||
private static async Task EnsureTableCreatedAsync(SQLiteConnection connection)
|
||||
{
|
||||
using var createCommand = _connection.CreateCommand();
|
||||
using var createCommand = connection.CreateCommand();
|
||||
createCommand.CommandText = Command.CREATE_PROJECT_TABLE_STRING;
|
||||
await createCommand.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<ProjectInfo> LoadProjectsAsync()
|
||||
public static async IAsyncEnumerable<ProjectInfo> LoadProjectsAsync()
|
||||
{
|
||||
await EnsureTableCreatedAsync();
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
using var command = _connection.CreateCommand();
|
||||
await EnsureTableCreatedAsync(connection);
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.SELECT_PROJECT_STRING;
|
||||
|
||||
using var reader = command.ExecuteReader();
|
||||
@@ -52,11 +47,14 @@ internal class ProjectRepository : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
public async Task AddProjectAsync(ProjectInfo project)
|
||||
public static async Task AddProjectAsync(ProjectInfo project)
|
||||
{
|
||||
await EnsureTableCreatedAsync();
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
using var command = _connection.CreateCommand();
|
||||
await EnsureTableCreatedAsync(connection);
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.INSERT_PROJECT_STRING;
|
||||
|
||||
command.Parameters.AddWithValue("@Name", project.Name);
|
||||
@@ -65,9 +63,12 @@ internal class ProjectRepository : IDisposable
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public async Task RemoveProjectAsync(ProjectInfo project)
|
||||
public static async Task RemoveProjectAsync(ProjectInfo project)
|
||||
{
|
||||
using var command = _connection.CreateCommand();
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.REMOVE_PROJECT_STRING;
|
||||
|
||||
command.Parameters.AddWithValue("@ID", project.ID);
|
||||
@@ -75,9 +76,12 @@ internal class ProjectRepository : IDisposable
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateProjectAsync(ProjectInfo project)
|
||||
public static async Task UpdateProjectAsync(ProjectInfo project)
|
||||
{
|
||||
using var command = _connection.CreateCommand();
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.UPDATE_PROJECT_STRING;
|
||||
|
||||
command.Parameters.AddWithValue("@Name", project.Name);
|
||||
@@ -86,14 +90,4 @@ internal class ProjectRepository : IDisposable
|
||||
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_connection.State == ConnectionState.Open)
|
||||
{
|
||||
_connection.Close();
|
||||
}
|
||||
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,11 @@ namespace Ghost.Data.Services;
|
||||
|
||||
internal partial class ProjectService
|
||||
{
|
||||
private const string _ASSETS_FOLDER = "Assets";
|
||||
private const string _CONFIG_FOLDER = "ProjectConfig";
|
||||
private const string _TEMPLATE_CONTENT_FILE = "content.zip";
|
||||
|
||||
public const string ASSETS_FOLDER = "Assets";
|
||||
public const string CONFIG_FOLDER = "ProjectConfig";
|
||||
|
||||
public static void EnsureDefaultTemplate()
|
||||
{
|
||||
var templates = Directory.GetFiles(DataPath.s_projectTemplateFolder, "template.json", SearchOption.AllDirectories);
|
||||
@@ -70,8 +71,8 @@ internal partial class ProjectService
|
||||
return Result<ProjectMetadataInfo>.Error("Project directory is invalid or does not exist.");
|
||||
}
|
||||
|
||||
var projectAssetsPath = Path.Combine(projectDirectory, _ASSETS_FOLDER);
|
||||
var projectConfigPath = Path.Combine(projectDirectory, _CONFIG_FOLDER);
|
||||
var projectAssetsPath = Path.Combine(projectDirectory, ASSETS_FOLDER);
|
||||
var projectConfigPath = Path.Combine(projectDirectory, CONFIG_FOLDER);
|
||||
if (!Directory.Exists(projectAssetsPath) || !Directory.Exists(projectConfigPath))
|
||||
{
|
||||
return Result<ProjectMetadataInfo>.Error("Project folder structure is invalid.");
|
||||
@@ -94,8 +95,8 @@ internal partial class ProjectService
|
||||
|
||||
private static async ValueTask SetupRequestFolderAsync(string projectDirectory, string templateDirectory)
|
||||
{
|
||||
var projectAssetsPath = Path.Combine(projectDirectory, _ASSETS_FOLDER);
|
||||
var projectConfigPath = Path.Combine(projectDirectory, _CONFIG_FOLDER);
|
||||
var projectAssetsPath = Path.Combine(projectDirectory, ASSETS_FOLDER);
|
||||
var projectConfigPath = Path.Combine(projectDirectory, CONFIG_FOLDER);
|
||||
var templateContentPath = Path.Combine(templateDirectory, _TEMPLATE_CONTENT_FILE);
|
||||
|
||||
Directory.CreateDirectory(projectAssetsPath);
|
||||
@@ -111,13 +112,17 @@ internal partial class ProjectService
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class ProjectService : IDisposable
|
||||
internal partial class ProjectService
|
||||
{
|
||||
private readonly ProjectRepository _repository = new(DataPath.s_applicationDataFolder);
|
||||
public static ProjectMetadataInfo CurrentProject
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public Task AddProjectAsync(ProjectInfo project)
|
||||
{
|
||||
return _repository.AddProjectAsync(project);
|
||||
return ProjectRepository.AddProjectAsync(project);
|
||||
}
|
||||
|
||||
public async Task<ProjectInfo> AddProjectAsync(string name, string path)
|
||||
@@ -127,27 +132,27 @@ internal partial class ProjectService : IDisposable
|
||||
Name = name,
|
||||
MetadataPath = path,
|
||||
};
|
||||
await _repository.AddProjectAsync(project);
|
||||
await ProjectRepository.AddProjectAsync(project);
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
public Task RemoveProjectAsync(ProjectInfo project)
|
||||
{
|
||||
return _repository.RemoveProjectAsync(project);
|
||||
return ProjectRepository.RemoveProjectAsync(project);
|
||||
}
|
||||
|
||||
public Task UpdateProjectAsync(ProjectInfo project)
|
||||
{
|
||||
return _repository.UpdateProjectAsync(project);
|
||||
return ProjectRepository.UpdateProjectAsync(project);
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<ProjectInfo> LoadAllProjectAsync()
|
||||
{
|
||||
return _repository.LoadProjectsAsync();
|
||||
return ProjectRepository.LoadProjectsAsync();
|
||||
}
|
||||
|
||||
public async Task<Result<ProjectInfo>> CreateProjectAsync(string projectName, string projectDirectory, Version engineVersion, string templatePath)
|
||||
public async Task<Result<ProjectMetadataInfo>> CreateProjectAsync(string projectName, string projectDirectory, Version engineVersion, string templatePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -161,7 +166,7 @@ internal partial class ProjectService : IDisposable
|
||||
// Check if folder is empty
|
||||
if (Directory.EnumerateFiles(projectPath, "*", SearchOption.AllDirectories).Any())
|
||||
{
|
||||
return new(false, null, "Directory is not empty");
|
||||
return new(false, default, "Directory is not empty");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,11 +176,11 @@ internal partial class ProjectService : IDisposable
|
||||
await SetupRequestFolderAsync(projectPath, templatePath);
|
||||
|
||||
var info = await AddProjectAsync(projectName, metadataPath);
|
||||
return new(true, info);
|
||||
return new(true, new(metadataPath, metadata));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result<ProjectInfo>.Error($"Failed to create project: {e.Message}");
|
||||
return Result<ProjectMetadataInfo>.Error($"Failed to create project: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,9 +194,4 @@ internal partial class ProjectService : IDisposable
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_repository.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user