-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.ts
117 lines (110 loc) · 3.99 KB
/
handler.ts
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
import { ILoadOrderGameInfo, IValidationResult, LoadOrder } from "vortex-api/lib/extensions/file_based_loadorder/types/types";
import { IExtensionApi, IMod } from "vortex-api/lib/types/api";
import {deserialize, serialize, validate} from './serialization';
import { LoadOrderFilter } from "./types";
import { toLOPrefix } from "./util";
/**
* Helper class to simplify setting up UE4 games with Vortex's FBLO API.
*
* @remarks The FBLO API is only available in Vortex 1.4+
* @remarks This helper is in preview state and may not be 100% stable.
*/
export class LoadOrderHelper {
private _api: IExtensionApi;
private _gameId: string;
private _filter: LoadOrderFilter;
private _filters: LoadOrderFilter[] = [];
/**
* Creates a new instance of the helper for the specified game.
*
* @example `var lo = new LoadOrderHelper(context.api, GAME_ID);`
*
* @param api The extension API.
* @param gameId The game to manage load order for.
*/
constructor(api: IExtensionApi, gameId: string) {
this._api = api
this._gameId = gameId;
this._filter = (val, mod) => this._filters.every(f => f(val, mod));
}
/**
* Adds a simple predicate to help control load order deserialization.
*
* @param filter Filter predicate.
* @returns The helper itself (for method chaining).
*/
withFilter = (filter: LoadOrderFilter): LoadOrderHelper => {
this._filters.push(filter);
return this;
}
/**
* The serialize function used when serializing the load order to disk.
*
* @example How to register the serialize function
* ```typescript
*context.registerLoadOrder({
* ...
* serializeLoadOrder: lo.serialize,
* ...
* });
*```
* @returns The serialization function.
*/
serialize: (loadOrder: LoadOrder) => Promise<void> = async (lo) => serialize(this._api, this._gameId, lo);
/**
* The deserialize function used when loading the load order from disk into app state.
*
* * @example Registering the deserialize function
* ```typescript
*context.registerLoadOrder({
* ...
* deserializeLoadOrder: lo.deserialize,
* ...
* });
*```
* @returns The deserialization function.
*/
deserialize: () => Promise<LoadOrder> = () => deserialize(this._api, this._gameId, undefined, {filterFn: this._filter, tryRepair: true});
/**
* The validation function used when validating the "correctness" of the load order before/after serialization.
*
* @returns The validation function
*/
validate: (prev: LoadOrder, curr: LoadOrder) => Promise<IValidationResult> = (prev, curr) => {
return validate(prev, curr);
}
/**
* A simple function that will return a prefix path for merging that enforces the current load order.
*
* @example Registering the function
* ```typescript
* mergeMods: lo.createPrefix,
* ```
* @param mod The mod to prefix.
* @returns The string prefix for merging.
*/
createPrefix: (mod: IMod) => string = (mod) => {
return toLOPrefix(this._api, mod)
}
}
/**
* A simple wrapper function that can create a complete `ILoadOrderGameInfo` object ready for registration with `IExtensionContext.registerLoadOrder`
*
* @experimental
* @remarks The returned helper will use all the default behaviours where relevant.
* @param api The extension API.
* @param gameId The game to manage the order for.
* @param instructions Optional instructions for the user.
* @returns A complete `ILoadOrderGameInfo` object.
*/
export function getLoadOrderInfo(api: IExtensionApi, gameId: string, instructions?: string): ILoadOrderGameInfo {
var helper = new LoadOrderHelper(api, gameId);
return {
toggleableEntries: false,
gameId,
serializeLoadOrder: helper.serialize,
deserializeLoadOrder: helper.deserialize,
validate: helper.validate,
usageInstructions: instructions
};
}