Implement the plugin button
This commit is contained in:
parent
35a33b89f4
commit
c089c52d05
35
src/index.ts
35
src/index.ts
@ -2,21 +2,27 @@ import { log, selectors, types, util } from "vortex-api";
|
|||||||
import { IExtensionContext } from 'vortex-api/lib/types/api';
|
import { IExtensionContext } from 'vortex-api/lib/types/api';
|
||||||
import { loadWasm } from "./wasmLoader";
|
import { loadWasm } from "./wasmLoader";
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
const supportedGames: string[] = [ 'skyrimse', 'skyrimspecialedition', 'skyrimvr' ];
|
const supportedGames: string[] = [ 'skyrimse', 'skyrimspecialedition', 'skyrimvr' ];
|
||||||
const modMapperBase: string = 'https://modmapper.com/';
|
const modMapperBase: string = 'https://modmapper.com/';
|
||||||
|
|
||||||
const modmapperModUrl = (gameId: string, modId: number): string => `${modMapperBase}?mod=${modId}&game=${gameId}`;
|
const modmapperModUrl = (gameId: string, modId: number): string => `${modMapperBase}?mod=${modId}&game=${gameId}`;
|
||||||
|
const modmapperPluginUrl = (hash: string): string => `${modMapperBase}?plugin=${hash}`;
|
||||||
|
|
||||||
function pluginPath(pluginName : string, api : types.IExtensionApi): string {
|
function pluginPath(pluginName: string, api: types.IExtensionApi): string {
|
||||||
const store = api.store
|
const store = api.store
|
||||||
const activeGameId = selectors.activeGameId(store.getState());
|
const activeGameId = selectors.activeGameId(store.getState());
|
||||||
|
|
||||||
const gamePath = util.getSafe(store.getState(), ['settings', 'gameMode', 'discovered', activeGameId, 'path'], undefined);
|
const gamePath = util.getSafe(store.getState(), ['settings', 'gameMode', 'discovered', activeGameId, 'path'], undefined);
|
||||||
return `${gamePath}${pluginName}`;
|
return path.join(gamePath, 'Data', pluginName);
|
||||||
}
|
}
|
||||||
|
|
||||||
//This is the main function Vortex will run when detecting the game extension.
|
//This is the main function Vortex will run when detecting the game extension.
|
||||||
function main(context: IExtensionContext) {
|
function main(context: IExtensionContext) {
|
||||||
|
let hash_plugin = null;
|
||||||
|
loadWasm().then((wasm) => hash_plugin = wasm.hash_plugin);
|
||||||
|
|
||||||
// Add button to the mods table.
|
// Add button to the mods table.
|
||||||
context.registerAction('mods-action-icons', 999, 'highlight-map', {}, 'See on Modmapper',
|
context.registerAction('mods-action-icons', 999, 'highlight-map', {}, 'See on Modmapper',
|
||||||
@ -30,7 +36,6 @@ function main(context: IExtensionContext) {
|
|||||||
// Is this mod from gather the info about the mod.
|
// Is this mod from gather the info about the mod.
|
||||||
const game: string = mod.attributes?.downloadGame;
|
const game: string = mod.attributes?.downloadGame;
|
||||||
const modId: number = mod.attributes?.modId;
|
const modId: number = mod.attributes?.modId;
|
||||||
loadWasm(({ hash_plugin }) => alert(hash_plugin(new Uint8Array([1, 2, 3]))));
|
|
||||||
return util.opn(modmapperModUrl(game, modId)).catch((err) => log('error', 'Could not open web page', err));
|
return util.opn(modmapperModUrl(game, modId)).catch((err) => log('error', 'Could not open web page', err));
|
||||||
},
|
},
|
||||||
(instanceIds: string[]): boolean => {
|
(instanceIds: string[]): boolean => {
|
||||||
@ -51,12 +56,30 @@ function main(context: IExtensionContext) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Add button to the plugins table.
|
// Add button to the plugins table.
|
||||||
context.registerAction('gamebryo-plugins-action-icons', 100, 'highlight-map', {}, 'See on Modmapper',
|
context.registerAction('gamebryo-plugins-action-icons', 100, 'highlight-map', {}, ' See on Modmapper',
|
||||||
(instanceIds: string[]) => {
|
(instanceIds: string[]) => {
|
||||||
alert(pluginPath(instanceIds[0], context.api));
|
const pluginData = util.getSafe(context.api.store.getState(), ['session', 'plugins', 'pluginInfo', instanceIds[0].toLowerCase()], undefined);
|
||||||
|
fs.readFile(pluginData?.filePath ?? pluginPath(instanceIds[0], context.api), (err, data) => {
|
||||||
|
if (err) {
|
||||||
|
alert('Failed to read plugin: ' + err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hash_plugin === null) {
|
||||||
|
throw new Error('Failed to load WebAssembly module. Cannot hash plugin.');
|
||||||
|
}
|
||||||
|
const hash = hash_plugin(Uint8Array.from(Buffer.from(data))).toString(36);
|
||||||
|
util.opn(modmapperPluginUrl(hash)).catch((err) => log('error', 'Could not open web page', err));
|
||||||
|
})
|
||||||
|
|
||||||
},
|
},
|
||||||
(instanceIds: string[]) => {
|
(instanceIds: string[]) => {
|
||||||
return true;
|
// Should we show the option?
|
||||||
|
const state = context.api.getState();
|
||||||
|
const gameId: string = selectors.activeGameId(state);
|
||||||
|
// Make sure active game is a supported game
|
||||||
|
const gameIsSupported: boolean = supportedGames.includes(gameId);
|
||||||
|
return gameIsSupported;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
export function loadWasm(closure) {
|
export function loadWasm() {
|
||||||
import("skyrim-cell-dump-wasm").then(closure);
|
return import("skyrim-cell-dump-wasm");
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user