( packFiles: NewPackedFile[], path: string, existingPackToAppend?: Pack, dependencyPacks: string[] = [], )
| 1991 | }; |
| 1992 | // Legacy implementation (kept for reference, but replaced by the optimized versions above) |
| 1993 | export const writePackLegacy = async ( |
| 1994 | packFiles: NewPackedFile[], |
| 1995 | path: string, |
| 1996 | existingPackToAppend?: Pack, |
| 1997 | dependencyPacks: string[] = [], |
| 1998 | ) => { |
| 1999 | let outFile: BinaryFile | undefined; |
| 2000 | const timestamp = format(new Date(), "ddMMyy_HHmmss"); |
| 2001 | const outPath = existingPackToAppend ? `${path}_${timestamp}` : path; |
| 2002 | try { |
| 2003 | const header = gameToPackHeader[appData.currentGame]; |
| 2004 | const byteMask = 3; |
| 2005 | const refFileCount = 0; |
| 2006 | if (packFiles.length < 1) return; |
| 2007 | ensurePackFilesReadyForWrite(packFiles); |
| 2008 | outFile = new BinaryFile(outPath, "w", true); |
| 2009 | await outFile.open(); |
| 2010 | const allPackFiles = existingPackToAppend |
| 2011 | ? [ |
| 2012 | ...packFiles, |
| 2013 | ...existingPackToAppend.packedFiles.map( |
| 2014 | (pf) => |
| 2015 | ({ |
| 2016 | name: pf.name, |
| 2017 | file_size: pf.file_size, |
| 2018 | readBuffer: true, |
| 2019 | }) as NewPackedFile, |
| 2020 | ), |
| 2021 | ] |
| 2022 | : packFiles; |
| 2023 | allPackFiles.sort((firstPf, secondPf) => { |
| 2024 | return firstPf.name.localeCompare(secondPf.name); |
| 2025 | }); |
| 2026 | // Use existing dependency packs if not provided |
| 2027 | const finalDependencyPacks = |
| 2028 | dependencyPacks.length > 0 ? dependencyPacks : existingPackToAppend?.dependencyPacks || []; |
| 2029 | // Calculate pack_file_index_size based on dependency packs |
| 2030 | const pack_file_index_size = finalDependencyPacks.reduce( |
| 2031 | (acc, dep) => acc + Buffer.byteLength(dep, "utf8") + 1, |
| 2032 | 0, |
| 2033 | ); |
| 2034 | const index_size = allPackFiles.reduce((acc, pack) => acc + new Blob([pack.name]).size + 1 + 5, 0); |
| 2035 | // Use BufferAccumulator to batch header writes and reduce Promise overhead |
| 2036 | const headerAccumulator = new BufferAccumulator(outFile); |
| 2037 | headerAccumulator.addString(header); |
| 2038 | headerAccumulator.addInt32(byteMask); |
| 2039 | headerAccumulator.addInt32(refFileCount); |
| 2040 | headerAccumulator.addInt32(pack_file_index_size); |
| 2041 | headerAccumulator.addInt32(allPackFiles.length); |
| 2042 | headerAccumulator.addInt32(index_size); |
| 2043 | headerAccumulator.addInt32(0x7fffffff); // header_buffer |
| 2044 | await headerAccumulator.flush(); |
| 2045 | // Write dependency packs |
| 2046 | for (const dep of finalDependencyPacks) { |
| 2047 | await outFile.writeString(dep); |
| 2048 | await outFile.writeUInt8(0); |
| 2049 | } |
| 2050 | console.log("position after header:", outFile.tell()); |
nothing calls this directly
no test coverage detected