-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRandomize.py
More file actions
77 lines (66 loc) · 2.62 KB
/
Randomize.py
File metadata and controls
77 lines (66 loc) · 2.62 KB
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
import sys
import yaml
import os
import copy
import random
#Get KH2 music filenames
currentDir = sys.argv[0].replace((sys.argv[0].split('\\')[-1]),'')
data = yaml.safe_load(open(currentDir+'MusicData.yml','r'))
newmusiclist = {}
#Get a list of all music types
for music in data['Base Music List']:
for musictype in music['type']:
musictype = musictype.lower() #Make it case-insensitive
if musictype not in newmusiclist:
newmusiclist[musictype] = []
#Make a dictionary of all replacement music & type
if data['Replacement Music Location'] == '':
replaceDir = currentDir #Use the python file's location (can't be empty for os.walk to work)
truncateDir = len(currentDir)
else:
replaceDir = data['Replacement Music Location']
truncateDir = 0
for root, dirs, files in os.walk(replaceDir):
if replaceDir == currentDir: #Revert replaceDir
replaceDir = ''
for folder in reversed(root.split('\\')): #Get type of new music (whichever folder name match that comes last)
folder = folder.lower()
if folder in newmusiclist:
musictype = folder
break;
else: #Stop scanning current path if the music isn't part of any type
continue
for file in files:
if file[-4:] != '.scd':
continue
newmusiclist[musictype].append(root[truncateDir:]+'\\'+file)
#Randomization Function
def getmusic(category):
category = category.lower()
currentpool = newmusicpool[category]
if len(currentpool) == 0: #Skip if no music is available
return False
newmusic = currentpool.pop(random.randint(0, len(currentpool)-1)) #Randomly choose new music and remove it from the pool
if len(currentpool) == 0: #Refresh if ran out of available music
newmusicpool[category] = copy.deepcopy(newmusiclist[category])
return newmusic
#Do the randomization & write the mod.yml
newmusicpool = copy.deepcopy(newmusiclist)
musicresult = {}
f = open(currentDir+'mod.yml','w',encoding='utf-8')
f.write('assets:\n')
for music in data['Base Music List']:
types = music['type']
for i in range(len(types)): #Repick a type on unsuccessful attempts
chosentype = types.pop(random.randint(0,len(types)-1)) #Pick a type and remove it from the pool
newmusic = getmusic(chosentype) #Do the randomization
if newmusic:
break
if not newmusic: #Skip current music if all types are exhausted
continue
#Write the mod.yml
f.write('- name: '+music['filename']+' #'+music['title']+'\n')
f.write(' method: copy\n')
f.write(' source:\n')
f.write(' - name: '+newmusic+'\n')
f.close()