From be6e852a52610e5715a6cb87d606647e39a6b487 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 31 Oct 2014 19:34:22 +0300 Subject: hercules: add basic support for converting npc scripts from evol to hercules. Can convert script line in npc scripts. Can create map_index.txt and map.conf based on npc dir. --- hercules/.gitignore | 2 + hercules/code/fileutils.py | 4 ++ hercules/code/server/npcs.py | 105 ++++++++++++++++++++++++++++++++++++++++++ hercules/code/server/utils.py | 11 +++++ hercules/code/stringutils.py | 7 +++ hercules/convert_server.py | 11 +++++ 6 files changed, 140 insertions(+) create mode 100644 hercules/code/server/npcs.py create mode 100644 hercules/code/server/utils.py create mode 100755 hercules/convert_server.py diff --git a/hercules/.gitignore b/hercules/.gitignore index 96ea0f2..b21c9db 100644 --- a/hercules/.gitignore +++ b/hercules/.gitignore @@ -1,6 +1,8 @@ clientdata maps serverdata +oldserverdata +newserverdata mapcache *.pyc diff --git a/hercules/code/fileutils.py b/hercules/code/fileutils.py index 150cb8c..884768f 100644 --- a/hercules/code/fileutils.py +++ b/hercules/code/fileutils.py @@ -60,3 +60,7 @@ def saveFile(fileName, data): def makeDir(path): if not os.path.exists(path): os.makedirs(path) + +def removeAllFiles(path): + if os.path.exists(path): + shutil.rmtree(path) diff --git a/hercules/code/server/npcs.py b/hercules/code/server/npcs.py new file mode 100644 index 0000000..7157c31 --- /dev/null +++ b/hercules/code/server/npcs.py @@ -0,0 +1,105 @@ +# -*- coding: utf8 -*- +# +# Copyright (C) 2014 Evol Online +# Author: Andrei Karas (4144) + +import re + +from code.fileutils import * +from code.stringutils import * + +mapsConfFile = "newserverdata/conf/maps.conf" +mapsIndexFile = "newserverdata/db/map_index.txt" +mapsIndex = 1 +scriptRe = re.compile("^(?P[^/](.+))[.]gat,([ ]*)(?P[\d]+),([ ]*)(?P[\d]+),([ ]*)(?P[\d]+)(|,(?P[\d]+))" + + "[\t](?Pscript)[\t](?P[\w#' ]+)[\t]" + "(?P[\d]+)((,((?P[\d]+),(?P[\d]+)))|)(|;(?P[\d]+))(|,|;){$") + +class ScriptTracker: + pass + +def convertNpcs(): + processNpcDir("oldserverdata/npc/", "newserverdata/npc/") + +def processNpcDir(srcDir, dstDir): + makeDir(dstDir) + files = os.listdir(srcDir) + for file1 in files: + if file1[0] == '.': + continue + srcPath = os.path.abspath(srcDir + os.path.sep + file1) + dstPath = os.path.abspath(dstDir + os.path.sep + file1) + if not os.path.isfile(srcPath): + processNpcDir(srcPath, dstPath) + else: + if file1[-5:] == ".conf" or file1[-4:] == ".txt": + processNpcFile(srcPath, dstPath) + +def processNpcFile(srcFile, dstFile): +# print "file: " + srcFile + tracker = ScriptTracker() + tracker.insideScript = False + with open(srcFile, "r") as r: + with open(dstFile, "w") as w: + tracker.w = w + for line in r: + tracker.line = line + res = convertTextLine(tracker) + if res: + w.write(tracker.line) + +def convertTextLine(tracker): + line = tracker.line + if line[:5] == "map: ": + processScriptMapLine(line) + return False + idx = line.find("\tscript\t") + if idx >= 0: + processScript(tracker) + return False + return True + +def processScriptMapLine(line): + global mapsIndex + line = stripNewLine(line) + if line[-4:] == ".gat": + line = line[:-4] + with open(mapsConfFile, "a") as w: + w.write(line + "\n") + + with open(mapsIndexFile, "a") as w: + w.write("{0} {1}\n".format(line, mapsIndex)) + mapsIndex = mapsIndex + 1 + +def processScript(tracker): + line = tracker.line + w = tracker.w + if line[:9] == "function\t": + tracker.w.write(line) + return + + m = scriptRe.search(line) + if m == None: + print "error in parsing: " + line + w.write("!!!error parsing line") + w.write(line) + return +# print "source=" + line[:-1] +# print "map={0} x={1} y={2} dir={3} gender={4} tag={5} name={6} class={7}, xs={8}, ys={9}, size={10}".format( +# m.group("map"), m.group("x"), m.group("y"), m.group("dir"), m.group("gender"), +# m.group("tag"), m.group("name"), m.group("class"), m.group("xs"), m.group("ys"), m.group("size")) + + if m.group("gender") != None: + w.write("// Gender = {0}\n".format(m.group("gender"))); + if m.group("size") != None: + w.write("// Size = {0}\n".format(m.group("size"))); + + if m.group("x") == 0 and m.group("y") == 0 and m.group("class") == 0: # float npc + w.write("-") + else: + w.write("{0},{1},{2},{3}".format(m.group("map"), m.group("x"), m.group("y"), m.group("dir"))) + w.write("\tscript\t{0}\t{1}".format(m.group("name"), m.group("class"))); + + if m.group("xs") != None: + w.write(",{0},{1}".format(m.group("xs"), m.group("ys"))); + w.write(",{\n"); diff --git a/hercules/code/server/utils.py b/hercules/code/server/utils.py new file mode 100644 index 0000000..922cf5b --- /dev/null +++ b/hercules/code/server/utils.py @@ -0,0 +1,11 @@ +# -*- coding: utf8 -*- +# +# Copyright (C) 2014 Evol Online +# Author: Andrei Karas (4144) + +from code.fileutils import * + +def cleanServerData(): + removeAllFiles("newserverdata") + makeDir("newserverdata/conf") + makeDir("newserverdata/db") diff --git a/hercules/code/stringutils.py b/hercules/code/stringutils.py index 09e3cf8..3d0b77a 100644 --- a/hercules/code/stringutils.py +++ b/hercules/code/stringutils.py @@ -26,3 +26,10 @@ def strToXml(data): data = data.replace("<", "<"); data = data.replace(">", ">"); return data + +def stripNewLine(data): + if len(data) == 0: + return data + if data[-1] == "\n": + data = data[:-1] + return data diff --git a/hercules/convert_server.py b/hercules/convert_server.py new file mode 100755 index 0000000..2902015 --- /dev/null +++ b/hercules/convert_server.py @@ -0,0 +1,11 @@ +#! /usr/bin/env python +# -*- coding: utf8 -*- +# +# Copyright (C) 2014 Evol Online +# Author: Andrei Karas (4144) + +from code.server.npcs import * +from code.server.utils import * + +cleanServerData() +convertNpcs(); -- cgit v1.2.3-70-g09d2