-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpouet-parade.js
160 lines (146 loc) · 5.42 KB
/
pouet-parade.js
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
async function start(config) {
const {
screen,
nav,
demosUrl,
dosImgUrl,
biosUrl,
vgaBiosUrl,
stateUrl,
which
} = config;
const elements = {
nameLabel: nav.querySelector('.demoName'),
authorLabel: nav.querySelector('.demoAuthor'),
sizeLabel: nav.querySelector('.demoSize'),
sourceLink: nav.querySelector('.demoLink'),
prevButton: nav.querySelector('.demoPrev'),
randButton: nav.querySelector('.demoRand'),
nextButton: nav.querySelector('.demoNext'),
shareLink: nav.querySelector('.demoShare'),
searchInput: nav.querySelector('.demoSearch input'),
demoNameList: nav.querySelector('.demoSearch datalist')
};
// Load demo content
const demoResponse = await fetch(demosUrl);
const demos = await demoResponse.json();
// Load DOS floppy image
const dosImgResponse = await fetch(dosImgUrl);
const dosImg = new Uint8Array(await dosImgResponse.arrayBuffer());
// Select a random demo and replace RUN.COM
let demo;
if (which) {
demo = demos.find(({ id }) => id === which);
if (!demo)
location.href = '.';
}
if (!demo)
demo = demos[(Math.random() * demos.length) | 0];
const contentUrl = `data:application/octet-binary;base64,${demo.content}`;
const contentResponse = await fetch(contentUrl);
const content = new Uint8Array(await contentResponse.arrayBuffer());
dosImg.set(content, 0x26000);
// Fill in links
elements.nameLabel.innerText = demo.name;
elements.sizeLabel.innerText = `${content.length} bytes`;
const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
if (demo.byType === 'group') {
const links = elements.authorLabel.innerHTML = demo.by
.map(({ id, name }) =>
`<a href="https://www.pouet.net/groups.php?which=${id}">${name}</a>`);
elements.authorLabel.innerHTML = `by ${formatter.format(links)}`;
} else if (demo.byType === 'user') {
const links = demo.by
.map(({ id, name }) =>
`<a href="https://www.pouet.net/user.php?who=${id}">${name}</a>`);
elements.authorLabel.innerHTML = `by ${formatter.format(links)}`;
} else {
const links = demo.by
.map(({ id, name }) =>
`<a href="https://www.pouet.net/user.php?who=${id}">${name}</a>`);
elements.authorLabel.innerHTML = `uploaded to pouet by ${formatter.format(links)}`;
}
elements.sourceLink.href = `https://www.pouet.net/prod.php?which=${demo.id}`;
const demoIndex = demos.findIndex(({ id }) => id === demo.id);
if (demoIndex == 0)
elements.prevButton.disabled = true;
else {
const prevId = demos[demoIndex - 1].id;
elements.prevButton.href = `?which=${prevId}`;
}
if (demoIndex === demos.length - 1)
elements.nextButton.disabled = true;
else {
const nextId = demos[demoIndex + 1].id;
elements.nextButton.href = `?which=${nextId}`;
}
const shareUrl = `${location.href.split('?')[0]}?which=${demo.id}`
elements.shareLink.value = shareUrl;
// for debugging - use to download image
/*
if (window.location.search.includes("download")) {
const blob=new Blob([dosImg], {type: "application/octet-stream"});
const link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="demo.img";
link.click();
}
*/
// Add search fields
const df = document.createDocumentFragment();
for (const demo of demos) {
const option = document.createElement('option');
option.setAttribute('value', `${demo.name}\u2063`);
option.textContent = `${~~(demo.content.length * 3 / 4)} bytes`;
df.appendChild(option);
}
elements.demoNameList.appendChild(df);
elements.searchInput.addEventListener('change', () => {
if (elements.searchInput.value.endsWith('\u2063')) {
// Invisible character means user selected from data list
const name = elements.searchInput.value.slice(0, -1);
const demo = demos.find(v => v.name === name);
if (demo)
location.href = `?which=${demo.id}`;
}
});
elements.searchInput.addEventListener('keyup', ev => {
if (ev.key === 'Enter') {
const searchField = elements.searchInput.value.toLowerCase().trim();
const demo = demos.find(v => v.name.toLowerCase().includes(searchField));
if (demo)
location.href = `?which=${demo.id}`;
}
})
// Start emulator
const v86Config = {
screen_container: screen,
bios: {
url: biosUrl,
},
vga_bios: {
url: vgaBiosUrl,
},
fda: {
buffer: dosImg.buffer
},
initial_state: {
url: stateUrl
},
autostart: true
};
const emulator = new V86Starter(v86Config);
return emulator;
}
const params = new URLSearchParams(location.search);
const which = params.get('which') ?? null;
start({
screen: document.getElementById('screen_container'),
nav: document.querySelector('nav'),
demosUrl: 'demos/demos.json',
dosImgUrl: 'image/freedos.img',
biosUrl: 'bios/bochs-bios.bin',
vgaBiosUrl: 'bios/bochs-vgabios.bin',
stateUrl: 'image/v86state.bin',
which
});