Skip to content

Commit

Permalink
multithreaded_maybe??????????????
Browse files Browse the repository at this point in the history
  • Loading branch information
khbsd committed May 27, 2024
1 parent a1a2c70 commit 737ef0f
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 82 deletions.
45 changes: 24 additions & 21 deletions commands/debug2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,42 @@ const { lslibPath, rootModPath, gameInstallLocation } = getConfig();
const compatRootModPath = path.join(rootModPath + "\\");
const lslibToolsPath = path.join(lslibPath, TOOL_SUBDIR);

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

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 { pak } = getFormats();
const { processPak } = require('../support_files/process_pak.js');

const { isMainThread, parentPort, Worker } = require('node:worker_threads')


const debug2 = vscode.commands.registerCommand('bg3-mod-helper.debug2Command', async function () {
// raiseInfo("hi dipshit! 💩");
// let test_dir = path.join(rootModPath, 'unpacking_test\\test_paks');
let test_dir = path.join(gameInstallLocation, 'Data');
let unpackDest = "W:\\Libraries\\Documents\\Baldur's Gate 3 Mods\\unpacked_game_data";
let unpackList = await FIND_FILES(pak, test_dir);

console.log(test_dir);

raiseInfo(unpackList);

// for (let i = 0; i < unpackList.length; i++) {
for await (const pakPath of unpackList) {
//let pakPath = dirSeparator(unpackList[i]);
//let pakPath = unpackList[i];
let modName = path.basename(pakPath, pak);
console.log(modName)
//let unpackDest = dirSeparator(path.join(path.dirname(pakPath), modName));
console.log(unpackDest);

raiseInfo(`${modName} unpacking started`);
// processPak(pakPath, modName, unpackDest);
raiseInfo(`${modName} unpacking ended`);

let halfCoreCount = os.availableParallelism() / 2;
let workerArray = [];
let workerInfo = await loadConfigFile(true);

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

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

workerArray[i].postMessage(workerInfo);
}

}

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


});

module.exports = { debug2 }
7 changes: 4 additions & 3 deletions commands/packMod.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ const packModCommand = vscode.commands.registerCommand('bg3-mod-helper.packMod',
}
}

// changed existsSync() to statSync().isFile(), since existsSync() is being deprecated. this section can be shortened with the saveConfigFile and loadConfigFile functions in helper_functions.js
// Path to .vscode directory and settings file
const vscodeDirPath = path.join(rootModPath, '.vscode');
const settingsFilePath = path.join(vscodeDirPath, 'settings.json');
let settingsContent = '';

// Check and save settings.json if .vscode exists
if (fs.existsSync(vscodeDirPath)) {
if (fs.existsSync(settingsFilePath)) {
if (fs.statSync(vscodeDirPath).isFile()) {
if (fs.statSync(settingsFilePath).isFile()) {
settingsContent = fs.readFileSync(settingsFilePath, 'utf8');
}
// console.log('test1')
Expand All @@ -104,7 +105,7 @@ const packModCommand = vscode.commands.registerCommand('bg3-mod-helper.packMod',

if (settingsContent) {
// console.log('test3')
if (!fs.existsSync(vscodeDirPath)) {
if (!fs.statSync(vscodeDirPath).isFile()) {
fs.mkdirSync(vscodeDirPath, { recursive: true });
// console.log('test4')
}
Expand Down
10 changes: 10 additions & 0 deletions commands/worker_test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// const { CREATE_LOGGER, raiseError, raiseInfo } = require('../support_files/log_utils.js');
// const bg3mh_logger = CREATE_LOGGER();
const { parentPort } = require('node:worker_threads');
const path = require('node:path');
const fs = require('node:fs');


parentPort.once('message', (message) => {
let messageParsed = JSON.parse(message);

let rootModPath = messageParsed.rootModPath;
parentPort.postMessage(rootModPath);
});
5 changes: 3 additions & 2 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const addDependenciesCommand = require('./commands/addDependencies')

const AutoCompleteProvider = require('./autocomplete/autoCompleteProvider');

const { CREATE_LOGGER } = require('./support_files/log_utils');
const { CREATE_LOGGER, raiseInfo } = require('./support_files/log_utils');
var bg3mh_logger = CREATE_LOGGER();

const debugCommand = require('./commands/debug');
Expand Down Expand Up @@ -116,7 +116,8 @@ function activate(context) {
excludedFiles: config.get('excludedFiles') || [],
gameInstallLocation: config.get('gameInstallLocation')
});
bg3mh_logger.info('Initial configs set:' + JSON.stringify(config, null, 2))
raiseInfo('Initial configs set:' + JSON.stringify(config, null, 2), false);

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
56 changes: 53 additions & 3 deletions support_files/helper_functions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const vscode = require('vscode');
const path = require('path');
const { getConfig } = require('./config');
const { getConfig, setConfig } = require('./config');
const fs = require('fs');

const { CREATE_LOGGER } = require('./log_utils');
const rootModPath = getConfig().rootModPath;
const vscodeDirPath = path.join(rootModPath, '.vscode');
const settingsFilePath = path.join(vscodeDirPath, 'settings.json');

const { CREATE_LOGGER, raiseInfo } = require('./log_utils');
var bg3mh_logger = CREATE_LOGGER();

// Function to insert text, replacing the current selection
Expand Down Expand Up @@ -65,4 +69,50 @@ async function findInstancesInWorkspace(word, currentFilePath, maxFilesToShow) {
return instances;
}

module.exports = { insertText, findInstancesInWorkspace, getFullPath };

async function saveConfigFile(settingToSave = "all") {
let config = vscode.workspace.getConfiguration('bg3ModHelper');
let settings = {};

console.log(`${settingsFilePath}\n${rootModPath}`)

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(settings);
let settingsJson = JSON.stringify(config, null, 4);

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


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

if (fs.statSync(settingsFilePath).isFile()) {
settingsContent = fs.readFileSync(settingsFilePath, 'utf8');
raiseInfo(settingsContent, false);

if (get) {
return settingsContent;
} else {
return undefined;
}
}
}

module.exports = { insertText, findInstancesInWorkspace, getFullPath, loadConfigFile, saveConfigFile };
170 changes: 117 additions & 53 deletions support_files/log_utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const path = require('path');
const vscode = require('vscode');

const lslibPath = require('./config').getConfig().lslibPath;
const logPath = path.normalize(lslibPath + "\\logs\\bg3mh_log_" + LOGDATE() + ".log");
var lslibPath;
var logPath;

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

// TODO: clear logs function

Expand All @@ -20,66 +21,129 @@ function LOGDATE() {
return date_string;
}


function CREATE_LOGGER() {
var log4js = require('log4js');
log4js.configure ({
appenders: {
bg3mh_logger: {
type: "file",
filename: logPath,
layout: {
type: "pattern",
pattern: "[%d] %f{1}:%l in %M [%p]: %m"
}
}
},
categories: {
default: {
appenders:
["bg3mh_logger"],
level: "debug",
enableCallStack: true
var raiseError, raiseInfo, CREATE_LOGGER;


if (isMainThread) {
const vscode = require('vscode');
lslibPath = require('./config').getConfig().lslibPath;
logPath = path.normalize(lslibPath + "\\logs\\bg3mh_log_" + LOGDATE() + ".log");

CREATE_LOGGER = () => {
var log4js = require('log4js');
log4js.configure ({
appenders: {
bg3mh_logger: {
type: "file",
filename: logPath,
layout: {
type: "pattern",
pattern: "[%d] %f{1}:%l in %M [%p]: %m"
}
}
},
error: {
appenders:
["bg3mh_logger"],
level: "error",
enableCallStack: true
categories: {
default: {
appenders:
["bg3mh_logger"],
level: "debug",
enableCallStack: true
},
error: {
appenders:
["bg3mh_logger"],
level: "error",
enableCallStack: true
},
info: {
appenders:
["bg3mh_logger"],
level: "info",
enableCallStack: true
},
},
info: {
appenders:
["bg3mh_logger"],
level: "info",
enableCallStack: true
},
},
});
});

return log4js.getLogger("bg3mh_logger");
}

return log4js.getLogger("bg3mh_logger");
}
raiseError = (error, popup = true) => {
var bg3mh_logger = CREATE_LOGGER();

if (popup) {
vscode.window.showErrorMessage(`${error}`);
}
console.error(error);
bg3mh_logger.error(error);
}

raiseInfo = (info, popup = true) => {
var bg3mh_logger = CREATE_LOGGER();

if (popup) {
vscode.window.showInformationMessage(`${info}`);
}
console.info(info);
bg3mh_logger.info(info);
}
} else {
lslibPath = require('./config').getConfig().lslibPath;
logPath = path.normalize(lslibPath + "\\logs\\bg3mh_log_" + LOGDATE() + ".log");

CREATE_LOGGER = () => {
var log4js = require('log4js');
log4js.configure ({
appenders: {
bg3mh_logger: {
type: "file",
filename: logPath,
layout: {
type: "pattern",
pattern: "[%d] %f{1}:%l in %M [%p]: %m"
}
}
},
categories: {
default: {
appenders:
["bg3mh_logger"],
level: "debug",
enableCallStack: true
},
error: {
appenders:
["bg3mh_logger"],
level: "error",
enableCallStack: true
},
info: {
appenders:
["bg3mh_logger"],
level: "info",
enableCallStack: true
},
},
});

return log4js.getLogger("bg3mh_logger");
}

raiseError = (error) => {
var bg3mh_logger = CREATE_LOGGER();

function raiseError(error, popup = true) {
var bg3mh_logger = CREATE_LOGGER();
console.error(error);
bg3mh_logger.error(error);
}

raiseInfo = (info) => {
var bg3mh_logger = CREATE_LOGGER();

if (popup) {
vscode.window.showErrorMessage(`${error}`);
console.info(info);
bg3mh_logger.info(info);
}
console.error(error);
bg3mh_logger.error(error);
}


function raiseInfo(info, popup = true) {
var bg3mh_logger = CREATE_LOGGER();

if (popup) {
vscode.window.showInformationMessage(`${info}`);
}
console.info(info);
bg3mh_logger.info(info);
}


module.exports = { LOGDATE, logPath, CREATE_LOGGER, raiseError, raiseInfo };

0 comments on commit 737ef0f

Please sign in to comment.