-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
616 lines (549 loc) · 27.1 KB
/
Copy pathindex.html
File metadata and controls
616 lines (549 loc) · 27.1 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>Flash小游戏</title>
<link rel="icon" href="favicon.png" sizes="64x64">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--声明网页需要绕过Dark Reader的深色主题-->
<meta name="darkreader-lock">
<meta name="description" content="Flash小游戏">
<!--预加载-->
<link rel="dns-prefetch" href="https://unpkg.com">
<link rel="dns-prefetch" href="https://cdn.jsdelivr.net">
<link rel="dns-prefetch" href="https://fonts.googleapis.com">
<link rel="dns-prefetch" href="https://fonts.gstatic.com">
<link rel="preconnect" href="https://unpkg.com">
<link rel="preconnect" href="https://cdn.jsdelivr.net">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
<style>
html {
height: calc(100% - 30px); /*使html与body元素伸展到整个可见视图*/
}
:root {
color-scheme: light dark; /* 声明页面同时支持浅色和深色模式 */
}
body {
display: flex; /*用flex布局使所有元素在可见视图里左右居中*/
flex-direction: column;
align-items: center;
background-color: light-dark(white, #101010);
margin: 15px;
height: 100%;
font-family: "Noto Sans SC", sans-serif;
}
@media only screen and (max-width: 576px) and (pointer: coarse) {
body {
margin-left: 5px;
margin-right: 5px;
}
}
.nav {
display: flex;
flex-direction: row;
align-items: last baseline;
gap: 5px;
height: 25px;
margin-bottom: 15px;
color: white;
background-color: rgba(0, 0, 0, 0.8);
background-clip: border-box;
border: 10px solid transparent;
border-radius: 5px;
}
.nav > * {
height: 100%;
}
.player {
display: grid !important;
/*
以4比3比例动态更改Ruffle Player的大小,且固定高度或宽度来计算另外一个属性的值:
height: calc(100% - calc(40px + 15px * 2));
width: calc(100% - calc(15px * 2));
*/
aspect-ratio: 4 / 3; /*Chrome系浏览器中必须为父子元素同时加上aspect-ratio才能生效*/
margin: auto;
background-color: #696969;
}
.player > ruffle-player {
aspect-ratio: 4 / 3;
/*覆盖Ruffle Player自身CSS样式*/
width: auto !important;
height: auto !important;
}
#rendererChangeWarning {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 15px;
width: 280px;
border-radius: 5px;
z-index: 1000;
display: none;
line-break: strict;
}
#fontChangeWarning {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 15px;
border-radius: 5px;
z-index: 1000;
display: none;
text-align: center;
line-break: strict;
}
.moreSettings {
position: fixed;
bottom: 20px;
right: 20px;
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 10px;
border-radius: 5px;
height: 80px;
z-index: 1000;
display: grid; /*用grid让内部的元素自动等宽对齐*/
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
align-items: center;
gap: 3px;
}
.moreSettings > * {
height: 24px;
}
.moreSettings > label {
text-align: center;
}
.helpInfo {
position: fixed;
bottom: 20px;
left: 20px;
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 10px;
border-radius: 5px;
height: 24px;
z-index: 1000;
display: grid;
align-content: center;
}
#helpInfoDiv {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 15px;
width: 280px;
border-radius: 5px;
z-index: 1000;
text-align: center;
line-break: strict;
display: flex;
flex-direction: column;
}
.helpInfoDivHide {
visibility: hidden; /*默认状态下使帮助信息部分隐藏*/
}
button, select {
font-family: inherit;
display: inline-flex;
align-items: center;
}
button {
justify-content: center;
}
#isLoadingWarning {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 15px;
border-radius: 5px;
z-index: 1000;
display: none;
line-break: strict;
}
</style>
</head>
<body>
<div class="nav">
<label for="gameSelect">选择:</label>
<select id="gameSelect"></select>
<button id="loadButton">加载</button>
<input type="file" id="fileInput" accept="application/vnd.adobe.flash.movie,application/x-shockwave-flash" style="display: none;">
<button id="chooseFile">选择文件</button>
</div>
<div class="moreSettings">
<label for="renderer">渲染器:</label>
<select id="renderer">
<option value="wgpu-webgl">wgpu-webgl</option>
<option value="webgpu">webgpu</option>
<option value="webgl">webgl</option>
<option value="canvas">canvas</option>
</select>
<input type="file" id="fontFileInput" accept=".ttf,.otf,application/vnd.adobe.flash.movie,application/x-shockwave-flash" style="display: none;">
<button id="chooseFontFile" title="仅限于已汉化的SWF文件字体显示不正常时使用。">导入字体文件</button>
</div>
<div class="helpInfo">
<button onclick="helpInfo()">使用方法</button>
</div>
<div id="helpInfoDiv" class="helpInfoDivHide">
<span style="font-size: larger;">使用方法:</span><br style="display: none;" />
<span style="padding-top: 10px;text-align: justify;text-align-last: left;">
从游戏列表中任意选择一款游戏加载,或者从本地选择一个文件加载。
</span>
<span style="padding-top: 10px;text-align: justify;text-align-last: left;">
不同的渲染器的区别可在<a href="https://github.com/ruffle-rs/ruffle/wiki/Using-Ruffle#list-of-options" target="_blank" style="text-decoration: none;color: #87ceeb;">Ruffle Player的官方Wiki</a>中查找相关信息。
</span>
<span style="padding-top: 10px;text-align: justify;text-align-last: left;">
导入字体文件用于部分汉化后的小游戏字体显示不正常的情况,仅限导入预先做好内含字体的SWF文件。可在<a href="https://github.com/ruffle-rs/ruffle/wiki/Using-Ruffle#list-of-options" target="_blank" style="text-decoration: none;color: #87ceeb;">Ruffle Player的官方Wiki</a>中查找相关信息。
</span>
<span style="padding-top: 10px;text-align: justify;text-align-last: left;">
再次点击按钮即可隐藏本提示。
</span>
</div>
<div id="rendererChangeWarning"></div>
<div id="fontChangeWarning"></div>
<div id="isLoadingWarning"></div>
<div class="player" id="player"></div>
<script src="https://unpkg.com/@ruffle-rs/ruffle"></script>
<script defer>
const games = [
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/CaravanBeast.swf', name: '灵兽大师' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/GoldMiner.swf', name: '黄金矿工' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/JackSmith.swf', name: '杰克驴铁匠' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasBurgeria.swf', name: '老爹汉堡店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasCheeseria.swf', name: '老爹三明治店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasDonuteria.swf', name: '老爹甜甜圈店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasFreezeria.swf', name: '老爹圣代店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasHotDoggeria.swf', name: '老爹热狗店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasPanCakeria.swf', name: '老爹煎饼店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasPastaria.swf', name: '老爹意面店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasPizzeria.swf', name: '老爹披萨店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasSushiria.swf', name: '老爹寿司店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/PapasWingeria.swf', name: '老爹烤翅店' },
{ url: 'https://cdn.jsdelivr.net/gh/NevadaCities/swffiles@main/Rebuild2.swf', name: '重建僵尸大陆2' }
];
// 获取已存在的 select 元素
const select = document.getElementById('gameSelect');
games.forEach(game => {
const option = document.createElement('option');
option.value = game.name;
option.textContent = game.name;
option.id = game.url;
select.appendChild(option);
});
// 缓存配置
const CACHE_NAME = 'flash-game-cache';
const CACHE_EXPIRY_DAYS = 7; // 缓存有效期(天)
const EXPIRY_CHECK_INTERVAL_HOURS = 6; // 自动清理过期缓存的间隔(小时)
// 缓存辅助函数
function getExpiryKey(url) {
return `cache_expiry_${btoa(url).replace(/=/g, '')}`;
}
function setExpiryForUrl(url, expiryTs) {
try {
localStorage.setItem(getExpiryKey(url), expiryTs.toString());
} catch (e) {}
}
function getExpiryForUrl(url) {
const val = localStorage.getItem(getExpiryKey(url));
return val ? parseInt(val, 10) : null;
}
function removeExpiryForUrl(url) {
localStorage.removeItem(getExpiryKey(url));
}
// 将 SWF 存入 CacheStorage,同时记录过期时间
async function saveToCache(url, response) {
try {
const cache = await caches.open(CACHE_NAME);
await cache.put(url, response.clone());
const expiryTs = Date.now() + CACHE_EXPIRY_DAYS * 24 * 60 * 60 * 1000;
setExpiryForUrl(url, expiryTs);
console.log(`Cache Stored: ${url}, valid until ${new Date(expiryTs).toLocaleString()}`);
} catch (err) {
console.error('Cache Storage failed:', err);
}
}
// 从缓存获取 SWF 的 Blob(自动判断是否过期,过期则删除并返回 null)
async function getFromCache(url) {
try {
const cache = await caches.open(CACHE_NAME);
const response = await cache.match(url);
if (!response) return null;
const expiry = getExpiryForUrl(url);
if (expiry && Date.now() > expiry) {
// 缓存已过期,删除
await cache.delete(url);
removeExpiryForUrl(url);
console.log(`Cache Expired and deleted: ${url}`);
return null;
}
return await response.blob();
} catch (err) {
console.error('Cache Read failed:', err);
return null;
}
}
// 清理所有过期缓存(扫描整个 CacheStorage)
async function cleanExpiredCache() {
try {
const cache = await caches.open(CACHE_NAME);
const requests = await cache.keys();
let deletedCount = 0;
for (const request of requests) {
const url = request.url;
const expiry = getExpiryForUrl(url);
if (expiry === null || Date.now() > expiry) {
await cache.delete(request);
removeExpiryForUrl(url);
deletedCount++;
}
}
if (deletedCount) console.log(`Cache Cleaned ${deletedCount} expired files`);
} catch (err) {
console.error('Cache Cleanup error:', err);
}
}
window.RufflePlayer = window.RufflePlayer || {};
//Ruffle Player的基本配置
window.RufflePlayer.config = {
"allowScriptAccess": true,
"autoplay": "on",
"showSwfDownload": true,
"upgradeToHttps": window.location.protocol === "https:",
//缩放设置,防止当前swf不应显示的素材显示出来
"scale": "showAll",
"forceAlign": "true",
"forceScale": "true",
"letterbox": "on",
"quality": "low",
"openUrlMode": "confirm",
"allowNetworking": "all",
"preferredRenderer": "wgpu-webgl",
"playerVersion": "32",
"playerRuntime": "flashPlayer",
};
const isLoadingWarning = document.getElementById('isLoadingWarning');
isLoadingWarning.textContent = '正在从JsDelivr加载Flash文件,请稍候...';
function showLoadingWarning() {
isLoadingWarning.style.display = 'block';
}
function hideLoadingWarning() {
isLoadingWarning.style.display = 'none';
}
// 全局加载标志
let isLoading = false;
// 播放器创建与缓存加载
let currentBlobUrl = null; // 用于释放之前的 Blob URL
window.addEventListener("load", async () => {
// 初次清理过期缓存,并定时清理
await cleanExpiredCache();
setInterval(cleanExpiredCache, EXPIRY_CHECK_INTERVAL_HOURS * 60 * 60 * 1000);
// 创建 Ruffle 播放器
const ruffle = window.RufflePlayer.newest();
const player = ruffle.createPlayer();
const container = document.getElementById("player");
container.appendChild(player);
//记录当前选择的游戏并存入localStorage
document.querySelector('#gameSelect').addEventListener('change', function(event) {
localStorage.setItem('selectedOption', event.target.value);
});
// 绑定加载按钮事件(带缓存逻辑)
const loadButton = document.getElementById("loadButton");
loadButton.addEventListener("click", async () => {
// 防止重复加载
if (isLoading) {
showLoadingWarning(); // 再次提醒用户正在加载(不重复请求)
return;
}
const selectedOption = document.getElementById('gameSelect').selectedOptions[0];
if (!selectedOption || !selectedOption.id) {
console.warn("Please select a game first");
return;
}
const gameUrl = selectedOption.id;
// 释放上一个游戏占用的 Blob URL
if (currentBlobUrl) {
URL.revokeObjectURL(currentBlobUrl);
currentBlobUrl = null;
}
try {
let blobData = null;
// 尝试从缓存读取
blobData = await getFromCache(gameUrl);
if (blobData) {
console.info(`Loading game from cache: ${selectedOption.textContent}`);
} else {
// 缓存未命中,显示加载提示
isLoading = true;
showLoadingWarning();
// 缓存未命中,从网络下载
console.info(`Downloading game from network: ${selectedOption.textContent}`);
const response = await fetch(gameUrl);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
// 先克隆再消费,避免 body 已被使用
const responseForCache = response.clone();
blobData = await response.blob();
saveToCache(gameUrl, responseForCache).catch(e => console.warn);
}
// 生成 Blob URL 并传递给 Ruffle
const blobUrl = URL.createObjectURL(blobData);
currentBlobUrl = blobUrl;
await player.ruffle().load(blobUrl);
console.info("Ruffle successfully loaded the game");
} catch (error) {
console.error("Game loading failed:", error);
} finally {
if (isLoading) {
isLoading = false;
hideLoadingWarning();
}
}
});
const chooseFile = document.getElementById('chooseFile');
const fileInput = document.getElementById('fileInput');
chooseFile.addEventListener('click', function() {
fileInput.click();
});
//单独为用户从本地选择的swf文件加载Ruffle Player
fileInput.addEventListener('change', function() {
const fileChosen = fileInput.files[0];
const fileChosenURL = URL.createObjectURL(fileChosen);
player.ruffle().load(fileChosenURL).then(() => {
console.info("Ruffle successfully loaded the file");
}).catch((e) => {
console.error(`Ruffle failed to load the file: ${e}`);
})
.finally(() => {
// 释放 Blob URL,释放内存
URL.revokeObjectURL(fileChosenURL);
// 重置 input,确保再次选择同一文件仍能触发 change
fileInput.value = '';
});
});
const chooseFontFile = document.getElementById('chooseFontFile');
const fontFileInput = document.getElementById('fontFileInput');
chooseFontFile.addEventListener('click', function() {
fontFileInput.click();
});
//导入SWF文件所需的字体,仅限汉化后的SWF
let timeoutId;
fontFileInput.addEventListener('change', function() {
const fontFileChosen = fontFileInput.files[0];
const fontFileChosenURL = URL.createObjectURL(fontFileChosen);
window.RufflePlayer.config.fontSources = [fontFileChosenURL]
console.log('User-defined fonts have been imported:', fontFileChosenURL);
const fontChangeWarning = document.getElementById('fontChangeWarning');
fontChangeWarning.textContent = '用户自定义字体已加载';
fontChangeWarning.style.display = 'block';
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
fontChangeWarning.style.display = 'none';
}, 3000);
});
});
//修改偏好渲染器并显示提醒弹窗
const selectRenderer = document.getElementById('renderer');
let timeoutId;
selectRenderer.addEventListener('change', (event) => {
const selectedRenderer = event.target.value;
window.RufflePlayer.config.preferredRenderer = selectedRenderer
console.log('Preferred renderer set to:', selectedRenderer);
const rendererChangeWarning = document.getElementById('rendererChangeWarning');
rendererChangeWarning.textContent = '渲染器已更改为“' + selectedRenderer + '”,请重新加载游戏以使更改生效。';
rendererChangeWarning.style.display = 'block';
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
rendererChangeWarning.style.display = 'none';
}, 3000);
});
//点击使用方法按钮显示或隐藏帮助信息
function helpInfo() {
var div = document.getElementById("helpInfoDiv");
div.classList.toggle("helpInfoDivHide");
}
//判断是否处于手机视图,条件为屏幕宽度小于等于768px且指针为粗略类型(即触摸屏)
function isMobileView() {
const mediaQuery = window.matchMedia('(max-width: 576px) and (pointer: coarse)');
// 如果条件生效(即处于手机视图),.matches 会返回 true
return mediaQuery.matches;
}
//以4比3比例动态更改Ruffle Player的大小
function changeStyle() {
const aspectRatio = window.innerWidth / window.innerHeight;
const playerDiv = document.querySelector('.player');
if (!playerDiv) return;
if (aspectRatio < 1.25 && isMobileView()) {
playerDiv.style.width = 'calc(100% - 10px)';
playerDiv.style.removeProperty('height');
} else if (aspectRatio < 1.25) {
playerDiv.style.width = 'calc(100% - 30px)';
playerDiv.style.removeProperty('height');
} else {
playerDiv.style.height = 'calc(100% - 70px)';
playerDiv.style.removeProperty('width');
}
}
changeStyle();
window.onresize = changeStyle;
function updateRuffleModalStyle() {
// 尝试获取宿主元素
const hostElement = document.querySelector('ruffle-player');
if (!hostElement) return;
// 尝试获取 shadowRoot
const shadow = hostElement.shadowRoot;
if (!shadow) return;
// 尝试获取内部的 modal-area
const innerSaveManager = shadow.querySelector('.modal-area');
if (!innerSaveManager) return;
// 如果以上都找到了,说明 Ruffle 已经就绪
if (isMobileView()) {
innerSaveManager.style.width = '280px';
innerSaveManager.style.height = '260px';
}
// 成功修改后,清除定时器,不再循环检查
clearInterval(checkRuffleTimer);
}
// 每 100 毫秒检查一次(由于是异步加载,用轮询最稳妥)
const checkRuffleTimer = setInterval(updateRuffleModalStyle, 100);
// 如果窗口大小改变,除了改变 player,可能还需要重新判定 modal-area 的大小
window.addEventListener('resize', () => {
changeStyle();
const hostElement = document.querySelector('ruffle-player');
if (hostElement && hostElement.shadowRoot) {
const innerSaveManager = hostElement.shadowRoot.querySelector('.modal-area');
if (innerSaveManager) {
// 重新判断是否是手机视图
if (isMobileView()) {
innerSaveManager.style.width = '280px';
innerSaveManager.style.height = '260px';
} else {
// 如果不是手机视图,恢复默认宽度
innerSaveManager.style.removeProperty('width');
innerSaveManager.style.removeProperty('height');
}
}
}
});
//当网页加载完成后恢复记忆选择的游戏
const saved = localStorage.getItem('selectedOption');
if (saved) {
document.getElementById('gameSelect').value = saved;
}
</script>
</body>
</html>