|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import platform |
| 4 | +import subprocess |
| 5 | +import shutil |
| 6 | +import codecs |
| 7 | + |
| 8 | +class ResourceCompiler (object): |
| 9 | + def __init__ (self, devKitPath, languageCode, sourcesPath, resourcesPath, resourceObjectsPath): |
| 10 | + self.devKitPath = devKitPath |
| 11 | + self.languageCode = languageCode |
| 12 | + self.sourcesPath = sourcesPath |
| 13 | + self.resourcesPath = resourcesPath |
| 14 | + self.resourceObjectsPath = resourceObjectsPath |
| 15 | + self.resConvPath = None |
| 16 | + |
| 17 | + def IsValid (self): |
| 18 | + if self.resConvPath == None: |
| 19 | + return False |
| 20 | + if not os.path.exists (self.resConvPath): |
| 21 | + return False |
| 22 | + return True |
| 23 | + |
| 24 | + def GetPrecompiledResourceFilePath (self, grcFilePath): |
| 25 | + grcFileName = os.path.split (grcFilePath)[1] |
| 26 | + return os.path.join (self.resourceObjectsPath, grcFileName + '.i') |
| 27 | + |
| 28 | + def CompileLocalizedResources (self): |
| 29 | + locResourcesFolder = os.path.join (self.resourcesPath, 'R' + self.languageCode) |
| 30 | + grcFiles = self.CollectFilesFromFolderWithExtension (locResourcesFolder, '.grc') |
| 31 | + for grcFilePath in grcFiles: |
| 32 | + assert self.CompileResourceFile (grcFilePath), 'Failed to compile resource: ' + grcFilePath |
| 33 | + |
| 34 | + def CompileFixResources (self): |
| 35 | + fixResourcesFolder = os.path.join (self.resourcesPath, 'RFIX') |
| 36 | + grcFiles = self.CollectFilesFromFolderWithExtension (fixResourcesFolder, '.grc') |
| 37 | + for grcFilePath in grcFiles: |
| 38 | + assert self.CompileResourceFile (grcFilePath), 'Failed to compile resource: ' + grcFilePath |
| 39 | + |
| 40 | + def RunResConv (self, platformSign, codepage, inputFilePath, nativeResourceFileExtenion): |
| 41 | + imageResourcesFolder = os.path.join (self.resourcesPath, 'RFIX', 'Images') |
| 42 | + inputFileBaseName = os.path.splitext (os.path.split (inputFilePath)[1])[0] |
| 43 | + nativeResourceFilePath = os.path.join (self.resourceObjectsPath, inputFileBaseName + nativeResourceFileExtenion) |
| 44 | + result = subprocess.call ([ |
| 45 | + self.resConvPath, |
| 46 | + '-m', 'r', # resource compile mode |
| 47 | + '-T', platformSign, # target platform |
| 48 | + '-q', 'utf8', codepage, # code page conversion |
| 49 | + '-w', '2', # HiDPI image size list |
| 50 | + '-p', imageResourcesFolder, # image search path |
| 51 | + '-i', inputFilePath, # input path |
| 52 | + '-o', nativeResourceFilePath # output path |
| 53 | + ]) |
| 54 | + if result != 0: |
| 55 | + return False |
| 56 | + return True |
| 57 | + |
| 58 | + def CollectFilesFromFolderWithExtension (self, folderPath, extension): |
| 59 | + result = [] |
| 60 | + for fileName in os.listdir (folderPath): |
| 61 | + fileExtension = os.path.splitext (fileName)[1] |
| 62 | + if fileExtension.lower () == extension.lower (): |
| 63 | + fullPath = os.path.join (folderPath, fileName) |
| 64 | + result.append (fullPath) |
| 65 | + return result |
| 66 | + |
| 67 | +class WinResourceCompiler (ResourceCompiler): |
| 68 | + def __init__ (self, devKitPath, languageCode, sourcesPath, resourcesPath, resourceObjectsPath): |
| 69 | + super (WinResourceCompiler, self).__init__ (devKitPath, languageCode, sourcesPath, resourcesPath, resourceObjectsPath) |
| 70 | + self.resConvPath = os.path.join (devKitPath, 'Support', 'Tools', 'Win', 'ResConv.exe') |
| 71 | + |
| 72 | + def PrecompileResourceFile (self, grcFilePath): |
| 73 | + precompiledGrcFilePath = self.GetPrecompiledResourceFilePath (grcFilePath) |
| 74 | + result = subprocess.call ([ |
| 75 | + 'cl', |
| 76 | + '/nologo', |
| 77 | + '/X', |
| 78 | + '/EP', |
| 79 | + '/P', |
| 80 | + '/I', os.path.join (self.devKitPath, 'Support', 'Inc'), |
| 81 | + '/I', os.path.join (self.devKitPath, 'Support', 'Modules', 'DGLib'), |
| 82 | + '/I', self.sourcesPath, |
| 83 | + '/DWINDOWS', |
| 84 | + '/execution-charset:utf-8', |
| 85 | + '/Fi{}'.format (precompiledGrcFilePath), |
| 86 | + grcFilePath, |
| 87 | + ]) |
| 88 | + assert result == 0, "Failed to precompile resource " + grcFilePath |
| 89 | + return precompiledGrcFilePath |
| 90 | + |
| 91 | + def CompileResourceFile (self, grcFilePath): |
| 92 | + precompiledGrcFilePath = self.PrecompileResourceFile (grcFilePath) |
| 93 | + return self.RunResConv ('W', '1252', precompiledGrcFilePath, '.rc2') |
| 94 | + |
| 95 | + def GetNativeResourceFile (self): |
| 96 | + defaultNativeResourceFile = os.path.join (self.resourcesPath, 'RFIX.win', 'AddOnMain.rc2') |
| 97 | + if os.path.exists (defaultNativeResourceFile): |
| 98 | + return defaultNativeResourceFile |
| 99 | + |
| 100 | + existingNativeResourceFiles = self.CollectFilesFromFolderWithExtension (os.path.join (self.resourcesPath, 'RFIX.win'), '.rc2') |
| 101 | + assert existingNativeResourceFiles, 'Native resource file was not found at RFIX.win folder' |
| 102 | + |
| 103 | + return existingNativeResourceFiles[0] |
| 104 | + |
| 105 | + def CompileNativeResource (self, resultResourcePath): |
| 106 | + nativeResourceFile = self.GetNativeResourceFile () |
| 107 | + result = subprocess.call ([ |
| 108 | + 'rc', |
| 109 | + '/i', os.path.join (self.devKitPath, 'Support', 'Inc'), |
| 110 | + '/i', os.path.join (self.devKitPath, 'Support', 'Modules', 'DGLib'), |
| 111 | + '/i', self.sourcesPath, |
| 112 | + '/i', self.resourceObjectsPath, |
| 113 | + '/fo', resultResourcePath, |
| 114 | + nativeResourceFile |
| 115 | + ]) |
| 116 | + assert result == 0, 'Failed to compile native resource ' + nativeResourceFile |
| 117 | + |
| 118 | +class MacResourceCompiler (ResourceCompiler): |
| 119 | + def __init__ (self, devKitPath, languageCode, sourcesPath, resourcesPath, resourceObjectsPath): |
| 120 | + super (MacResourceCompiler, self).__init__ (devKitPath, languageCode, sourcesPath, resourcesPath, resourceObjectsPath) |
| 121 | + self.resConvPath = os.path.join (devKitPath, 'Support', 'Tools', 'OSX', 'ResConv') |
| 122 | + |
| 123 | + def PrecompileResourceFile (self, grcFilePath): |
| 124 | + precompiledGrcFilePath = self.GetPrecompiledResourceFilePath (grcFilePath) |
| 125 | + result = subprocess.call ([ |
| 126 | + 'clang', |
| 127 | + '-x', 'c++', |
| 128 | + '-E', |
| 129 | + '-P', |
| 130 | + '-Dmacintosh', |
| 131 | + '-I', os.path.join (self.devKitPath, 'Support', 'Inc'), |
| 132 | + '-I', os.path.join (self.devKitPath, 'Support', 'Modules', 'DGLib'), |
| 133 | + '-I', self.sourcesPath, |
| 134 | + '-o', precompiledGrcFilePath, |
| 135 | + grcFilePath, |
| 136 | + ]) |
| 137 | + assert result == 0, "Failed to precompile resource " + grcFilePath |
| 138 | + return precompiledGrcFilePath |
| 139 | + |
| 140 | + def CompileResourceFile (self, grcFilePath): |
| 141 | + precompiledGrcFilePath = self.PrecompileResourceFile (grcFilePath) |
| 142 | + return self.RunResConv ('M', 'utf16', precompiledGrcFilePath, '.ro') |
| 143 | + |
| 144 | + def CompileNativeResource (self, resultResourcePath): |
| 145 | + resultLocalizedResourcePath = os.path.join (resultResourcePath, 'English.lproj') |
| 146 | + if not os.path.exists (resultLocalizedResourcePath): |
| 147 | + os.makedirs (resultLocalizedResourcePath) |
| 148 | + resultLocalizableStringsPath = os.path.join (resultLocalizedResourcePath, 'Localizable.strings') |
| 149 | + resultLocalizableStringsFile = codecs.open (resultLocalizableStringsPath, 'w', 'utf-16') |
| 150 | + for fileName in os.listdir (self.resourceObjectsPath): |
| 151 | + filePath = os.path.join (self.resourceObjectsPath, fileName) |
| 152 | + extension = os.path.splitext (fileName)[1].lower () |
| 153 | + if extension == '.tif': |
| 154 | + shutil.copy (filePath, resultResourcePath) |
| 155 | + elif extension == '.rsrd': |
| 156 | + shutil.copy (filePath, resultLocalizedResourcePath) |
| 157 | + elif extension == '.strings': |
| 158 | + stringsFile = codecs.open (filePath, 'r', 'utf-16') |
| 159 | + resultLocalizableStringsFile.write (stringsFile.read ()) |
| 160 | + stringsFile.close () |
| 161 | + resultLocalizableStringsFile.close () |
| 162 | + |
| 163 | +def Main (argv): |
| 164 | + assert len (argv) == 7, 'Usage: CompileResources.py <languageCode> <devKitPath> <sourcesPath> <resourcesPath> <resourceObjectsPath> <resultResourcePath>' |
| 165 | + |
| 166 | + currentDir = os.path.dirname (os.path.abspath (__file__)) |
| 167 | + os.chdir (currentDir) |
| 168 | + |
| 169 | + languageCode = argv[1] |
| 170 | + devKitPath = os.path.abspath (argv[2]) |
| 171 | + sourcesPath = os.path.abspath (argv[3]) |
| 172 | + resourcesPath = os.path.abspath (argv[4]) |
| 173 | + resourceObjectsPath = os.path.abspath (argv[5]) |
| 174 | + resultResourcePath = os.path.abspath (argv[6]) |
| 175 | + |
| 176 | + resourceCompiler = None |
| 177 | + system = platform.system () |
| 178 | + if system == 'Windows': |
| 179 | + resourceCompiler = WinResourceCompiler (devKitPath, languageCode, sourcesPath, resourcesPath, resourceObjectsPath) |
| 180 | + elif system == 'Darwin': |
| 181 | + resourceCompiler = MacResourceCompiler (devKitPath, languageCode, sourcesPath, resourcesPath, resourceObjectsPath) |
| 182 | + |
| 183 | + assert resourceCompiler, 'Platform is not supported' |
| 184 | + assert resourceCompiler.IsValid (), 'Invalid resource compiler' |
| 185 | + |
| 186 | + resourceCompiler.CompileLocalizedResources () |
| 187 | + resourceCompiler.CompileFixResources () |
| 188 | + resourceCompiler.CompileNativeResource (resultResourcePath) |
| 189 | + |
| 190 | + return 0 |
| 191 | + |
| 192 | +sys.exit (Main (sys.argv)) |
0 commit comments