From 4acb440b3faa1d88b1be5a43fb73a1be3a63019c Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Thu, 9 Jul 2015 20:10:19 +0200 Subject: [PATCH] Updated SQLextract.py with metadata handling --- mods/u_skins/purge_skins.py | 4 +- mods/u_skins/update_from_db.py | 4 +- other_things/scripts/SQLextract.py | 279 ++++++++++++++++------------- 3 files changed, 154 insertions(+), 133 deletions(-) diff --git a/mods/u_skins/purge_skins.py b/mods/u_skins/purge_skins.py index d55a7d4c..8cfa7ab0 100755 --- a/mods/u_skins/purge_skins.py +++ b/mods/u_skins/purge_skins.py @@ -27,8 +27,8 @@ for skin in skins_exclued: sys.stderr.write("%s\n" % err) continue # for texture, preview and meta files - for f_skin in ( os.path.join(p_textures,"%s.png" % skin), - os.path.join(p_textures,"%s_preview.png" % skin), + for f_skin in ( os.path.join(p_textures,"%s.png" % skin), + os.path.join(p_textures,"%s_preview.png" % skin), os.path.join(p_meta, "%s.txt" % skin) ): if os.path.exists(f_skin): try: diff --git a/mods/u_skins/update_from_db.py b/mods/u_skins/update_from_db.py index be6930c1..71e94140 100755 --- a/mods/u_skins/update_from_db.py +++ b/mods/u_skins/update_from_db.py @@ -23,7 +23,7 @@ def addpage(page): print("Error", r.status) exit(r.status) return - + data = r.read().decode() l = json.loads(data) if not l["success"]: @@ -48,7 +48,7 @@ def addpage(page): if r.status != 200: print("Error", r.status) continue - + data = r.read() f = open(skinsdir + "character_" + str(i) + "_preview.png", "wb") f.write(data) diff --git a/other_things/scripts/SQLextract.py b/other_things/scripts/SQLextract.py index 6bfef37e..39a2d117 100755 --- a/other_things/scripts/SQLextract.py +++ b/other_things/scripts/SQLextract.py @@ -1,144 +1,163 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 # -*- coding: UTF-8 -*- - import sys, os, sqlite3 +import encodings +from cStringIO import StringIO home = os.environ.get("HOME") db = "%s/rollback/rollback.sqlite" % (home) -############# -## Node IDs -# +#[id=16000;actor=crabman;type=2;list=main,index=13,add=1, stacknode=default:glass,stackquantity=9,nodemeta=0;x=None, y=None,z=None,oldnode=,oldparam1=None,oldparam2=None,oldmeta=None:newnode=,newparam1=None,newparam2=None,newmeta=None] -def get_node_ids(): - try: - conn = sqlite3.connect(db) - cur = conn.cursor() - except sqlite3.OperationalError as err: - print(err) - sys.exit(1) - try: - cur.execute("SELECT * FROM node") - except sqlite3.OperationalError as err: - print(err) - sys.exit(1) - else: - result = cur.fetchall() - finally: - cur.close() - conn.close() - return(result) +class Convert_id(object): + def __init__(self, base): + self.__players = dict() + self.__nodes = dict() + try: + conn = sqlite3.connect(db) + cur = conn.cursor() + except Exception as err: + print(err) + print("problème avec la base de données") + sys.exit(1) + else: + try: + cur.execute("SELECT * from actor") + except sqlite3.OperationalError as err: + print(err) + else: + result = cur.fetchall() + for res in result: + self.__players[res[0]] = res[1][len("player:"):] + try: + cur.execute("SELECT * from node") + except sqlite3.OperationalError as err: + print(err) + else: + result = cur.fetchall() + for res in result: + self.__nodes[res[0]] = res[1] + finally: + cur.close() + conn.close() -node_ids = get_node_ids() - -def get_node(nodeid): - if not nodeid: - return None - else: - return node_ids[nodeid-1][1] - -############### -## Player IDs -# - -def get_player_ids(): - try: - conn = sqlite3.connect(db) - cur = conn.cursor() - except sqlite3.OperationalError as err: - print(err) - sys.exit(1) - try: - cur.execute("SELECT * FROM actor") - except sqlite3.OperationalError as err: - print(err) - sys.exit(1) - else: - result = cur.fetchall() - finally: - cur.close() - conn.close() - return result - -player_ids = get_player_ids() - -def get_player(id): - if id: - return player_ids[id-1][1][len("player:"):] - else: - return None + def get_player_name(self, player_id): + if self.__players.has_key(player_id): + return self.__players[player_id] + else: + return("") + def get_node_name(self, node_id): + if self.__nodes.has_key(node_id): + return self.__nodes[node_id] + else: + return("") ######################################################################## ## Utilities # def ston(a): - """ - Returns a string equal to a or None - """ - if a: - return str(a) - else: - return a - + """ + Returns a string equal to a or None + """ + if a: + return str(a) + else: + return "" def select_all_nodes(stamp): - try: - conn = sqlite3.connect(db) - conn.text_factory = str - cur = conn.cursor() - except sqlite3.OperationalError as err: - print(err) - sys.exit(1) - try: - cur.execute("SELECT * FROM action WHERE timestamp >=:stamp", {"stamp":stamp}) - except sqlite3.OperationalError as err: - print(err) - sys.exit(1) - else: - result = cur.fetchall() - finally: - cur.close() - conn.close() - return(result) + try: + conn = sqlite3.connect(db) + conn.text_factory = str + cur = conn.cursor() + except sqlite3.OperationalError as err: + print(err) + sys.exit(1) + try: + cur.execute("SELECT * FROM action WHERE timestamp >=:stamp", {"stamp":stamp}) + except sqlite3.OperationalError as err: + print(err) + sys.exit(1) + else: + result = cur.fetchall() + finally: + cur.close() + conn.close() + return(result) + +# Big-endian!!! +def readU16(strm): + return (ord(strm.read(1)) << 16) + (ord(strm.read(1))) + +def readU32(strm): + return (ord(strm.read(1)) << 24) + (ord(strm.read(1)) << 16) + (ord(strm.read(1)) << 8) + (ord(strm.read(1))) + +def decode(chaine): + if not chaine : + return "[]" + else: + strm = StringIO(chaine) + table = "[" + nEntries = readU32(strm) + for n in range(nEntries): + keyLen = readU16(strm) + key = strm.read(keyLen) + valLen = readU32(strm) + val = strm.read(valLen) + # Beware of potential quotes in meta, they must be escaped + # Fortunately their escape codes are the same in Python and Lua + # (and pretty much every language influenced by C) + # ------------ + # Attention aux potentiels quotes dedans les meta, il faut les escape + # Heureusement leur mode d'escape est quasi le même en Python que Lua + table += '%s="%s"' % (key, val.encode('string-escape').replace('"', '\\"')) + if n != nEntries-1: + table += ", " + table += "]" + return table + + + +def to_table(node): + # Init bracket and basic datas + table = '[' + table += 'id=%d' % node[0] + table += ', actor="%s"' % Id.get_player_name(node[1]) + table += ', type=%d' % node[3] + # Inventory Manipulation + table += ', list="%s"' % ston(node[4]) + table += ', index="%s"' % ston(node[5]) + table += ', add="%s"' % ston(node[6]) + table += ', stacknode="%s"' % Id.get_node_name(node[7]) + table += ', stackquantity="%s"' % ston(node[8]) + table += ', nodemeta=%s' % ston(node[9]) + # Position + table += ', x=%s, y=%s, z=%s' % (node[10], node[11], node[12]) + # Old node + table += ', oldnode="%s"' % Id.get_node_name(node[13]) + table += ', oldparam1="%s"' % node[14] + table += ', oldparam2="%s"' % ston(node[15]) + table += ', oldmeta=%s' % decode(node[16]) + # New node + table += ', newnode="%s"' % Id.get_node_name(node[17]) + table += ', newparam1="%s"' % ston(node[18]) + table += ', newparam2="%s"' % ston(node[19]) + table += ', newmeta=%s' % decode(node[20]) + # Ending + table += ']\n' + return (table) def write_list(nodes): - try: - name = "database-output.txt" - f = open(name, "w") - except Exception as err: - print(err) - sys.exit(1) - - for node in nodes: - # Init bracket and basic datas - f.write("[") - f.write("id=%d" % node[0]) - f.write(";actor=%s" % get_player(node[1])) - f.write(";type=%d" % node[3]) - # Inventory Manipulation - f.write(";list=%s" % ston(node[4])) - f.write(",index=%s" % ston(node[5])) - f.write(",add=%s" % ston(node[6])) - f.write(",stacknode=%s" % get_node(node[7])) - f.write(",stackquantity=%s" % ston(node[8])) - f.write(",nodemeta=%s" % ston(node[9])) - # Position - f.write(";x=%s,y=%s,z=%s" % (node[10], node[11], node[12])) - # Old node - f.write(";oldnode=%s" % get_node(node[13])) - f.write(",oldparam1=%s" % node[14]) - f.write(",oldparam2=%s" % ston(node[15])) - f.write(",oldmeta=%s" % ston(node[16])) - # New node - f.write(":newnode=%s" % get_node(node[17])) - f.write(",newparam1=%s" % ston(node[18])) - f.write(",newparam2=%s" % ston(node[19])) - f.write(",newmeta=%s" % ston(node[20])) - # Ending - f.write("]\n") - - f.close() + try: + name = "database-output.txt" + f = open(name, "w") + except Exception as err: + print(err) + sys.exit(1) + for node in nodes: + table = to_table(node) + f.write(table) + f.close() ######################################################################## @@ -146,10 +165,12 @@ def write_list(nodes): # if __name__ == '__main__': - #select all nodes player as set i where time >= time - all_nodes = select_all_nodes(0) #1418613979) + Id = Convert_id(db) + #select all nodes player as set i where time >= time + all_nodes = select_all_nodes(0) #1418613979) + + # write in file +# for x in range(0,len(all_nodes)): +# print(all_nodes[x]) + write_list(all_nodes) - # write in file -# for x in range(0,len(all_nodes)): -# print(all_nodes[x]) - write_list(all_nodes)