Skip to content

Commit e7dcb66

Browse files
committed
Can choose debuggee encoding in launch.json
1 parent a144c1c commit e7dcb66

4 files changed

Lines changed: 56 additions & 13 deletions

File tree

DebugAdapter/DebugSession.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.IO;
1818
using System.Net;
1919
using System.Net.Sockets;
20+
using System.Text;
2021

2122
namespace VSCodeDebug
2223
{
@@ -286,15 +287,36 @@ void AcceptDebuggee(string command, int seq, dynamic args, TcpListener listener)
286287
var workingDirectory = ReadWorkingDirectory(command, seq, args);
287288
if (workingDirectory == null) { return; }
288289

290+
var encodingName = (string)args.encoding;
291+
Encoding encoding;
292+
if (encodingName != null)
293+
{
294+
int codepage;
295+
if (int.TryParse(encodingName, out codepage))
296+
{
297+
encoding = Encoding.GetEncoding(codepage);
298+
}
299+
else
300+
{
301+
encoding = Encoding.GetEncoding(encodingName);
302+
}
303+
}
304+
else
305+
{
306+
encoding = Encoding.UTF8;
307+
}
308+
289309
Program.WaitingUI.SetLabelText(
290310
"Waiting for debugee at TCP " +
291311
listener.LocalEndpoint.ToString() + "...");
292312

293313
var clientSocket = listener.AcceptSocket(); // blocked here
294314
listener.Stop();
295315
Program.WaitingUI.Hide();
296-
var networkStream = new NetworkStream(clientSocket);
297-
var ncom = new DebuggeeProtocol(this, networkStream);
316+
var ncom = new DebuggeeProtocol(
317+
this,
318+
new NetworkStream(clientSocket),
319+
encoding);
298320
this.toDebugee = ncom;
299321

300322
var welcome = new

DebugAdapter/Debugee/DebuggeeProtocol.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ class DebuggeeProtocol : IDebugeeSender
1010
IDebugeeListener debugeeListener;
1111
NetworkStream networkStream;
1212
ByteBuffer recvBuffer = new ByteBuffer();
13+
Encoding encoding;
1314

14-
public DebuggeeProtocol(IDebugeeListener debugeeListener, NetworkStream networkStream)
15+
public DebuggeeProtocol(
16+
IDebugeeListener debugeeListener,
17+
NetworkStream networkStream,
18+
Encoding encoding)
1519
{
1620
this.debugeeListener = debugeeListener;
1721
this.networkStream = networkStream;
22+
this.encoding = encoding;
1823
}
1924

2025
public void StartThread()
@@ -52,7 +57,7 @@ void SocketStreamLoop()
5257

5358
bool ProcessData()
5459
{
55-
string s = recvBuffer.GetString(System.Text.Encoding.UTF8);
60+
string s = recvBuffer.GetString(encoding);
5661
int headerEnd = s.IndexOf('\n');
5762
if (headerEnd < 0) { return false; }
5863

@@ -67,7 +72,7 @@ bool ProcessData()
6772
recvBuffer.RemoveFirst(headerEnd + 1);
6873
byte[] bodyBytes = recvBuffer.RemoveFirst(bodySize);
6974

70-
string body = Encoding.UTF8.GetString(bodyBytes);
75+
string body = encoding.GetString(bodyBytes);
7176
//MessageBox.OK(body);
7277

7378
lock (debugeeListener)
@@ -79,9 +84,9 @@ bool ProcessData()
7984

8085
void IDebugeeSender.Send(string reqText)
8186
{
82-
byte[] bodyBytes = Encoding.UTF8.GetBytes(reqText);
87+
byte[] bodyBytes = encoding.GetBytes(reqText);
8388
string header = '#' + bodyBytes.Length.ToString() + "\n";
84-
byte[] headerBytes = Encoding.UTF8.GetBytes(header);
89+
byte[] headerBytes = encoding.GetBytes(header);
8590
try
8691
{
8792
networkStream.Write(headerBytes, 0, headerBytes.Length);

Extension/package.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"executable": "${workspaceRoot}/lua.exe",
4444
"arguments": "main.lua",
4545
"listenPublicly": false,
46-
"listenPort": 56789
46+
"listenPort": 56789,
47+
"encoding": "UTF-8"
4748
},
4849
{
4950
"name": "launch-gideros",
@@ -53,15 +54,17 @@
5354
"giderosPath": "C:/Program Files (x86)/Gideros",
5455
"gprojPath": "${workspaceRoot}/GPROJ.gproj",
5556
"listenPublicly": false,
56-
"listenPort": 56789
57+
"listenPort": 56789,
58+
"encoding": "UTF-8"
5759
},
5860
{
5961
"name": "wait",
6062
"type": "lua",
6163
"request": "attach",
6264
"workingDirectory": "${workspaceRoot}",
6365
"listenPublicly": false,
64-
"listenPort": 56789
66+
"listenPort": 56789,
67+
"encoding": "UTF-8"
6568
}
6669
],
6770
"configurationAttributes": {
@@ -109,6 +112,11 @@
109112
"type": "string",
110113
"description": "Path of .gproj file",
111114
"default": "${workspaceRoot}/GPROJ.gproj"
115+
},
116+
"encoding": {
117+
"type": "string",
118+
"description": "Encoding of the debuggee. (Example: 'UTF-8', '949')",
119+
"default": "UTF-8"
112120
}
113121
}
114122
},
@@ -136,6 +144,11 @@
136144
"type": "integer",
137145
"description": "Local TCP port to communicate between debug adapter and debugee.",
138146
"default": 56789
147+
},
148+
"encoding": {
149+
"type": "string",
150+
"description": "Encoding of the debuggee. (Example: 'UTF-8', '949')",
151+
"default": "UTF-8"
139152
}
140153
}
141154
}

debugee/.vscode/launch.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"executable": "${workspaceRoot}/lua.exe",
1010
"arguments": "main.lua",
1111
"listenPublicly": false,
12-
"listenPort": 56789
12+
"listenPort": 56789,
13+
"encoding": "UTF-8"
1314
},
1415
{
1516
"name": "launch-gideros",
@@ -19,15 +20,17 @@
1920
"giderosPath": "C:/Program Files (x86)/Gideros",
2021
"gprojPath": "${workspaceRoot}/gideros.gproj",
2122
"listenPublicly": false,
22-
"listenPort": 56789
23+
"listenPort": 56789,
24+
"encoding": "UTF-8"
2325
},
2426
{
2527
"name": "wait",
2628
"type": "lua",
2729
"request": "attach",
2830
"workingDirectory": "${workspaceRoot}",
2931
"listenPublicly": false,
30-
"listenPort": 56789
32+
"listenPort": 56789,
33+
"encoding": "UTF-8"
3134
}
3235
]
3336
}

0 commit comments

Comments
 (0)