1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# -*- encoding: utf-8 -*-
"""
OMOD Installer plugin for Mod Organizer 2 (Linux port).
Handles .omod archives (Oblivion Mod Manager format) by parsing the binary
config, extracting compressed data/plugin streams, and re-packaging as a
standard zip for MO2's installation manager.
"""
import io
import lzma
import os
import shutil
import struct
import sys
import tempfile
import zipfile
import zlib
from pathlib import Path
import mobase
def _log_error(msg: str) -> None:
print(f"[OMOD] {msg}", file=sys.stderr)
def _log_warn(msg: str) -> None:
print(f"[OMOD] WARNING: {msg}", file=sys.stderr)
class OmodInstaller(mobase.IPluginInstallerCustom):
_organizer: mobase.IOrganizer
def init(self, organizer: mobase.IOrganizer) -> bool:
self._organizer = organizer
return True
def name(self) -> str:
return "OMOD Installer"
def localizedName(self) -> str:
return "OMOD Installer"
def author(self) -> str:
return "Fluorine Manager"
def description(self) -> str:
return "Installer for .omod archives (Oblivion Mod Manager format)"
def version(self) -> mobase.VersionInfo:
return mobase.VersionInfo(1, 0, 0)
def settings(self) -> list[mobase.PluginSetting]:
return []
def priority(self) -> int:
return 500
def isManualInstaller(self) -> bool:
return False
def isArchiveSupported(self, archive) -> bool:
# Called with IFileTree (mod list refresh) or str (install check).
if isinstance(archive, str):
return archive.lower().endswith(".omod")
# IFileTree: look for the "config" file that every OMOD contains.
try:
for entry in archive:
if entry.isFile() and entry.name() == "config":
return True
except TypeError:
pass
return False
def supportedExtensions(self) -> set[str]:
return {"omod"}
def install(
self,
mod_name: mobase.GuessedString,
game_name: str,
archive_name: str,
version: str,
nexus_id: int,
) -> mobase.InstallResult:
try:
return self._do_install(mod_name, archive_name)
except Exception as e:
_log_error(f"OMOD install failed: {e}")
return mobase.InstallResult.FAILED
def _do_install(
self,
mod_name: mobase.GuessedString,
archive_name: str,
) -> mobase.InstallResult:
with zipfile.ZipFile(archive_name, "r") as zf:
names = zf.namelist()
if "config" not in names:
_log_warn("no config entry found")
return mobase.InstallResult.NOT_ATTEMPTED
config = self._parse_config(zf.read("config"))
if config.get("mod_name"):
mod_name.update(config["mod_name"])
compression = config.get("compression_type", 0)
tmpdir = tempfile.mkdtemp(prefix="omod_")
try:
self._extract_stream(
zf, names, "data", "data.crc", compression, tmpdir
)
self._extract_stream(
zf, names, "plugins", "plugins.crc", compression, tmpdir
)
# If the OMOD contains a readme, extract it too.
for entry in names:
lower = entry.lower()
if lower == "readme" or lower.startswith("readme."):
data = zf.read(entry)
dest = Path(tmpdir) / entry
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_bytes(data)
# Collect extracted files.
file_list = []
for root, _dirs, files in os.walk(tmpdir):
for f in files:
full = os.path.join(root, f)
rel = os.path.relpath(full, tmpdir)
file_list.append(rel)
if not file_list:
_log_warn("no files extracted")
return mobase.InstallResult.FAILED
# Repackage as a standard zip for MO2's installer.
repack_path = os.path.join(tmpdir, "_repack.zip")
with zipfile.ZipFile(repack_path, "w", zipfile.ZIP_DEFLATED) as out_zip:
for rel in file_list:
out_zip.write(os.path.join(tmpdir, rel), rel)
result, _, _ = self.manager().installArchive(mod_name, repack_path)
return result
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
def _extract_stream(
self,
zf: zipfile.ZipFile,
names: list[str],
stream_name: str,
crc_name: str,
compression: int,
out_dir: str,
) -> None:
if stream_name not in names:
return
if crc_name not in names:
_log_warn(f"{stream_name} present but {crc_name} missing")
return
file_list = self._parse_crc_file(zf.read(crc_name))
if not file_list:
return
raw = zf.read(stream_name)
decompressed = self._decompress_stream(raw, compression)
offset = 0
for path, size in file_list:
if offset + size > len(decompressed):
_log_warn(
f"truncated stream for {path} "
f"(need {size} bytes at offset {offset}, "
f"have {len(decompressed)})"
)
break
file_data = decompressed[offset : offset + size]
offset += size
# Normalise path separators from Windows.
path = path.replace("\\", "/")
dest = Path(out_dir) / path
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_bytes(file_data)
def _parse_config(self, data: bytes) -> dict:
"""Parse the OMOD binary config entry."""
reader = io.BytesIO(data)
config = {}
config["file_version"] = struct.unpack("<B", reader.read(1))[0]
config["mod_name"] = self._read_net_string(reader)
# Major and minor version.
config["major"] = struct.unpack("<i", reader.read(4))[0]
config["minor"] = struct.unpack("<i", reader.read(4))[0]
config["author"] = self._read_net_string(reader)
config["email"] = self._read_net_string(reader)
config["website"] = self._read_net_string(reader)
config["description"] = self._read_net_string(reader)
# Creation time (Windows FILETIME, 8 bytes) - skip.
reader.read(8)
# Compression type: 0 = deflate, 1 = lzma.
config["compression_type"] = struct.unpack("<B", reader.read(1))[0]
return config
def _parse_crc_file(self, data: bytes) -> list[tuple[str, int]]:
"""Parse data.crc or plugins.crc.
Returns [(relative_path, uncompressed_size), ...].
"""
reader = io.BytesIO(data)
count = self._read_7bit_encoded_int(reader)
files = []
for _ in range(count):
path = self._read_net_string(reader)
_crc = struct.unpack("<I", reader.read(4))[0]
size = struct.unpack("<q", reader.read(8))[0]
files.append((path, size))
return files
def _decompress_stream(self, data: bytes, compression_type: int) -> bytes:
"""Decompress an OMOD data or plugins stream."""
if compression_type == 0:
# Raw deflate (no zlib/gzip header).
return zlib.decompress(data, -15)
elif compression_type == 1:
return lzma.decompress(data)
else:
raise ValueError(f"Unknown OMOD compression type: {compression_type}")
@staticmethod
def _read_net_string(reader: io.BytesIO) -> str:
"""Read a .NET BinaryWriter-style length-prefixed string.
The length is encoded as a 7-bit encoded int, followed by that many
bytes of UTF-8.
"""
length = OmodInstaller._read_7bit_encoded_int(reader)
if length == 0:
return ""
raw = reader.read(length)
return raw.decode("utf-8", errors="replace")
@staticmethod
def _read_7bit_encoded_int(reader: io.BytesIO) -> int:
"""Read a .NET 7-bit encoded integer."""
result = 0
shift = 0
while True:
byte_data = reader.read(1)
if not byte_data:
break
b = byte_data[0]
result |= (b & 0x7F) << shift
shift += 7
if (b & 0x80) == 0:
break
return result
def createPlugin() -> mobase.IPlugin:
return OmodInstaller()
|