Skip to content

Commit

Permalink
HUGEHGUEGHUHGUEHGUEHUGE
Browse files Browse the repository at this point in the history
  • Loading branch information
WtzLAS committed Dec 22, 2023
1 parent 5d5993e commit 896de24
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 28 deletions.
2 changes: 2 additions & 0 deletions NorthstarDLL/NorthstarDLL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ IF EXIST "$(SolutionDir)..\NorthstarCN_Binaries\Northstar.dll" del "$(SolutionDi
<ClInclude Include="dedicated\dedicatedlogtoclient.h" />
<ClInclude Include="dllmain.h" />
<ClInclude Include="engine\hoststate.h" />
<ClInclude Include="engine\netgraph.h" />
<ClInclude Include="engine\r2engine.h" />
<ClInclude Include="exploit_fixes\ns_limits.h" />
<ClInclude Include="hooks\hooks.h" />
Expand Down Expand Up @@ -296,6 +297,7 @@ IF EXIST "$(SolutionDir)..\NorthstarCN_Binaries\Northstar.dll" del "$(SolutionDi
</ClCompile>
<ClCompile Include="engine\host.cpp" />
<ClCompile Include="engine\hoststate.cpp" />
<ClCompile Include="engine\netgraph.cpp" />
<ClCompile Include="engine\r2engine.cpp" />
<ClCompile Include="engine\runframe.cpp" />
<ClCompile Include="logging\crashhandler.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions NorthstarDLL/NorthstarDLL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@
<ClInclude Include="shared\rs.h">
<Filter>Header Files\shared</Filter>
</ClInclude>
<ClInclude Include="engine\netgraph.h">
<Filter>Header Files\engine</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="client\audio.cpp">
Expand Down Expand Up @@ -671,6 +674,9 @@
<ClCompile Include="markdown.cpp">
<Filter>Source Files\client</Filter>
</ClCompile>
<ClCompile Include="engine\netgraph.cpp">
<Filter>Source Files\engine</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<MASM Include="audio_asm.asm">
Expand Down
12 changes: 12 additions & 0 deletions NorthstarDLL/engine/netgraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "pch.h"
#include "netgraph.h"

AUTOHOOK_INIT()

float* g_frameTime;

ON_DLL_LOAD("engine.dll", SERVERFPS, (CModule module))
{
g_frameTime = module.Offset(0x13158ba0).As<float*>();
AUTOHOOK_DISPATCH();
}
3 changes: 3 additions & 0 deletions NorthstarDLL/engine/netgraph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

extern float* g_frameTime;
135 changes: 117 additions & 18 deletions NorthstarDLL/shared/kcpintegration.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "shared/kcpintegration.h"
#include "core/hooks.h"
#include <dedicated/dedicated.h>
#include "kcpintegration.h"

ConVar* Cvar_kcp_timer_resolution;
ConVar* Cvar_kcp_select_timeout;
Expand Down Expand Up @@ -353,7 +354,7 @@ void selectThreadPayload(std::stop_token stoken)
NS::log::NEW_NET.get()->warn("[UdpSource] Routed {} to uninitalized NetSink*", ctx);
continue;
}
auto inputResult = route->first->input(NetBuffer(buf), ctx);
auto inputResult = route->first->input(NetBuffer(buf), ctx, UdpSource::instance().get());
if (inputResult != 0)
{
NS::log::NEW_NET.get()->error("[UdpSource] NetSink*->input {} error: {}", ctx, inputResult);
Expand All @@ -371,7 +372,7 @@ void selectThreadPayload(std::stop_token stoken)
NS::log::NEW_NET.get()->warn("[UdpSource] Routed {} to uninitalized NetSink*", ctx);
continue;
}
auto inputResult = newRoute.first->input(NetBuffer(buf), ctx);
auto inputResult = newRoute.first->input(NetBuffer(buf), ctx, UdpSource::instance().get());
if (inputResult != 0)
{
NS::log::NEW_NET.get()->error("[UdpSource] NetSink*->input {} error: {}", ctx, inputResult);
Expand All @@ -390,7 +391,7 @@ UdpSource::~UdpSource()
selectThread.join();
}

int UdpSource::sendto(const NetBuffer& buf, const NetContext& ctx)
int UdpSource::sendto(NetBuffer&& buf, const NetContext& ctx, const NetSink* top)
{
if (ctx.socket != socket)
{
Expand Down Expand Up @@ -422,9 +423,9 @@ std::shared_ptr<UdpSource> UdpSource::instance()

GameSink::GameSink() {}

int GameSink::input(const NetBuffer& buf, const NetContext& ctx)
int GameSink::input(NetBuffer&& buf, const NetContext& ctx, const NetSource* bottom)
{
pendingData.push(std::make_pair(buf, ctx));
pendingData.push(std::make_pair(std::move(buf), ctx));
return 0;
}

Expand Down Expand Up @@ -504,7 +505,7 @@ int GameSink::sendto(
NS::log::NEW_NET.get()->warn("[GameSink] Routed {} to uninitalized NetSource*", ctx);
return NET_HOOK_NOT_ALTERED;
}
auto sendtoResult = route->second->sendto(NetBuffer(buf, len), ctx);
auto sendtoResult = route->second->sendto(NetBuffer(buf, len), ctx, GameSink::instance().get());
if (sendtoResult < 0)
{
NS::log::NEW_NET.get()->error("[GameSink] NetSource*->sendto {} error: {}", ctx, sendtoResult);
Expand All @@ -524,7 +525,7 @@ int GameSink::sendto(
NS::log::NEW_NET.get()->warn("[GameSink] Routed {} to uninitalized NetSource*", ctx);
return NET_HOOK_NOT_ALTERED;
}
auto sendtoResult = newRoute.second->sendto(NetBuffer(buf, len), ctx);
auto sendtoResult = newRoute.second->sendto(NetBuffer(buf, len), ctx, GameSink::instance().get());
if (sendtoResult != 0)
{
NS::log::NEW_NET.get()->error("[GameSink] NetSource*->sendto {} error: {}", ctx, sendtoResult);
Expand Down Expand Up @@ -560,12 +561,12 @@ FecLayer::~FecLayer()
reed_solomon_release(encoderCodec);
}

int FecLayer::sendto(const NetBuffer& buf, const NetContext& ctx)
int FecLayer::sendto(NetBuffer&& buf, const NetContext& ctx, const NetSink* top)
{
auto encoded = encode(buf);
for (const auto& nBuf : encoded)
for (auto& nBuf : encoded)
{
auto sendtoResult = bottom.lock()->sendto(nBuf, ctx);
auto sendtoResult = bottom.lock()->sendto(std::move(nBuf), ctx, this);
if (sendtoResult == SOCKET_ERROR)
{
NS::log::NEW_NET.get()->error("[FEC] bottom->sendto {} error: {}", ctx, sendtoResult);
Expand All @@ -574,7 +575,7 @@ int FecLayer::sendto(const NetBuffer& buf, const NetContext& ctx)
return 0;
}

int FecLayer::input(const NetBuffer& buf, const NetContext& ctx)
int FecLayer::input(NetBuffer&& buf, const NetContext& ctx, const NetSource* bottom)
{
if (buf.size() < FEC_MIN_SIZE)
{
Expand All @@ -590,7 +591,7 @@ int FecLayer::input(const NetBuffer& buf, const NetContext& ctx)
auto reconstructed = reconstruct(buf, ctx);
if (flag == FEC_TYPE_DATA)
{
NetBuffer nBuf = buf;
NetBuffer nBuf = std::move(buf);
nBuf.getU32H(); // drop seqid
nBuf.getU16H(); // drop flag
auto fecSize = nBuf.getU16H(); // drop size
Expand All @@ -602,7 +603,7 @@ int FecLayer::input(const NetBuffer& buf, const NetContext& ctx)
{
nBuf.resize(fecSize - 2, 0);

auto inputResult = top->input(nBuf, ctx);
auto inputResult = top->input(std::move(nBuf), ctx, this);
if (inputResult == SOCKET_ERROR)
{
NS::log::NEW_NET.get()->error("[FEC] top->input {} error: {}", ctx, inputResult);
Expand All @@ -620,7 +621,7 @@ int FecLayer::input(const NetBuffer& buf, const NetContext& ctx)
}
rBuf.resize(fecSize - 2, 0);

auto inputResult = top->input(rBuf, ctx);
auto inputResult = top->input(std::move(rBuf), ctx, this);
if (inputResult == SOCKET_ERROR)
{
NS::log::NEW_NET.get()->error("[FEC] top->input {} error: {}", ctx, inputResult);
Expand Down Expand Up @@ -967,7 +968,7 @@ int kcpOutput(const char* buf, int len, ikcpcb* kcp, void* user)
NS::log::NEW_NET.get()->error("[KCP] kcpOutput: Uninitalized bottom");
return -1;
}
auto sendToResult = source->sendto(NetBuffer(buf, len), layer->remoteAddr);
auto sendToResult = source->sendto(NetBuffer(buf, len), layer->remoteAddr, layer);
if (sendToResult == SOCKET_ERROR)
{
NS::log::NEW_NET.get()->error("[KCP] kcpOutput: sendto error");
Expand Down Expand Up @@ -995,7 +996,7 @@ KcpLayer::~KcpLayer()
cb = nullptr;
}

int KcpLayer::sendto(const NetBuffer& buf, const NetContext& ctx)
int KcpLayer::sendto(NetBuffer&& buf, const NetContext& ctx, const NetSink* top)
{
int result;
{
Expand All @@ -1013,7 +1014,7 @@ int KcpLayer::sendto(const NetBuffer& buf, const NetContext& ctx)
return result;
}

int KcpLayer::input(const NetBuffer& buf, const NetContext& ctx)
int KcpLayer::input(NetBuffer&& buf, const NetContext& ctx, const NetSource* bottom)
{
int result;
{
Expand All @@ -1031,7 +1032,7 @@ int KcpLayer::input(const NetBuffer& buf, const NetContext& ctx)
NetBuffer buf(std::move(std::vector<char>(peeksize)));
auto recvsize = ikcp_recv(cb, buf.data(), peeksize);
buf.resize(recvsize, 0);
top->input(buf, ctx);
top->input(std::move(buf), ctx, this);
}
peeksize = ikcp_peeksize(cb);
}
Expand Down Expand Up @@ -1069,3 +1070,101 @@ void KcpLayer::bindBottom(std::weak_ptr<NetSource> bottom)
{
this->bottom = bottom;
}

MuxLayer::MuxLayer() {}

MuxLayer::~MuxLayer() {}

int MuxLayer::sendto(NetBuffer&& buf, const NetContext& ctx, const NetSink* top)
{
if (!topInverseMap.contains((uintptr_t)top))
{
NS::log::NEW_NET.get()->error("[MUX] sendto {} silently dropping packets from unknown", ctx);
return SOCKET_ERROR;
}

buf.putU8H(topInverseMap[(uintptr_t)top]);
auto sendtoResult = bottom.lock()->sendto(std::move(buf), ctx, this);
if (sendtoResult == SOCKET_ERROR)
{
NS::log::NEW_NET.get()->error("[MUX] sendto {} error: {}", ctx, sendtoResult);
}

return sendtoResult;
}

int MuxLayer::input(NetBuffer&& buf, const NetContext& ctx, const NetSource* bottom)
{
IUINT8 channelId = buf.getU8H();
if (!topMap.contains(channelId))
{
NS::log::NEW_NET.get()->error("[MUX] input {} silently dropping packets from unknown", ctx);
return SOCKET_ERROR;
}
auto inputResult = topMap[channelId]->input(std::move(buf), ctx, this);
if (inputResult == SOCKET_ERROR)
{
NS::log::NEW_NET.get()->error("[MUX] input {} error: {}", ctx, inputResult);
}
return inputResult;
}

bool MuxLayer::initialized(int from)
{
if (from == FROM_TOP)
{
return bottom.lock()->initialized(FROM_TOP);
}
else if (from == FROM_BOT)
{
bool result = true;
for (const auto& entry : topMap)
{
if (!entry.second->initialized(FROM_BOT))
{
result = false;
break;
}
}
return result;
}
else if (from == FROM_CAL)
{
bool result = true;
for (const auto& entry : topMap)
{
if (!entry.second->initialized(FROM_BOT))
{
result = false;
break;
}
}
return result && bottom.lock()->initialized(FROM_TOP);
}
}

void MuxLayer::bindTop(IUINT8 channelId, std::shared_ptr<NetSink> top)
{
topMap.insert(std::make_pair(channelId, top));
topInverseMap.insert(std::make_pair((uintptr_t)top.get(), channelId));
}

void MuxLayer::bindBottom(std::weak_ptr<NetSource> bottom)
{
this->bottom = bottom;
}

DummySink::DummySink() {}

DummySink::~DummySink() {}

int DummySink::input(NetBuffer&& buf, const NetContext& ctx, const NetSource* bottom)
{
NS::log::NEW_NET.get()->error("[DUMMY] input {}: {} {}", ctx, buf.data()[0], buf.data()[1]);
return 0;
}

bool DummySink::initialized(int from)
{
return true;
}
Loading

0 comments on commit 896de24

Please sign in to comment.