Skip to content

Commit

Permalink
Made the library lazy loaded, should not be loaded anymore when not r…
Browse files Browse the repository at this point in the history
…equired

Added the tools section explanation
  • Loading branch information
Aragas committed Jun 1, 2024
1 parent 48cb413 commit 45edcef
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 134 deletions.
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---------------------------------------------------------------------------------------------------
Version: 1.0.11
* Made the library lazy loaded, should not be loaded anymore when not required
---------------------------------------------------------------------------------------------------
Version: 1.0.10
* Fixed interference with other games
* Fixed CLI parameters passing
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "game-mount-and-blade-ii-bannerlord-butr",
"version": "1.0.10",
"version": "1.0.11",
"description": "A Vortex extension for Mount and Blade II: Bannerlord mod management.",
"author": "BUTR Team & Nexus Mods",
"license": "GPL-3.0+",
Expand Down
27 changes: 15 additions & 12 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import Bluebird, { Promise, method as toBluebird } from 'bluebird';
import { types } from 'vortex-api';
import { EPICAPP_ID, GAME_ID, GOG_IDS, MODULES, STEAMAPP_ID, XBOX_ID } from './common';
import { VortexLauncherManager, findGame, getBannerlordMainExe, requiresLauncher, setup } from './utils';
import { findGame, getBannerlordMainExe, requiresLauncher, setup } from './utils';
import { GetLauncherManager } from './types';

export class BannerlordGame implements types.IGame {
private _api: types.IExtensionApi;
private _launcherManager: VortexLauncherManager;
private _getLauncherManager: GetLauncherManager;

public id: string = GAME_ID;
public name = `Mount & Blade II: Bannerlord (BUTR)`;
Expand All @@ -29,27 +30,29 @@ export class BannerlordGame implements types.IGame {
customOpenModsPath: MODULES,
};

constructor(api: types.IExtensionApi, launcherManager: VortexLauncherManager) {
constructor(api: types.IExtensionApi, getLauncherManager: GetLauncherManager) {
this._api = api;
this._launcherManager = launcherManager;
this._getLauncherManager = getLauncherManager;
}

public queryPath = (): Bluebird<string | types.IGameStoreEntry> => {
return toBluebird(findGame)({}).then((game) => game.gamePath);
};
public queryPath = toBluebird<string | types.IGameStoreEntry>(async (): Promise<string | types.IGameStoreEntry> => {
const game = await findGame();
return game.gamePath;
});
public queryModPath = (_gamePath: string): string => {
return `.`;
};
public getGameVersion = (_gamePath: string, _exePath: string): PromiseLike<string> => {
return this._launcherManager.getGameVersionVortexAsync();
return this._getLauncherManager().getGameVersionVortexAsync();
};
public executable = (discoveredPath?: string): string => {
return getBannerlordMainExe(discoveredPath, this._api);
};
public setup = toBluebird((discovery: types.IDiscoveryResult): Bluebird<void> => {
return toBluebird(setup)(this._api, discovery, this._launcherManager);
public setup = toBluebird(async (discovery: types.IDiscoveryResult) => {
const launcherManager = this._getLauncherManager();
await setup(this._api, discovery, launcherManager);
});
//public requiresLauncher = toBluebird((_gamePath: string, store?: string) => {
// return requiresLauncher(store);
//public requiresLauncher = toBluebird(async (_gamePath: string, store?: string) => {
// return await requiresLauncher(store);
//});
}
69 changes: 54 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import { version } from '../package.json';
const main = (context: types.IExtensionContext): boolean => {
log('info', `Extension Version: ${version}`);

const launcherManager = new VortexLauncherManager(context.api);

const getLoadOrderManager = () => LoadOrderManager.getInstance(context.api, launcherManager);
const getSaveManager = () => SaveManager.getInstance(context.api, launcherManager);
const getLauncherManager = () => VortexLauncherManager.getInstance(context.api);
const getLoadOrderManager = () => LoadOrderManager.getInstance(context.api, getLauncherManager);
const getSaveManager = () => SaveManager.getInstance(context.api, getLauncherManager);

// Register Settings
context.registerReducer([`settings`, GAME_ID], reducer);
Expand All @@ -46,7 +45,7 @@ const main = (context: types.IExtensionContext): boolean => {
// Register Settings

// Register Game
context.registerGame(new BannerlordGame(context.api, launcherManager));
context.registerGame(new BannerlordGame(context.api, getLauncherManager));

/*
// Register Collection Feature
Expand All @@ -70,7 +69,7 @@ const main = (context: types.IExtensionContext): boolean => {
'savegame',
'Saves',
SaveList,
new SavePageOptions(context, launcherManager, getSaveManager())
new SavePageOptions(context, getLauncherManager, getSaveManager)
);

// Register Installer.
Expand All @@ -91,8 +90,14 @@ const main = (context: types.IExtensionContext): boolean => {
context.registerInstaller(
`bannerlord-module-installer`,
25,
toBluebird(launcherManager.testModule),
toBluebird(launcherManager.installModule)
toBluebird(async (files: string[], gameId: string) => {
const launcherManager = getLauncherManager();
return await launcherManager.testModule(files, gameId);
}),
toBluebird(async (files: string[], destinationPath: string) => {
const launcherManager = getLauncherManager();
return await launcherManager.installModule(files, destinationPath);
})
);
context.registerModType(
'bannerlord-module',
Expand All @@ -104,13 +109,45 @@ const main = (context: types.IExtensionContext): boolean => {
// Register Installer.

// Register AutoSort button
const autoSortIcon = launcherManager.isSorting() ? `spinner` : `loot-sort`;
const autoSortAction = (_instanceIds?: string[]): boolean | void => launcherManager.autoSort();
const autoSortCondition = (_instanceIds?: string[]): boolean =>
selectors.activeGameId(context.api.getState()) === GAME_ID;
context.registerAction(`fb-load-order-icons`, 200, autoSortIcon, {}, `Auto Sort`, autoSortAction, autoSortCondition);
context.registerAction(
`fb-load-order-icons`,
200,
`loot-sort`,
{},
`Auto Sort`,
(_instanceIds?: string[]): boolean | void => {
const launcherManager = getLauncherManager();
launcherManager.autoSort();
},
(_instanceIds?: string[]): boolean => {
const state = context.api.getState();
const gameId = selectors.activeGameId(state);
return gameId === GAME_ID;
}
);
// Register AutoSort button

// Register Fetch Compatibility Scores button
/* Disabled for now because the name is too long
context.registerAction(
`fb-load-order-icons`,
201,
`changelog`,
{},
`Fetch Compatibility Scores`,
(_instanceIds?: string[]): boolean | void => {
const loadOrderManager = getLoadOrderManager();
loadOrderManager.updateCompatibilityScores();
},
(_instanceIds?: string[]): boolean => {
const state = context.api.getState();
const gameId = selectors.activeGameId(state);
return gameId === GAME_ID;
}
);
*/
// Register Fetch Compatibility Scores button

// Register Callbacks
context.once(
toBluebird<void>(async () => {
Expand All @@ -121,13 +158,15 @@ const main = (context: types.IExtensionContext): boolean => {
return;
}
try {
await getLoadOrderManager().deserializeLoadOrder();
const loadOrderManager = getLoadOrderManager();
await loadOrderManager.deserializeLoadOrder();
} catch (err) {
context.api.showErrorNotification?.('Failed to deserialize load order file', err);
return;
}
try {
getSaveManager().reloadSave();
const saveManager = getSaveManager();
saveManager.reloadSave();
} catch (err) {
context.api.showErrorNotification?.('Failed to reload the currect save file', err);
return;
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { types } from 'vortex-api';
import { types as vetypes } from '@butr/vortexextensionnative';
import { GAME_ID } from './common';
import { LoadOrderManager, SaveManager, VortexLauncherManager } from './utils';

export type RequiredProperties<T, P extends keyof T> = Omit<T, P> & Required<Pick<T, P>>;

export type GetLauncherManager = () => VortexLauncherManager;
export type GetLoadOrderManager = () => LoadOrderManager;
export type GetSaveManager = () => SaveManager;

export interface IModuleCompatibilityInfoCache {
[moduleId: string]: IModuleCompatibilityInfo;
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/blse/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { findBLSEMod } from './shared';
import { GAME_ID } from '../../common';
import { hasSettingsInterfacePrimaryTool } from '../vortex';
import { LoadOrderManager } from '../loadOrder';
import { GetLoadOrderManager } from '../../types';

/**
* Event function, be careful
*/
export const didDeployEvent = async (
api: types.IExtensionApi,
profileId: string,
getLoadOrderManager: () => LoadOrderManager
getLoadOrderManager: GetLoadOrderManager
) => {
const state = api.getState();
const profile = selectors.profileById(state, profileId);
Expand Down
Loading

0 comments on commit 45edcef

Please sign in to comment.