summaryrefslogtreecommitdiff
path: root/merge-client.py
blob: 50c0a45cca4f097bc024eb996e5ac868abc01c64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/python3
# This consolidates client XMLs
import os, shutil, traceback, subprocess, time

MYSELF="clientdata/"
EVOLVED="../pre-renewal/client-data/"
MOUBOO="../client-data/"
REVOLT="../evol/client-data/"

# items.xml and their sprites in graphics/
# Remember to regenerate weapons.xml after merging

os.chdir("..")
item_db=[]
paths=[]

def fix_id(l, offset):
    if "id=" in l:
        tmp=l.split("\"")
        try:
            if (int(tmp[1]) > 0):
                tmp[1]=str(int(tmp[1]) + offset)
        except:
            traceback.print_exc()
        l="\"".join(tmp)
    return l

## This is to find misc files and copy them over, but
## We probably could just c/p one over the other >.>
def hp(l, WHERE):
    global paths
    if "<include" in l:
        return l
    if ".png" in l or ".ogg" in l or ".xml" in l:
        tmpi=l.split("\"")
        if len(tmpi) == 1:
            tmpi=l.split("sprite")
        for e in tmpi:
            e=e.replace(">", "").replace("<", "")
            if e.endswith("/"):
                e=e[:-1]
            if e.endswith(".png"):
                paths.append(e)
            elif e.endswith(".ogg"):
                paths.append(e)
            elif e.endswith(".xml"):
                paths.append(e)

                ## We must also open the XML and extract paths from it

                pt="NO PATH FOUND"
                if os.path.exists("%s/%s" % (WHERE, e)):
                    pt = "%s/%s" % (WHERE, e)
                elif os.path.exists("%s/graphics/sprites/%s" % (WHERE, e)):
                    pt = "%s/graphics/sprites/%s" % (WHERE, e)
                elif os.path.exists("%s/graphics/%s" % (WHERE, e)):
                    pt = "%s/graphics/%s" % (WHERE, e)

                with open(pt, 'r') as j:
                    for k in j:
                        i=k.split("\"")
                        for n in i:
                            if n.endswith(".png"):
                                paths.append(n)
    # TODO: Update the line with a prefix
    return l

def unify(YESELF):
    global paths
    for f in paths:
        i="/".join(f.split("/")[:-1])
        ## Raw Copy
        try:
            os.makedirs(os.path.abspath("%s/%s" % (MYSELF, i)), exist_ok=True)
            shutil.copy2("%s/%s" % (YESELF, f),
                         "%s/%s" % (MYSELF, f))
            continue
        except FileNotFoundError:
            pass
        except:
            traceback.print_exc()

        ## Raw2 Copy
        try:
            os.makedirs(os.path.abspath("%s/graphics/%s" % (MYSELF, i)), exist_ok=True)
            shutil.copy2("%s/graphics/%s" % (YESELF, f),
                         "%s/graphics/%s" % (MYSELF, f))
            continue
        except FileNotFoundError:
            pass
        except:
            traceback.print_exc()

        ## Item Icon Copy
        try:
            os.makedirs(os.path.abspath("%s/graphics/items/%s" % (MYSELF, i)), exist_ok=True)
            shutil.copy2("%s/graphics/items/%s" % (YESELF, f),
                         "%s/graphics/items/%s" % (MYSELF, f))
            continue
        except FileNotFoundError:
            pass
        except:
            traceback.print_exc()

        ## Spritesheet Copy
        try:
            os.makedirs(os.path.abspath("%s/graphics/sprites/%s" % (YESELF, i)), exist_ok=True)
            shutil.copy2("%s/graphics/sprites/%s" % (YESELF, f),
                         "%s/graphics/sprites/%s" % (MYSELF, f))
            continue
        except FileNotFoundError:
            print("\033[1mFailed to copy: %s\033[0m" % f)
            pass
        except:
            traceback.print_exc()

    paths=[]
    return

#################################################################################
## Moubootaur Legends is a raw copy
with open("%s/items.xml" % MOUBOO, "r") as f:
    for l in f:
        ## FIXME: RACESPRITE
        #l=l.replace("    ", "\t")
        ## Add headers
        if "Authors:" in l:
            item_db.append("<!-- This file is generated automatically. -->\n")
            item_db.append("<!-- Please do not edit it directly. -->\n")
            item_db.append("<!-- ************************************************************************ -->\n")
        ## Follow includes instead of saving them
        if "<include" in l:
            tmp=l.split("\"")
            if not "name" in tmp[0]:
                print("\033[31mIllegal include: %s" % l)
                continue
            with open("%s/%s" % (MOUBOO, tmp[1]), "r") as k:
                save=False
                for j in k:
                    if not save:
                        if "<items>" in j:
                            save=True
                        continue
                    if "</items>" in j:
                        break
                    j=hp(j, MOUBOO)
                    item_db.append(j)
            continue

        ## Save the line
        l=hp(l, MOUBOO)
        item_db.append(l)
    while not "</items>" in item_db[-1]:
        item_db=item_db[:-1]
item_db=item_db[:-1]
print("\033[32;1mMoubootaur Legends OK\033[0m")


## Handle copy pastes
unify(MOUBOO)

################################################################################
## Legacy/Evolved has an offset of 10k
with open("%s/items.xml" % EVOLVED, "r") as f:
    saving=False
    for l in f:
        ## FIXME: RACESPRITE
        #l=l.replace("    ", "\t")
        ## Fix ID offset
        l=fix_id(l, 10000)
        ## Follow includes instead of saving them
        if "<include" in l:
            tmp=l.split("\"")
            if not "name" in tmp[0]:
                print("\033[31mIllegal include: %s" % l)
                continue
            with open("%s/%s" % (EVOLVED, tmp[1]), "r") as k:
                save=False
                for j in k:
                    if not save:
                        if "<items>" in j:
                            save=True
                        continue
                    if "</items>" in j:
                        break
                    j=fix_id(j, 10000)
                    j=hp(j, EVOLVED)
                    item_db.append(j)
            continue

        ## Save the line
        if not saving:
            if "<items>" in l:
                saving=True
            continue
        l=hp(l, EVOLVED)
        item_db.append(l)
    while not "</items>" in item_db[-1]:
        item_db=item_db[:-1]
item_db=item_db[:-1]
print("\033[32;1mLegacy/Evolved OK\033[0m")



## Handle copy pastes
unify(EVOLVED)

################################################################################
## rEvolt has an offset of 20k
with open("%s/items.xml" % REVOLT, "r") as f:
    saving=False
    for l in f:
        ## FIXME: RACESPRITE
        #l=l.replace("    ", "\t")
        ## Fix ID offset
        l=fix_id(l, 20000)
        ## Follow includes instead of saving them
        if "<include" in l:
            tmp=l.split("\"")
            if not "name" in tmp[0]:
                print("\033[31mIllegal include: %s" % l)
                continue
            with open("%s/%s" % (REVOLT, tmp[1]), "r") as k:
                save=False
                for j in k:
                    if not save:
                        if "<items>" in j:
                            save=True
                        continue
                    if "</items>" in j:
                        break
                    j=fix_id(j, 20000)
                    j=hp(j, REVOLT)
                    item_db.append(j)
            continue

        ## Save the line
        if not saving:
            if "<items>" in l:
                saving=True
            continue
        l=hp(l, REVOLT)
        item_db.append(l)
    while not "</items>" in item_db[-1]:
        item_db=item_db[:-1]
#item_db=item_db[:-1]
print("\033[32;1mrEvolt OK\033[0m")



## Handle copy pastes
unify(REVOLT)





################################################################################
## Save the final item database
with open(MYSELF+"items.xml", "w") as f:
    for l in item_db:
        f.write(l)

print("\033[33;1mSaved! Databases merged!\033[0m")

## Clean empty dirs
time.sleep(1)
print("Now deleting empty folders:")
time.sleep(4)
os.chdir(MYSELF)
subprocess.call("""find . -type d -empty -not -iwholename "./.*" -print -delete""", shell=True)