MCPcopy Create free account
hub / github.com/Shazbot/WH3-Mod-Manager / writePackSorted

Function writePackSorted

src/packFileSerializer.ts:1888–1971  ·  view source on GitHub ↗
(packFiles: NewPackedFile[], path: string, dependencyPacks: string[] = [])

Source from the content-addressed store, hash-verified

1886};
1887// Original sorted implementation for when no existing pack is provided
1888const writePackSorted = async (packFiles: NewPackedFile[], path: string, dependencyPacks: string[] = []) => {
1889 let outFile: BinaryFile | undefined;
1890 try {
1891 const header = gameToPackHeader[appData.currentGame];
1892 const byteMask = 3;
1893 const refFileCount = 0;
1894 if (packFiles.length < 1) return;
1895 ensurePackFilesReadyForWrite(packFiles);
1896 outFile = new BinaryFile(path, "w", true);
1897 await outFile.open();
1898 // Sort files for consistent ordering
1899 packFiles.sort((firstPf, secondPf) => {
1900 return firstPf.name.localeCompare(secondPf.name);
1901 });
1902 // Calculate pack_file_index_size based on dependency packs
1903 const pack_file_index_size = dependencyPacks.reduce(
1904 (acc, dep) => acc + Buffer.byteLength(dep, "utf8") + 1,
1905 0,
1906 );
1907 const index_size = packFiles.reduce((acc, pack) => acc + new Blob([pack.name]).size + 1 + 5, 0);
1908 // Write header
1909 const headerAccumulator = new BufferAccumulator(outFile);
1910 headerAccumulator.addString(header);
1911 headerAccumulator.addInt32(byteMask);
1912 headerAccumulator.addInt32(refFileCount);
1913 headerAccumulator.addInt32(pack_file_index_size);
1914 headerAccumulator.addInt32(packFiles.length);
1915 headerAccumulator.addInt32(index_size);
1916 headerAccumulator.addInt32(0x7fffffff); // header_buffer
1917 await headerAccumulator.flush();
1918 // Write dependency packs
1919 for (const dep of dependencyPacks) {
1920 await outFile.writeString(dep);
1921 await outFile.writeUInt8(0);
1922 }
1923 // Write file index
1924 const indexAccumulator = new BufferAccumulator(outFile);
1925 for (const packFile of packFiles) {
1926 const { name } = packFile;
1927 const file_size = packFile.file_size!;
1928 if (packFile.schemaFields && packFile.tableSchema && !packFile.name.endsWith(".loc")) {
1929 indexAccumulator.addInt32(file_size);
1930 } else {
1931 indexAccumulator.addInt32(file_size);
1932 }
1933 if (supportsCompression[appData.currentGame]) indexAccumulator.addInt8(0); // is_compressed
1934 indexAccumulator.addString(name + "\0");
1935 await indexAccumulator.flushIfNeeded();
1936 }
1937 await indexAccumulator.flush();
1938 // Write file contents
1939 for (const packFile of packFiles) {
1940 if (packFile.buffer) {
1941 console.log("WRITE AS BUFFER:", packFile.name);
1942 await outFile.write(packFile.buffer);
1943 } else if (packFile.schemaFields && packFile.tableSchema) {
1944 if (packFile.name.endsWith(".loc")) {
1945 await outFile.write(Buffer.from([0xff, 0xfe]));

Callers 2

writePackAppendFastFunction · 0.85
writePackFunction · 0.85

Calls 8

addStringMethod · 0.95
addInt32Method · 0.95
flushMethod · 0.95
addInt8Method · 0.95
flushIfNeededMethod · 0.95
writeFieldBufferedFunction · 0.85
closeMethod · 0.80

Tested by

no test coverage detected