-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
184 lines (161 loc) · 5.24 KB
/
index.js
File metadata and controls
184 lines (161 loc) · 5.24 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
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
const Promise = require('bluebird');
const path = require('path');
const { parseStringPromise } = require('xml2js');
const { fs, util } = require('vortex-api');
// Platform specific IDs used by Hacknet.
const NEXUS_ID = 'hacknet';
const STEAM_ID = '365450';
const GOG_ID = '1439474400';
// Main executable for Hacknet Pathfinder. Required to use Pathfinder mods.
const PF_EXE = 'HacknetPathfinder.exe';
let PF_EXE_LOCATION = String.raw`C:\Program Files (x86)\Steam\steamapps\common\Hacknet\HacknetPathfinder.exe`;
// Manifest file required by Hacknet Extensions.
const EXT_FILE = 'extensioninfo.xml';
// File extension used to identify Pathfinder Mods.
const PF_FILE = '.dll';
// Include common tools used in Hacknet modding for convenience. Pathfinder should be run by default if present.
const tools = [
{
id: 'PF',
name: 'Hacknet Pathfinder',
shortName: 'Pathfinder',
logo: 'pf.png',
executable: () => 'HacknetPathfinder.exe',
requiredFiles: [
'HacknetPathfinder.exe',
],
relative: true,
defaultPrimary: true
},
{
id: 'HTE',
name: 'Hacknet Themes Editor',
logo: 'hte.png',
executable: () => 'Hacknet Themes Editor.exe',
requiredFiles: [
'Hacknet Themes Editor.exe',
],
}
];
function findGame() {
return util.GameStoreHelper.findByAppId([STEAM_ID, GOG_ID])
.then(game => game.gamePath);
}
function prepareForModding(discovery) {
PF_EXE_LOCATION = path.join(discovery.path, PF_EXE);
}
function testHacknetMod(files, gameId) {
if (gameId === NEXUS_ID) {
const validExtension = !!files.find(file => path.basename(file).toLowerCase() === EXT_FILE);
const validPathfinderMod = !!files.find(file => path.extname(file).toLowerCase() === PF_FILE);
return Promise.resolve({ supported: (validExtension || validPathfinderMod), requiredFiles: [] });
} else return Promise.resolve({ supported: false, requiredFiles: [] });
}
function installHacknetMod(files, destinationPath, api) {
return installExtension(files, destinationPath)
.catch(() => installPathfinderMod(files, api)
.catch(() => Promise.reject(new util.DataInvalid('Unrecognised or invalid Hacknet mod')))
);
}
// Extensions need to be in seperate folders, so the name specified in ExtensionInfo.xml is used.
function getExtensionName(destination, modFile) {
return fs.readFileAsync(path.join(destination, modFile))
.then(async xmlData => {
let extName;
try {
extName = (await parseStringPromise(xmlData))?.HacknetExtension?.Name?.[0];
extName = extName.replace(/[\/:*?"<>|]/g, '');
if (extName === '') {
return Promise.reject(new util.DataInvalid('Name missing in ExtensionInfo.xml'));
} else return Promise.resolve(extName);
} catch {
return Promise.reject(new util.DataInvalid('Failed to parse ExtensionInfo.xml'));
}
});
}
function installExtension(files, destinationPath) {
const modFile = files.find(file => path.basename(file).toLowerCase() === EXT_FILE);
if (!modFile) return Promise.reject('Not a valid Hacknet extension');
const idx = modFile.indexOf(path.basename(modFile));
const rootPath = path.dirname(modFile);
return getExtensionName(destinationPath, modFile)
.then(extName => {
const filtered = files.filter(file =>
((file.indexOf(rootPath) !== -1)
&& (!file.endsWith(path.sep))));
const instructions = filtered.map(file => {
return {
type: 'copy',
source: file,
destination: path.join('Extensions', extName, file.substr(idx)),
};
});
return Promise.resolve({ instructions });
});
}
// Note: Will install any archive containing a DLL as a Pathfinder mod. Hopefully won't cause issues with additional DLLs.
function installPathfinderMod(files, api) {
fs.statAsync(PF_EXE_LOCATION)
.catch(() => {
warnPathfinder(api);
}
);
const modFile = files.find(file => path.extname(file).toLowerCase() === PF_FILE);
if (!modFile) return Promise.reject('Not a valid Pathfinder mod');
const idx = modFile.indexOf(path.basename(modFile));
const rootPath = path.dirname(modFile);
const filtered = files.filter(file =>
((file.indexOf(rootPath) !== -1)
&& (!file.endsWith(path.sep))));
const instructions = filtered.map(file => {
return {
type: 'copy',
source: file,
destination: path.join('Mods', file.substr(idx)),
};
});
return Promise.resolve({ instructions });
}
function warnPathfinder(api) {
api.sendNotification({
id: 'pathfinder-missing',
type: 'warning',
title: 'Pathfinder not installed',
message: 'Hacknet Pathfinder is required to use Pathfinder mods',
actions: [
{
title: 'Get Pathfinder',
action: () => util.opn('https://www.nexusmods.com/hacknet/mods/1').catch(() => undefined)
}
]
});
}
function main(context) {
context.registerGame({
id: NEXUS_ID,
name: 'Hacknet',
mergeMods: true,
requiresCleanup: true,
queryPath: findGame,
supportedTools: tools,
queryModPath: () => '',
logo: 'gameart.jpg',
executable: () => 'Hacknet.exe',
requiredFiles: [
'Hacknet.bmp'
],
setup: prepareForModding,
environment: {
SteamAPPId: STEAM_ID,
},
details: {
steamAppId: parseInt(STEAM_ID),
gogAppId: GOG_ID,
},
});
context.registerInstaller('hacknet-mod-installer', 25, testHacknetMod, (files, destinationPath) => installHacknetMod(files, destinationPath, context.api));
return true;
}
module.exports = {
default: main,
};