Basic project creation

+ Added TreeNode.RemoveChild
+ Added Directory.Copy
+ Path.Combine now replaces alt directory separator
+ Project: Renamed ProjectName to Name
+ Project.Load: Save settings, if file doesn't exist
This commit is contained in:
Simon Lübeß
2023-07-31 16:35:12 +02:00
parent 17e4a6a589
commit 0d015b485e
15 changed files with 803 additions and 41 deletions
+39 -30
View File
@@ -145,7 +145,9 @@ class AssetHierarchy
_pathToAssetNode.Remove(node->Path);
_identifierToAssetNode.Remove(node->Identifier);
});
_assetRootNode.RemoveChild(_resourcesDirectoryNode);
DeleteTreeAndChildren!(_resourcesDirectoryNode);
_resourcesDirectoryNode = null;
if (!Directory.Exists(ResourcesDirectory))
{
@@ -182,7 +184,9 @@ class AssetHierarchy
_pathToAssetNode.Remove(node->Path);
_identifierToAssetNode.Remove(node->Identifier);
});
_assetRootNode.RemoveChild(_assetsDirectoryNode);
DeleteTreeAndChildren!(_assetsDirectoryNode);
_assetsDirectoryNode = null;
if (!Directory.Exists(AssetsDirectory))
{
@@ -190,6 +194,7 @@ class AssetHierarchy
Log.EngineLogger.Warning($"Assets directory \"{AssetsDirectory}\" doesn't exist.");
return;
}
AssetNode assetNode = new AssetNode();
assetNode.Path = new String(AssetsDirectory);
@@ -297,6 +302,27 @@ class AssetHierarchy
}
}
/// Determines and set the Identfier for the given asset node.
private void DetermineIdentifier(TreeNode<AssetNode> assetNode)
{
if (assetNode->Identifier == null)
assetNode->Identifier = new String();
assetNode->Identifier.Clear();
// Get the Identifier which is simply the path relative to the asste root (either Resources- or Assets-Folder)
if (assetNode.IsInSubtree(_resourcesDirectoryNode))
{
Path.GetRelativePath(assetNode->Path, _resourcesDirectoryNode->Path, assetNode->Identifier);
assetNode->Identifier.Insert(0, "Resources/");
}
else if (assetNode.IsInSubtree(_assetsDirectoryNode))
{
Path.GetRelativePath(assetNode->Path, _assetsDirectoryNode->Path, assetNode->Identifier);
assetNode->Identifier.Insert(0, "Assets/");
}
AssetIdentifier.Fixup(assetNode->Identifier);
}
/// Rebuilds the asset file hierarchy.
private void UpdateFiles()
@@ -324,6 +350,7 @@ class AssetHierarchy
entry.GetFilePath(filepathBuffer..Clear());
Path.GetExtension(filepathBuffer, .. extensionBuffer..Clear());
filepathBuffer.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
// Ignore meta files.
if (extensionBuffer.Equals(AssetFile.ConfigFileExtension, .OrdinalIgnoreCase))
@@ -337,14 +364,13 @@ class AssetHierarchy
assetNode.Name = new String();
Path.GetFileName(filepathBuffer, assetNode.Name);
filepathBuffer.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
assetNode.Path = new String(filepathBuffer);
assetNode.IsDirectory = false;
DetermineIdentifier(assetNode, directory);
treeNode = directory.AddChild(assetNode);
DetermineIdentifier(treeNode);
_pathToAssetNode.Add(assetNode.Path, treeNode);
_identifierToAssetNode.Add(assetNode.Identifier, treeNode);
@@ -376,27 +402,6 @@ class AssetHierarchy
}
}
void DetermineIdentifier(AssetNode assetNode, TreeNode<AssetNode> parentNode)
{
if (assetNode.Identifier == null)
assetNode.Identifier = new String();
assetNode.Identifier.Clear();
// Get the Identifier which is simply the path relative to the asste root (either Resources- or Assets-Folder)
if (parentNode.IsInSubtree(_resourcesDirectoryNode))
{
Path.GetRelativePath(assetNode.Path, _resourcesDirectoryNode->Path, assetNode.Identifier);
assetNode.Identifier.Insert(0, "Resources/");
}
else if (parentNode.IsInSubtree(_assetsDirectoryNode))
{
Path.GetRelativePath(assetNode.Path, _assetsDirectoryNode->Path, assetNode.Identifier);
assetNode.Identifier.Insert(0, "Assets/");
}
AssetIdentifier.Fixup(assetNode.Identifier);
}
/// Adds the given directory to the specified tree.
/// Recursively adds all Files and Subdirectories.
void AddDirectoryToTree(String path, TreeNode<AssetNode> parentNode)
@@ -416,8 +421,6 @@ class AssetHierarchy
assetNode.IsDirectory = true;
Path.GetFileName(assetNode.Path, assetNode.Name);
DetermineIdentifier(assetNode, parentNode);
/*// Get the Identifier which is simply the path relative to the asste root (either Resources- or Assets-Folder)
if (parentNode == _resourcesDirectoryNode || parentNode.IsChildOf(_resourcesDirectoryNode))
{
@@ -432,6 +435,9 @@ class AssetHierarchy
AssetIdentifier.Fixup(assetNode.Identifier);*/
treeNode = parentNode.AddChild(assetNode);
DetermineIdentifier(treeNode);
_pathToAssetNode.Add(assetNode.Path, treeNode);
// No Identifiers for Directories, because we can't use directories as Assets anyways...
//_identifierToAssetNode.Add(assetNode.Identifier, treeNode);
@@ -450,10 +456,9 @@ class AssetHierarchy
AddDirectoryToTree(directoryNameBuffer, treeNode);
}
AddFilesOfDirectory(treeNode);
RemoveOrphanedEntries(treeNode);
AddFilesOfDirectory(treeNode);
}
if (!AssetsDirectory.IsWhiteSpace)
@@ -573,9 +578,13 @@ class AssetHierarchy
}
_pathToAssetNode.Remove(oldFileNameWithContentRoot);
_identifierToAssetNode.Remove(node->Identifier);
node->Path.Set(newFileNameWithContentRoot);
_pathToAssetNode.Add(node->Path, node);
DetermineIdentifier(node);
_identifierToAssetNode.Add(node->Identifier, node);
node->Name.Clear();
Path.GetFileName(newFileNameWithContentRoot, node->Name);
+174 -5
View File
@@ -663,8 +663,18 @@ namespace GlitchyEditor
}
Project newProject = Project.CreateNew(directory, projectName);
defer
{
// If we return an error, we need to cleanup the directory.
if (@return == .Err)
{
Directory.DelTree(directory);
delete newProject;
}
}
var createAssetDirResult = Directory.CreateDirectory(newProject.AssetsFolder);
/*var createAssetDirResult = Directory.CreateDirectory(newProject.AssetsFolder);
if (createAssetDirResult case .Err(let error))
{
@@ -681,12 +691,145 @@ namespace GlitchyEditor
{
Log.ClientLogger.Error($"Failed to copy .gitignore (\"{error}\").");
}
CreateScriptsProject(newProject);*/
// TODO: VS Project
Try!(InitProject(newProject));
Try!(OpenProject(newProject));
return .Ok;
}
private static Result<void> CopyTemplate(Project project)
{
String templatePath = scope .();
Directory.GetCurrentDirectory(templatePath);
Path.Combine(templatePath, "Resources/ProjectTemplate");
if (Directory.Copy(templatePath, project.WorkspacePath) case .Err)
{
Log.EngineLogger.Error($"CopyTemplate: Failed to copy template {templatePath} to {project.WorkspacePath}.");
return .Err;
}
return .Ok;
}
private static Result<void> InitProject(Project project)
{
Try!(CopyTemplate(project));
Try!(MoveFile(project, "Assets/Scripts/TemplateProject.sln", scope $"Assets/Scripts/{project.Name}.sln"));
Try!(MoveFile(project, "Assets/Scripts/TemplateProject.csproj", scope $"Assets/Scripts/{project.Name}.csproj"));
Try!(FixupFile(project, scope $"Assets/Scripts/{project.Name}.sln"));
Try!(FixupFile(project, scope $"Assets/Scripts/{project.Name}.csproj"));
Try!(FixupFile(project, scope $"Assets/Scripts/Properties/AssemblyInfo.cs"));
return .Ok;
}
private static Result<void> MoveFile(Project project, StringView fromName, StringView toName)
{
String fromPath = scope String();
project.PathInProject(fromPath, fromName);
String toPath = scope String();
project.PathInProject(toPath, toName);
if (File.Move(fromPath, toPath) case .Err(let error))
{
Log.EngineLogger.Error($"Failed to move file from {fromPath} to {toPath}. ({error})");
return .Err;
}
return .Ok;
}
private static Result<void> FixupFile(Project project, StringView fileName)
{
String fileTargetPath = scope String();
project.PathInProject(fileTargetPath, fileName);
String fileContent = new:ScopedAlloc! String();
if (File.ReadAllText(fileTargetPath, fileContent, true) case .Err)
{
Log.EngineLogger.Error($"FixupFile: Failed to read file {fileTargetPath}.");
return .Err;
}
fileContent.Replace("[ProjectName]", project.Name);
String scriptCorePath = scope .();
Directory.GetCurrentDirectory(scriptCorePath);
scriptCorePath.Append("/Resources/Scripts/ScriptCore.dll");
fileContent.Replace("[CoreLibPath]", scriptCorePath);
if (File.WriteAllText(fileTargetPath, fileContent, false) case .Err)
{
Log.EngineLogger.Error($"FixupFile: Failed to write file {fileTargetPath}.");
return .Err;
}
return .Ok;
}
/*private static Result<void> CreateFolderInProject(Project project, StringView directoryName)
{
String directoryPath = scope String();
project.PathInProject(directoryPath, directoryName);
if (Directory.CreateDirectory(directoryPath) case .Err(let error))
{
Log.ClientLogger.Error($"Failed to create directory \"{project.AssetsFolder}\".");
return .Err;
}
return .Ok;
}*/
/*private static Result<void> CopyAndFixupFile(Project project, StringView sourcePath, StringView targetName)
{
String fileTargetPath = scope String();
project.PathInProject(fileTargetPath, targetName);
if (File.Copy(sourcePath, fileTargetPath) case .Err(let error))
{
Log.ClientLogger.Error($"Failed to copy file (\"{error}\").");
return .Err;
}
String fileContent = new:ScopedAlloc! String();
Try!(File.ReadAllText(fileTargetPath, fileContent, true));
fileContent.Replace("[ProjectName]", project.Name);
String scriptCorePath = scope .();
Directory.GetCurrentDirectory(scriptCorePath);
scriptCorePath.Append("/Resources/Scripts/ScriptCore.dll");
fileContent.Replace("[CoreLibPath]", scriptCorePath);
Try!(File.WriteAllText(fileTargetPath, fileContent, false));
return .Ok;
}*/
/*private Result<void> CreateScriptsProject(Project project)
{
Try!(CreateFolderInProject(project, "Assets/Scripts"));
Try!(CreateFolderInProject(project, "Assets/Scripts/Properties"));
Try!(CopyAndFixupFile(project, "Resources/Scripts/EmptyProject/EmptyProject.sln", scope $"Assets/Scripts/{project.Name}.sln"));
Try!(CopyAndFixupFile(project, "Resources/Scripts/EmptyProject/EmptyProject.csproj", scope $"Assets/Scripts/{project.Name}.csproj"));
Try!(CopyAndFixupFile(project, "Resources/Scripts/EmptyProject/Properties/AssemblyInfo.cs", scope $"Assets/Scripts/Properties/AssemblyInfo.cs"));
Try!(CopyAndFixupFile(project, "Resources/Scripts/EmptyProject/.gitignore", scope $"Assets/Scripts/.gitignore"));
return .Ok;
}*/
private bool _openCreateProjectModal;
private void ShowCreateNewProjectModal()
@@ -763,6 +906,17 @@ namespace GlitchyEditor
}
}
private void ShowOpenProjectDialog()
{
FolderBrowserDialog folderDialog = scope FolderBrowserDialog();
Result<DialogResult> result = folderDialog.ShowDialog();
if (result case .Ok(let dialogResult) && dialogResult case .OK)
{
OpenProject(folderDialog.SelectedPath);
}
}
private void CloseCurrentProject()
{
OnSceneStop();
@@ -778,15 +932,27 @@ namespace GlitchyEditor
private Result<void> OpenProject(StringView workspacePath)
{
Project openedProject = Project.Load(workspacePath);
Try!(OpenProject(openedProject));
return .Ok;
}
private Result<void> OpenProject(Project project)
{
if (project == null)
return .Err;
CloseCurrentProject();
_currentProject = Project.Load(workspacePath);
_currentProject = project;
String appAssemblyPath = scope String();
_contentManager.SetAssetDirectory(_currentProject.AssetsFolder);
// TODO: obviously change dll name, configurable?
Path.Combine(appAssemblyPath, _currentProject.AssetsFolder, "Scripts/bin/Sandbox.dll");
Path.Combine(appAssemblyPath, _currentProject.AssetsFolder, scope $"Scripts/bin/{_currentProject.Name}.dll");
ScriptEngine.SetAppAssemblyPath(appAssemblyPath);
@@ -933,6 +1099,9 @@ namespace GlitchyEditor
if (ImGui.MenuItem("Create new Project...", "Ctrl+N"))
_openCreateProjectModal = true;
if (ImGui.MenuItem("Open Project...", "Ctrl+O"))
ShowOpenProjectDialog();
if (ImGui.MenuItem("New", "Ctrl+N"))
NewScene();
+15 -1
View File
@@ -20,7 +20,7 @@ class Project
[BonIgnore]
private String _scriptFolder ~ delete _;
public StringView ProjectName => _projectName;
public StringView Name => _projectName;
public StringView WorkspacePath => _workspacePath;
public StringView AssetsFolder => _assetsFolder;
@@ -77,8 +77,22 @@ class Project
else
{
Log.EngineLogger.Warning($"Project file \"{settingsFile}\" doesn't exist.");
if (project.Name.IsWhiteSpace)
{
if (project._projectName == null)
project._projectName = new String();
Path.GetFileName(project.WorkspacePath, project._projectName);
Result<void> result = Bon.SerializeIntoFile(project, settingsFile);
if (result case .Err)
Log.EngineLogger.Warning($"Failed to save project file \"{settingsFile}\".");
}
}
return project;
}
}