-
Notifications
You must be signed in to change notification settings - Fork 14
/
minecraft-web.js
192 lines (160 loc) · 4.69 KB
/
minecraft-web.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
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
/**
* Downloads a file from a url and writes it to the CheerpJ filesystem.
* @param {string} url
* @param {string} destPath
* @param {(downloadedBytes: number, totalBytes: number) => void} [progressCallback]
* @returns {Promise<void>}
*/
async function downloadFileToCheerpJ(url, destPath, progressCallback) {
const response = await fetch(url);
const reader = response.body.getReader();
const contentLength = +response.headers.get('Content-Length');
const bytes = new Uint8Array(contentLength);
progressCallback?.(0, contentLength);
let pos = 0;
while (true) {
const { done, value } = await reader.read();
if (done)
break;
bytes.set(value, pos);
pos += value.length;
progressCallback?.(pos, contentLength);
}
// Write to CheerpJ filesystem
return new Promise((resolve, reject) => {
cheerpOSOpen(cjFDs, destPath, "w", fd => {
cheerpOSWrite(cjFDs, fd, bytes, 0, bytes.length, w => {
cheerpOSClose(cjFDs, fd);
resolve();
});
});
});
}
const template = document.createElement('template');
template.innerHTML = `
<style>
:host {
display: inline-block;
aspect-ratio: 854 / 480;
background: black;
color: #eee;
color-scheme: dark;
width: 854px;
height: 480px;
}
:host([hidden]) {
display: none;
}
canvas {
width: inherit;
height: inherit;
}
.display {
width: 854px;
height: 480px;
position: absolute;
inset: 0;
visibility: hidden;
}
.intro {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
}
p {
max-width: 60ch;
}
.disclaimer {
font-size: 0.8em;
opacity: 0.5;
}
button {
padding: 0.5em 1em;
margin: 2em;
}
progress {
width: calc(100% - 2em);
margin: 1em;
}
*:focus {
outline: none;
}
</style>
<canvas width="854" height="480" tabindex="-1"></canvas>
<div class="display"></div>
<div class="intro">
<p>
This is a proof-of-concept demo of Minecraft 1.2.5 running unmodified in the browser.
</p>
<p>
Clicking the button below will download the client from mojang.com.
By clicking it, you agree to the <a href="https://www.minecraft.net/eula">Minecraft EULA</a>.
</p>
<button>Play!</button>
<div class="disclaimer">
This is not an official Minecraft product. It is not approved by or associated with Mojang or Microsoft.
</div>
</div>
<progress style="display: none"></progress>
`;
export default class MinecraftClient extends HTMLElement {
#canvas;
#progress;
#button;
#display;
#intro;
#isRunning;
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(template.content.cloneNode(true));
this.#button = shadowRoot.querySelector('button');
this.#button.addEventListener('click', () => this.run());
this.#canvas = shadowRoot.querySelector('canvas');
this.#canvas.width = 854;
this.#canvas.height = 480;
this.#canvas.tabIndex = -1;
this.#canvas.style.display = 'none';
this.#progress = shadowRoot.querySelector('progress');
this.#progress.style.display = 'none';
this.#intro = shadowRoot.querySelector('.intro');
// CheerpJ needs an element to render to, but we are going to render to own canvas
this.#display = shadowRoot.querySelector('.display');
this.#display.setAttribute('style', 'width:100%;height:100%;position:absolute;top:0;left:0px;visibility:hidden;');
cheerpjCreateDisplay(-1, -1, this.#display);
this.#isRunning = false;
}
static register() {
customElements.define('minecraft-client', this);
}
/** @returns {Promise<number>} Exit code */
async run() {
if (this.#isRunning) {
throw new Error('Already running');
}
this.#intro.style.display = 'none';
this.#progress.style.display = 'unset';
const jarPath = "/files/client_1.2.5.jar"
await downloadFileToCheerpJ(
"https://piston-data.mojang.com/v1/objects/4a2fac7504182a97dcbcd7560c6392d7c8139928/client.jar",
jarPath,
(downloadedBytes, totalBytes) => {
this.#progress.value = downloadedBytes;
this.#progress.max = totalBytes;
}
);
this.#progress.style.display = 'none';
this.#canvas.style.display = 'unset';
window.lwjglCanvasElement = this.#canvas;
const exitCode = await cheerpjRunMain("net.minecraft.client.Minecraft", `/app/lwjgl-2.9.0.jar:/app/lwjgl_util-2.9.0.jar:${jarPath}`)
this.#canvas.style.display = 'none';
this.#isRunning = false;
return exitCode;
}
/** @returns {boolean} */
get isRunning() {
return this.#isRunning;
}
}