Files
GhostEngine/Ghost.Data/Services/ProjectService.cs
Misaki 7cd881b7d4 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.
2025-04-05 16:07:53 +09:00

100 lines
2.9 KiB
C#

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 _ASSETS_FOLDER = "Assets";
private const string _TEMPLATE_CONTENT_FILE = "content.zip";
public async IAsyncEnumerable<(string path, TemplateInfo info)> GetProjectTemplatesAsync()
{
var templatesFolder = DataPath.PROJECT_TEMPLATES_FOLDER;
if (!Directory.Exists(templatesFolder))
{
yield break;
}
var templates = Directory.GetFiles(DataPath.PROJECT_TEMPLATES_FOLDER, "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 IAsyncEnumerable<ProjectInfo> LoadAllProjectAsync()
{
return ProjectRepository.LoadProjectsAsync();
}
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 async Task<ProjectInfo> AddProjectAsync(string name, string path, Version version)
{
var project = new ProjectInfo
{
Name = name,
Path = path,
EngineVersion = version,
LastOpened = DateTime.Now
};
await ProjectRepository.AddProjectAsync(project);
return project;
}
public Task RemoveProjectAsync(ProjectInfo project)
{
return ProjectRepository.RemoveProjectAsync(project);
}
public Task UpdateProjectAsync(ProjectInfo project)
{
return ProjectRepository.UpdateProjectAsync(project);
}
}