forked from mrwonko/Blender-Jedi-Academy-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJAFilesystem.py
96 lines (71 loc) · 3.35 KB
/
JAFilesystem.py
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import os
# name of the directory containing base / the mod folders, e.g. gamedata for Jedi Academy (in lowercase)
# TODO #9 / #33: SoF2 does not have a gamedata, make guessing more lenient
DIRNAME = "gamedata"
# returns the prefix and the rest, e.g. ("/foo/bar/JKA/GameData/Base", "textures/img.jpg") (where GameData is DIRNAME)
def SplitPrefix(fullPath):
normFullPath = os.path.normpath(fullPath)
searchme = os.path.normcase(normFullPath)
pos = searchme.find(os.path.sep + DIRNAME + os.path.sep)
# find /DIRNAME/
if pos == -1:
return "", normFullPath
pos = pos+len(DIRNAME)+2*len(os.path.sep)
# find first / after that
pos = searchme.find(os.path.sep, pos)
if pos == -1:
return "", normFullPath
return [normFullPath[:pos+len(os.path.sep)], normFullPath[pos+len(os.path.sep):]]
# removes a file extension, i.e. /foo/bar.baz -> /foo/bar
def RemoveExtension(path):
return os.path.splitext(path)[0]
# removes leading path, i.e. /foo/bar.baz -> bar.baz
def GetFilename(path):
return os.path.split(path)[1]
# returns the relative path as used in the game, given the full path and the prefix (as returned by GetPrefix())
def RelPath(fullpath, prefix):
normPrefix = os.path.normcase(os.path.normcase(prefix))
normFullpath = os.path.normcase(os.path.normcase(fullpath))
if normPrefix != os.path.commonprefix([normPrefix, normFullpath]):
print("Warning: \"", fullpath,
"\" does not start with \"", prefix, "\"!", sep="")
return fullpath
return os.path.relpath(fullpath, prefix).replace(os.path.sep, "/")
# returns the relative path as used in the game without the extension, given the full path and the prefix (as returned by GetPrefix())
def RelPathNoExt(fullpath, prefix):
return RemoveExtension(RelPath(fullpath, prefix))
# returns the absolute path of a game path, given its prefix
def AbsPath(relpath, prefix):
if prefix == "":
return relpath # would be prefixed "/" otherwise
return os.path.normpath(os.path.normpath(prefix) + os.path.sep + relpath)
# finds a file given its game name and the possible extensions (usually image extensions).
# Returns (success, filename)
def FindFile(relpath, prefix, extensions):
absPath = AbsPath(relpath, prefix)
if os.path.isfile(absPath):
return True, absPath
absPath = os.path.splitext(absPath)[0]
for extension in extensions:
if os.path.isfile(absPath + "." + extension):
return True, absPath + "." + extension
return False, ""
def FileExists(path: str) -> bool:
return os.path.isfile(path)