|
| 1 | +import { appStorage } from '../react/appStorageProvider' |
| 2 | +import { getChangedSettings, options } from '../optionsStorage' |
| 3 | +import { customKeymaps } from '../controls' |
| 4 | +import { showInputsModal } from '../react/SelectOption' |
| 5 | + |
| 6 | +interface ExportedFile { |
| 7 | + _about: string |
| 8 | + options?: Record<string, any> |
| 9 | + keybindings?: Record<string, any> |
| 10 | + servers?: any[] |
| 11 | + username?: string |
| 12 | + proxy?: string |
| 13 | + proxies?: string[] |
| 14 | + accountTokens?: any[] |
| 15 | +} |
| 16 | + |
| 17 | +export const importData = async () => { |
| 18 | + try { |
| 19 | + const input = document.createElement('input') |
| 20 | + input.type = 'file' |
| 21 | + input.accept = '.json' |
| 22 | + input.click() |
| 23 | + |
| 24 | + const file = await new Promise<File>((resolve) => { |
| 25 | + input.onchange = () => { |
| 26 | + if (!input.files?.[0]) return |
| 27 | + resolve(input.files[0]) |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + const text = await file.text() |
| 32 | + const data = JSON.parse(text) |
| 33 | + |
| 34 | + if (!data._about?.includes('Minecraft Web Client')) { |
| 35 | + const doContinue = confirm('This file does not appear to be a Minecraft Web Client profile. Continue anyway?') |
| 36 | + if (!doContinue) return |
| 37 | + } |
| 38 | + |
| 39 | + // Build available data types for selection |
| 40 | + const availableData: Record<keyof Omit<ExportedFile, '_about'>, { present: boolean, description: string }> = { |
| 41 | + options: { present: !!data.options, description: 'Game settings and preferences' }, |
| 42 | + keybindings: { present: !!data.keybindings, description: 'Custom key mappings' }, |
| 43 | + servers: { present: !!data.servers, description: 'Saved server list' }, |
| 44 | + username: { present: !!data.username, description: 'Username' }, |
| 45 | + proxy: { present: !!data.proxy, description: 'Selected proxy server' }, |
| 46 | + proxies: { present: !!data.proxies, description: 'Global proxies list' }, |
| 47 | + accountTokens: { present: !!data.accountTokens, description: 'Account authentication tokens' }, |
| 48 | + } |
| 49 | + |
| 50 | + // Filter to only present data types |
| 51 | + const presentTypes = Object.fromEntries(Object.entries(availableData) |
| 52 | + .filter(([_, info]) => info.present) |
| 53 | + .map<any>(([key, info]) => [key, info])) |
| 54 | + |
| 55 | + if (Object.keys(presentTypes).length === 0) { |
| 56 | + alert('No compatible data found in the imported file.') |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + const importChoices = await showInputsModal('Select Data to Import', { |
| 61 | + mergeData: { |
| 62 | + type: 'checkbox', |
| 63 | + label: 'Merge with existing data (uncheck to remove old data)', |
| 64 | + defaultValue: true, |
| 65 | + }, |
| 66 | + ...Object.fromEntries(Object.entries(presentTypes).map(([key, info]) => [key, { |
| 67 | + type: 'checkbox', |
| 68 | + label: info.description, |
| 69 | + defaultValue: true, |
| 70 | + }])) |
| 71 | + }) as { mergeData: boolean } & Record<keyof ExportedFile, boolean> |
| 72 | + |
| 73 | + if (!importChoices) return |
| 74 | + |
| 75 | + const importedTypes: string[] = [] |
| 76 | + const shouldMerge = importChoices.mergeData |
| 77 | + |
| 78 | + if (importChoices.options && data.options) { |
| 79 | + if (shouldMerge) { |
| 80 | + Object.assign(options, data.options) |
| 81 | + } else { |
| 82 | + for (const key of Object.keys(options)) { |
| 83 | + if (key in data.options) { |
| 84 | + options[key as any] = data.options[key] |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + importedTypes.push('settings') |
| 89 | + } |
| 90 | + |
| 91 | + if (importChoices.keybindings && data.keybindings) { |
| 92 | + if (shouldMerge) { |
| 93 | + Object.assign(customKeymaps, data.keybindings) |
| 94 | + } else { |
| 95 | + for (const key of Object.keys(customKeymaps)) delete customKeymaps[key] |
| 96 | + Object.assign(customKeymaps, data.keybindings) |
| 97 | + } |
| 98 | + importedTypes.push('keybindings') |
| 99 | + } |
| 100 | + |
| 101 | + if (importChoices.servers && data.servers) { |
| 102 | + if (shouldMerge && appStorage.serversList) { |
| 103 | + // Merge by IP, update existing entries and add new ones |
| 104 | + const existingIps = new Set(appStorage.serversList.map(s => s.ip)) |
| 105 | + const newServers = data.servers.filter(s => !existingIps.has(s.ip)) |
| 106 | + appStorage.serversList = [...appStorage.serversList, ...newServers] |
| 107 | + } else { |
| 108 | + appStorage.serversList = data.servers |
| 109 | + } |
| 110 | + importedTypes.push('servers') |
| 111 | + } |
| 112 | + |
| 113 | + if (importChoices.username && data.username) { |
| 114 | + appStorage.username = data.username |
| 115 | + importedTypes.push('username') |
| 116 | + } |
| 117 | + |
| 118 | + if ((importChoices.proxy && data.proxy) || (importChoices.proxies && data.proxies)) { |
| 119 | + if (!appStorage.proxiesData) { |
| 120 | + appStorage.proxiesData = { proxies: [], selected: '' } |
| 121 | + } |
| 122 | + |
| 123 | + if (importChoices.proxies && data.proxies) { |
| 124 | + if (shouldMerge) { |
| 125 | + // Merge unique proxies |
| 126 | + const uniqueProxies = new Set([...appStorage.proxiesData.proxies, ...data.proxies]) |
| 127 | + appStorage.proxiesData.proxies = [...uniqueProxies] |
| 128 | + } else { |
| 129 | + appStorage.proxiesData.proxies = data.proxies |
| 130 | + } |
| 131 | + importedTypes.push('proxies list') |
| 132 | + } |
| 133 | + |
| 134 | + if (importChoices.proxy && data.proxy) { |
| 135 | + appStorage.proxiesData.selected = data.proxy |
| 136 | + importedTypes.push('selected proxy') |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if (importChoices.accountTokens && data.accountTokens) { |
| 141 | + if (shouldMerge && appStorage.authenticatedAccounts) { |
| 142 | + // Merge by unique identifier (assuming accounts have some unique ID or username) |
| 143 | + const existingAccounts = new Set(appStorage.authenticatedAccounts.map(a => a.username)) |
| 144 | + const newAccounts = data.accountTokens.filter(a => !existingAccounts.has(a.username)) |
| 145 | + appStorage.authenticatedAccounts = [...appStorage.authenticatedAccounts, ...newAccounts] |
| 146 | + } else { |
| 147 | + appStorage.authenticatedAccounts = data.accountTokens |
| 148 | + } |
| 149 | + importedTypes.push('account tokens') |
| 150 | + } |
| 151 | + |
| 152 | + alert(`Profile imported successfully! Imported data: ${importedTypes.join(', ')}.\nYou may need to reload the page for some changes to take effect.`) |
| 153 | + } catch (err) { |
| 154 | + console.error('Failed to import profile:', err) |
| 155 | + alert('Failed to import profile: ' + (err.message || err)) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +export const exportData = async () => { |
| 160 | + const data = await showInputsModal('Export Profile', { |
| 161 | + profileName: { |
| 162 | + type: 'text', |
| 163 | + }, |
| 164 | + exportSettings: { |
| 165 | + type: 'checkbox', |
| 166 | + defaultValue: true, |
| 167 | + }, |
| 168 | + exportKeybindings: { |
| 169 | + type: 'checkbox', |
| 170 | + defaultValue: true, |
| 171 | + }, |
| 172 | + exportServers: { |
| 173 | + type: 'checkbox', |
| 174 | + defaultValue: true, |
| 175 | + }, |
| 176 | + saveUsernameAndProxy: { |
| 177 | + type: 'checkbox', |
| 178 | + defaultValue: true, |
| 179 | + }, |
| 180 | + exportGlobalProxiesList: { |
| 181 | + type: 'checkbox', |
| 182 | + defaultValue: false, |
| 183 | + }, |
| 184 | + exportAccountTokens: { |
| 185 | + type: 'checkbox', |
| 186 | + defaultValue: false, |
| 187 | + }, |
| 188 | + }) |
| 189 | + const fileName = `${data.profileName ? `${data.profileName}-` : ''}web-client-profile.json` |
| 190 | + const json: ExportedFile = { |
| 191 | + _about: 'Minecraft Web Client (mcraft.fun) Profile', |
| 192 | + ...data.exportSettings ? { |
| 193 | + options: getChangedSettings(), |
| 194 | + } : {}, |
| 195 | + ...data.exportKeybindings ? { |
| 196 | + keybindings: customKeymaps, |
| 197 | + } : {}, |
| 198 | + ...data.exportServers ? { |
| 199 | + servers: appStorage.serversList, |
| 200 | + } : {}, |
| 201 | + ...data.saveUsernameAndProxy ? { |
| 202 | + username: appStorage.username, |
| 203 | + proxy: appStorage.proxiesData?.selected, |
| 204 | + } : {}, |
| 205 | + ...data.exportGlobalProxiesList ? { |
| 206 | + proxies: appStorage.proxiesData?.proxies, |
| 207 | + } : {}, |
| 208 | + ...data.exportAccountTokens ? { |
| 209 | + accountTokens: appStorage.authenticatedAccounts, |
| 210 | + } : {}, |
| 211 | + } |
| 212 | + const blob = new Blob([JSON.stringify(json, null, 2)], { type: 'application/json' }) |
| 213 | + const url = URL.createObjectURL(blob) |
| 214 | + const a = document.createElement('a') |
| 215 | + a.href = url |
| 216 | + a.download = fileName |
| 217 | + a.click() |
| 218 | + URL.revokeObjectURL(url) |
| 219 | +} |
0 commit comments