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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// SPDX-FileCopyrightText: 2024 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later
// Rust deprecating this is stupid, I don't want to use a crate here
#[allow(deprecated)]
use std::env::home_dir;
use std::fs;
use std::fs::read_dir;
use std::path::PathBuf;
use crate::resource::SqPackResource;
/// Where the existing installation came from.
#[derive(Clone, Copy)]
#[repr(C)]
pub enum ExistingInstallType {
/// Installed via the official launcher.
OfficialLauncher,
/// Installed via XIVQuickLauncher.
XIVQuickLauncher,
/// Installed via XIVLauncherCore.
XIVLauncherCore,
/// Installed via XIVOnMac.
XIVOnMac,
/// Installed via Astra.
Astra,
}
/// An existing install location on disk.
pub struct ExistingGameDirectory {
/// The application where this installation was from.
pub install_type: ExistingInstallType,
/// The path to the "main folder" where "game" and "boot" sits.
pub path: String,
/// The latest expansion and game version in this directory.
pub version: String,
}
fn read_version(path: &str) -> String {
let mut path = PathBuf::from(path);
path.push("game");
let path = path.to_str().unwrap().to_string();
let game_data = SqPackResource::from_existing(&path);
if let Some(latest_repository) = game_data.repositories.last() {
return latest_repository.version.clone().unwrap_or_default();
}
String::default()
}
/// Finds existing installations on disk.
///
/// Will only return locations that actually have files in them, and a really basic check to see if the data is valid.
pub fn find_existing_game_dirs() -> Vec<ExistingGameDirectory> {
let mut install_dirs = Vec::new();
match std::env::consts::OS {
"linux" => {
// Official install (Steam)
{
let path = from_home_dir(
".steam/steam/steamapps/common/FINAL FANTASY XIV - A Realm Reborn",
);
install_dirs.push(ExistingGameDirectory {
install_type: ExistingInstallType::OfficialLauncher,
path: path.clone(),
version: read_version(&path),
});
}
// XIVLauncherCore location
{
let path = from_home_dir(".xlcore/ffxiv");
install_dirs.push(ExistingGameDirectory {
install_type: ExistingInstallType::XIVLauncherCore,
path: path.clone(),
version: read_version(&path),
});
}
// Astra location. But we have to iterate through each UUID.
if let Ok(entries) = read_dir(from_home_dir(".local/share/astra/game/")) {
entries
.flatten()
.flat_map(|entry| {
let Ok(meta) = entry.metadata() else {
return vec![];
};
if meta.is_dir() {
return vec![entry.path()];
}
vec![]
})
.for_each(|path| {
let path = path.to_str().unwrap().to_string();
let version = read_version(&path);
install_dirs.push(ExistingGameDirectory {
install_type: ExistingInstallType::Astra,
path,
version,
})
});
}
}
"macos" => {
// Official Launcher (macOS)
{
let path = from_home_dir(
"Library/Application Support/FINAL FANTASY XIV ONLINE/Bottles/published_Final_Fantasy/drive_c/Program Files (x86)/SquareEnix/FINAL FANTASY XIV - A Realm Reborn",
);
install_dirs.push(ExistingGameDirectory {
install_type: ExistingInstallType::OfficialLauncher,
path: path.clone(),
version: read_version(&path),
});
}
// TODO: add XIV on Mac
}
"windows" => {
// Official install (Wine)
{
let path =
"C:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn"
.to_string();
install_dirs.push(ExistingGameDirectory {
install_type: ExistingInstallType::OfficialLauncher,
path: path.clone(),
version: read_version(&path),
});
}
// TODO: Add Astra
}
&_ => {}
}
install_dirs
.into_iter()
.filter(|dir| is_valid_game_dir(&dir.path))
.filter(|dir| !dir.version.is_empty())
.collect()
}
/// An existing user directory.
pub struct ExistingUserDirectory {
/// The application where this directory was from.
pub install_type: ExistingInstallType,
/// The path to the user folder.
pub path: String,
}
/// Finds existing user folders on disk.
///
/// Will only return locations that actually have files in them, and a really basic check to see if the data is valid.
pub fn find_existing_user_dirs() -> Vec<ExistingUserDirectory> {
let mut user_dirs = Vec::new();
#[allow(deprecated)] // We still want std::env::home_dir
let Some(_) = home_dir() else {
return user_dirs;
};
match std::env::consts::OS {
"linux" => {
// Official install (Wine)
user_dirs.push(ExistingUserDirectory {
install_type: ExistingInstallType::OfficialLauncher,
path: from_home_dir("Documents/My Games/FINAL FANTASY XIV - A Realm Reborn"),
});
// XIVLauncherCore location
user_dirs.push(ExistingUserDirectory {
install_type: ExistingInstallType::XIVLauncherCore,
path: from_home_dir(".xlcore/ffxivConfig"),
});
// Astra location. But we have to iterate through each UUID.
if let Ok(entries) = read_dir(from_home_dir(".local/share/astra/user/")) {
entries
.flatten()
.flat_map(|entry| {
let Ok(meta) = entry.metadata() else {
return vec![];
};
if meta.is_dir() {
return vec![entry.path()];
}
vec![]
})
.for_each(|path| {
user_dirs.push(ExistingUserDirectory {
install_type: ExistingInstallType::Astra,
path: path.into_os_string().into_string().unwrap(),
})
});
}
}
"macos" => {
// Official install (Wine)
user_dirs.push(ExistingUserDirectory {
install_type: ExistingInstallType::OfficialLauncher,
path: from_home_dir("Documents/My Games/FINAL FANTASY XIV - A Realm Reborn"),
})
// TODO: Add XIV on Mac?
}
"windows" => {
// Official install
user_dirs.push(ExistingUserDirectory {
install_type: ExistingInstallType::OfficialLauncher,
path: from_home_dir("Documents/My Games/FINAL FANTASY XIV - A Realm Reborn"),
})
// TODO: Add Astra
}
&_ => {}
}
user_dirs
.into_iter()
.filter(|dir| is_valid_user_dir(&dir.path))
.collect()
}
fn from_home_dir(path: &'static str) -> String {
#[allow(deprecated)] // We still want std::env::home_dir
let mut new_path = home_dir().unwrap();
new_path.push(path);
new_path.into_os_string().into_string().unwrap()
}
fn is_valid_game_dir(path: &String) -> bool {
let mut d = PathBuf::from(path);
// Check for the dir itself
if fs::metadata(d.as_path()).is_err() {
return false;
}
// Check for "game"
d.push("game");
if fs::metadata(d.as_path()).is_err() {
return false;
}
// Check for "boot"
d.pop();
d.push("boot");
if fs::metadata(d.as_path()).is_err() {
return false;
}
true
}
fn is_valid_user_dir(path: &String) -> bool {
let mut d = PathBuf::from(path);
// Check for the dir itself
if fs::metadata(d.as_path()).is_err() {
return false;
}
// Check for "FFXIV.cfg"
d.push("FFXIV.cfg");
if fs::metadata(d.as_path()).is_err() {
return false;
}
true
}