Created
September 6, 2017 14:41
-
-
Save nev3rm0re/79fcc7a1bf97f05c27505c687a8b9001 to your computer and use it in GitHub Desktop.
Couple of node scripts to convert GIF sprites to PNG format for TWoTS OpenXcom mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This scripts takes a list of filenames from `files.txt` and converts | |
* all of them to PNG format. | |
* | |
* File list can either be generated by running `extra-sprites.js` script | |
* or by using `find` utility from mod folder. | |
* ``` | |
* [OpenXcom/mods/TWoTS] > find . -iname \*.gif > files.txt | |
* ``` | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
const gm = require('gm'); | |
const base_dir = process.env['HOME'] + '/Library/Application Support/OpenXcom/mods/TWoTS/'; | |
const files = fs.readFileSync('./files.txt') | |
.toString() | |
.trim() // remove trailing whitespace | |
.split("\n"); | |
const paths = files.map((resource_path) => { | |
return path.join(base_dir, resource_path); | |
}); | |
function processImages(paths) { | |
paths.map((image_path) => { | |
const ext = path.extname(image_path); | |
const new_filename = image_path.slice(0, -ext.length) + '.png'; | |
if (!fs.existsSync(new_filename)) { | |
gm(image_path) | |
.write(new_filename, (err) => { | |
if (err) { | |
console.error("Error during write", err); | |
} | |
}); | |
} | |
}); | |
} | |
processImages(paths); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This script goes through all of the GIF filenames from ExtraSprites.rul, | |
* checks if "converted" (PNG) version of that filename exists and spits out | |
* ExtraSprites.rul content with updated filenames. | |
* | |
* If the "converted" files do not exist, original filenames will be appended to `files.txt` | |
* in script's directory. | |
*/ | |
const fs = require('fs'); | |
const yaml = require('js-yaml'); | |
const base_dir = process.env['HOME'] + '/Library/Application Support/OpenXcom/mods/TWoTS/'; | |
const extra_sprites_text = fs.readFileSync(base_dir + "Ruleset/ExtraSprites.rul.bak").toString(); | |
const extra_sprites = yaml.load(extra_sprites_text).extraSprites; | |
const resource_files = extra_sprites.reduce((carry, value) => { | |
return carry.concat(Object.values(value.files)); | |
}, []); | |
const updated_text = resource_files.reduce((config_text, resource_path) => { | |
if ( | |
fs.existsSync(base_dir + resource_path) | |
&& (/\.gif$/i).test(resource_path) | |
) { | |
const new_name = resource_path.replace(/\.gif$/i, '.png'); | |
const is_converted = fs.existsSync(base_dir + new_name); | |
if (is_converted) { | |
return config_text.replace(resource_path, new_name); | |
} else { | |
fs.appendFile("./files.txt", resource_path + "\n", (err) => {if (err) throw err}); | |
} | |
} else { | |
} | |
return config_text; | |
}, extra_sprites_text); | |
if (updated_text == extra_sprites_text) { | |
console.log("File list created"); | |
} else { | |
process.stdout.write(updated_text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment