| 
									
										
										
										
											2022-01-30 03:39:54 +08:00
										 |  |  | import * as zstd from "zstddec"; | 
					
						
							|  |  |  | import { KeyEvent, controlKeyFromJSON, ControlKey } from "./message"; | 
					
						
							|  |  |  | import { KEY_MAP, LANGS } from "./gen_js_from_hbb"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let decompressor: zstd.ZSTDDecoder; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function initZstd() { | 
					
						
							|  |  |  |   const tmp = new zstd.ZSTDDecoder(); | 
					
						
							|  |  |  |   await tmp.init(); | 
					
						
							|  |  |  |   console.log("zstd ready"); | 
					
						
							|  |  |  |   decompressor = tmp; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function decompress(compressedArray: Uint8Array) { | 
					
						
							|  |  |  |   const MAX = 1024 * 1024 * 64; | 
					
						
							|  |  |  |   const MIN = 1024 * 1024; | 
					
						
							|  |  |  |   let n = 30 * compressedArray.length; | 
					
						
							|  |  |  |   if (n > MAX) { | 
					
						
							|  |  |  |     n = MAX; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (n < MIN) { | 
					
						
							|  |  |  |     n = MIN; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     if (!decompressor) { | 
					
						
							|  |  |  |       await initZstd(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return decompressor.decode(compressedArray, n); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     console.error("decompress failed: " + e); | 
					
						
							|  |  |  |     return undefined; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-07 15:00:06 +08:00
										 |  |  | const LANG = getLang(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-30 03:39:54 +08:00
										 |  |  | export function translate(locale: string, text: string): string { | 
					
						
							| 
									
										
										
										
											2022-04-07 15:00:06 +08:00
										 |  |  |   const lang = LANG || locale.substring(locale.length - 2).toLowerCase(); | 
					
						
							| 
									
										
										
										
											2022-01-30 03:39:54 +08:00
										 |  |  |   let en = LANGS.en as any; | 
					
						
							|  |  |  |   let dict = (LANGS as any)[lang]; | 
					
						
							|  |  |  |   if (!dict) dict = en; | 
					
						
							|  |  |  |   let res = dict[text]; | 
					
						
							|  |  |  |   if (!res && lang != "en") res = en[text]; | 
					
						
							|  |  |  |   return res || text; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const zCode = "z".charCodeAt(0); | 
					
						
							|  |  |  | const aCode = "a".charCodeAt(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-05 01:55:23 +08:00
										 |  |  | export function mapKey(name: string, isDesktop: Boolean) { | 
					
						
							| 
									
										
										
										
											2022-02-05 04:28:40 +08:00
										 |  |  |   const tmp = KEY_MAP[name] || name; | 
					
						
							| 
									
										
										
										
											2022-01-30 03:39:54 +08:00
										 |  |  |   if (tmp.length == 1) { | 
					
						
							|  |  |  |     const chr = tmp.charCodeAt(0); | 
					
						
							| 
									
										
										
										
											2022-02-05 01:55:23 +08:00
										 |  |  |     if (!isDesktop && (chr > zCode || chr < aCode)) | 
					
						
							| 
									
										
										
										
											2022-01-30 03:39:54 +08:00
										 |  |  |       return KeyEvent.fromPartial({ unicode: chr }); | 
					
						
							|  |  |  |     else return KeyEvent.fromPartial({ chr }); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-04-25 18:27:15 +08:00
										 |  |  |   const control_key = controlKeyFromJSON(tmp); | 
					
						
							| 
									
										
										
										
											2022-01-30 03:39:54 +08:00
										 |  |  |   if (control_key == ControlKey.UNRECOGNIZED) { | 
					
						
							| 
									
										
										
										
											2022-04-25 18:27:15 +08:00
										 |  |  |     console.error("Unknown control key " + tmp); | 
					
						
							| 
									
										
										
										
											2022-01-30 03:39:54 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return KeyEvent.fromPartial({ control_key }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function sleep(ms: number) { | 
					
						
							|  |  |  |   await new Promise((r) => setTimeout(r, ms)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-04-07 15:00:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function getLang(): string { | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     const queryString = window.location.search; | 
					
						
							|  |  |  |     const urlParams = new URLSearchParams(queryString); | 
					
						
							|  |  |  |     return urlParams.get("lang") || ""; | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     return ""; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |