]> git.localhorst.tv Git - alttp.git/blob - resources/js/helpers/SNESSocket.js
chat bot config
[alttp.git] / resources / js / helpers / SNESSocket.js
1 export default class SNESSocket {
2
3         constructor(uri) {
4                 this.uri = uri;
5                 this.queue = [];
6                 this.device = '';
7                 this.deviceList = [];
8                 this.open();
9         }
10
11         open() {
12                 if (this.sock) {
13                         this.sock.close();
14                         this.sock = null;
15                 }
16                 this.sock = new WebSocket(this.uri);
17                 this.sock.binaryType = 'arraybuffer';
18                 this.sock.onclose = () => {
19                         if (this.onclose) {
20                                 this.onclose();
21                         }
22                 };
23                 this.sock.onerror = (e) => {
24                         if (this.onerror) {
25                                 this.onerror(e);
26                         }
27                 };
28                 this.sock.onopen = () => {
29                         if (this.onopen) {
30                                 this.onopen();
31                         }
32                 };
33                 this.sock.onmessage = (e) => {
34                         if (this.queue.length) {
35                                 const handler = this.queue.shift();
36                                 handler(e.data);
37                         }
38                 };
39         }
40
41         close() {
42                 this.sock.close();
43         }
44
45         isOpen() {
46                 return this.sock.readyState === 1;
47         }
48
49         send(opcode, flags, operands, callback) {
50                 const payload = {
51                         Opcode: opcode,
52                         Space: 'SNES',
53                 };
54                 if (flags) {
55                         payload.Flags = flags;
56                 }
57                 if (operands) {
58                         payload.Operands = operands;
59                 }
60                 this.sock.send(JSON.stringify(payload));
61                 if (callback) {
62                         this.queue.push(callback);
63                 }
64         }
65
66         attachDevice(device) {
67                 this.device = device;
68                 this.send('Attach', null, [device]);
69         }
70
71         readBytes(start, size, callback) {
72                 const handler = (rsp) => {
73                         if (callback) callback(new Uint8Array(rsp));
74                 };
75                 this.send('GetAddress', null, [start.toString(16), size.toString(16)], handler);
76         }
77
78         readSRAM(start, size, callback) {
79                 this.readBytes(start + 0xE00000, size, callback);
80         }
81
82         readVRAM(start, size, callback) {
83                 this.readBytes(start + 0xF70000, size, callback);
84         }
85
86         readWRAM(start, size, callback) {
87                 this.readBytes(start + 0xF50000, size, callback);
88         }
89
90         requestDeviceInfo(callback) {
91                 const handler = (rsp) => {
92                         const data = JSON.parse(rsp);
93                         if (callback) callback(data);
94                 };
95                 this.send('Info', null, null, handler);
96         }
97
98         requestDeviceList(callback) {
99                 const handler = (rsp) => {
100                         const data = JSON.parse(rsp);
101                         this.deviceList = data.Results || [];
102                         if (callback) callback(data);
103                 };
104                 this.send('DeviceList', null, null, handler);
105         }
106
107 }