forked from libtcod/python-tcod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
115 lines (90 loc) · 3.06 KB
/
loader.py
File metadata and controls
115 lines (90 loc) · 3.06 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
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
"""This module handles loading of the libtcod cffi API.
"""
import sys
import os
import cffi # type: ignore
import platform
from typing import Any # noqa: F401
from tcod import __path__ # type: ignore
__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 = ffi_check.dlopen("SDL2.dll") # Make sure SDL2.dll is here.
version = ffi_check.new("struct SDL_version*")
lib_test.SDL_GetVersion(version) # Need to check this version.
version = version.major, version.minor, version.patch
if version < (2, 0, 5):
raise RuntimeError(
"Tried to load an old version of SDL %r" % (version,)
)
try:
ffi_check.dlopen("vcruntime140.dll") # Make sure VC++ 2015 exists.
except OSError:
print(
"You will need to install 'vc_redist.{arch}.exe'"
" from Microsoft at:\n"
"https://support.microsoft.com/en-us/help/2977003/"
"the-latest-supported-visual-c-downloads\n".format(
arch=get_architecture()
),
file=sys.stderr,
)
raise
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 "%s.%s.%s" % (
sdl_version.major,
sdl_version.minor,
sdl_version.patch,
)
if sys.platform == "win32":
# add Windows dll's to PATH
_bits, _linkage = platform.architecture()
os.environ["PATH"] = "%s;%s" % (
os.path.join(__path__[0], get_architecture()),
os.environ["PATH"],
)
class _Mock(object):
"""Mock object needed for ReadTheDocs."""
CData = () # This gets passed to an isinstance call.
@staticmethod
def def_extern() -> Any:
"""Pass def_extern call silently."""
return lambda func: func
def __getattr__(self, attr: Any) -> Any:
"""This object pretends to have everything."""
return self
def __call__(self, *args: Any, **kargs: Any) -> Any:
"""Suppress any other calls"""
return self
def __str__(self) -> Any:
"""Just have ? in case anything leaks as a parameter default."""
return "?"
lib = None # type: Any
ffi = None # type: Any
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 lib, ffi # type: ignore # noqa: F401
__sdl_version__ = get_sdl_version()
__all__ = ["ffi", "lib"]