From 8ffe6b656666117abc813d3d0c661566b4efd8ff Mon Sep 17 00:00:00 2001 From: Salvatore Santagati Date: Fri, 16 Jul 2021 11:00:18 +0200 Subject: [PATCH] feat: added add scene on theme Now is possible add a single scene to a OBS theme with all files --- README.md | 26 ++++++++++-- otu.js | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- 3 files changed, 140 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 34f0f5a..dee6493 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ **otu** ( OBS themes utility ) is [Node.js](https://nodejs.org) script, an handle tool for work with your scenes of Open Broadcaster Software # Description -otu is an utility to permit to export/import your theme with all files, and many features are added in the next release +otu is an utility to permit to export/import your theme with all files in an archive, add and extract scene from theme, and many features are added in the future # Installation ```bash @@ -11,19 +11,37 @@ npm i otu-tool # Features * Export OBS theme scene * Import OBS theme scene +* Add single scene on OBS theme +* Extract single scene from OBS theme # Usage + ### Export Create a zip archive (.otu) from scene.json with all files present on scenes ```bash -otu-tool export -i myscene.json -o myscenearchive.otu +otu-tool export -i mytheme.json -o mythemearchive.otu ``` ### Import Extract files from zip archive (.otu) and create scene.json for OBS + +```bash +otu-tool import -i mythemearchive.otu -o mytheme.json +``` + +### Add +Add a single scene to your theme ```bash -otu-tool import -i myscenearchive.otu -o myscene.json +otu-tool add -i mytheme.json --scene myscenearchive.otu -o mynewtheme.json ``` + +### Extract +Extract single scene from theme +```bash +otu-tool extract -i mytheme.json --scene "NameOfScene" -o myscenearchive.otu +``` + + # Supported Broadcaster Sofware * [OBS Studio](https://obsproject.com/) @@ -32,7 +50,7 @@ otu-tool import -i myscenearchive.otu -o myscene.json # Todo - [ ] Check fonts ~~and install it if is required~~ -- [ ] ~~Extract~~/Add single scene of your theme with relative files +- [x] ~~Extract/Add single scene of your theme with relative files~~ - [ ] Convert [Streamlabs OBS](https://streamlabs.com/streamlabs-obs-live-streaming-software) scenes to [OBS Broadcaster Software](https://obsproject.com/) # License diff --git a/otu.js b/otu.js index 4198aba..b289e74 100644 --- a/otu.js +++ b/otu.js @@ -94,11 +94,126 @@ yargs(hideBin(process.argv)) return false; } }) + .command('add [scene] [input] [output]', 'add scene to theme', { + input: { + description: 'insert path of theme want to add scene', + alias: 'i', + demandOption: true, + type: 'string', + requiresArg: true, + }, + output: { + description: 'insert path where save new theme', + alias: 'o', + type: 'string', + requiresArg: true + }, + scene: { + description: 'insert path of scene want add', + alias: 's', + type: 'string', + requiresArg: true + } + }, + (argv) => { + if (fs.existsSync(argv.input)) { + addScene(argv.input, argv.output, argv.scene, argv); + } else { + console.log(argv.input + "file doesn't exist") + return false; + } + }) .help() .alias('help', 'h') .alias('version', 'v') .argv; +/** + * @description addScene add scene to theme + * @param {string} fsource - source file + * @param {string} fdest - destination file + * @param {string} sceneName - name of scene archive want add + * @param {Object} options - Options + */ + +function addScene(fsource, fdest, sceneArchive, options) { + + fs.readFile(fsource, 'utf8', (err, data) => { + const zip = new Archive(sceneArchive); + const scene = JSON.parse(zip.getEntry('scene.json').getData().toString("utf8")); + const theme = JSON.parse(data); + const currentDir = path.dirname(fdest); + const fontDir = process.env.LOCALAPPDATA + '\\Microsoft\\Windows\\Fonts\\'; + + theme.scene_order.push({ + "name": theme.name + }); + + scene.sources.forEach(src => { + + if (typeof(src.settings.files) !== 'undefined') { + src.settings.files.forEach((item, index) => { + if ((fs.statSync(item.value).isFile()) && (zip.getEntry('files/' + path.win32.basename((item.value))))) { + zip.extractEntryTo("files/" + path.win32.basename(item.value), currentDir, true, true); + } else zip.extractEntryTo("files/" + path.win32.basename(item.value) + "/", currentDir, true, true); + + src.settings.files[index].value = path.resolve(currentDir, 'files', path.win32.basename(src.settings.files[index].value)); + }) + } + + if ((typeof(src.settings.file) !== 'undefined') && (zip.getEntry('files/' + path.win32.basename(src.settings.file)))) { + src.settings.file = path.resolve(currentDir, 'files', path.win32.basename(src.settings.file)); + zip.extractEntryTo('files/' + path.win32.basename(src.settings.file), currentDir, true, true); + } + + if ((typeof(src.settings.custom_font) !== 'undefined') && (zip.getEntry('files/' + path.win32.basename(src.settings.custom_font)))) { + let fontPath = fontDir + path.win32.basename(src.settings.custom_font); + src.settings.custom_font = fontPath; + + if (!fs.existsSync(fontPath)) { + zip.extractEntryTo('files/' + path.win32.basename(src.settings.custom_font), fontDir, false, false); + + let font = openType.loadSync(fontPath); + let fontFamilyName = font["tables"]["name"]["fontFamily"]["en"] + ' (TrueType)'; + let strReg = 'reg add "HKCU\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts" /v ' + '"' + fontFamilyName + '"' + ' /t REG_SZ /d ' + '"' + fontPath + '"' + ' /f'; + + const result = exec(strReg, (error, stdout, stderr) => { + + if (error) { + console.error(`error: ${error.message}`); + return; + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + return; + } + + console.log(`stdout:\n${stdout}`); + }) + } else console.log("File exist : " + fontPath); + + + } + + if (typeof(src.settings.local_file) !== 'undefined') { + src.settings.local_file = path.resolve(currentDir, 'files', path.win32.basename(src.settings.local_file)); + zip.extractEntryTo("files/" + path.win32.basename(src.settings.local_file), currentDir, true, true); + } + + theme.sources.push(src); + }); + + fdest = fdest || theme.name; + fs.writeFile(fdest + '.json', JSON.stringify(theme), function(err) { + if (err) return console.log(err); + console.log('Completed!!! ==> ' + fdest + '.json'); + return; + }); + + }); + +} /** * @description checkItemsRequired check all items required from scene * @param {Object} theme - full source of original theme @@ -180,6 +295,7 @@ function extractScene(fsource, fdest, sceneName, options) { } + fdest = fdest || scene.name; zip.addFile("scene.json", Buffer.from(JSON.stringify(scene), "utf8")); zip.writeZip(fdest + ".otu"); console.log('Completed!!! ' + fdest + '.otu'); diff --git a/package.json b/package.json index cd37e89..8aedad3 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "keywords": [ "OBS", "utility", - "tool" + "tool", + "cli" ], "author": "Salvatore Santagati info@salsan.dev", "license": "GPL-3.0-or-later",