Skip to content

Commit

Permalink
kcp pt1
Browse files Browse the repository at this point in the history
  • Loading branch information
WtzLAS committed Oct 14, 2023
1 parent 2633cc6 commit eccbb66
Show file tree
Hide file tree
Showing 158 changed files with 26,310 additions and 14,798 deletions.
11 changes: 8 additions & 3 deletions NorthstarDLL/NorthstarDLL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@
<Optimization>Disabled</Optimization>
<ForcedIncludeFiles>pch.h</ForcedIncludeFiles>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>MinHook.x64.lib;zlibstatic.lib;kcp.lib;libcrypto_static.lib;libssl_static.lib;imgui.lib;libcurl\lib\libcurl_a.lib;dbghelp.lib;Wldap32.lib;Normaliz.lib;version.lib;crypt32.lib;imm32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>MinHook.x64.lib;zlibstatic.lib;libcrypto_static.lib;libssl_static.lib;imgui.lib;libcurl\lib\libcurl_a.lib;dbghelp.lib;Wldap32.lib;Normaliz.lib;version.lib;crypt32.lib;imm32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ForceSymbolReferences>
</ForceSymbolReferences>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
Expand Down Expand Up @@ -236,11 +237,13 @@
<ClInclude Include="server\r2server.h" />
<ClInclude Include="server\serverchathooks.h" />
<ClInclude Include="server\serverpresence.h" />
<ClInclude Include="shared\kcp.h" />
<ClInclude Include="shared\ikcp.h" />
<ClInclude Include="shared\kcpintegration.h" />
<ClInclude Include="shared\keyvalues.h" />
<ClInclude Include="shared\maxplayers.h" />
<ClInclude Include="shared\misccommands.h" />
<ClInclude Include="shared\playlist.h" />
<ClInclude Include="shared\rs.h" />
<ClInclude Include="squirrel\squirrel.h" />
<ClInclude Include="squirrel\squirrelautobind.h" />
<ClInclude Include="squirrel\squirrelclasstypes.h" />
Expand Down Expand Up @@ -341,11 +344,13 @@
<ClCompile Include="shared\exploit_fixes\exploitfixes_lzss.cpp" />
<ClCompile Include="shared\exploit_fixes\exploitfixes_utf8parser.cpp" />
<ClCompile Include="shared\exploit_fixes\ns_limits.cpp" />
<ClCompile Include="shared\kcp.cpp" />
<ClCompile Include="shared\ikcp.cpp" />
<ClCompile Include="shared\kcpintegration.cpp" />
<ClCompile Include="shared\keyvalues.cpp" />
<ClCompile Include="shared\maxplayers.cpp" />
<ClCompile Include="shared\misccommands.cpp" />
<ClCompile Include="shared\playlist.cpp" />
<ClCompile Include="shared\rs.cpp" />
<ClCompile Include="squirrel\squirrel.cpp" />
<ClCompile Include="squirrel\squirrelautobind.cpp" />
<ClCompile Include="util\base64.cpp" />
Expand Down
16 changes: 14 additions & 2 deletions NorthstarDLL/NorthstarDLL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
<ClInclude Include="client\vguihooks.h">
<Filter>Header Files\client</Filter>
</ClInclude>
<ClInclude Include="shared\kcp.h">
<ClInclude Include="shared\kcpintegration.h">
<Filter>Header Files\shared</Filter>
</ClInclude>
<ClInclude Include="client\imgui.h">
Expand All @@ -360,6 +360,12 @@
<ClInclude Include="scripts\scriptjson.h">
<Filter>Header Files\scripts</Filter>
</ClInclude>
<ClInclude Include="shared\ikcp.h">
<Filter>Header Files\shared</Filter>
</ClInclude>
<ClInclude Include="shared\rs.h">
<Filter>Header Files\shared</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="client\audio.cpp">
Expand Down Expand Up @@ -638,7 +644,7 @@
<ClCompile Include="client\vguihooks.cpp">
<Filter>Source Files\client</Filter>
</ClCompile>
<ClCompile Include="shared\kcp.cpp">
<ClCompile Include="shared\kcpintegration.cpp">
<Filter>Source Files\shared</Filter>
</ClCompile>
<ClCompile Include="client\imgui.cpp">
Expand All @@ -653,6 +659,12 @@
<ClCompile Include="mods\modsavefiles.cpp">
<Filter>Source Files\mods</Filter>
</ClCompile>
<ClCompile Include="shared\ikcp.cpp">
<Filter>Source Files\shared</Filter>
</ClCompile>
<ClCompile Include="shared\rs.cpp">
<Filter>Source Files\shared</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<MASM Include="audio_asm.asm">
Expand Down
48 changes: 4 additions & 44 deletions NorthstarDLL/client/kcpstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,14 @@
#include "client/imgui.h"
#include "imgui/implot.h"
#include "core/hooks.h"
#include "shared/kcp.h"
#include "shared/ikcp.h"
#include "shared/kcpintegration.h"

ConVar* Cvar_kcp_stats;
ConVar* Cvar_kcp_stats_interval;

AUTOHOOK_INIT()

static inline void itimeofday(long* sec, long* usec)
{
static long mode = 0, addsec = 0;
BOOL retval;
static IINT64 freq = 1;
IINT64 qpc;
if (mode == 0)
{
retval = QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
freq = (freq == 0) ? 1 : freq;
retval = QueryPerformanceCounter((LARGE_INTEGER*)&qpc);
addsec = (long)time(NULL);
addsec = addsec - (long)((qpc / freq) & 0x7fffffff);
mode = 1;
}
retval = QueryPerformanceCounter((LARGE_INTEGER*)&qpc);
retval = retval * 2;
if (sec)
*sec = (long)(qpc / freq) + addsec;
if (usec)
*usec = (long)((qpc % freq) * 1000000 / freq);
}

/* get clock in millisecond 64 */
static inline IINT64 iclock64(void)
{
long s, u;
IINT64 value;
itimeofday(&s, &u);
value = ((IINT64)s) * 1000 + (u / 1000);
return value;
}

static inline IUINT32 iclock()
{
return (IUINT32)(iclock64() & 0xfffffffful);
}

static inline IINT32 itimediff(IUINT32 later, IUINT32 earlier)
{
return ((IINT32)(later - earlier));
}

const char* KCP_NETGRAPH_LABELS[] = {" SRTT", "LOS%", "RTS%", "RCS%"};

sliding_window sw_retrans_segs(10, false, true);
Expand All @@ -73,6 +31,7 @@ bool has_connection = false;

void draw_kcp_stats()
{
/*
if (Cvar_kcp_stats == nullptr || !Cvar_kcp_stats->GetBool() || !g_kcp_initialized())
{
return;
Expand Down Expand Up @@ -242,6 +201,7 @@ void draw_kcp_stats()
}
ImGui::End();
*/
}

ON_DLL_LOAD("client.dll", KCPSTATS, (CModule module))
Expand Down
1 change: 1 addition & 0 deletions NorthstarDLL/core/math/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace NS::Colors
Color RPAK (255, 190, 0 );
Color NORTHSTAR (66 , 72 , 128);
Color ECHO (150, 150, 159);
Color NEW_NET (255, 127, 0 );

Color TRACE (0 , 255, 255);
Color DEBUG (0 , 255, 255);
Expand Down
1 change: 1 addition & 0 deletions NorthstarDLL/core/math/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ namespace NS::Colors
extern Color RPAK;
extern Color NORTHSTAR;
extern Color ECHO;
extern Color NEW_NET;

extern Color TRACE;
extern Color DEBUG;
Expand Down
4 changes: 2 additions & 2 deletions NorthstarDLL/logging/crashhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ void PrintExceptionLog(ExceptionLog& exc)
// Generate format string for stack trace
std::stringstream formatString;
formatString << " {:<" << exc.longestModuleNameLength + 2 << "} {:<" << exc.longestRelativeAddressLength << "} {}";
std::string guide = fmt::format(formatString.str(), "Module Name", "Offset", "Full Address");
std::string guide = fmt::format(fmt::runtime(formatString.str()), "Module Name", "Offset", "Full Address");
std::string line(guide.length() + 2, '-');
spdlog::error(guide);
spdlog::error(line);

for (const auto& module : exc.trace)
spdlog::error(formatString.str(), module.name, module.relativeAddress, module.address);
spdlog::error(fmt::runtime(formatString.str()), module.name, module.relativeAddress, module.address);

// Print dump of most CPU registers
spdlog::error("");
Expand Down
4 changes: 4 additions & 0 deletions NorthstarDLL/logging/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace NS::log
std::shared_ptr<ColoredLogger> rpak;
std::shared_ptr<ColoredLogger> echo;

std::shared_ptr<ColoredLogger> NEW_NET;

std::shared_ptr<ColoredLogger> NORTHSTAR;
}; // namespace NS::log

Expand Down Expand Up @@ -190,6 +192,7 @@ void InitialiseLogging()
NS::log::fs = std::make_shared<ColoredLogger>("FILESYSTM", NS::Colors::FILESYSTEM);
NS::log::rpak = std::make_shared<ColoredLogger>("RPAK_FSYS", NS::Colors::RPAK);
NS::log::echo = std::make_shared<ColoredLogger>("ECHO", NS::Colors::ECHO);
NS::log::NEW_NET = std::make_shared<ColoredLogger>("NEW _ NET", NS::Colors::NEW_NET);

loggers.push_back(NS::log::SCRIPT_UI);
loggers.push_back(NS::log::SCRIPT_CL);
Expand All @@ -203,6 +206,7 @@ void InitialiseLogging()
loggers.push_back(NS::log::fs);
loggers.push_back(NS::log::rpak);
loggers.push_back(NS::log::echo);
loggers.push_back(NS::log::NEW_NET);
}

void NS::log::FlushLoggers()
Expand Down
9 changes: 7 additions & 2 deletions NorthstarDLL/logging/logging.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once
#include "spdlog/common.h"
#include "spdlog/sinks/base_sink.h"
#include "spdlog/logger.h"
#include "squirrel/squirrel.h"
#include "core/math/color.h"

namespace fmt_lib = fmt;

void CreateLogFiles();
void InitialiseLogging();
void InitialiseConsole();
Expand Down Expand Up @@ -59,7 +62,7 @@ class ColoredLogger : public spdlog::logger
{
sink->log(custom_msg);
}
SPDLOG_LOGGER_CATCH()
SPDLOG_LOGGER_CATCH(custom_msg.source)
}

for (auto& sink : custom_sinks_)
Expand All @@ -68,7 +71,7 @@ class ColoredLogger : public spdlog::logger
{
sink->custom_log(custom_msg);
}
SPDLOG_LOGGER_CATCH()
SPDLOG_LOGGER_CATCH(custom_msg.source)
}

if (should_flush_(custom_msg))
Expand Down Expand Up @@ -98,6 +101,8 @@ namespace NS::log
// Echo
extern std::shared_ptr<ColoredLogger> echo;

extern std::shared_ptr<ColoredLogger> NEW_NET;

extern std::shared_ptr<ColoredLogger> NORTHSTAR;

void FlushLoggers();
Expand Down
2 changes: 1 addition & 1 deletion NorthstarDLL/logging/loghooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void,, (BFRead* msg))
break;

default:
spdlog::warn("Unimplemented TextMsg type {}! printing to console", msg_dest);
spdlog::warn("Unimplemented TextMsg type {}! printing to console", (int)msg_dest);
[[fallthrough]];

case TextMsgPrintType_t::HUD_PRINTCONSOLE:
Expand Down
9 changes: 5 additions & 4 deletions NorthstarDLL/masterserver/masterserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ bool MasterServerManager::StartMatchmaking(MatchmakeInfo* status)
{
query_fmt_str.append(fmt::format("&playlist={}", status->playlistList[i]));
}
std::string query = fmt::format(query_fmt_str, Cvar_ns_matchmaker_hostname->GetString(), local_uid_escaped, token_escaped, "true")
std::string query =
fmt::format(fmt::runtime(query_fmt_str), Cvar_ns_matchmaker_hostname->GetString(), local_uid_escaped, token_escaped, "true")
.c_str(); // TODO: add working AA selection
//spdlog::warn("{}", query);
//return false;
Expand Down Expand Up @@ -526,7 +527,7 @@ void MasterServerManager::RequestServerList()
else
{
auto err = res.error();
spdlog::error("Failed requesting servers: error {}", err);
spdlog::error("Failed requesting servers: error {}", httplib::to_string(err));
m_bSuccessfullyConnected = false;
}
m_bRequestingServerList = false;
Expand Down Expand Up @@ -653,7 +654,7 @@ void MasterServerManager::AuthenticateWithOwnServer(const char* uid, const std::
else
{
auto err = res.error();
spdlog::error("Failed authenticating with local server: encountered connection error {}", err);
spdlog::error("Failed authenticating with local server: encountered connection error {}", httplib::to_string(err));
m_bSuccessfullyConnected = false;
m_bSuccessfullyAuthenticatedWithGameServer = false;
m_bScriptAuthenticatingWithGameServer = false;
Expand Down Expand Up @@ -808,7 +809,7 @@ void MasterServerManager::WritePlayerPersistentData(const char* player_id, const
else
{
auto err = res.error();
spdlog::error("[Pdata] Write persistence failed for user: {}, error: {}", strPlayerId, err);
spdlog::error("[Pdata] Write persistence failed for user: {}, error: {}", strPlayerId, httplib::to_string(err));

m_bSuccessfullyConnected = false;
}
Expand Down
5 changes: 2 additions & 3 deletions NorthstarDLL/scripts/client/scriptserverbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,12 @@ ADD_SQFUNC("void", NSConnectToAuthedServer, "", "", ScriptContext::UI)
R2::Cbuf_AddText(
R2::Cbuf_GetCurrentPlayer(),
fmt::format(
"kcp_connect ::ffff:{}.{}.{}.{} {} {}",
"connect {}.{}.{}.{}:{}",
info.ip.S_un.S_un_b.s_b1,
info.ip.S_un.S_un_b.s_b2,
info.ip.S_un.S_un_b.s_b3,
info.ip.S_un.S_un_b.s_b4,
info.port,
info.conv)
info.port)
.c_str(),
R2::cmd_source_t::kCommandSrcCode);

Expand Down
2 changes: 1 addition & 1 deletion NorthstarDLL/scripts/scriptmatchmakingevents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ MatchmakeManager::MatchmakeManager()
// TODO: cleanup
}
std::string connection_cmd = fmt::format(
"kcp_connect ::ffff:{}.{}.{}.{} {} {}",
"kcp_connect {}.{}.{}.{} {} {}",
conn_info.ip.S_un.S_un_b.s_b1,
conn_info.ip.S_un.S_un_b.s_b2,
conn_info.ip.S_un.S_un_b.s_b3,
Expand Down
5 changes: 3 additions & 2 deletions NorthstarDLL/server/auth/serverauthentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "httplib.h"
#include "nlohmann/json.hpp"
#include "shared/playlist.h"
#include "shared/kcp.h"

#include <fstream>
#include <filesystem>
#include <thread>
Expand Down Expand Up @@ -110,6 +110,7 @@ void ServerAuthenticationManager::StartPlayerAuthServer()
std::lock_guard<std::mutex> guard(m_AuthDataMutex);
m_RemoteAuthenticationData.insert(std::make_pair(request.get_param_value("authToken"), newAuthData));

/*
if (g_kcp_initialized())
{
response.set_content(
Expand All @@ -120,7 +121,7 @@ void ServerAuthenticationManager::StartPlayerAuthServer()
response.set_content(
"{\"success\":true}", "application/json");
}

*/
});

m_PlayerAuthServer.listen("0.0.0.0", Cvar_ns_player_auth_port->GetInt());
Expand Down
Loading

0 comments on commit eccbb66

Please sign in to comment.