|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Text; |
| 7 | +using System.Linq; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Text.Json; |
| 10 | +using UndertaleModLib; |
| 11 | +using UndertaleModLib.Models; |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +string InputDirectory = ""; |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +void PrintLine(string s) => Console.WriteLine(s); |
| 22 | + |
| 23 | +string ResolveInputDirectory() |
| 24 | +{ |
| 25 | + if (!string.IsNullOrEmpty(InputDirectory) && Directory.Exists(InputDirectory)) |
| 26 | + return InputDirectory; |
| 27 | + |
| 28 | + if (string.IsNullOrEmpty(FilePath)) |
| 29 | + throw new ScriptException("No data.win file loaded. Please load a game data file first."); |
| 30 | + |
| 31 | + string dataWinDir = Path.GetDirectoryName(FilePath); |
| 32 | + string textureGroupsDir = Path.Combine(dataWinDir, "Objects", "TextureGroups"); |
| 33 | + |
| 34 | + if (Directory.Exists(textureGroupsDir)) |
| 35 | + return textureGroupsDir; |
| 36 | + |
| 37 | + throw new ScriptException($"TextureGroups directory not found at: {textureGroupsDir}\nPlease specify InputDirectory or place texture groups in an 'Objects/TextureGroups' folder next to data.win."); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +EnsureDataLoaded(); |
| 44 | + |
| 45 | +if (Data.TextureGroupInfo == null) |
| 46 | +{ |
| 47 | + PrintLine("[ImportTextureGroupInfo] This game does not support texture group information. This feature requires GameMaker 2.2.1+ (Bytecode 17+)."); |
| 48 | + return; |
| 49 | +} |
| 50 | + |
| 51 | +string textureGroupsDir = ResolveInputDirectory(); |
| 52 | +PrintLine($"[ImportTextureGroupInfo] Importing from: {textureGroupsDir}"); |
| 53 | + |
| 54 | +string[] textureGroupFiles = Directory.GetFiles(textureGroupsDir, "*.json"); |
| 55 | +if (textureGroupFiles.Length == 0) |
| 56 | +{ |
| 57 | + PrintLine("[ImportTextureGroupInfo] No texture group info JSON files found, skipping import."); |
| 58 | + return; |
| 59 | +} |
| 60 | + |
| 61 | +PrintLine($"[ImportTextureGroupInfo] Found {textureGroupFiles.Length} texture group info file(s) to import."); |
| 62 | + |
| 63 | +SetProgressBar(null, "Importing Texture Group Info", 0, textureGroupFiles.Length); |
| 64 | +StartProgressBarUpdater(); |
| 65 | + |
| 66 | +foreach (string textureGroupFile in textureGroupFiles) |
| 67 | +{ |
| 68 | + try |
| 69 | + { |
| 70 | + string jsonContent = File.ReadAllText(textureGroupFile, Encoding.UTF8); |
| 71 | + string textureGroupName = Path.GetFileNameWithoutExtension(textureGroupFile); |
| 72 | + |
| 73 | + JsonDocument jsonDoc = JsonDocument.Parse(jsonContent); |
| 74 | + JsonElement root = jsonDoc.RootElement; |
| 75 | + |
| 76 | + UndertaleTextureGroupInfo textureGroup = Data.TextureGroupInfo.FirstOrDefault(tg => tg.Name?.Content == textureGroupName); |
| 77 | + if (textureGroup == null) |
| 78 | + { |
| 79 | + textureGroup = new UndertaleTextureGroupInfo(); |
| 80 | + textureGroup.Name = Data.Strings.MakeString(textureGroupName); |
| 81 | + Data.TextureGroupInfo.Add(textureGroup); |
| 82 | + PrintLine($"[ImportTextureGroupInfo] Created new texture group info: {textureGroupName}"); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + PrintLine($"[ImportTextureGroupInfo] Updating existing texture group info: {textureGroupName}"); |
| 87 | + } |
| 88 | + |
| 89 | + UpdateTextureGroupFromJson(textureGroup, root); |
| 90 | + |
| 91 | + jsonDoc.Dispose(); |
| 92 | + IncrementProgress(); |
| 93 | + } |
| 94 | + catch (Exception ex) |
| 95 | + { |
| 96 | + PrintLine($"[ImportTextureGroupInfo] Error importing texture group info {Path.GetFileName(textureGroupFile)}: {ex.Message}"); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +await StopProgressBarUpdater(); |
| 101 | +HideProgressBar(); |
| 102 | + |
| 103 | +PrintLine("[ImportTextureGroupInfo] Texture group info import completed."); |
| 104 | + |
| 105 | +void UpdateTextureGroupFromJson(UndertaleTextureGroupInfo textureGroup, JsonElement data) |
| 106 | +{ |
| 107 | + if (data.TryGetProperty("name", out JsonElement nameElm) && nameElm.ValueKind == JsonValueKind.String) |
| 108 | + textureGroup.Name = Data.Strings.MakeString(nameElm.GetString()); |
| 109 | + |
| 110 | + if (Data.IsVersionAtLeast(2022, 9)) |
| 111 | + { |
| 112 | + if (data.TryGetProperty("directory", out JsonElement dirElm) && dirElm.ValueKind == JsonValueKind.String) |
| 113 | + textureGroup.Directory = Data.Strings.MakeString(dirElm.GetString()); |
| 114 | + |
| 115 | + if (data.TryGetProperty("extension", out JsonElement extElm) && extElm.ValueKind == JsonValueKind.String) |
| 116 | + textureGroup.Extension = Data.Strings.MakeString(extElm.GetString()); |
| 117 | + |
| 118 | + if (data.TryGetProperty("loadType", out JsonElement loadTypeElm) && loadTypeElm.ValueKind == JsonValueKind.Number) |
| 119 | + textureGroup.LoadType = (UndertaleTextureGroupInfo.TextureGroupLoadType)loadTypeElm.GetInt32(); |
| 120 | + } |
| 121 | + |
| 122 | + if (data.TryGetProperty("texturePages", out JsonElement texPagesElm) && texPagesElm.ValueKind == JsonValueKind.Array) |
| 123 | + { |
| 124 | + textureGroup.TexturePages.Clear(); |
| 125 | + foreach (JsonElement texPageElm in texPagesElm.EnumerateArray()) |
| 126 | + { |
| 127 | + if (texPageElm.ValueKind == JsonValueKind.String) |
| 128 | + { |
| 129 | + string texPageName = texPageElm.GetString(); |
| 130 | + if (!string.IsNullOrEmpty(texPageName)) |
| 131 | + { |
| 132 | + var texPage = Data.EmbeddedTextures.FirstOrDefault(t => t.Name?.Content == texPageName); |
| 133 | + if (texPage != null) |
| 134 | + textureGroup.TexturePages.Add(texPage); |
| 135 | + else |
| 136 | + PrintLine($"[ImportTextureGroupInfo] Warning: Texture page '{texPageName}' not found in game data."); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (data.TryGetProperty("sprites", out JsonElement spritesElm) && spritesElm.ValueKind == JsonValueKind.Array) |
| 143 | + { |
| 144 | + textureGroup.Sprites.Clear(); |
| 145 | + foreach (JsonElement spriteElm in spritesElm.EnumerateArray()) |
| 146 | + { |
| 147 | + if (spriteElm.ValueKind == JsonValueKind.String) |
| 148 | + { |
| 149 | + string spriteName = spriteElm.GetString(); |
| 150 | + if (!string.IsNullOrEmpty(spriteName)) |
| 151 | + { |
| 152 | + var sprite = Data.Sprites.ByName(spriteName); |
| 153 | + if (sprite != null) |
| 154 | + textureGroup.Sprites.Add(sprite); |
| 155 | + else |
| 156 | + PrintLine($"[ImportTextureGroupInfo] Warning: Sprite '{spriteName}' not found in game data."); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if (!Data.IsNonLTSVersionAtLeast(2023, 1)) |
| 163 | + { |
| 164 | + if (data.TryGetProperty("spineSprites", out JsonElement spineSpritesElm) && spineSpritesElm.ValueKind == JsonValueKind.Array) |
| 165 | + { |
| 166 | + textureGroup.SpineSprites.Clear(); |
| 167 | + foreach (JsonElement spineSpriteElm in spineSpritesElm.EnumerateArray()) |
| 168 | + { |
| 169 | + if (spineSpriteElm.ValueKind == JsonValueKind.String) |
| 170 | + { |
| 171 | + string spineSpriteName = spineSpriteElm.GetString(); |
| 172 | + if (!string.IsNullOrEmpty(spineSpriteName)) |
| 173 | + { |
| 174 | + var spineSprite = Data.Sprites.ByName(spineSpriteName); |
| 175 | + if (spineSprite != null) |
| 176 | + textureGroup.SpineSprites.Add(spineSprite); |
| 177 | + else |
| 178 | + PrintLine($"[ImportTextureGroupInfo] Warning: Spine sprite '{spineSpriteName}' not found in game data."); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if (data.TryGetProperty("fonts", out JsonElement fontsElm) && fontsElm.ValueKind == JsonValueKind.Array) |
| 186 | + { |
| 187 | + textureGroup.Fonts.Clear(); |
| 188 | + foreach (JsonElement fontElm in fontsElm.EnumerateArray()) |
| 189 | + { |
| 190 | + if (fontElm.ValueKind == JsonValueKind.String) |
| 191 | + { |
| 192 | + string fontName = fontElm.GetString(); |
| 193 | + if (!string.IsNullOrEmpty(fontName)) |
| 194 | + { |
| 195 | + var font = Data.Fonts.ByName(fontName); |
| 196 | + if (font != null) |
| 197 | + textureGroup.Fonts.Add(font); |
| 198 | + else |
| 199 | + PrintLine($"[ImportTextureGroupInfo] Warning: Font '{fontName}' not found in game data."); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + if (data.TryGetProperty("tilesets", out JsonElement tilesetsElm) && tilesetsElm.ValueKind == JsonValueKind.Array) |
| 206 | + { |
| 207 | + textureGroup.Tilesets.Clear(); |
| 208 | + foreach (JsonElement tilesetElm in tilesetsElm.EnumerateArray()) |
| 209 | + { |
| 210 | + if (tilesetElm.ValueKind == JsonValueKind.String) |
| 211 | + { |
| 212 | + string tilesetName = tilesetElm.GetString(); |
| 213 | + if (!string.IsNullOrEmpty(tilesetName)) |
| 214 | + { |
| 215 | + var tileset = Data.Backgrounds.ByName(tilesetName); |
| 216 | + if (tileset != null) |
| 217 | + textureGroup.Tilesets.Add(tileset); |
| 218 | + else |
| 219 | + PrintLine($"[ImportTextureGroupInfo] Warning: Tileset '{tilesetName}' not found in game data."); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments