-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstats-webview.ts
189 lines (163 loc) · 5.74 KB
/
stats-webview.ts
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
import { EventEmitter } from 'events';
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import { DebugProtocol } from '@vscode/debugprotocol';
import * as bin from '../dbg/binary-dto';
export class StatsWebview {
private static _state: {
current?: ImageData,
sprites: ImageData[],
screenText?: ImageData,
memory: number[],
palette: number[],
banks: bin.SingleBankMeta[]
registers: bin.SingleRegisterInfo[]
metas: bin.SingleRegisterMeta[]
disassembly: DebugProtocol.DisassembledInstruction[]
} = {
sprites: [],
memory: [],
palette: [],
banks: [],
registers: [],
metas: [],
disassembly: [],
};
private static _emitter: EventEmitter = new EventEmitter();
public static readonly viewType = 'statsWebview';
private static _currentPanel: StatsWebview | undefined;
private readonly _panel: vscode.WebviewPanel;
private readonly _extensionPath: string;
private _disposables: vscode.Disposable[] = [];
public static reset() {
StatsWebview._state.current = <any>null;
StatsWebview._state.screenText = <any>null;
StatsWebview._state.sprites = [];
StatsWebview._state.memory = [];
StatsWebview._state.palette = [];
StatsWebview._state.banks = [];
StatsWebview._state.registers = [];
StatsWebview._state.metas = [];
StatsWebview._state.disassembly = [];
if(StatsWebview._currentPanel) {
StatsWebview._currentPanel._panel.webview.postMessage({
reset: true,
});
}
}
public static update(update: Partial<typeof StatsWebview._state>) {
Object.assign(StatsWebview._state, update);
if(StatsWebview._currentPanel) {
StatsWebview._currentPanel._panel.webview.postMessage(StatsWebview._state);
}
}
public static addEventListener(event: string, cb: (...args: any[]) => void) {
StatsWebview._emitter.addListener(event, cb);
}
public static async maybeCreate(extensionPath: string) : Promise<void> {
if (StatsWebview._currentPanel) {
return;
}
const panel = vscode.window.createWebviewPanel(
StatsWebview.viewType,
'CC65 - Run',
vscode.ViewColumn.Two,
{
retainContextWhenHidden: true,
enableScripts: true,
localResourceRoots: [vscode.Uri.file(path.join(extensionPath, 'dist'))]
}
);
panel.webview.onDidReceiveMessage((evt) => {
const r = evt.request;
if(r == 'keydown' || r == 'keyup' || r == 'offset' || r == 'bank') {
StatsWebview._emitter.emit(r, evt);
}
});
StatsWebview._currentPanel = new StatsWebview(panel, extensionPath);
await StatsWebview._currentPanel._init();
StatsWebview.update(StatsWebview._state);
}
public static async revive(panel: vscode.WebviewPanel, extensionPath: string) {
StatsWebview._currentPanel = new StatsWebview(panel, extensionPath);
await StatsWebview._currentPanel._init();
}
private constructor(panel: vscode.WebviewPanel, extensionPath: string) {
this._panel = panel;
this._extensionPath = extensionPath;
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
}
public dispose() {
StatsWebview._currentPanel = undefined;
this._panel.dispose();
while (this._disposables.length) {
const x = this._disposables.pop();
if (x) {
x.dispose();
}
}
}
private async _init() : Promise<void> {
const webview = this._panel.webview;
this._panel.title = 'CC65 - Run';
this._panel.webview.html = await this._getHtmlForWebview(webview);
}
private async _getHtmlForWebview(webview: vscode.Webview) : Promise<string> {
let scriptUri : vscode.Uri;
let connectSrc = "'none'";
try {
await fs.promises.stat(this._extensionPath + '/.git');
scriptUri = vscode.Uri.parse(
'http://localhost:8794/dist/webviews.js'
);
connectSrc = "http://localhost:8794 ws://localhost:8794";
}
catch {
const sp = vscode.Uri.file(
path.join(this._extensionPath, 'dist', 'webviews.js')
);
scriptUri = webview.asWebviewUri(sp);
}
const cssPathOnDisk = vscode.Uri.file(
path.join(this._extensionPath, 'dist/styles.css')
);
const c64TtfPathOnDisk = vscode.Uri.file(
path.join(this._extensionPath, 'dist', 'c64.ttf')
);
const cssUri = webview.asWebviewUri(cssPathOnDisk);
const c64TtfUri = webview.asWebviewUri(c64TtfPathOnDisk);
const nonce = getNonce();
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; connect-src ${connectSrc}; font-src ${webview.cspSource}; style-src 'nonce-${nonce}' ${webview.cspSource}; img-src blob: ${webview.cspSource}; script-src 'nonce-${nonce}';">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style nonce="${nonce}" type="text/css">
@font-face {
font-family: "C64";
src: url("${c64TtfUri}") format("truetype");
}
</style>
<link rel="stylesheet" type="text/css" href="${cssUri}" nonce="${nonce}" />
<title>Thoughts of a Dying Atheist</title>
</head>
<body>
<div id="content"></div>
<script nonce="${nonce}" type="text/javascript" src="${scriptUri}"></script>
<script nonce="${nonce}" type="text/javascript">
window.statsWebView();
</script>
</body>
</html>`;
}
}
function getNonce() {
let text = '';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < 32; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}