refactor: redesign project structure

This commit is contained in:
h z
2025-02-16 22:36:38 +00:00
parent 44099d41d0
commit f69ceb5f4d
6 changed files with 121 additions and 75 deletions

View File

@@ -9,7 +9,9 @@
AssemblyFile="$(CustomTasksAssembly)" AssemblyFile="$(CustomTasksAssembly)"
/> />
<UsingTask <UsingTask
TaskName="ScanTextureSetFolderTask" TaskName="GenerateTextureSetTask"
AssemblyFile="$(CustomTasksAssembly)" AssemblyFile="$(CustomTasksAssembly)"
/> />
</Project> </Project>

View File

@@ -1,3 +1,5 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="CheckCustomTasksAssembly" BeforeTargets="CoreCompile">
<Message Text="CustomTasksAssembly: $(CustomTasksAssembly)" Importance="High" />
</Target>
</Project> </Project>

View File

@@ -1,4 +1,3 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;

View File

@@ -0,0 +1,113 @@
using System;
using System.IO;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Polonium.Tasks;
public class GenerateTextureSetTask : Task
{
[Required]
public string RootPath { get; set; }
[Required]
public string OutputPath { get; set; }
public override bool Execute()
{
try
{
if (!Directory.Exists(RootPath))
{
Log.LogError($"Root path does not exist: {RootPath}");
return false;
}
string[] textureSetDirs = Directory.GetDirectories(RootPath, "*.TextureSet", SearchOption.AllDirectories);
StringBuilder sb = new StringBuilder();
sb
.AppendLine("using System;")
.AppendLine("using Polonium.Attributes;")
.AppendLine("[AutoRegister]")
.AppendLine("public class TextureSetMapRegister")
.AppendLine("{")
.AppendLine(" public static void Register()")
.AppendLine(" {");
StringBuilder sbx = new StringBuilder();
sbx
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine("using Polonium.DataStructures;")
.AppendLine("public static partial class GlobalRegistry")
.AppendLine("{")
.AppendLine(" public static Dictionary<TextureSetName, TextureSet> TextureSetMap = new();")
.AppendLine(" public enum TextureSetName")
.AppendLine(" {");
foreach (string dir in textureSetDirs)
{
StringBuilder sby = new StringBuilder();
sby
.AppendLine("using System;")
.AppendLine("using Polonium.DataStructures;")
.AppendLine("public static partial class GlobalRegistry")
.AppendLine("{")
.AppendLine(" public static partial class TextureSets")
.AppendLine(" {");
string relativePath = GetRelativePath(RootPath, dir);
string godotPath = $"res://Resources/ButtonTextureSet/{relativePath}";
string lName = relativePath.Replace("/", "_").Replace(".TextureSet", "").Replace(".", "");
string[] parts = relativePath.Split('/');
string instanceName = parts[parts.Length - 1].Replace(".TextureSet", "");
string instanceFullName = "GlobalRegistry.TextureSets";
string indent = " ";
StringBuilder sbsy = new StringBuilder();
foreach (string part in parts)
{
if (part == parts[parts.Length - 1])
break;
sby
.AppendLine($"{indent}public static partial class {part}")
.AppendLine($"{indent}{{");
sbsy.Insert(0, $"{indent}}}");
instanceFullName += $".{part}";
indent += " ";
}
instanceFullName += $".{instanceName}";
sby
.AppendLine($"{indent} public static readonly TextureSet {instanceName} = new TextureSet(\"{godotPath}\");")
.Append(sbsy)
.AppendLine(" }")
.AppendLine("}");
File.WriteAllText($"{OutputPath}Patches/{lName}.p.cs", sby.ToString());
sbx.AppendLine($" {lName},");
sb.AppendLine($" GlobalRegistry.TextureSetMap[GlobalRegistry.TextureSetName.{lName}] = {instanceFullName};");
}
sbx
.AppendLine(" }")
.AppendLine("}");
sb
.AppendLine(" }")
.AppendLine("}");
File.WriteAllText($"{OutputPath}Patches/TextureSetName.p.cs", sbx.ToString());
File.WriteAllText($"{OutputPath}Patches/TextureSetMapRegister.p.cs", sb.ToString());
return true;
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
}
private string GetRelativePath(string basePath, string fullPath)
{
Uri baseUri = new Uri(basePath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? basePath : basePath + Path.DirectorySeparatorChar);
Uri fullUri = new Uri(fullPath);
return Uri.UnescapeDataString(baseUri.MakeRelativeUri(fullUri).ToString().Replace('/', Path.DirectorySeparatorChar));
}
}

View File

@@ -1,70 +0,0 @@
using System;
using System.IO;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Polonium.Tasks;
public class ScanTextureSetFolderTask : Task
{
[Required]
public string RootPath { get; set; }
[Required]
public string OutputPath { get; set; }
public override bool Execute()
{
try
{
if (!Directory.Exists(RootPath))
{
Log.LogError($"Root path does not exist: {RootPath}");
return false;
}
string[] textureSetDirs = Directory.GetDirectories(RootPath, "*.TextureSet", SearchOption.AllDirectories);
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
sb1
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine("using Polonium.DataStructures;")
.AppendLine("public static partial class GlobalRegistry")
.AppendLine("{")
.AppendLine(" public static Dictionary<TextureSetName, TextureSet> TextureSetMap = new();")
.AppendLine(" public enum TextureSetName")
.AppendLine(" {");
foreach (var dir in textureSetDirs)
{
string relativePath = GetRelativePath(RootPath, dir);
sb.AppendLine($"res://Resources/ButtonTextureSet/{relativePath}");
sb1.AppendLine($" {relativePath.Replace("/", "_").Replace(".TextureSet", "").Replace(".", "")},");
}
sb1
.AppendLine(" }")
.AppendLine("}");
string outputPath = $"{OutputPath}textures_set.manifest";
File.WriteAllText($"{OutputPath}Patches/TextureSetName.p.cs", sb1.ToString());
File.WriteAllText(outputPath, sb.ToString());
Log.LogMessage($"Output written to {outputPath}");
return true;
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
}
private string GetRelativePath(string basePath, string fullPath)
{
Uri baseUri = new Uri(basePath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? basePath : basePath + Path.DirectorySeparatorChar);
Uri fullUri = new Uri(fullPath);
return Uri.UnescapeDataString(baseUri.MakeRelativeUri(fullUri).ToString().Replace('/', Path.DirectorySeparatorChar));
}
}