feat: add export via ccv3 charx and json

This commit is contained in:
kwaroran
2024-06-02 23:38:55 +09:00
parent 958217839b
commit 8dff1a6cde
7 changed files with 154 additions and 23 deletions

View File

@@ -1,3 +1,5 @@
import { AppendableBuffer, type LocalWriter, type VirtualWriter } from "../storage/globalApi";
import * as fflate from "fflate";
export async function processZip(dataArray: Uint8Array): Promise<string> {
const jszip = await import("jszip");
@@ -12,4 +14,59 @@ export async function processZip(dataArray: Uint8Array): Promise<string> {
} else {
throw new Error("No image found in ZIP file");
}
}
export class CharXWriter{
zip:fflate.Zip
writeEnd:boolean = false
apb = new AppendableBuffer()
constructor(private writer:LocalWriter|WritableStreamDefaultWriter<Uint8Array>|VirtualWriter){
const handlerAsync = async (err:Error, dat:Uint8Array, final:boolean) => {
if(dat){
this.apb.append(dat)
}
if(final){
this.writeEnd = true
}
}
this.zip = new fflate.Zip()
this.zip.ondata = handlerAsync
}
async init(){
//do nothing, just to make compatible with other writer
}
async write(key:string,data:Uint8Array|string){
console.log('write',key)
let dat:Uint8Array
if(typeof data === 'string'){
dat = new TextEncoder().encode(data)
}
else{
dat = data
}
this.writeEnd = false
const file = new fflate.ZipDeflate(key, {
level: 0
});
await this.zip.add(file)
await file.push(dat, true)
await this.writer.write(this.apb.buffer)
this.apb.buffer = new Uint8Array(0)
if(this.writeEnd){
await this.writer.close()
}
}
async end(){
await this.zip.end()
await this.writer.write(this.apb.buffer)
this.apb.buffer = new Uint8Array(0)
if(this.writeEnd){
await this.writer.close()
}
}
}