-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmergeUnityFile.py
More file actions
98 lines (85 loc) · 3.85 KB
/
mergeUnityFile.py
File metadata and controls
98 lines (85 loc) · 3.85 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# _____ _ __
# ,´ \/ \ _`, Author: Christian Grund
# | C/ / / __ Company: AISC GmbH
# \_/__/__/ /_ mailto: cg@aisc.digital
#
import os
import pathlib
import shutil
from configparser import ConfigParser
from typing import List
from UnityMerger.listmerger import Listmerger
from UnityMerger.merge import Merge
from unityData.unityFileBlock import UnityFileBlock
from unityData.unityProject import UnityProject
class MergeUnityFile:
def __init__(self, projectconf, file):
self.projectconf = projectconf
self.file = file
if os.path.isdir(projectconf):
if os.path.exists(os.path.join(projectconf,"UnityToolPackConfig.cfg")):
self.projectconf = os.path.join(projectconf,"UnityToolPackConfig.cfg")
else:
if(os.path.exists(os.path.join(projectconf,"Assets")) and os.path.exists(os.path.join(projectconf,"Packages")) and os.path.exists(os.path.join(projectconf,"Library"))):
config = ConfigParser()
config['PATHS'] = {
'assetfolder': 'Assets',
'libraryfolder': 'Library',
'packagesfolder': 'Packages'
}
# Write the configuration to a file
with open(os.path.join(projectconf,"UnityToolPackConfig.cfg"), 'w') as configfile:
config.write(configfile)
self.projectconf = os.path.join(projectconf,"UnityToolPackConfig.cfg")
else:
print("The folder does not seem to be a Unity Project")
raise FileNotFoundError;
def resolveInteractive(self):
mineGUID, theirsGUID, mineFileName, theirsFilename = Merge.createMineTheirsFiles(self.file)
project = UnityProject(self.projectconf)
mineFile = project.getReferenceFromGUID(mineGUID)
theirFile = project.getReferenceFromGUID(theirsGUID)
#merged = Merge.merge_dicts(mine_blocks, theirs_blocks)
mergedBlocks:List[UnityFileBlock] = Merge.merge_UnityFiles(mineFile,theirFile)
mergedBlocks.sort(key=lambda x:x.uid)
outText = "".join([b.block for b in mergedBlocks])
splt = self.file.rsplit('.',1)
mergedFileName = splt[0] + "_MERGED." + splt[1]
with open(mergedFileName, "w") as merged:
with open(mineFile.fileName) as mine:
for line in mine:
if line.startswith("%"):
merged.write(line)
else:
break
merged.write(outText)
print("##################################")
print("+++ Merge Finished +++")
if(Listmerger.yesno("Do you want to overwrite " + self.file + " now?")):
shutil.move(mergedFileName, self.file)
todelete = [mineFileName, mineFileName + ".meta", theirsFilename, theirsFilename + ".meta"]
print("\n")
if(Listmerger.yesno("Do you want to delete the following files now?\n" + "\n".join(todelete))):
for f in todelete:
pathlib.Path.unlink(f)
pass
if __name__ == "__main__":
import sys
print("""
_____ _ __ AISC Unity Tool Pack
,´ \/ \ _`, Merge Tool
| C/ / / __
\_/__/__/ /_ by Christian Grund, AISC GmbH
#####################################################
""")
if(len(sys.argv) != 3):
print("""
Usage:
using ProjectConfigFile:
mergeUnityFile.py [ProjectConfig] [FileWithMergeConflicts]
using Directory
mergeUnityFile.py [UnityProjectPath] [FileWithMergeConflicts]
note: this will automatically generate a config file UnityToolPackConfig.cfg in your Unity Project Directory
""")
muf = MergeUnityFile(sys.argv[1], sys.argv[2])
muf.resolveInteractive()