|
1 | 1 | import os |
2 | 2 |
|
3 | 3 | class ReferenceUpdater: |
4 | | - SCANNABLE_EXTS = {'.vmdl', '.vsmart', '.vmat', '.vpcf', '.vsndevts', '.vsnd'} |
| 4 | + SCANNABLE_EXTS = {'.vmdl', '.vsmart', '.vmat', '.vpcf', '.vsndevts', '.vsnd', '.vmap'} |
5 | 5 |
|
6 | 6 | def __init__(self, addon_content_path: str): |
7 | 7 | self.addon_content_path = addon_content_path |
8 | 8 |
|
| 9 | + def _process_element_values(self, element, old_rel: str, new_rel: str) -> bool: |
| 10 | + """Recursively process all string values in an Element, replacing old_rel with new_rel.""" |
| 11 | + modified = False |
| 12 | + try: |
| 13 | + keys = list(element.Keys) |
| 14 | + except Exception: |
| 15 | + return False |
| 16 | + |
| 17 | + for key in keys: |
| 18 | + try: |
| 19 | + val = element[key] |
| 20 | + except Exception: |
| 21 | + continue |
| 22 | + |
| 23 | + if isinstance(val, str) and old_rel in val: |
| 24 | + try: |
| 25 | + element[key] = val.replace(old_rel, new_rel) |
| 26 | + modified = True |
| 27 | + except Exception: |
| 28 | + pass |
| 29 | + elif hasattr(val, 'Keys'): |
| 30 | + # Nested Element – recurse |
| 31 | + if self._process_element_values(val, old_rel, new_rel): |
| 32 | + modified = True |
| 33 | + elif hasattr(val, 'Count') and hasattr(val, 'Item'): |
| 34 | + # Array/List attribute |
| 35 | + try: |
| 36 | + for i in range(val.Count): |
| 37 | + item_val = val[i] |
| 38 | + if isinstance(item_val, str) and old_rel in item_val: |
| 39 | + val[i] = item_val.replace(old_rel, new_rel) |
| 40 | + modified = True |
| 41 | + elif hasattr(item_val, 'Keys'): |
| 42 | + if self._process_element_values(item_val, old_rel, new_rel): |
| 43 | + modified = True |
| 44 | + except Exception: |
| 45 | + pass |
| 46 | + |
| 47 | + return modified |
| 48 | + |
| 49 | + def _update_vmap_references(self, abs_path: str, old_rel: str, new_rel: str) -> bool: |
| 50 | + import tempfile |
| 51 | + import shutil |
| 52 | + |
| 53 | + dmx_model = None |
| 54 | + temp_path = None |
| 55 | + try: |
| 56 | + from src.dotnet import setup_keyvalues2 |
| 57 | + Datamodel, Element, DeferredMode = setup_keyvalues2() |
| 58 | + |
| 59 | + # Load from a temporary copy so the original file is not locked |
| 60 | + with tempfile.NamedTemporaryFile(mode='wb', suffix='.vmap', delete=False) as tmp: |
| 61 | + temp_path = tmp.name |
| 62 | + with open(abs_path, 'rb') as src: |
| 63 | + shutil.copyfileobj(src, tmp) |
| 64 | + |
| 65 | + dmx_model = Datamodel.Load(temp_path, DeferredMode.Automatic) |
| 66 | + if not dmx_model: |
| 67 | + return False |
| 68 | + |
| 69 | + modified = False |
| 70 | + |
| 71 | + # Process PrefixAttributes (AttributeList – a Dictionary<string, object>) |
| 72 | + if hasattr(dmx_model, 'PrefixAttributes') and dmx_model.PrefixAttributes: |
| 73 | + try: |
| 74 | + for key in list(dmx_model.PrefixAttributes.Keys): |
| 75 | + val = dmx_model.PrefixAttributes[key] |
| 76 | + if isinstance(val, str) and old_rel in val: |
| 77 | + dmx_model.PrefixAttributes[key] = val.replace(old_rel, new_rel) |
| 78 | + modified = True |
| 79 | + elif hasattr(val, 'Count') and hasattr(val, 'Item'): |
| 80 | + try: |
| 81 | + for i in range(val.Count): |
| 82 | + item_val = val[i] |
| 83 | + if isinstance(item_val, str) and old_rel in item_val: |
| 84 | + val[i] = item_val.replace(old_rel, new_rel) |
| 85 | + modified = True |
| 86 | + except Exception: |
| 87 | + pass |
| 88 | + except Exception as e: |
| 89 | + print(f"Warning: failed to process PrefixAttributes in {abs_path}: {e}") |
| 90 | + |
| 91 | + # Process AllElements (ElementList – iterates as DictionaryEntry) |
| 92 | + # Each DictionaryEntry.Value is the actual Element object |
| 93 | + if hasattr(dmx_model, 'AllElements') and dmx_model.AllElements: |
| 94 | + try: |
| 95 | + for entry in dmx_model.AllElements: |
| 96 | + element = entry.Value if hasattr(entry, 'Value') else entry |
| 97 | + if self._process_element_values(element, old_rel, new_rel): |
| 98 | + modified = True |
| 99 | + except Exception as e: |
| 100 | + print(f"Warning: failed to process AllElements in {abs_path}: {e}") |
| 101 | + |
| 102 | + if modified: |
| 103 | + # Save to the original path (not locked since we loaded from temp) |
| 104 | + dmx_model.Save(abs_path, dmx_model.Encoding, dmx_model.EncodingVersion) |
| 105 | + |
| 106 | + if hasattr(dmx_model, 'Dispose'): |
| 107 | + dmx_model.Dispose() |
| 108 | + dmx_model = None |
| 109 | + import gc; gc.collect() |
| 110 | + |
| 111 | + return modified |
| 112 | + except Exception as e: |
| 113 | + print(f"Error updating vmap references via .NET in {abs_path}: {e}") |
| 114 | + return False |
| 115 | + finally: |
| 116 | + if dmx_model is not None: |
| 117 | + try: |
| 118 | + if hasattr(dmx_model, 'Dispose'): |
| 119 | + dmx_model.Dispose() |
| 120 | + except Exception: |
| 121 | + pass |
| 122 | + if temp_path: |
| 123 | + try: |
| 124 | + os.unlink(temp_path) |
| 125 | + except Exception: |
| 126 | + pass |
| 127 | + |
9 | 128 | def update_references(self, old_rel: str, new_rel: str) -> list[str]: |
10 | 129 | modified = [] |
11 | 130 | old_rel = old_rel.replace('\\', '/') |
12 | 131 | new_rel = new_rel.replace('\\', '/') |
13 | 132 | for root, _, files in os.walk(self.addon_content_path): |
14 | 133 | for f in files: |
15 | | - if os.path.splitext(f)[1].lower() not in self.SCANNABLE_EXTS: |
| 134 | + ext = os.path.splitext(f)[1].lower() |
| 135 | + if ext not in self.SCANNABLE_EXTS: |
16 | 136 | continue |
17 | 137 | abs_path = os.path.join(root, f) |
18 | 138 | try: |
19 | | - with open(abs_path, 'r', encoding='utf-8', errors='ignore') as file: |
20 | | - text = file.read() |
21 | | - if old_rel in text: |
22 | | - new_text = text.replace(old_rel, new_rel) |
23 | | - with open(abs_path, 'w', encoding='utf-8') as file: |
24 | | - file.write(new_text) |
25 | | - modified.append(abs_path) |
| 139 | + if ext == '.vmap': |
| 140 | + if self._update_vmap_references(abs_path, old_rel, new_rel): |
| 141 | + modified.append(abs_path) |
| 142 | + else: |
| 143 | + with open(abs_path, 'r', encoding='utf-8', errors='ignore') as file: |
| 144 | + text = file.read() |
| 145 | + if old_rel in text: |
| 146 | + new_text = text.replace(old_rel, new_rel) |
| 147 | + with open(abs_path, 'w', encoding='utf-8') as file: |
| 148 | + file.write(new_text) |
| 149 | + modified.append(abs_path) |
26 | 150 | except Exception as e: |
27 | 151 | print(f"Error updating references in {abs_path}: {e}") |
28 | 152 | return modified |
0 commit comments