Line 0
Link Here
|
|
|
1 |
# This file is part of Ren'Py. The license below applies to Ren'Py only. |
2 |
# Games and other projects that use Ren'Py may use a different license. |
3 |
|
4 |
# Copyright 2004-2015 Tom Rothamel <pytom@bishoujo.us> |
5 |
# |
6 |
# Permission is hereby granted, free of charge, to any person |
7 |
# obtaining a copy of this software and associated documentation files |
8 |
# (the "Software"), to deal in the Software without restriction, |
9 |
# including without limitation the rights to use, copy, modify, merge, |
10 |
# publish, distribute, sublicense, and/or sell copies of the Software, |
11 |
# and to permit persons to whom the Software is furnished to do so, |
12 |
# subject to the following conditions: |
13 |
# |
14 |
# The above copyright notice and this permission notice shall be |
15 |
# included in all copies or substantial portions of the Software. |
16 |
# |
17 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19 |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
20 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
21 |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
22 |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
23 |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24 |
|
25 |
import os |
26 |
import sys |
27 |
import warnings |
28 |
from distutils.sysconfig import get_python_lib |
29 |
|
30 |
# Given the Ren'Py base directory (usually the directory containing |
31 |
# this file), this is expected to return the path to the common directory. |
32 |
def path_to_common(renpy_base): |
33 |
return renpy_base + "/renpy/common" |
34 |
|
35 |
# Given a directory holding a Ren'Py game, this is expected to return |
36 |
# the path to a directory that will hold save files. |
37 |
def path_to_saves(gamedir, save_directory=None): |
38 |
import renpy #@UnresolvedImport |
39 |
|
40 |
if save_directory is None: |
41 |
save_directory = renpy.config.save_directory |
42 |
|
43 |
# Makes sure the permissions are right on the save directory. |
44 |
def test_writable(d): |
45 |
try: |
46 |
fn = os.path.join(d, "test.txt") |
47 |
open(fn, "w").close() |
48 |
open(fn, "r").close() |
49 |
os.unlink(fn) |
50 |
return True |
51 |
except: |
52 |
return False |
53 |
|
54 |
|
55 |
# Android. |
56 |
if renpy.android: |
57 |
paths = [ |
58 |
os.path.join(os.environ["ANDROID_OLD_PUBLIC"], "game/saves"), |
59 |
os.path.join(os.environ["ANDROID_PRIVATE"], "saves"), |
60 |
os.path.join(os.environ["ANDROID_PUBLIC"], "saves"), |
61 |
] |
62 |
|
63 |
for rv in paths: |
64 |
if os.path.isdir(rv) and test_writable(rv): |
65 |
break |
66 |
|
67 |
print "Saving to", rv |
68 |
|
69 |
# We return the last path as the default. |
70 |
|
71 |
return rv |
72 |
|
73 |
if renpy.ios: |
74 |
from pyobjus import autoclass |
75 |
from pyobjus.objc_py_types import enum |
76 |
|
77 |
NSSearchPathDirectory = enum("NSSearchPathDirectory", NSDocumentDirectory=9) |
78 |
NSSearchPathDomainMask = enum("NSSearchPathDomainMask", NSUserDomainMask=1) |
79 |
|
80 |
NSFileManager = autoclass('NSFileManager') |
81 |
manager = NSFileManager.defaultManager() |
82 |
url = manager.URLsForDirectory_inDomains_( |
83 |
NSSearchPathDirectory.NSDocumentDirectory, |
84 |
NSSearchPathDomainMask.NSUserDomainMask, |
85 |
).lastObject() |
86 |
|
87 |
# url.path seems to change type based on iOS version, for some reason. |
88 |
try: |
89 |
rv = url.path().UTF8String().decode("utf-8") |
90 |
except: |
91 |
rv = url.path.UTF8String().decode("utf-8") |
92 |
|
93 |
print "Saving to", rv |
94 |
return rv |
95 |
|
96 |
# No save directory given. |
97 |
if not save_directory: |
98 |
return gamedir + "/saves" |
99 |
|
100 |
# Search the path above Ren'Py for a directory named "Ren'Py Data". |
101 |
# If it exists, then use that for our save directory. |
102 |
path = renpy.config.renpy_base |
103 |
|
104 |
while True: |
105 |
if os.path.isdir(path + "/Ren'Py Data"): |
106 |
return path + "/Ren'Py Data/" + save_directory |
107 |
|
108 |
newpath = os.path.dirname(path) |
109 |
if path == newpath: |
110 |
break |
111 |
path = newpath |
112 |
|
113 |
# Otherwise, put the saves in a platform-specific location. |
114 |
if renpy.macintosh: |
115 |
rv = "~/Library/RenPy/" + save_directory |
116 |
return os.path.expanduser(rv) |
117 |
|
118 |
elif renpy.windows: |
119 |
if 'APPDATA' in os.environ: |
120 |
return os.environ['APPDATA'] + "/RenPy/" + save_directory |
121 |
else: |
122 |
rv = "~/RenPy/" + renpy.config.save_directory |
123 |
return os.path.expanduser(rv) |
124 |
|
125 |
else: |
126 |
rv = "~/.renpy/" + save_directory |
127 |
return os.path.expanduser(rv) |
128 |
|
129 |
|
130 |
# Returns the path to the Ren'Py base directory (containing common and |
131 |
# the launcher, usually.) |
132 |
def path_to_renpy_base(): |
133 |
renpy_base = os.path.dirname(os.path.realpath(sys.argv[0])) |
134 |
renpy_base = get_python_lib() + "/renpy@SLOT@" |
135 |
renpy_base = os.path.abspath(renpy_base) |
136 |
|
137 |
return renpy_base |