Skip to content

Commit

Permalink
lslib_utils is worker_thread friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
khbsd committed May 28, 2024
1 parent 737ef0f commit 1e9db5b
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 84 deletions.
12 changes: 5 additions & 7 deletions commands/debug2.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { saveConfigFile, loadConfigFile } = require('../support_files/helper_func
const { CREATE_LOGGER, raiseError, raiseInfo } = require('../support_files/log_utils');
var bg3mh_logger = CREATE_LOGGER();

const { FIND_FILES, getFormats, dirSeparator } = require('../support_files/lslib_utils.js');
const { FIND_FILES, getFormats, dirSeparator, LOAD_LSLIB } = require('../support_files/lslib_utils.js');
const { pak } = getFormats();
const { processPak } = require('../support_files/process_pak.js');

Expand All @@ -29,24 +29,22 @@ const debug2 = vscode.commands.registerCommand('bg3-mod-helper.debug2Command', a

let halfCoreCount = os.availableParallelism() / 2;
let workerArray = [];
let workerInfo = await loadConfigFile(true);
let workerConfig = JSON.parse(loadConfigFile(true));
// let workerLSLIB = await LOAD_LSLIB();

if (isMainThread) {
for (let i = 0; i < halfCoreCount; i++) {
workerArray.push(new Worker(__dirname + "/worker_test.js"));
workerArray.push(new Worker(__dirname + "/worker_test.js", { workerData: workerConfig }));

workerArray[i].on('message', (message) => {
console.log(`${message} received from worker ${workerArray[i].threadId}!`)
});

workerArray[i].postMessage(workerInfo);
workerArray[i].postMessage(workerConfig);
}

}

saveConfigFile();
console.log(await loadConfigFile(true));


});

Expand Down
19 changes: 12 additions & 7 deletions commands/worker_test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// const { CREATE_LOGGER, raiseError, raiseInfo } = require('../support_files/log_utils.js');
// const bg3mh_logger = CREATE_LOGGER();
const { parentPort } = require('node:worker_threads');
const { CREATE_LOGGER, raiseError, raiseInfo } = require('../support_files/log_utils.js');
const bg3mh_logger = CREATE_LOGGER();
const { parentPort, workerData } = require('node:worker_threads');
const path = require('node:path');
const fs = require('node:fs');
const { LOAD_LSLIB } = require('../support_files/lslib_utils.js')


parentPort.once('message', (message) => {
let messageParsed = JSON.parse(message);
parentPort.once('message', async () => {
raiseInfo(`lookee lslib @ ${workerData.lslibPath}`)
parentPort.postMessage("i got the workerData bitchhhhhhh");

let rootModPath = messageParsed.rootModPath;
parentPort.postMessage(rootModPath);
/*try {
let LSLIB = await LOAD_LSLIB();
} catch (Error) {
raiseError(Error);
}*/
});
23 changes: 5 additions & 18 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const setupFunctionDescriptionHoverProvider = require('./hovers/functionDescript
const setupUuidsHandlesHoverProvider = require('./hovers/uuidsHandlesCollector');
const { resizeImageTooltip, resizeImageController, resizeImageHotbar, resizeImageCustom } = require('./commands/resizeImage');

const { getFullPath } = require('./support_files/helper_functions');
const { getFullPath, saveConfigFile, loadConfigFile } = require('./support_files/helper_functions');


async function addToExcludeList(fileUri) {
Expand Down Expand Up @@ -101,23 +101,10 @@ function activate(context) {
});
}

let config = vscode.workspace.getConfiguration('bg3ModHelper');

setConfig({
maxFilesToShow: config.get('hover.maxFiles'),
hoverEnabled: config.get('hover.enabled'),
maxCacheSize: config.get('maxCacheSize'),
rootModPath: config.get('rootModPath'),
modDestPath: config.get('modDestPath'),
lslibPath: config.get('lslibPath'),
autoLaunchOnPack: config.get('autoLaunchOnPack'),
launchContinueGame: config.get('launchContinueGame'),
addHandlesToAllLocas: config.get('addHandlesToAllLocas'),
excludedFiles: config.get('excludedFiles') || [],
gameInstallLocation: config.get('gameInstallLocation')
});
raiseInfo('Initial configs set:' + JSON.stringify(config, null, 2), false);

if (loadConfigFile(true) === undefined) {
saveConfigFile();
}

if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) {
vscode.window.showWarningMessage(
'bg3-mod-helper extension requires a workspace to be set for optimal functionality, one not found.'
Expand Down
50 changes: 30 additions & 20 deletions support_files/helper_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { getConfig, setConfig } = require('./config');
const fs = require('fs');

const rootModPath = getConfig().rootModPath;
const vscodeDirPath = path.join(rootModPath, '.vscode');
const vscodeDirPath = path.join(rootModPath, '/.vscode');
const settingsFilePath = path.join(vscodeDirPath, 'settings.json');

const { CREATE_LOGGER, raiseInfo } = require('./log_utils');
Expand Down Expand Up @@ -70,37 +70,47 @@ async function findInstancesInWorkspace(word, currentFilePath, maxFilesToShow) {
}


async function saveConfigFile(settingToSave = "all") {
function saveConfigFile(settingToSave = "all") {
let config = vscode.workspace.getConfiguration('bg3ModHelper');
let settings = {};
let allSettings = {
maxFilesToShow: config.get('hover.maxFiles'),
hoverEnabled: config.get('hover.enabled'),
maxCacheSize: config.get('maxCacheSize'),
rootModPath: config.get('rootModPath'),
modDestPath: config.get('modDestPath'),
lslibPath: config.get('lslibPath'),
autoLaunchOnPack: config.get('autoLaunchOnPack'),
launchContinueGame: config.get('launchContinueGame'),
addHandlesToAllLocas: config.get('addHandlesToAllLocas'),
excludedFiles: config.get('excludedFiles') || [],
gameInstallLocation: config.get('gameInstallLocation')
};

try {
console.log(fs.statSync(vscodeDirPath).isFile());
} catch (error) {
setConfig(allSettings);
console.log(`settings set to ${allSettings}`)
}

console.log(`${settingsFilePath}\n${rootModPath}`)
if (!fs.statSync(vscodeDirPath).isFile()) {
fs.mkdirSync(vscodeDirPath, { recursive: true });
}

if (settingToSave === "all") {
settings = {
maxFilesToShow: config.get('hover.maxFiles'),
hoverEnabled: config.get('hover.enabled'),
maxCacheSize: config.get('maxCacheSize'),
rootModPath: config.get('rootModPath'),
modDestPath: config.get('modDestPath'),
lslibPath: config.get('lslibPath'),
autoLaunchOnPack: config.get('autoLaunchOnPack'),
launchContinueGame: config.get('launchContinueGame'),
addHandlesToAllLocas: config.get('addHandlesToAllLocas'),
excludedFiles: config.get('excludedFiles') || [],
gameInstallLocation: config.get('gameInstallLocation')
};
setConfig(allSettings);
} else { // it's gonna do something else eventually
setConfig(allSettings);
}

setConfig(settings);
let settingsJson = JSON.stringify(config, null, 4);

fs.writeFileSync(settingsFilePath, settingsJson, 'utf8');
raiseInfo('Initial configs set:' + JSON.stringify(config, null, 4), false);
raiseInfo('Initial configs set:\n' + JSON.stringify(config, null, 4), false);
}


async function loadConfigFile(get = false) {
function loadConfigFile(get = false) {
let settingsContent;

if (fs.statSync(settingsFilePath).isFile()) {
Expand Down
6 changes: 2 additions & 4 deletions support_files/log_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
var lslibPath;
var logPath;

const { isMainThread } = require('worker_threads');
const { isMainThread, workerData } = require('worker_threads');

// TODO: clear logs function

Expand Down Expand Up @@ -87,7 +87,7 @@ if (isMainThread) {
bg3mh_logger.info(info);
}
} else {
lslibPath = require('./config').getConfig().lslibPath;
lslibPath = workerData.lslibPath;
logPath = path.normalize(lslibPath + "\\logs\\bg3mh_log_" + LOGDATE() + ".log");

CREATE_LOGGER = () => {
Expand Down Expand Up @@ -144,6 +144,4 @@ if (isMainThread) {
}




module.exports = { LOGDATE, logPath, CREATE_LOGGER, raiseError, raiseInfo };
95 changes: 67 additions & 28 deletions support_files/lslib_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
*/
const path = require('path');
const fs = require('fs');
const vscode = require('vscode');
const findFiles = vscode.workspace.findFiles;
const parse = vscode.Uri.parse;


// loads the api
Expand All @@ -23,11 +20,6 @@ const TOOL_SUBDIR = 'Tools\\';
const { CREATE_LOGGER, raiseError, raiseInfo } = require('./log_utils.js')
const bg3mh_logger = CREATE_LOGGER();

const { getConfig } = require('./config.js');
const { lslibPath } = getConfig();
const compatRootModPath = path.join(getConfig().rootModPath + "\\");
const lslibToolsPath = path.join(getConfig().lslibPath, TOOL_SUBDIR);

// separated for future use
const elasticDlls = ['Elastic.Transport.dll', 'Elastic.Clients.Elasticsearch.dll'];
const storyCompilerDlls = ['StoryCompiler.dll', 'StoryDecompiler.dll'];
Expand All @@ -40,9 +32,28 @@ const convertDirs = ["[PAK]_UI", "[PAK]_Armor", "RootTemplates", "MultiEffectInf
// excluding this because it will match to "UI" in convertDirs
const illegalFiles = ["Icons_Items.lsx"];

// tools to test where the process is
const { isMainThread, workerData } = require('node:worker_threads');

var DLLS = [];
var DLL_PATHS;
var LSLIB;
var DLL_PATHS, LSLIB, lslibPath, compatRootModPath, lslibToolsPath, vscode, findFiles, parse;
var getConfig;


if (isMainThread) {
getConfig = require('./config.js').getConfig;
lslibPath = getConfig().lslibPath;
compatRootModPath = path.join(getConfig().rootModPath + "\\");
lslibToolsPath = path.join(getConfig().lslibPath, TOOL_SUBDIR);
vscode = require('vscode');
findFiles = vscode.workspace.findFiles;
parse = vscode.Uri.parse;
} else {
getConfig = workerData;
lslibPath = getConfig.lslibPath;
compatRootModPath = path.join(getConfig.rootModPath + "\\");
lslibToolsPath = path.join(getConfig.lslibPath, TOOL_SUBDIR);
}


function getFormats() {
Expand Down Expand Up @@ -106,21 +117,36 @@ async function loadDlls() {

// handles the finding of LSLib. logs will be created wherever this laods from.
async function LOAD_LSLIB() {
if (fs.existsSync(path.join(lslibPath, LSLIB_DLL))) {
DLL_PATHS = await FIND_FILES(getFormats().dll, lslibPath);
}
else if (fs.existsSync(path.join(lslibToolsPath, LSLIB_DLL))) {
DLL_PATHS = await FIND_FILES(getFormats().dll, lslibToolsPath);
}
else {
raiseError("LSLib.dll not found at " + lslibPath + ".", false);
vscode.window.showErrorMessage(`LSLib.dll not found at ${lslibPath}. Are you sure you aren't using the legacy option using divine.exe?`);
return null;
if (isMainThread) {
if (fs.existsSync(path.join(lslibPath, LSLIB_DLL))) {
DLL_PATHS = await FIND_FILES(getFormats().dll, lslibPath);
}
else if (fs.existsSync(path.join(lslibToolsPath, LSLIB_DLL))) {
DLL_PATHS = await FIND_FILES(getFormats().dll, lslibToolsPath);
}
else {
raiseError("LSLib.dll not found at " + lslibPath + ".", false);
vscode.window.showErrorMessage(`LSLib.dll not found at ${lslibPath}. Are you sure you aren't using the legacy option using divine.exe?`);
return null;
}

} else {
if (fs.existsSync(path.join(lslibPath, LSLIB_DLL))) {
DLL_PATHS = FIND_FILES_SYNC(lslibPath, getFormats().dll);
}
else if (fs.existsSync(path.join(lslibToolsPath, LSLIB_DLL))) {
DLL_PATHS = FIND_FILES_SYNC(lslibToolsPath, getFormats().dll);
}
else {
raiseError("LSLib.dll not found at " + lslibPath + ".");
return null;
}
}

processDllPaths();
await loadDlls();
raiseInfo(`${DLL_PATHS} \n.dlls loaded`, false);
bg3mh_logger.info(`${DLL_PATHS} \n.dlls loaded`);
console.log(`${DLL_PATHS} \n.dlls loaded`);

// have to ignore this because the ts-linter doesn't know 'LSLib' exists :starege:
// @ts-ignore
Expand All @@ -131,7 +157,7 @@ async function LOAD_LSLIB() {

// returns an array with the absolute paths to every file found with the target file extension.
// maybe replace with findFiles()?
function FIND_FILES_v1(filesPath, targetExt = getFormats().lsf, isRecursive = true) {
function FIND_FILES_SYNC(filesPath, targetExt = getFormats().lsf, isRecursive = true) {
let filesToConvert = [];

// console.log(filesPath);
Expand Down Expand Up @@ -161,12 +187,12 @@ async function FIND_FILES(targetExt = getFormats().lsf, filesPath = '**/*') {

if (targetExt === getFormats().dll) {
let dllDir = new vscode.RelativePattern(filesPath, '*' + targetExt);
console.log(dllDir);
// console.log(dllDir);
filesList = (await findFiles(dllDir)).map(file => file.path);
}
else if (targetExt === getFormats().pak) {
let dllDir = new vscode.RelativePattern(filesPath, '**/*' + targetExt);
console.log(dllDir);
// console.log(dllDir);
filesList = (await findFiles(dllDir)).map(file => dirSeparator(file.path));
}
else {
Expand All @@ -184,7 +210,13 @@ async function FIND_FILES(targetExt = getFormats().lsf, filesPath = '**/*') {


function FILTER_PATHS(filesPath) {
let excludedFiles = getConfig().excludedFiles;
let excludedFiles = [];
if (isMainThread) {
excludedFiles = getConfig().excludedFiles;
} else {
excludedFiles = getConfig.excludedFiles;
}

if (Array.isArray(filesPath)) {
let filteredPaths = [];

Expand Down Expand Up @@ -222,6 +254,8 @@ function FILTER_PATHS(filesPath) {

// here in case people (i'm people) have their working directory and their AppData on different hard drives.
function moveFileAcrossDevices(sourcePath, destPath, raiseError) {
let infoMsg = `${path.basename(sourcePath)} moved to ${destPath}.`;

fs.readFile(sourcePath, (readErr, data) => {
if (readErr) {
raiseError(readErr);
Expand All @@ -241,9 +275,14 @@ function moveFileAcrossDevices(sourcePath, destPath, raiseError) {
});
});
});
raiseInfo(path.basename(sourcePath) + " moved to " + destPath, false);
vscode.window.showInformationMessage(`${path.basename(sourcePath)} moved to ${destPath}.`);

if (isMainThread) {
raiseInfo(infoMsg, false);
vscode.window.showInformationMessage(infoMsg);
} else {
raiseInfo(infoMsg);
}
}


module.exports = { LSLIB, LOAD_LSLIB, FIND_FILES, FIND_FILES_v1, FILTER_PATHS, getFormats, moveFileAcrossDevices, baseNamePath, dirSeparator, compatRootModPath };
module.exports = { LSLIB, LOAD_LSLIB, FIND_FILES, FIND_FILES_SYNC, FILTER_PATHS, getFormats, moveFileAcrossDevices, baseNamePath, dirSeparator, compatRootModPath };

0 comments on commit 1e9db5b

Please sign in to comment.