(packFiles: NewPackedFile[], path: string, dependencyPacks: string[] = [])
| 1886 | }; |
| 1887 | // Original sorted implementation for when no existing pack is provided |
| 1888 | const 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])); |
no test coverage detected