gctGenerator/site/.vuepress/components/DownloadButton.vue

201 lines
5.6 KiB
Vue
Raw Normal View History

2020-06-28 06:33:20 +09:00
<template>
2021-06-11 07:48:15 +09:00
<div>
<ButtonComponent
label="Download"
:onClick="onClick"
:disabled="(!codes || codes.length === 0) && !stageLoaderCode"
2022-01-17 03:17:07 +09:00
/>
2021-06-11 07:48:15 +09:00
</div>
2020-06-28 06:33:20 +09:00
</template>
<script>
2020-06-30 05:30:29 +09:00
// Data
import gameVersions from '../data/gameVersions.json';
2020-07-10 12:21:13 +09:00
// Util
import { translate, translateCode } from '../i18n/localeHelper';
2020-07-10 12:21:13 +09:00
// customizable code
import codegens from './codes/codegen.js';
export const lskeyLDC = '@/lastDLCodes';
2020-06-28 06:33:20 +09:00
export default {
props: {
codes: { type: Array },
2020-06-28 10:45:44 +09:00
stageLoaderCode: { type: String },
2020-06-28 06:33:20 +09:00
format: { type: String },
2020-06-28 10:45:44 +09:00
versionIdentifier: { type: String },
2020-06-28 06:33:20 +09:00
},
2021-06-11 07:48:15 +09:00
data() {
2022-01-17 03:17:07 +09:00
return {};
2021-06-11 07:48:15 +09:00
},
2020-06-28 06:33:20 +09:00
methods: {
onClick() {
if ((!this.codes || !this.codes.length) && !this.stageLoaderCode) {
2020-06-28 06:33:20 +09:00
return;
}
const codeList = this.codes.map((c) => ({
...c,
// for recording previous downloaded code
titleEN: c.title.find(o => o.lang === 'en-US').content,
// for generated txt, ini
title: translateCode(c, this.$lang).title,
}));
// add dependencies information to title
const id2code = Object.fromEntries(codeList.map(c => [c.id, c]));
const depBys = {};
/* depends on */
for (const c of codeList) {
if (c.dependencies.length) {
c.dependencies.forEach(id => {
depBys[id] ??= [];
depBys[id].push(c.title);
});
const depList = c.dependencies.map(id => id2code[id].title).join(', ');
c.title += ` **(REQUIRES: ${depList})**`;
}
}
/* used by */
for (const [id, depBy] of Object.entries(depBys)) {
id2code[id].title += ` (Used by: ${depBy.join(', ')})`;
}
// save downloaded code list
try {
const codeTitles = codeList.map(c => c.titleEN);
localStorage.setItem(lskeyLDC, JSON.stringify(codeTitles));
} catch {}
2020-06-28 10:45:44 +09:00
if (this.stageLoaderCode)
2023-01-04 06:09:04 +09:00
codeList.push({
2020-06-29 00:35:18 +09:00
title: 'Stage List Loader',
author: 'Noki Doki',
date: '-',
version: '',
2020-06-28 10:45:44 +09:00
source: this.stageLoaderCode,
});
2020-06-28 06:33:20 +09:00
try {
2020-07-01 13:19:15 +09:00
window._paq.push([
'trackEvent',
'GCT Generator',
'Code Download',
JSON.stringify({
gameVersion: this.versionIdentifier,
format: this.format,
codes: codeList.map((code) => ({
title: code.title,
version: code.version,
})),
}),
]);
} catch {}
2020-07-01 14:26:48 +09:00
const fileName = gameVersions.find((v) => v.identifier === this.versionIdentifier).version;
2020-06-30 05:30:29 +09:00
// apply customizable codes
for (const code of codeList) {
const codegen = codegens[code.id];
if (codegen) {
2022-12-17 08:54:58 +09:00
code.source = codegen(this.versionIdentifier, code.source);
}
}
// generate file
const codeSize = codeList.reduce((a, e) => a + e.source.length, 0) / 2 + 16; // 8(00D0)+8(F000)
// console.log(codeSize, codeList);
2020-06-28 06:33:20 +09:00
switch (this.format) {
2020-06-29 00:35:18 +09:00
case 'gct':
this.alertGCTCodeSize(codeSize);
this.generateGCT(codeList, fileName);
2020-06-28 06:33:20 +09:00
break;
2020-06-29 00:35:18 +09:00
case 'dolphin':
this.alertDolphinCodeSize(codeSize);
this.generateDolphinINI(codeList, fileName);
2020-06-28 06:33:20 +09:00
break;
2020-06-29 00:35:18 +09:00
case 'gcm':
this.alertDolphinCodeSize(codeSize);
this.generateCheatManagerTXT(codeList, fileName);
2020-06-28 06:33:20 +09:00
break;
}
2020-06-28 10:45:44 +09:00
},
alertGCTCodeSize(size) {
if (size > 5000) {
alert(translate('generatorconfig.alert.gct', this.$lang).replaceAll('{size}', size));
}
},
alertDolphinCodeSize(size) {
2022-12-17 08:54:58 +09:00
if (size > 3272) {
// 0x3000-0x2338
// excluding header+footer
2022-12-17 08:54:58 +09:00
alert(
translate('generatorconfig.alert.dolphin', this.$lang).replaceAll('{size}', size - 16),
);
}
},
2020-07-03 10:53:48 +09:00
generateGCT(codes, version) {
let code = '00D0C0DE00D0C0DE';
codes.forEach((c) => (code += c.source));
2021-01-15 08:39:49 +09:00
code += 'F000000000000000';
2020-07-03 10:53:48 +09:00
let rawData = new Uint8Array(code.length / 2);
for (let x = 0; x < rawData.length; x++) {
rawData[x] = parseInt(code.substr(x * 2, 2), 16);
}
this.downloadFile(rawData, `${version}.gct`);
},
generateDolphinINI(codes, version) {
let data = 'Paste the following on top of your games .ini file:\r\n[Gecko]';
codes.forEach((code) => {
data += `\r\n$${code.title} (${code.date}) [${code.author}]\r\n`;
2020-07-03 10:53:48 +09:00
data += code.source
.match(/.{8}/g)
.join(' ')
.replace(/(.{17})./g, '$1\r\n');
});
this.downloadFile(data, `${version}.txt`);
},
generateCheatManagerTXT(codes, version) {
let data = `${version}\r\nSuper Mario Sunshine`;
codes.forEach((code) => {
2020-07-10 12:21:13 +09:00
const codeTitle =
typeof code.title === 'string' ? code.title : translateCode(code, this.$lang).title;
data += `\r\n\r\n${code.title} (${code.date}) [${code.author}]\r\n`;
2020-07-03 10:53:48 +09:00
data += code.source
.match(/.{8}/g)
.join(' ')
.replace(/(.{17})./g, '$1\r\n');
});
this.downloadFile(data, `${version}.txt`);
},
downloadFile(data, filename) {
var file = new Blob([data], {
type: 'application/octet-stream',
});
if (window.navigator.msSaveOrOpenBlob) window.navigator.msSaveOrOpenBlob(file, filename);
else {
var a = document.createElement('a'),
url = window.URL.createObjectURL(file);
a.href = url;
a.download = filename;
a.click();
setTimeout(function () {
window.URL.revokeObjectURL(url);
}, 500);
}
},
2020-06-28 10:45:44 +09:00
},
2020-06-28 06:33:20 +09:00
};
</script>