Skip to content

Commit

Permalink
working cppdap implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitalita committed Feb 12, 2023
1 parent d390277 commit 7c16d64
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<TargetFramework>net472</TargetFramework>
<PlatformTarget>x86</PlatformTarget>
<Configurations>Debug</Configurations>
<OutputPath>bin\$(Configuration)\$(TargetFramework)\$(MSBuildProjectName)</OutputPath>
<OutputPath>C:\Users\Nikita\.vscode\extensions\joelday.papyrus-lang-vscode-3.1.1\debug-bin\$(Configuration)\$(TargetFramework)\$(MSBuildProjectName)</OutputPath>
<DefaultItemExcludes>$(DefaultItemExcludes);obj\**</DefaultItemExcludes>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
Expand All @@ -23,14 +23,17 @@
<StartupObject />
<DefineConstants>TRACE;FALLOUT4</DefineConstants>
<RootNamespace>DarkId.Papyrus.DebugAdapterProxy</RootNamespace>
<BaseOutputPath>C:\Users\Nikita\.vscode\extensions\joelday.papyrus-lang-vscode-3.1.1\debug-bin\</BaseOutputPath>
</PropertyGroup>

<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0-dev-00032" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="WebSocketSharp-netstandard" Version="1.0.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0-dev-00032" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="WebSocketSharp-netstandard" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DarkId.Papyrus.Common\DarkId.Papyrus.Common.csproj" />
<ProjectReference
Include="..\DarkId.Papyrus.LanguageService\DarkId.Papyrus.LanguageService.Skyrim.csproj" />
<ProjectReference Include="..\DarkId.Papyrus.LanguageService\DarkId.Papyrus.LanguageService.Skyrim.csproj" />
</ItemGroup>
</Project>
32 changes: 23 additions & 9 deletions src/DarkId.Papyrus.DebugAdapterProxy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
using DarkId.Papyrus.LanguageService.Projects;
using DarkId.Papyrus.LanguageService.Configuration.CreationKit;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;
using Microsoft.Extensions.Logging.Console;
using Newtonsoft.Json.Linq;
using System.Runtime.InteropServices;

using logger = Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Threading;

namespace DarkId.Papyrus.DebugAdapterProxy
{
public class Options
Expand Down Expand Up @@ -61,21 +65,31 @@ class Program

static int Main(string[] args)
{
loggerFactory = new LoggerFactory()
.AddDebug(Microsoft.Extensions.Logging.LogLevel.Trace)
.AddFile(
Path.Combine(
// Console.WriteLine("Waiting for debugger to attach");
// while (!Debugger.IsAttached)
//{
// Thread.Sleep(100);
// }
// Console.WriteLine("Debugger attached");

var logpath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
#if FALLOUT4
"My Games\\Fallout4\\F4SE\\DarkId.Papyrus.DebugAdapterProxy.log"
#else
"My Games\\Skyrim Special Edition\\SKSE\\DarkId.Papyrus.DebugAdapterProxy.log"
#endif
),
Microsoft.Extensions.Logging.LogLevel.Trace);

);

loggerFactory = LoggerFactory.Create(builder => {
builder.AddConsole(i => {
i.LogToStandardErrorThreshold = Microsoft.Extensions.Logging.LogLevel.Trace;
});
builder.AddFile(logpath, Microsoft.Extensions.Logging.LogLevel.Trace);
builder.AddDebug();
});
logger = loggerFactory.CreateLogger<Program>();

logger.LogInformation("Debugger adapter started");
int exitCode = 0;

Parser.Default.ParseArguments<Options>(args)
Expand Down
2 changes: 1 addition & 1 deletion src/DarkId.Papyrus.DebugServer/BreakpointManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace DarkId::Papyrus::DebugServer
}
const auto sourceReference = m_pexCache->GetScriptReference(scriptName.c_str());
source.sourceReference = sourceReference;

#if _DEBUG_DUMP_PEX
std::string dir = logger::log_directory().value_or("").string();
if (dir.empty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<ClCompile Include="PapyrusDebugger.cpp" />
<ClCompile Include="Pex.cpp" />
<ClCompile Include="PexCache.cpp" />
<ClCompile Include="Protocol\struct_extensions.cpp" />
<ClCompile Include="Protocol\websocket_reader_writer.cpp" />
<ClCompile Include="Protocol\websocket_server.cpp" />
<ClCompile Include="RuntimeEvents.cpp" />
Expand Down Expand Up @@ -242,6 +243,7 @@
<ClInclude Include="Pex.h" />
<ClInclude Include="PexCache.h" />
<ClInclude Include="protocol\debugger.h" />
<ClInclude Include="Protocol\struct_extensions.h" />
<ClInclude Include="Protocol\websocket_impl.h" />
<ClInclude Include="Protocol\websocket_reader_writer.h" />
<ClInclude Include="Protocol\websocket_server.h" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<ClCompile Include="Protocol\websocket_reader_writer.cpp">
<Filter>Protocol</Filter>
</ClCompile>
<ClCompile Include="Protocol\struct_extensions.cpp">
<Filter>Protocol</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="version.rc" />
Expand Down Expand Up @@ -122,5 +125,8 @@
<ClInclude Include="Protocol\websocket_impl.h">
<Filter>Protocol</Filter>
</ClInclude>
<ClInclude Include="Protocol\struct_extensions.h">
<Filter>Protocol</Filter>
</ClInclude>
</ItemGroup>
</Project>
77 changes: 32 additions & 45 deletions src/DarkId.Papyrus.DebugServer/DebugServer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#include "DebugServer.h"
#include <thread>
#include <functional>

namespace DarkId::Papyrus::DebugServer
{
DebugServer::DebugServer(): m_session(nullptr){ }
DebugServer::DebugServer() {
restart_thread = std::thread(std::bind(&DebugServer::runRestartThread, this));
}

void DebugServer::runRestartThread() {
while (true){
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&] { return terminate; });
terminate = false;
debugger = nullptr;
}
}

bool DebugServer::Listen()
{
Expand All @@ -12,59 +26,32 @@ namespace DarkId::Papyrus::DebugServer
#endif
auto onClientConnected =
[&](const std::shared_ptr<dap::ReaderWriter>& connection) {
m_session = dap::Session::create();
m_session->bind(connection);
std::shared_ptr<dap::Session> thing = std::move(m_session);
debugger = std::unique_ptr<PapyrusDebugger>( new PapyrusDebugger(std::move(thing)) );
// The Initialize request is the first message sent from the client and
// the response reports debugger capabilities.
// https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Initialize
m_session->registerHandler([](const dap::InitializeRequest& request) {
dap::InitializeResponse response;
response.supportsConfigurationDoneRequest = true;
response.supportsLoadedSourcesRequest = true;
return response;
});

m_session->registerSentHandler(
[&](const dap::ResponseOrError<dap::InitializeResponse>&) {
m_session->send(dap::InitializedEvent());
});

// Signal used to terminate the server session when a DisconnectRequest
// is made by the client.
bool terminate = false;
std::condition_variable cv;
std::mutex mutex; // guards 'terminate'

// The Disconnect request is made by the client before it disconnects
// from the server.
// https://microsoft.github.io/debug-adapter-protocol/specification#Requests_Disconnect
m_session->registerHandler([&](const dap::DisconnectRequest&) {
// Client wants to disconnect. Set terminate to true, and signal the
// condition variable to unblock the server thread.
std::unique_lock<std::mutex> lock(mutex);
terminate = true;
cv.notify_one();
return dap::DisconnectResponse{};
std::shared_ptr<dap::Session> sess;
sess = dap::Session::create();
sess->bind(connection);
debugger = std::unique_ptr<PapyrusDebugger>( new PapyrusDebugger(sess) );
sess->registerSentHandler(
[&](const dap::ResponseOrError<dap::DisconnectResponse>&) {
terminate = true;
cv.notify_all();
});

// Wait for the client to disconnect (or reach a 5 second timeout)
// before releasing the session and disconnecting the socket to the
// client.
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [&] { return terminate; });
printf("Session terminated\n");
m_session = nullptr;
};
auto onError = [&](const char* msg) { printf("Server error: %s\n", msg); };

auto onError = [&](const char* msg) {
logger::error("Server error: %s\n", msg);
};

printf("Session terminated\n");
auto thread = std::thread();
m_server.start(port, onClientConnected, onError);
return true;
}

DebugServer::~DebugServer()
{
m_server.stop();
if (restart_thread.joinable()) {
restart_thread.join();
}
}
}
6 changes: 5 additions & 1 deletion src/DarkId.Papyrus.DebugServer/DebugServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ namespace DarkId::Papyrus::DebugServer
{
public:
DebugServer();
void runRestartThread();
~DebugServer();

bool Listen();
private:
std::unique_ptr<dap::Session> m_session;
std::unique_ptr<PapyrusDebugger> debugger;
dap::net::WebsocketServer m_server;
std::condition_variable cv;
std::mutex mutex; // guards 'terminate'
bool terminate;
std::thread restart_thread;
};
}
Loading

0 comments on commit 7c16d64

Please sign in to comment.