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 AosBaseRomContext = React.createContext(null);
11 const AosBaseRomProvider = ({ 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 === 0x35536183) {
19 localforage.setItem('aosBaseRom', buffer);
20 toastr.success(i18n.t('aos.baseRomSet'));
22 toastr.error(i18n.t('aos.baseRomInvalid'));
26 localforage.removeItem('aosBaseRom');
27 toastr.success(i18n.t('aos.baseRomRemoved'));
31 React.useEffect(async () => {
32 const stored = await localforage.getItem('aosBaseRom');
34 const crc = CRC32.buf(new Uint8Array(stored));
35 if (crc == 0x35536183) {
41 return <AosBaseRomContext.Provider value={{ rom, setRom: setRomCallback }}>
43 </AosBaseRomContext.Provider>;
46 AosBaseRomProvider.propTypes = {
47 children: PropTypes.node,
50 export const useAosBaseRom = () => React.useContext(AosBaseRomContext);
52 export default AosBaseRomProvider;