|
1 | 1 | import sys
|
2 | 2 | import os
|
3 | 3 | import colorsys
|
| 4 | +from pathlib import Path |
4 | 5 | from math import modf
|
5 | 6 | from enum import IntEnum, IntFlag
|
6 | 7 | from typing import Tuple, List, Union, Sequence, AnyStr, Optional, Iterator, Type
|
|
42 | 43 | if "ENABLE_V2_0_0_FEATURE_CLIPRECT" in os.environ:
|
43 | 44 | ENABLE_V2_0_0_FEATURE_CLIPRECT = True
|
44 | 45 |
|
45 |
| -RAYLIB_BIN_PATH = None |
46 |
| -if "RAYLIB_BIN_PATH" in os.environ: |
47 |
| - env_path = os.environ['RAYLIB_BIN_PATH'] |
48 |
| - if env_path == '__main__': |
49 |
| - RAYLIB_BIN_PATH = os.path.dirname(sys.modules['__main__'].__file__) |
50 |
| - elif env_path == '__file__': |
51 |
| - RAYLIB_BIN_PATH = os.path.abspath(os.path.dirname(__file__)) |
52 |
| - elif os.path.exists(env_path) and os.path.isdir(env_path): |
53 |
| - RAYLIB_BIN_PATH = env_path |
54 |
| -else: |
55 |
| - first_path = os.path.abspath(os.path.dirname(__file__)) |
56 |
| - second_path = os.path.dirname(sys.modules['__main__'].__file__) |
57 |
| - if os.path.exists(os.path.join(first_path, _lib_filename[_platform])): |
58 |
| - RAYLIB_BIN_PATH = first_path |
59 |
| - elif os.path.exists(os.path.join(second_path, _lib_filename[_platform])): |
60 |
| - RAYLIB_BIN_PATH = second_path |
61 |
| - else: |
62 |
| - s = ("'{}' is expected to be located\n" |
63 |
| - "in the directory specified by the environment variable\n" |
64 |
| - "RAYLIB_BIN_PATH, in the program's entry point (__main__)\n" |
65 |
| - "directory, or in the raylibpy package __init__ directory.\n" |
66 |
| - "The library file is not in any of these locations.") |
67 |
| - print(s.format(_lib_filename[_platform])) |
68 |
| - raise RuntimeError("Unable to find raylib library ('{}').".format(_lib_filename[_platform])) |
| 46 | +lib_name = _lib_filename[_platform] |
| 47 | +main_mod = sys.modules['__main__'] |
| 48 | +running_from_repl = '__file__' not in dir(main_mod) |
| 49 | + |
| 50 | +env_path = Path(os.environ['RAYLIB_BIN_PATH']) if 'RAYLIB_BIN_PATH' in os.environ else None |
| 51 | +file_path = Path(__file__).parent |
| 52 | +main_path = Path(main_mod.__file__).parent if not running_from_repl else Path('REPL') |
69 | 53 |
|
70 |
| -if RAYLIB_BIN_PATH: |
71 |
| - _rl = CDLL(os.path.join(RAYLIB_BIN_PATH, _lib_filename[_platform])) |
| 54 | +if env_path and env_path.exists(): |
| 55 | + RAYLIB_BIN_PATH = env_path |
72 | 56 |
|
| 57 | +elif (file_path / lib_name).exists(): |
| 58 | + RAYLIB_BIN_PATH = file_path |
| 59 | + |
| 60 | +elif (main_path / lib_name).exists(): |
| 61 | + RAYLIB_BIN_PATH = main_path |
| 62 | +else: |
| 63 | + raise Exception( |
| 64 | + f'Cannot find "{lib_name}" in these search paths:\n' |
| 65 | + f'__file__ folder: "{str(file_path)}"\n' |
| 66 | + f'__main__ folder: "{str(main_path)}"\n' |
| 67 | + f'os.environ["RAYLIB_BIN_PATH"] -> "{str(env_path) if env_path else "NOT SET"}"' |
| 68 | + ) |
| 69 | + |
| 70 | +print(f'INFO: Found "{lib_name}" in "{str(RAYLIB_BIN_PATH)}"') |
| 71 | +_rl = CDLL(str(RAYLIB_BIN_PATH / lib_name)) |
73 | 72 |
|
74 | 73 | __all__ = [
|
75 | 74 | # CONSTANTS
|
|
0 commit comments