|
| 1 | +/* |
| 2 | +========================================================= |
| 3 | +Name : MetaScriptToolbox |
| 4 | +GitHub : |
| 5 | +Roll20 Contact : timmaugh |
| 6 | +Version : 0.0.2 |
| 7 | +Last Update : 6 SEP 2024 |
| 8 | +========================================================= |
| 9 | +*/ |
| 10 | +var API_Meta = API_Meta || {}; |
| 11 | +API_Meta.MetaScriptToolbox = { offset: Number.MAX_SAFE_INTEGER, lineCount: -1 }; |
| 12 | +{ try { throw new Error(''); } catch (e) { API_Meta.MetaScriptToolbox.offset = (parseInt(e.stack.split(/\n/)[1].replace(/^.*:(\d+):.*$/, '$1'), 10) - (12)); } } |
| 13 | + |
| 14 | +const MetaScriptToolbox = (() => { // eslint-disable-line no-unused-vars |
| 15 | + const apiproject = 'MetaScriptToolbox'; |
| 16 | + const version = '0.0.2'; |
| 17 | + const schemaVersion = 0.1; |
| 18 | + API_Meta[apiproject].version = version; |
| 19 | + const vd = new Date(1725630209434); |
| 20 | + const versionInfo = () => { |
| 21 | + log(`\u0166\u0166 ${apiproject} v${API_Meta[apiproject].version}, ${vd.getFullYear()}/${vd.getMonth() + 1}/${vd.getDate()} \u0166\u0166 -- offset ${API_Meta[apiproject].offset}`); |
| 22 | + }; |
| 23 | + const logsig = () => { |
| 24 | + // initialize shared namespace for all signed projects, if needed |
| 25 | + state.torii = state.torii || {}; |
| 26 | + // initialize siglogged check, if needed |
| 27 | + state.torii.siglogged = state.torii.siglogged || false; |
| 28 | + state.torii.sigtime = state.torii.sigtime || Date.now() - 3001; |
| 29 | + if (!state.torii.siglogged || Date.now() - state.torii.sigtime > 3000) { |
| 30 | + const logsig = '\n' + |
| 31 | + ' _____________________________________________ ' + '\n' + |
| 32 | + ' )_________________________________________( ' + '\n' + |
| 33 | + ' )_____________________________________( ' + '\n' + |
| 34 | + ' ___| |_______________| |___ ' + '\n' + |
| 35 | + ' |___ _______________ ___| ' + '\n' + |
| 36 | + ' | | | | ' + '\n' + |
| 37 | + ' | | | | ' + '\n' + |
| 38 | + ' | | | | ' + '\n' + |
| 39 | + ' | | | | ' + '\n' + |
| 40 | + ' | | | | ' + '\n' + |
| 41 | + '______________|_|_______________|_|_______________' + '\n' + |
| 42 | + ' ' + '\n'; |
| 43 | + log(`${logsig}`); |
| 44 | + state.torii.siglogged = true; |
| 45 | + state.torii.sigtime = Date.now(); |
| 46 | + } |
| 47 | + return; |
| 48 | + }; |
| 49 | + const checkInstall = () => { |
| 50 | + if (!state.hasOwnProperty(apiproject) || state[apiproject].version !== schemaVersion) { |
| 51 | + log(` > Updating ${apiproject} Schema to v${schemaVersion} <`); |
| 52 | + switch (state[apiproject] && state[apiproject].version) { |
| 53 | + |
| 54 | + case 0.1: |
| 55 | + /* falls through */ |
| 56 | + |
| 57 | + case 'UpdateSchemaVersion': |
| 58 | + state[apiproject].version = schemaVersion; |
| 59 | + break; |
| 60 | + |
| 61 | + default: |
| 62 | + state[apiproject] = { |
| 63 | + settings: {}, |
| 64 | + defaults: {}, |
| 65 | + version: schemaVersion |
| 66 | + } |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + }; |
| 71 | + let stateReady = false; |
| 72 | + const assureState = () => { |
| 73 | + if (!stateReady) { |
| 74 | + checkInstall(); |
| 75 | + stateReady = true; |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + const checkDependencies = (deps) => { |
| 80 | + /* pass array of objects like |
| 81 | + { name: 'ModName', version: '#.#.#' || '', mod: ModName || undefined, checks: [ [ExposedItem, type], [ExposedItem, type] ] } |
| 82 | + */ |
| 83 | + const dependencyEngine = (deps) => { |
| 84 | + const versionCheck = (mv, rv) => { |
| 85 | + let modv = [...mv.split('.'), ...Array(4).fill(0)].slice(0, 4); |
| 86 | + let reqv = [...rv.split('.'), ...Array(4).fill(0)].slice(0, 4); |
| 87 | + return reqv.reduce((m, v, i) => { |
| 88 | + if (m.pass || m.fail) return m; |
| 89 | + if (i < 3) { |
| 90 | + if (parseInt(modv[i]) > parseInt(reqv[i])) m.pass = true; |
| 91 | + else if (parseInt(modv[i]) < parseInt(reqv[i])) m.fail = true; |
| 92 | + } else { |
| 93 | + // all betas are considered below the release they are attached to |
| 94 | + if (reqv[i] === 0 && modv[i] === 0) m.pass = true; |
| 95 | + else if (modv[i] === 0) m.pass = true; |
| 96 | + else if (reqv[i] === 0) m.fail = true; |
| 97 | + else if (parseInt(modv[i].slice(1)) >= parseInt(reqv[i].slice(1))) m.pass = true; |
| 98 | + } |
| 99 | + return m; |
| 100 | + }, { pass: false, fail: false }).pass; |
| 101 | + }; |
| 102 | + |
| 103 | + let result = { passed: true, failures: {}, optfailures: {} }; |
| 104 | + deps.forEach(d => { |
| 105 | + let failObj = d.optional ? result.optfailures : result.failures; |
| 106 | + if (!d.mod) { |
| 107 | + if (!d.optional) result.passed = false; |
| 108 | + failObj[d.name] = 'Not found'; |
| 109 | + return; |
| 110 | + } |
| 111 | + if (d.version && d.version.length) { |
| 112 | + if (!(API_Meta[d.name].version && API_Meta[d.name].version.length && versionCheck(API_Meta[d.name].version, d.version))) { |
| 113 | + if (!d.optional) result.passed = false; |
| 114 | + failObj[d.name] = `Incorrect version. Required v${d.version}. ${API_Meta[d.name].version && API_Meta[d.name].version.length ? `Found v${API_Meta[d.name].version}` : 'Unable to tell version of current.'}`; |
| 115 | + return; |
| 116 | + } |
| 117 | + } |
| 118 | + d.checks.reduce((m, c) => { |
| 119 | + if (!m.passed) return m; |
| 120 | + let [pname, ptype] = c; |
| 121 | + if (!d.mod.hasOwnProperty(pname) || typeof d.mod[pname] !== ptype) { |
| 122 | + if (!d.optional) m.passed = false; |
| 123 | + failObj[d.name] = `Incorrect version.`; |
| 124 | + } |
| 125 | + return m; |
| 126 | + }, result); |
| 127 | + }); |
| 128 | + return result; |
| 129 | + }; |
| 130 | + let depCheck = dependencyEngine(deps); |
| 131 | + let failures = '', contents = '', msg = ''; |
| 132 | + if (Object.keys(depCheck.optfailures).length) { // optional components were missing |
| 133 | + failures = Object.keys(depCheck.optfailures).map(k => `• <code>${k}</code> : ${depCheck.optfailures[k]}`).join('<br>'); |
| 134 | + contents = `<span style="font-weight: bold">${apiproject}</span> utilizies one or more other scripts for optional features, and works best with those scripts installed. You can typically find these optional scripts in the 1-click Mod Library:<br>${failures}`; |
| 135 | + msg = `<div style="width: 100%;border: none;border-radius: 0px;min-height: 60px;display: block;text-align: left;white-space: pre-wrap;overflow: hidden"><div style="font-size: 14px;font-family: "Segoe UI", Roboto, Ubuntu, Cantarell, "Helvetica Neue", sans-serif"><div style="background-color: #000000;border-radius: 6px 6px 0px 0px;position: relative;border-width: 2px 2px 0px 2px;border-style: solid;border-color: black;"><div style="border-radius: 18px;width: 35px;height: 35px;position: absolute;left: 3px;top: 2px;"><img style="background-color: transparent ; float: left ; border: none ; max-height: 40px" src="${typeof apilogo !== 'undefined' ? apilogo : 'https://i.imgur.com/kxkuQFy.png'}"></div><div style="background-color: #c94d4d;font-weight: bold;font-size: 18px;line-height: 36px;border-radius: 6px 6px 0px 0px;padding: 4px 4px 0px 43px;color: #ffffff;min-height: 38px;">MISSING MOD DETECTED</div></div><div style="background-color: white;padding: 4px 8px;border: 2px solid #000000;border-bottom-style: none;color: #404040;">${contents}</div><div style="background-color: white;text-align: right;padding: 4px 8px;border: 2px solid #000000;border-top-style: none;border-radius: 0px 0px 6px 6px"></div></div></div>`; |
| 136 | + sendChat(apiproject, `/w gm ${msg}`); |
| 137 | + } |
| 138 | + if (!depCheck.passed) { |
| 139 | + failures = Object.keys(depCheck.failures).map(k => `• <code>${k}</code> : ${depCheck.failures[k]}`).join('<br>'); |
| 140 | + contents = `<span style="font-weight: bold">${apiproject}</span> requires other scripts to work. Please use the 1-click Mod Library to correct the listed problems:<br>${failures}`; |
| 141 | + msg = `<div style="width: 100%;border: none;border-radius: 0px;min-height: 60px;display: block;text-align: left;white-space: pre-wrap;overflow: hidden"><div style="font-size: 14px;font-family: "Segoe UI", Roboto, Ubuntu, Cantarell, "Helvetica Neue", sans-serif"><div style="background-color: #000000;border-radius: 6px 6px 0px 0px;position: relative;border-width: 2px 2px 0px 2px;border-style: solid;border-color: black;"><div style="border-radius: 18px;width: 35px;height: 35px;position: absolute;left: 3px;top: 2px;"><img style="background-color: transparent ; float: left ; border: none ; max-height: 40px" src="${typeof apilogo !== 'undefined' ? apilogo : 'https://i.imgur.com/kxkuQFy.png'}"></div><div style="background-color: #c94d4d;font-weight: bold;font-size: 18px;line-height: 36px;border-radius: 6px 6px 0px 0px;padding: 4px 4px 0px 43px;color: #ffffff;min-height: 38px;">MISSING MOD DETECTED</div></div><div style="background-color: white;padding: 4px 8px;border: 2px solid #000000;border-bottom-style: none;color: #404040;">${contents}</div><div style="background-color: white;text-align: right;padding: 4px 8px;border: 2px solid #000000;border-top-style: none;border-radius: 0px 0px 6px 6px"></div></div></div>`; |
| 142 | + sendChat(apiproject, `/w gm ${msg}`); |
| 143 | + return false; |
| 144 | + } |
| 145 | + return true; |
| 146 | + }; |
| 147 | + |
| 148 | + on('ready', () => { |
| 149 | + versionInfo(); |
| 150 | + assureState(); |
| 151 | + logsig(); |
| 152 | + let reqs = [ |
| 153 | + { |
| 154 | + name: 'ZeroFrame', |
| 155 | + version: `1.2.2`, |
| 156 | + mod: typeof ZeroFrame !== 'undefined' ? ZeroFrame : undefined, |
| 157 | + checks: [['RegisterMetaOp', 'function']], |
| 158 | + }, |
| 159 | + { |
| 160 | + name: 'APILogic', |
| 161 | + version: `2.0.9`, |
| 162 | + mod: typeof APILogic !== 'undefined' ? APILogic : undefined, |
| 163 | + checks: [], |
| 164 | + }, |
| 165 | + { |
| 166 | + name: 'Fetch', |
| 167 | + version: `2.1.1`, |
| 168 | + mod: typeof Fetch !== 'undefined' ? Fetch : undefined, |
| 169 | + checks: [], |
| 170 | + }, |
| 171 | + { |
| 172 | + name: 'SelectManager', |
| 173 | + version: `1.1.8`, |
| 174 | + mod: typeof SelectManager !== 'undefined' ? SelectManager : undefined, |
| 175 | + checks: [['GetPlayerID', 'function'], ['GetSelected', 'function'], ['GetWho', 'function']], |
| 176 | + }, |
| 177 | + { |
| 178 | + name: 'Muler', |
| 179 | + version: `2.0.2`, |
| 180 | + mod: typeof Muler !== 'undefined' ? Muler : undefined, |
| 181 | + checks: [], |
| 182 | + }, |
| 183 | + { |
| 184 | + name: 'Plugger', |
| 185 | + version: `1.0.9`, |
| 186 | + mod: typeof Plugger !== 'undefined' ? Plugger : undefined, |
| 187 | + checks: [], |
| 188 | + }, |
| 189 | + { |
| 190 | + name: 'MathOps', |
| 191 | + version: `1.0.8`, |
| 192 | + mod: typeof MathOps !== 'undefined' ? MathOps : undefined, |
| 193 | + checks: [['MathProcessor', 'function']], |
| 194 | + }, |
| 195 | + { |
| 196 | + name: 'libTokenMarkers', |
| 197 | + version: `0.1.2`, |
| 198 | + mod: typeof libTokenMarkers !== 'undefined' ? libTokenMarkers : undefined, |
| 199 | + checks: [['getStatus', 'function'], ['getStatuses', 'function'], ['getOrderedList', 'function']] |
| 200 | + }, |
| 201 | + { |
| 202 | + name: 'libTable', |
| 203 | + version: `1.0.0`, |
| 204 | + mod: typeof libTable !== 'undefined' ? libTable : undefined, |
| 205 | + checks: [ |
| 206 | + ['getTable', 'function'], |
| 207 | + ['getTables', 'function'], |
| 208 | + ['getItems', 'function'], |
| 209 | + ['getItemsByIndex', 'function'], |
| 210 | + ['getItemsByName', 'function'], |
| 211 | + ['getItemsByWeight', 'function'], |
| 212 | + ['getItemsByWeightedIndex', 'function'] |
| 213 | + ] |
| 214 | + }, |
| 215 | + { |
| 216 | + name: 'checkLightLevel', |
| 217 | + // version: `1.0.0.b3`, |
| 218 | + mod: typeof checkLightLevel !== 'undefined' ? checkLightLevel : undefined, |
| 219 | + checks: [['isLitBy', 'function']], |
| 220 | + optional: true |
| 221 | + }, |
| 222 | + { |
| 223 | + name: 'Messenger', |
| 224 | + version: `1.0.1`, |
| 225 | + mod: typeof Messenger !== 'undefined' ? Messenger : undefined, |
| 226 | + checks: [['Button', 'function'], ['MsgBox', 'function'], ['HE', 'function'], ['Html', 'function'], ['Css', 'function']] |
| 227 | + } |
| 228 | + ]; |
| 229 | + if (!checkDependencies(reqs)) return; |
| 230 | + }); |
| 231 | + return {}; |
| 232 | +})(); |
| 233 | + |
| 234 | +{ try { throw new Error(''); } catch (e) { API_Meta.MetaScriptToolbox.lineCount = (parseInt(e.stack.split(/\n/)[1].replace(/^.*:(\d+):.*$/, '$1'), 10) - API_Meta.MetaScriptToolbox.offset); } } |
0 commit comments