Last active
September 23, 2020 09:53
-
-
Save havchr/9db7da49bac0bc1b6afe25ab9cef8a78 to your computer and use it in GitHub Desktop.
Explodes a unity shader collection into multiple shader collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import subprocess | |
import os | |
if len(sys.argv) < 3: | |
print("This snippet explodes a shader collections into multiple shader collection containing x shaders") | |
print("use like this... ./shaderSplitter.py AllShaders.shaderVariants ShaderPrefix 4") | |
print("where 4 is amount of shaders per exploded file, also the number of shaders per file is optional - it defaults to 1") | |
print("Exiting program.. Please provide correct arguments") | |
quit() | |
shadeFileIn = sys.argv[1] | |
shadeOutPrefix = sys.argv[2] | |
shadeOutPostfix=".shadervariants" | |
pattern ="- first:" | |
fileCounter = 1 | |
shaderCounter = 1 | |
shadersPerOutputFile = 1 | |
if len(sys.argv) == 4: | |
shadersPerOutputFile = int(sys.argv[3]) | |
header = "" | |
hasCollectedHeader = False | |
print("shadePerOutputFile " + str(shadersPerOutputFile)) | |
dirs = shadeOutPrefix.rpartition("/")[0] | |
sys.stdout.write("them dirs be " + dirs + "\n") | |
if len(dirs) >=1: | |
os.makedirs(dirs) | |
print("exploding shaders from " + shadeFileIn + "into " + dirs + " as " + shadeOutPrefix + "_x") | |
def OpenShaderFileForOutput(): | |
filename = shadeOutPrefix + "_" + str(fileCounter) + shadeOutPostfix | |
print("writing to " + filename) | |
fout = open(filename,"w") | |
return fout | |
fout = OpenShaderFileForOutput() | |
shaderCollected="" | |
with open(shadeFileIn) as fp: | |
for cnt,line in enumerate(fp): | |
if pattern in line: | |
if hasCollectedHeader is True: | |
shaderCounter +=1 | |
if shaderCounter % shadersPerOutputFile == 0: | |
fileCounter +=1 | |
fout.close() | |
fout = OpenShaderFileForOutput() | |
fout.write(header) | |
hasCollectedHeader = True | |
fout.write(line) | |
else: | |
if hasCollectedHeader is False: | |
header+=line | |
fout.write(line) | |
fout.close() | |
print("Done.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment