Skip to content

Commit f8fdd66

Browse files
committed
🐛 use TextDecoder
to replace Uint8array toString method
1 parent e64ee37 commit f8fdd66

File tree

12 files changed

+69
-47
lines changed

12 files changed

+69
-47
lines changed

i18n/i18n.zh-cn.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
"num.bin": "二进制数",
5353
"dosbox.console.stdout": "[DOSBox 控制台标准输出] No.{0}",
5454
"dosbox.console.stderr": "[DOSBox 控制台标准错误输出] No.{0}",
55-
"openemu.msg": "\n[执行命令] 打开DOS模拟器并准备相关环境",
56-
"run.msg": "\n[执行命令] 在{1}中使用{0}编译运行文件: ",
57-
"debug.msg": "\n[执行命令] 在{1}中使用{0}调试汇编文件:",
55+
"openemu.msg": "[执行命令] 在文件的位置打开DOS模拟器{0}并配置{2}环境\n\t{1}",
56+
"run.msg": "[执行命令] 在{2}中使用{1}编译运行文件\n\t{0}: ",
57+
"debug.msg": "[执行命令] 在{2}中使用{1}调试汇编文件\n\t{0}:",
5858
"copy.msg": "\n复制文件为{0}",
5959
"runcode.error": "{0}汇编出错,无法运行/调试,详细信息见输出面板",
6060
"runcode.warn": "成功汇编链接生成EXE,但是汇编时产生了警告信息({0}warning),可能无法运行/调试,是否继续操作",

src/ASM/main.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@ import * as statusBar from './statusBar';
77
import * as Diag from '../diagnose/main';
88
import * as conf from '../utils/configuration';
99
import { messageCollector } from '../diagnose/messageCollector';
10+
import { logger } from '../utils/logger';
1011

1112
const fs = vscode.workspace.fs;
1213

13-
enum actionType {
14-
open,
15-
run,
16-
debug
17-
}
18-
1914
type ACTIONS = {
2015
[id: string]: {
2116
baseBundle: string,
@@ -41,7 +36,8 @@ export async function activate(context: vscode.ExtensionContext) {
4136
const assemblyToolsFolder = vscode.Uri.joinPath(context.globalStorageUri, conf.extConf.asmType);
4237
const seperateSpaceFolder = vscode.Uri.joinPath(context.globalStorageUri, "workspace");
4338

44-
async function singleFileMode(type: actionType, _uri: vscode.Uri): Promise<AsmResult> {
39+
async function singleFileMode(act: conf.actionType, _uri: vscode.Uri): Promise<AsmResult> {
40+
logger.actionLog(act, _uri);
4541

4642
if (nodefs.existsSync(seperateSpaceFolder.fsPath)) {
4743
await fs.delete(seperateSpaceFolder, { recursive: true, useTrash: false });
@@ -95,10 +91,10 @@ export async function activate(context: vscode.ExtensionContext) {
9591
}
9692
return r + " >>C:\\" + logFilename;
9793
}
98-
if (type === actionType.run) {
94+
if (act === conf.actionType.run) {
9995
autoexec.push(...action.run.map(cb));
10096
}
101-
if (type === actionType.debug) {
97+
if (act === conf.actionType.debug) {
10298
autoexec.push(...action.debug.map(cb));
10399
}
104100

@@ -108,7 +104,7 @@ export async function activate(context: vscode.ExtensionContext) {
108104
await box.fromBundle(bundle, assemblyToolsFolder);
109105
box.updateAutoexec(autoexec);
110106

111-
if (type !== actionType.open) {
107+
if (act !== conf.actionType.open) {
112108
const [hook, promise] = messageCollector();
113109
nodefs.watchFile(logUri.fsPath, () => {
114110
try {
@@ -153,15 +149,15 @@ export async function activate(context: vscode.ExtensionContext) {
153149
}
154150
return r;
155151
}
156-
if (type === actionType.run) {
152+
if (act === conf.actionType.run) {
157153
autoexec.push(...action.run.map(cb));
158154
}
159-
if (type === actionType.debug) {
155+
if (act === conf.actionType.debug) {
160156
autoexec.push(...action.debug.map(cb));
161157
}
162158
api.jsdos.updateAutoexec(autoexec);
163159
const webview = await api.jsdos.runInWebview();
164-
if (type !== actionType.open) {
160+
if (act !== conf.actionType.open) {
165161
const [hook, promise] = messageCollector();
166162
webview.onDidReceiveMessage(e => {
167163
switch (e.command) {
@@ -184,7 +180,7 @@ export async function activate(context: vscode.ExtensionContext) {
184180
(terminal as vscode.Terminal).sendText(val.replace('C:', assemblyToolsFolder.fsPath));
185181
}
186182
);
187-
if (type === actionType.open) {
183+
if (act === conf.actionType.open) {
188184
terminal.sendText(`cd "${vscode.Uri.joinPath(_uri, '..').fsPath}"`);
189185
}
190186
else {
@@ -199,10 +195,10 @@ export async function activate(context: vscode.ExtensionContext) {
199195
return r + `>> ${logFilename} \n type ${logFilename}`;
200196
}
201197
}
202-
if (type === actionType.run) {
198+
if (act === conf.actionType.run) {
203199
action.run.map(cb).forEach(val => (terminal as vscode.Terminal).sendText(val));
204200
}
205-
if (type === actionType.debug) {
201+
if (act === conf.actionType.debug) {
206202
action.debug.map(cb).forEach(val => (terminal as vscode.Terminal).sendText(val));
207203
}
208204
const logUri = vscode.Uri.joinPath(folder, logFilename);
@@ -234,8 +230,8 @@ export async function activate(context: vscode.ExtensionContext) {
234230
}
235231

236232
context.subscriptions.push(
237-
vscode.commands.registerCommand('masm-tasm.openEmulator', (uri: vscode.Uri) => singleFileMode(actionType.open, uri)),
238-
vscode.commands.registerCommand('masm-tasm.runASM', (uri: vscode.Uri) => singleFileMode(actionType.run, uri)),
239-
vscode.commands.registerCommand('masm-tasm.debugASM', (uri: vscode.Uri) => singleFileMode(actionType.debug, uri))
233+
vscode.commands.registerCommand('masm-tasm.openEmulator', (uri: vscode.Uri) => singleFileMode(conf.actionType.open, uri)),
234+
vscode.commands.registerCommand('masm-tasm.runASM', (uri: vscode.Uri) => singleFileMode(conf.actionType.run, uri)),
235+
vscode.commands.registerCommand('masm-tasm.debugASM', (uri: vscode.Uri) => singleFileMode(conf.actionType.debug, uri))
240236
);
241237
}

src/language/hoverFelix.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Uri, workspace } from "vscode";
2+
import { logger } from "../utils/logger";
23

34
const fs = workspace.fs;
45

@@ -28,10 +29,11 @@ export class FELIX {
2829
static async create(fileuri: Uri): Promise<FELIX> {
2930
const arr = await fs.readFile(fileuri);
3031
try {
31-
const json = JSON.parse(arr.toString());
32+
const data = new TextDecoder().decode(arr);
33+
const json = JSON.parse(data);
3234
return new FELIX(json);
3335
} catch (e) {
34-
console.error(e, arr.toString());
36+
console.error(e);
3537
}
3638
throw new Error();
3739
}

src/language/hoverFromMarkdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class HoverFromMarkdown {
1919
}
2020
static async create(uri: Uri): Promise<HoverFromMarkdown> {
2121
const arr = await fs.readFile(uri);
22-
const str = arr.toString().replace(/\r\n/g, '\n');
22+
const str = new TextDecoder().decode(arr).replace(/\r\n/g, '\n');
2323
const target: HoverInfoItem[] = [];
2424
const regex = /```yaml\n([\s\S]+?)\n```([\s\S]+?)(?=```)/g;
2525
let re = regex.exec(str);

src/utils/browser-fetch.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/utils/configuration.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export enum DosEmulatorType {
2222
jsdos = 'jsdos',
2323
}
2424

25+
export enum actionType {
26+
open,
27+
run,
28+
debug
29+
}
30+
2531
import * as vscode from 'vscode';
2632

2733
class ExtensionConfiguration {

src/utils/i18n.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { workspace, ExtensionContext, env, Uri } from "vscode";
2+
import * as conf from './configuration';
3+
24
const fs = workspace.fs;
35

46
let text: { [id: string]: string } | null = null;
@@ -19,4 +21,15 @@ export function localize(key: string, value: string, ...args: string[]): string
1921
value = value.replace(`{${argidx}}`, args[argidx]);
2022
}
2123
return value;
24+
}
25+
26+
export function actionMessage(act: conf.actionType, file: string, emu: conf.DosEmulatorType, asm: conf.Assembler): string {
27+
switch (act) {
28+
case conf.actionType.open:
29+
return localize("openemu.msg", "open DOS emulator {2} set {1} in folder of: \n\t{0}", file, asm, emu);
30+
case conf.actionType.run:
31+
return localize("run.msg", "run code with {1} in {2}\n\t{0}", file, asm, emu);
32+
case conf.actionType.debug:
33+
return localize("debug.msg", "debug code with {1} in {2}\n\t{0}", file, asm, emu);
34+
}
2235
}

src/utils/logger.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import * as vscode from 'vscode';
2+
import { actionMessage, localize } from './i18n';
3+
import * as conf from './configuration';
24

35
class Logger {
46
outputChannel: vscode.OutputChannel = vscode.window.createOutputChannel('masm-tasm');
57
log = console.log;
68
warn = console.warn;
79
error = console.error;
10+
localize = localize;
11+
actionLog(act: conf.actionType, uri: vscode.Uri) {
12+
const message = actionMessage(act, uri.fsPath, conf.extConf.emulator, conf.extConf.asmType);
13+
this.channel('\n' + message + '\n').show();
14+
}
15+
816
channel(value: string) {
917
this.outputChannel.append(value);
1018
return this.outputChannel;

src/web/main.ts renamed to src/web/ASM.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as statusBar from '../ASM/statusBar';
66
import * as Diag from '../diagnose/main';
77
import * as conf from '../utils/configuration';
88
import { messageCollector } from '../diagnose/messageCollector';
9+
import { logger } from '../utils/logger';
910

1011
const fs = vscode.workspace.fs;
1112

@@ -37,13 +38,8 @@ export async function activate(context: vscode.ExtensionContext) {
3738
const vscode_dosbox = vscode.extensions.getExtension('xsro.vscode-dosbox');
3839
const api: API = await vscode_dosbox?.activate();
3940

40-
const assemblyToolsFolder = vscode.Uri.joinPath(context.globalStorageUri, conf.extConf.asmType);
41-
const seperateSpaceFolder = vscode.Uri.joinPath(context.globalStorageUri, "workspace");
42-
43-
async function singleFileMode(type: actionType, _uri: vscode.Uri): Promise<AsmResult> {
44-
45-
await fs.createDirectory(assemblyToolsFolder);
46-
41+
async function singleFileMode(act: actionType, _uri: vscode.Uri): Promise<AsmResult> {
42+
logger.actionLog(act, _uri);
4743
const actions: ACTIONS | undefined = vscode.workspace.getConfiguration('masmtasm').get('ASM.actions');
4844
if (actions === undefined) {
4945
throw new Error("configurate `masmtasm.ASM.actions` first");
@@ -59,13 +55,12 @@ export async function activate(context: vscode.ExtensionContext) {
5955
const bundle = await fs.readFile(bundlePath);
6056

6157
let result = "<should-not-return>";
62-
const uri = vscode.Uri.joinPath(seperateSpaceFolder, ("test" + path.extname(_uri.fsPath)).toUpperCase());
63-
await fs.copy(_uri, uri);
64-
const fileInfo = path.parse(uri.fsPath);
58+
59+
const fileInfo = path.parse(_uri.fsPath);
6560

6661
if (conf.extConf.emulator === conf.DosEmulatorType.jsdos) {
6762
await api.jsdos.jszip.loadAsync(bundle);
68-
api.jsdos.jszip.file('code/' + fileInfo.base, doc.getText());
63+
api.jsdos.jszip.file('code/test' + fileInfo.ext, doc.getText());
6964
const autoexec = [
7065
`mount c .`,
7166
`mount d ./code`,
@@ -74,22 +69,22 @@ export async function activate(context: vscode.ExtensionContext) {
7469
];
7570
function cb(val: string) {
7671
const r = val
77-
.replace("${file}", fileInfo.base)
78-
.replace("${filename}", fileInfo.name);
72+
.replace("${file}", 'test' + fileInfo.ext)
73+
.replace("${filename}", 'test');
7974
if (val.startsWith('>')) {
8075
return r.replace(">", "");
8176
}
8277
return r;
8378
}
84-
if (type === actionType.run) {
79+
if (act === actionType.run) {
8580
autoexec.push(...action.run.map(cb));
8681
}
87-
if (type === actionType.debug) {
82+
if (act === actionType.debug) {
8883
autoexec.push(...action.debug.map(cb));
8984
}
9085
api.jsdos.updateAutoexec(autoexec);
9186
const webview = await api.jsdos.runInWebview();
92-
if (type !== actionType.open) {
87+
if (act !== actionType.open) {
9388
const [hook, promise] = messageCollector();
9489
webview.onDidReceiveMessage(e => {
9590
switch (e.command) {

src/web/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
22
import { localize, loadI18n } from '../utils/i18n';
33

44
import * as lan from '../language/main';
5-
import * as asm from './main';
5+
import * as asm from './ASM';
66

77
export function activate(context: vscode.ExtensionContext): void {
88

0 commit comments

Comments
 (0)