modmapper-web/components/DataDirPicker.tsx

91 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-03-03 04:53:20 +00:00
import React, { useContext, useEffect, useRef } from "react";
2022-03-03 04:53:20 +00:00
import { WorkerContext } from "../pages/index";
import { useAppSelector, useAppDispatch } from "../lib/hooks";
import {
addPluginInOrder,
clearPlugins,
setPending,
decrementPending,
PluginFile,
} from "../slices/plugins";
import styles from "../styles/DataDirPicker.module.css";
export const excludedPlugins = [
"Skyrim.esm",
"Update.esm",
"Dawnguard.esm",
"HearthFires.esm",
"Dragonborn.esm",
];
type Props = {};
const DataDirPicker: React.FC<Props> = () => {
2022-03-03 04:53:20 +00:00
const worker = useContext(WorkerContext);
const dispatch = useAppDispatch();
const plugins = useAppSelector((state) => state.plugins.plugins);
const onDataDirButtonClick = async () => {
2022-03-03 04:53:20 +00:00
if (!worker) {
return alert("Worker not loaded yet");
}
const dirHandle = await (
window as Window & typeof globalThis & { showDirectoryPicker: () => any }
).showDirectoryPicker();
dispatch(clearPlugins());
const values = dirHandle.values();
const plugins = [];
while (true) {
const next = await values.next();
if (next.done) {
break;
}
if (
next.value.kind == "file" &&
(next.value.name.endsWith(".esp") ||
next.value.name.endsWith(".esm") ||
next.value.name.endsWith(".esl"))
) {
console.log(next.value);
plugins.push(next.value);
}
}
dispatch(setPending(plugins.length));
for (const plugin of plugins) {
const file = await plugin.getFile();
console.log(file.lastModified);
console.log(file.lastModifiedDate);
const contents = new Uint8Array(await file.arrayBuffer());
try {
2022-03-03 04:53:20 +00:00
worker.postMessage(
{
skipParsing: excludedPlugins.includes(plugin.name),
filename: plugin.name,
lastModified: file.lastModified,
contents,
},
[contents.buffer]
);
} catch (error) {
console.error(error);
}
}
};
return (
<>
<p className={styles["no-top-margin"]}>
To see all of the cell edits and conflicts for your current mod load
order select your <code>Data</code> directory below to load the plugins.
</p>
2022-03-03 04:53:20 +00:00
<button onClick={onDataDirButtonClick} disabled={!worker}>
{plugins.length === 0 ? "Open" : "Reload"} Skyrim Data directory
</button>
</>
);
};
export default DataDirPicker;