#if TOOLS using Godot; using Godot.Collections; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; [Tool] public partial class RDatCanvasTextureImporter : EditorImportPlugin { public override string _GetImporterName() { return "aeqw89.rdatImporter"; } public override string _GetVisibleName() { return "RDAT CanvasTexture Importer"; } public override string[] _GetRecognizedExtensions() { return ["rdat"]; } public override string _GetSaveExtension() { return "tres"; } public override string _GetResourceType() { return nameof(CanvasTexture); } public override int _GetPresetCount() { return 1; } public override string _GetPresetName(int presetIndex) { return presetIndex switch { 0 => "Conventionally Named Layers", _ => "Undefined Preset" }; } public override Array _GetImportOptions(string path, int presetIndex) { Console.WriteLine("Building options here!"); return [ ImportOption.CreateAsMap(new("normal", "normal", PropertyHint.None, "Normal Map Data Field Name")), ImportOption.CreateAsMap(new("diffuse", "diffuse", PropertyHint.None, "Diffuse Map Data Field Name")), ImportOption.CreateAsMap(new("specular", "specular", PropertyHint.None, "Glossy Map Data Field Name")) ]; } private record ImportOption<[MustBeVariant] T>( string Name, T DefaultValue, [property: JsonPropertyName("property_hint")] PropertyHint PropertyHint = PropertyHint.None, [property: JsonPropertyName("hint_string")] string HintString = "", [property: JsonPropertyName("usage")] PropertyUsageFlags Usage = PropertyUsageFlags.Default ) { public static Dictionary CreateAsMap(ImportOption option) { return new Dictionary() { {"name", option.Name}, {"default_value", Variant.From(option.DefaultValue)}, {"property_hint", Variant.From(option.PropertyHint)}, {"hint_string", option.HintString}, {"usage", Variant.From(option.Usage)} }; } } public override string[] _GetBuildDependencies(string path) { using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read); var data = JsonSerializer.Deserialize>(file.GetAsText()); return data.Values.ToArray(); } public override Error _Import(string sourceFile, string savePath, Dictionary options, Array platformVariants, Array genFiles) { using var file = FileAccess.Open(sourceFile, FileAccess.ModeFlags.Read); var data = JsonSerializer.Deserialize>(file.GetAsText()); System.Collections.Generic.Dictionary resources = []; foreach(var (attributeName, path) in data) { if (attributeName == "index") continue; resources[attributeName] = ResourceLoader.Load(path); if (resources[attributeName] is null) { GD.PrintErr($"Unable to load texture with path {path}. "); } } CanvasTexture texture = new CanvasTexture(); if (options.TryGetValue("diffuse", out var key ) && resources.TryGetValue(key.AsString(), out var diffuseTexture)) texture.DiffuseTexture = diffuseTexture; if (options.TryGetValue("normal", out key ) && resources.TryGetValue(key.AsString(), out var normalTexture)) texture.NormalTexture = normalTexture; if (options.TryGetValue("specular", out key ) && resources.TryGetValue(key.AsString(), out var glossyTexture)) texture.SpecularTexture = glossyTexture; var filename = savePath + "." + _GetSaveExtension(); return ResourceSaver.Save(texture, filename); } } #endif