mc-sdk 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ name: Publish mc-sdk to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+ workflow_dispatch:
7
+
8
+ jobs:
9
+ publish:
10
+ runs-on: ubuntu-latest
11
+ permissions:
12
+ id-token: write
13
+ contents: read
14
+
15
+ steps:
16
+ - name: Checkout repository
17
+ uses: actions/checkout@v4
18
+
19
+ - name: Install uv
20
+ uses: astral-sh/setup-uv@v5
21
+
22
+ - name: Set up Python
23
+ run: uv python install 3.12
24
+
25
+ - name: Build package
26
+ run: uv build
27
+
28
+ - name: Publish to PyPI
29
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,11 @@
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
11
+ data/
@@ -0,0 +1 @@
1
+ 3.13
mc_sdk-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
mc_sdk-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,38 @@
1
+ Metadata-Version: 2.5
2
+ Name: mc-sdk
3
+ Version: 0.1.0
4
+ Summary: Modern Python SDK for building Minecraft launchers (parallel downloads, automatic Java, Fabric/Quilt, Modrinth, isolated instances).
5
+ License: MIT License
6
+
7
+ Copyright (c) 2026
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+ License-File: LICENSE
27
+ Keywords: fabric,launcher,minecraft,modrinth,quilt,sdk
28
+ Classifier: Development Status :: 3 - Alpha
29
+ Classifier: Intended Audience :: Developers
30
+ Classifier: License :: OSI Approved :: MIT License
31
+ Classifier: Programming Language :: Python :: 3
32
+ Classifier: Programming Language :: Python :: 3.12
33
+ Classifier: Programming Language :: Python :: 3.13
34
+ Classifier: Topic :: Games/Entertainment
35
+ Classifier: Typing :: Typed
36
+ Requires-Python: >=3.12
37
+ Requires-Dist: pydantic>=2.10
38
+ Requires-Dist: requests>=2.31
mc_sdk-0.1.0/README.md ADDED
File without changes
mc_sdk-0.1.0/main.py ADDED
@@ -0,0 +1,45 @@
1
+ import time
2
+ from contextlib import contextmanager
3
+
4
+ from mc_sdk import (
5
+ Instance,
6
+ InstanceNotFoundError,
7
+ JavaNotFoundError,
8
+ LaunchOptions,
9
+ McSdk,
10
+ ModSearchResult,
11
+ ProgressCallback,
12
+ ProgressEvent,
13
+ VersionNotFoundError,
14
+ )
15
+
16
+ __all__ = [
17
+ "Instance",
18
+ "InstanceNotFoundError",
19
+ "JavaNotFoundError",
20
+ "LaunchOptions",
21
+ "McSdk",
22
+ "ModSearchResult",
23
+ "ProgressCallback",
24
+ "ProgressEvent",
25
+ "VersionNotFoundError",
26
+ ]
27
+
28
+
29
+ @contextmanager
30
+ def measure(nombre="Bloque"):
31
+ inicio = time.perf_counter()
32
+ try:
33
+ yield
34
+ finally:
35
+ print(f"{nombre}: {time.perf_counter() - inicio:.4f} s")
36
+
37
+
38
+ def on_progress(event: ProgressEvent) -> None:
39
+ print(f"[{event.stage}] {event.current}/{event.total} - {event.file}")
40
+
41
+
42
+ if __name__ == "__main__":
43
+ with McSdk() as sdk, measure():
44
+ version = sdk.download_version("1.8.9", on_progress=on_progress)
45
+ print(version)
@@ -0,0 +1,21 @@
1
+ from mc_sdk.client import McSdk
2
+ from mc_sdk.exceptions import (
3
+ InstanceNotFoundError,
4
+ JavaNotFoundError,
5
+ VersionNotFoundError,
6
+ )
7
+ from mc_sdk.instances import Instance
8
+ from mc_sdk.mods import ModSearchResult
9
+ from mc_sdk.types import LaunchOptions, ProgressCallback, ProgressEvent
10
+
11
+ __all__ = [
12
+ "Instance",
13
+ "InstanceNotFoundError",
14
+ "JavaNotFoundError",
15
+ "LaunchOptions",
16
+ "McSdk",
17
+ "ModSearchResult",
18
+ "ProgressCallback",
19
+ "ProgressEvent",
20
+ "VersionNotFoundError",
21
+ ]
@@ -0,0 +1,448 @@
1
+ import logging
2
+ import subprocess
3
+ import time
4
+ from pathlib import Path
5
+
6
+ import requests
7
+ from pydantic import BaseModel, ValidationError
8
+ from requests.adapters import HTTPAdapter
9
+
10
+ from mc_sdk.downloader import Downloader
11
+ from mc_sdk.exceptions import InstanceNotFoundError, VersionNotFoundError
12
+ from mc_sdk.instances import (
13
+ Instance,
14
+ list_all_instances,
15
+ load_instance,
16
+ remove_instance,
17
+ )
18
+ from mc_sdk.java import ensure_java_runtime
19
+ from mc_sdk.launcher import build_launch_command as _build_launch_command
20
+ from mc_sdk.launcher import extract_natives as _extract_natives
21
+ from mc_sdk.launcher import launch_version as _launch_version
22
+ from mc_sdk.loaders import (
23
+ fetch_loader_profile,
24
+ list_loader_versions,
25
+ merge_version_profile,
26
+ )
27
+ from mc_sdk.mods import (
28
+ ModSearchResult,
29
+ add_instance_mod,
30
+ install_modrinth_mod,
31
+ list_instance_mods,
32
+ remove_instance_mod,
33
+ search_modrinth_mods,
34
+ toggle_instance_mod,
35
+ )
36
+ from mc_sdk.types import LaunchOptions, ProgressCallback
37
+ from mc_sdk.models import Manifest
38
+ from mc_sdk.models.manifest import Version
39
+ from mc_sdk.models.version import MinecraftVersionManifest
40
+
41
+ logger = logging.getLogger(__name__)
42
+
43
+
44
+ class McSdk:
45
+ MANIFEST_URL = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json"
46
+
47
+ def __init__(
48
+ self,
49
+ minecraft_dir: Path | str = "data",
50
+ session: requests.Session | None = None,
51
+ default_max_age: int = 86400,
52
+ max_workers: int = 32,
53
+ ):
54
+ self.minecraft_dir = Path(minecraft_dir)
55
+ self._owns_session = session is None
56
+ self.session = session or requests.Session()
57
+ self.default_max_age = default_max_age
58
+ self.max_workers = max_workers
59
+ if self._owns_session:
60
+ adapter = HTTPAdapter(
61
+ pool_connections=max_workers, pool_maxsize=max_workers
62
+ )
63
+ self.session.mount("https://", adapter)
64
+ self.session.mount("http://", adapter)
65
+ self._downloader = Downloader(self.session, max_workers=max_workers)
66
+
67
+ def __enter__(self) -> "McSdk":
68
+ return self
69
+
70
+ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
71
+ self.close()
72
+
73
+ def close(self) -> None:
74
+ if self._owns_session:
75
+ self.session.close()
76
+
77
+ def get_minecraft_path(self) -> Path:
78
+ self.minecraft_dir.mkdir(parents=True, exist_ok=True)
79
+ return self.minecraft_dir
80
+
81
+ def _get_cached_model[T: BaseModel](
82
+ self,
83
+ url: str,
84
+ cache_path: Path,
85
+ model_cls: type[T],
86
+ force: bool = False,
87
+ max_age_seconds: int = 86400,
88
+ ) -> T:
89
+ if not force and cache_path.exists() and cache_path.stat().st_size > 0:
90
+ is_expired = (time.time() - cache_path.stat().st_mtime) > max_age_seconds
91
+ if not is_expired:
92
+ try:
93
+ return model_cls.model_validate_json(
94
+ cache_path.read_text(encoding="utf-8")
95
+ )
96
+ except ValidationError as e:
97
+ logger.warning("Error leyendo caché en %s: %s", cache_path.name, e)
98
+
99
+ response = self.session.get(url, timeout=10)
100
+ response.raise_for_status()
101
+ instance = model_cls.model_validate(response.json())
102
+
103
+ cache_path.parent.mkdir(parents=True, exist_ok=True)
104
+ cache_path.write_text(
105
+ instance.model_dump_json(indent=2, by_alias=True), encoding="utf-8"
106
+ )
107
+
108
+ return instance
109
+
110
+ def get_manifest(
111
+ self, force: bool = False, max_age_seconds: int | None = None
112
+ ) -> Manifest:
113
+ age = self.default_max_age if max_age_seconds is None else max_age_seconds
114
+ return self._get_cached_model(
115
+ url=self.MANIFEST_URL,
116
+ cache_path=self.get_minecraft_path() / "manifest.json",
117
+ model_cls=Manifest,
118
+ force=force,
119
+ max_age_seconds=age,
120
+ )
121
+
122
+ def latest_release(self, force: bool = False) -> str:
123
+ return self.get_manifest(force=force).latest.release
124
+
125
+ def latest_snapshot(self, force: bool = False) -> str:
126
+ return self.get_manifest(force=force).latest.snapshot
127
+
128
+ def list_versions(
129
+ self, version_type: str | None = None, force: bool = False
130
+ ) -> list[Version]:
131
+ versions = self.get_manifest(force=force).versions
132
+ if version_type is None:
133
+ return versions
134
+ return [v for v in versions if v.type == version_type]
135
+
136
+ def get_version(
137
+ self, id: str, force: bool = False, max_age_seconds: int | None = None
138
+ ) -> MinecraftVersionManifest | None:
139
+ age = self.default_max_age if max_age_seconds is None else max_age_seconds
140
+ manifest = self.get_manifest(force=force, max_age_seconds=age)
141
+ version = manifest.get_version(id)
142
+
143
+ if version is None:
144
+ local_path = (
145
+ self.get_minecraft_path() / "versions_manifest" / f"{id}.json"
146
+ )
147
+ if local_path.exists():
148
+ return MinecraftVersionManifest.model_validate_json(
149
+ local_path.read_text(encoding="utf-8")
150
+ )
151
+ return None
152
+
153
+ return self._get_cached_model(
154
+ url=version.url,
155
+ cache_path=self.get_minecraft_path()
156
+ / "versions_manifest"
157
+ / f"{version.id}.json",
158
+ model_cls=MinecraftVersionManifest,
159
+ force=force,
160
+ max_age_seconds=age,
161
+ )
162
+
163
+ def get_version_or_raise(
164
+ self, id: str, force: bool = False, max_age_seconds: int | None = None
165
+ ) -> MinecraftVersionManifest:
166
+ version = self.get_version(id, force=force, max_age_seconds=max_age_seconds)
167
+ if version is None:
168
+ raise VersionNotFoundError(
169
+ f"Minecraft version '{id}' not found in manifest."
170
+ )
171
+ return version
172
+
173
+ def download_libraries(
174
+ self,
175
+ version: MinecraftVersionManifest,
176
+ force: bool = False,
177
+ on_progress: ProgressCallback | None = None,
178
+ ) -> list[Path]:
179
+ return self._downloader.download_libraries(
180
+ self.get_minecraft_path(), version, force=force, on_progress=on_progress
181
+ )
182
+
183
+ def download_assets(
184
+ self,
185
+ version: MinecraftVersionManifest,
186
+ force: bool = False,
187
+ on_progress: ProgressCallback | None = None,
188
+ ) -> Path:
189
+ return self._downloader.download_assets(
190
+ self.get_minecraft_path(), version, force=force, on_progress=on_progress
191
+ )
192
+
193
+ def download_version(
194
+ self,
195
+ id: str,
196
+ dest_dir: Path | str | None = None,
197
+ force: bool = False,
198
+ on_progress: ProgressCallback | None = None,
199
+ ) -> Path:
200
+ version = self.get_version_or_raise(id, force=force)
201
+ return self._downloader.download_version(
202
+ self.get_minecraft_path(),
203
+ version,
204
+ dest_dir=dest_dir,
205
+ force=force,
206
+ on_progress=on_progress,
207
+ )
208
+
209
+ def extract_natives(
210
+ self, version: MinecraftVersionManifest, natives_dir: Path
211
+ ) -> Path:
212
+ return _extract_natives(self.get_minecraft_path(), version, natives_dir)
213
+
214
+ def build_launch_command(
215
+ self,
216
+ id: str,
217
+ options: LaunchOptions | None = None,
218
+ ) -> list[str]:
219
+ version = self.get_version_or_raise(id)
220
+ return _build_launch_command(self.get_minecraft_path(), version, options)
221
+
222
+ def ensure_java(
223
+ self,
224
+ component: str,
225
+ force: bool = False,
226
+ on_progress: ProgressCallback | None = None,
227
+ ) -> Path:
228
+ return ensure_java_runtime(
229
+ self.session,
230
+ self._downloader,
231
+ self.get_minecraft_path(),
232
+ component,
233
+ force=force,
234
+ on_progress=on_progress,
235
+ )
236
+
237
+ def launch(
238
+ self,
239
+ id: str,
240
+ options: LaunchOptions | None = None,
241
+ ensure_downloaded: bool = True,
242
+ force_download: bool = False,
243
+ auto_java: bool = True,
244
+ on_progress: ProgressCallback | None = None,
245
+ ) -> subprocess.Popen[bytes]:
246
+ if ensure_downloaded:
247
+ self.download_version(id, force=force_download, on_progress=on_progress)
248
+ version = self.get_version_or_raise(id)
249
+ opts = options or LaunchOptions()
250
+ if auto_java and opts.java_executable == "java":
251
+ java_path = self.ensure_java(
252
+ version.javaVersion.component,
253
+ on_progress=on_progress,
254
+ )
255
+ opts.java_executable = str(java_path)
256
+ return _launch_version(self.get_minecraft_path(), version, opts)
257
+
258
+ def create_instance(
259
+ self,
260
+ name: str,
261
+ version_id: str,
262
+ java_executable: str = "java",
263
+ max_memory: str | None = None,
264
+ min_memory: str | None = None,
265
+ jvm_args: list[str] | None = None,
266
+ resolution: tuple[int, int] | None = None,
267
+ fullscreen: bool = False,
268
+ ) -> Instance:
269
+ instance = Instance(
270
+ name=name,
271
+ version_id=version_id,
272
+ java_executable=java_executable,
273
+ max_memory=max_memory,
274
+ min_memory=min_memory,
275
+ jvm_args=list(jvm_args or []),
276
+ resolution=resolution,
277
+ fullscreen=fullscreen,
278
+ )
279
+ instance.save(self.get_minecraft_path())
280
+ return instance
281
+
282
+ def get_instance(self, name: str) -> Instance | None:
283
+ return load_instance(self.get_minecraft_path(), name)
284
+
285
+ def list_instances(self) -> list[Instance]:
286
+ return list_all_instances(self.get_minecraft_path())
287
+
288
+ def delete_instance(self, name: str) -> bool:
289
+ return remove_instance(self.get_minecraft_path(), name)
290
+
291
+ def launch_instance(
292
+ self,
293
+ name: str,
294
+ username: str = "Player",
295
+ uuid: str | None = None,
296
+ access_token: str = "0",
297
+ user_type: str = "mojang",
298
+ server: str | None = None,
299
+ port: int | None = None,
300
+ ensure_downloaded: bool = True,
301
+ force_download: bool = False,
302
+ on_progress: ProgressCallback | None = None,
303
+ ) -> subprocess.Popen[bytes]:
304
+ instance = self.get_instance(name)
305
+ if instance is None:
306
+ raise InstanceNotFoundError(f"Instance '{name}' not found.")
307
+ options = instance.to_launch_options(
308
+ self.get_minecraft_path(),
309
+ username=username,
310
+ uuid=uuid,
311
+ access_token=access_token,
312
+ user_type=user_type,
313
+ server=server,
314
+ port=port,
315
+ )
316
+ return self.launch(
317
+ instance.version_id,
318
+ options=options,
319
+ ensure_downloaded=ensure_downloaded,
320
+ force_download=force_download,
321
+ on_progress=on_progress,
322
+ )
323
+
324
+ def list_fabric_loaders(self, mc_version: str) -> list[str]:
325
+ return list_loader_versions(self.session, "fabric", mc_version)
326
+
327
+ def list_quilt_loaders(self, mc_version: str) -> list[str]:
328
+ return list_loader_versions(self.session, "quilt", mc_version)
329
+
330
+ def _install_loader(
331
+ self,
332
+ loader: str,
333
+ mc_version: str,
334
+ loader_version: str | None = None,
335
+ force: bool = False,
336
+ on_progress: ProgressCallback | None = None,
337
+ ) -> str:
338
+ if loader not in ("fabric", "quilt"):
339
+ raise ValueError(f"Unsupported loader: {loader}")
340
+ base = self.get_version_or_raise(mc_version, force=force)
341
+ profile = fetch_loader_profile(
342
+ self.session,
343
+ loader,
344
+ mc_version,
345
+ loader_version=loader_version,
346
+ )
347
+ merged = merge_version_profile(base, profile)
348
+
349
+ cache_path = (
350
+ self.get_minecraft_path() / "versions_manifest" / f"{merged.id}.json"
351
+ )
352
+ cache_path.parent.mkdir(parents=True, exist_ok=True)
353
+ cache_path.write_text(
354
+ merged.model_dump_json(indent=2, by_alias=True), encoding="utf-8"
355
+ )
356
+
357
+ self._downloader.download_version(
358
+ self.get_minecraft_path(),
359
+ merged,
360
+ force=force,
361
+ on_progress=on_progress,
362
+ )
363
+ return merged.id
364
+
365
+ def install_fabric(
366
+ self,
367
+ mc_version: str,
368
+ loader_version: str | None = None,
369
+ force: bool = False,
370
+ on_progress: ProgressCallback | None = None,
371
+ ) -> str:
372
+ return self._install_loader(
373
+ "fabric",
374
+ mc_version,
375
+ loader_version=loader_version,
376
+ force=force,
377
+ on_progress=on_progress,
378
+ )
379
+
380
+ def install_quilt(
381
+ self,
382
+ mc_version: str,
383
+ loader_version: str | None = None,
384
+ force: bool = False,
385
+ on_progress: ProgressCallback | None = None,
386
+ ) -> str:
387
+ return self._install_loader(
388
+ "quilt",
389
+ mc_version,
390
+ loader_version=loader_version,
391
+ force=force,
392
+ on_progress=on_progress,
393
+ )
394
+
395
+ def _get_instance_or_raise(self, name: str) -> Instance:
396
+ instance = self.get_instance(name)
397
+ if instance is None:
398
+ raise InstanceNotFoundError(f"Instance '{name}' not found.")
399
+ return instance
400
+
401
+ def search_mods(
402
+ self,
403
+ query: str,
404
+ mc_version: str | None = None,
405
+ loader: str | None = None,
406
+ limit: int = 10,
407
+ ) -> list[ModSearchResult]:
408
+ return search_modrinth_mods(
409
+ self.session,
410
+ query,
411
+ mc_version=mc_version,
412
+ loader=loader,
413
+ limit=limit,
414
+ )
415
+
416
+ def list_mods(self, instance_name: str) -> list[Path]:
417
+ instance = self._get_instance_or_raise(instance_name)
418
+ return list_instance_mods(self.get_minecraft_path(), instance)
419
+
420
+ def add_mod(self, instance_name: str, jar_path: Path | str) -> Path:
421
+ instance = self._get_instance_or_raise(instance_name)
422
+ return add_instance_mod(self.get_minecraft_path(), instance, jar_path)
423
+
424
+ def remove_mod(self, instance_name: str, filename: str) -> bool:
425
+ instance = self._get_instance_or_raise(instance_name)
426
+ return remove_instance_mod(self.get_minecraft_path(), instance, filename)
427
+
428
+ def toggle_mod(self, instance_name: str, filename: str) -> Path:
429
+ instance = self._get_instance_or_raise(instance_name)
430
+ return toggle_instance_mod(self.get_minecraft_path(), instance, filename)
431
+
432
+ def install_mod(
433
+ self,
434
+ instance_name: str,
435
+ project: str,
436
+ install_dependencies: bool = True,
437
+ ) -> list[Path]:
438
+ instance = self._get_instance_or_raise(instance_name)
439
+ version = self.get_version_or_raise(instance.version_id)
440
+ return install_modrinth_mod(
441
+ self.session,
442
+ self._downloader,
443
+ self.get_minecraft_path(),
444
+ instance,
445
+ version,
446
+ project,
447
+ install_dependencies=install_dependencies,
448
+ )