Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import os
- FORCE_RENAME = {
- "Die": "Clonk/Verbal/Die",
- "Munch1": "Clonk/Action/Munch1",
- "WindCharge": "Objects/Windbag/Charge",
- "WindChargeStop": "Objects/Windbag/ChargeStop",
- "WindGust": "Objects/Windbag/Gust",
- "PukaHurt1": "Animals/Puka/Hurt1",
- "PukaHurt2": "Animals/Puka/Hurt2",
- "PukaHurt3": "Animals/Puka/Hurt3",
- "PukaHurt3b": "Animals/Puka/Hurt4",
- "Hurt1": "Clonk/Verbal/Hurt1",
- "Hurt2": "Clonk/Verbal/Hurt2",
- "Click": "UI/Click"
- }
- def splitPath(path):
- folders = []
- while 1:
- path, folder = os.path.split(path)
- if folder != "":
- folders.append(folder)
- else:
- if path != "":
- folders.append(path)
- break
- folders.reverse()
- return folders
- def cutPath(path):
- if not path: return path
- def trimEnding(path):
- return path[:-4] if path.endswith("ocg") else path
- return [trimEnding(p) for p in splitPath(path)]
- def isSound(filename):
- type = filename[-3:]
- return type in ["wav", "ogg"]
- class Sound(object):
- name = None
- filename = None
- path = None
- force_rename = ""
- def __init__(self, filename, path):
- self.filename = filename
- self.name = filename[:-4]
- self.path = cutPath(path)
- def __repr__(self):
- return self.name + " (" + self.filename + " from /" + "/".join(self.path) + ")"
- def shortenBeginningOfPath(self, levels):
- self.path = self.path[levels:]
- def getPrefixedName(self, sep="/", use_filename=False):
- name = self.name if not use_filename else self.filename
- if len(self.path) == 0:
- return name
- return sep.join(self.path) + sep + name
- def getSoundFolderPath(self):
- path = ""
- if len(self.path):
- path = ".ocg/".join(self.path) + ".ocg"
- return path
- class SoundMap(object):
- sounds = None
- def __init__(self):
- self.sounds = []
- @staticmethod
- def generate(root, rename_map = None):
- sound_map = SoundMap()
- # crawl new sound folder and remember all sounds with the new folder structure
- def recurse(foldername):
- for folder, subfolders, filenames in os.walk(foldername):
- for subfolder in subfolders:
- recurse(subfolder)
- for file in filenames:
- if not isSound(file): continue
- sound = Sound(file, folder)
- if rename_map and (sound.name in rename_map):
- sound.force_rename = rename_map[sound.name]
- sound_map.sounds.append(sound)
- recurse(root)
- return sound_map
- def __repr__(self):
- return_value = ""
- for sound in self.sounds:
- return_value += str(sound) + "\n"
- return return_value
- def shortenBeginningOfPaths(self, levels):
- for sound in self.sounds:
- sound.shortenBeginningOfPath(levels)
- def map(self, key):
- # the sound has a forced target?
- if key.force_rename:
- for sound in self.sounds:
- if sound.getPrefixedName() == key.force_rename:
- return sound
- return None
- # first check if a name matches directly
- for sound in self.sounds:
- if sound.name == key.name:
- return sound
- # then check if name + prefix/suffix matches
- for sound in self.sounds:
- if len(sound.path) == 0: continue
- # prefix the path
- if (sound.path[-1] + sound.name) == key.name:
- return sound
- # suffix the path
- if (sound.name + sound.path[-1]) == key.name:
- return sound
- # suffix path but strip 1 char (number e.g.)
- if (sound.name[:-1] + sound.path[-1] + sound.name[-1]) == key.name:
- return sound
- return None
- def mapBackTarget(self, key):
- for sound in self.sounds:
- if sound.target == key:
- return sound
- return None
- def assertUniqueMapping(self):
- mapped_targets = {"None": [None]}
- for sound in self.sounds:
- key = str(sound.target)
- if key in mapped_targets:
- mapped_targets[key].append(sound)
- else:
- mapped_targets[key] = [sound]
- # check duplicates
- duplicates = {}
- for key in mapped_targets:
- if len(mapped_targets[key]) > 1:
- duplicates[key] = mapped_targets[key]
- return duplicates
- if __name__ == "__main__":
- old_sound_map = SoundMap.generate("planet/Sound.ocg", FORCE_RENAME)
- new_sound_map = SoundMap.generate("planet/Sound_new.ocg")
- for map in [old_sound_map, new_sound_map]:
- map.shortenBeginningOfPaths(2)
- for old_sound in old_sound_map.sounds:
- old_sound.target = new_sound_map.map(old_sound)
- duplicates = old_sound_map.assertUniqueMapping()
- print ("Duplicates: " + str(len(duplicates)))
- for key in duplicates:
- print (key + "\t\t----------")
- for dup in duplicates[key]:
- print ("\t\t" + str(dup))
- missing_sounds = {}
- for sound in new_sound_map.sounds:
- backref = old_sound_map.mapBackTarget(sound)
- key = str(backref)
- if key in missing_sounds:
- missing_sounds[key].append(sound)
- else:
- missing_sounds[key] = [sound]
- if "None" in missing_sounds:
- print ("Missing sounds:\t\t----------")
- for sound in missing_sounds["None"]:
- print ("\t\t" + str(sound))
- required_directories = {}
- for sound in old_sound_map.sounds:
- path = sound.target.getSoundFolderPath()
- if not path: continue
- required_directories[path] = True
- PLANET_PREFIX = "planet/"
- SOUND_DIRECTORY_PREFIX = PLANET_PREFIX + "Sound.ocg/"
- with open('git_rename.sh','w') as f:
- for directory in required_directories:
- print ("mkdir -p " + SOUND_DIRECTORY_PREFIX + directory + "", file=f)
- print ("\n")
- for old_sound in old_sound_map.sounds:
- source = SOUND_DIRECTORY_PREFIX + old_sound.filename
- destination = SOUND_DIRECTORY_PREFIX + (old_sound.target.getSoundFolderPath() + "/" + old_sound.target.filename)
- if source == destination:
- continue
- print ("git mv " + source + " " + destination + "", file=f)
- print ("\n")
- multi_sound_list = {}
- for old_sound in old_sound_map.sounds:
- last_char = old_sound.name[-1]
- prefix = old_sound.name[:-1]
- # merge sounds that are just numbered 1-3 or so?
- if last_char.isdigit():
- new_name = old_sound.target.getPrefixedName(sep="::")[:-1]
- key = prefix + "_" + new_name
- if not key in multi_sound_list:
- multi_sound_list[key] = [prefix, last_char, new_name]
- else:
- multi_sound_list[key][1] += last_char
- # normal sound -> simple replacement
- else:
- 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)
- for key in multi_sound_list:
- prefix, suffix, new_name = multi_sound_list[key]
- 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