diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-09-02 13:16:26 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-09-02 13:16:26 +0300 |
commit | ee4f2984a63e73940d6d729a9ffe3211dc3b68ad (patch) | |
tree | 99fbc186f6f5e756816da7f191f2a024192d6b04 | |
parent | a23e7cf3a06901565fd664b9609b1373d184a054 (diff) | |
download | evol-tools-ee4f2984a63e73940d6d729a9ffe3211dc3b68ad.tar.gz evol-tools-ee4f2984a63e73940d6d729a9ffe3211dc3b68ad.tar.bz2 evol-tools-ee4f2984a63e73940d6d729a9ffe3211dc3b68ad.tar.xz evol-tools-ee4f2984a63e73940d6d729a9ffe3211dc3b68ad.zip |
add map cache tool for extract mapcache.dat.
-rw-r--r-- | mapcache/.gitignore | 3 | ||||
l--------- | mapcache/cachetotmx.py | 1 | ||||
l--------- | mapcache/extract.py | 1 | ||||
l--------- | mapcache/list.py | 1 | ||||
-rwxr-xr-x | mapcache/maptool.py | 169 | ||||
-rw-r--r-- | mapcache/templates/collision.png | bin | 0 -> 275 bytes | |||
-rw-r--r-- | mapcache/templates/example.tmx | 38 | ||||
-rw-r--r-- | mapcache/templates/template.tmx | 20 | ||||
-rw-r--r-- | mapcache/templates/tileset.png | bin | 0 -> 1132 bytes |
9 files changed, 233 insertions, 0 deletions
diff --git a/mapcache/.gitignore b/mapcache/.gitignore new file mode 100644 index 0000000..9092a2d --- /dev/null +++ b/mapcache/.gitignore @@ -0,0 +1,3 @@ +clientdata +maps +serverdata diff --git a/mapcache/cachetotmx.py b/mapcache/cachetotmx.py new file mode 120000 index 0000000..df2b3e6 --- /dev/null +++ b/mapcache/cachetotmx.py @@ -0,0 +1 @@ +maptool.py
\ No newline at end of file diff --git a/mapcache/extract.py b/mapcache/extract.py new file mode 120000 index 0000000..df2b3e6 --- /dev/null +++ b/mapcache/extract.py @@ -0,0 +1 @@ +maptool.py
\ No newline at end of file diff --git a/mapcache/list.py b/mapcache/list.py new file mode 120000 index 0000000..df2b3e6 --- /dev/null +++ b/mapcache/list.py @@ -0,0 +1 @@ +maptool.py
\ No newline at end of file diff --git a/mapcache/maptool.py b/mapcache/maptool.py new file mode 100755 index 0000000..b145044 --- /dev/null +++ b/mapcache/maptool.py @@ -0,0 +1,169 @@ +#! /usr/bin/env python +# -*- coding: utf8 -*- +# +# Copyright (C) 2014 Evol Online +# Author: Andrei Karas (4144) + +import array +import base64 +import gzip +import os +import re +import datetime +import xml +import csv +import ogg.vorbis +import StringIO +import sys +import zlib +import struct +import shutil + +def detectCommand(): + if sys.argv[0][-8:] == "/list.py": + return "list" + elif sys.argv[0][-11:] == "/extract.py": + return "extract" + elif sys.argv[0][-14:] == "/cachetotmx.py": + return "cachetotmx" + return "help" + +def makeDir(path): + if not os.path.exists(path): + os.makedirs(path) + +def readInt32(f): + data = f.read(4) + arr = array.array("I") + arr.fromstring(data) + return arr[0] + +def readInt16(f): + data = f.read(2) + arr = array.array("H") + arr.fromstring(data) + return arr[0] + +def readMapName(f): + data = f.read(12) + data = str(data) + while data[-1] == '\x00': + data = data[:-1] + return data + +def readData(f, sz): + return f.read(sz) + +def readFile(path): + with open(path, "r") as f: + return f.read() + +def getTile(mapData, x, y, sx): + data = mapData[y * sx + x] + arr = array.array("B") + arr.fromstring(data) + return arr[0] + +def getGroundTile(flag): + return str(flag + 1) + +def getCollisionTile(flag): + if flag == 0: + return "0" + return "4" + +def copyFile(src, dst, name): + shutil.copyfile(src + name, dst + name) + +def saveFile(fileName, data): + with open(fileName, "w") as w: + w.write(data) + +def printHelp(): + print "Unknown options selected." + print "" + print "Use list.py for list maps in cache" + print "Use extract.py to extract maps from cache" + exit(0) + +def listMapCache(f, mapsCount): + print "Known maps:" + print "{0:12} {1:<4}x {2:<4} {3:<10}".format("Map name", "sx", "sy", "compressed size") + for i in xrange(0, mapsCount): + name = readMapName(f) + sx = readInt16(f) + sy = readInt16(f) + sz = readInt32(f) + print "{0:12} {1:<4}x {2:<4} {3:<10}".format(name, sx, sy, sz) + f.seek(sz, 1) + +def extractMaps(f, mapsCount): + destDir = "maps/" + makeDir(destDir) + for i in xrange(0, mapsCount): + name = readMapName(f) + sx = readInt16(f) + sy = readInt16(f) + sz = readInt32(f) + data = readData(f, sz) + dc = zlib.decompressobj() + data = dc.decompress(data) + with open(destDir + name, "wb") as w: + w.write(struct.pack("H", sx)) + w.write(struct.pack("H", sy)) + w.write(data) + +def covertToTmx(f, mapsCount): + destDir = "clientdata/" + mapsDir = destDir + "maps/" + tilesetsDir = destDir + "graphics/tilesets/" + templatesDir = "templates/" + makeDir(mapsDir) + makeDir(tilesetsDir) + copyFile(templatesDir, tilesetsDir, "collision.png") + copyFile(templatesDir, tilesetsDir, "tileset.png") + tmx = readFile("templates/template.tmx") + for i in xrange(0, mapsCount): + name = readMapName(f) + print "converting map [{0:4}/{1:4}]: {2}".format(i, mapsCount + 1, name) + sx = readInt16(f) + sy = readInt16(f) + sz = readInt32(f) + mapData = readData(f, sz) + dc = zlib.decompressobj() + mapData = dc.decompress(mapData) + ground = "" + collision = "" + for y in xrange(0, sy): + for x in xrange(0, sx): + tile = getTile(mapData, x, y, sx) + if x + 1 == sy and y + 1 == sy: + ground = ground + getGroundTile(tile) + collision = collision + getCollisionTile(tile) + else: + ground = ground + getGroundTile(tile) + "," + collision = collision + getCollisionTile(tile) + "," + ground = ground + "\n" + collision = collision + "\n" + saveFile(mapsDir + name + ".tmx", tmx.format(sx, sy, ground, collision)) + +def readMapCache(path, cmd): + if cmd == "help": + printHelp() + with open(path, "rb") as f: + size = readInt32(f) + if os.path.getsize(path) != size: + print "Map cache corrupted, wrong file size." + exit(1) + mapsCount = readInt16(f) + print "Maps count: " + str(mapsCount) + readInt16(f) # padding + if cmd == "list": + listMapCache(f, mapsCount) + elif cmd == "extract": + extractMaps(f, mapsCount) + elif cmd == "cachetotmx": + covertToTmx(f, mapsCount) + + +readMapCache("serverdata/db/re/map_cache.dat", detectCommand()) diff --git a/mapcache/templates/collision.png b/mapcache/templates/collision.png Binary files differnew file mode 100644 index 0000000..f360c63 --- /dev/null +++ b/mapcache/templates/collision.png diff --git a/mapcache/templates/example.tmx b/mapcache/templates/example.tmx new file mode 100644 index 0000000..1d14957 --- /dev/null +++ b/mapcache/templates/example.tmx @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd"> +<map version="1.0" orientation="orthogonal" width="10" height="10" tilewidth="32" tileheight="32"> + <tileset firstgid="1" name="tiles" tilewidth="32" tileheight="32"> + <image source="tileset.png" width="64" height="32"/> + </tileset> + <tileset firstgid="3" name="Collision" tilewidth="32" tileheight="32"> + <image source="collision.png" width="64" height="32"/> + </tileset> + <layer name="ground" width="10" height="10"> + <data encoding="csv"> +1,2,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0 +</data> + </layer> + <layer name="Collision" width="10" height="10"> + <data encoding="csv"> +4,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0 +</data> + </layer> +</map> diff --git a/mapcache/templates/template.tmx b/mapcache/templates/template.tmx new file mode 100644 index 0000000..78a319f --- /dev/null +++ b/mapcache/templates/template.tmx @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd"> +<map version="1.0" orientation="orthogonal" width="{0}" height="{1}" tilewidth="32" tileheight="32"> + <tileset firstgid="1" name="tiles" tilewidth="32" tileheight="32"> + <image source="../graphics/tilesets/tileset.png" width="64" height="32"/> + </tileset> + <tileset firstgid="3" name="Collision" tilewidth="32" tileheight="32"> + <image source="../graphics/tilesets/collision.png" width="64" height="32"/> + </tileset> + <layer name="ground" width="{0}" height="{1}"> + <data encoding="csv"> +{2} +</data> + </layer> + <layer name="Collision" width="{0}" height="{1}"> + <data encoding="csv"> +{3} +</data> + </layer> +</map> diff --git a/mapcache/templates/tileset.png b/mapcache/templates/tileset.png Binary files differnew file mode 100644 index 0000000..cee4071 --- /dev/null +++ b/mapcache/templates/tileset.png |