Skip to content

Commit

Permalink
merge with dev and lots of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
khbsd committed Jun 5, 2024
1 parent 2551b30 commit 73e926b
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 96 deletions.
34 changes: 34 additions & 0 deletions commands/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const vscode = require('vscode');
const fs = require('fs');
const os = require('os');

const path = require('path');

const LSLIB_DLL = 'LSLib.dll';
const TOOL_SUBDIR = 'Tools\\';

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

const { } = 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, LOAD_LSLIB } = 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 { jobs } = require('../support_files/conversion_junction');


const debug = vscode.commands.registerCommand('bg3-mod-helper.debugCommand', async function () {
raiseInfo("hi dipshit! 💩");
});


module.exports = { debug }
2 changes: 1 addition & 1 deletion commands/packMod.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const packModCommand = vscode.commands.registerCommand('bg3-mod-helper.packMod',
}

// send the directory to the convert() function, and let it know it's a pak
await convert(rootModPath, pak, modName, getConfig().zipOnPack);
await convert(rootModPath, pak, modName);

if (autoLaunchOnPack) {
vscode.commands.executeCommand('bg3-mod-helper.launchGame');
Expand Down
2 changes: 1 addition & 1 deletion extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function aSimpleDataProvider() {
return Promise.resolve([
{ label: 'Reload Window', command: 'workbench.action.reloadWindow' },
{ label: 'Extension Settings', command: 'workbench.action.openSettings', arguments: 'bg3ModHelper' },
{ label: 'Upate Settings File', command: 'bg3-mod-helper.saveConfigToFile' }
{ label: 'Update Settings File', command: 'bg3-mod-helper.saveConfigToFile' }
]);
} else if (element.id === 'conversion') {
return Promise.resolve([
Expand Down
11 changes: 4 additions & 7 deletions support_files/conversion_junction.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function getDynamicPath(filePath) {


// at the moment this has all the functionality i planned for it, ie lsf, loca, and paks. for any other conversions we can make separate functions
async function convert(convertPath, targetExt = path.extname(getDynamicPath(convertPath)), modName = getConfig.getModName, zipCheck = getConfig.zipOnPack) {
async function convert(convertPath, targetExt = path.extname(getDynamicPath(convertPath)), modName = getConfig.getModName) {
let rootModPath = getConfig.rootModPath;

// checks if the convertPath was undefined and halts the function before it goes any further
Expand All @@ -163,34 +163,31 @@ async function convert(convertPath, targetExt = path.extname(getDynamicPath(conv
await convert(rootModPath, lsx)
.then(() => bg3mh_logger.info(`lsx conversion done`, false));

processPak(rootModPath, modName, '', zipCheck);
processPak(rootModPath);
}
// has a check for main thread here because when a worker thread calls this function, it's batch unpacking and has a specific place it needs the files inside to go, but can't rely on vscode module functions to get them
else if (fs.statSync(convertPath).isFile()) {
if (isMainThread) {
processPak(convertPath, modName);
processPak(convertPath);
} else {
processPak(convertPath, modName, workerData.jobDestPath);
processPak(convertPath, workerData.jobDestPath);
}

}
}
// this function works best on single files, so we need to process that array and re-run this function with each of its elements
else if (Array.isArray(convertPath)) {
// console.log('array1')
for (let i = 0; i < convertPath.length; i++) {
convert(convertPath[i], path.extname(convertPath[i]));
}
}
// if this function is passsed a directory, parse it into an array of files of the specified type in targetExt, then send that back through
else if (fs.statSync(convertPath).isDirectory()) {
// console.log('plz1')
const filesToConvert = await FIND_FILES(targetExt);
convert(filesToConvert);
}
// finally, if this function is handed a file, convert that bitch :catyes:
else if (fs.statSync(convertPath).isFile()) {
// console.log('plz2')
if (isLoca(targetExt)) {
try {
processLoca(convertPath, targetExt);
Expand Down
14 changes: 12 additions & 2 deletions support_files/loca_convert.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const path = require('path');

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

const { getFormats, baseNamePath, LOAD_LSLIB } = require('./lslib_utils');
const { CREATE_LOGGER } = require('./log_utils');
const { CREATE_LOGGER, raiseInfo } = require('./log_utils');
const bg3mh_logger = CREATE_LOGGER();

const { xml, loca } = getFormats();
Expand Down Expand Up @@ -54,7 +56,15 @@ async function processLoca(file, targetExt) {
temp_loca = LocaUtils.Load(file);

LocaUtils.Save(temp_loca, file_output);
bg3mh_logger.info(`Exported ${to_loca} file: ${file_output}`)

if (isMainThread) {
const vscode = require('vscode');
vscode.window.showInformationMessage(`Exported ${to_loca} file: ${file_output}`);
} else {
bg3mh_logger.info(`Exported ${to_loca} file: ${file_output}`);
}


}
catch (Error) {
bg3mh_logger.error(Error);
Expand Down
21 changes: 15 additions & 6 deletions support_files/lsf_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const path = require('path');
const fs = require('fs');

const { getFormats, baseNamePath, LOAD_LSLIB } = require('./lslib_utils');

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

const { lsb, lsf, lsj, lsfx, lsbc, lsbs, lsx } = getFormats();
const lsfFormats = [lsb, lsf, lsj, lsfx, lsbc, lsbs, lsx];

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

var to_lsf;
Expand Down Expand Up @@ -110,11 +110,20 @@ async function processLsf(file, targetExt) {
try {
temp_lsf = ResourceUtils.LoadResource(file, load_params);
ResourceUtils.SaveResource(temp_lsf, file_output, conversion_params);

bg3mh_logger.info(`Exported ${to_lsf} file: ${file_output}`)

if (isMainThread) {
const vscode = require('vscode');
vscode.window.showInformationMessage(`Exported ${to_lsf} file: ${file_output}`);
} else {
bg3mh_logger.info(`Exported ${to_lsf} file: ${file_output}`);
}
}
catch (Error) {
bg3mh_logger.error(Error);
catch (Error) {
if (isMainThread) {
vscode.window.showErrorMessage(Error);
} else {
bg3mh_logger.info(Error);
}
}
}

Expand Down
48 changes: 28 additions & 20 deletions support_files/lslib_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const hotfixPatchRegex = /Patch[\d]+_Hotfix[\d]+/;
// tools to test where the process is
const { isMainThread, workerData } = require('node:worker_threads');

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

Expand Down Expand Up @@ -174,20 +173,25 @@ function FIND_FILES_SYNC(filesPath, targetExt = getFormats().lsf, isRecursive =
// beautiful. still needs dll handling in lslib_utils though, and some refactoring
async function FIND_FILES(targetExt = getFormats().lsf, filesPath = '**/*') {
let filesList;
// these don't do anything... yet
let globToSearch;
let nonRecursiveGlob = '*';
let recursiveGlob = '**/*'

// pak logic may not be needed here, will check at some point
if (targetExt === getFormats().dll || targetExt === getFormats().pak) {
// finding dlls needs to not be recursive so we don't accidentally load things twice
if (targetExt === getFormats().dll) {
globToSearch = nonRecursiveGlob;
}
// paks can be recursive, but we need to account for them not being in the workspace, like dlls
else if (targetExt === getFormats().pak) {
globToSearch = recursiveGlob;
}

// finding dlls needs to not be recursive so we don't accidentally load things twice
if (targetExt === getFormats().dll) {
let dllDir = new vscode.RelativePattern(filesPath, '*' + targetExt);
filesList = (await findFiles(dllDir)).map(file => dirSeparator(file.path));
}
// paks can be recursive, but we need to account for them not being in the workspace, like dlls
else if (targetExt === getFormats().pak) {
let dllDir = new vscode.RelativePattern(filesPath, '**/*' + targetExt);
filesList = (await findFiles(dllDir)).map(file => dirSeparator(file.path));
}
else {
let fileDir = new vscode.RelativePattern(filesPath, globToSearch + targetExt);
filesList = (await findFiles(fileDir)).map(file => dirSeparator(file.path));
} else {
filesList = (await findFiles(filesPath + targetExt)).map(file => dirSeparator(file.path));
}

Expand Down Expand Up @@ -228,9 +232,12 @@ function FILTER_PATHS(filesPath) {
return filesPath;
}
// if paks are being grabbed, it's for unpacking, so exclude the ones that throw errors
else if (temp_ext === getFormats().pak && !(virtualTextureRegex.test(temp_name) || hotfixPatchRegex.test(temp_name))) {
else if (
temp_ext === getFormats().pak &&
!(virtualTextureRegex.test(temp_name) ||
hotfixPatchRegex.test(temp_name))
) {
return filesPath;

}
// check if an item is excluded by user, in a path that should be converted, or can be ignored
else if (
Expand All @@ -248,37 +255,38 @@ 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, bg3mh_logger) {
function moveFileAcrossDevices(sourcePath, destPath, logger = bg3mh_logger) {
let infoMsg = `${path.basename(sourcePath)} moved to ${destPath}.`;

fs.readFile(sourcePath, (readErr, data) => {
if (readErr) {
bg3mh_logger.error(readErr);
logger.error(readErr);
return;
}
fs.writeFile(destPath, data, (writeErr) => {
if (writeErr) {
bg3mh_logger.error(writeErr);
logger.error(writeErr);
return;
}
fs.unlink(sourcePath, unlinkErr => {
// added the check because it was raising an error every time the func was called
if (unlinkErr) {
bg3mh_logger.error(unlinkErr);
logger.error(unlinkErr);
return;
}
});
});
});

if (isMainThread) {
bg3mh_logger.info(infoMsg, false);
logger.info(infoMsg, false);
vscode.window.showInformationMessage(infoMsg);
} else {
bg3mh_logger.info(infoMsg, false);
logger.info(infoMsg, false);
}
}

// probably not needed but i'll investigate later
// i don't like putting this here but i need a worker_thread friendly version
function getModNameSync() {
let rootModPath;
Expand Down
Loading

0 comments on commit 73e926b

Please sign in to comment.