Advertisement
Guest User

Untitled

a guest
Dec 14th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.58 KB | None | 0 0
  1. from __future__ import print_function
  2. import os
  3.  
  4. FORCE_RENAME =  {
  5.     "Die": "Clonk/Verbal/Die",
  6.     "Munch1": "Clonk/Action/Munch1",
  7.     "WindCharge": "Objects/Windbag/Charge",
  8.     "WindChargeStop": "Objects/Windbag/ChargeStop",
  9.     "WindGust": "Objects/Windbag/Gust",
  10.     "PukaHurt1": "Animals/Puka/Hurt1",
  11.     "PukaHurt2": "Animals/Puka/Hurt2",
  12.     "PukaHurt3": "Animals/Puka/Hurt3",
  13.     "PukaHurt3b": "Animals/Puka/Hurt4",
  14.     "Hurt1": "Clonk/Verbal/Hurt1",
  15.     "Hurt2": "Clonk/Verbal/Hurt2",
  16.     "Click": "UI/Click"
  17. }
  18.  
  19. def splitPath(path):
  20.     folders = []
  21.     while 1:
  22.         path, folder = os.path.split(path)
  23.  
  24.         if folder != "":
  25.             folders.append(folder)
  26.         else:
  27.             if path != "":
  28.                 folders.append(path)
  29.  
  30.             break
  31.  
  32.     folders.reverse()
  33.     return folders
  34.  
  35. def cutPath(path):
  36.     if not path: return path
  37.  
  38.     def trimEnding(path):
  39.         return path[:-4] if path.endswith("ocg") else path
  40.     return [trimEnding(p) for p in splitPath(path)]
  41.  
  42. def isSound(filename):
  43.     type = filename[-3:]
  44.     return type in ["wav", "ogg"]
  45.  
  46. class Sound(object):
  47.     name = None
  48.     filename = None
  49.     path = None
  50.     force_rename = ""
  51.  
  52.     def __init__(self, filename, path):
  53.         self.filename = filename
  54.         self.name = filename[:-4]
  55.         self.path = cutPath(path)
  56.  
  57.     def __repr__(self):
  58.         return self.name + " (" + self.filename + " from /" + "/".join(self.path) + ")"
  59.  
  60.     def shortenBeginningOfPath(self, levels):
  61.         self.path = self.path[levels:]
  62.  
  63.     def getPrefixedName(self, sep="/", use_filename=False):
  64.         name = self.name if not use_filename else self.filename
  65.         if len(self.path) == 0:
  66.             return name
  67.         return sep.join(self.path) + sep + name
  68.  
  69.     def getSoundFolderPath(self):
  70.         path = ""
  71.         if len(self.path):
  72.             path = ".ocg/".join(self.path) + ".ocg"
  73.         return path
  74.  
  75. class SoundMap(object):
  76.  
  77.     sounds = None
  78.  
  79.     def __init__(self):
  80.         self.sounds = []
  81.  
  82.     @staticmethod
  83.     def generate(root, rename_map = None):
  84.         sound_map = SoundMap()
  85.         # crawl new sound folder and remember all sounds with the new folder structure
  86.         def recurse(foldername):
  87.             for folder, subfolders, filenames in os.walk(foldername):
  88.  
  89.                 for subfolder in subfolders:
  90.                     recurse(subfolder)
  91.  
  92.                 for file in filenames:
  93.                     if not isSound(file): continue
  94.                     sound = Sound(file, folder)
  95.  
  96.                     if rename_map and (sound.name in rename_map):
  97.                         sound.force_rename = rename_map[sound.name]
  98.  
  99.                     sound_map.sounds.append(sound)
  100.  
  101.         recurse(root)
  102.         return sound_map
  103.  
  104.     def __repr__(self):
  105.         return_value = ""
  106.         for sound in self.sounds:
  107.             return_value += str(sound) + "\n"
  108.         return return_value
  109.  
  110.     def shortenBeginningOfPaths(self, levels):
  111.         for sound in self.sounds:
  112.             sound.shortenBeginningOfPath(levels)
  113.  
  114.     def map(self, key):
  115.  
  116.         # the sound has a forced target?
  117.         if key.force_rename:
  118.             for sound in self.sounds:
  119.                 if sound.getPrefixedName() == key.force_rename:
  120.                     return sound
  121.             return None
  122.  
  123.         # first check if a name matches directly
  124.         for sound in self.sounds:
  125.             if sound.name == key.name:
  126.                 return sound
  127.  
  128.         # then check if name + prefix/suffix matches
  129.         for sound in self.sounds:
  130.             if len(sound.path) == 0: continue
  131.  
  132.             # prefix the path
  133.             if (sound.path[-1] + sound.name) == key.name:
  134.                 return sound
  135.             # suffix the path
  136.             if (sound.name + sound.path[-1]) == key.name:
  137.                 return sound
  138.             # suffix path but strip 1 char (number e.g.)
  139.             if (sound.name[:-1] + sound.path[-1] + sound.name[-1]) == key.name:
  140.                 return sound
  141.         return None
  142.  
  143.     def mapBackTarget(self, key):
  144.         for sound in self.sounds:
  145.             if sound.target == key:
  146.                 return sound
  147.         return None
  148.  
  149.     def assertUniqueMapping(self):
  150.         mapped_targets = {"None": [None]}
  151.         for sound in self.sounds:
  152.             key = str(sound.target)
  153.             if key in mapped_targets:
  154.                 mapped_targets[key].append(sound)
  155.             else:
  156.                 mapped_targets[key] = [sound]
  157.  
  158.         # check duplicates
  159.         duplicates = {}
  160.         for key in mapped_targets:
  161.             if len(mapped_targets[key]) > 1:
  162.                 duplicates[key] = mapped_targets[key]
  163.         return duplicates
  164.  
  165. if __name__ == "__main__":
  166.     old_sound_map = SoundMap.generate("planet/Sound.ocg", FORCE_RENAME)
  167.     new_sound_map = SoundMap.generate("planet/Sound_new.ocg")
  168.  
  169.     for map in [old_sound_map, new_sound_map]:
  170.         map.shortenBeginningOfPaths(2)
  171.  
  172.     for old_sound in old_sound_map.sounds:
  173.         old_sound.target = new_sound_map.map(old_sound)
  174.  
  175.     duplicates = old_sound_map.assertUniqueMapping()
  176.     print ("Duplicates: " + str(len(duplicates)))
  177.     for key in duplicates:
  178.         print (key + "\t\t----------")
  179.         for dup in duplicates[key]:
  180.             print ("\t\t" + str(dup))
  181.  
  182.     missing_sounds = {}
  183.     for sound in new_sound_map.sounds:
  184.         backref = old_sound_map.mapBackTarget(sound)
  185.         key = str(backref)
  186.         if key in missing_sounds:
  187.             missing_sounds[key].append(sound)
  188.         else:
  189.             missing_sounds[key] = [sound]
  190.  
  191.     if "None" in missing_sounds:
  192.         print ("Missing sounds:\t\t----------")
  193.         for sound in missing_sounds["None"]:
  194.             print ("\t\t" + str(sound))
  195.  
  196.     required_directories = {}
  197.     for sound in old_sound_map.sounds:
  198.         path = sound.target.getSoundFolderPath()
  199.         if not path: continue
  200.         required_directories[path] = True
  201.  
  202.     PLANET_PREFIX = "planet/"
  203.     SOUND_DIRECTORY_PREFIX = PLANET_PREFIX + "Sound.ocg/"
  204.  
  205.     with open('git_rename.sh','w') as f:
  206.         for directory in required_directories:
  207.             print ("mkdir -p " + SOUND_DIRECTORY_PREFIX + directory + "", file=f)
  208.         print ("\n")
  209.         for old_sound in old_sound_map.sounds:
  210.             source = SOUND_DIRECTORY_PREFIX + old_sound.filename
  211.             destination = SOUND_DIRECTORY_PREFIX + (old_sound.target.getSoundFolderPath() + "/" + old_sound.target.filename)
  212.             if source == destination:
  213.                 continue
  214.             print ("git mv " + source + " " + destination + "", file=f)
  215.         print ("\n")
  216.  
  217.         multi_sound_list = {}
  218.  
  219.         for old_sound in old_sound_map.sounds:
  220.             last_char = old_sound.name[-1]
  221.             prefix    = old_sound.name[:-1]
  222.             # merge sounds that are just numbered 1-3 or so?
  223.             if last_char.isdigit():
  224.                 new_name = old_sound.target.getPrefixedName(sep="::")[:-1]
  225.                 key = prefix + "_" + new_name
  226.                 if not key in multi_sound_list:
  227.                     multi_sound_list[key] = [prefix, last_char, new_name]
  228.                 else:
  229.                     multi_sound_list[key][1] += last_char
  230.             # normal sound -> simple replacement
  231.             else:
  232.                 print ("find " + PLANET_PREFIX + " -type f -name \"*.c\" -print0 | xargs -0 sed -i -b -r 's/((SoundAt|Sound)\(\")" + old_sound.name + "\"/\\1" + old_sound.target.getPrefixedName(sep="::") + "\"/g'", file=f)
  233.  
  234.         for key in multi_sound_list:
  235.             prefix, suffix, new_name = multi_sound_list[key]
  236.             print ("find " + PLANET_PREFIX + " -type f -name \"*.c\" -print0 | xargs -0 sed -i -b -r 's/((SoundAt|Sound)\(\")" + prefix + "([" + suffix + "*?]\")/\\1" + new_name + "\\3/g'", file=f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement