Skip to content

Commit

Permalink
control which languages get patched
Browse files Browse the repository at this point in the history
  • Loading branch information
BLooperZ committed Apr 15, 2023
1 parent a66cf2c commit 1926d4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 18 additions & 3 deletions boozook/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import itertools
import os
from pathlib import Path
from typing import Iterable, Iterator, MutableMapping, Optional, Sequence
from typing import Iterable, Iterator, MutableMapping, Optional, Sequence, Set

from boozook.codex import stk
from boozook.codex.stk_compress import recompress_archive
Expand Down Expand Up @@ -39,6 +39,8 @@ def game_search(base_dir, patterns=('*',), patches=()):
class GameBase:
base_dir: str
patches: Optional[Sequence[str]]
allowed_patches: Optional[Set[str]] = None
restricted_patches: Optional[Set[str]] = None

_patched: dict[tuple[str, str], bytes] = field(default_factory=dict)

Expand All @@ -48,6 +50,11 @@ def search(self, patterns):
def patch(self, fname: str, data: bytes, alias: str | None = None):
if not alias:
alias = fname
if self.allowed_patches:
if not any(Path(alias).match(pattern) for pattern in self.allowed_patches):
return
if any(Path(alias).match(pattern) for pattern in self.restricted_patches or ()):
return
self._patched[(fname, alias)] = data

def rebuild(self, target='patch'):
Expand Down Expand Up @@ -76,8 +83,16 @@ def rebuild(self, target='patch'):
raise ValueError(f'archive {arc} was not found')


def open_game(base_dir, patches=()):
return GameBase(base_dir, patches=patches)
def open_game(
base_dir,
patches=(),
allowed_patches=(),
):
return GameBase(
base_dir,
patches=patches,
allowed_patches=set(allowed_patches),
)


class DirectoryBackedArchive(MutableMapping[str, bytes]):
Expand Down
8 changes: 7 additions & 1 deletion boozook/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,20 @@ def encode(game, patterns, texts_dir):
action='store_true',
help='create modifed game resource with the changes',
)
parser.add_argument(
'--allowed',
'-i',
action='append',
help='allow only specific patterns to be modified',
)
args = parser.parse_args()

patterns = TEXT_PATTERNS

texts_dir = Path('texts')
os.makedirs(texts_dir, exist_ok=True)

game = archive.open_game(args.directory)
game = archive.open_game(args.directory, allowed_patches=args.allowed or ())
if not args.rebuild:
decode(game, patterns, texts_dir)
else:
Expand Down

0 comments on commit 1926d4a

Please sign in to comment.