]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/aos/Seed.js
basic aosr patcher
[alttp.git] / resources / js / components / aos / Seed.js
1 import FileSaver from 'file-saver';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { Button, Col, Container, Row } from 'react-bootstrap';
5 import { withTranslation } from 'react-i18next';
6 import toastr from 'toastr';
7
8 import BaseRomButton from './BaseRomButton';
9 import { useAosBaseRom } from '../../helpers/AosBaseRomContext';
10 import BPS from '../../helpers/bps';
11 import i18n from '../../i18n';
12
13 const applyPatch = (rom, patch, filename) => {
14         try {
15                 const bps = new BPS();
16                 bps.setPatch(patch);
17                 bps.setSource(rom);
18                 const result = bps.applyPatch();
19                 FileSaver.saveAs(new Blob([result], { type: 'application/octet-stream' }), filename);
20         } catch (e) {
21                 toastr.error(i18n.t('aosSeeds.patchError', { msg: e.message }));
22         }
23 };
24
25 const Seed = ({ patch, seed }) => {
26         const { rom } = useAosBaseRom();
27
28         return <Container>
29                 <h1>{i18n.t('aosSeeds.heading')}</h1>
30                 <Row>
31                         <Col md={{ order: 2 }}>
32                                 {rom ?
33                                         <Button
34                                                 disabled={!patch}
35                                                 onClick={() => applyPatch(
36                                                         rom,
37                                                         patch,
38                                                         `${i18n.t('aosSeeds.filename', {
39                                                                 hash: seed.hash,
40                                                                 preset: seed.preset,
41                                                         })}.gba`,
42                                                 )}
43                                                 variant="primary"
44                                         >
45                                                 {i18n.t(patch ? 'aosSeeds.patch' : 'aosSeeds.fetchingPatch')}
46                                         </Button>
47                                 :
48                                         <BaseRomButton />
49                                 }
50                         </Col>
51                         <Col md={{ order: 1 }}>
52                                 <p>
53                                         {i18n.t('aosSeeds.preset')}:
54                                         {' '}
55                                         <strong>{i18n.t(`aosSeeds.presets.${seed.preset}`)}</strong>
56                                 </p>
57                                 <p>{i18n.t(seed.race ? 'aosSeeds.race' : 'aosSeeds.noRace')}</p>
58                                 <p>{i18n.t(seed.mystery ? 'aosSeeds.mystery' : 'aosSeeds.noMystery')}</p>
59                         </Col>
60                 </Row>
61                 <h2 className="mt-5">{i18n.t('aosSeeds.generator')}</h2>
62                 <p>{i18n.t(`aosSeeds.generators.${seed.generator}`)}</p>
63         </Container>;
64 };
65
66 Seed.propTypes = {
67         patch: PropTypes.instanceOf(ArrayBuffer),
68         seed: PropTypes.shape({
69                 generator: PropTypes.string,
70                 hash: PropTypes.string,
71                 mystery: PropTypes.bool,
72                 preset: PropTypes.string,
73                 race: PropTypes.bool,
74         }),
75 };
76
77 export default withTranslation()(Seed);