summaryrefslogblamecommitdiff
path: root/merge-client.py
blob: 7e10521b83d1129f1680af655459e6f86264f277 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

                               
                                              










                                                  
        

                      
                                                  



                                                


                                                                   




                                 





































                                                                                   



                                                                        
                              

                       
                
                                                 

                                            
                                  










                                        
                                                 
                                          

                               
                                                        
                               



                                                                                            
                                    




                                                                                            

                                                                      
                                     






                                   
            
 
                       




                                     
                                                                                      
                                               
                                                       







                                 
                                                                                               
                                                        
                                                                







                                 
                                                                                                     
                                                              
                                                                      







                                 
                                                                                                       
                                                                
                                                                        






                                                         











                                                         



                                                                                 









                                                                                                                 
                                                 













                                                            
                                                 



                                     
                                     






                                               
                     
                   
 
                                                                                







                                              
                                                 














                                                             
                                                   







                                     
                                       


                                        
                    



                                           
                     
                     
 







                                                                                
                          














                                                            
                                      
                                                 
                                     






                              
                                     
                         







                                        
                   
 

 

 
                                                                                






                                                  





                                                                                               
 
#!/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 or " from=" in l or " to=" in l:
        tmp=l.split("\"")
        try:
            if (int(tmp[1]) > 0):
                tmp[1]=str(int(tmp[1]) + offset)
            else:
                # Negative IDs are special (10,000 => 200 per game)
                tmp[1]=str(int(tmp[1]) - int(offset//50))
        except:
            traceback.print_exc()
        l="\"".join(tmp)
    return l

## This was foked from transverse()
def subxml(WHERE, e, sub):
    global MYSELF, paths
    pt="NO PATH FOUND"
    if os.path.exists("%s/%s" % (WHERE, e)):
        pt = "%s/%s" % (WHERE, e)
        TARG = "%s/%s/%s" % (MYSELF, sub, e)
    elif os.path.exists("%s/graphics/sprites/%s" % (WHERE, e)):
        pt = "%s/graphics/sprites/%s" % (WHERE, e)
        TARG = "%s/graphics/sprites/%s/%s" % (MYSELF, sub, e)
    elif os.path.exists("%s/graphics/%s" % (WHERE, e)):
        pt = "%s/graphics/%s" % (WHERE, e)
        TARG = "%s/graphics/%s/%s" % (MYSELF, sub, e)
    else:
        print("\033[1mNO path: %s\033[0m" % e)

    if pt != "NO PATH FOUND":
        bf=[]
        with open(pt, 'r') as j:
            for k in j:
                h=k.split("\"")
                for g, n in enumerate(h):
                    if n.split("|")[0].endswith(".png"):
                        paths.append(n.split("|")[0])
                        h[g]="%s/%s" % (sub, n)
                    ## XML might need to fall in a loop
                    elif n.split("|")[0].endswith(".xml"):
                        h[g]="%s/%s" % (sub, n)
                        subxml(WHERE, n.split("|")[0], sub)
                bf.append("\"".join(h))

        ## Unfortunately, we must save 'bf' here so it is safely done
        os.makedirs(os.path.abspath("/".join(TARG.split("/")[:-1])), exist_ok=True)
        with open("%s" % (TARG), "w") as j:
            for k in bf:
                j.write(k)
    return

## This is to find misc files and copy them over with new path
## TODO: sfx makes it confuse (has "quotes" but is enclosed in >arrows<)
## ALSO: Dyed items e.g. "equipment/chest/cottonshirt.png|W"
## ALSO: Sprites may have "quotes" but be enclosed in >arrows<
def transverse(l, WHERE, sub):
    global paths
    if "<include" in l:
        return l
    if ".png" in l or ".ogg" in l or ".xml" in l:
        ## Determine the type of split
        if "<sprite" in l or "sprite>" in l:
            tmpi=l.split("sprite")
            ori=0
        #elif "sound" in l:
        else:
            tmpi=l.split("\"")
            ori=1

        ## Process it
        for i, e in enumerate(tmpi):
            raw=str(e)
            if ">" in e:
                e=raw[raw.index('>')+1:]
            e=e.replace(">", "").replace("<", "")
            e=e.split("|")[0] # Remove dye
            if e.endswith("/"):
                e=e[:-1]
            if e.endswith(".png") or e.endswith(".ogg"):
                paths.append(e)
                if ">" in raw:
                    tmpi[i]="%s>%s/%s" % (raw[:raw.index('>')], sub, raw[raw.index('>')+1:])
                else:
                    tmpi[i]="%s/%s" % (sub, raw)
            elif e.endswith(".xml"):
                #paths.append(e)
                if ">" in raw:
                    tmpi[i]="%s>%s/%s" % (raw[:raw.index('>')], sub, raw[raw.index('>')+1:])
                else:
                    tmpi[i]="%s/%s" % (sub, raw)

                ## We must also open the XML and extract paths from it
                subxml(WHERE, e, sub)

        ## Update the line
        if ori == 0:
            l="sprite".join(tmpi)
        else:
            l="\"".join(tmpi)
    # Send the line with the prefix
    return l

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

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

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

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

        ## Sounds Effects Copy (clobber method)
        try:
            shutil.copytree("%s/sfx" % (YESELF),
                            "%s/sfx" % (MYSELF),
                            dirs_exist_ok=True)
            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=transverse(j, MOUBOO, 'ml')
                    item_db.append(j)
            continue

        ## Save the line
        l=transverse(l, MOUBOO, 'ml')
        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, 'ml')

################################################################################
## 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=transverse(j, EVOLVED, 'tmw')
                    item_db.append(j)
            continue

        ## Save the line
        if not saving:
            if "<items>" in l:
                saving=True
            continue
        l=transverse(l, EVOLVED, 'tmw')
        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, 'tmw')

################################################################################
## 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=transverse(j, REVOLT, 're')
                    item_db.append(j)
            continue

        ## Save the line
        if not saving:
            if "<items>" in l:
                saving=True
            continue
        l=transverse(l, REVOLT, 're')
        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, 're')





################################################################################
## 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)