Skip to content

Commit

Permalink
🐛 use TextDecoder
Browse files Browse the repository at this point in the history
to replace Uint8array toString method
  • Loading branch information
xsro committed Nov 1, 2021
1 parent e64ee37 commit f8fdd66
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 47 deletions.
6 changes: 3 additions & 3 deletions i18n/i18n.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
"num.bin": "二进制数",
"dosbox.console.stdout": "[DOSBox 控制台标准输出] No.{0}",
"dosbox.console.stderr": "[DOSBox 控制台标准错误输出] No.{0}",
"openemu.msg": "\n[执行命令] 打开DOS模拟器并准备相关环境",
"run.msg": "\n[执行命令] 在{1}中使用{0}编译运行文件: ",
"debug.msg": "\n[执行命令] 在{1}中使用{0}调试汇编文件:",
"openemu.msg": "[执行命令] 在文件的位置打开DOS模拟器{0}并配置{2}环境\n\t{1}",
"run.msg": "[执行命令] 在{2}中使用{1}编译运行文件\n\t{0}: ",
"debug.msg": "[执行命令] 在{2}中使用{1}调试汇编文件\n\t{0}:",
"copy.msg": "\n复制文件为{0}",
"runcode.error": "{0}汇编出错,无法运行/调试,详细信息见输出面板",
"runcode.warn": "成功汇编链接生成EXE,但是汇编时产生了警告信息({0}warning),可能无法运行/调试,是否继续操作",
Expand Down
34 changes: 15 additions & 19 deletions src/ASM/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ import * as statusBar from './statusBar';
import * as Diag from '../diagnose/main';
import * as conf from '../utils/configuration';
import { messageCollector } from '../diagnose/messageCollector';
import { logger } from '../utils/logger';

const fs = vscode.workspace.fs;

enum actionType {
open,
run,
debug
}

type ACTIONS = {
[id: string]: {
baseBundle: string,
Expand All @@ -41,7 +36,8 @@ export async function activate(context: vscode.ExtensionContext) {
const assemblyToolsFolder = vscode.Uri.joinPath(context.globalStorageUri, conf.extConf.asmType);
const seperateSpaceFolder = vscode.Uri.joinPath(context.globalStorageUri, "workspace");

async function singleFileMode(type: actionType, _uri: vscode.Uri): Promise<AsmResult> {
async function singleFileMode(act: conf.actionType, _uri: vscode.Uri): Promise<AsmResult> {
logger.actionLog(act, _uri);

if (nodefs.existsSync(seperateSpaceFolder.fsPath)) {
await fs.delete(seperateSpaceFolder, { recursive: true, useTrash: false });
Expand Down Expand Up @@ -95,10 +91,10 @@ export async function activate(context: vscode.ExtensionContext) {
}
return r + " >>C:\\" + logFilename;
}
if (type === actionType.run) {
if (act === conf.actionType.run) {
autoexec.push(...action.run.map(cb));
}
if (type === actionType.debug) {
if (act === conf.actionType.debug) {
autoexec.push(...action.debug.map(cb));
}

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

if (type !== actionType.open) {
if (act !== conf.actionType.open) {
const [hook, promise] = messageCollector();
nodefs.watchFile(logUri.fsPath, () => {
try {
Expand Down Expand Up @@ -153,15 +149,15 @@ export async function activate(context: vscode.ExtensionContext) {
}
return r;
}
if (type === actionType.run) {
if (act === conf.actionType.run) {
autoexec.push(...action.run.map(cb));
}
if (type === actionType.debug) {
if (act === conf.actionType.debug) {
autoexec.push(...action.debug.map(cb));
}
api.jsdos.updateAutoexec(autoexec);
const webview = await api.jsdos.runInWebview();
if (type !== actionType.open) {
if (act !== conf.actionType.open) {
const [hook, promise] = messageCollector();
webview.onDidReceiveMessage(e => {
switch (e.command) {
Expand All @@ -184,7 +180,7 @@ export async function activate(context: vscode.ExtensionContext) {
(terminal as vscode.Terminal).sendText(val.replace('C:', assemblyToolsFolder.fsPath));
}
);
if (type === actionType.open) {
if (act === conf.actionType.open) {
terminal.sendText(`cd "${vscode.Uri.joinPath(_uri, '..').fsPath}"`);
}
else {
Expand All @@ -199,10 +195,10 @@ export async function activate(context: vscode.ExtensionContext) {
return r + `>> ${logFilename} \n type ${logFilename}`;
}
}
if (type === actionType.run) {
if (act === conf.actionType.run) {
action.run.map(cb).forEach(val => (terminal as vscode.Terminal).sendText(val));
}
if (type === actionType.debug) {
if (act === conf.actionType.debug) {
action.debug.map(cb).forEach(val => (terminal as vscode.Terminal).sendText(val));
}
const logUri = vscode.Uri.joinPath(folder, logFilename);
Expand Down Expand Up @@ -234,8 +230,8 @@ export async function activate(context: vscode.ExtensionContext) {
}

context.subscriptions.push(
vscode.commands.registerCommand('masm-tasm.openEmulator', (uri: vscode.Uri) => singleFileMode(actionType.open, uri)),
vscode.commands.registerCommand('masm-tasm.runASM', (uri: vscode.Uri) => singleFileMode(actionType.run, uri)),
vscode.commands.registerCommand('masm-tasm.debugASM', (uri: vscode.Uri) => singleFileMode(actionType.debug, uri))
vscode.commands.registerCommand('masm-tasm.openEmulator', (uri: vscode.Uri) => singleFileMode(conf.actionType.open, uri)),
vscode.commands.registerCommand('masm-tasm.runASM', (uri: vscode.Uri) => singleFileMode(conf.actionType.run, uri)),
vscode.commands.registerCommand('masm-tasm.debugASM', (uri: vscode.Uri) => singleFileMode(conf.actionType.debug, uri))
);
}
6 changes: 4 additions & 2 deletions src/language/hoverFelix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Uri, workspace } from "vscode";
import { logger } from "../utils/logger";

const fs = workspace.fs;

Expand Down Expand Up @@ -28,10 +29,11 @@ export class FELIX {
static async create(fileuri: Uri): Promise<FELIX> {
const arr = await fs.readFile(fileuri);
try {
const json = JSON.parse(arr.toString());
const data = new TextDecoder().decode(arr);
const json = JSON.parse(data);
return new FELIX(json);
} catch (e) {
console.error(e, arr.toString());
console.error(e);
}
throw new Error();
}
Expand Down
2 changes: 1 addition & 1 deletion src/language/hoverFromMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class HoverFromMarkdown {
}
static async create(uri: Uri): Promise<HoverFromMarkdown> {
const arr = await fs.readFile(uri);
const str = arr.toString().replace(/\r\n/g, '\n');
const str = new TextDecoder().decode(arr).replace(/\r\n/g, '\n');
const target: HoverInfoItem[] = [];
const regex = /```yaml\n([\s\S]+?)\n```([\s\S]+?)(?=```)/g;
let re = regex.exec(str);
Expand Down
3 changes: 0 additions & 3 deletions src/utils/browser-fetch.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/utils/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export enum DosEmulatorType {
jsdos = 'jsdos',
}

export enum actionType {
open,
run,
debug
}

import * as vscode from 'vscode';

class ExtensionConfiguration {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { workspace, ExtensionContext, env, Uri } from "vscode";
import * as conf from './configuration';

const fs = workspace.fs;

let text: { [id: string]: string } | null = null;
Expand All @@ -19,4 +21,15 @@ export function localize(key: string, value: string, ...args: string[]): string
value = value.replace(`{${argidx}}`, args[argidx]);
}
return value;
}

export function actionMessage(act: conf.actionType, file: string, emu: conf.DosEmulatorType, asm: conf.Assembler): string {
switch (act) {
case conf.actionType.open:
return localize("openemu.msg", "open DOS emulator {2} set {1} in folder of: \n\t{0}", file, asm, emu);
case conf.actionType.run:
return localize("run.msg", "run code with {1} in {2}\n\t{0}", file, asm, emu);
case conf.actionType.debug:
return localize("debug.msg", "debug code with {1} in {2}\n\t{0}", file, asm, emu);
}
}
8 changes: 8 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import * as vscode from 'vscode';
import { actionMessage, localize } from './i18n';
import * as conf from './configuration';

class Logger {
outputChannel: vscode.OutputChannel = vscode.window.createOutputChannel('masm-tasm');
log = console.log;
warn = console.warn;
error = console.error;
localize = localize;
actionLog(act: conf.actionType, uri: vscode.Uri) {
const message = actionMessage(act, uri.fsPath, conf.extConf.emulator, conf.extConf.asmType);
this.channel('\n' + message + '\n').show();
}

channel(value: string) {
this.outputChannel.append(value);
return this.outputChannel;
Expand Down
27 changes: 11 additions & 16 deletions src/web/main.ts → src/web/ASM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as statusBar from '../ASM/statusBar';
import * as Diag from '../diagnose/main';
import * as conf from '../utils/configuration';
import { messageCollector } from '../diagnose/messageCollector';
import { logger } from '../utils/logger';

const fs = vscode.workspace.fs;

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

const assemblyToolsFolder = vscode.Uri.joinPath(context.globalStorageUri, conf.extConf.asmType);
const seperateSpaceFolder = vscode.Uri.joinPath(context.globalStorageUri, "workspace");

async function singleFileMode(type: actionType, _uri: vscode.Uri): Promise<AsmResult> {

await fs.createDirectory(assemblyToolsFolder);

async function singleFileMode(act: actionType, _uri: vscode.Uri): Promise<AsmResult> {
logger.actionLog(act, _uri);
const actions: ACTIONS | undefined = vscode.workspace.getConfiguration('masmtasm').get('ASM.actions');
if (actions === undefined) {
throw new Error("configurate `masmtasm.ASM.actions` first");
Expand All @@ -59,13 +55,12 @@ export async function activate(context: vscode.ExtensionContext) {
const bundle = await fs.readFile(bundlePath);

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

const fileInfo = path.parse(_uri.fsPath);

if (conf.extConf.emulator === conf.DosEmulatorType.jsdos) {
await api.jsdos.jszip.loadAsync(bundle);
api.jsdos.jszip.file('code/' + fileInfo.base, doc.getText());
api.jsdos.jszip.file('code/test' + fileInfo.ext, doc.getText());
const autoexec = [
`mount c .`,
`mount d ./code`,
Expand All @@ -74,22 +69,22 @@ export async function activate(context: vscode.ExtensionContext) {
];
function cb(val: string) {
const r = val
.replace("${file}", fileInfo.base)
.replace("${filename}", fileInfo.name);
.replace("${file}", 'test' + fileInfo.ext)
.replace("${filename}", 'test');
if (val.startsWith('>')) {
return r.replace(">", "");
}
return r;
}
if (type === actionType.run) {
if (act === actionType.run) {
autoexec.push(...action.run.map(cb));
}
if (type === actionType.debug) {
if (act === actionType.debug) {
autoexec.push(...action.debug.map(cb));
}
api.jsdos.updateAutoexec(autoexec);
const webview = await api.jsdos.runInWebview();
if (type !== actionType.open) {
if (act !== actionType.open) {
const [hook, promise] = messageCollector();
webview.onDidReceiveMessage(e => {
switch (e.command) {
Expand Down
2 changes: 1 addition & 1 deletion src/web/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode';
import { localize, loadI18n } from '../utils/i18n';

import * as lan from '../language/main';
import * as asm from './main';
import * as asm from './ASM';

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

Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"module": "commonjs",
"target": "es2017",
"outDir": "dist",
"lib": [
"DOM",
"ES2017"
],
"sourceMap": true,
"rootDir": "src",
"strict": true /* enable all strict type-checking options */
Expand Down
5 changes: 3 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ const webExtensionConfig = {
mainFields: ['browser', 'module', 'main'], // look for `browser` entry point in imported node modules
extensions: ['.ts', '.js'], // support ts-files and js-files
alias: {
'node-fetch': './browser-fetch'
// 'node-fetch': './browser-fetch'
// provides alternate implementation for node module and source files
},
fallback: {
// Webpack 5 no longer polyfills Node.js core modules automatically.
// see https://webpack.js.org/configuration/resolve/#resolvefallback
// for the list of Node.js core module polyfills.
assert: require.resolve('assert'),
path: require.resolve('path-browserify')
path: require.resolve('path-browserify'),
"node-fetch": false
},
},
module: {
Expand Down

0 comments on commit f8fdd66

Please sign in to comment.