-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrootbuilder_linker.py
More file actions
84 lines (78 loc) · 3.78 KB
/
rootbuilder_linker.py
File metadata and controls
84 lines (78 loc) · 3.78 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
from pathlib import Path
from .rootbuilder_paths import RootBuilderPaths
from .rootbuilder_files import RootBuilderFiles
from .rootbuilder_settings import RootBuilderSettings
from ...shared.shared_utilities import SharedUtilities
import mobase, os, json
try:
from PyQt5.QtCore import QCoreApplication, qInfo
except:
from PyQt6.QtCore import QCoreApplication, qInfo
class RootBuilderLinker():
""" Root Builder link module. Used to create links for specific file types. """
def __init__(self, organiser=mobase.IOrganizer, paths=RootBuilderPaths, files=RootBuilderFiles, settings=RootBuilderSettings):
self.organiser = organiser
self.paths = paths
self.files = files
self.settings = settings
self.utilities = SharedUtilities()
super().__init__()
def build(self):
""" Generates links for all linkable mod files and saves records of each of them """
# Get all linkable mod files.
linkFileData = []
if self.settings.linkonlymode():
linkFileData = self.files.getRootModFiles()
else:
linkFileData = self.files.getLinkableModFiles()
if self.paths.rootLinkDataFilePath().exists():
linkOutputData = json.load(open(self.paths.rootLinkDataFilePath(),"r", encoding="utf-8"))
else:
linkOutputData = {}
for file in linkFileData:
relativePath = self.paths.rootRelativePath(file)
gamePath = self.paths.gamePath() / relativePath
file_path = Path(file)
createLink = True
if gamePath.exists():
if str(gamePath) not in linkOutputData:
# The linkable file is already in the game folder, rename it.
self.utilities.moveTo(gamePath, Path(str(gamePath) + ".rbackup"))
elif gamePath.samefile(file_path):
# This is already linked.
createLink = False
else:
# Remove old link to update with new one
gamePath.unlink()
if createLink:
# Create the dirs if they don't exist.
if not gamePath.parent.exists():
os.makedirs(gamePath.parent)
# Try and create a link. This will fail if a link is already there.
file_path.link_to(gamePath)
linkOutputData[str(gamePath)] = str(file)
# Save our link data.
if not self.paths.rootLinkDataFilePath().exists():
self.paths.rootLinkDataFilePath().touch()
with open(self.paths.rootLinkDataFilePath(), "w", encoding="utf-8") as jsonFile:
json.dump(linkOutputData, jsonFile)
def clear(self):
""" Clears any created links from mod files """
# Check if we have any link data and load it if we do.
if self.paths.rootLinkDataFilePath().exists():
linkFileData = json.load(open(self.paths.rootLinkDataFilePath(),"r", encoding="utf-8"))
# Loop through our link data and unlink individual files.
for dest in linkFileData:
destPath = Path(dest)
srcPath = Path(linkFileData[dest])
if destPath.exists():
if os.stat(destPath).st_nlink <= 1:
self.utilities.moveTo(destPath, srcPath)
else:
#qInfo("Removing link for " + str(gamePath))
destPath.unlink(True)
if Path(str(destPath) + ".rbackup").exists():
#qInfo("Renaming from link " + str(gamePath))
self.utilities.moveTo(Path(str(destPath) + ".rbackup"), destPath)
# Remove our link data file.
self.utilities.deletePath(self.paths.rootLinkDataFilePath())