Skip to content

Commit 5433a9d

Browse files
committed
move code from vscode-dosbox to here
1 parent 389bd89 commit 5433a9d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+5372
-102
lines changed

masm-tasm/src/ASM/vscode-dosbox.ts

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,4 @@
33
* originally from https://github.com/dosasm/vscode-dosbox/blob/main/src/api.ts
44
*/
55

6-
import { CommandInterface, Emulators } from 'emulators';
7-
import * as Jszip from 'jszip';
8-
import * as vscode from 'vscode';
9-
10-
export interface DosboxResult {
11-
stdout: string,
12-
stderr: string,
13-
exitCode: number | null;
14-
}
15-
16-
export interface Dosbox {
17-
/**
18-
* update the main part of the dosbox configuration file
19-
*/
20-
updateConf(section: string, key: string, value: string | number | boolean): void,
21-
/**
22-
* update the autoexec section of the dosbox configuration file
23-
*/
24-
updateAutoexec(context: string[]): void,
25-
/**
26-
* update the conf file from jsdos bundle
27-
*
28-
* @param bundle the bundle data
29-
* @param tempFolder the destination to exact the bundle file
30-
* @param useBundleConf use the bundle's dosbox.conf to update the dosbox's one (default false)
31-
*/
32-
fromBundle(bundle: Uint8Array, tempFolder: vscode.Uri, useBundleConf?: boolean): Promise<void>
33-
/**
34-
* run the emulator
35-
*
36-
* @param params the parameter passed to dosbox via command line
37-
*/
38-
run(params?: string[]): Promise<DosboxResult>
39-
}
40-
41-
export interface Jsdos {
42-
/**
43-
* set the jsdos bundle to use
44-
*
45-
* @deprecated use jszip
46-
* @param bundle the Uint8Array data of the jsdos bundle or its Uri
47-
* @param updateConf use the conf file in the bundle
48-
*/
49-
setBundle(bundle: vscode.Uri | Uint8Array, updateConf?: boolean): void,
50-
/**
51-
* the [jszip object](https://stuk.github.io/jszip/)
52-
*
53-
* change this to change the bundle's data,
54-
* the extension call it to generate bundle data
55-
*/
56-
jszip: Jszip;
57-
updateConf(section: string, key: string, value: string | number | boolean): boolean,
58-
updateAutoexec(context: string[]): void,
59-
/**
60-
* run jsdos in the VSCode's extension Host
61-
*
62-
* @todo make this also work in web extension
63-
* @returns [CommandInterface](https://js-dos.com/v7/build/docs/command-interface)
64-
*/
65-
runInHost(): Promise<CommandInterface>,
66-
/**
67-
* run **jsdos in the webview**. This works in all platform including web
68-
*
69-
* @param bundle the Uint8Array data of the jsdos bundle
70-
* @returns the vscode webview running JSDos
71-
*/
72-
runInWebview(): Promise<vscode.Webview>,
73-
}
74-
75-
export interface API {
76-
/**
77-
* [jsdos](https://js-dos.com/v7/build/) emulator
78-
* is the core of jsdos -- the simpliest API to run DOS games in browser
79-
*
80-
* @see https://github.com/js-dos/emulators
81-
*/
82-
emulators: Emulators;
83-
/**
84-
* run Jsdos in ExtensionHost or Webview
85-
*/
86-
jsdos: Jsdos;
87-
88-
/**
89-
* run DOSBox via child_process
90-
*/
91-
dosbox: Dosbox;
92-
/**
93-
* run DOSBox-x via child_process
94-
*/
95-
dosboxX: Dosbox;
96-
97-
/**
98-
* run msdos player via cmd.exe
99-
*
100-
* @returns a terminal to control
101-
*/
102-
msdosPlayer(msdosArgs?: string[], command?: string): vscode.Terminal;
103-
/**path of the packed msdos player */
104-
msdosPath: string,
105-
/**path of the packed command.com file */
106-
commandPath: string
107-
}
6+
export * from "../dosbox/api";

masm-tasm/src/dosbox/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SOURCE CODE
2+
3+
The code can be complicated for codes are designed for run in following environment
4+
5+
- `global`: nodejs vscode extension host, all codes
6+
- `window`: webview script, the `emulators` folder
7+
- `self`: webworker vscode extension host, jsdos related codes including `emulators`,`jsdos`
8+
9+
The code in [emulators/](emulators/) are copied from <https://github.com/js-dos/emulators> for modification to run in VSCode.
10+
11+
Thanks to [caiiiycuk](https://github.com/caiiiycuk).

masm-tasm/src/dosbox/api.ts

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/**
2+
* API from extension [vscode-dosbox](https://marketplace.visualstudio.com/items?itemName=xsro.vscode-dosbox)
3+
* originally from https://github.com/dosasm/vscode-dosbox/blob/main/src/api.ts
4+
*/
5+
6+
import { CommandInterface, Emulators } from "./emulators/emulators";
7+
import * as Jszip from "jszip";
8+
import * as vscode from "vscode";
9+
10+
export interface CreateBundleOptions {
11+
sample?: string;
12+
boxConf?: string;
13+
mount?: { dir: vscode.Uri; disk: string }[];
14+
}
15+
16+
export interface CI extends CommandInterface {
17+
/**a simple shell with stdout and stdin */
18+
shell: {
19+
onStdout: (consumer: (message: string) => void) => void;
20+
shell: (cmd: string) => void;
21+
};
22+
/**use jsdos shell as VSCode Terminal */
23+
terminal(): vscode.Terminal;
24+
}
25+
26+
export interface DosboxResult {
27+
stdout: string;
28+
stderr: string;
29+
exitCode: number | null;
30+
}
31+
32+
export interface Dosbox {
33+
/**
34+
* update the main part of the dosbox configuration file
35+
*/
36+
updateConf(
37+
section: string,
38+
key: string,
39+
value: string | number | boolean
40+
): void;
41+
/**
42+
* update the autoexec section of the dosbox configuration file
43+
*/
44+
updateAutoexec(context: string[]): void;
45+
/**
46+
* update the conf file from jsdos bundle
47+
*
48+
* @param bundle the bundle data
49+
* @param tempFolder the destination to exact the bundle file
50+
* @param useBundleConf use the bundle's dosbox.conf to update the dosbox's one (default false)
51+
*/
52+
fromBundle(
53+
bundle: Uint8Array,
54+
tempFolder: vscode.Uri,
55+
useBundleConf?: boolean
56+
): Promise<void>;
57+
/**
58+
* run the emulator
59+
*
60+
* @param params the parameter passed to dosbox via command line
61+
*/
62+
run(params?: string[]): Promise<DosboxResult>;
63+
}
64+
65+
export interface Jsdos {
66+
/**
67+
* set the jsdos bundle to use
68+
*
69+
* @deprecated use jszip directly
70+
* @param bundle the Uint8Array data of the jsdos bundle or its Uri
71+
* @param updateConf use the conf file in the bundle
72+
*/
73+
setBundle(bundle: vscode.Uri | Uint8Array, updateConf?: boolean): void;
74+
/**
75+
* the [jszip object](https://stuk.github.io/jszip/)
76+
*
77+
* change this to change the bundle's data,
78+
* the extension call it to generate bundle data
79+
*/
80+
jszip: Jszip;
81+
updateConf(
82+
section: string,
83+
key: string,
84+
value: string | number | boolean
85+
): boolean;
86+
updateAutoexec(context: string[]): void;
87+
/**
88+
* run jsdos in the VSCode's extension Host
89+
*
90+
* @param bundle: the uri of the jsdos bundle
91+
* - if set as undefined, will load from the jszip property
92+
* - if set as null,will force to load an empty jszip bundle
93+
* - if set as a vscode.Uri with schema of file, will load in VSCode with workspace.fs API
94+
* @param useWorker use dosboxWorker or dosboxDirect
95+
* - by default, dosboxDirect for nodejs env and dosboxWorker for browser env
96+
* - dosboxWorker now can't work on the nodejs env
97+
* @returns [CommandInterface](https://js-dos.com/v7/build/docs/command-interface)
98+
*/
99+
runInHost(
100+
bundle?: vscode.Uri | null | undefined,
101+
useWorker?: boolean
102+
): Promise<CI>;
103+
/**
104+
* run **jsdos in the webview**. This works in all platform including web
105+
*
106+
* @param uri the uri of the jsdos bundle
107+
* - if set as undefined, will load from the jszip property
108+
* - if set as null,will force to load an empty jszip bundle
109+
* - if set as a vscode.Uri with schema of http and https, will load inside the webview
110+
* - if set as a vscode.Uri with schema of file, will load in VSCode and post data to the webview
111+
* @returns the vscode webview running JSDos
112+
*/
113+
runInWebview(uri?: undefined | null | vscode.Uri): Promise<vscode.Webview>;
114+
runInWebviewPanel(
115+
bundle?: vscode.Uri | null | undefined
116+
): Promise<vscode.WebviewPanel>;
117+
}
118+
119+
export interface API {
120+
/**create a bundle with recursively mount directories */
121+
createBundle: (optiion: CreateBundleOptions) => Promise<Jszip>;
122+
/**
123+
* [jsdos](https://js-dos.com/v7/build/) emulator
124+
* is the core of jsdos -- the simpliest API to run DOS games in browser
125+
*
126+
* @see https://github.com/js-dos/emulators
127+
*/
128+
emulators: Emulators;
129+
/**
130+
* run Jsdos in ExtensionHost or Webview
131+
*/
132+
jsdos: Jsdos;
133+
134+
/**
135+
* run DOSBox via child_process
136+
*/
137+
dosbox: Dosbox;
138+
/**
139+
* run DOSBox-x via child_process
140+
*/
141+
dosboxX: Dosbox;
142+
143+
/**
144+
* run msdos player via cmd.exe
145+
*
146+
* @returns a terminal to control
147+
*/
148+
msdosPlayer(msdosArgs?: string[], command?: string): vscode.Terminal;
149+
/**path of the packed msdos player */
150+
msdosPath: string;
151+
/**path of the packed command.com file */
152+
commandPath: string;
153+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Manage DOSBox's configuration file
3+
*/
4+
export class Conf {
5+
private _target: string[] = [];
6+
private eol = "\n";
7+
constructor(confStr: string = "") {
8+
if (confStr.includes("\r\n")) {
9+
this.eol = "\r\n";
10+
}
11+
this._target = confStr.split(this.eol);
12+
if (!Array.isArray(this._target)) {
13+
throw new Error("error target");
14+
}
15+
}
16+
17+
/**check whether the config file has the item or not
18+
* @returns line number of the section or key
19+
*/
20+
has(section: string, key?: string): number | undefined {
21+
let sectionIdx = this._target.findIndex((val) => {
22+
return val.trim().toLowerCase() === `[${section.trim().toLowerCase()}]`;
23+
});
24+
if (key === undefined && sectionIdx >= 0) {
25+
return sectionIdx;
26+
}
27+
for (let i = sectionIdx + 1; i < this._target.length; i++) {
28+
const line = this._target[i];
29+
if (typeof line !== "string") {
30+
continue;
31+
}
32+
if (line.trimLeft().startsWith("[")) {
33+
break;
34+
}
35+
const kv = line.replace(/#.*/g, "");
36+
const [_key, _value] = kv.split("=");
37+
if (key && key.trim().toLowerCase() === _key.trim().toLowerCase()) {
38+
return i;
39+
}
40+
}
41+
return undefined;
42+
}
43+
44+
get(section: string, key: string) {
45+
const idx = this.has(section, key);
46+
if (idx !== undefined) {
47+
const [name, value] = this._target[idx]
48+
.replace(/#.*/g, "")
49+
.trim()
50+
.split("=");
51+
if (value) {
52+
return value.trim();
53+
}
54+
}
55+
}
56+
57+
update(section: string, key: string, value: boolean | number | string) {
58+
let idx = this.has(section, key);
59+
if (idx !== undefined) {
60+
this._target[idx] = `${key} = ${value.toString()}`;
61+
return;
62+
}
63+
idx = this.has(section);
64+
if (idx !== undefined) {
65+
this._target.splice(idx + 1, 0, `${key}=${value}`);
66+
return;
67+
} else {
68+
this._target.push(`[${section}]`);
69+
this._target.push(`${key}=${value}`);
70+
return;
71+
}
72+
}
73+
74+
updateAutoexec(context: string[]) {
75+
const section = "[autoexec]";
76+
const idx = this._target.findIndex((val) => val === section);
77+
if (idx >= 0) {
78+
for (let i = idx + 1; i < this._target.length; i++) {
79+
if (!this._target[i].trim().startsWith("#")) {
80+
this._target[i] = "#" + this._target[i];
81+
}
82+
if (this._target[i].trim().startsWith("[")) {
83+
break;
84+
}
85+
}
86+
this._target.splice(idx + 1, 0, ...context);
87+
} else {
88+
this._target.push("[autoexec]", ...context);
89+
}
90+
}
91+
toString() {
92+
return this._target.join(this.eol);
93+
}
94+
}

0 commit comments

Comments
 (0)