Refactor project structure and enhance functionality

Added `InternalsVisibleTo` attribute for "Ghost.Editor" in `AssemblyInfo.cs`.
Added a binary file `Empty.zip` to the project.
Added a new `ProjectMetadata` class in `ProjectMetadata.cs`.
Added new states and interfaces for managing application states in `EditorState.cs`, `LandingState.cs`, and `IAppState.cs`.
Added a notification service in `INotificationService.cs` and `StackedNotificationService.cs`.
Added new XAML files for UI components, including `InspectorView.xaml` and `InternalControls.xaml`.

Changed the `ProjectInfo` class in `ProjectInfo.cs` to include a `MetadataPath` property instead of `Path` and `EngineVersion`.
Changed the `TemplateInfo` class in `TemplateInfo.cs` to use a struct instead of a class for `TemplateData`.
Changed the `ProjectService` class to use the new `ProjectRepository` for managing project data.

Removed several using directives and the entire `ProjectRepository` class from `ProjectRepository.cs`, replacing it with a new implementation.
Removed old methods and properties in `EntityManager` and `World` classes to improve entity management and component handling.

Updated the `Ghost.Data.csproj` file to include the new `Empty.zip` file as a content item.
Updated the `ProjectRepository` class to manage project data using SQLite.
Updated various XAML files to include new styles and controls, improving the overall UI design.
Updated the `CreateProjectViewModel` to include a notification service and handle project creation logic.
Updated the test project to include references to the new `Ghost.Graphics` project and modified test cases to align with the new structure.
This commit is contained in:
2025-05-31 01:45:34 +09:00
parent 67b6040b5e
commit 61bbb1bc68
66 changed files with 1923 additions and 733 deletions

View File

@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Ghost.Editor")]

Binary file not shown.

View File

@@ -1,101 +0,0 @@
using Ghost.Data.Models;
using Ghost.Data.Resources;
using System.Data.SQLite;
namespace Ghost.Data.DataContext;
internal static class ProjectRepository
{
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(Command.CONNECTION_STRING, DataPath.APPLICATION_DATA_FOLDER);
private static async Task EnsureTableCreatedAsync(SQLiteConnection connection)
{
using var createCommand = connection.CreateCommand();
createCommand.CommandText = Command.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 = 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)),
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 = 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

@@ -20,4 +20,10 @@
<PackageReference Include="System.Drawing.Common" Version="4.7.3" />
</ItemGroup>
<ItemGroup>
<Content Include="Assets\ProjectTemplates\Empty.zip">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -5,6 +5,7 @@ namespace Ghost.Data;
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(TemplateInfo))]
[JsonSerializable(typeof(ProjectMetadata))]
internal partial class JsonContext : JsonSerializerContext
{
}

View File

@@ -2,7 +2,7 @@
namespace Ghost.Data.Models;
public class ProjectInfo
internal class ProjectInfo
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID
@@ -15,17 +15,7 @@ public class ProjectInfo
get; set;
}
public required string Path
{
get; set;
}
public required Version EngineVersion
{
get; set;
}
public required DateTime LastOpened
public required string MetadataPath
{
get; set;
}

View File

@@ -0,0 +1,51 @@
namespace Ghost.Data.Models;
public class ProjectMetadata
{
public const string PROJECT_EXTENSION = "ghostproj";
public Guid ID
{
get; set;
}
public string Name
{
get; set;
}
public Version EngineVersion
{
get; set;
}
public DateTime CreatedAt
{
get; set;
}
public DateTime LastOpened
{
get; set;
}
public ProjectMetadata(string name, Version engineVersion)
{
ID = Guid.NewGuid();
Name = name;
EngineVersion = engineVersion;
CreatedAt = DateTime.UtcNow;
LastOpened = DateTime.UtcNow;
}
// Parameterless constructor for deserialization
public ProjectMetadata()
{
}
}
public readonly struct ProjectMetadataInfo(string path, ProjectMetadata metadata)
{
public readonly string Path => path;
public readonly ProjectMetadata Metadata => metadata;
}

View File

@@ -0,0 +1,53 @@
namespace Ghost.Data.Models;
public readonly struct Result
{
public readonly bool success;
public readonly string? message;
public Result(bool success, string? message = null)
{
this.success = success;
this.message = message;
}
public static Result OK()
{
return new Result(true);
}
public static Result Error(string? message)
{
return new Result(false, message);
}
public override string ToString() => success ? "OK" : $"Error: {message}";
}
public readonly struct Result<T>
{
public readonly bool success;
public readonly T? data;
public readonly string? message;
public Result(bool success, T? data, string? message = null)
{
this.success = success;
this.data = data;
this.message = message;
}
public static Result<T> OK(T data)
{
return new Result<T>(true, data);
}
public static Result<T> Error(string? message)
{
return new Result<T>(false, default, message);
}
public override string ToString() => success ? $"OK: {data}" : $"Error: {message}";
}

View File

@@ -23,21 +23,21 @@ public class TemplateInfo
}
}
public class TemplateData(string templatePath, TemplateInfo info)
public struct 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 readonly TemplateInfo Info => info;
public Uri GetIconURI()
public readonly Uri GetIconURI()
{
return new Uri(Path.Combine(directory, _ICON_NAME));
}
public Uri GetPreviewURI()
public readonly Uri GetPreviewURI()
{
return new Uri(Path.Combine(directory, _PREVIEW_NAME));
}

View File

@@ -0,0 +1,99 @@
using Ghost.Data.Models;
using System.Data;
using System.Data.SQLite;
namespace Ghost.Data.Repository;
internal class ProjectRepository : IDisposable
{
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;";
public const string CREATE_PROJECT_TABLE_STRING = "CREATE TABLE IF NOT EXISTS Projects (ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, MetadataPath TEXT);";
public const string SELECT_PROJECT_STRING = "SELECT * FROM Projects";
public const string INSERT_PROJECT_STRING = "INSERT INTO Projects (Name, MetadataPath) VALUES (@Name, @MetadataPath);";
public const string REMOVE_PROJECT_STRING = "DELETE FROM Projects WHERE ID = @ID;";
public const string UPDATE_PROJECT_STRING = "UPDATE Projects SET Name = @Name, MetadataPath = @MetadataPath WHERE ID = @ID;";
}
private async Task EnsureTableCreatedAsync()
{
using var createCommand = _connection.CreateCommand();
createCommand.CommandText = Command.CREATE_PROJECT_TABLE_STRING;
await createCommand.ExecuteNonQueryAsync();
}
public async IAsyncEnumerable<ProjectInfo> LoadProjectsAsync()
{
await EnsureTableCreatedAsync();
using var command = _connection.CreateCommand();
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),
MetadataPath = reader.GetString(2),
};
yield return project;
}
}
public async Task AddProjectAsync(ProjectInfo project)
{
await EnsureTableCreatedAsync();
using var command = _connection.CreateCommand();
command.CommandText = Command.INSERT_PROJECT_STRING;
command.Parameters.AddWithValue("@Name", project.Name);
command.Parameters.AddWithValue("@MetadataPath", project.MetadataPath);
await command.ExecuteNonQueryAsync();
}
public async Task RemoveProjectAsync(ProjectInfo project)
{
using var command = _connection.CreateCommand();
command.CommandText = Command.REMOVE_PROJECT_STRING;
command.Parameters.AddWithValue("@ID", project.ID);
await command.ExecuteNonQueryAsync();
}
public async Task UpdateProjectAsync(ProjectInfo project)
{
using var command = _connection.CreateCommand();
command.CommandText = Command.UPDATE_PROJECT_STRING;
command.Parameters.AddWithValue("@Name", project.Name);
command.Parameters.AddWithValue("@MetadataPath", project.MetadataPath);
command.Parameters.AddWithValue("@ID", project.ID);
await command.ExecuteNonQueryAsync();
}
public void Dispose()
{
if (_connection.State == ConnectionState.Open)
{
_connection.Close();
}
_connection.Dispose();
}
}

View File

@@ -4,5 +4,5 @@ public static class AssetsPath
{
public const string ASSETS_FOLDER = "Assets";
public readonly static string AppIconPath = Path.Combine(AppContext.BaseDirectory, $"{ASSETS_FOLDER}/Icon-256.ico");
public readonly static string s_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 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");
public readonly static string s_applicationDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ENGINE_DATA_FOLDER_NAME);
public readonly static string s_projectTemplateFolder = Path.Combine(s_applicationDataFolder, "ProjectTemplates");
}

View File

@@ -1,25 +1,38 @@
using Ghost.Data.DataContext;
using Ghost.Data.Models;
using Ghost.Data.Models;
using Ghost.Data.Repository;
using Ghost.Data.Resources;
using System.IO.Compression;
using System.Text.Json;
namespace Ghost.Data.Services;
public class ProjectService
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 async IAsyncEnumerable<(string path, TemplateInfo info)> GetProjectTemplatesAsync()
public static void EnsureDefaultTemplate()
{
var templatesFolder = DataPath.PROJECT_TEMPLATES_FOLDER;
var templates = Directory.GetFiles(DataPath.s_projectTemplateFolder, "template.json", SearchOption.AllDirectories);
if (templates.Length > 0)
{
return; // Default template already exists
}
var defaultTemplatePath = Path.Combine(AppContext.BaseDirectory, "Assets/ProjectTemplates/Empty.zip");
ZipFile.ExtractToDirectory(defaultTemplatePath, DataPath.s_projectTemplateFolder, true);
}
public static async IAsyncEnumerable<(string path, TemplateInfo info)> GetProjectTemplatesAsync()
{
var templatesFolder = DataPath.s_projectTemplateFolder;
if (!Directory.Exists(templatesFolder))
{
yield break;
}
var templates = Directory.GetFiles(DataPath.PROJECT_TEMPLATES_FOLDER, "template.json", SearchOption.AllDirectories);
var templates = Directory.GetFiles(DataPath.s_projectTemplateFolder, "template.json", SearchOption.AllDirectories);
foreach (var templatePath in templates)
{
var fileStream = File.OpenRead(templatePath);
@@ -33,68 +46,152 @@ public class ProjectService
}
}
private Task SetupAssetsFolder(string projectPath, string templatePath)
public static async Task CreateMetadataFileAsync(string path, ProjectMetadata metadata)
{
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);
});
await using var fileStream = File.Create(path);
await JsonSerializer.SerializeAsync(fileStream, metadata, JsonContext.Default.ProjectMetadata);
}
public IAsyncEnumerable<ProjectInfo> LoadAllProjectAsync()
public static async Task<ProjectMetadata?> LoadMetadataAsync(string ghostprojPath)
{
return ProjectRepository.LoadProjectsAsync();
}
public async Task<string> CreateProjectAsync(string projectName, string projectDirectory, string templatePath)
{
var projectPath = Path.Combine(projectDirectory, projectName);
if (!Directory.Exists(projectPath))
if (!File.Exists(ghostprojPath))
{
Directory.CreateDirectory(projectPath);
throw new FileNotFoundException("Project metadata file not found.", ghostprojPath);
}
await SetupAssetsFolder(projectPath, templatePath);
return projectPath;
await using var fileStream = File.OpenRead(ghostprojPath);
return await JsonSerializer.DeserializeAsync<ProjectMetadata>(fileStream, JsonContext.Default.ProjectMetadata);
}
public static async Task<Result<ProjectMetadataInfo>> ValidateProjectDirectoryAsync(string? projectDirectory)
{
if (string.IsNullOrWhiteSpace(projectDirectory) || !Directory.Exists(projectDirectory))
{
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);
if (!Directory.Exists(projectAssetsPath) || !Directory.Exists(projectConfigPath))
{
return Result<ProjectMetadataInfo>.Error("Project folder structure is invalid.");
}
var metadataPath = Directory.GetFiles(projectDirectory, $"*.{ProjectMetadata.PROJECT_EXTENSION}", SearchOption.TopDirectoryOnly).FirstOrDefault();
if (string.IsNullOrWhiteSpace(metadataPath) || !File.Exists(metadataPath))
{
return Result<ProjectMetadataInfo>.Error("Project metadata file not found.");
}
var metadata = await LoadMetadataAsync(metadataPath);
if (metadata == null)
{
return Result<ProjectMetadataInfo>.Error("Project metadata file is corrupted or invalid.");
}
return Result<ProjectMetadataInfo>.OK(new(metadataPath, metadata));
}
private static async ValueTask SetupRequestFolderAsync(string projectDirectory, string templateDirectory)
{
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);
if (File.Exists(templateContentPath))
{
await Task.Run(() =>
{
ZipFile.ExtractToDirectory(templateContentPath, projectAssetsPath);
});
}
Directory.CreateDirectory(projectConfigPath);
}
}
internal partial class ProjectService : IDisposable
{
private readonly ProjectRepository _repository = new(DataPath.s_applicationDataFolder);
public Task AddProjectAsync(ProjectInfo project)
{
return ProjectRepository.AddProjectAsync(project);
return _repository.AddProjectAsync(project);
}
public async Task<ProjectInfo> AddProjectAsync(string name, string path, Version version)
public async Task<ProjectInfo> AddProjectAsync(string name, string path)
{
var project = new ProjectInfo
{
Name = name,
Path = path,
EngineVersion = version,
LastOpened = DateTime.Now
MetadataPath = path,
};
await ProjectRepository.AddProjectAsync(project);
await _repository.AddProjectAsync(project);
return project;
}
public Task RemoveProjectAsync(ProjectInfo project)
{
return ProjectRepository.RemoveProjectAsync(project);
return _repository.RemoveProjectAsync(project);
}
public Task UpdateProjectAsync(ProjectInfo project)
{
return ProjectRepository.UpdateProjectAsync(project);
return _repository.UpdateProjectAsync(project);
}
public IAsyncEnumerable<ProjectInfo> LoadAllProjectAsync()
{
return _repository.LoadProjectsAsync();
}
public async Task<Result<ProjectInfo>> CreateProjectAsync(string projectName, string projectDirectory, Version engineVersion, string templatePath)
{
try
{
var projectPath = Path.Combine(projectDirectory, projectName);
if (!Directory.Exists(projectPath))
{
Directory.CreateDirectory(projectPath);
}
else
{
// Check if folder is empty
if (Directory.EnumerateFiles(projectPath, "*", SearchOption.AllDirectories).Any())
{
return new(false, null, "Directory is not empty");
}
}
var metadata = new ProjectMetadata(projectName, engineVersion);
var metadataPath = Path.Combine(projectPath, $"{projectName}.{ProjectMetadata.PROJECT_EXTENSION}");
await CreateMetadataFileAsync(metadataPath, metadata);
await SetupRequestFolderAsync(projectPath, templatePath);
var info = await AddProjectAsync(projectName, metadataPath);
return new(true, info);
}
catch (Exception e)
{
return Result<ProjectInfo>.Error($"Failed to create project: {e.Message}");
}
}
public async Task<Result<ProjectMetadataInfo>> AddProjectFromDirectoryAsync(string projectDirectory)
{
var result = await ValidateProjectDirectoryAsync(projectDirectory);
if (result.success)
{
await AddProjectAsync(result.data.Metadata.Name, result.data.Path);
}
return result;
}
public void Dispose()
{
_repository.Dispose();
}
}