|
| 1 | +# Created with python 3.11.4 |
| 2 | + |
| 3 | +# This script helps with moving cpp files from Generals or GeneralsMD to Core |
| 4 | + |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +from enum import Enum |
| 8 | + |
| 9 | + |
| 10 | +class Game(Enum): |
| 11 | + GENERALS = 0 |
| 12 | + ZEROHOUR = 1 |
| 13 | + CORE = 2 |
| 14 | + |
| 15 | + |
| 16 | +class CmakeModifyType(Enum): |
| 17 | + ADD_COMMENT = 0 |
| 18 | + REMOVE_COMMENT = 1 |
| 19 | + |
| 20 | + |
| 21 | +current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 22 | +root_dir = os.path.join(current_dir, "..", "..") |
| 23 | +root_dir = os.path.normpath(root_dir) |
| 24 | +core_dir = os.path.join(root_dir, "Core") |
| 25 | +generals_dir = os.path.join(root_dir, "Generals", "Code") |
| 26 | +generalsmd_dir = os.path.join(root_dir, "GeneralsMD", "Code") |
| 27 | + |
| 28 | + |
| 29 | +def get_game_path(game: Game): |
| 30 | + if game == Game.GENERALS: |
| 31 | + return generals_dir |
| 32 | + elif game == Game.ZEROHOUR: |
| 33 | + return generalsmd_dir |
| 34 | + elif game == Game.CORE: |
| 35 | + return core_dir |
| 36 | + assert(0) |
| 37 | + |
| 38 | + |
| 39 | +def get_opposite_game(game: Game): |
| 40 | + if game == Game.GENERALS: |
| 41 | + return Game.ZEROHOUR |
| 42 | + elif game == Game.ZEROHOUR: |
| 43 | + return Game.GENERALS |
| 44 | + assert(0) |
| 45 | + |
| 46 | + |
| 47 | +def move_file(fromGame: Game, fromFile: str, toGame: Game, toFile: str): |
| 48 | + fromPath = os.path.join(get_game_path(fromGame), os.path.normpath(fromFile)) |
| 49 | + toPath = os.path.join(get_game_path(toGame), os.path.normpath(toFile)) |
| 50 | + os.makedirs(os.path.dirname(toPath), exist_ok=True) |
| 51 | + shutil.move(fromPath, toPath) |
| 52 | + |
| 53 | + |
| 54 | +def delete_file(game: Game, path: str): |
| 55 | + os.remove(os.path.join(get_game_path(game), os.path.normpath(path))) |
| 56 | + |
| 57 | + |
| 58 | +def modify_cmakelists(cmakeFile: str, searchString: str, type: CmakeModifyType): |
| 59 | + lines: list[str] |
| 60 | + with open(cmakeFile, 'r', encoding="ascii") as file: |
| 61 | + lines = file.readlines() |
| 62 | + |
| 63 | + with open(cmakeFile, 'w', encoding="ascii") as file: |
| 64 | + for index, line in enumerate(lines): |
| 65 | + if searchString in line: |
| 66 | + if type == CmakeModifyType.ADD_COMMENT: |
| 67 | + lines[index] = "#" + line |
| 68 | + else: |
| 69 | + lines[index] = line.replace("#", "", 1) |
| 70 | + |
| 71 | + file.writelines(lines) |
| 72 | + |
| 73 | + |
| 74 | +def unify_file(fromGame: Game, fromFile: str, toGame: Game, toFile: str): |
| 75 | + assert(toGame == Game.CORE) |
| 76 | + |
| 77 | + fromOppositeGame = get_opposite_game(fromGame) |
| 78 | + fromOppositeGamePath = get_game_path(fromOppositeGame) |
| 79 | + fromGamePath = get_game_path(fromGame) |
| 80 | + toGamePath = get_game_path(toGame) |
| 81 | + |
| 82 | + fromFirstFolderIndex = fromFile.find("/") |
| 83 | + toFirstFolderIndex = toFile.find("/") |
| 84 | + assert(fromFirstFolderIndex > 0) |
| 85 | + assert(toFirstFolderIndex > 0) |
| 86 | + |
| 87 | + fromFirstFolderName = fromFile[:fromFirstFolderIndex] |
| 88 | + toFirstFolderName = toFile[:toFirstFolderIndex] |
| 89 | + fromFileInCmake = fromFile[fromFirstFolderIndex+1:] |
| 90 | + toFileInCmake = toFile[toFirstFolderIndex+1:] |
| 91 | + |
| 92 | + fromOppositeCmakeFile = os.path.join(fromOppositeGamePath, fromFirstFolderName, "CMakeLists.txt") |
| 93 | + fromCmakeFile = os.path.join(fromGamePath, fromFirstFolderName, "CMakeLists.txt") |
| 94 | + toCmakeFile = os.path.join(toGamePath, toFirstFolderName, "CMakeLists.txt") |
| 95 | + |
| 96 | + modify_cmakelists(fromOppositeCmakeFile, fromFileInCmake, CmakeModifyType.ADD_COMMENT) |
| 97 | + modify_cmakelists(fromCmakeFile, fromFileInCmake, CmakeModifyType.ADD_COMMENT) |
| 98 | + modify_cmakelists(toCmakeFile, toFileInCmake, CmakeModifyType.REMOVE_COMMENT) |
| 99 | + |
| 100 | + delete_file(fromOppositeGame, fromFile) |
| 101 | + move_file(fromGame, fromFile, toGame, toFile) |
| 102 | + |
| 103 | + |
| 104 | +def unify_move_file(fromGame: Game, fromFile: str, toGame: Game, toFile: str): |
| 105 | + assert(toGame == Game.CORE) |
| 106 | + |
| 107 | + fromGamePath = get_game_path(fromGame) |
| 108 | + toGamePath = get_game_path(toGame) |
| 109 | + |
| 110 | + fromFirstFolderIndex = fromFile.find("/") |
| 111 | + toFirstFolderIndex = toFile.find("/") |
| 112 | + assert(fromFirstFolderIndex > 0) |
| 113 | + assert(toFirstFolderIndex > 0) |
| 114 | + |
| 115 | + fromFirstFolderName = fromFile[:fromFirstFolderIndex] |
| 116 | + toFirstFolderName = toFile[:toFirstFolderIndex] |
| 117 | + fromFileInCmake = fromFile[fromFirstFolderIndex+1:] |
| 118 | + toFileInCmake = toFile[toFirstFolderIndex+1:] |
| 119 | + |
| 120 | + fromCmakeFile = os.path.join(fromGamePath, fromFirstFolderName, "CMakeLists.txt") |
| 121 | + toCmakeFile = os.path.join(toGamePath, toFirstFolderName, "CMakeLists.txt") |
| 122 | + |
| 123 | + modify_cmakelists(fromCmakeFile, fromFileInCmake, CmakeModifyType.ADD_COMMENT) |
| 124 | + modify_cmakelists(toCmakeFile, toFileInCmake, CmakeModifyType.REMOVE_COMMENT) |
| 125 | + |
| 126 | + move_file(fromGame, fromFile, toGame, toFile) |
| 127 | + |
| 128 | + |
| 129 | +def main(): |
| 130 | + |
| 131 | + return |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + main() |
0 commit comments