├── .gitignore ├── README.md ├── index.js ├── multibar.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | log 4 | *~ 5 | .DS_Store 6 | out/ 7 | *.spr 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sprite Extractor 2 | 3 | JavaScript Sprite Extractor for Tibia written in NodeJS. 4 | 5 | ## Getting Started 6 | 7 | 1. Clone this repo 8 | 2. Install dependencies using `npm install` 9 | 3. Just type `node index.js [Output Dir]` 10 | 4. Check out a `out` folder 11 | 12 | ## Supported Version 13 | * 7.x 14 | * 8.x 15 | * 9.x 16 | 17 | ## Contributing 18 | Please submit all issues and pull requests to the [TibiaJS/sprites-extractor](http://github.com/TibiaJS/sprites-extractor) repository! 19 | 20 | ## Support 21 | If you have any problem or suggestion please open an issue [here](http://github.com/TibiaJS/sprites-extractor/issues). 22 | 23 | ## References 24 | * http://tpforums.org/forum/threads/5031-Tibia-Sprite-File-Structure 25 | * https://code.google.com/p/tibiaapi/source/browse/trunk/tibiaapi/Util/SpriteReader.cs 26 | 27 | ## License 28 | The BSD License 29 | 30 | Copyright (c) 2014, TibiaJS Community 31 | 32 | All rights reserved. 33 | 34 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 35 | 36 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 37 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 38 | *Neither the name of the TibiaJS Community nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 39 | 40 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs, BufferReader, PNGImage, MultiBar, mbars, bars, sprFile, baseColor, base, async, exportImg, outDir; 2 | 3 | fs = require('fs'); 4 | sprFile = process.argv[2]; 5 | outDir = process.argv[3]; 6 | 7 | // Checking if pass first argument 8 | if(!sprFile || sprFile.length < 6) { 9 | throw new Error('Missing Tibia.spr.'); 10 | } 11 | 12 | // Check if second arg passed otherwise set to default output 13 | if(!outDir) { 14 | outDir = './out/'; 15 | } 16 | 17 | // Check if last char is '/' 18 | if(outDir.charAt(outDir.length-1) !='/') { 19 | outDir = outDir + '/'; 20 | } 21 | 22 | if(!fs.existsSync(outDir)) { 23 | fs.mkdirSync(outDir); 24 | } 25 | 26 | // Checking if extension is .spr 27 | if(sprFile.substring((sprFile.length - 4), sprFile.length) != '.spr') { 28 | throw new Error('Only .spr is allowed!'); 29 | } 30 | 31 | // Checking if file exists 32 | if(!fs.existsSync(sprFile)) { 33 | throw new Error('File not found: ' + sprFile); 34 | } 35 | 36 | MultiBar = require('./multibar.js'); 37 | mbars = new MultiBar(); 38 | bars = []; 39 | BufferReader = require('buffer-reader'); 40 | PNGImage = require('pngjs-image'); 41 | async = require('async'); 42 | baseColor = {red: 255, green: 0, blue: 255, alpha: 255}; 43 | 44 | // Create a 32x32 with pink bg 45 | base = function(spriteId) { 46 | var image = PNGImage.createImage(32, 32); 47 | for(i = 0; i < 32; i++) { 48 | for(j = 0; j < 32; j++) { 49 | image.setPixel(i,j, baseColor); 50 | } 51 | } 52 | 53 | return {filename: spriteId, img: image}; 54 | } 55 | 56 | exportImg = function(obj, cb) { 57 | obj.img.writeImage(outDir + obj.filename + '.png', function() { 58 | cb(); 59 | bars[1].tick(); 60 | }); 61 | }; 62 | 63 | var queue = async.queue(exportImg, 10); 64 | 65 | queue.drain = function() { 66 | console.log("\n"); 67 | } 68 | 69 | fs.readFile(sprFile, function (err, buffer) { 70 | if (err) throw err; 71 | 72 | var reader = new BufferReader(buffer), 73 | count = 0, 74 | info = { 75 | signature: reader.nextUInt32LE(), 76 | size: reader.nextUInt16LE(), 77 | }; 78 | 79 | console.log("Signature: " + info.signature); 80 | console.log(" Sprites: " + info.size); 81 | 82 | bars.push(mbars.newBar(' Parsing: [:bar] :percent | ETA: :eta | Time Elapsed: :elapsed', { complete: '=', incomplete: ' ', clear: true, width: 40, total: info.size })); 83 | 84 | for(var spriteId = 1; spriteId < info.size; spriteId++) { 85 | 86 | var obj = base(spriteId); 87 | var formula = 6 + (spriteId - 1) * 4; 88 | reader.seek(formula); 89 | 90 | var address = reader.nextUInt32LE(); 91 | if (address == 0) { // Address 0 always is an empty sprite. 92 | bars[0].tick(); 93 | continue; 94 | } 95 | reader.seek(address); 96 | 97 | // Skipping color key. 98 | reader.move(3); 99 | 100 | var offset = reader.tell() + reader.nextUInt16LE(); 101 | 102 | var currentPixel = 0; 103 | var size = 32; 104 | while(reader.tell() < offset) { 105 | var transparentPixels = reader.nextUInt16LE(); 106 | var coloredPixels = reader.nextUInt16LE(); 107 | currentPixel += transparentPixels; 108 | for (var i = 0; i < coloredPixels; i++) 109 | { 110 | obj.img.setPixel( 111 | parseInt(currentPixel % size), 112 | parseInt(currentPixel / size), 113 | {red:reader.nextUInt8(), green:reader.nextUInt8(), blue:reader.nextUInt8(), alpha:255}); 114 | currentPixel++; 115 | } 116 | } 117 | 118 | count++; 119 | queue.push(obj); 120 | bars[0].tick(); 121 | } 122 | 123 | bars.push(mbars.newBar('Exporting: [:bar] :percent | ETA: :eta | Time Elapsed: :elapsed', { complete: '=', incomplete: ' ', clear: true, width: 40, total: count })); 124 | }); 125 | -------------------------------------------------------------------------------- /multibar.js: -------------------------------------------------------------------------------- 1 | // Reference: https://gist.github.com/nuxlli/b425344b92ac1ff99c74 2 | var ProgressBar = require('progress'); 3 | 4 | function Multibar(stream) { 5 | this.stream = stream || process.stderr; 6 | this.cursor = 0; 7 | this.bars = []; 8 | this.terminates = 0; 9 | } 10 | 11 | Multibar.prototype = { 12 | newBar: function(schema, options) { 13 | options.stream = this.stream; 14 | var bar = new ProgressBar(schema, options); 15 | this.bars.push(bar); 16 | var index = this.bars.length - 1; 17 | 18 | // alloc line 19 | this.move(index); 20 | this.stream.write('\n'); 21 | this.cursor ++; 22 | 23 | // replace original 24 | var self = this; 25 | bar.otick = bar.tick; 26 | bar.oterminate = bar.terminate; 27 | bar.tick = function(value, options) { 28 | self.tick(index, value, options); 29 | } 30 | bar.terminate = function() { 31 | self.terminates++; 32 | if (self.terminates == self.bars.length) { 33 | self.terminate(); 34 | } 35 | } 36 | 37 | return bar; 38 | }, 39 | 40 | terminate: function() { 41 | this.move(this.bars.length); 42 | this.stream.clearLine(); 43 | this.stream.cursorTo(0); 44 | }, 45 | 46 | move: function(index) { 47 | if (!this.stream.isTTY) return; 48 | this.stream.moveCursor(0, index - this.cursor); 49 | this.cursor = index; 50 | }, 51 | 52 | tick: function(index, value, options) { 53 | var bar = this.bars[index]; 54 | if (bar) { 55 | this.move(index); 56 | bar.otick(value, options); 57 | } 58 | } 59 | } 60 | 61 | module.exports = Multibar; 62 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sprites-extractor", 3 | "version": "1.2.0", 4 | "description": "JavaScript Sprite Extractor for Tibia written in NodeJS.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/TibiaJS/sprites-extractor.git" 12 | }, 13 | "keywords": [ 14 | "tibia", 15 | "sprites", 16 | "otserv", 17 | "tibia.spr", 18 | "cipsoft" 19 | ], 20 | "author": "gpedro ", 21 | "license": "BSD", 22 | "bugs": { 23 | "url": "https://github.com/TibiaJS/sprites-extractor/issues" 24 | }, 25 | "homepage": "https://github.com/TibiaJS/sprites-extractor", 26 | "dependencies": { 27 | "async": "^0.9.0", 28 | "buffer-reader": "0.0.2", 29 | "pngjs-image": "^0.9.3", 30 | "progress": "^1.1.8" 31 | } 32 | } 33 | --------------------------------------------------------------------------------