From 26af82cef5906cfdb806c5ce20e9c047cf0a67e8 Mon Sep 17 00:00:00 2001 From: thekingofspace <37231416+thekingofspace@users.noreply.github.com> Date: Thu, 29 Feb 2024 20:24:13 -0800 Subject: [PATCH] Added JSON data Enjoy! --- actions/check_content_title_MOD.js | 67 ++++++++++++++++++ actions/check_title_MOD.js | 61 ++++++++++++++++ actions/read_json_data_MOD.js | 88 +++++++++++++++++++++++ actions/write_json_data_MOD.js | 110 +++++++++++++++++++++++++++++ 4 files changed, 326 insertions(+) create mode 100644 actions/check_content_title_MOD.js create mode 100644 actions/check_title_MOD.js create mode 100644 actions/read_json_data_MOD.js create mode 100644 actions/write_json_data_MOD.js diff --git a/actions/check_content_title_MOD.js b/actions/check_content_title_MOD.js new file mode 100644 index 00000000..03b45956 --- /dev/null +++ b/actions/check_content_title_MOD.js @@ -0,0 +1,67 @@ +module.exports = { + name: 'Check if JSON Title Exists', + section: 'Json Data', + meta: { + version: '2.1.8', + author: 'DBM Mods', + }, + + subtitle(data) { + return `Check if JSON File Title "${data.title}" exists in File "${data.filePath}"`; + }, + + fields: ['filePath', 'title', 'branch'], + + html(isEvent, data) { + return ` +
+ JSON File Path + +
+ +


+ +
+ Title to Check + +
+ +



+ +
+ +
+ +`; + }, + + init() {}, + + action(cache) { + const data = cache.actions[cache.index]; + const filePath = this.evalMessage(data.filePath, cache); + const titleToCheck = this.evalMessage(data.title, cache); + + let jsonData; + try { + jsonData = require(filePath); + } catch (error) { + console.error(`Error reading JSON file: ${error.message}`); + } + + let result = false; + + if (jsonData && jsonData instanceof Object) { + result = jsonData.hasOwnProperty(titleToCheck); + } + + this.executeResults(result, data.branch ?? data, cache); + }, + + modInit(data) { + this.prepareActions(data.branch?.iftrueActions); + this.prepareActions(data.branch?.iffalseActions); + }, + + mod() {}, +}; \ No newline at end of file diff --git a/actions/check_title_MOD.js b/actions/check_title_MOD.js new file mode 100644 index 00000000..d867efb7 --- /dev/null +++ b/actions/check_title_MOD.js @@ -0,0 +1,61 @@ +module.exports = { + name: 'Check if JSON Title Exists', + section: 'Json Data', + meta: { + version: '2.1.8', + author: 'DBM Mods', + }, + + subtitle(data) { + return `Check if JSON Title "${data.title}" exists in File Path "${data.filePath}"`; + }, + + fields: ['filePath', 'title', 'branch'], + + html(isEvent, data) { + return ` +
+ File Path + +
+ +


+ +
+ Title to Check + +
+ +


+ +
+ +
+ +`; + }, + + init() {}, + + action(cache) { + const data = cache.actions[cache.index]; + const filePath = this.evalMessage(data.filePath, cache); + const titleToCheck = this.evalMessage(data.title, cache); + let result = false; + + try { + const fileContent = fs.readFileSync(filePath, 'utf8'); + const jsonData = JSON.parse(fileContent); + + if (jsonData && jsonData instanceof Object) { + result = jsonData.hasOwnProperty(titleToCheck); + } + } catch (error) { + console.error(`Error reading or parsing JSON file: ${error}`); + } + + this.executeResults(result, data.branch ?? data, cache); + }, + + mod() {}, +}; \ No newline at end of file diff --git a/actions/read_json_data_MOD.js b/actions/read_json_data_MOD.js new file mode 100644 index 00000000..268cca0e --- /dev/null +++ b/actions/read_json_data_MOD.js @@ -0,0 +1,88 @@ +module.exports = { + name: 'Read JSON Data', + section: 'Json Data', + meta: { + version: '2.1.8', + author: 'Raikin', + }, + + subtitle(data) { + return `Read JSON Data from "${data.filename}"`; + }, + + variableStorage(data, varType) { + if (parseInt(data.storage, 10) !== varType) return; + return [data.varName, 'String']; + }, + + fields: ['filename', 'title', 'contentTitle', 'storage', 'varName'], + + html() { + return ` +
+ File Path + +
+

+ +
+ Title + +
+
+ +
+ Content Title + +
+
+ +
+ +
+`; + }, + + async action(cache) { + const data = cache.actions[cache.index]; + const { readFileSync, existsSync } = require('fs'); + const path = this.evalMessage(data.filename, cache); + const title = this.evalMessage(data.title, cache); + const contentTitle = this.evalMessage(data.contentTitle, cache); + const varName = this.evalMessage(data.varName, cache); + + try { + if (path) { + let jsonData = []; + + if (existsSync(path)) { + const existingContent = readFileSync(path, 'utf8'); + + try { + jsonData = JSON.parse(existingContent); + } catch (err) { + console.error(`Error parsing existing JSON data: ${err}`); + } + } + + const entry = jsonData.find((item) => item.Title === title); + + if (entry && entry[contentTitle] !== undefined) { + const content = entry[contentTitle]; + this.storeValue(content, parseInt(data.storage, 10), varName, cache); + } else { + console.warn(`Content not found for title "${title}" and content title "${contentTitle}"`); + this.storeValue('', parseInt(data.storage, 10), varName, cache); + } + } else { + console.log('File path is missing from Read JSON Data action!'); + } + } catch (err) { + console.error(`ERROR! ${err.stack || err}`); + } + + this.callNextAction(cache); + }, + + mod() {}, +}; \ No newline at end of file diff --git a/actions/write_json_data_MOD.js b/actions/write_json_data_MOD.js new file mode 100644 index 00000000..29fe0aee --- /dev/null +++ b/actions/write_json_data_MOD.js @@ -0,0 +1,110 @@ +module.exports = { + name: 'Write JSON Data', + section: 'Json Data', + meta: { + version: '2.1.8', + author: 'DBM Mods', + }, + + subtitle(data) { + return `Write JSON Data to "${data.filename}"`; + }, + + fields: ['filename', 'title', 'contentTitle', 'contentText'], + + html() { + return ` +
+ File Path + +
+

+ +
+ Title + +
+
+ +
+ Content Title + +
+
+ +
+ Content Text + +
+`; + }, + + async action(cache) { + const data = cache.actions[cache.index]; + const { writeFileSync, readFileSync, existsSync } = require('fs'); + const path = this.evalMessage(data.filename, cache); + const title = this.evalMessage(data.title, cache); + const contentTitle = this.evalMessage(data.contentTitle, cache); + const contentText = this.evalMessage(data.contentText, cache); + + try { + if (path) { + let jsonData = []; + + // Check if the file already exists + if (existsSync(path)) { + // Read existing content + const existingContent = readFileSync(path, 'utf8'); + + try { + // Parse existing JSON data + jsonData = JSON.parse(existingContent); + } catch (err) { + console.error(`Error parsing existing JSON data: ${err}`); + } + } + + // Find the entry with the specified title + const entry = jsonData.find((item) => item.Title === title); + + // If the contentTitle already exists, replace it + if (contentTitle.includes('/')) { + const nestedTitles = contentTitle.split('/'); + let currentContent = entry || jsonData; + + for (let i = 0; i < nestedTitles.length; i++) { + const nestedTitle = nestedTitles[i]; + + if (i === nestedTitles.length - 1) { + currentContent[nestedTitle] = contentText; + } else { + currentContent[nestedTitle] = currentContent[nestedTitle] || {}; + currentContent = currentContent[nestedTitle]; + } + } + } else { + // If the title doesn't exist, create a new entry + const newEntry = { + Title: title, + }; + + newEntry[contentTitle] = contentText; + + // Push the new entry to JSON data + jsonData.push(newEntry); + } + + // Write the updated JSON data back to the file + writeFileSync(path, JSON.stringify(jsonData, null, 2), 'utf8'); + } else { + console.log('File path is missing from Write JSON Data action!'); + } + } catch (err) { + console.error(`ERROR! ${err.stack || err}`); + } + + this.callNextAction(cache); + }, + + mod() {}, +}; \ No newline at end of file