1 import CRC32 from 'crc-32';
2 import localforage from 'localforage';
3 import PropTypes from 'prop-types';
4 import React from 'react';
5 import toastr from 'toastr';
7 import i18n from '../i18n';
9 const AlttpBaseRomContext = React.createContext(null);
11 const AlttpBaseRomProvider = ({ children }) => {
12 const [rom, setRom] = React.useState(null);
14 const setRomCallback = React.useCallback(buffer => {
16 const crc = CRC32.buf(new Uint8Array(buffer));
17 if (crc === 0x3322EFFC) {
19 localforage.setItem('alttpBaseRom', buffer);
20 toastr.success(i18n.t('alttp.baseRomSet'));
22 toastr.error(i18n.t('alttp.baseRomInvalid'));
26 localforage.removeItem('alttpBaseRom');
27 toastr.success(i18n.t('alttp.baseRomRemoved'));
31 React.useEffect(() => {
33 const stored = await localforage.getItem('alttpBaseRom');
35 const crc = CRC32.buf(new Uint8Array(stored));
36 if (crc == 0x3322EFFC) {
43 return <AlttpBaseRomContext.Provider value={{ rom, setRom: setRomCallback }}>
45 </AlttpBaseRomContext.Provider>;
48 AlttpBaseRomProvider.propTypes = {
49 children: PropTypes.node,
52 export const useAlttpBaseRom = () => React.useContext(AlttpBaseRomContext);
54 export default AlttpBaseRomProvider;