|
| 1 | +#@PydevCodeAnalysisIgnore |
| 2 | + |
| 3 | +# This file is part of Ren'Py. The license below applies to Ren'Py only. |
| 4 | +# Games and other projects that use Ren'Py may use a different license. |
| 5 | + |
| 6 | +# Copyright 2004-2017 Tom Rothamel <pytom@bishoujo.us> |
| 7 | +# |
| 8 | +# Permission is hereby granted, free of charge, to any person |
| 9 | +# obtaining a copy of this software and associated documentation files |
| 10 | +# (the "Software"), to deal in the Software without restriction, |
| 11 | +# including without limitation the rights to use, copy, modify, merge, |
| 12 | +# publish, distribute, sublicense, and/or sell copies of the Software, |
| 13 | +# and to permit persons to whom the Software is furnished to do so, |
| 14 | +# subject to the following conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be |
| 17 | +# included in all copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 23 | +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 24 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 25 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +from __future__ import print_function |
| 28 | + |
| 29 | +import os |
| 30 | +import sys |
| 31 | +import warnings |
| 32 | + |
| 33 | +# Functions to be customized by distributors. ################################ |
| 34 | + |
| 35 | +# Given the Ren'Py base directory (usually the directory containing |
| 36 | +# this file), this is expected to return the path to the common directory. |
| 37 | + |
| 38 | + |
| 39 | +def path_to_common(renpy_base): |
| 40 | + return renpy_base + "/renpy/common" |
| 41 | + |
| 42 | +# Given a directory holding a Ren'Py game, this is expected to return |
| 43 | +# the path to a directory that will hold save files. |
| 44 | + |
| 45 | + |
| 46 | +def path_to_saves(gamedir, save_directory=None): |
| 47 | + import renpy # @UnresolvedImport |
| 48 | + |
| 49 | + if save_directory is None: |
| 50 | + save_directory = renpy.config.save_directory |
| 51 | + save_directory = renpy.exports.fsencode(save_directory) |
| 52 | + |
| 53 | + # Makes sure the permissions are right on the save directory. |
| 54 | + def test_writable(d): |
| 55 | + try: |
| 56 | + fn = os.path.join(d, "test.txt") |
| 57 | + open(fn, "w").close() |
| 58 | + open(fn, "r").close() |
| 59 | + os.unlink(fn) |
| 60 | + return True |
| 61 | + except: |
| 62 | + return False |
| 63 | + |
| 64 | + # Android. |
| 65 | + if renpy.android: |
| 66 | + paths = [ |
| 67 | + os.path.join(os.environ["ANDROID_OLD_PUBLIC"], "game/saves"), |
| 68 | + os.path.join(os.environ["ANDROID_PRIVATE"], "saves"), |
| 69 | + os.path.join(os.environ["ANDROID_PUBLIC"], "saves"), |
| 70 | + ] |
| 71 | + |
| 72 | + for rv in paths: |
| 73 | + if os.path.isdir(rv) and test_writable(rv): |
| 74 | + break |
| 75 | + |
| 76 | + print("Saving to", rv) |
| 77 | + |
| 78 | + # We return the last path as the default. |
| 79 | + |
| 80 | + return rv |
| 81 | + |
| 82 | + if renpy.ios: |
| 83 | + from pyobjus import autoclass |
| 84 | + from pyobjus.objc_py_types import enum |
| 85 | + |
| 86 | + NSSearchPathDirectory = enum("NSSearchPathDirectory", NSDocumentDirectory=9) |
| 87 | + NSSearchPathDomainMask = enum("NSSearchPathDomainMask", NSUserDomainMask=1) |
| 88 | + |
| 89 | + NSFileManager = autoclass('NSFileManager') |
| 90 | + manager = NSFileManager.defaultManager() |
| 91 | + url = manager.URLsForDirectory_inDomains_( |
| 92 | + NSSearchPathDirectory.NSDocumentDirectory, |
| 93 | + NSSearchPathDomainMask.NSUserDomainMask, |
| 94 | + ).lastObject() |
| 95 | + |
| 96 | + # url.path seems to change type based on iOS version, for some reason. |
| 97 | + try: |
| 98 | + rv = url.path().UTF8String().decode("utf-8") |
| 99 | + except: |
| 100 | + rv = url.path.UTF8String().decode("utf-8") |
| 101 | + |
| 102 | + print("Saving to", rv) |
| 103 | + return rv |
| 104 | + |
| 105 | + # No save directory given. |
| 106 | + if not save_directory: |
| 107 | + return gamedir + "/saves" |
| 108 | + |
| 109 | + # Search the path above Ren'Py for a directory named "Ren'Py Data". |
| 110 | + # If it exists, then use that for our save directory. |
| 111 | + path = renpy.config.renpy_base |
| 112 | + |
| 113 | + while True: |
| 114 | + if os.path.isdir(path + "/Ren'Py Data"): |
| 115 | + return path + "/Ren'Py Data/" + save_directory |
| 116 | + |
| 117 | + newpath = os.path.dirname(path) |
| 118 | + if path == newpath: |
| 119 | + break |
| 120 | + path = newpath |
| 121 | + |
| 122 | + # Otherwise, put the saves in a platform-specific location. |
| 123 | + if renpy.macintosh: |
| 124 | + rv = "~/Library/RenPy/" + save_directory |
| 125 | + return os.path.expanduser(rv) |
| 126 | + |
| 127 | + elif renpy.windows: |
| 128 | + if 'APPDATA' in os.environ: |
| 129 | + return os.environ['APPDATA'] + "/RenPy/" + save_directory |
| 130 | + else: |
| 131 | + rv = "~/RenPy/" + renpy.config.save_directory |
| 132 | + return os.path.expanduser(rv) |
| 133 | + |
| 134 | + else: |
| 135 | + rv = "~/.renpy/" + save_directory |
| 136 | + return os.path.expanduser(rv) |
| 137 | + |
| 138 | + |
| 139 | +# Returns the path to the Ren'Py base directory (containing common and |
| 140 | +# the launcher, usually.) |
| 141 | +def path_to_renpy_base(): |
| 142 | + renpy_base = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 143 | + renpy_base = os.path.abspath(renpy_base) |
| 144 | + |
| 145 | + return renpy_base |
| 146 | + |
| 147 | +############################################################################## |
| 148 | + |
| 149 | +# Doing the version check this way also doubles as an import of ast, |
| 150 | +# which helps py2exe et al. |
| 151 | +try: |
| 152 | + import ast; ast |
| 153 | +except: |
| 154 | + raise |
| 155 | + print("Ren'Py requires at least python 2.6.") |
| 156 | + sys.exit(0) |
| 157 | + |
| 158 | +android = ("ANDROID_PRIVATE" in os.environ) |
| 159 | + |
| 160 | +# Android requires us to add code to the main module, and to command some |
| 161 | +# renderers. |
| 162 | +if android: |
| 163 | + __main__ = sys.modules["__main__"] |
| 164 | + __main__.path_to_renpy_base = path_to_renpy_base |
| 165 | + __main__.path_to_common = path_to_common |
| 166 | + __main__.path_to_saves = path_to_saves |
| 167 | + os.environ["RENPY_RENDERER"] = "gl" |
| 168 | + |
| 169 | + |
| 170 | +def main(): |
| 171 | + |
| 172 | + renpy_base = path_to_renpy_base() |
| 173 | + |
| 174 | + # Add paths. |
| 175 | + if os.path.exists(renpy_base + "/module"): |
| 176 | + sys.path.append(renpy_base + "/module") |
| 177 | + |
| 178 | + sys.path.append(renpy_base) |
| 179 | + |
| 180 | + # This is looked for by the mac launcher. |
| 181 | + if os.path.exists(renpy_base + "/renpy.zip"): |
| 182 | + sys.path.append(renpy_base + "/renpy.zip") |
| 183 | + |
| 184 | + # Ignore warnings that happen. |
| 185 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 186 | + |
| 187 | + # Start Ren'Py proper. |
| 188 | + try: |
| 189 | + import renpy.bootstrap |
| 190 | + except ImportError: |
| 191 | + print("Could not import renpy.bootstrap. Please ensure you decompressed Ren'Py", file=sys.stderr) |
| 192 | + print("correctly, preserving the directory structure.", file=sys.stderr) |
| 193 | + raise |
| 194 | + |
| 195 | + renpy.bootstrap.bootstrap(renpy_base) |
| 196 | + |
| 197 | +if __name__ == "__main__": |
| 198 | + main() |
0 commit comments