|
| 1 | +/* Add Output Index script by eishiya, last updated 15 Mar 2024 |
| 2 | +
|
| 3 | + Requires Tiled 1.10.2+ |
| 4 | +
|
| 5 | + As of Tiled 1.10.3, output layers without an index always get output. |
| 6 | + Before, such layers would simply count as another index to choose at random. |
| 7 | + This causes a potential change in behaviour for older Automapping rules. |
| 8 | + |
| 9 | + This script adds an action to the Map menu that updates old rules |
| 10 | + to work the same as they did before. |
| 11 | + For any rules map it finds that has output layers with named indices, |
| 12 | + any output layers *without* named indices get a named index. |
| 13 | + |
| 14 | + This script will attempt to fix commented-out layer names too, |
| 15 | + as long as they're otherwise valid, e.g. |
| 16 | + "//output_Blah" will get fixed, "//TODO: output_Blah" will not. |
| 17 | + |
| 18 | + The replacement index will be "0". If such an index already exists |
| 19 | + in the map, it will be "00", and so on. |
| 20 | + |
| 21 | + Any maps that are modified by the script will be left open and not saved. |
| 22 | + This makes it easier for you to verify the changes. You can Save All if you |
| 23 | + don't want to check and save each one manually. |
| 24 | +
|
| 25 | + If you have a Project loaded, the project's directories will be scanned. |
| 26 | + Otherwise, you'll be prompted to select a directory to scan. |
| 27 | + |
| 28 | + If you have many maps, this script can take a while to run! |
| 29 | + |
| 30 | + You should only need to run this script once per pre-1.10.3 Project. |
| 31 | + You can uninstall it when you're done, or comment out the last 3 lines so |
| 32 | + it doesn't clutter your Map menu, in which case you can still run the script |
| 33 | + via Search Actions. |
| 34 | +*/ |
| 35 | + |
| 36 | +let addOutputIndex = tiled.registerAction("AddOutputIndex", function(/* action */) { |
| 37 | + function fixIndex(map) { |
| 38 | + let newIndexCharacter = "0"; |
| 39 | + let newIndex = newIndexCharacter; |
| 40 | + |
| 41 | + let layersToRename = []; |
| 42 | + let hasNamedIndices = false; |
| 43 | + function checkLayer(layer) { |
| 44 | + if(!layer) return; |
| 45 | + if(layer.isGroupLayer || layer.isTileMap) { |
| 46 | + //process over its child layers recursively: |
| 47 | + for(let gi = 0; gi < layer.layerCount; ++gi) { |
| 48 | + checkLayer(layer.layerAt(gi)); |
| 49 | + } |
| 50 | + } else { //regular layer that may be an output layer |
| 51 | + let layerName = layer.name; |
| 52 | + //to fix commented-out layers, strip the comment bit: |
| 53 | + layerName.replace(/^(\/\/|#)\s*/, ''); |
| 54 | + if(layerName.indexOf("output_") == 0) //output with unnamed index |
| 55 | + layersToRename.push(layer); |
| 56 | + else { |
| 57 | + //the name has output but not output_, it must have a named output index |
| 58 | + if(layerName.indexOf("output") == 0) |
| 59 | + hasNamedIndices = true; |
| 60 | + //If our new index would conflict with an existing index, change it: |
| 61 | + while(layerName.indexOf("output"+newIndex+"_") == 0) |
| 62 | + newIndex += newIndexCharacter; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + checkLayer(map); |
| 67 | + |
| 68 | + if(hasNamedIndices && layersToRename.length > 0) { |
| 69 | + map.macro("Add Output Index", function() { |
| 70 | + for(let layer of layersToRename) |
| 71 | + layer.name = layer.name.replace("output_", "output"+newIndex+"_"); |
| 72 | + tiled.log("Renamed "+layersToRename.length+" layer(s) in "+map.fileName); |
| 73 | + }); |
| 74 | + |
| 75 | + return layersToRename.length; |
| 76 | + } |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | + let maps = []; |
| 81 | + |
| 82 | + //Helper function that returns a TileMap if it's already open |
| 83 | + function getOpenMap(file) { |
| 84 | + for(let asset of tiled.openAssets) { |
| 85 | + if(asset.fileName == file && asset.isTileMap) |
| 86 | + return asset; |
| 87 | + } |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + //Recursively add all the maps in a folder to maps |
| 92 | + let checkedFolders = {}; |
| 93 | + function collectMaps(folder) { |
| 94 | + let canonicalPath = FileInfo.canonicalPath(folder); |
| 95 | + if(checkedFolders[canonicalPath]) return; |
| 96 | + |
| 97 | + checkedFolders[canonicalPath] = true; |
| 98 | + //First, get all the files in this folder |
| 99 | + let files = File.directoryEntries(folder, File.Files | File.Readable | File.NoDotAndDotDot); |
| 100 | + for(let file of files) { |
| 101 | + let path = folder+"/"+file; |
| 102 | + let format = tiled.mapFormatForFile(path); |
| 103 | + if(format) { |
| 104 | + let map = getOpenMap(path); |
| 105 | + if(map) |
| 106 | + maps.push(map); |
| 107 | + else |
| 108 | + maps.push(path); |
| 109 | + } //else there's no map format that can read this file, it's not a Tiled map, skip it. |
| 110 | + } |
| 111 | + //Then, look at any subfolders: |
| 112 | + files = File.directoryEntries(folder, File.Dirs | File.Readable | File.NoDotAndDotDot); |
| 113 | + for(let file of files) { |
| 114 | + collectMaps(folder+"/"+file); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + let folders; |
| 119 | + if(tiled.project && tiled.projectFilePath.length > 0) |
| 120 | + folders = tiled.project.folders; |
| 121 | + else |
| 122 | + folders = [tiled.promptDirectory(null, "Select a directory to scan")]; |
| 123 | + |
| 124 | + for(let folder of folders) |
| 125 | + collectMaps(folder); |
| 126 | + |
| 127 | + let previousAsset = tiled.activeAsset; |
| 128 | + let totalLayersChanged = 0; |
| 129 | + |
| 130 | + for(let map of maps) { |
| 131 | + if(map.isTileMap) { |
| 132 | + totalLayersChanged += fixIndex(map); |
| 133 | + } else { //a path |
| 134 | + map = tiled.open(map); |
| 135 | + let layersChanged = fixIndex(map); |
| 136 | + totalLayersChanged += layersChanged; |
| 137 | + if(layersChanged == 0) //leave modified maps open for inspection |
| 138 | + tiled.close(map); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if(totalLayersChanged == 0) |
| 143 | + tiled.log("No maps were found to need output index modification. No changes made."); |
| 144 | + if(previousAsset) |
| 145 | + tiled.activeAsset = previousAsset; |
| 146 | +}); |
| 147 | +addOutputIndex.text = "Add Output Indices"; |
| 148 | + |
| 149 | +//Add this action to the Map menu: |
| 150 | +tiled.extendMenu("Map", [ |
| 151 | + { action: "AddOutputIndex", before: "AutoMapWhileDrawing" } |
| 152 | +]); |
0 commit comments