@@ -6,6 +6,7 @@ import { API } from './vscode-dosbox';
66import * as statusBar from './statusBar' ;
77import * as Diag from '../diagnose/main' ;
88import * as conf from '../utils/configuration' ;
9+ import { messageCollector } from '../diagnose/messageCollector' ;
910
1011const fs = vscode . workspace . fs ;
1112
@@ -24,6 +25,12 @@ type ACTIONS = {
2425 }
2526} ;
2627
28+ export interface AsmResult {
29+ message ?: string ,
30+ error ?: number ,
31+ warn ?: number
32+ }
33+
2734export async function activate ( context : vscode . ExtensionContext ) {
2835 statusBar . activate ( context ) ;
2936 const diag = Diag . activate ( context ) ;
@@ -34,7 +41,8 @@ export async function activate(context: vscode.ExtensionContext) {
3441 const assemblyToolsFolder = vscode . Uri . joinPath ( context . globalStorageUri , conf . extConf . asmType ) ;
3542 const seperateSpaceFolder = vscode . Uri . joinPath ( context . globalStorageUri , "workspace" ) ;
3643
37- async function singleFileMode ( type : actionType , _uri : vscode . Uri ) {
44+ async function singleFileMode ( type : actionType , _uri : vscode . Uri ) : Promise < AsmResult > {
45+
3846 if ( nodefs . existsSync ( seperateSpaceFolder . fsPath ) ) {
3947 await fs . delete ( seperateSpaceFolder , { recursive : true , useTrash : false } ) ;
4048 }
@@ -59,69 +67,116 @@ export async function activate(context: vscode.ExtensionContext) {
5967 const bundle = await fs . readFile ( bundlePath ) ;
6068
6169 let result = "<should-not-return>" ;
62-
63- switch ( conf . extConf . emulator ) {
64- case conf . DosEmulatorType . dosbox :
65- case conf . DosEmulatorType . dosboxX :
66- const uri = vscode . Uri . joinPath ( seperateSpaceFolder , ( "test" + path . extname ( _uri . fsPath ) ) . toUpperCase ( ) ) ;
67- await fs . copy ( _uri , uri ) ;
68- const fileInfo = path . parse ( uri . fsPath ) ;
69- const folder = vscode . Uri . joinPath ( uri , '..' ) ;
70-
71- const autoexec = [
72- `mount c "${ assemblyToolsFolder . fsPath } ""` ,
73- `mount d "${ folder . fsPath } ""` ,
74- 'd:' ,
75- ...action . before
76- ] ;
77-
78- const logFilename = 'box.log' . toUpperCase ( ) ;
79- const logUri = vscode . Uri . joinPath ( assemblyToolsFolder , logFilename ) ;
80- if ( nodefs . existsSync ( logUri . fsPath ) ) {
81- await fs . delete ( logUri ) ;
70+ const uri = vscode . Uri . joinPath ( seperateSpaceFolder , ( "test" + path . extname ( _uri . fsPath ) ) . toUpperCase ( ) ) ;
71+ await fs . copy ( _uri , uri ) ;
72+ const fileInfo = path . parse ( uri . fsPath ) ;
73+ const folder = vscode . Uri . joinPath ( uri , '..' ) ;
74+
75+ if ( conf . extConf . emulator === conf . DosEmulatorType . dosbox || conf . extConf . emulator === conf . DosEmulatorType . dosboxX ) {
76+ const autoexec = [
77+ `mount c "${ assemblyToolsFolder . fsPath } ""` ,
78+ `mount d "${ folder . fsPath } ""` ,
79+ 'd:' ,
80+ ...action . before
81+ ] ;
82+ const timeStamp = new Date ( ) . getTime ( ) . toString ( ) ;
83+ const logFilename = 't' + timeStamp . substr ( timeStamp . length - 6 , 8 ) + '.log' . toUpperCase ( ) ;
84+ const logUri = vscode . Uri . joinPath ( assemblyToolsFolder , logFilename ) ;
85+ if ( nodefs . existsSync ( logUri . fsPath ) ) {
86+ await fs . delete ( logUri ) ;
87+ }
88+ function cb ( val : string ) {
89+ const r = val
90+ . replace ( "${file}" , fileInfo . base )
91+ . replace ( "${filename}" , fileInfo . name ) ;
92+ if ( val . startsWith ( '>' ) ) {
93+ return r . replace ( ">" , "" ) ;
8294 }
83- function cb ( val : string ) {
84- const r = val
85- . replace ( "${file}" , fileInfo . base )
86- . replace ( "${filename}" , fileInfo . name ) ;
87- if ( val . startsWith ( '>' ) ) {
88- return r . replace ( ">" , "" ) ;
95+ return r + " >>C:\\" + logFilename ;
96+ }
97+ if ( type === actionType . run ) {
98+ autoexec . push ( ...action . run . map ( cb ) ) ;
99+ }
100+ if ( type === actionType . debug ) {
101+ autoexec . push ( ...action . debug . map ( cb ) ) ;
102+ }
103+
104+ autoexec . push ( "exit" ) ;
105+
106+ const box = conf . extConf . emulator === conf . DosEmulatorType . dosboxX ? api . dosboxX : api . dosbox ;
107+ await box . fromBundle ( bundle , assemblyToolsFolder ) ;
108+ box . updateAutoexec ( autoexec ) ;
109+
110+ if ( type !== actionType . open ) {
111+ const [ hook , promise ] = messageCollector ( ) ;
112+ nodefs . watchFile ( logUri . fsPath , ( ) => {
113+ try {
114+ if ( nodefs . existsSync ( logUri . fsPath ) ) {
115+ const _result = nodefs . readFileSync ( logUri . fsPath , { encoding : 'utf-8' } ) ;
116+ hook ( _result ) ;
117+ }
89118 }
90- return r + " >>C:\\" + logFilename ;
91- }
92- if ( type === actionType . run ) {
93- autoexec . push ( ...action . run . map ( cb ) ) ;
94- }
95- if ( type === actionType . debug ) {
96- autoexec . push ( ...action . debug . map ( cb ) ) ;
97- }
119+ catch ( e ) {
120+ console . error ( e ) ;
121+ }
122+ } ) ;
123+ promise . then ( val => result = val ) ;
124+ }
125+ await box . run ( ) ;
126+ if ( result === '<should-not-return>' ) {
127+ result = ( await fs . readFile ( logUri ) ) . toString ( ) ;
128+ }
129+ }
98130
99- autoexec . push ( "exit" ) ;
131+ if ( conf . extConf . emulator === conf . DosEmulatorType . jsdos ) {
132+ await api . jsdos . jszip . loadAsync ( bundle ) ;
133+ api . jsdos . jszip . file ( 'code/' + fileInfo . base , doc . getText ( ) ) ;
134+ const autoexec = [
135+ `mount c .` ,
136+ `mount d ./code` ,
137+ 'd:' ,
138+ ...action . before
139+ ] ;
140+ function cb ( val : string ) {
141+ const r = val
142+ . replace ( "${file}" , fileInfo . base )
143+ . replace ( "${filename}" , fileInfo . name ) ;
144+ if ( val . startsWith ( '>' ) ) {
145+ return r . replace ( ">" , "" ) ;
146+ }
147+ return r ;
148+ }
149+ if ( type === actionType . run ) {
150+ autoexec . push ( ...action . run . map ( cb ) ) ;
151+ }
152+ if ( type === actionType . debug ) {
153+ autoexec . push ( ...action . debug . map ( cb ) ) ;
154+ }
155+ api . jsdos . updateAutoexec ( autoexec ) ;
156+ const webview = await api . jsdos . runInWebview ( ) ;
157+ if ( type !== actionType . open ) {
158+ const [ hook , promise ] = messageCollector ( ) ;
159+ webview . onDidReceiveMessage ( e => {
160+ switch ( e . command ) {
161+ case 'stdout' :
162+ hook ( e . value ) ;
163+ break ;
164+ }
165+ } ) ;
166+ result = await promise ;
167+ }
168+ }
100169
101- const box = conf . extConf . emulator === conf . DosEmulatorType . dosboxX ? api . dosboxX : api . dosbox ;
102- await box . fromBundle ( bundle , assemblyToolsFolder ) ;
103- box . updateAutoexec ( autoexec ) ;
104- const _run = box . run ( ) ;
105- const _wait = new Promise < void > (
106- resolve => setTimeout ( resolve , 10000 )
107- ) ;
108- await Promise . race ( [ _run , _wait ] ) ;
109- result = ( await fs . readFile ( logUri ) ) . toString ( ) ;
110- break ;
111- case conf . DosEmulatorType . jsdos :
112- api . jsdos . setBundle ( bundle ) ;
113- api . jsdos . runInWebview ( ) ;
114- break ;
115- case conf . DosEmulatorType . msdos :
116- api . msdosPlayer ( ) ;
117- break ;
170+ if ( conf . extConf . emulator === conf . DosEmulatorType . msdos ) {
171+ api . msdosPlayer ( ) ;
118172 }
119173
120174 const diagnose = diag . process ( result , doc , conf . extConf . asmType ) ;
121175
122176 return {
123177 message : result ,
124- diagnose
178+ error : diagnose ?. error ,
179+ warn : diagnose ?. warn
125180 } ;
126181 }
127182
0 commit comments