From 54934ba18a26df24b1754bc83851e019277aa863 Mon Sep 17 00:00:00 2001 From: thekingofspace <37231416+thekingofspace@users.noreply.github.com> Date: Thu, 6 Jun 2024 16:06:08 -0700 Subject: [PATCH] Updated JSON actions and added new json action. (#1039) * Updated with 2 things Added create primitive mod Added seperator mod * Updated these with lint * Updated these with lint * Updated canvas_create_primitive_MOD.js * Added json_read_MOD added json_write_MOD.js * Added json_read_MOD added json_write_MOD.js * Added json_read_MOD added json_write_MOD.js * Added json_read_MOD added json_write_MOD.js * Fixed lint issue I hope * Fixed lint issue I hope * Fixed prettier issues Fixed linting issues inside json write Fixed issue with canvas paint not using cases * Added json random * **Changelog** * Fixed json_random_MOD.js meta data * Fixed json_read_MOD.js meta data * Added json_check_MOD.json * **Changelog** * Fixed json_random_MOD.js meta data * Fixed json_read_MOD.js meta data * Added json_check_MOD.json * **Changelog** * Fixed json_random_MOD.js meta data * Fixed json_read_MOD.js meta data * Added json_check_MOD.json * **Changelog** * Fixed json_random_MOD.js meta data * Fixed json_read_MOD.js meta data * Added json_check_MOD.json * **Changelog** * added nested check * **Changelog** * removed error handler * **Changelog** * removed error handler --- actions/json_check_MOD.js | 134 +++++++++++++++++++++++++++++++++++++ actions/json_random_MOD.js | 6 ++ actions/json_read_MOD.js | 6 ++ 3 files changed, 146 insertions(+) create mode 100644 actions/json_check_MOD.js diff --git a/actions/json_check_MOD.js b/actions/json_check_MOD.js new file mode 100644 index 00000000..bd2cad8d --- /dev/null +++ b/actions/json_check_MOD.js @@ -0,0 +1,134 @@ +const fs = require('fs'); +const path = require('path'); + +module.exports = { + name: 'Check JSON File', + + section: 'File Stuff', + + subtitle(data) { + return `Check JSON file "${data.filepath}"`; + }, + + meta: { + version: '2.1.7', + preciseCheck: false, + author: 'DBM Mods', + authorUrl: 'https://github.com/dbm-network/mods', + }, + + fields: ['filepath', 'checkType', 'title', 'contentTitle', 'branch'], + + html() { + return ` +
+ File Path + +
+
+ Check Type
+ +
+ + + `; + }, + + init() { + const { glob, document } = this; + + glob.onCheckTypeChanged = function handleCheckTypeChange(event) { + const titleSection = document.getElementById('titleSection'); + const contentTitleSection = document.getElementById('contentTitleSection'); + switch (event.value) { + case '0': + titleSection.style.display = 'none'; + contentTitleSection.style.display = 'none'; + break; + case '1': + titleSection.style.display = null; + contentTitleSection.style.display = 'none'; + break; + case '2': + titleSection.style.display = null; + contentTitleSection.style.display = null; + break; + } + }; + + glob.onCheckTypeChanged(document.getElementById('checkType')); + }, + + async action(cache) { + const data = cache.actions[cache.index]; + let filepath = this.evalMessage(data.filepath, cache); + const checkType = parseInt(data.checkType, 10); + const title = this.evalMessage(data.title, cache); + const contentTitle = this.evalMessage(data.contentTitle, cache); + + if (filepath.startsWith('./')) { + filepath = path.join(__dirname, '..', filepath.substring(2)); + } + + let jsonData; + + if (fs.existsSync(filepath)) { + try { + const fileData = fs.readFileSync(filepath); + if (fileData.length === 0) throw new Error('JSON file is empty.'); + jsonData = JSON.parse(fileData); + } catch (error) { + console.error(`Error reading JSON file: ${error}`); + this.executeResults(false, data.branch, cache); + return; + } + } else { + this.executeResults(false, data.branch, cache); + return; + } + + let result = false; + + try { + switch (checkType) { + case 0: + result = true; + break; + case 1: { + result = jsonData.some((item) => item.Title === title); + break; + } + case 2: { + const titleData = jsonData.find((item) => item.Title === title); + if (!titleData) throw new Error('Title not found'); + const nestedTitles = contentTitle.split('/'); + let nestedData = titleData; + for (const nestedTitle of nestedTitles) { + if (!(nestedTitle in nestedData)) { + throw new Error('Content title not found'); + } + nestedData = nestedData[nestedTitle]; + } + result = true; + break; + } + } + } catch (error) { + console.error(`Error checking JSON data: ${error}`); + } + + this.executeResults(result, data.branch, cache); + }, + + mod() {}, +}; diff --git a/actions/json_random_MOD.js b/actions/json_random_MOD.js index 38587f8e..df6f63cd 100644 --- a/actions/json_random_MOD.js +++ b/actions/json_random_MOD.js @@ -5,6 +5,12 @@ module.exports = { name: 'Pick Random JSON Item', section: 'File Stuff', fields: ['filepath', 'title', 'storage', 'varName'], + meta: { + version: '2.1.7', + preciseCheck: false, + author: 'DBM Mods', + authorUrl: 'https://github.com/dbm-network/mods', + }, subtitle(data) { return `Pick random item from JSON file "${data.filepath}"`; diff --git a/actions/json_read_MOD.js b/actions/json_read_MOD.js index dd85f0f0..c0c21207 100644 --- a/actions/json_read_MOD.js +++ b/actions/json_read_MOD.js @@ -5,6 +5,12 @@ module.exports = { name: 'Read JSON File', section: 'File Stuff', fields: ['filepath', 'title', 'contentTitle', 'storage', 'varName'], + meta: { + version: '2.1.7', + preciseCheck: false, + author: 'DBM Mods', + authorUrl: 'https://github.com/dbm-network/mods', + }, subtitle(data) { return `Read JSON file "${data.filepath}"`;