-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathloader.py
More file actions
86 lines (63 loc) · 2.23 KB
/
loader.py
File metadata and controls
86 lines (63 loc) · 2.23 KB
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
"""This module handles loading of the libtcod cffi API."""
from __future__ import annotations
import os
import platform
import sys
from pathlib import Path
from typing import Any
import cffi
__sdl_version__ = ""
ffi_check = cffi.FFI()
ffi_check.cdef(
"""
typedef struct SDL_version
{
uint8_t major;
uint8_t minor;
uint8_t patch;
} SDL_version;
void SDL_GetVersion(SDL_version * ver);
"""
)
def verify_dependencies() -> None:
"""Try to make sure dependencies exist on this system."""
if sys.platform == "win32":
lib_test: Any = ffi_check.dlopen("SDL2.dll") # Make sure SDL2.dll is here.
version: Any = ffi_check.new("struct SDL_version*")
lib_test.SDL_GetVersion(version) # Need to check this version.
version_tuple = version.major, version.minor, version.patch
if version_tuple < (2, 0, 5):
msg = f"Tried to load an old version of SDL {version_tuple!r}"
raise RuntimeError(msg)
def get_architecture() -> str:
"""Return the Windows architecture, one of "x86" or "x64"."""
return "x86" if platform.architecture()[0] == "32bit" else "x64"
def get_sdl_version() -> str:
sdl_version = ffi.new("SDL_version*")
lib.SDL_GetVersion(sdl_version)
return f"{sdl_version.major}.{sdl_version.minor}.{sdl_version.patch}"
if sys.platform == "win32":
# add Windows dll's to PATH
os.environ["PATH"] = f"""{Path(__file__).parent / get_architecture()}{os.pathsep}{os.environ["PATH"]}"""
class _Mock:
"""Mock object needed for ReadTheDocs."""
@staticmethod
def def_extern() -> Any:
"""Pass def_extern call silently."""
return lambda func: func
def __getattr__(self, attr: str) -> None:
"""Return None on any attribute."""
def __bool__(self) -> bool:
"""Allow checking for this mock object at import time."""
return False
lib: Any = None
ffi: Any = None
if os.environ.get("READTHEDOCS"):
# Mock the lib and ffi objects needed to compile docs for readthedocs.io
# Allows an import without building the cffi module first.
lib = ffi = _Mock()
else:
verify_dependencies()
from tcod._libtcod import ffi, lib # type: ignore
__sdl_version__ = get_sdl_version()
__all__ = ["ffi", "lib"]