mirror of
https://github.com/motajs/template.git
synced 2026-11-01 06:04:30 +08:00
refactor: 客户端加载
This commit is contained in:
1 parent
fdeee99d40
commit
4fc6db3e79
55 files changed
+1699
-1234
No files matched your search
+12
-7
@@ -9,8 +9,13 @@ import { transformAsync } from '@babel/core';
|
||||
import archiver from 'archiver';
|
||||
import { createWriteStream } from 'fs';
|
||||
import { zip } from 'compressing';
|
||||
import { RequiredData, RequiredIconsData, ResourceType } from './types';
|
||||
import { splitResource, SplittedResource } from './build-resource';
|
||||
import { RequiredData, RequiredIconsData } from './types';
|
||||
import {
|
||||
CompressedUsage,
|
||||
LoadDataType,
|
||||
splitResource,
|
||||
SplittedResource
|
||||
} from './build-resource';
|
||||
import { formatSize } from './utils';
|
||||
|
||||
/** 打包调试 */
|
||||
@@ -292,9 +297,9 @@ async function getAllChars(client: RollupOutput[]) {
|
||||
}
|
||||
|
||||
interface CompressedLoadListItem {
|
||||
type: ResourceType;
|
||||
name: string;
|
||||
usage: string;
|
||||
readonly readAs: LoadDataType;
|
||||
readonly name: string;
|
||||
readonly usage: CompressedUsage;
|
||||
}
|
||||
|
||||
type CompressedLoadList = Record<string, CompressedLoadListItem[]>;
|
||||
@@ -309,7 +314,7 @@ function generateResourceJSON(resources: SplittedResource[]) {
|
||||
const uri = `project/resource/${file.fileName}`;
|
||||
file.content.forEach(content => {
|
||||
const item: CompressedLoadListItem = {
|
||||
type: content.type,
|
||||
readAs: content.readAs,
|
||||
name: content.name,
|
||||
usage: content.usage
|
||||
};
|
||||
@@ -468,7 +473,7 @@ async function buildGame() {
|
||||
await Promise.all(
|
||||
fonts.map(v => {
|
||||
const fontmin = new Fontmin();
|
||||
const src = resolve(tempDir, 'client/project/fonts', `${v}.ttf`);
|
||||
const src = resolve(tempDir, 'client/project/fonts', v);
|
||||
const dest = resolve(tempDir, 'fonts');
|
||||
const plugin = Fontmin.glyph({
|
||||
text: [...chars].join('')
|
||||
|
||||
+72
-41
@@ -1,20 +1,34 @@
|
||||
import JSZip from 'jszip';
|
||||
import {
|
||||
RequiredData,
|
||||
RequiredIconsData,
|
||||
ResourceType,
|
||||
ResourceUsage
|
||||
} from './types';
|
||||
import { RequiredData, RequiredIconsData } from './types';
|
||||
import { Stats } from 'fs';
|
||||
import { readdir, readFile, stat } from 'fs/promises';
|
||||
import { resolve } from 'path';
|
||||
import { fileHash } from './utils';
|
||||
|
||||
export const enum CompressedUsage {
|
||||
// ---- 系统加载内容,不可更改
|
||||
Font,
|
||||
Image,
|
||||
Sound,
|
||||
Tileset,
|
||||
Autotile,
|
||||
Material,
|
||||
Animate
|
||||
}
|
||||
|
||||
export const enum LoadDataType {
|
||||
ArrayBuffer,
|
||||
Uint8Array,
|
||||
Blob,
|
||||
Text,
|
||||
JSON
|
||||
}
|
||||
|
||||
export interface ResourceInfo {
|
||||
name: string;
|
||||
type: ResourceType;
|
||||
usage: ResourceUsage;
|
||||
stats: Stats;
|
||||
readonly name: string;
|
||||
readonly readAs: LoadDataType;
|
||||
readonly usage: CompressedUsage;
|
||||
readonly stats: Stats;
|
||||
}
|
||||
|
||||
export interface SplittedResource {
|
||||
@@ -26,35 +40,52 @@ export interface SplittedResource {
|
||||
}
|
||||
|
||||
interface ResourceContent extends ResourceInfo {
|
||||
content: string | Buffer | Uint8Array;
|
||||
exceed: boolean;
|
||||
readonly content: string | Buffer | Uint8Array;
|
||||
readonly exceed: boolean;
|
||||
}
|
||||
|
||||
interface ResourcePath {
|
||||
name: string;
|
||||
path: string;
|
||||
usage: ResourceUsage;
|
||||
readonly name: string;
|
||||
readonly path: string;
|
||||
readonly usage: CompressedUsage;
|
||||
}
|
||||
|
||||
function getTypeByUsage(usage: ResourceUsage): ResourceType {
|
||||
function getTypeByUsage(usage: CompressedUsage): LoadDataType {
|
||||
switch (usage) {
|
||||
case 'animate':
|
||||
return 'text';
|
||||
case 'autotile':
|
||||
case 'image':
|
||||
case 'tileset':
|
||||
return 'image';
|
||||
case 'sound':
|
||||
return 'byte';
|
||||
case 'font':
|
||||
return 'buffer';
|
||||
case 'material':
|
||||
return 'material';
|
||||
case CompressedUsage.Animate:
|
||||
return LoadDataType.Text;
|
||||
case CompressedUsage.Autotile:
|
||||
case CompressedUsage.Image:
|
||||
case CompressedUsage.Tileset:
|
||||
case CompressedUsage.Material:
|
||||
return LoadDataType.Blob;
|
||||
case CompressedUsage.Font:
|
||||
case CompressedUsage.Sound:
|
||||
return LoadDataType.ArrayBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
function readFileOfType(path: string, type: ResourceType) {
|
||||
if (type === 'text') {
|
||||
function getZipFolderByUsage(usage: CompressedUsage): string {
|
||||
switch (usage) {
|
||||
case CompressedUsage.Image:
|
||||
return 'image';
|
||||
case CompressedUsage.Tileset:
|
||||
return 'tileset';
|
||||
case CompressedUsage.Autotile:
|
||||
return 'autotile';
|
||||
case CompressedUsage.Material:
|
||||
return 'material';
|
||||
case CompressedUsage.Font:
|
||||
return 'font';
|
||||
case CompressedUsage.Sound:
|
||||
return 'sound';
|
||||
case CompressedUsage.Animate:
|
||||
return 'animate';
|
||||
}
|
||||
}
|
||||
|
||||
function readFileOfType(path: string, type: LoadDataType) {
|
||||
if (type === LoadDataType.Text) {
|
||||
return readFile(path, 'utf-8');
|
||||
} else {
|
||||
return readFile(path);
|
||||
@@ -64,7 +95,7 @@ function readFileOfType(path: string, type: ResourceType) {
|
||||
async function compressFiles(files: ResourceContent[]) {
|
||||
const zip = new JSZip();
|
||||
files.forEach(v => {
|
||||
const dir = `${v.type}/${v.name}`;
|
||||
const dir = `${getZipFolderByUsage(v.usage)}/${v.name}`;
|
||||
zip.file(dir, v.content);
|
||||
});
|
||||
const buffer = await zip.generateAsync({
|
||||
@@ -107,37 +138,37 @@ export async function splitResource(
|
||||
...animates.map<ResourcePath>(v => ({
|
||||
name: `${v}.animate`,
|
||||
path: resolve(base, 'project/animates', `${v}.animate`),
|
||||
usage: 'animate'
|
||||
usage: CompressedUsage.Animate
|
||||
})),
|
||||
...fonts.map<ResourcePath>(v => ({
|
||||
name: `${v}.ttf`,
|
||||
path: resolve(fontsDir, `${v}.ttf`),
|
||||
usage: 'font'
|
||||
name: v,
|
||||
path: resolve(fontsDir, v),
|
||||
usage: CompressedUsage.Font
|
||||
})),
|
||||
...images.map<ResourcePath>(v => ({
|
||||
name: v,
|
||||
path: resolve(base, 'project/images', v),
|
||||
usage: 'image'
|
||||
usage: CompressedUsage.Image
|
||||
})),
|
||||
...sounds.map<ResourcePath>(v => ({
|
||||
name: v,
|
||||
path: resolve(base, 'project/sounds', v),
|
||||
usage: 'sound'
|
||||
usage: CompressedUsage.Sound
|
||||
})),
|
||||
...tilesets.map<ResourcePath>(v => ({
|
||||
name: v,
|
||||
path: resolve(base, 'project/tilesets', v),
|
||||
usage: 'tileset'
|
||||
usage: CompressedUsage.Tileset
|
||||
})),
|
||||
...autotiles.map<ResourcePath>(v => ({
|
||||
name: `${v}.png`,
|
||||
path: resolve(base, 'project/autotiles', `${v}.png`),
|
||||
usage: 'autotile'
|
||||
usage: CompressedUsage.Autotile
|
||||
})),
|
||||
...materials.map<ResourcePath>(v => ({
|
||||
name: v,
|
||||
path: resolve(base, 'project/materials', v),
|
||||
usage: 'material'
|
||||
usage: CompressedUsage.Material
|
||||
}))
|
||||
];
|
||||
|
||||
@@ -154,7 +185,7 @@ export async function splitResource(
|
||||
const type = getTypeByUsage(usage);
|
||||
const content = await readFileOfType(path, type);
|
||||
const info: ResourceContent = {
|
||||
type,
|
||||
readAs: type,
|
||||
name,
|
||||
usage,
|
||||
stats,
|
||||
|
||||
+5
-5
@@ -73,11 +73,11 @@ import fs from 'fs/promises';
|
||||
'./src/types/source/data.d.ts',
|
||||
`
|
||||
${floorId}
|
||||
${d.images.length > 0 ? imgs : 'type ImageIds = never\n'}
|
||||
${d.animates.length > 0 ? anis : 'type AnimationIds = never\n'}
|
||||
${d.sounds.length > 0 ? sounds : 'type SoundIds = never\n'}
|
||||
${d.bgms.length > 0 ? bgms : 'type BgmIds = never\n'}
|
||||
${d.fonts.length > 0 ? fonts : 'type FontIds = never\n'}
|
||||
${d.images.length > 0 ? imgs : 'type ImageIds = string;\n'}
|
||||
${d.animates.length > 0 ? anis : 'type AnimationIds = string;\n'}
|
||||
${d.sounds.length > 0 ? sounds : 'type SoundIds = string;\n'}
|
||||
${d.bgms.length > 0 ? bgms : 'type BgmIds = string;\n'}
|
||||
${d.fonts.length > 0 ? fonts : 'type FontIds = string;\n'}
|
||||
${names}
|
||||
`,
|
||||
'utf-8'
|
||||
|
||||
+5
-5
@@ -590,11 +590,11 @@ async function doDeclaration(type: string, data: string) {
|
||||
'src/types/source/data.d.ts',
|
||||
`
|
||||
${floorId}
|
||||
${d.images.length > 0 ? imgs : 'type ImageIds = never\n'}
|
||||
${d.animates.length > 0 ? anis : 'type AnimationIds = never\n'}
|
||||
${d.sounds.length > 0 ? sounds : 'type SoundIds = never\n'}
|
||||
${d.bgms.length > 0 ? bgms : 'type BgmIds = never\n'}
|
||||
${d.fonts.length > 0 ? fonts : 'type FontIds = never\n'}
|
||||
${d.images.length > 0 ? imgs : 'type ImageIds = string;\n'}
|
||||
${d.animates.length > 0 ? anis : 'type AnimationIds = string;\n'}
|
||||
${d.sounds.length > 0 ? sounds : 'type SoundIds = string;\n'}
|
||||
${d.bgms.length > 0 ? bgms : 'type BgmIds = string;\n'}
|
||||
${d.fonts.length > 0 ? fonts : 'type FontIds = string;\n'}
|
||||
${names}
|
||||
`,
|
||||
'utf-8'
|
||||
|
||||
Reference in new issue
Block a user