diff --git a/at_remove_duplicate_tilesets/remove_duplicate_tileset_usages.py b/at_remove_duplicate_tilesets/remove_duplicate_tileset_usages.py
index b77ed79..fb3c7b8 100644
--- a/at_remove_duplicate_tilesets/remove_duplicate_tileset_usages.py
+++ b/at_remove_duplicate_tilesets/remove_duplicate_tileset_usages.py
@@ -5,7 +5,9 @@ import xml.etree.ElementTree
import zlib
import gzip
import struct
+from re import RegexFlag
from typing import Any
+import re
def get_min_gid_after(after: int, tilesets: list[tuple[Any, int]]):
@@ -14,6 +16,33 @@ def get_min_gid_after(after: int, tilesets: list[tuple[Any, int]]):
)
+def remove_tilesets(
+ filepath: str, duplicates: list[tuple[int, int, int, str, int, int]]
+):
+ print(f"writing file: {filepath}")
+ with open(filepath, "r", encoding="utf-8") as f:
+ content = f.read()
+ for duplicate in duplicates:
+ content = remove_tileset_with_regex(content, duplicate[3], duplicate[0])
+
+ with open(filepath, "w", encoding="utf-8") as f:
+ f.write(content)
+ pass
+
+
+def remove_tileset_with_regex(content, tileset_name, tileset_first_gid):
+ first_gid = tileset_first_gid
+ name = tileset_name
+ modified = re.sub(
+ f' <[^<]*firstgid="{first_gid}"[^<]*({name})[\\S\\s]*?\\n',
+ "",
+ content,
+ 1,
+ RegexFlag.MULTILINE,
+ )
+ return modified
+
+
def remove_duplicate_usages(xml_file_path: str):
print(f"checking {xml_file_path}")
tree = xml.etree.ElementTree.parse(xml_file_path)
@@ -27,6 +56,7 @@ def remove_duplicate_usages(xml_file_path: str):
found_duplicate = len(duplicates) > 0
if not found_duplicate:
+ print("no duplicates found")
return
# basically a map where the first two numbers are the range, that needs to be
@@ -78,8 +108,11 @@ def remove_duplicate_usages(xml_file_path: str):
modified = original.replace(data, modified_data_str)
f.write(modified)
+ remove_tilesets(xml_file_path, duplicate_usages_maps)
+
print(
- f"XML file has unused duplicate tilesets please remove them manually: '{xml_file_path}' "
+ "XML file had duplicate tilesets. They were removed automatically"
+ f", so please check if this worked: '{xml_file_path}' "
)
diff --git a/at_remove_duplicate_tilesets/test_remove_duplicate_tileset_usages.py b/at_remove_duplicate_tilesets/test_remove_duplicate_tileset_usages.py
new file mode 100644
index 0000000..21ede32
--- /dev/null
+++ b/at_remove_duplicate_tilesets/test_remove_duplicate_tileset_usages.py
@@ -0,0 +1,490 @@
+from unittest import TestCase
+
+from remove_duplicate_tileset_usages import remove_tileset_with_regex
+
+sample_input = """
+
+
+
+"""
+expected_output = """
+
+
+
+"""
+
+
+class Test(TestCase):
+
+ def test_remove_tileset_with_regex(self):
+ """
+ should remove this tileset:
+
+
+
+ """
+ response = remove_tileset_with_regex(sample_input, "map_tree_1", 8181)
+ self.assertEqual(
+ response, expected_output, "removed stuff was not what we expected"
+ )