mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 23:04:52 +09:00
Some mods, for example cubium, includes bogus folders like __MACOSX in addition to the root folder where everything is located. This only works by chance as Factorio will check for the folder of the first entry in the zip file and use that. Match Factorio's logic in findRoot so that these mods work with Clusterio.
28 lines
750 B
TypeScript
28 lines
750 B
TypeScript
import type JSZip from "jszip";
|
|
|
|
/**
|
|
* Returns the root folder in the zip file
|
|
*
|
|
* Returns the name of the folder that the first entry in the zip file is
|
|
* contained in. Throws an error if this is not in a folder.
|
|
*
|
|
* This matches the logic Factorio uses when determining the folder to look
|
|
* for content in a zip file.
|
|
*
|
|
* @param zip - Zip to search through.
|
|
* @returns name of the root folder.
|
|
*/
|
|
export function findRoot(zip: JSZip) {
|
|
const relativePath = Object.keys(zip.files)[0];
|
|
if (relativePath === undefined) {
|
|
throw new Error("Empty zip file");
|
|
}
|
|
|
|
let index = relativePath.indexOf("/");
|
|
if (index === -1) {
|
|
throw new Error(`Zip contains file '${relativePath}' in root dir`);
|
|
}
|
|
|
|
return relativePath.slice(0, index);
|
|
}
|