├── .gitignore ├── .jshintrc ├── .travis.yml ├── Gruntfile.js ├── README.md ├── index.js ├── package.json ├── src ├── Metadata.js ├── Sprites.js └── ThingType.js └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | *.log 3 | log 4 | *~ 5 | .DS_Store 6 | out/ 7 | *.dat 8 | *.spr -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "immed": true, 5 | "latedef": true, 6 | "newcap": true, 7 | "noarg": true, 8 | "sub": true, 9 | "undef": true, 10 | "unused": true, 11 | "boss": true, 12 | "eqnull": true, 13 | "node": true 14 | } 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function (grunt) { 3 | // Show elapsed time at the end 4 | require('time-grunt')(grunt); 5 | // Load all grunt tasks 6 | require('load-grunt-tasks')(grunt); 7 | 8 | grunt.initConfig({ 9 | jshint: { 10 | options: { 11 | jshintrc: '.jshintrc', 12 | reporter: require('jshint-stylish') 13 | }, 14 | gruntfile: { 15 | src: ['Gruntfile.js'] 16 | }, 17 | js: { 18 | src: ['src/**/*.js', 'index.js'] 19 | }, 20 | test: { 21 | src: ['test/**/*.js'] 22 | } 23 | }, 24 | mochacli: { 25 | options: { 26 | reporter: 'nyan', 27 | bail: true 28 | }, 29 | all: ['test/*.js'] 30 | }, 31 | watch: { 32 | gruntfile: { 33 | files: '<%= jshint.gruntfile.src %>', 34 | tasks: ['jshint:gruntfile'] 35 | }, 36 | js: { 37 | files: '<%= jshint.js.src %>', 38 | tasks: ['jshint:js', 'mochacli'] 39 | }, 40 | test: { 41 | files: '<%= jshint.test.src %>', 42 | tasks: ['jshint:test', 'mochacli'] 43 | } 44 | } 45 | }); 46 | 47 | grunt.registerTask('default', ['jshint', 'mochacli']); 48 | }; 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sprite Sheet Extractor 2 | # [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-url]][daviddm-image] [![Code Climate][coverge-image]][coverge-url] [![Test Coverge][test-image]][coverge-url] 3 | 4 | > CLI Tool to extract SpriteSheet from Tibia Spr & Dat files 5 | 6 | ## Getting Started 7 | 8 | 1. Clone this repo. 9 | 2. Install dependencies using `npm install` 10 | 3. Just type `node index.js [Output Dir]` 11 | 4. Check out a `out` folder 12 | 13 | ## Supported Client Versions 14 | * 10.57 or greater 15 | 16 | ## Contributing 17 | Please submit all issues and pull requests to the [TibiaJS/spritesheet-extractor](https://github.com/TibiaJS/spritesheet-extractor) repository! 18 | 19 | ## Support 20 | If you have any problem or suggestion please open an issue [here](https://github.com/TibiaJS/spritesheet-extractor/issues). 21 | 22 | ## License 23 | 24 | The BSD License 25 | 26 | Copyright (c) 2015, TibiaJS Community 27 | 28 | All rights reserved. 29 | 30 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 31 | 32 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 33 | * 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. 34 | *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. 35 | 36 | 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. 37 | 38 | 39 | [npm-url]: https://npmjs.org/package/tibia-spritesheet-extractor 40 | [npm-image]: https://badge.fury.io/js/tibia-spritesheet-extractor.svg 41 | [travis-url]: https://travis-ci.org/TibiaJS/spritesheet-extractor 42 | [travis-image]: https://travis-ci.org/TibiaJS/spritesheet-extractor.svg?branch=master 43 | [daviddm-url]: https://david-dm.org/TibiaJS/spritesheet-extractor.svg?theme=shields.io 44 | [daviddm-image]: https://david-dm.org/TibiaJS/spritesheet-extractor 45 | [test-image]: https://codeclimate.com/github/TibiaJS/spritesheet-extractor/badges/coverage.svg 46 | [coverge-image]: https://codeclimate.com/github/TibiaJS/spritesheet-extractor/badges/gpa.svg 47 | [coverge-url]: https://codeclimate.com/github/TibiaJS/spritesheet-extractor -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | 4 | // Process 5 | var datFile = process.argv[2], 6 | sprFile = process.argv[3], 7 | category = process.argv[4], 8 | id = process.argv[5], 9 | outDir = process.argv[6]; 10 | 11 | // Require 12 | var fs = require('fs'), 13 | PNGImage = require('pngjs-image'), 14 | Metadata = require('./src/Metadata'), 15 | Sprites = require('./src/Sprites'); 16 | 17 | // Checking extension of datFile. 18 | if(datFile.substring((datFile.length - 4), datFile.length) !== '.dat') { 19 | throw new Error('Only .dat is allowed for the first argument!'); 20 | } 21 | 22 | // Checking if the dat file exists. 23 | if(!fs.existsSync(datFile)) { 24 | throw new Error('File not found: ' + datFile); 25 | } 26 | 27 | // Checking if extension of sprFile. 28 | if(sprFile.substring((sprFile.length - 4), sprFile.length) !== '.spr') { 29 | throw new Error('Only .spr is allowed for the second argument!'); 30 | } 31 | 32 | // Checking if the spr file exists. 33 | if(!fs.existsSync(sprFile)) { 34 | throw new Error('File not found: ' + sprFile); 35 | } 36 | 37 | // Checking if the last argument passed otherwise set to default output. 38 | if(!outDir) { 39 | outDir = './out/'; 40 | } else if (outDir.charAt(outDir.length-1) !== '/') { // Check if last char is '/' 41 | outDir = outDir + '/'; 42 | } 43 | 44 | if(!fs.existsSync(outDir)) { 45 | fs.mkdirSync(outDir); 46 | } 47 | 48 | // Checking if is a valid category. 49 | //if(category != 'item' || category != 'outfit' || category != 'effect' || category != 'missile') { 50 | //throw new Error('Invalid category: ' + category); 51 | //} 52 | 53 | var getTextureIndex = function(group, l, x, y, z, f) { 54 | return (((f % group.frames * group.patternZ + z) * group.patternY + y) * group.patternX + x) * group.layers + l; 55 | }; 56 | 57 | var getSpriteIndex = function(group, w, h, l, x, y, z, f) { 58 | return ((((((f % group.frames) * group.patternZ + z) * group.patternY + y) * group.patternX + x) * group.layers + l) * group.height + h) * group.width + w; 59 | }; 60 | 61 | var createSpriteSheet = function(frameGroup, spr) { 62 | // Measures and creates the image. 63 | var size = 32, 64 | totalX = frameGroup.patternZ * frameGroup.patternX * frameGroup.layers, 65 | totalY = frameGroup.frames * frameGroup.patternY, 66 | bitmapWidth = (totalX * frameGroup.width) * size, 67 | bitmapHeight = (totalY * frameGroup.height) * size, 68 | pixelsWidth = frameGroup.width * size, 69 | pixelsHeight = frameGroup.height * size, 70 | image = PNGImage.createImage(bitmapWidth, bitmapHeight); 71 | 72 | for (var f = 0; f < frameGroup.frames; f++) { 73 | for (var z = 0; z < frameGroup.patternZ; z++) { 74 | for (var y = 0; y < frameGroup.patternY; y++) { 75 | for (var x = 0; x < frameGroup.patternX; x++) { 76 | for (var l = 0; l < frameGroup.layers; l++) { 77 | 78 | var index = getTextureIndex(frameGroup, l, x, y, z, f); 79 | var fx = (index % totalX) * pixelsWidth; 80 | var fy = Math.floor(index / totalX) * pixelsHeight; 81 | 82 | for (var w = 0; w < frameGroup.width; w++) { 83 | for (var h = 0; h < frameGroup.height; h++) { 84 | 85 | index = getSpriteIndex(frameGroup, w, h, l, x, y, z, f); 86 | var px = ((frameGroup.width - w - 1) * size); 87 | var py = ((frameGroup.height - h - 1) * size); 88 | spr.copyPixels(frameGroup.sprites[index], image, px + fx, py + fy); 89 | } 90 | } 91 | } 92 | } 93 | } 94 | } 95 | } 96 | return image; 97 | }; 98 | 99 | var metadata = new Metadata(); 100 | metadata.load(datFile, function(dat) { 101 | if (!dat.hasThingType(category, id)) { 102 | var maxid = dat.getMaxId(category); 103 | console.log('Invalid ' + category + ' id ' + id + '. The max ' + category + ' id is ' + maxid + '.'); 104 | return; 105 | } 106 | 107 | var sprites = new Sprites(); 108 | sprites.load(sprFile, function (spr) { 109 | var thing = dat.getThingType(category, id); 110 | for (var i = 0; i < thing.groups.length; i++) { 111 | var group = thing.getFrameGroup(i); 112 | if (group != null) { 113 | var fileName = category + '_' + id; 114 | if (category === 'outfit') { 115 | if (i === 0) { 116 | fileName = 'idle_' + fileName; 117 | } 118 | else { 119 | fileName = 'walking_' + fileName; 120 | } 121 | } 122 | var image = createSpriteSheet(group, spr); 123 | image.writeImage(outDir + fileName + '.png'); 124 | } 125 | } 126 | }); 127 | }); 128 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tibia-spritesheet-extractor", 3 | "version": "0.0.3", 4 | "description": "CLI Tool to extract SpriteSheet from Tibia Spr & Dat files", 5 | "homepage": "https://github.com/TibiaJS/spritesheet-extractor", 6 | "author": { 7 | "name": "Nailson Santos", 8 | "email": "nailsonnego@gmail.com", 9 | "url": "https://github.com/Mignari" 10 | }, 11 | "repository": "TibiaJS/tibia-spritesheet-extractor", 12 | "license": "BSD", 13 | "keywords": [ 14 | "tibia-spritesheet-extractor", 15 | "tibia", 16 | "spritesheet", 17 | "extractor", 18 | "cipsoft", 19 | "tibiajs" 20 | ], 21 | "dependencies": { 22 | "buffer-reader": "0.0.2", 23 | "pngjs-image": "^0.9.3" 24 | }, 25 | "devDependencies": { 26 | "grunt-cli": "^0.1.13", 27 | "grunt-contrib-jshint": "^0.10.0", 28 | "grunt-contrib-nodeunit": "^0.4.1", 29 | "grunt-contrib-watch": "^0.6.1", 30 | "load-grunt-tasks": "^1.0.0", 31 | "time-grunt": "^1.0.0", 32 | "grunt-mocha-cli": "^1.11.0", 33 | "jshint-stylish": "^1.0.0" 34 | }, 35 | "scripts": { 36 | "test": "grunt" 37 | }, 38 | "bin": { 39 | "tibia-spritesheet-extractor": "index.js" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Metadata.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var fs = require('fs'); 4 | var BufferReader = require('buffer-reader'); 5 | var ThingType = require("./ThingType.js"); 6 | var reader; 7 | 8 | function Metadata() { 9 | } 10 | 11 | Metadata.prototype.load = function(file, cb) { 12 | // Checking if file exists 13 | if(!fs.existsSync(file)) { 14 | throw new Error('File not found: ' + file); 15 | } 16 | 17 | // Setupping variables 18 | this.signature = 0; 19 | this.items = []; 20 | this.outfits = []; 21 | this.effects = []; 22 | this.missiles = []; 23 | this.itemCount = 0; 24 | this.outfitCount = 0; 25 | this.effectCount = 0; 26 | this.missileCount = 0; 27 | 28 | var self = this; 29 | fs.readFile(file, function(err, buffer) { 30 | if (err) { throw err; } 31 | 32 | self._readLists(buffer); 33 | 34 | cb(self); 35 | }); 36 | }; 37 | 38 | Metadata.prototype.getSignature = function() { 39 | return this.signature; 40 | }; 41 | 42 | Metadata.prototype.getItemCount = function() { 43 | return this.itemCount; 44 | }; 45 | 46 | Metadata.prototype.getOutfitCount = function() { 47 | return this.outfitCount; 48 | }; 49 | 50 | Metadata.prototype.getEffectCount = function() { 51 | return this.effectCount; 52 | }; 53 | 54 | Metadata.prototype.getMissileCount = function() { 55 | return this.missileCount; 56 | }; 57 | 58 | Metadata.prototype.hasThingType = function(category, id) { 59 | if (category === 'item') { 60 | return (id >= 100 && id <= this.itemCount); 61 | } else if (category === 'outfit') { 62 | return (id >= 1 && id <= this.outfitCount); 63 | } else if (category === 'effect') { 64 | return (id >= 1 && id <= this.effectCount); 65 | } else if (category === 'missile') { 66 | return (id >= 1 && id <= this.missileCount); 67 | } 68 | return false; 69 | }; 70 | 71 | Metadata.prototype.getThingType = function(category, id) { 72 | if (category === 'item') { 73 | return this.getItem(id); 74 | } else if (category === 'outfit') { 75 | return this.getOutfit(id); 76 | } else if (category === 'effect') { 77 | return this.getEffect(id); 78 | } else if (category === 'missile') { 79 | return this.getMissile(id); 80 | } 81 | return null; 82 | }; 83 | 84 | Metadata.prototype.getMinId = function(category) { 85 | if (category === 'item') { 86 | return 100; 87 | } 88 | return 1; 89 | }; 90 | 91 | Metadata.prototype.getMaxId = function(category) { 92 | if (category === 'item') { 93 | return this.itemCount; 94 | } else if (category === 'outfit') { 95 | return this.outfitCount; 96 | } else if (category === 'effect') { 97 | return this.effectCount; 98 | } else if (category === 'missile') { 99 | return this.missileCount; 100 | } 101 | return 0; 102 | }; 103 | 104 | Metadata.prototype.getItem = function(id) { 105 | if (id >= 100 && id <= this.itemCount) { 106 | return this.items[id]; 107 | } 108 | return null; 109 | }; 110 | 111 | Metadata.prototype.getOutfit = function(id) { 112 | if (id >= 1 && id <= this.outfitCount) { 113 | return this.outfits[id]; 114 | } 115 | return null; 116 | }; 117 | 118 | Metadata.prototype.getEffect = function(id) { 119 | if (id >= 1 && id <= this.effectCount) { 120 | return this.effects[id]; 121 | } 122 | return null; 123 | }; 124 | 125 | Metadata.prototype.getMissile = function(id) { 126 | if (id >= 1 && id <= this.missileCount) { 127 | return this.missiles[id]; 128 | } 129 | return null; 130 | }; 131 | 132 | Metadata.prototype._readLists = function(buffer) { 133 | reader = new BufferReader(buffer); 134 | this.signature = reader.nextUInt32LE(); 135 | this.itemCount = reader.nextUInt16LE(); 136 | this.outfitCount = reader.nextUInt16LE(); 137 | this.effectCount = reader.nextUInt16LE(); 138 | this.missileCount = reader.nextUInt16LE(); 139 | 140 | var id; 141 | for (id = 100; id <= this.itemCount; id++) { 142 | this.items[id] = this._readThing(id, 'item'); 143 | } 144 | 145 | for (id = 1; id <= this.outfitCount; id++) { 146 | this.outfits[id] = this._readThing(id, 'outfit'); 147 | } 148 | 149 | for (id = 1; id <= this.effectCount; id++) { 150 | this.effects[id] = this._readThing(id, 'effect'); 151 | } 152 | 153 | for (id = 1; id <= this.missileCount; id++) { 154 | this.missiles[id] = this._readThing(id, 'missile'); 155 | } 156 | }; 157 | 158 | Metadata.prototype._readThing = function(id, category) { 159 | 160 | var thing = new ThingType(id, category); 161 | var flag; 162 | 163 | do 164 | { 165 | flag = reader.nextUInt8(); 166 | if (flag === 0xFF) { 167 | break; 168 | } 169 | 170 | switch (flag) { 171 | case 0x00: // Is ground 172 | case 0x08: // Writable 173 | case 0x09: // Writable once 174 | case 0x1A: // Has elevation 175 | case 0x1D: // Minimap 176 | case 0x1E: // Lens help 177 | case 0x21: // Cloth 178 | case 0x23: // Default action 179 | { 180 | reader.move(2); 181 | break; 182 | } 183 | 184 | case 0x16: // Has light 185 | case 0x19: // Has offset 186 | { 187 | reader.move(4); 188 | break; 189 | } 190 | 191 | case 0x22: // Market 192 | { 193 | reader.move(6); 194 | var length = reader.nextUInt16LE(); 195 | reader.move(length + 4); 196 | break; 197 | } 198 | } 199 | } while (flag !== 0xFF); 200 | 201 | var isOutfit = category === 'outfit'; 202 | var groupCount = 1; 203 | if (isOutfit) { 204 | groupCount = reader.nextUInt8(); 205 | } 206 | 207 | for (var k = 0; k < groupCount; k++) { 208 | var groupType = 0; 209 | if (isOutfit) { 210 | groupType = reader.nextUInt8(); 211 | } 212 | 213 | var group = { 214 | width : 0, 215 | height : 0, 216 | layers : 0, 217 | patternX : 0, 218 | patternY : 0, 219 | patternZ : 0, 220 | frames : 0, 221 | sprites : [] 222 | }; 223 | 224 | group.width = reader.nextUInt8(); 225 | group.height = reader.nextUInt8(); 226 | 227 | // Skipping exact size 228 | if (group.width > 1 || group.height > 1) { 229 | reader.move(1); 230 | } 231 | 232 | group.layers = reader.nextUInt8(); 233 | group.patternX = reader.nextUInt8(); 234 | group.patternY = reader.nextUInt8(); 235 | group.patternZ = reader.nextUInt8(); 236 | group.frames = reader.nextUInt8(); 237 | 238 | // Skipping frame durations 239 | if (group.frames > 1) { 240 | reader.move(6 + (8 * group.frames)); 241 | } 242 | 243 | var totalSprites = group.width * 244 | group.height * 245 | group.layers * 246 | group.patternX * 247 | group.patternY * 248 | group.patternZ * 249 | group.frames; 250 | 251 | if (totalSprites > 4096) { 252 | throw new Error('A thing type has more than 4096 sprites.'); 253 | } 254 | 255 | for (var i = 0; i < totalSprites; i++) { 256 | group.sprites[i] = reader.nextUInt32LE(); 257 | } 258 | thing.groups[groupType] = group; 259 | } 260 | return thing; 261 | }; 262 | 263 | module.exports = Metadata; 264 | -------------------------------------------------------------------------------- /src/Sprites.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var fs = require('fs'); 4 | var BufferReader = require('buffer-reader'); 5 | var reader; 6 | 7 | function Sprites() { 8 | } 9 | 10 | Sprites.prototype.load = function(file, cb) { 11 | // Checking if file exists 12 | if(!fs.existsSync(file)) { 13 | throw new Error('File not found: ' + file); 14 | } 15 | 16 | // Setupping variables 17 | this.signature = 0; 18 | this.spriteCount = 0; 19 | 20 | var self = this; 21 | fs.readFile(file, function(err, buffer) { 22 | if (err) { throw err; } 23 | 24 | reader = new BufferReader(buffer); 25 | self.signature = reader.nextUInt32LE(); 26 | self.spriteCount = reader.nextUInt32LE(); 27 | 28 | cb(self); 29 | }); 30 | }; 31 | 32 | Sprites.prototype.getSignature = function() { 33 | return this.signature; 34 | }; 35 | 36 | Sprites.prototype.getSpriteCount = function() { 37 | return this.spriteCount; 38 | }; 39 | 40 | Sprites.prototype.copyPixels = function(spriteId, image, x, y) { 41 | var formula = 8 + (spriteId - 1) * 4; 42 | reader.seek(formula); 43 | 44 | var address = reader.nextUInt32LE(); 45 | if (address === 0) { // Address 0 always is an empty sprite. 46 | return; 47 | } 48 | reader.seek(address); 49 | 50 | // Skipping color key. 51 | reader.move(3); 52 | 53 | var pixelDataSize = reader.nextUInt16LE(); 54 | if (pixelDataSize === 0) { return; } 55 | 56 | var size = 32, 57 | read = 0, 58 | currentPixel = 0, 59 | color = {red:0, green:0, blue:0, alpha:0}; 60 | 61 | for (var px = 0; px < size; px++) { 62 | for (var py = 0; py < size; py++) { 63 | image.setPixel(px + x, py + y, color); 64 | } 65 | } 66 | 67 | color.alpha = 255; 68 | 69 | while(read < pixelDataSize) { 70 | var transparentPixels = reader.nextUInt16LE(), 71 | coloredPixels = reader.nextUInt16LE(); 72 | currentPixel += transparentPixels; 73 | for (var i = 0; i < coloredPixels; i++) { 74 | color.red = reader.nextUInt8(); 75 | color.green = reader.nextUInt8(); 76 | color.blue = reader.nextUInt8(); 77 | image.setPixel(parseInt(currentPixel % size) + x, parseInt(currentPixel / size) + y, color); 78 | currentPixel++; 79 | } 80 | read += 4 + (coloredPixels * 3); 81 | } 82 | }; 83 | 84 | module.exports = Sprites; 85 | -------------------------------------------------------------------------------- /src/ThingType.js: -------------------------------------------------------------------------------- 1 | function ThingType(id, category) { 2 | this.id = id; 3 | this.category = category; 4 | this.groups = []; 5 | } 6 | 7 | ThingType.prototype.getFrameGroup = function(type) { 8 | if (type < this.groups.length) { 9 | return this.groups[type]; 10 | } 11 | return null; 12 | }; 13 | 14 | module.exports = ThingType; 15 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | // TODO! --------------------------------------------------------------------------------