|
| 1 | +#!/usr/bin/python |
| 2 | +import argparse |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import re |
| 6 | +import collections |
| 7 | +import glob |
| 8 | + |
| 9 | +############################# |
| 10 | +### |
| 11 | +### HoI 4 News Event Title Header Adder by Yard1, originally for Equestria at War mod |
| 12 | +### Written in Python 3.7 |
| 13 | +### |
| 14 | +### Copyright (c) 2018 Antoni Baum (Yard1) |
| 15 | +### Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 16 | +### The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 17 | +### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 18 | +### |
| 19 | +### usage: hoi4newspaperheaderadded.py [-h] [--scripted_loc scripted_loc] mod_path |
| 20 | +### |
| 21 | +### Given a mod folder, add a scripted localisation call to every news event title |
| 22 | +### (including triggered titles). |
| 23 | +### |
| 24 | +### positional arguments: |
| 25 | +### mod_path Path to the root mod folder |
| 26 | +### |
| 27 | +### optional arguments: |
| 28 | +### -h, --help show this help message and exit |
| 29 | +### --scripted_loc scripted_loc |
| 30 | +### The full string (including brackets) to prefix |
| 31 | +### localisation values with (Default: |
| 32 | +### [This.GetNewspaperHeader]) |
| 33 | +### |
| 34 | +############################# |
| 35 | + |
| 36 | +def readable_dir(prospective_dir): |
| 37 | + if not os.path.isdir(prospective_dir): |
| 38 | + raise Exception("readable_dir:{0} is not a valid path".format(prospective_dir)) |
| 39 | + if os.access(prospective_dir, os.R_OK): |
| 40 | + return prospective_dir |
| 41 | + else: |
| 42 | + raise Exception("readable_dir:{0} is not a readable dir".format(prospective_dir)) |
| 43 | + |
| 44 | +############################# |
| 45 | + |
| 46 | +def read_event_file(name, loc_set): |
| 47 | + print("Reading file " + name + "...") |
| 48 | + lines = [] |
| 49 | + try: |
| 50 | + with open(name, "r") as f: |
| 51 | + lines = f.read().splitlines() |
| 52 | + except: |
| 53 | + try: |
| 54 | + with open(name, "r", encoding='utf-8') as f: |
| 55 | + lines = f.read().splitlines() |
| 56 | + except: |
| 57 | + try: |
| 58 | + with open(name, "r", encoding='utf-8-sig') as f: |
| 59 | + lines = f.read().splitlines() |
| 60 | + except: |
| 61 | + print("Could not read file " + name + "!") |
| 62 | + |
| 63 | + open_blocks = 0 |
| 64 | + is_in_news_event = False |
| 65 | + is_in_title = False |
| 66 | + for line in lines: |
| 67 | + line = re.sub(r'#.*?$', "", line) |
| 68 | + if open_blocks == 0 and "news_event" in line: |
| 69 | + is_in_news_event = True |
| 70 | + if is_in_news_event: |
| 71 | + match = re.search(r'(^|\s)\s*title\s*=\s*([^\{]+?)(\s|$)', line) |
| 72 | + if match: |
| 73 | + loc_set.add(match.group(2).strip()) |
| 74 | + else: |
| 75 | + match = re.search(r'(^|\s)\s*title\s*=\s*{', line) |
| 76 | + if match: |
| 77 | + is_in_title = True |
| 78 | + if is_in_title: |
| 79 | + match = re.search(r'(^|\s)\s*text\s*=\s*([^\{]+?)\s*($|\})', line) |
| 80 | + if match: |
| 81 | + loc_set.add(match.group(2).strip()) |
| 82 | + open_blocks += line.count('{') |
| 83 | + open_blocks -= line.count('}') |
| 84 | + if open_blocks == 1: |
| 85 | + is_in_title = False |
| 86 | + if is_in_news_event and open_blocks == 0: |
| 87 | + is_in_news_event = False |
| 88 | + |
| 89 | + print("File " + name + " read successfully!") |
| 90 | + return |
| 91 | + |
| 92 | +def read_loc_file(name, loc_set, scripted_loc_re, scripted_loc): |
| 93 | + print("Reading file " + name + "...") |
| 94 | + lines = [] |
| 95 | + try: |
| 96 | + with open(name, "r", encoding='utf-8-sig') as f: |
| 97 | + lines = f.read().splitlines() |
| 98 | + except: |
| 99 | + print("Could not read file " + name + "!") |
| 100 | + print("File " + name + " read successfully!") |
| 101 | + new_lines = [] |
| 102 | + has_changed = False |
| 103 | + for line in lines: |
| 104 | + match = re.search(r'^\s*?([^#\s]*?):[0-9]+', line) |
| 105 | + if match: |
| 106 | + loc_key = match.group(1).strip() |
| 107 | + if loc_key in loc_set: |
| 108 | + line = scripted_loc_re.sub("\\1\\2" + scripted_loc, line) |
| 109 | + has_changed = True |
| 110 | + new_lines.append(line) |
| 111 | + if has_changed: |
| 112 | + with open(name, "w", encoding="utf-8-sig") as f: |
| 113 | + f.writelines(str(line) + "\n" for line in new_lines) |
| 114 | + print("File " + name + " modified successfully!") |
| 115 | + return |
| 116 | +################################################################### |
| 117 | +parser = argparse.ArgumentParser(description='Given a mod folder, add a scripted localisation call to every news event title (including triggered titles).') |
| 118 | +parser.add_argument('mod_path', metavar='mod_path', |
| 119 | + help='Path to the root mod folder') |
| 120 | +parser.add_argument( '--scripted_loc', metavar='scripted_loc', default="[This.GetNewspaperHeader]", required=False, |
| 121 | + help='The full string (including brackets) to prefix localisation values with (Default: [This.GetNewspaperHeader])') |
| 122 | + |
| 123 | +args = parser.parse_args() |
| 124 | + |
| 125 | +events_path = os.path.join(args.mod_path, 'events') |
| 126 | +loc_path = os.path.join(args.mod_path, 'localisation') |
| 127 | + |
| 128 | +try: |
| 129 | + dir = readable_dir(events_path) |
| 130 | +except: |
| 131 | + print("{0} is not a directory or does not exist." % events_path) |
| 132 | + |
| 133 | +try: |
| 134 | + dir = readable_dir(loc_path) |
| 135 | +except: |
| 136 | + print("{0} is not a directory or does not exist." % loc_path) |
| 137 | + |
| 138 | +loc_set = set() |
| 139 | + |
| 140 | +for file in glob.glob(os.path.join(events_path, '*.txt')): |
| 141 | + read_event_file(file, loc_set) |
| 142 | + |
| 143 | +scripted_loc = args.scripted_loc.strip() |
| 144 | +scripted_loc_re_string = r'(\s*?[^\s]*?:[0-9]+\s*)(\")(?!' + re.escape(scripted_loc) + r')' |
| 145 | +scripted_loc_re = re.compile(scripted_loc_re_string, re.IGNORECASE) |
| 146 | + |
| 147 | +for file in glob.glob(os.path.join(loc_path, '*.yml'), recursive=True): |
| 148 | + read_loc_file(file, loc_set, scripted_loc_re, scripted_loc) |
| 149 | + |
| 150 | +try: |
| 151 | + dir = readable_dir(os.path.join(loc_path, "replace")) |
| 152 | + for file in glob.glob(os.path.join(loc_path, "replace", '*.yml'), recursive=True): |
| 153 | + read_loc_file(file, loc_set, scripted_loc_re, scripted_loc) |
| 154 | +except: |
| 155 | + pass |
0 commit comments