diff --git a/README.md b/README.md index 10ccba7..78b7d4c 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,8 @@ Hackdex is a community hub for discovering and sharing Pokémon romhack patches. ## Core features - **Discover**: curated hacks with screenshots, tags, versions, and summaries -- **Submit**: metadata, screenshots, social links, and a BPS patch file -- **Patch in the browser**: Powered by [RomPatcher.js](https://github.com/marcrobledo/RomPatcher.js); linked base roms stay on the user's device +- **Submit**: metadata, screenshots, social links, and a `.bps` or `.xdelta` patch file +- **Patch in the browser**: BPS via [RomPatcher.js](https://github.com/marcrobledo/RomPatcher.js); xdelta (VCDIFF) via a WASM build of [xdelta3](https://github.com/jmacd/xdelta) with glue from the Hackdex fork of [xdelta-wasm](https://github.com/Hackdex-App/xdelta-wasm) (forked from [kotcrab/xdelta-wasm](https://github.com/kotcrab/xdelta-wasm); statically linked [XZ Utils](https://tukaani.org/xz/) liblzma); linked base roms stay on the user's device - **Safe delivery**: public urls for cover images, short-lived signed URLs for patch downloads and other assets; no rom storage required ## Tech stack @@ -25,7 +25,7 @@ Hackdex is a community hub for discovering and sharing Pokémon romhack patches. - Next.js 15 (App Router), TypeScript, React 19, Tailwind CSS 4 - Supabase (Postgres, Auth, Storage) for data, auth, and cover images - S3-compatible object storage (Minio locally or preferred provider) for patch files (`patches` bucket) -- In-browser patching with RomPatcher.js; local persistence with IndexedDB and the File System Access API +- In-browser patching with RomPatcher.js and xdelta3 WASM; local persistence with IndexedDB and the File System Access API ## High-level architecture diff --git a/docs/xdelta-wasm.md b/docs/xdelta-wasm.md new file mode 100644 index 0000000..e14d781 --- /dev/null +++ b/docs/xdelta-wasm.md @@ -0,0 +1,69 @@ +# Building and vendoring xdelta WASM + +Hackdex patches xdelta (VCDIFF) files in the browser using a WebAssembly build of [xdelta3](https://github.com/jmacd/xdelta). That binary is **not** built inside this repo. You build it from a local checkout of the Hackdex fork of [xdelta-wasm](https://github.com/Hackdex-App/xdelta-wasm) (forked from [kotcrab/xdelta-wasm](https://github.com/kotcrab/xdelta-wasm)), then copy two artifacts into `public/xdelta/`. + +Use this guide when you need to rebuild or update those artifacts. + +--- + +## Prerequisites + +- A local checkout of [Hackdex-App/xdelta-wasm](https://github.com/Hackdex-App/xdelta-wasm). +- [Emscripten](https://emscripten.org) (`emcc` 6.x tested). On macOS: `brew install emscripten`. Otherwise follow the [emsdk install docs](https://emscripten.org/docs/getting_started/downloads.html). +- After cloning xdelta-wasm, initialize the jmacd/xdelta submodule: + +```bash +git submodule update --init +``` + +This populates `native/xdelta`. + +--- + +## One-time setup (XZ / liblzma) + +Secondary LZMA compression (`xdelta3 -S lzma`) needs static liblzma. Run this if `native/xz/` is missing: + +```bash +./native/build-xz.sh +``` + +That downloads XZ Utils and builds it with `emconfigure` / `emmake`. + +--- + +## Building + +From the xdelta-wasm checkout root: + +```bash +./native/build.sh +``` + +This compiles `native/xdelta/xdelta3/xdelta3.c` and `native/xdelta3-wasm.c`, links liblzma, and writes: + +- `public/xdelta3.js` (ES6 module) +- `public/xdelta3.wasm` (Companion WASM binary) + +--- + +## Vendoring into Hackdex + +Copy **only** those two files into this repo (overwrite existing): + +```bash +cp public/xdelta3.js public/xdelta3.wasm \ + /path/to/hackdex-website/public/xdelta/ +``` + +Do **not** overwrite these Hackdex-owned files in `public/xdelta/`: + +- `xdelta3.worker.js`: Hackdex worker (protocol differs from upstream) +- `LICENSE-xdelta3.txt` +- `NOTICE.txt` + +--- + +## Licensing + +See `public/xdelta/LICENSE-xdelta3.txt` and `public/xdelta/NOTICE.txt`. \ No newline at end of file diff --git a/public/xdelta/LICENSE-xdelta3.txt b/public/xdelta/LICENSE-xdelta3.txt new file mode 100644 index 0000000..7a77415 --- /dev/null +++ b/public/xdelta/LICENSE-xdelta3.txt @@ -0,0 +1,176 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, +and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by +the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all +other entities that control, are controlled by, or are under common +control with that entity. For the purposes of this definition, +"control" means (i) the power, direct or indirect, to cause the +direction or management of such entity, whether by contract or +otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity +exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, +including but not limited to software source code, documentation +source, and configuration files. + +"Object" form shall mean any form resulting from mechanical +transformation or translation of a Source form, including but +not limited to compiled object code, generated documentation, +and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or +Object form, made available under the License, as indicated by a +copyright notice that is included in or attached to the work +(an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object +form, that is based on (or derived from) the Work and for which the +editorial revisions, annotations, elaborations, or other modifications +represent, as a whole, an original work of authorship. For the purposes +of this License, Derivative Works shall not include works that remain +separable from, or merely link (or bind by name) to the interfaces of, +the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including +the original version of the Work and any modifications or additions +to that Work or Derivative Works thereof, that is intentionally +submitted to Licensor for inclusion in the Work by the copyright owner +or by an individual or Legal Entity authorized to submit on behalf of +the copyright owner. For the purposes of this definition, "submitted" +means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, +and issue tracking systems that are managed by, or on behalf of, the +Licensor for the purpose of discussing and improving the Work, but +excluding communication that is conspicuously marked or otherwise +designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity +on behalf of whom a Contribution has been received by Licensor and +subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the +Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +(except as stated in this section) patent license to make, have made, +use, offer to sell, sell, import, and otherwise transfer the Work, +where such license applies only to those patent claims licensable +by such Contributor that are necessarily infringed by their +Contribution(s) alone or by combination of their Contribution(s) +with the Work to which such Contribution(s) was submitted. If You +institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work +or a Contribution incorporated within the Work constitutes direct +or contributory patent infringement, then any patent licenses +granted to You under this License for that Work shall terminate +as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the +Work or Derivative Works thereof in any medium, with or without +modifications, and in Source or Object form, provided that You +meet the following conditions: + +(a) You must give any other recipients of the Work or +Derivative Works a copy of this License; and + +(b) You must cause any modified files to carry prominent notices +stating that You changed the files; and + +(c) You must retain, in the Source form of any Derivative Works +that You distribute, all copyright, patent, trademark, and +attribution notices from the Source form of the Work, +excluding those notices that do not pertain to any part of +the Derivative Works; and + +(d) If the Work includes a "NOTICE" text file as part of its +distribution, then any Derivative Works that You distribute must +include a readable copy of the attribution notices contained +within such NOTICE file, excluding those notices that do not +pertain to any part of the Derivative Works, in at least one +of the following places: within a NOTICE text file distributed +as part of the Derivative Works; within the Source form or +documentation, if provided along with the Derivative Works; or, +within a display generated by the Derivative Works, if and +wherever such third-party notices normally appear. The contents +of the NOTICE file are for informational purposes only and +do not modify the License. You may add Your own attribution +notices within Derivative Works that You distribute, alongside +or as an addendum to the NOTICE text from the Work, provided +that such additional attribution notices cannot be construed +as modifying the License. + +You may add Your own copyright statement to Your modifications and +may provide additional or different license terms and conditions +for use, reproduction, or distribution of Your modifications, or +for any such Derivative Works as a whole, provided Your use, +reproduction, and distribution of the Work otherwise complies with +the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, +any Contribution intentionally submitted for inclusion in the Work +by You to the Licensor shall be under the terms and conditions of +this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify +the terms of any separate license agreement you may have executed +with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade +names, trademarks, service marks, or product names of the Licensor, +except as required for reasonable and customary use in describing the +origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or +agreed to in writing, Licensor provides the Work (and each +Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied, including, without limitation, any warranties or conditions +of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A +PARTICULAR PURPOSE. You are solely responsible for determining the +appropriateness of using or redistributing the Work and assume any +risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, +whether in tort (including negligence), contract, or otherwise, +unless required by applicable law (such as deliberate and grossly +negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, +incidental, or consequential damages of any character arising as a +result of this License or out of the use or inability to use the +Work (including but not limited to damages for loss of goodwill, +work stoppage, computer failure or malfunction, or any and all +other commercial damages or losses), even if such Contributor +has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing +the Work or Derivative Works thereof, You may choose to offer, +and charge a fee for, acceptance of support, warranty, indemnity, +or other liability obligations and/or rights consistent with this +License. However, in accepting such obligations, You may act only +on Your own behalf and on Your sole responsibility, not on behalf +of any other Contributor, and only if You agree to indemnify, +defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason +of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS diff --git a/public/xdelta/NOTICE.txt b/public/xdelta/NOTICE.txt new file mode 100644 index 0000000..52d37cb --- /dev/null +++ b/public/xdelta/NOTICE.txt @@ -0,0 +1,17 @@ +xdelta3.wasm and xdelta3.js +=========================== + +These artifacts are built from xdelta3 +(https://github.com/jmacd/xdelta), Copyright Joshua MacDonald, +licensed under the Apache License, Version 2.0. See LICENSE-xdelta3.txt. + +They include glue code from the Hackdex fork of xdelta-wasm +(https://github.com/Hackdex-App/xdelta-wasm), forked from +kotcrab/xdelta-wasm (https://github.com/kotcrab/xdelta-wasm), also +licensed under Apache-2.0. That glue code has been modified for +Hackdex (encoder support and checksum-presence reporting). Per +Apache-2.0 section 4(b), this NOTICE states that modifications were +made. + +The build statically links liblzma from XZ Utils +(https://tukaani.org/xz/), which is 0BSD / public domain. diff --git a/public/xdelta/xdelta3.js b/public/xdelta/xdelta3.js new file mode 100644 index 0000000..d885e3a --- /dev/null +++ b/public/xdelta/xdelta3.js @@ -0,0 +1,2 @@ +async function createXdelta3Module(moduleArg={}){var Module=moduleArg;var ENVIRONMENT_IS_WEB=false;var ENVIRONMENT_IS_WORKER=true;var programArgs=[];var thisProgram="./this.program";var quit_=(status,toThrow)=>{throw toThrow};var _scriptName=import.meta.url;var scriptDirectory="";function locateFile(path){if(Module["locateFile"]){return Module["locateFile"](path,scriptDirectory)}return scriptDirectory+path}var readAsync,readBinary;if(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER){try{scriptDirectory=new URL(".",_scriptName).href}catch{}{if(ENVIRONMENT_IS_WORKER){readBinary=url=>{var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.responseType="arraybuffer";xhr.send(null);return new Uint8Array(xhr.response)}}readAsync=async url=>{var response=await fetch(url,{credentials:"same-origin"});if(response.ok){return response.arrayBuffer()}throw new Error(response.status+" : "+response.url)}}}else{}var out=console.log.bind(console);var err=console.error.bind(console);var wasmBinary;var ABORT=false;var EXITSTATUS;class EmscriptenEH{}class EmscriptenSjLj extends EmscriptenEH{}var runtimeInitialized=false;function getMemoryBuffer(){return wasmMemory.buffer}function updateMemoryViews(){if(HEAP8?.buffer?.resizable)return;var b=getMemoryBuffer();Module["HEAP8"]=HEAP8=new Int8Array(b);HEAP16=new Int16Array(b);HEAPU8=new Uint8Array(b);HEAPU16=new Uint16Array(b);HEAP32=new Int32Array(b);HEAPU32=new Uint32Array(b);HEAPF32=new Float32Array(b);HEAPF64=new Float64Array(b);HEAP64=new BigInt64Array(b);HEAPU64=new BigUint64Array(b)}function preRun(){var preRun=Module["preRun"];if(preRun){if(typeof preRun=="function")preRun=[preRun];onPreRuns.push(...preRun)}callRuntimeCallbacks(onPreRuns)}function initRuntime(){runtimeInitialized=true;wasmExports["__wasm_call_ctors"]()}function postRun(){var postRun=Module["postRun"];if(postRun){if(typeof postRun=="function")postRun=[postRun];onPostRuns.push(...postRun)}callRuntimeCallbacks(onPostRuns)}function abort(what){Module["onAbort"]?.(what);what=`Aborted(${what})`;err(what);ABORT=true;what+=". Build with -sASSERTIONS for more info.";var e=new WebAssembly.RuntimeError(what);throw e}var wasmBinaryFile;function findWasmBinary(){if(Module["locateFile"]){return locateFile("xdelta3.wasm")}return new URL("xdelta3.wasm",import.meta.url).href}function getBinarySync(file){if(readBinary){return readBinary(file)}throw"both async and sync fetching of the wasm failed"}async function getWasmBinary(binaryFile){if(!wasmBinary){try{var response=await readAsync(binaryFile);return new Uint8Array(response)}catch{}}return getBinarySync(binaryFile)}async function instantiateArrayBuffer(binaryFile,imports){try{var binary=await getWasmBinary(binaryFile);var instance=await WebAssembly.instantiate(binary,imports);return instance}catch(reason){err(`failed to asynchronously prepare wasm: ${reason}`);abort(reason)}}async function instantiateAsync(binary,binaryFile,imports){if(!binary){try{var response=fetch(binaryFile,{credentials:"same-origin"});var instantiationResult=await WebAssembly.instantiateStreaming(response,imports);return instantiationResult}catch(reason){err(`wasm streaming compile failed: ${reason}`);err("falling back to ArrayBuffer instantiation")}}return instantiateArrayBuffer(binaryFile,imports)}function getWasmImports(){var imports={env:wasmImports,wasi_snapshot_preview1:wasmImports};return imports}async function createWasm(){function receiveInstance(instance){wasmExports=instance.exports;assignWasmExports(wasmExports);updateMemoryViews();return wasmExports}function receiveInstantiationResult(result){return receiveInstance(result["instance"])}var info=getWasmImports();var instantiateWasm=Module["instantiateWasm"];if(instantiateWasm){return new Promise(resolve=>{instantiateWasm(info,inst=>resolve(receiveInstance(inst)))})}wasmBinaryFile??=findWasmBinary();var result=await instantiateAsync(wasmBinary,wasmBinaryFile,info);var exports=receiveInstantiationResult(result);return exports}class ExitStatus{name="ExitStatus";constructor(status){this.message=`Program terminated with exit(${status})`;this.status=status}}var HEAP16;var HEAP32;var HEAP64;var HEAP8;var HEAPF32;var HEAPF64;var HEAPU16;var HEAPU32;var HEAPU64;var HEAPU8;var callRuntimeCallbacks=callbacks=>{while(callbacks.length>0){callbacks.shift()(Module)}};var onPostRuns=[];var onPreRuns=[];var noExitRuntime=true;var readEmAsmArgsArray=[];var readEmAsmArgs=(sigPtr,buf)=>{readEmAsmArgsArray.length=0;var ch;while(ch=HEAPU8[sigPtr++]){var wide=ch!=105;wide&=ch!=112;buf+=wide&&buf%8?4:0;readEmAsmArgsArray.push(ch==112?HEAPU32[buf>>2]:ch==106?HEAP64[buf>>3]:ch==105?HEAP32[buf>>2]:HEAPF64[buf>>3]);buf+=wide?8:4}return readEmAsmArgsArray};var runEmAsmFunction=(code,sigPtr,argbuf)=>{var args=readEmAsmArgs(sigPtr,argbuf);return ASM_CONSTS[code](...args)};var _emscripten_asm_const_int=(code,sigPtr,argbuf)=>runEmAsmFunction(code,sigPtr,argbuf);var abortOnCannotGrowMemory=requestedSize=>{abort("OOM")};var getHeapMax=()=>2147483648;var alignMemory=(size,alignment)=>Math.ceil(size/alignment)*alignment;var growMemory=size=>{var oldHeapSize=wasmMemory.buffer.byteLength;var pages=(size-oldHeapSize+65535)/65536|0;try{wasmMemory.grow(pages);updateMemoryViews();return 1}catch(e){}};var _emscripten_resize_heap=requestedSize=>{var oldSize=HEAPU8.length;requestedSize>>>=0;var maxHeapSize=getHeapMax();if(requestedSize>maxHeapSize){abortOnCannotGrowMemory(requestedSize)}for(var cutDown=1;cutDown<=4;cutDown*=2){var overGrownHeapSize=oldSize*(1+.2/cutDown);overGrownHeapSize=Math.min(overGrownHeapSize,requestedSize+100663296);var newSize=Math.min(maxHeapSize,alignMemory(Math.max(requestedSize,overGrownHeapSize),65536));var replacement=growMemory(newSize);if(replacement){return true}}abortOnCannotGrowMemory(requestedSize)};var _fd_close=fd=>52;var INT53_MAX=9007199254740992;var INT53_MIN=-9007199254740992;var bigintToI53Checked=num=>numINT53_MAX?NaN:Number(num);function _fd_seek(fd,offset,whence,newOffset){offset=bigintToI53Checked(offset);return 70}var printCharBuffers=[null,[],[]];var UTF8Decoder=globalThis.TextDecoder&&new TextDecoder;var findStringEnd=(heapOrArray,idx,maxBytesToRead,ignoreNul)=>{var maxIdx=idx+maxBytesToRead;if(ignoreNul)return maxIdx;while(heapOrArray[idx]&&!(idx>=maxIdx))++idx;return idx};var UTF8ArrayToString=(heapOrArray,idx=0,maxBytesToRead,ignoreNul)=>{var endPtr=findStringEnd(heapOrArray,idx,maxBytesToRead,ignoreNul);if(endPtr-idx>16&&heapOrArray.buffer&&UTF8Decoder){return UTF8Decoder.decode(heapOrArray.subarray(idx,endPtr))}var str="";while(idx>10,56320|ch&1023)}}return str};var printChar=(stream,curr)=>{var buffer=printCharBuffers[stream];if(curr===0||curr===10){(stream===1?out:err)(UTF8ArrayToString(buffer));buffer.length=0}else{buffer.push(curr)}};var UTF8ToString=(ptr,maxBytesToRead,ignoreNul)=>ptr?UTF8ArrayToString(HEAPU8,ptr,maxBytesToRead,ignoreNul):"";var _fd_write=(fd,iov,iovcnt,pnum)=>{var num=0;for(var i=0;i>2];var len=HEAPU32[iov+4>>2];iov+=8;for(var j=0;j>2]=num;return 0};var runtimeKeepaliveCounter=0;var keepRuntimeAlive=()=>noExitRuntime||runtimeKeepaliveCounter>0;var _proc_exit=code=>{EXITSTATUS=code;if(!keepRuntimeAlive()){Module["onExit"]?.(code);ABORT=true}quit_(code,new ExitStatus(code))};var exitJS=(status,implicit)=>{EXITSTATUS=status;_proc_exit(status)};var handleException=e=>{if(e instanceof ExitStatus||e=="unwind"){return EXITSTATUS}quit_(1,e)};var lengthBytesUTF8=str=>{var len=0;for(var i=0;i=55296&&c<=57343){len+=4;++i}else{len+=3}}return len};var stringToUTF8Array=(str,heap,outIdx,maxBytesToWrite)=>{if(!(maxBytesToWrite>0))return 0;var startIdx=outIdx;var endIdx=outIdx+maxBytesToWrite-1;for(var i=0;i=endIdx)break;heap[outIdx++]=u}else if(u<=2047){if(outIdx+1>=endIdx)break;heap[outIdx++]=192|u>>6;heap[outIdx++]=128|u&63}else if(u<=65535){if(outIdx+2>=endIdx)break;heap[outIdx++]=224|u>>12;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63}else{if(outIdx+3>=endIdx)break;heap[outIdx++]=240|u>>18;heap[outIdx++]=128|u>>12&63;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63;i++}}heap[outIdx]=0;return outIdx-startIdx};var stringToUTF8=(str,outPtr,maxBytesToWrite)=>stringToUTF8Array(str,HEAPU8,outPtr,maxBytesToWrite);var stackAlloc=sz=>__emscripten_stack_alloc(sz);var stringToUTF8OnStack=str=>{var size=lengthBytesUTF8(str)+1;var ret=stackAlloc(size);stringToUTF8(str,ret,size);return ret};{if(Module["noExitRuntime"])noExitRuntime=Module["noExitRuntime"];if(Module["print"])out=Module["print"];if(Module["printErr"])err=Module["printErr"];if(Module["arguments"])programArgs=Module["arguments"];if(Module["thisProgram"])thisProgram=Module["thisProgram"];var preInit=Module["preInit"];if(preInit){if(typeof preInit=="function")Module["preInit"]=preInit=[preInit];while(preInit.length>0){preInit.shift()()}}}Module["callMain"]=callMain;Module["UTF8ToString"]=UTF8ToString;var ASM_CONSTS={30936:($0,$1,$2)=>Module.readSource($0,$1,$2),30978:($0,$1,$2)=>Module.readInput($0,$1,$2),31019:($0,$1)=>{Module.outputFile($0,$1)},31047:$0=>{Module.reportError($0)},31072:$0=>{Module.reportChecksums($0)}};var _main,__emscripten_stack_restore,__emscripten_stack_alloc,_emscripten_stack_get_current,memory,__indirect_function_table,wasmMemory;function assignWasmExports(wasmExports){_main=Module["_main"]=wasmExports["__main_argc_argv"];__emscripten_stack_restore=wasmExports["_emscripten_stack_restore"];__emscripten_stack_alloc=wasmExports["_emscripten_stack_alloc"];_emscripten_stack_get_current=wasmExports["emscripten_stack_get_current"];memory=wasmMemory=wasmExports["memory"];__indirect_function_table=wasmExports["__indirect_function_table"]}var wasmImports={emscripten_asm_const_int:_emscripten_asm_const_int,emscripten_resize_heap:_emscripten_resize_heap,fd_close:_fd_close,fd_seek:_fd_seek,fd_write:_fd_write};function callMain(args=[]){var entryFunction=_main;args.unshift(thisProgram);var argc=args.length;var argv=stackAlloc((argc+1)*4);var argv_ptr=argv;for(var arg of args){HEAPU32[argv_ptr>>2]=stringToUTF8OnStack(arg);argv_ptr+=4}HEAPU32[argv_ptr>>2]=0;try{var ret=entryFunction(argc,argv);exitJS(ret,true);return ret}catch(e){return handleException(e)}}async function run(args=programArgs){preRun();var setStatus=Module["setStatus"];if(setStatus){setStatus("Running...");await new Promise(resolve=>setTimeout(resolve,1));setTimeout(setStatus,1,"")}if(ABORT)return;initRuntime();Module["onRuntimeInitialized"]?.();var noInitialRun=Module["noInitialRun"]||true;if(!noInitialRun)callMain(args);postRun()}var wasmExports;wasmExports=await createWasm();await run(); +;return Module}export default createXdelta3Module; diff --git a/public/xdelta/xdelta3.wasm b/public/xdelta/xdelta3.wasm new file mode 100755 index 0000000..680e599 Binary files /dev/null and b/public/xdelta/xdelta3.wasm differ diff --git a/public/xdelta/xdelta3.worker.js b/public/xdelta/xdelta3.worker.js new file mode 100644 index 0000000..c34b82b --- /dev/null +++ b/public/xdelta/xdelta3.worker.js @@ -0,0 +1,133 @@ +import createXdelta3Module from './xdelta3.js' + +const bufferSize = 4 * 1024 * 1024 +const cacheSize = 32 +const PROGRESS_INTERVAL = 8 * 1024 * 1024 + +let module = undefined +const state = { + sourceFile: undefined, + inputFile: undefined, + errorMessage: undefined, + hasChecksums: null, + discardOutput: false, + bytesOut: 0, + bytesIn: 0, + lastProgressAt: 0, +} + +// eslint-disable-next-line no-undef +const reader = new FileReaderSync() + +function readSource(buffer, offset, size) { + return readFile(state.sourceFile, buffer, Number(offset), size) +} + +function readInput(buffer, offset, size) { + const read = readFile(state.inputFile, buffer, Number(offset), size) + state.bytesIn += read + return read +} + +function reportChecksums(hasChecksums) { + state.hasChecksums = hasChecksums ? true : false +} + +function readFile(file, buffer, offset, size) { + const end = Math.min(file.size, offset + size) + const blob = file.slice(offset, end) + const read = end - offset + const data = reader.readAsArrayBuffer(blob) + module.HEAP8.set(new Uint8Array(data), buffer) + return read +} + +function maybeProgress() { + if (state.bytesOut - state.lastProgressAt >= PROGRESS_INTERVAL) { + postMessage({ + type: 'progress', + bytesOut: state.bytesOut, + bytesIn: state.bytesIn, + }) + state.lastProgressAt = state.bytesOut + } +} + +function outputFile(buffer, size) { + state.bytesOut += size + if (!state.discardOutput) { + const dataView = new Uint8Array(module.HEAP8.buffer, buffer, size) + const data = new Uint8Array(dataView) + postMessage({ type: 'chunk', bytes: data }, [data.buffer]) + } + maybeProgress() +} + +function reportError(msgPtr) { + state.errorMessage = module.UTF8ToString(msgPtr) +} + +function postDone(ok, errorCode) { + const msg = { + type: 'done', + ok, + hasChecksums: state.hasChecksums, + } + if (errorCode !== undefined) { + msg.errorCode = errorCode + } + if (state.errorMessage) { + msg.errorMessage = state.errorMessage + } + postMessage(msg) +} + +onmessage = async function (event) { + if (!event.data) { + return + } + const { command, mode, sourceFile, inputFile, disableChecksum, discardOutput } = event.data + if (command !== 'start') { + return + } + + state.sourceFile = sourceFile + state.inputFile = inputFile + state.errorMessage = undefined + state.hasChecksums = null + state.discardOutput = !!discardOutput + state.bytesOut = 0 + state.bytesIn = 0 + state.lastProgressAt = 0 + + try { + module = await createXdelta3Module() + module.readInput = readInput + module.readSource = readSource + module.outputFile = outputFile + module.reportError = reportError + module.reportChecksums = reportChecksums + + const result = module.callMain([ + mode, + bufferSize.toString(), + cacheSize.toString(), + (!!disableChecksum).toString(), + // Known source size lets the encoder search the whole source for matches. + sourceFile.size.toString(), + ]) + + if (result !== 0) { + postDone(false, result) + } else { + postDone(true) + } + } catch (e) { + console.error(e) + if (!state.errorMessage && e && typeof e.message === 'string') { + state.errorMessage = e.message + } + postDone(false) + } + module = undefined +} diff --git a/src/app/faq/entries.md b/src/app/faq/entries.md index 426a6b5..7d6700d 100644 --- a/src/app/faq/entries.md +++ b/src/app/faq/entries.md @@ -79,14 +79,12 @@ Only the original creator or a member of their team can submit the hack to Hackd Account creation is required for submissions to preserve author control and attribution. This ensures your work is properly credited and you maintain control over your hack's listing. This also allows you to update your hack after submission. ### What format should I submit my hack in? -We only accept BPS patch files, not complete ROMs. Hackdex utilizes a built-in patcher that users apply to their own legally obtained base ROMs. This helps keep the platform safer from potential legal issues. +We accept `.bps` and `.xdelta` patch files, not complete ROMs. Hackdex utilizes a built-in patcher that users apply to their own legally obtained base ROMs. This helps keep the platform safer from potential legal issues. xdelta is the preferred format going forward; legacy BPS patches continue to work. -A built-in patcher is also included in the submission form, so you also have the option to provide your modified ROM and the base ROM to generate the patch file automatically. +A built-in patcher is also included in the submission form, so you also have the option to provide your modified ROM and the base ROM to generate a `.xdelta` patch file automatically. -### Why only BPS patch files? -The BPS format is the successor to the IPS and UPS formats, with the added benefit of including hash checksums for verification. This helps ensure that the patch file is linked to the correct base ROM. An incorrect base ROM will result in a corrupted game. - -There are also plans to add Xdelta support for NDS hacks in the future. +### Why only BPS and Xdelta patch files? +Both formats support checksum verification so the patch is linked to the correct base ROM. An incorrect base ROM will result in a corrupted game. BPS remains supported for existing hacks; new in-browser patch creation produces Xdelta files, which is the preferred format going forward. ### How does my hack gain visibility? We highly recommend linking to your romhack's Hackdex page from PokéCommunity, Reddit, or other social media platforms. Doing so can help boost your hack's visibility and outrank those sketchy ROM sharing sites that steal many creators' hard work. diff --git a/src/app/hack/[slug]/actions.ts b/src/app/hack/[slug]/actions.ts index 29854a6..2105063 100644 --- a/src/app/hack/[slug]/actions.ts +++ b/src/app/hack/[slug]/actions.ts @@ -229,12 +229,21 @@ export async function getHackDownloads(slug: string): Promise { return runner(); } +type GetSignedPatchUrlResult = { + ok: true; + url: string; + format: Database["public"]["Enums"]["Patch Format"]; +} | { + ok: false; + error: string; +}; + export async function getSignedPatchUrl( slug: string, options?: { patchId?: number; } -): Promise<{ ok: true; url: string } | { ok: false; error: string }> { +): Promise { const supabase = await createClient(); // Get user for permission check @@ -282,7 +291,7 @@ export async function getSignedPatchUrl( // Fetch patch info const { data: patch, error: patchError } = await supabase .from("patches") - .select("id, bucket, filename, parent_hack, published, archived") + .select("id, bucket, filename, parent_hack, published, archived, format") .eq("id", selectedPatchId) .maybeSingle(); @@ -297,12 +306,12 @@ export async function getSignedPatchUrl( try { const workerUrl = buildPatchDownloadUrl(patch.filename); if (workerUrl) { - return { ok: true, url: workerUrl }; + return { ok: true, url: workerUrl, format: patch.format }; } const client = getMinioClient(); const bucket = patch.bucket || PATCHES_BUCKET; const signedUrl = await client.presignedGetObject(bucket, patch.filename, 60 * 5); - return { ok: true, url: signedUrl }; + return { ok: true, url: signedUrl, format: patch.format }; } catch (error) { console.error("Error signing patch URL:", error); return { ok: false, error: "Failed to generate download URL" }; @@ -1010,11 +1019,12 @@ export async function confirmReuploadPatchVersion( return { ok: false, error: "Patch not found" }; } - // Update patch filename + // Update patch filename and format (derived from object key extension) + const format = objectKey.toLowerCase().endsWith(".xdelta") ? "xdelta" : "bps"; const serviceClient = await createServiceClient(); const { error: updateErr } = await serviceClient .from("patches") - .update({ filename: objectKey, updated_at: new Date().toISOString() }) + .update({ filename: objectKey, format, updated_at: new Date().toISOString() }) .eq("id", patchId); if (updateErr) return { ok: false, error: updateErr.message }; diff --git a/src/app/hack/[slug]/page.tsx b/src/app/hack/[slug]/page.tsx index ca8fddb..011b5fd 100644 --- a/src/app/hack/[slug]/page.tsx +++ b/src/app/hack/[slug]/page.tsx @@ -598,7 +598,7 @@ export default async function HackDetail({ params }: HackDetailProps) { using our built-in patcher.

- By pressing "Agree and Patch", your browser will download and apply the {hack.title} .bps patch file to your legally-obtained {baseRom?.name} ROM. The patched ROM will then be automatically downloaded. + By pressing "Agree and Patch", your browser will download and apply the {hack.title} patch file to your legally-obtained {baseRom?.name} ROM. The patched ROM will then be automatically downloaded.

No pre-patched ROMs or base ROMs are hosted or distributed on this site. All patching is done locally on your device. diff --git a/src/app/hack/[slug]/versions/page.tsx b/src/app/hack/[slug]/versions/page.tsx index 8b9d05d..c9cbda8 100644 --- a/src/app/hack/[slug]/versions/page.tsx +++ b/src/app/hack/[slug]/versions/page.tsx @@ -3,7 +3,7 @@ import { createClient } from "@/utils/supabase/server"; import { canEditAsCreator, canEditAsAdmin } from "@/utils/hack"; import VersionList from "@/components/Hack/VersionList"; import DownloadPermissionSettings from "@/components/Hack/DownloadPermissionSettings"; -import PatcherVersionManager from "@/components/Hack/PatcherVersionManager"; +import PatcherVersionManager, { type Patch } from "@/components/Hack/PatcherVersionManager"; import CollapsibleCard from "@/components/Primitives/CollapsibleCard"; import Link from "next/link"; import { FaChevronLeft, FaPlus, FaStar } from "react-icons/fa6"; @@ -33,7 +33,7 @@ export default async function VersionsPage({ params }: VersionsPageProps) { // Fetch all published, non-archived patches const { data: patches } = await supabase .from("patches") - .select("id, version, created_at, updated_at, changelog, published, archived") + .select("id, version, created_at, updated_at, changelog, published, archived, format") .eq("parent_hack", slug) .eq("published", true) .eq("archived", false) @@ -44,7 +44,7 @@ export default async function VersionsPage({ params }: VersionsPageProps) { if (canEdit) { const { data: unpub } = await supabase .from("patches") - .select("id, version, created_at, updated_at, changelog, published, archived") + .select("id, version, created_at, updated_at, changelog, published, archived, format") .eq("parent_hack", slug) .eq("published", false) .eq("archived", false) @@ -52,7 +52,7 @@ export default async function VersionsPage({ params }: VersionsPageProps) { unpublishedPatches = unpub || []; } - const allPatches = [...(patches || []), ...unpublishedPatches].sort((a, b) => + const allPatches: Patch[] = [...(patches || []), ...unpublishedPatches].sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime() ); const patcherSelection = await getPatcherSelectablePatches(supabase, slug, hack.current_patch); diff --git a/src/app/submit/actions.ts b/src/app/submit/actions.ts index 8c85d82..d3a037a 100644 --- a/src/app/submit/actions.ts +++ b/src/app/submit/actions.ts @@ -8,9 +8,14 @@ import { APIEmbed } from "discord-api-types/v10"; import { slugify } from "@/utils/format"; import { checkEditPermission, checkPatchEditPermission } from "@/utils/hack"; import { getCachedTagsWithUsage, resolveTagIdsInOrder } from "@/data/tags"; +import type { PatchFormat } from "@/utils/patching"; type HackInsert = TablesInsert<"hacks">; +function patchFormatFromObjectKey(objectKey: string): PatchFormat { + return objectKey.toLowerCase().endsWith(".xdelta") ? "xdelta" : "bps"; +} + async function ensureUniqueSlug(base: string, supabase: Awaited>) { let candidate = base; let suffix = 2; @@ -173,8 +178,7 @@ export async function presignPatchAndSaveCovers(args: { slug: string; version: string; coverUrls: string[]; - // desired object key; if omitted we build from slug+version - objectKey?: string; + objectKey: string; }) { const supabase = await createClient(); const { @@ -207,15 +211,11 @@ export async function presignPatchAndSaveCovers(args: { const { error: cErr } = await supabase.from("hack_covers").insert(rows); if (cErr) return { ok: false, error: cErr.message } as const; } - - const safeVersion = args.version.replace(/[^a-zA-Z0-9._-]+/g, "-"); - const objectKey = args.objectKey || `${args.slug}-${safeVersion}.bps`; - const client = getMinioClient(); // 10 minutes to upload - const url = await client.presignedPutObject(PATCHES_BUCKET, objectKey, 60 * 10); + const url = await client.presignedPutObject(PATCHES_BUCKET, args.objectKey, 60 * 10); - return { ok: true, presignedUrl: url, objectKey } as const; + return { ok: true, presignedUrl: url, objectKey: args.objectKey } as const; } export async function confirmPatchUpload(args: { slug: string; objectKey: string; version: string, firstUpload?: boolean; publishAutomatically?: boolean }) { @@ -268,6 +268,7 @@ export async function confirmPatchUpload(args: { slug: string; objectKey: string filename: args.objectKey, version: args.version, parent_hack: args.slug, + format: patchFormatFromObjectKey(args.objectKey), }; // Set published status based on publishAutomatically flag diff --git a/src/components/Hack/HackActions.tsx b/src/components/Hack/HackActions.tsx index 804f991..b5d3d38 100644 --- a/src/components/Hack/HackActions.tsx +++ b/src/components/Hack/HackActions.tsx @@ -5,8 +5,6 @@ import StickyActionBar from "@/components/Hack/StickyActionBar"; import BaseRomErrorModal, { type BaseRomErrorModalState } from "@/components/Hack/BaseRomErrorModal"; import { useBaseRoms } from "@/contexts/BaseRomContext"; import { baseRoms } from "@/data/baseRoms"; -import BinFile from "rom-patcher-js/rom-patcher-js/modules/BinFile.js"; -import BPS from "rom-patcher-js/rom-patcher-js/modules/RomPatcher.format.bps.js"; import type { DownloadEventDetail } from "@/types/util"; import { getSignedPatchUrl, updatePatchDownloadCount } from "@/app/hack/[slug]/actions"; import { sha1Hex } from "@/utils/hash"; @@ -16,6 +14,8 @@ import { isAnyRomExtension, } from "@/utils/romFile"; import type { SelectablePatch } from "@/types/patcher"; +import { applyPatch, patchFormatFromFilename, type PatchFormat } from "@/utils/patching"; +import { createOutputSink, SaveCancelledError, type OutputSink } from "@/utils/patching/save"; interface HackActionsProps { title: string; @@ -46,9 +46,11 @@ const HackActions: React.FC = ({ const { isLinked, hasPermission, hasCached, importUploadedBlob, ensurePermission, linkRom, getFileBlob, supported } = useBaseRoms(); const [file, setFile] = React.useState(null); const [status, setStatus] = React.useState<"idle" | "ready" | "patching" | "done" | "downloading">("idle"); + const [patchProgress, setPatchProgress] = React.useState(null); const [error, setError] = React.useState(null); const [patchBlob, setPatchBlob] = React.useState(null); const [patchUrl, setPatchUrl] = React.useState(null); + const [patchFormat, setPatchFormat] = React.useState(null); const [termsAgreed, setTermsAgreed] = React.useState(false); const [romErrorModal, setRomErrorModal] = React.useState(null); const [isVerifyingRom, setIsVerifyingRom] = React.useState(false); @@ -79,6 +81,7 @@ const HackActions: React.FC = ({ setTermsAgreed(false); setPatchUrl(null); setPatchBlob(null); + setPatchFormat(null); setStatus("idle"); } @@ -126,19 +129,6 @@ const HackActions: React.FC = ({ } }, [error]); - // When patch URL is fetched and terms are agreed, automatically proceed with patching if ROM is ready - React.useEffect(() => { - if (termsAgreed && patchUrl && patchBlob && status === "idle") { - const romReady = isRomReadyForPatch(); - if (romReady) { - const timeoutId = setTimeout(() => { - onPatch(); - }, 0); - return () => clearTimeout(timeoutId); - } - } - }, [termsAgreed, patchUrl, patchBlob, file, baseRomId, isLinked, hasPermission, hasCached, status]); - async function onSelectFile(e: React.ChangeEvent) { const f = e.target.files?.[0] ?? null; setFile(null); @@ -216,7 +206,7 @@ const HackActions: React.FC = ({ } } - async function onAgreeToTerms(): Promise<{ url: string; blob: Blob } | null> { + async function onAgreeToTerms(): Promise<{ url: string; blob: Blob; format: PatchFormat } | null> { try { setError(null); setStatus("downloading"); @@ -232,6 +222,7 @@ const HackActions: React.FC = ({ } setPatchUrl(result.url); + setPatchFormat(result.format); setTermsAgreed(true); const res = await fetch(result.url); @@ -244,7 +235,7 @@ const HackActions: React.FC = ({ setStatus("idle"); } - return { url: result.url, blob }; + return { url: result.url, blob, format: result.format }; } catch (e: any) { setError(e?.message || "Failed to fetch patch URL"); setStatus("idle"); @@ -254,20 +245,63 @@ const HackActions: React.FC = ({ } async function onPatch() { + let outputSink: OutputSink | null = null; + const discardSink = async () => { + if (!outputSink) return; + const sink = outputSink; + outputSink = null; + try { + await sink.abort(); + } catch { + // ignore abort failures when discarding an unused sink + } + }; + try { setError(null); + // Create xdelta sink during the click gesture, before any other awaits that + // would drop transient user activation (terms download, ROM permission, etc.). + const outExt = platform ? platform.toLowerCase() : "bin"; + const outputName = `${title} (${selectedVersion}).${outExt}`; + const earlyFormat = patchFormat ?? patchFormatFromFilename(selectedFilename); + if (earlyFormat === "xdelta" && status !== "patching") { + try { + outputSink = await createOutputSink(outputName); + } catch (e: unknown) { + if (e instanceof SaveCancelledError) { + setStatus("idle"); + setPatchProgress(null); + return; + } + throw e; + } + } + let url = patchUrl; let blob = patchBlob; + let format = patchFormat; - if (!termsAgreed || !url || !blob) { + if (!termsAgreed || !url || !blob || !format) { const downloaded = await onAgreeToTerms(); - if (!downloaded) return; + if (!downloaded) { + await discardSink(); + return; + } url = downloaded.url; blob = downloaded.blob; + format = downloaded.format; const romReady = isRomReadyForPatch(); - if (!romReady) return; + if (!romReady) { + await discardSink(); + return; + } + } + + // BPS uses rom-patcher save; drop any unused early sink. + if (format !== "xdelta") { + await discardSink(); } if (status === "patching") { @@ -276,39 +310,56 @@ const HackActions: React.FC = ({ let baseFile = file; if (!baseFile) { - if (!isLinked(baseRomId) && !hasCached(baseRomId)) return; + if (!isLinked(baseRomId) && !hasCached(baseRomId)) { + await discardSink(); + return; + } if (!hasCached(baseRomId)) { const perm = await ensurePermission(baseRomId, true); - if (perm !== "granted") return; + if (perm !== "granted") { + await discardSink(); + return; + } } const linkedFile = await getFileBlob(baseRomId); - if (!linkedFile) return; + if (!linkedFile) { + await discardSink(); + return; + } baseFile = linkedFile; } setStatus("patching"); + setPatchProgress(null); - await Promise.all([ - new Promise((r) => setTimeout(r, 1000)), - (async () => { - const [romBuf, patchBuf] = await Promise.all([ - baseFile.arrayBuffer(), - blob.arrayBuffer(), - ]); - - const romBin = new BinFile(romBuf); - romBin.fileName = baseFile.name + (platform ? `.${platform.toLowerCase()}` : ""); - const patchBin = new BinFile(patchBuf); - - const patch = BPS.fromFile(patchBin); - const patchedRom = patch.apply(romBin); - - const outExt = platform ? platform.toLowerCase() : 'bin'; - const outputName = `${title} (${selectedVersion}).${outExt}`; - patchedRom.fileName = outputName; - patchedRom.save(); - })(), - ]); + try { + await Promise.all([ + new Promise((r) => setTimeout(r, 1000)), + (async () => { + // BPS ignores outputSink; xdelta uses the gesture-created sink. + const sink = outputSink; + outputSink = null; + await applyPatch({ + format, + baseFile, + patchBlob: blob, + outputName, + sourceName: baseFile.name + (platform ? `.${platform.toLowerCase()}` : ""), + outputSink: sink ?? undefined, + onProgress: ({ bytesOut }) => setPatchProgress(bytesOut), + }); + })(), + ]); + } catch (e: unknown) { + if (e instanceof SaveCancelledError) { + setStatus("idle"); + setPatchProgress(null); + return; + } + throw e; + } finally { + setPatchProgress(null); + } setStatus("done"); @@ -337,8 +388,10 @@ const HackActions: React.FC = ({ console.error(e); } } catch (e: any) { + await discardSink(); setError(e?.message || "Failed to patch ROM"); setStatus("idle"); + setPatchProgress(null); console.error(e); } } @@ -365,6 +418,7 @@ const HackActions: React.FC = ({ onUploadChange={onSelectFile} termsAgreed={termsAgreed} isVerifyingRom={isVerifyingRom} + patchProgress={patchProgress} /> {romErrorModal && ( ("bps"); const [patchFile, setPatchFile] = React.useState(null); const [genStatus, setGenStatus] = React.useState<"idle" | "generating" | "ready" | "error">("idle"); const [genError, setGenError] = React.useState(""); + const [checksumStatus, setChecksumStatus] = React.useState<"idle" | "validating" | "valid" | "invalid" | "unknown">("idle"); + const [checksumError, setChecksumError] = React.useState(""); const [submitting, setSubmitting] = React.useState(false); const [error, setError] = React.useState(""); const [publishAutomatically, setPublishAutomatically] = React.useState(false); @@ -36,7 +39,6 @@ export default function HackPatchForm(props: HackPatchFormProps) { const patchInputRef = React.useRef(null); const modifiedRomInputRef = React.useRef(null); - const supabase = createClient(); const baseRomEntry = React.useMemo(() => baseRoms.find(r => r.id === baseRomId) || null, [baseRomId]); const baseRomPlatform = baseRomEntry?.platform; const baseRomName = baseRomEntry?.name; @@ -48,8 +50,13 @@ export default function HackPatchForm(props: HackPatchFormProps) { const isVersionTaken = version.trim() && existingVersions.includes(version.trim()); const canSubmit = React.useMemo(() => { - return !!version.trim() && ((!!patchFile && patchMode === "bps") || (patchMode === "rom" && genStatus === "ready")) && !isVersionTaken && !submitting; - }, [version, patchFile, patchMode, genStatus, isVersionTaken, submitting]); + return !!version.trim() + && ((!!patchFile && patchMode === "bps") || (patchMode === "rom" && genStatus === "ready")) + && !isVersionTaken + && !submitting + && checksumStatus !== "invalid" + && checksumStatus !== "validating"; + }, [version, patchFile, patchMode, genStatus, isVersionTaken, submitting, checksumStatus]); React.useEffect(() => { versionInputRef.current?.focus(); @@ -76,6 +83,8 @@ export default function HackPatchForm(props: HackPatchFormProps) { setPatchFile(null); setGenStatus("idle"); setGenError(""); + setChecksumStatus("idle"); + setChecksumError(""); patchInputRef.current && (patchInputRef.current.value = ""); modifiedRomInputRef.current && (modifiedRomInputRef.current.value = ""); }, [patchMode]); @@ -127,14 +136,14 @@ export default function HackPatchForm(props: HackPatchFormProps) { return; } } - const [origBuf, modBuf] = await Promise.all([baseFile.arrayBuffer(), mod.arrayBuffer()]); - const origBin = new BinFile(origBuf); - const modBin = new BinFile(modBuf); - const deltaMode = origBin.fileSize <= 4194304; - const patch = BPS.buildFromRoms(origBin, modBin, deltaMode); const fname = `${slug}-${(version || "patch").replace(/[^a-zA-Z0-9._-]+/g, "-")}`; - const patchBin = patch.export(fname); - const out = new File([patchBin._u8array], `${fname}.bps`, { type: 'application/octet-stream' }); + const { result, patch } = await encodeXdelta({ sourceFile: baseFile, targetFile: mod }); + if (!result.ok || !patch) { + setGenStatus("error"); + setGenError(friendlyXdeltaError(result)); + return; + } + const out = new File([patch], `${fname}.xdelta`, { type: 'application/octet-stream' }); setPatchFile(out); setGenStatus("ready"); } catch (err: any) { @@ -143,12 +152,93 @@ export default function HackPatchForm(props: HackPatchFormProps) { } } + async function onUploadPatch(e: React.ChangeEvent) { + try { + setChecksumStatus("validating"); + setChecksumError(""); + + const patch = e.target.files?.[0] || null; + if (!patch) { + setChecksumStatus("idle"); + setChecksumError(""); + setPatchFile(null); + return; + } + + if (patchFormatFromFilename(patch.name) === "xdelta") { + const baseFile = baseRomId ? await getFileBlob(baseRomId) : null; + if (!baseFile) { + setChecksumStatus("unknown"); + setChecksumError("Cannot validate without the base ROM on this device. Proceed at your own risk, or upload your modified ROM instead."); + setPatchFile(patch); + return; + } + const result = await trialDecodeXdelta({ sourceFile: baseFile, patchBlob: patch }); + if (result.ok && result.hasChecksums === true) { + setChecksumStatus("valid"); + setChecksumError(""); + setPatchFile(patch); + return; + } + if (!result.ok) { + const msg = (result.errorMessage ?? "").toLowerCase(); + setChecksumStatus("invalid"); + setChecksumError( + msg.includes("checksum") + ? "Checksum validation failed. The patch file is not compatible with the selected base ROM." + : friendlyXdeltaError(result) + ); + setPatchFile(null); + return; + } + setChecksumStatus("unknown"); + setChecksumError("This patch has no embedded checksums. Proceed at your own risk, or upload your modified ROM instead."); + setPatchFile(patch); + return; + } + + if (!baseRomEntry) { + setChecksumStatus("unknown"); + setChecksumError("A checksum is not available to validate this patch file. Proceed at your own risk, or upload your modified ROM instead."); + setPatchFile(patch); + return; + } + + const bps = BPS.fromFile(new BinFile(await patch.arrayBuffer())); + if (bps.sourceChecksum === 0 || bps.sourceChecksum === undefined) { + setChecksumStatus("unknown"); + setChecksumError("A checksum is not available to validate this patch file. Proceed at your own risk, or upload your modified ROM instead."); + setPatchFile(patch); + return; + } + + const baseRomChecksum = parseInt(baseRomEntry.crc32, 16); + if (bps.sourceChecksum !== baseRomChecksum) { + setChecksumStatus("invalid"); + setChecksumError("Checksum validation failed. The patch file is not compatible with the selected base ROM."); + setPatchFile(null); + return; + } + + setChecksumStatus("valid"); + setChecksumError(""); + setPatchFile(patch); + } catch (err: any) { + setChecksumStatus("unknown"); + setChecksumError(err?.message || "Failed to validate patch file."); + setPatchFile(e.target.files?.[0] || null); + } + } + const onSubmit = async () => { if (!canSubmit) return; setSubmitting(true); setError(""); try { - const presigned = await presignNewPatchVersion({ slug, version: version.trim() }); + const safeVersion = version.trim().replace(/[^a-zA-Z0-9._-]+/g, "-"); + const patchExt = patchFormatFromFilename(patchFile?.name) === "xdelta" ? "xdelta" : "bps"; + const objectKey = `${slug}-${safeVersion}.${patchExt}`; + const presigned = await presignNewPatchVersion({ slug, version: version.trim(), objectKey }); if (!presigned.ok) throw new Error(presigned.error || 'Failed to presign'); await fetch(presigned.presignedUrl!, { method: 'PUT', body: patchFile!, headers: { 'Content-Type': 'application/octet-stream' } }); const finalized = await confirmPatchUpload({ slug, objectKey: presigned.objectKey!, version: version.trim(), publishAutomatically }); @@ -206,14 +296,14 @@ export default function HackPatchForm(props: HackPatchFormProps) { onClick={() => setPatchMode("bps")} className={`rounded-md rounded-r-none px-3 py-1.5 text-xs border-l-1 border-y-1 ${patchMode === "bps" ? "bg-[var(--surface-2)] border-[var(--border)]" : "text-foreground/70 border-[var(--border)]"}`} > - Upload .bps + Upload .bps/.xdelta @@ -221,12 +311,16 @@ export default function HackPatchForm(props: HackPatchFormProps) {

setPatchFile(e.target.files?.[0] || null)} + onChange={onUploadPatch} type="file" - accept=".bps" + accept=".bps,.xdelta" className="rounded-md bg-[var(--surface-2)] px-3 py-2 text-sm italic text-foreground/50 ring-1 ring-inset ring-[var(--border)] file:bg-black/10 dark:file:bg-[var(--surface-2)] file:text-foreground/80 file:text-sm file:font-medium file:not-italic file:rounded-md file:border-0 file:px-3 file:py-2 file:mr-2 file:cursor-pointer" /> -

Upload a BPS patch file.

+

Upload a .bps or .xdelta patch file.

+ {checksumStatus === "validating" &&
Validating checksum…
} + {checksumStatus === "valid" &&
Checksum valid.
} + {checksumStatus === "invalid" && !!checksumError &&
{checksumError}
} + {checksumStatus === "unknown" && !!checksumError &&
{checksumError}
}
)} @@ -262,7 +356,7 @@ export default function HackPatchForm(props: HackPatchFormProps) { onChange={onUploadModifiedRom} className="rounded-md bg-[var(--surface-2)] px-3 py-2 text-sm ring-1 ring-inset ring-[var(--border)] disabled:opacity-50 disabled:cursor-not-allowed" /> -

We'll generate a .bps patch on-device. No ROMs are uploaded.

+

We'll generate a .xdelta patch on-device. No ROMs are uploaded.

{genStatus === "generating" &&
Generating patch…
} {genStatus === "ready" && patchFile &&
Patch ready: {patchFile.name}
} {genStatus === "error" && !!genError &&
{genError}
} @@ -315,5 +409,3 @@ export default function HackPatchForm(props: HackPatchFormProps) { ); } - - diff --git a/src/components/Hack/HackSubmitForm.tsx b/src/components/Hack/HackSubmitForm.tsx index 22ef03b..f64c910 100644 --- a/src/components/Hack/HackSubmitForm.tsx +++ b/src/components/Hack/HackSubmitForm.tsx @@ -25,6 +25,8 @@ import BPS from "rom-patcher-js/rom-patcher-js/modules/RomPatcher.format.bps.js" import { sha1Hex } from "@/utils/hash"; import { platformAccept, setDraftCovers, getDraftCovers, deleteDraftCovers } from "@/utils/idb"; import { slugify, sortOrderedTags } from "@/utils/format"; +import { patchFormatFromFilename } from "@/utils/patching"; +import { encodeXdelta, trialDecodeXdelta, friendlyXdeltaError } from "@/utils/patching/xdelta"; import type { CatalogTagRow } from "@/types/catalogTag"; import { HACK_FORM_DESCRIPTION_PLACEHOLDER } from "./hackFormConstants"; @@ -179,6 +181,8 @@ export default function HackSubmitForm({ setPatchFile(null); setGenStatus("idle"); setGenError(""); + setChecksumStatus("idle"); + setChecksumError(""); patchInputRef.current && (patchInputRef.current.value = ""); modifiedRomInputRef.current && (modifiedRomInputRef.current.value = ""); }, [patchMode]); @@ -431,7 +435,7 @@ export default function HackSubmitForm({ const step1Valid = !!title.trim() && !!platform && !!baseRom.trim() && !!language.trim() && !!completionStatus.trim() && (isArchive ? !!originalAuthor.trim() : true); const step2Valid = (isArchive ? true : !!version.trim()) && !!summary.trim() && !summaryTooLong && !!description.trim() && tags.length > 0; const step3Valid = (newCoverFiles.length > 0) && !overLimit && coverErrors.length === 0 && (!boxArt.trim() || urlLike(boxArt)) && allSocialValid; - const isValid = step1Valid && step2Valid && step3Valid && (isArchive ? true : !!patchFile); + const isValid = step1Valid && step2Valid && step3Valid && (isArchive ? true : !!patchFile) && checksumStatus !== "invalid" && checksumStatus !== "validating"; const onSubmit = async () => { if (!isValid || submitting) return; @@ -484,7 +488,10 @@ export default function HackSubmitForm({ window.location.href = `/hack/${prepared.slug}`; } else { console.log('[HackSubmitForm] Getting patch upload URL...'); - const presigned = await presignPatchAndSaveCovers({ slug: prepared.slug, version, coverUrls: uploadedCoverUrls }); + const safeVersion = version.replace(/[^a-zA-Z0-9._-]+/g, "-"); + const patchExt = patchFormatFromFilename(patchFile?.name) === "xdelta" ? "xdelta" : "bps"; + const objectKey = `${prepared.slug}-${safeVersion}.${patchExt}`; + const presigned = await presignPatchAndSaveCovers({ slug: prepared.slug, version, coverUrls: uploadedCoverUrls, objectKey }); if (!presigned.ok) throw new Error(presigned.error || 'Failed to presign'); if (patchFile) { @@ -567,14 +574,14 @@ export default function HackSubmitForm({ return; } } - const [origBuf, modBuf] = await Promise.all([baseFile.arrayBuffer(), mod.arrayBuffer()]); - const origBin = new BinFile(origBuf); - const modBin = new BinFile(modBuf); - const deltaMode = origBin.fileSize <= 4194304; - const patch = BPS.buildFromRoms(origBin, modBin, deltaMode); const fileName = slug || title || "patch"; - const patchBin = patch.export(fileName); - const out = new File([patchBin._u8array], `${fileName}.bps`, { type: 'application/octet-stream' }); + const { result, patch } = await encodeXdelta({ sourceFile: baseFile, targetFile: mod }); + if (!result.ok || !patch) { + setGenStatus("error"); + setGenError(friendlyXdeltaError(result)); + return; + } + const out = new File([patch], `${fileName}.xdelta`, { type: 'application/octet-stream' }); setPatchFile(out); setGenStatus("ready"); } catch (err: any) { @@ -596,9 +603,42 @@ export default function HackSubmitForm({ return; } + if (patchFormatFromFilename(patch.name) === "xdelta") { + const baseFile = baseRom ? await getFileBlob(baseRom) : null; + if (!baseFile) { + setChecksumStatus("unknown"); + setChecksumError("Cannot validate without the base ROM on this device. Proceed at your own risk, or upload your modified ROM instead."); + setPatchFile(patch); + return; + } + const result = await trialDecodeXdelta({ sourceFile: baseFile, patchBlob: patch }); + if (result.ok && result.hasChecksums === true) { + setChecksumStatus("valid"); + setChecksumError(""); + setPatchFile(patch); + return; + } + if (!result.ok) { + const msg = (result.errorMessage ?? "").toLowerCase(); + setChecksumStatus("invalid"); + setChecksumError( + msg.includes("checksum") + ? "Checksum validation failed. The patch file is not compatible with the selected base ROM." + : friendlyXdeltaError(result) + ); + setPatchFile(null); + return; + } + setChecksumStatus("unknown"); + setChecksumError("This patch has no embedded checksums. Proceed at your own risk, or upload your modified ROM instead."); + setPatchFile(patch); + return; + } + if (!baseRomEntry) { setChecksumStatus("unknown"); setChecksumError("A checksum is not available to validate this patch file. Proceed at your own risk, or upload your modified ROM instead."); + setPatchFile(patch); return; } @@ -607,6 +647,7 @@ export default function HackSubmitForm({ if (bps.sourceChecksum === 0 || bps.sourceChecksum === undefined) { setChecksumStatus("unknown"); setChecksumError("A checksum is not available to validate this patch file. Proceed at your own risk, or upload your modified ROM instead."); + setPatchFile(patch); return; } @@ -614,18 +655,19 @@ export default function HackSubmitForm({ if (bps.sourceChecksum !== baseRomChecksum) { setChecksumStatus("invalid"); setChecksumError("Checksum validation failed. The patch file is not compatible with the selected base ROM."); + setPatchFile(null); return; } // All checks passed, set the checksum status to valid setChecksumStatus("valid"); setChecksumError(""); - setPatchFile(patch); } catch (err: any) { setChecksumStatus("unknown"); setChecksumError(err?.message || "Failed to validate patch file."); + setPatchFile(e.target.files?.[0] || null); } } @@ -1205,14 +1247,14 @@ https://discord.gg/example`} onClick={() => setPatchMode("bps")} className={`rounded-md rounded-r-none px-3 py-1.5 text-xs border-l-1 border-y-1 ${patchMode === "bps" ? "bg-[var(--surface-2)] border-[var(--border)]" : "text-foreground/70 border-[var(--border)]"}`} > - Upload .bps + Upload .bps/.xdelta @@ -1222,10 +1264,10 @@ https://discord.gg/example`} ref={patchInputRef} onChange={onUploadPatch} type="file" - accept=".bps" + accept=".bps,.xdelta" className="rounded-md bg-[var(--surface-2)] px-3 py-2 text-sm italic text-foreground/50 ring-1 ring-inset ring-[var(--border)] file:bg-black/10 dark:file:bg-[var(--surface-2)] file:text-foreground/80 file:text-sm file:font-medium file:not-italic file:rounded-md file:border-0 file:px-3 file:py-2 file:mr-2 file:cursor-pointer" /> -

Upload a BPS patch file.

+

Upload a .bps or .xdelta patch file.

{checksumStatus === "validating" &&
Validating checksum…
} {checksumStatus === "valid" &&
Checksum valid.
} {checksumStatus === "invalid" && !!checksumError &&
{checksumError}
} @@ -1265,7 +1307,7 @@ https://discord.gg/example`} onChange={onUploadModifiedRom} className="rounded-md bg-[var(--surface-2)] px-3 py-2 text-sm ring-1 ring-inset ring-[var(--border)] disabled:opacity-50 disabled:cursor-not-allowed" /> -

We'll generate a .bps patch on-device. No ROMs are uploaded.

+

We'll generate a .xdelta patch on-device. No ROMs are uploaded.

{genStatus === "generating" &&
Generating patch…
} {genStatus === "ready" && patchFile &&
Patch ready: {patchFile.name}
} {genStatus === "error" && !!genError &&
{genError}
} diff --git a/src/components/Hack/PatcherVersionManager.tsx b/src/components/Hack/PatcherVersionManager.tsx index 74059c9..27e5443 100644 --- a/src/components/Hack/PatcherVersionManager.tsx +++ b/src/components/Hack/PatcherVersionManager.tsx @@ -8,10 +8,11 @@ import PatcherVersionSettings from "@/components/Hack/PatcherVersionSettings"; import VersionList from "@/components/Hack/VersionList"; import { CUSTOM_VERSION_NAME_MAX_LENGTH, suggestCustomVersionName } from "@/utils/patches/hack-display-version"; import type { PatchesDownloadPermission } from "@/components/Hack/DownloadPermissionSettings"; +import type { PatchFormat } from "@/utils/patching"; type PatcherOption = "latest" | "custom"; -interface Patch { +export interface Patch { id: number; version: string; created_at: string; @@ -19,6 +20,7 @@ interface Patch { changelog: string | null; published: boolean; archived: boolean; + format: PatchFormat; } interface PatcherVersionManagerProps { diff --git a/src/components/Hack/StickyActionBar.tsx b/src/components/Hack/StickyActionBar.tsx index 8a01165..26d29e2 100644 --- a/src/components/Hack/StickyActionBar.tsx +++ b/src/components/Hack/StickyActionBar.tsx @@ -27,6 +27,7 @@ interface StickyActionBarProps { onUploadChange: (e: React.ChangeEvent) => void; termsAgreed: boolean; isVerifyingRom?: boolean; + patchProgress?: number | null; } export default function StickyActionBar({ @@ -49,6 +50,7 @@ export default function StickyActionBar({ onUploadChange, termsAgreed, isVerifyingRom = false, + patchProgress = null, }: StickyActionBarProps) { const [mounted, setMounted] = React.useState(false); React.useEffect(() => setMounted(true), []); @@ -159,7 +161,7 @@ export default function StickyActionBar({ ? "bg-amber-600/60 text-white ring-amber-700/80 dark:bg-amber-500/50 dark:text-amber-100 dark:ring-amber-400/90" : "bg-red-600/60 text-white ring-red-700/80 dark:bg-red-500/50 dark:text-red-100 dark:ring-red-400/90" }`}> - {romReady ? (filename ?? ".bps file ready") : isLinked ? "Permission needed" : "Base ROM needed"} + {romReady ? (filename ?? "patch file ready") : isLinked ? "Permission needed" : "Base ROM needed"} )} {!baseRomsLoading && !romReady && !isLinked && ( @@ -207,7 +209,11 @@ export default function StickyActionBar({ className={`shine-wrap btn-premium data-[ready=false]:hidden! h-11 md:h-9 w-full md:min-w-46 ${!termsAgreed || status === 'downloading' ? "md:w-32" : "md:w-auto"} text-base md:text-sm font-semibold cursor-pointer disabled:cursor-not-allowed disabled:opacity-70 ${romReady && status !== 'downloading' && status !== 'ready' && termsAgreed ? "mt-6 md:mt-0" : ""}`} > { - status === "patching" ? "Patching…" : + status === "patching" ? ( + patchProgress != null && patchProgress > 0 + ? `Patching… (${(patchProgress / (1024 * 1024)).toFixed(0)} MB)` + : "Patching…" + ) : status === "downloading" ? "Downloading…" : status === "done" ? ( patchAgainReady ? "Patch Again" : "Patched" diff --git a/src/components/Hack/VersionActions.tsx b/src/components/Hack/VersionActions.tsx index c221c75..83fde20 100644 --- a/src/components/Hack/VersionActions.tsx +++ b/src/components/Hack/VersionActions.tsx @@ -26,6 +26,8 @@ import { sha1Hex } from "@/utils/hash"; import { baseRoms, type BaseRom } from "@/data/baseRoms"; import { platformAccept } from "@/utils/idb"; import { useBaseRoms } from "@/contexts/BaseRomContext"; +import { patchFormatFromFilename } from "@/utils/patching"; +import { encodeXdelta, trialDecodeXdelta, friendlyXdeltaError } from "@/utils/patching/xdelta"; interface Patch { id: number; @@ -116,6 +118,16 @@ export default function VersionActions({ } }, [showDeleteModal, showRestoreModal, showRollbackModal, showPublishModal, showReuploadModal]); + useEffect(() => { + setReuploadFile(null); + setChecksumStatus("idle"); + setChecksumError(""); + setGenStatus("idle"); + setGenError(""); + if (patchInputRef.current) patchInputRef.current.value = ""; + if (modifiedRomInputRef.current) modifiedRomInputRef.current.value = ""; + }, [patchMode]); + const handleDownload = async () => { try { const result = await getPatchDownloadUrl(patch.id); @@ -210,6 +222,38 @@ export default function VersionActions({ return; } + if (patchFormatFromFilename(patchFile.name) === "xdelta") { + const baseFile = baseRom ? await getFileBlob(baseRom) : null; + if (!baseFile) { + setChecksumStatus("unknown"); + setChecksumError("Cannot validate without the base ROM on this device. Proceed at your own risk, or upload your modified ROM instead."); + setReuploadFile(patchFile); + return; + } + const result = await trialDecodeXdelta({ sourceFile: baseFile, patchBlob: patchFile }); + if (result.ok && result.hasChecksums === true) { + setChecksumStatus("valid"); + setChecksumError(""); + setReuploadFile(patchFile); + return; + } + if (!result.ok) { + const msg = (result.errorMessage ?? "").toLowerCase(); + setChecksumStatus("invalid"); + setChecksumError( + msg.includes("checksum") + ? "Checksum validation failed. The patch file is not compatible with the selected base ROM." + : friendlyXdeltaError(result) + ); + setReuploadFile(null); + return; + } + setChecksumStatus("unknown"); + setChecksumError("This patch has no embedded checksums. Proceed at your own risk, or upload your modified ROM instead."); + setReuploadFile(patchFile); + return; + } + if (!baseRomEntry) { setChecksumStatus("unknown"); setChecksumError("A checksum is not available to validate this patch file. Proceed at your own risk, or upload your modified ROM instead."); @@ -241,7 +285,7 @@ export default function VersionActions({ } catch (err: any) { setChecksumStatus("unknown"); setChecksumError(err?.message || "Failed to validate patch file."); - setReuploadFile(null); + setReuploadFile(e.target.files?.[0] || null); } } @@ -303,14 +347,14 @@ export default function VersionActions({ } } - const [origBuf, modBuf] = await Promise.all([baseFile.arrayBuffer(), mod.arrayBuffer()]); - const origBin = new BinFile(origBuf); - const modBin = new BinFile(modBuf); - const deltaMode = origBin.fileSize <= 4194304; - const patch = BPS.buildFromRoms(origBin, modBin, deltaMode); const fileName = hackSlug || "patch"; - const patchBin = patch.export(fileName); - const out = new File([patchBin._u8array], `${fileName}.bps`, { type: 'application/octet-stream' }); + const { result, patch } = await encodeXdelta({ sourceFile: baseFile, targetFile: mod }); + if (!result.ok || !patch) { + setGenStatus("error"); + setGenError(friendlyXdeltaError(result)); + return; + } + const out = new File([patch], `${fileName}.xdelta`, { type: 'application/octet-stream' }); setReuploadFile(out); setGenStatus("ready"); } catch (err: any) { @@ -329,7 +373,8 @@ export default function VersionActions({ setReuploadError(null); try { const safeVersion = patch.version.replace(/[^a-zA-Z0-9._-]+/g, "-"); - const objectKey = `${hackSlug}-${safeVersion}-reupload-${Date.now()}.bps`; + const patchExt = patchFormatFromFilename(reuploadFile.name) === "xdelta" ? "xdelta" : "bps"; + const objectKey = `${hackSlug}-${safeVersion}-reupload-${Date.now()}.${patchExt}`; const presignResult = await reuploadPatchVersion(hackSlug, patch.id, objectKey); if (!presignResult.ok) { @@ -710,14 +755,14 @@ export default function VersionActions({ onClick={() => setPatchMode("bps")} className={`rounded-md rounded-r-none px-3 py-1.5 text-xs border-l-1 border-y-1 ${patchMode === "bps" ? "bg-[var(--surface-2)] border-[var(--border)]" : "text-foreground/70 border-[var(--border)]"}`} > - Upload .bps + Upload .bps/.xdelta @@ -727,10 +772,10 @@ export default function VersionActions({ ref={patchInputRef} onChange={onUploadPatch} type="file" - accept=".bps" + accept=".bps,.xdelta" className="rounded-md bg-[var(--surface-2)] px-3 py-2 text-sm italic text-foreground/50 ring-1 ring-inset ring-[var(--border)] file:bg-black/10 dark:file:bg-[var(--surface-2)] file:text-foreground/80 file:text-sm file:font-medium file:not-italic file:rounded-md file:border-0 file:px-3 file:py-2 file:mr-2 file:cursor-pointer" /> -

Upload a BPS patch file.

+

Upload a .bps or .xdelta patch file.

{checksumStatus === "validating" &&
Validating checksum…
} {checksumStatus === "valid" &&
Checksum valid.
} {checksumStatus === "invalid" && !!checksumError &&
{checksumError}
} @@ -776,7 +821,7 @@ export default function VersionActions({ onChange={onUploadModifiedRom} className="rounded-md bg-[var(--surface-2)] px-3 py-2 text-sm ring-1 ring-inset ring-[var(--border)] disabled:opacity-50 disabled:cursor-not-allowed" /> -

We'll generate a .bps patch on-device. No ROMs are uploaded.

+

We'll generate a .xdelta patch on-device. No ROMs are uploaded.

{genStatus === "generating" &&
Generating patch…
} {genStatus === "ready" && reuploadFile &&
Patch ready: {reuploadFile.name}
} {genStatus === "error" && !!genError &&
{genError}
} @@ -791,7 +836,7 @@ export default function VersionActions({
); diff --git a/src/types/db.ts b/src/types/db.ts index 51ceb89..6316d96 100644 --- a/src/types/db.ts +++ b/src/types/db.ts @@ -390,6 +390,7 @@ export type Database = { changelog: string | null created_at: string filename: string + format: Database["public"]["Enums"]["Patch Format"] id: number parent_hack: string | null published: boolean @@ -405,6 +406,7 @@ export type Database = { changelog?: string | null created_at?: string filename: string + format?: Database["public"]["Enums"]["Patch Format"] id?: number parent_hack?: string | null published?: boolean @@ -420,6 +422,7 @@ export type Database = { changelog?: string | null created_at?: string filename?: string + format?: Database["public"]["Enums"]["Patch Format"] id?: number parent_hack?: string | null published?: boolean @@ -522,6 +525,7 @@ export type Database = { } Enums: { "Completion Status": "Complete" | "Demo" | "Alpha" | "Beta" + "Patch Format": "bps" | "xdelta" "Patches Download Permission": "None" | "Current" | "All" "Tag Categories": | "Pokédex" @@ -665,6 +669,7 @@ export const Constants = { public: { Enums: { "Completion Status": ["Complete", "Demo", "Alpha", "Beta"], + "Patch Format": ["bps", "xdelta"], "Patches Download Permission": ["None", "Current", "All"], "Tag Categories": [ "Pokédex", diff --git a/src/types/file-system-access.d.ts b/src/types/file-system-access.d.ts new file mode 100644 index 0000000..d2fe6e3 --- /dev/null +++ b/src/types/file-system-access.d.ts @@ -0,0 +1,23 @@ +/** Minimal File System Access API typings (not in TypeScript's default DOM lib). */ + +interface FileSystemWritableFileStream extends WritableStream { + write(data: BufferSource | Blob | string): Promise; + close(): Promise; + abort(): Promise; +} + +interface FileSystemFileHandle { + createWritable(options?: { keepExistingData?: boolean }): Promise; +} + +interface SaveFilePickerOptions { + suggestedName?: string; + types?: Array<{ + description?: string; + accept: Record; + }>; +} + +interface Window { + showSaveFilePicker?(options?: SaveFilePickerOptions): Promise; +} diff --git a/src/utils/patching/index.ts b/src/utils/patching/index.ts new file mode 100644 index 0000000..75fa03a --- /dev/null +++ b/src/utils/patching/index.ts @@ -0,0 +1,76 @@ +import BinFile from "rom-patcher-js/rom-patcher-js/modules/BinFile.js"; +import BPS from "rom-patcher-js/rom-patcher-js/modules/RomPatcher.format.bps.js"; +import { createOutputSink, SaveCancelledError, type OutputSink } from "@/utils/patching/save"; +import { decodeXdelta, friendlyXdeltaError } from "@/utils/patching/xdelta"; +import type { Database } from "@/types/db"; + +export type PatchFormat = Database["public"]["Enums"]["Patch Format"]; + +export function patchFormatFromFilename(filename: string | null | undefined): PatchFormat { + if (filename && filename.toLowerCase().endsWith(".xdelta")) { + return "xdelta"; + } + return "bps"; +} + +export async function applyPatch(opts: { + format: PatchFormat; + baseFile: File; + patchBlob: Blob; + outputName: string; + sourceName?: string; + /** Pre-created sink (xdelta). Prefer creating during the user-gesture before other awaits. */ + outputSink?: OutputSink; + onProgress?: (p: { bytesOut: number }) => void; +}): Promise { + const { format, baseFile, patchBlob, outputName, sourceName, outputSink, onProgress } = opts; + + if (format === "bps") { + const [romBuf, patchBuf] = await Promise.all([ + baseFile.arrayBuffer(), + patchBlob.arrayBuffer(), + ]); + + const romBin = new BinFile(romBuf); + romBin.fileName = sourceName ?? baseFile.name; + const patchBin = new BinFile(patchBuf); + + const patch = BPS.fromFile(patchBin); + const patchedRom = patch.apply(romBin); + patchedRom.fileName = outputName; + patchedRom.save(); + return; + } + + // format === "xdelta" + const sink = outputSink ?? await createOutputSink(outputName); + try { + const result = await decodeXdelta({ + sourceFile: baseFile, + patchBlob, + onChunk: (bytes) => sink.write(bytes), + onProgress: onProgress + ? (p) => { + onProgress({ bytesOut: p.bytesOut }); + } + : undefined, + }); + + if (!result.ok) { + await sink.abort(); + throw new Error(friendlyXdeltaError(result)); + } + + await sink.close(); + } catch (error) { + if (error instanceof SaveCancelledError) { + throw error; + } + try { + await sink.abort(); + } catch { + // ignore abort failures after a prior error + } + throw error; + } +} diff --git a/src/utils/patching/save.ts b/src/utils/patching/save.ts new file mode 100644 index 0000000..e3e81e2 --- /dev/null +++ b/src/utils/patching/save.ts @@ -0,0 +1,92 @@ +export class SaveCancelledError extends Error { + constructor(message = "Save cancelled") { + super(message); + this.name = "SaveCancelledError"; + } +} + +export type OutputSink = { + write(bytes: Uint8Array): Promise; + close(): Promise; + abort(): Promise; + streaming: boolean; +}; + +function isAbortError(error: unknown): boolean { + return ( + (error instanceof DOMException && error.name === "AbortError") || + (error instanceof Error && error.name === "AbortError") + ); +} + +function triggerBlobDownload(blob: Blob, fileName: string): void { + const blobUrl = URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = blobUrl; + a.download = fileName; + document.body.appendChild(a); + a.click(); + // Defer cleanup so the browser can start the download before the URL is revoked. + setTimeout(() => { + URL.revokeObjectURL(blobUrl); + a.remove(); + }, 1000); +} + +/** Normalize worker-transferred views for BlobPart / BufferSource (TS 5.9 ArrayBuffer typing). */ +function asArrayBufferView(bytes: Uint8Array): Uint8Array { + return bytes.buffer instanceof ArrayBuffer + ? (bytes as Uint8Array) + : new Uint8Array(bytes); +} + +function createBlobSink(fileName: string): OutputSink { + const chunks: BlobPart[] = []; + return { + streaming: false, + async write(bytes) { + chunks.push(asArrayBufferView(bytes)); + }, + async close() { + triggerBlobDownload(new Blob(chunks), fileName); + }, + async abort() { + chunks.length = 0; + }, + }; +} + +async function tryCreateStreamingSink(fileName: string): Promise { + if (typeof window.showSaveFilePicker !== "function") { + return null; + } + + try { + const handle = await window.showSaveFilePicker({ suggestedName: fileName }); + const writable = await handle.createWritable(); + return { + streaming: true, + async write(bytes) { + await writable.write(asArrayBufferView(bytes)); + }, + async close() { + await writable.close(); + }, + async abort() { + await writable.abort(); + }, + }; + } catch (error) { + if (isAbortError(error)) { + throw new SaveCancelledError(); + } + // No user activation, unsupported, or other picker/writable failure → Blob sink. + return null; + } +} + +export async function createOutputSink(fileName: string): Promise { + const streaming = await tryCreateStreamingSink(fileName); + if (streaming) return streaming; + return createBlobSink(fileName); +} diff --git a/src/utils/patching/xdelta.ts b/src/utils/patching/xdelta.ts new file mode 100644 index 0000000..262a67d --- /dev/null +++ b/src/utils/patching/xdelta.ts @@ -0,0 +1,162 @@ +export interface XdeltaResult { + ok: boolean; + hasChecksums: boolean | null; + errorCode?: number; + errorMessage?: string; +} + +type WorkerChunkMessage = { type: "chunk"; bytes: Uint8Array }; +type WorkerProgressMessage = { type: "progress"; bytesOut: number; bytesIn: number }; +type WorkerDoneMessage = { + type: "done"; + ok: boolean; + hasChecksums: boolean | null; + errorCode?: number; + errorMessage?: string; +}; +type WorkerMessage = WorkerChunkMessage | WorkerProgressMessage | WorkerDoneMessage; + +function isWorkerMessage(data: unknown): data is WorkerMessage { + return ( + typeof data === "object" && + data !== null && + "type" in data && + (data.type === "chunk" || data.type === "progress" || data.type === "done") + ); +} + +export function friendlyXdeltaError(result: XdeltaResult): string { + const msg = result.errorMessage ?? ""; + if (msg.toLowerCase().includes("checksum")) { + return "This patch does not match the selected base ROM."; + } + return msg || "xdelta patch failed"; +} + +export async function runXdelta(opts: { + mode: "decode" | "encode"; + sourceFile: Blob; + inputFile: Blob; + disableChecksum?: boolean; + discardOutput?: boolean; + onChunk?: (bytes: Uint8Array) => void | Promise; + onProgress?: (p: { bytesOut: number; bytesIn: number }) => void; +}): Promise { + const worker = new Worker("/xdelta/xdelta3.worker.js", { type: "module" }); + + return new Promise((resolve, reject) => { + let settled = false; + let chunkQueue: Promise = Promise.resolve(); + + const finish = (settle: () => void) => { + if (settled) return; + settled = true; + worker.terminate(); + settle(); + }; + + worker.onmessage = (event: MessageEvent) => { + if (!isWorkerMessage(event.data)) { + finish(() => reject(new Error("Unexpected xdelta worker message"))); + return; + } + + const data = event.data; + + if (data.type === "chunk") { + const bytes = data.bytes; + chunkQueue = chunkQueue.then(async () => { + if (opts.onChunk) await opts.onChunk(bytes); + }); + return; + } + + if (data.type === "progress") { + opts.onProgress?.({ bytesOut: data.bytesOut, bytesIn: data.bytesIn }); + return; + } + + // type === "done" + const result: XdeltaResult = { + ok: data.ok, + hasChecksums: data.hasChecksums ?? null, + ...(data.errorCode !== undefined ? { errorCode: data.errorCode } : {}), + ...(data.errorMessage !== undefined ? { errorMessage: data.errorMessage } : {}), + }; + + void chunkQueue + .then(() => { + finish(() => resolve(result)); + }) + .catch((err: unknown) => { + finish(() => reject(err)); + }); + }; + + worker.onerror = (event) => { + finish(() => + reject(event.error instanceof Error ? event.error : new Error(event.message || "xdelta worker error")), + ); + }; + + worker.postMessage({ + command: "start", + mode: opts.mode, + sourceFile: opts.sourceFile, + inputFile: opts.inputFile, + disableChecksum: opts.disableChecksum ?? false, + discardOutput: opts.discardOutput ?? false, + }); + }); +} + +export async function decodeXdelta(opts: { + sourceFile: Blob; + patchBlob: Blob; + onChunk: (bytes: Uint8Array) => void | Promise; + onProgress?: (p: { bytesOut: number; bytesIn: number }) => void; +}): Promise { + return runXdelta({ + mode: "decode", + sourceFile: opts.sourceFile, + inputFile: opts.patchBlob, + onChunk: opts.onChunk, + onProgress: opts.onProgress, + }); +} + +export async function encodeXdelta(opts: { + sourceFile: Blob; + targetFile: Blob; +}): Promise<{ result: XdeltaResult; patch: Blob | null }> { + const chunks: BlobPart[] = []; + const result = await runXdelta({ + mode: "encode", + sourceFile: opts.sourceFile, + inputFile: opts.targetFile, + onChunk: (bytes) => { + chunks.push( + bytes.buffer instanceof ArrayBuffer + ? (bytes as Uint8Array) + : new Uint8Array(bytes), + ); + }, + }); + return { + result, + patch: result.ok ? new Blob(chunks) : null, + }; +} + +export async function trialDecodeXdelta(opts: { + sourceFile: Blob; + patchBlob: Blob; +}): Promise { + return runXdelta({ + mode: "decode", + sourceFile: opts.sourceFile, + inputFile: opts.patchBlob, + discardOutput: true, + disableChecksum: false, + }); +} diff --git a/supabase/migrations/20260731233000_patch_format.sql b/supabase/migrations/20260731233000_patch_format.sql new file mode 100644 index 0000000..23ccbee --- /dev/null +++ b/supabase/migrations/20260731233000_patch_format.sql @@ -0,0 +1,4 @@ +create type public."Patch Format" as enum ('bps', 'xdelta'); + +alter table if exists public.patches + add column if not exists format "Patch Format" not null default 'bps';