diff --git a/Package/build/Polonium.Tasks.props b/Package/build/Polonium.Tasks.props index 9fb025f..3bb7cb2 100644 --- a/Package/build/Polonium.Tasks.props +++ b/Package/build/Polonium.Tasks.props @@ -9,7 +9,9 @@ AssemblyFile="$(CustomTasksAssembly)" /> + + \ No newline at end of file diff --git a/Package/build/Polonium.Tasks.targets b/Package/build/Polonium.Tasks.targets index 0dfb6a3..538978c 100644 --- a/Package/build/Polonium.Tasks.targets +++ b/Package/build/Polonium.Tasks.targets @@ -1,3 +1,5 @@ - + + + \ No newline at end of file diff --git a/Polonium.Tasks.csproj b/Polonium.Tasks.csproj index b7b9852..5001c6a 100644 --- a/Polonium.Tasks.csproj +++ b/Polonium.Tasks.csproj @@ -75,8 +75,8 @@ diff --git a/src/GenerateProxyNodesTask.cs b/src/GenerateProxyNodesTask.cs index b3279a5..8fcb57f 100644 --- a/src/GenerateProxyNodesTask.cs +++ b/src/GenerateProxyNodesTask.cs @@ -1,4 +1,3 @@ - using System; using System.Collections.Generic; using System.IO; diff --git a/src/GenerateTextureSetTask.cs b/src/GenerateTextureSetTask.cs new file mode 100644 index 0000000..759029a --- /dev/null +++ b/src/GenerateTextureSetTask.cs @@ -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 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)); + } + +} \ No newline at end of file diff --git a/src/ScanTextureSetFolderTask.cs b/src/ScanTextureSetFolderTask.cs deleted file mode 100644 index 3bddeeb..0000000 --- a/src/ScanTextureSetFolderTask.cs +++ /dev/null @@ -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 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)); - } - -} \ No newline at end of file