Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 6x 6x 6x 31x 31x 31x 31x 31x 31x 9x 21x 21x 21x 1x 1x 20x 14x 14x 6x 6x 6x 9x 9x 31x 31x 31x 31x 31x 31x 31x 31x 5x 5x 31x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 3x 3x 31x 31x 31x 31x 31x 31x 2x 2x 3386x 3386x 2x 2x 31x 31x 31x 31x 31x 31x 3x 3x 31x 31x 31x 31x 31x 31x 1x 1x 3x 3x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 7x 7x 7x 7x 7x 7x 39x 39x 16x 16x 23x 23x 39x 11x 11x 12x 12x 12x 7x 7x 7x 7x 1x 1x 7x 7x 1x 7x 7x 7x 7x 31x 31x 31x 31x 31x 31x 31x 31x 4x 4x 4x 4x 4x 4x 31x | import { Servicer } from '../servicer/servicer';
import { MessageType } from '../core/message-type';
import { Options } from '../options';
import { ZipOptions } from '../interfaces/zip-options';
import JSZip, { file } from 'jszip';
const commands = MessageType.file;
export class FileSystem {
constructor(
private servicer: Servicer,
private options: Options
) { }
/**
* 连接路径
* @param paths - 路径列表
*/
public joinPath(paths: Array<string>) {
return paths.reduce((path, subPath) => {
const hasLastSlash = path.length > 0 && path[path.length - 1] === '/';
const hasFirstSlash = subPath.length > 0 && subPath[0] === '/';
if(hasLastSlash && hasFirstSlash) {
return `${path}${subPath.substring(1)}`;
}
else if(!hasLastSlash && !hasFirstSlash) {
return `${path}/${subPath}`;
}
else {
return `${path}${subPath}`;
}
}, '');
}
/**
* 写入文件
* @param path - 文件路径
* @param content - 文件内容
* @param options - 配置参数
*/
async writeFile(path: string, content: string | Uint8Array, options: WriteFileOptions = { flags: 'w' }) {
await this.servicer.do(commands.writeFile, [path, content, options]);
}
/**
* 读取文件内容
* @param path - 文件路径
* @param options - 配置参数
*/
async readFile(path: string, options: ReadFileOptions = { encoding: 'utf8', flags: 'r' }): Promise<string | Uint8Array> {
return this.servicer.do(commands.readFile, [path, options]);
}
/**
* 创建目录
* @param dir - 目录地址
*/
async mkdir(dir: string) {
await this.servicer.do(commands.mkdir, [dir]);
}
/**
* 移除文件
* @param path - 文件地址
*/
async removeFile(path: string) {
await this.servicer.do(commands.remove, [path]);
}
/**
* 加密文件
* @param src - 要加密的文件路径
* @param dst - 加密后的文件存储路径
*/
async encryptFile(src: string, dst: string) {
await this.servicer.do(commands.encryptFile, [src, dst]);
}
/**
* 加密文本
* @param content - 要加密的文本
*/
encryptContent(content: string): Promise<ArrayLike<number>> {
return this.servicer.do(commands.encryptContent, [content]);
}
/**
* 加密内容
* @param buffer - 内容数据
*/
encryptBuffer(buffer: ArrayLike<number>): Promise<ArrayLike<number>> {
const chars = [];
for(let i = 0; i < buffer.length; i++) {
chars.push(String.fromCharCode(buffer[i]));
}
return this.encryptContent(chars.join(''));
}
/**
* 解密内容
* @param buffer - 内容数据
*/
decryptBuffer(buffer: ArrayLike<number>): Promise<string> {
return this.servicer.do(commands.decryptBuffer, [buffer]);
}
/**
* 解密文本
* @param content - 文本
*/
decryptContent(content: string): Promise<string> {
const buffer = [];
for(let i = 0; i < content.length; i++) {
buffer.push(content.charCodeAt(i));
}
return this.decryptBuffer(buffer);
}
/**
* 解压 zip 包,将内部的 shader 写入文件系统,返回 zip 中的配置
* @param zipData - zip 包数据
* @param options - 解压配置
* @param isEncrypted - zip 包内容是否被加密
* @returns 滤镜配置字符串
*/
async loadZip(zipData: any, options?: ZipOptions, isEncrypted: boolean = true): Promise<{ config: string, files: JSZip.JSZipObject[]}> {
const zip = await JSZip.loadAsync(zipData, options);
const filenames = Object.getOwnPropertyNames(zip.files);
const files: Array<JSZip.JSZipObject> = []
let configFile: JSZip.JSZipObject | null = null;
filenames.forEach(filename => {
// 移除 __MACOS 文件
if(filename.includes('__MACOSX')) {
return;
}
const file = zip.files[filename];
if(!filename.includes('config.json') && !file.dir) {
files.push(file);
}
else {
configFile = file;
}
});
// 处理配置文件
if(!configFile) {
throw new Error('config file is not exist');
}
let config = await configFile!.async(isEncrypted ? 'array' : 'string');
if(isEncrypted) {
config = await this.decryptBuffer(config as number[]);
}
return { config: config as string, files };
}
/**
* 将 shader 写入文件系统
* @param shader - shader 内容
* @param name - shader 名称
* @param version - 版本号
*/
async writeShader(shader: string | Uint8Array, name: string, version?: string) {
const { workResource } = this.options;
const filename = version ? `${version}_${name}` : name;
const path = this.joinPath([workResource!, filename]);
this.writeFile(path, shader);
}
} |