From cfe3c7aa9d5f9014819726f9bdda96c0b45da1e8 Mon Sep 17 00:00:00 2001 From: Heath123 Date: Thu, 24 Dec 2020 12:51:44 +0000 Subject: [PATCH] Migrate fully to electron-store and seperate filtering settings across versions --- html/mainPage/js/defaults.json | 14 +++++++ html/mainPage/js/main.js | 73 ++++++++++++++++++++++------------ src/proxy/bedrock/proxy.js | 3 +- src/proxy/java/proxy.js | 7 +++- 4 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 html/mainPage/js/defaults.json diff --git a/html/mainPage/js/defaults.json b/html/mainPage/js/defaults.json new file mode 100644 index 0000000..832d96a --- /dev/null +++ b/html/mainPage/js/defaults.json @@ -0,0 +1,14 @@ +{ + "^bedrock-proxypass-json$": { + "hiddenPackets": { + "serverbound": ["move_player", "player_action", "level_sound_event"], + "clientbound": ["level_chunk", "update_block", "network_chunk_publisher_update", "set_entity_motion", "move_entity_absolute", "set_entity_data", "level_sound_event"] + } + }, + "^java-node-minecraft-protocol-.*$": { + "hiddenPackets": { + "serverbound": ["position", "position_look", "look", "keep_alive", "entity_action"], + "clientbound": ["keep_alive", "update_time", "rel_entity_move", "entity_teleport", "map_chunk", "update_light", "update_view_position", "entity_metadata", "entity_update_attributes", "unload_chunk", "entity_velocity", "entity_move_look", "entity_head_rotation"] + } + } +} \ No newline at end of file diff --git a/html/mainPage/js/main.js b/html/mainPage/js/main.js index 52d691b..46f0f3d 100644 --- a/html/mainPage/js/main.js +++ b/html/mainPage/js/main.js @@ -1,19 +1,8 @@ /* global Split, jsonTree, escapeHtml, alert, CodeMirror */ -/* -box.onclick = function() { - if (!box.checked) { - return - } - if (window.wasIn) { - window.wasIn = false; - } else { - box.checked = false; - box.indeterminate = true; - window.wasIn = true; - } -} -*/ +const Store = require('electron-store'); + +const store = new Store(); const axios = require('axios') @@ -113,15 +102,50 @@ const sharedVars = { proxyCapabilities: {}, ipcRenderer: require('electron').ipcRenderer, packetList: document.getElementById('packetlist'), - hiddenPackets: localStorage.getItem('hiddenPackets') - ? JSON.parse(localStorage.getItem('hiddenPackets')) - : Object.assign({}, defaultHiddenPackets), + hiddenPackets: undefined, scripting: undefined, lastFilter: '' } sharedVars.proxyCapabilities = JSON.parse(sharedVars.ipcRenderer.sendSync('proxyCapabilities', '')) +function getVersionSpecificVar(name, defaultValue) { + const versionId = 'version-' + sharedVars.proxyCapabilities.versionId + const settingsObject = store.get(versionId) + if (settingsObject) { + if (!settingsObject[name]) { + settingsObject[name] = JSON.stringify(defaultValue) + store.set(versionId, settingsObject) + } + } else { + store.set(versionId, { + [name]: JSON.stringify(defaultValue) + }) + } + return JSON.parse(store.get(versionId)[name]) +} + +function setVersionSpecificVar(name, value) { + const versionId = 'version-' + sharedVars.proxyCapabilities.versionId + const settingsObject = store.get(versionId) + settingsObject[name] = JSON.stringify(value) + store.set(versionId, settingsObject) +} + +const defaultsJson = require('./js/defaults.json') + +function findDefault(setting) { + const versionId = sharedVars.proxyCapabilities.versionId + for (const key in defaultsJson) { + const regex = new RegExp(key) + if (versionId.match(regex)) { + return defaultsJson[key][setting] + } + } +} + +sharedVars.hiddenPackets = getVersionSpecificVar('hiddenPackets', findDefault('hiddenPackets')) + if (!sharedVars.proxyCapabilities.scriptingSupport) { document.getElementById('scriptingTab').style.display = 'none' } @@ -147,7 +171,7 @@ sharedVars.ipcHandler.setup(sharedVars) const filteringPackets = document.getElementById('filtering-packets') function updateFilteringStorage () { - localStorage.setItem('hiddenPackets', JSON.stringify(sharedVars.hiddenPackets)) + setVersionSpecificVar('hiddenPackets', JSON.stringify(sharedVars.hiddenPackets)) } function updateFilteringTab () { @@ -263,7 +287,9 @@ sharedVars.ipcRenderer.on('editAndResend', (event, arg) => { // Context menu }) function deselectPacket () { - removeOrAddSelection(currentPacket, false) + if (currentPacket) { + removeOrAddSelection(currentPacket, false) + } currentPacket = undefined currentPacketType = undefined sharedVars.packetDom.getTreeElement().firstElementChild.innerHTML = 'No packet selected!' @@ -288,13 +314,13 @@ const hexViewer = document.getElementById('hex-viewer') function removeOrAddSelection (id, add) { const fakeElement = document.createElement('div') - fakeElement.innerHTML = sharedVars.allPacketsHTML[currentPacket][0] + fakeElement.innerHTML = sharedVars.allPacketsHTML[id][0] if (add) { fakeElement.firstChild.classList.add('selected') } else { fakeElement.firstChild.classList.remove('selected') } - sharedVars.allPacketsHTML[currentPacket] = [fakeElement.innerHTML] + sharedVars.allPacketsHTML[id] = [fakeElement.innerHTML] wrappedClusterizeUpdate(sharedVars.allPacketsHTML) clusterize.refresh() @@ -303,11 +329,6 @@ function removeOrAddSelection (id, add) { window.packetClick = function (id) { // window. stops standardjs from complaining // Remove selection background from old selected packet if (currentPacket) { - /* const previousPacket = document.getElementById('packet' + currentPacket) - // May not be in view - if (previousPacket) { - document.getElementById('packet' + currentPacket).classList.remove('selected') - } */ removeOrAddSelection(currentPacket, false) } diff --git a/src/proxy/bedrock/proxy.js b/src/proxy/bedrock/proxy.js index b1f499d..93941ab 100644 --- a/src/proxy/bedrock/proxy.js +++ b/src/proxy/bedrock/proxy.js @@ -20,7 +20,8 @@ exports.capabilities = { scriptingSupport: false, clientboundPackets: {}, serverboundPackets: {}, - wikiVgPage: 'https://wiki.vg/Bedrock_Protocol' + wikiVgPage: 'https://wiki.vg/Bedrock_Protocol', + versionId: 'bedrock-proxypass-json' } exports.startProxy = function (host, port, listenPort, version, authConsent, callback, dataFolder) { diff --git a/src/proxy/java/proxy.js b/src/proxy/java/proxy.js index ed7ab1d..a140fad 100644 --- a/src/proxy/java/proxy.js +++ b/src/proxy/java/proxy.js @@ -19,11 +19,16 @@ exports.capabilities = { clientboundPackets: [], serverboundPackets: [], // TODO: Only for latest, or fetch older pages - wikiVgPage: 'https://wiki.vg/Protocol' + wikiVgPage: 'https://wiki.vg/Protocol', + versionId: undefined } exports.startProxy = function (host, port, listenPort, version, authConsent, callback, dataFolder) { storedCallback = callback + + // . cannot be in a JSON property name with electron-store + exports.capabilities.versionId = 'java-node-minecraft-protocol-' + version.split('.').join('-') + const mcdata = require('minecraft-data')(version) // Used to get packets, may remove if I find a better way toClientMappings = mcdata.protocol.play.toClient.types.packet[1][0].type[1].mappings toServerMappings = mcdata.protocol.play.toServer.types.packet[1][0].type[1].mappings