lyb-pixi-js 1.11.14 → 1.11.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,7 @@ export class LibPixiDragLocate extends Container {
17
17
  /** 按下坐标 */
18
18
  this._downPos = { x: 0, y: 0 };
19
19
  window.addEventListener("keydown", (e) => {
20
- if (e.altKey && e.key.toLowerCase() === "q") {
20
+ if (e.ctrlKey && e.key.toLowerCase() === "q") {
21
21
  e.preventDefault();
22
22
  if (this._isLocalte)
23
23
  return;
@@ -18,7 +18,7 @@ export class LibPixiPuzzleBg extends Container {
18
18
  bg.position.set(x || 0, y || 0);
19
19
  //监听鼠标空格事件
20
20
  document.addEventListener("keydown", (e) => {
21
- if (e.altKey && e.code === "Space") {
21
+ if (e.ctrlKey && e.key.toLowerCase() === "a") {
22
22
  e.preventDefault();
23
23
  bg.visible = !bg.visible;
24
24
  }
package/lyb-pixi.js CHANGED
@@ -57761,7 +57761,7 @@ void main(void){
57761
57761
  bg.alpha = alpha || 0.25;
57762
57762
  bg.position.set(x2 || 0, y2 || 0);
57763
57763
  document.addEventListener("keydown", (e2) => {
57764
- if (e2.altKey && e2.code === "Space") {
57764
+ if (e2.ctrlKey && e2.key.toLowerCase() === "a") {
57765
57765
  e2.preventDefault();
57766
57766
  bg.visible = !bg.visible;
57767
57767
  } else if (e2.code === "ArrowUp") {
@@ -57858,7 +57858,7 @@ void main(void){
57858
57858
  this._allowMove = false;
57859
57859
  this._downPos = { x: 0, y: 0 };
57860
57860
  window.addEventListener("keydown", (e2) => {
57861
- if (e2.altKey && e2.key.toLowerCase() === "q") {
57861
+ if (e2.ctrlKey && e2.key.toLowerCase() === "q") {
57862
57862
  e2.preventDefault();
57863
57863
  if (this._isLocalte)
57864
57864
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-pixi-js",
3
- "version": "1.11.14",
3
+ "version": "1.11.16",
4
4
  "description": "自用Pixi.JS方法库",
5
5
  "license": "ISC",
6
6
  "exports": {
@@ -1,36 +0,0 @@
1
- import { Container } from "pixi.js";
2
- /** @description 拖拽定位 */
3
- export declare class LibDragLocate extends Container {
4
- static stage: Container;
5
- /** 输入框 */
6
- private _inputEl;
7
- /** 搜索结果 */
8
- private _resultEl;
9
- /** 状态栏元素 */
10
- private _statusBarEl;
11
- /** 坐标元素 */
12
- private _positionEl;
13
- /** 当前坐标值 */
14
- private _positionValue;
15
- /** 是否处于定位状态 */
16
- private _isLocalte;
17
- /** 是否显示输入框 */
18
- private _showInput;
19
- /** 是否允许移动 */
20
- private _allowMove;
21
- /** 按下坐标 */
22
- private _downPos;
23
- /** 当前处于移动的元素 */
24
- private _currentMoveContainer?;
25
- constructor();
26
- /** @description 创建输入框 */
27
- private _createInput;
28
- /** @description 创建用于展示搜索结果的列表 */
29
- private _createResultList;
30
- /** @description 创建搜索结果元素 */
31
- private _createResultEl;
32
- /** @description 创建状态栏 */
33
- private _createStatusBar;
34
- /** @description 递归搜索 */
35
- private _findByName;
36
- }
@@ -1,230 +0,0 @@
1
- import { Container, Ticker } from "pixi.js";
2
- import gsap from "gsap";
3
- import { libPixiFilter } from "../../Utils/LibPixiFilter";
4
- import { libJsCopy } from "lyb-js/Browser/LibJsCopy.js";
5
- /** @description 拖拽定位 */
6
- export class LibDragLocate extends Container {
7
- constructor() {
8
- super();
9
- /** 当前坐标值 */
10
- this._positionValue = "";
11
- /** 是否处于定位状态 */
12
- this._isLocalte = false;
13
- /** 是否显示输入框 */
14
- this._showInput = false;
15
- /** 是否允许移动 */
16
- this._allowMove = false;
17
- /** 按下坐标 */
18
- this._downPos = { x: 0, y: 0 };
19
- window.addEventListener("keydown", (e) => {
20
- if (e.altKey && e.key.toLowerCase() === "q") {
21
- if (this._isLocalte)
22
- return;
23
- this._showInput = true;
24
- e.preventDefault();
25
- }
26
- if (e.key === "Escape") {
27
- this._showInput = false;
28
- }
29
- });
30
- this._createInput();
31
- this._createResultList();
32
- this._createStatusBar();
33
- LibDragLocate.stage.on("pointerdown", (event) => {
34
- if (!this._currentMoveContainer)
35
- return;
36
- this._allowMove = true;
37
- const { x, y } = LibDragLocate.stage.toLocal(event.global);
38
- this._downPos = { x, y };
39
- });
40
- LibDragLocate.stage.on("pointermove", (event) => {
41
- if (!this._allowMove || !this._currentMoveContainer)
42
- return;
43
- const { x, y } = LibDragLocate.stage.toLocal(event.global);
44
- const dx = x - this._downPos.x;
45
- const dy = y - this._downPos.y;
46
- this._currentMoveContainer.x += dx;
47
- this._currentMoveContainer.y += dy;
48
- this._downPos = { x, y };
49
- const posX = Math.round(this._currentMoveContainer.x);
50
- const posY = Math.round(this._currentMoveContainer.y);
51
- this._positionEl.textContent = `X: ${posX}, Y: ${posY}`;
52
- this._positionValue = `xxx.position.set(${posX}, ${posY})`;
53
- });
54
- LibDragLocate.stage.on("pointerup", () => {
55
- this._allowMove = false;
56
- }, this);
57
- Ticker.shared.add(() => {
58
- if (this._showInput) {
59
- if (this._isLocalte) {
60
- this._inputEl.style.display = "none";
61
- this._resultEl.style.display = "none";
62
- this._statusBarEl.style.display = "flex";
63
- }
64
- else {
65
- this._inputEl.style.display = "block";
66
- this._resultEl.style.display = "flex";
67
- this._statusBarEl.style.display = "none";
68
- this._inputEl.focus();
69
- }
70
- }
71
- else {
72
- this._inputEl.style.display = "none";
73
- this._resultEl.style.display = "none";
74
- }
75
- });
76
- }
77
- /** @description 创建输入框 */
78
- _createInput() {
79
- this._inputEl = document.createElement("input");
80
- this._inputEl.type = "text";
81
- this._inputEl.style.cssText = `
82
- background-color: rgba(0,0,0,0.75);
83
- border: 2px solid rgba(255,255,255,0.5);
84
- color: #fff;
85
- display: none;
86
- font-size: 25px;
87
- height: 50px;
88
- left: 50%;
89
- outline: none;
90
- position: fixed;
91
- text-align: center;
92
- top: 25%;
93
- transform: translate(-50%, -50%);
94
- width: 50vw;
95
- `;
96
- document.body.appendChild(this._inputEl);
97
- this._inputEl.addEventListener("input", () => {
98
- const results = this._findByName(LibDragLocate.stage, this._inputEl.value);
99
- //创建搜索结果列表
100
- this._resultEl.innerHTML = "";
101
- const renderNode = (item) => {
102
- const resultEl = this._createResultEl(item.node.constructor.name, item.node.name, item.depth);
103
- this._resultEl.appendChild(resultEl);
104
- gsap.killTweensOf(item.node);
105
- //悬浮结果列表元素闪烁
106
- resultEl.addEventListener("mouseenter", () => {
107
- gsap.to(item.node, {
108
- alpha: 0,
109
- duration: 0.25,
110
- yoyo: true,
111
- ease: "none",
112
- repeat: -1,
113
- });
114
- });
115
- //离开结果后停止闪烁
116
- resultEl.addEventListener("mouseleave", () => {
117
- gsap.killTweensOf(item.node);
118
- item.node.alpha = 1;
119
- });
120
- resultEl.addEventListener("click", () => {
121
- gsap.killTweensOf(item.node);
122
- this._currentMoveContainer = item.node;
123
- item.node.alpha = 1;
124
- item.node.filters = [libPixiFilter("desaturate", 0)];
125
- this._isLocalte = true;
126
- });
127
- item.children.forEach(renderNode);
128
- };
129
- results.forEach(renderNode);
130
- });
131
- }
132
- /** @description 创建用于展示搜索结果的列表 */
133
- _createResultList() {
134
- this._resultEl = document.createElement("div");
135
- this._resultEl.style.cssText = `
136
- background-color: rgba(0,0,0,0.75);
137
- border: 2px solid rgba(255,255,255,0.5);
138
- left: 50%;
139
- transform: translateX(-50%);
140
- height: 300px;
141
- overflow-y: auto;
142
- position: fixed;
143
- display: none;
144
- gap: 10px;
145
- flex-direction: column;
146
- top: calc(25% + 50px);
147
- width: 50vw;
148
- `;
149
- document.body.appendChild(this._resultEl);
150
- }
151
- /** @description 创建搜索结果元素 */
152
- _createResultEl(className, tagName, depth) {
153
- const resultItem = document.createElement("div");
154
- resultItem.style.cssText = `
155
- display: flex;
156
- justify-content: space-between;
157
- padding: 5px 10px;
158
- align-items: center;
159
- width: 100%;
160
- font-size: 25px;
161
- background-color: rgba(255,255,255,0.1);
162
- color: #fff;
163
- padding-left: ${depth * 20}px;
164
- `;
165
- const classNameEl = document.createElement("div");
166
- classNameEl.textContent = className;
167
- resultItem.appendChild(classNameEl);
168
- const tagNameEl = document.createElement("div");
169
- tagNameEl.textContent = tagName;
170
- resultItem.appendChild(tagNameEl);
171
- return resultItem;
172
- }
173
- /** @description 创建状态栏 */
174
- _createStatusBar() {
175
- this._statusBarEl = document.createElement("div");
176
- this._statusBarEl.style.cssText = `
177
- align-items: center;
178
- background-color: rgba(0,0,0,0.75);
179
- border: 2px solid rgba(0, 255, 13, 0.5);
180
- color: #fff;
181
- display: none;
182
- font-size: 25px;
183
- justify-content: center;
184
- position: fixed;
185
- top: 25%;
186
- height: 50px;
187
- left: 50%;
188
- transform: translate(-50%, -50%);
189
- width: 50vw;
190
- `;
191
- this._positionEl = document.createElement("div");
192
- this._statusBarEl.appendChild(this._positionEl);
193
- document.body.appendChild(this._statusBarEl);
194
- this._statusBarEl.addEventListener("click", () => {
195
- this._isLocalte = false;
196
- libJsCopy(this._positionValue);
197
- if (this._currentMoveContainer) {
198
- this._currentMoveContainer.filters = [];
199
- this._currentMoveContainer = undefined;
200
- }
201
- });
202
- }
203
- /** @description 递归搜索 */
204
- _findByName(root, keyword) {
205
- if (!keyword)
206
- return [];
207
- const match = (node) => {
208
- var _a;
209
- return ((_a = node.name) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes(keyword.toLowerCase())) ||
210
- node.constructor.name.toLowerCase().includes(keyword.toLowerCase());
211
- };
212
- const dfs = (node, depth) => {
213
- const children = node.children
214
- .filter((c) => c instanceof Container)
215
- .flatMap((c) => dfs(c, depth + 1));
216
- if (match(node) || children.length > 0) {
217
- return [
218
- {
219
- node,
220
- depth,
221
- children,
222
- expanded: true,
223
- },
224
- ];
225
- }
226
- return [];
227
- };
228
- return dfs(root, 0);
229
- }
230
- }
@@ -1,9 +0,0 @@
1
- import { Container } from "pixi.js";
2
- /**
3
- * @description 按照指定方向(水平或垂直)排列元素,支持固定间隔或自定义每个间隔。
4
- * @param items 要排列的元素数组。
5
- * @param gap 元素之间的间隔,可以是固定间隔或自定义的间隔数组。
6
- * @param direction 排列方向,"x"表示水平,"y"表示垂直,默认为水平。
7
- * @link 使用方法:https://www.npmjs.com/package/lyb-pixi-js#LibArrangeLinear-间隔布局
8
- */
9
- export declare const LibArrangeLinear: (items: Container[], gap: number | number[], direction?: "x" | "y") => void;
@@ -1,29 +0,0 @@
1
- /**
2
- * @description 按照指定方向(水平或垂直)排列元素,支持固定间隔或自定义每个间隔。
3
- * @param items 要排列的元素数组。
4
- * @param gap 元素之间的间隔,可以是固定间隔或自定义的间隔数组。
5
- * @param direction 排列方向,"x"表示水平,"y"表示垂直,默认为水平。
6
- * @link 使用方法:https://www.npmjs.com/package/lyb-pixi-js#LibArrangeLinear-间隔布局
7
- */
8
- export const LibArrangeLinear = (items, gap, direction = "x") => {
9
- if (Array.isArray(gap)) {
10
- if (gap.length !== items.length - 1) {
11
- console.error(new Error("间隔的数组长度只能等于元素数组长度-1"));
12
- return;
13
- }
14
- }
15
- let lastPosition = 0;
16
- items.forEach((item, index) => {
17
- const position = index === 0
18
- ? 0
19
- : lastPosition + (Array.isArray(gap) ? gap[index - 1] : gap);
20
- if (direction === "x") {
21
- item.x = position;
22
- lastPosition = item.x + item.width;
23
- }
24
- else {
25
- item.y = position;
26
- lastPosition = item.y + item.height;
27
- }
28
- });
29
- };
@@ -1,7 +0,0 @@
1
- import { Container } from "pixi.js";
2
- /** @description 触发后代监听
3
- * @param container 容器
4
- * @param event 事件名称
5
- * @param payload 事件携带数据
6
- */
7
- export declare const LibEmitContainerEvent: (container: Container, event: string, payload?: any) => void;
@@ -1,13 +0,0 @@
1
- /** @description 触发后代监听
2
- * @param container 容器
3
- * @param event 事件名称
4
- * @param payload 事件携带数据
5
- */
6
- export const LibEmitContainerEvent = (container, event, payload) => {
7
- container.children.forEach((child) => {
8
- child.emit(event, payload);
9
- if ("children" in child) {
10
- LibEmitContainerEvent(child, event, payload);
11
- }
12
- });
13
- };
@@ -1,8 +0,0 @@
1
- import { Plugin } from "vite";
2
- /**
3
- * Vite插件:Pixi.js缓存清除
4
- * @param srcDir - 源文件目录
5
- * @param distDir - 目标输出目录
6
- * @returns {Plugin} Vite插件实例
7
- */
8
- export declare const LibPixiViteAssetsHash: (srcDir: string, distDir: string) => Plugin;
@@ -1,343 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import { readdir, readFile, copyFile, mkdir, writeFile, rm, rename } from "fs/promises";
11
- import { createHash } from "crypto";
12
- import { extname, basename, join, dirname, relative } from "path";
13
- /**
14
- * .fnt文件处理工具类
15
- */
16
- class FntView {
17
- /**
18
- * 构造函数
19
- * @param {Buffer} buffer - .fnt文件内容缓冲区
20
- */
21
- constructor(buffer) {
22
- this.rawAtlas = buffer.toString();
23
- }
24
- /**
25
- * 获取.fnt文件中引用的所有贴图文件名
26
- * @returns {string[]} 贴图文件名数组
27
- */
28
- getTextures() {
29
- const regex = /file="([^"]+\.png)"/g;
30
- return [...this.rawAtlas.matchAll(regex)].map((m) => m[1]);
31
- }
32
- /**
33
- * 替换.fnt文件中的贴图引用
34
- * @param filename - 原始贴图文件名
35
- * @param newFilename - 新的贴图文件名
36
- */
37
- replaceTexture(filename, newFilename) {
38
- const escaped = filename.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
39
- const regex = new RegExp(`(file=")${escaped}(")`, "g");
40
- this.rawAtlas = this.rawAtlas.replace(regex, `$1${newFilename}$2`);
41
- }
42
- /**
43
- * 获取处理后的.fnt文件内容缓冲区
44
- * @returns {Buffer} 文件内容缓冲区
45
- */
46
- get buffer() {
47
- return Buffer.from(this.rawAtlas);
48
- }
49
- }
50
- /**
51
- * Pixi.js 资源缓存清除工具
52
- *
53
- * 通过为静态资源文件名添加哈希值来实现缓存清除功能,
54
- * 同时处理特殊文件格式(如.fnt、.atlas)的引用关系。
55
- */
56
- class PixiCacheBuster {
57
- /**
58
- * 构造函数
59
- * @param srcDir - 源文件目录路径
60
- * @param distDir - 目标输出目录路径
61
- */
62
- constructor(srcDir, distDir) {
63
- /** main 目录文件映射表(原始文件名 -> 哈希文件名) */
64
- this.mapMain = {};
65
- /** preload 目录文件映射表(原始文件名 -> 哈希文件名) */
66
- this.mapPreload = {};
67
- /** main 清单文件名 */
68
- this.manifestMain = "mainManifest.json";
69
- /** preload 清单文件名 */
70
- this.manifestPreload = "preloadManifest.json";
71
- this.srcDir = srcDir;
72
- this.distDir = distDir;
73
- }
74
- /**
75
- * 标准化路径(将反斜杠转换为正斜杠)
76
- * @param p - 原始路径
77
- * @returns 标准化后的路径
78
- */
79
- normalizePath(p) {
80
- return p.replace(/\\/g, "/");
81
- }
82
- /**
83
- * 检查文件是否为图片
84
- * @param file - 文件路径
85
- * @returns {boolean} 是否为图片文件
86
- */
87
- isImage(file) {
88
- return /\.(png|jpe?g|gif|webp|svg)$/i.test(file);
89
- }
90
- /**
91
- * 递归遍历目录获取所有文件路径
92
- * @param dir - 要遍历的目录路径
93
- * @returns {Promise<string[]>} 文件路径数组
94
- */
95
- walk(dir) {
96
- return __awaiter(this, void 0, void 0, function* () {
97
- const entries = yield readdir(dir, { withFileTypes: true });
98
- const files = yield Promise.all(entries.map((entry) => {
99
- const fullPath = join(dir, entry.name);
100
- return entry.isDirectory() ? this.walk(fullPath) : [fullPath];
101
- }));
102
- return files.flat();
103
- });
104
- }
105
- /**
106
- * 处理普通文件(添加哈希值到文件名)
107
- * @param filePath - 文件路径
108
- * @param srcBase - 源基础目录
109
- * @param distBase - 目标基础目录
110
- * @param {Record<string, string>} map - 文件映射表
111
- */
112
- processFile(filePath, srcBase, distBase, map) {
113
- return __awaiter(this, void 0, void 0, function* () {
114
- const buf = yield readFile(filePath);
115
- const ext = extname(filePath);
116
- const relPath = this.normalizePath(relative(srcBase, filePath));
117
- const relDir = dirname(relPath);
118
- const base = basename(filePath, ext);
119
- //生成8位MD5哈希值
120
- const hash = createHash("md5").update(buf).digest("hex").slice(0, 8);
121
- const hashedName = ext === ".ttf" ? `${base}${ext}` : `${base}-${hash}${ext}`;
122
- const distPath = join(distBase, relDir, hashedName);
123
- //创建目录并写入文件
124
- yield mkdir(dirname(distPath), { recursive: true });
125
- yield writeFile(distPath, buf);
126
- //更新映射表
127
- map[relPath] = this.normalizePath(join(relDir, hashedName));
128
- });
129
- }
130
- /**
131
- * 复制原始文件(不添加哈希值)
132
- * @param srcBase - 源基础目录
133
- * @param distBase - 目标基础目录
134
- */
135
- copyOriginal(srcBase, distBase) {
136
- return __awaiter(this, void 0, void 0, function* () {
137
- const files = yield this.walk(srcBase);
138
- yield Promise.all(files.map((file) => __awaiter(this, void 0, void 0, function* () {
139
- const relPath = this.normalizePath(relative(srcBase, file));
140
- const distPath = join(distBase, relPath);
141
- yield mkdir(dirname(distPath), { recursive: true });
142
- yield copyFile(file, distPath);
143
- })));
144
- });
145
- }
146
- /**
147
- * 处理.fnt文件(位图字体文件)
148
- * @param filePath - .fnt文件路径
149
- * @param srcBase - 源基础目录
150
- * @param distBase - 目标基础目录
151
- * @param {Record<string, string>} map - 文件映射表
152
- */
153
- processFntFile(filePath, srcBase, distBase, map) {
154
- return __awaiter(this, void 0, void 0, function* () {
155
- const buf = yield readFile(filePath);
156
- const fntView = new FntView(buf);
157
- const textures = fntView.getTextures();
158
- //获取.fnt文件所在目录的相对路径
159
- const fntDir = dirname(this.normalizePath(relative(srcBase, filePath)));
160
- //替换.fnt文件中引用的所有贴图文件名
161
- for (const tex of textures) {
162
- const fullTexPath = this.normalizePath(join(fntDir, tex));
163
- const newName = map[fullTexPath];
164
- fntView.replaceTexture(tex, basename(newName));
165
- }
166
- //写入处理后的.fnt文件
167
- const relPath = this.normalizePath(relative(srcBase, filePath));
168
- const hashedDistPath = join(distBase, dirname(relPath), basename(map[relPath]));
169
- yield mkdir(dirname(hashedDistPath), { recursive: true });
170
- yield writeFile(hashedDistPath, fntView.buffer);
171
- });
172
- }
173
- /**
174
- * 处理.atlas文件(纹理图集文件)
175
- * @param filePath - .atlas文件路径
176
- * @param srcBase - 源基础目录
177
- * @param distBase - 目标基础目录
178
- * @param {Record<string, string>} map - 文件映射表
179
- * @param [overrideFileName] - 覆盖输出文件名(不带扩展名)
180
- */
181
- processAtlasFile(filePath, srcBase, distBase, map, overrideFileName) {
182
- return __awaiter(this, void 0, void 0, function* () {
183
- const buf = yield readFile(filePath);
184
- let content = buf.toString();
185
- const atlasDir = dirname(this.normalizePath(relative(srcBase, filePath)));
186
- //替换.atlas文件中引用的所有图片文件名
187
- for (const [original, hashed] of Object.entries(map)) {
188
- if (!this.isImage(original))
189
- continue;
190
- const imageDir = dirname(original);
191
- //如果图片目录和.atlas文件目录不一致,则跳过
192
- if (imageDir !== atlasDir)
193
- continue;
194
- const originalBase = basename(original);
195
- const hashedBase = basename(hashed);
196
- const regex = new RegExp(`^${originalBase}$`, "gm");
197
- content = content.replace(regex, hashedBase);
198
- }
199
- //写入处理后的.atlas文件
200
- const relPath = this.normalizePath(relative(srcBase, filePath));
201
- const finalName = overrideFileName ? `${overrideFileName}.atlas` : basename(relPath);
202
- const finalDistPath = join(distBase, atlasDir, finalName);
203
- yield mkdir(dirname(finalDistPath), { recursive: true });
204
- yield writeFile(finalDistPath, content);
205
- });
206
- }
207
- /**
208
- * 处理.json文件(通常是Spine动画文件)
209
- * @param filePath - .json文件路径
210
- * @param srcBase - 源基础目录
211
- * @param distBase - 目标基础目录
212
- * @param {Record<string, string>} map - 文件映射表
213
- */
214
- processJsonFile(filePath, srcBase, distBase, map) {
215
- return __awaiter(this, void 0, void 0, function* () {
216
- const buf = yield readFile(filePath);
217
- const relPath = this.normalizePath(relative(srcBase, filePath));
218
- //生成哈希文件名
219
- const hash = createHash("md5").update(buf).digest("hex").slice(0, 8);
220
- const ext = extname(filePath);
221
- const base = basename(filePath, ext);
222
- const hashedName = `${base}.${hash}${ext}`;
223
- const distDir = join(distBase, dirname(relPath));
224
- const hashedDistPath = join(distDir, hashedName);
225
- //写入哈希后的.json文件
226
- yield mkdir(distDir, { recursive: true });
227
- yield writeFile(hashedDistPath, buf);
228
- //更新映射表
229
- map[relPath] = this.normalizePath(join(dirname(relPath), hashedName));
230
- //重命名关联的.atlas文件(如果有)
231
- const atlasPath = join(distDir, `${base}.atlas`);
232
- const newAtlasPath = join(distDir, `${base}.${hash}.atlas`);
233
- try {
234
- yield rename(atlasPath, newAtlasPath);
235
- }
236
- catch (_a) {
237
- //如果.atlas文件不存在,则忽略错误
238
- }
239
- });
240
- }
241
- /**
242
- * 处理目录中的所有文件
243
- * @param srcBase - 源基础目录
244
- * @param distBase - 目标基础目录
245
- * @param {Record<string, string>} map - 文件映射表
246
- */
247
- processFiles(srcBase, distBase, map) {
248
- return __awaiter(this, void 0, void 0, function* () {
249
- const files = yield this.walk(srcBase);
250
- //分类文件类型
251
- const atlasFiles = files.filter((f) => f.endsWith(".atlas"));
252
- const jsonFiles = files.filter((f) => f.endsWith(".json"));
253
- const fntFiles = files.filter((f) => f.endsWith(".fnt"));
254
- const otherFiles = files.filter((f) => !f.endsWith(".atlas") && !f.endsWith(".json"));
255
- //1. 先处理非.atlas/.json文件(包括图片和.fnt文件)
256
- yield Promise.all(otherFiles.map((file) => this.processFile(file, srcBase, distBase, map)));
257
- //2. 处理.fnt文件(替换其中引用的图片文件名)
258
- yield Promise.all(fntFiles.map((f) => this.processFntFile(f, srcBase, distBase, map)));
259
- //3. 处理.json文件,并记录哈希后的文件名
260
- const jsonNameMap = {};
261
- yield Promise.all(jsonFiles.map((file) => __awaiter(this, void 0, void 0, function* () {
262
- const relPath = this.normalizePath(relative(srcBase, file));
263
- const baseName = relPath.slice(0, -5); //去掉.json后缀
264
- yield this.processJsonFile(file, srcBase, distBase, map);
265
- jsonNameMap[baseName] = basename(map[relPath], ".json");
266
- })));
267
- //4. 处理.atlas文件,使用关联的.json文件的哈希名称
268
- yield Promise.all(atlasFiles.map((file) => __awaiter(this, void 0, void 0, function* () {
269
- const relPath = this.normalizePath(relative(srcBase, file));
270
- const baseName = relPath.slice(0, -6); //去掉.atlas后缀
271
- const hashedName = jsonNameMap[baseName];
272
- yield this.processAtlasFile(file, srcBase, distBase, map, hashedName);
273
- if (hashedName) {
274
- const distRelPath = join(dirname(relPath), hashedName);
275
- map[relPath] = this.normalizePath(distRelPath);
276
- }
277
- })));
278
- });
279
- }
280
- /**
281
- * 写入清单文件
282
- * @param {Record<string, string>} map - 文件映射表
283
- * @param manifestPath - 清单文件路径
284
- * @param topLevelDir - 顶层目录名(main/preload)
285
- */
286
- writeManifest(map, manifestPath, topLevelDir) {
287
- return __awaiter(this, void 0, void 0, function* () {
288
- const manifest = Object.entries(map)
289
- .filter(([original]) => {
290
- const ext = original.slice(original.lastIndexOf("."));
291
- //排除spine目录下的.png和.atlas文件
292
- if (original.startsWith("spine/") && (ext === ".png" || ext === ".atlas"))
293
- return false;
294
- //排除.fnt文件引用的图片(通常与.fnt同目录)
295
- if (ext === ".png") {
296
- const base = original.slice(0, -ext.length);
297
- const hasFnt = Object.keys(map).some((k) => k.endsWith(".fnt") && k.startsWith(base));
298
- if (hasFnt)
299
- return false;
300
- }
301
- return true;
302
- })
303
- .map(([original, hashed]) => ({
304
- //生成资源别名(去掉扩展名)和实际路径
305
- alias: `${topLevelDir}/${original.replace(/\.[^/.]+$/, "")}`,
306
- src: [`gameAssetsPack/${topLevelDir}/${hashed}`],
307
- }));
308
- yield mkdir(dirname(manifestPath), { recursive: true });
309
- yield writeFile(manifestPath, JSON.stringify(manifest, null, 2));
310
- });
311
- }
312
- /**
313
- * 构建处理流程
314
- */
315
- build() {
316
- return __awaiter(this, void 0, void 0, function* () {
317
- //清空目标目录
318
- yield rm(this.distDir, { recursive: true, force: true });
319
- yield mkdir(this.distDir, { recursive: true });
320
- //处理main和preload目录(添加哈希)
321
- yield this.processFiles(join(this.srcDir, "main"), join(this.distDir, "main"), this.mapMain);
322
- yield this.processFiles(join(this.srcDir, "preload"), join(this.distDir, "preload"), this.mapPreload);
323
- //复制original目录(不添加哈希)
324
- yield this.copyOriginal(join(this.srcDir, "original"), join(this.distDir, "original"));
325
- //写入清单文件
326
- yield this.writeManifest(this.mapMain, join(this.distDir, this.manifestMain), "main");
327
- yield this.writeManifest(this.mapPreload, join(this.distDir, this.manifestPreload), "preload");
328
- });
329
- }
330
- }
331
- /**
332
- * Vite插件:Pixi.js缓存清除
333
- * @param srcDir - 源文件目录
334
- * @param distDir - 目标输出目录
335
- * @returns {Plugin} Vite插件实例
336
- */
337
- export const LibPixiViteAssetsHash = (srcDir, distDir) => {
338
- const instance = new PixiCacheBuster(srcDir, distDir);
339
- instance.build();
340
- return {
341
- name: "vite-assets-hash",
342
- };
343
- };