Skip to content

Commit

Permalink
Merge branch 'main' into feat/auto-dl-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Alystrasz authored Dec 14, 2023
2 parents eda53a2 + 43f0bce commit d4ac223
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 5 deletions.
11 changes: 10 additions & 1 deletion NorthstarDLL/client/clientauthhooks.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "masterserver/masterserver.h"
#include "core/convar/convar.h"
#include "client/r2client.h"
#include "core/vanilla.h"

AUTOHOOK_INIT()

Expand All @@ -16,6 +17,14 @@ AUTOHOOK(AuthWithStryder, engine.dll + 0x1843A0,
void, __fastcall, (void* a1))
// clang-format on
{
// don't attempt to do Atlas auth if we are in vanilla compatibility mode
// this prevents users from joining untrustworthy servers (unless they use a concommand or something)
if (g_pVanillaCompatibility->GetVanillaCompatibility())
{
AuthWithStryder(a1);
return;
}

// game will call this forever, until it gets a valid auth key
// so, we need to manually invalidate our key until we're authed with northstar, then we'll allow game to auth with stryder
if (!g_pMasterServerManager->m_bOriginAuthWithMasterServerDone && Cvar_ns_has_agreed_to_send_token->GetInt() != DISAGREED_TO_SEND_TOKEN)
Expand All @@ -39,7 +48,7 @@ AUTOHOOK(Auth3PToken, engine.dll + 0x183760,
char*, __fastcall, ())
// clang-format on
{
if (g_pMasterServerManager->m_sOwnClientAuthToken[0])
if (!g_pVanillaCompatibility->GetVanillaCompatibility() && g_pMasterServerManager->m_sOwnClientAuthToken[0])
{
memset(p3PToken, 0x0, 1024);
strcpy(p3PToken, "Protocol 3: Protect the Pilot");
Expand Down
29 changes: 29 additions & 0 deletions NorthstarDLL/core/vanilla.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

/// Determines if we are in vanilla-compatibility mode.
/// In this mode we shouldn't auth with Atlas, which prevents users from joining a
/// non-trusted server. This means that we can unrestrict client/server commands
/// as well as various other small changes for compatibility
class VanillaCompatibility
{
public:
void SetVanillaCompatibility(bool isVanilla)
{
static bool bInitialised = false;
if (bInitialised)
return;

bInitialised = true;
m_bIsVanillaCompatible = isVanilla;
}

bool GetVanillaCompatibility()
{
return m_bIsVanillaCompatible;
}

private:
bool m_bIsVanillaCompatible = false;
};

inline VanillaCompatibility* g_pVanillaCompatibility;
5 changes: 5 additions & 0 deletions NorthstarDLL/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "logging/logging.h"
#include "logging/crashhandler.h"
#include "core/memalloc.h"
#include "core/vanilla.h"
#include "config/profile.h"
#include "plugins/plugin_abi.h"
#include "plugins/plugins.h"
Expand Down Expand Up @@ -53,6 +54,10 @@ bool InitialiseNorthstar()
bool bAllFatal = strstr(GetCommandLineA(), "-crash_handle_all") != NULL;
g_pCrashHandler->SetAllFatal(bAllFatal);

// determine if we are in vanilla-compatibility mode
g_pVanillaCompatibility = new VanillaCompatibility();
g_pVanillaCompatibility->SetVanillaCompatibility(strstr(GetCommandLineA(), "-vanilla") != NULL);

// Write launcher version to log
StartupLog();

Expand Down
7 changes: 4 additions & 3 deletions NorthstarDLL/masterserver/masterserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "shared/playlist.h"
#include "server/auth/serverauthentication.h"
#include "core/tier0.h"
#include "core/vanilla.h"
#include "engine/r2engine.h"
#include "mods/modmanager.h"
#include "shared/misccommands.h"
Expand Down Expand Up @@ -88,7 +89,7 @@ size_t CurlWriteToStringBufferCallback(char* contents, size_t size, size_t nmemb

void MasterServerManager::AuthenticateOriginWithMasterServer(const char* uid, const char* originToken)
{
if (m_bOriginAuthWithMasterServerInProgress)
if (m_bOriginAuthWithMasterServerInProgress || g_pVanillaCompatibility->GetVanillaCompatibility())
return;

// do this here so it's instantly set
Expand Down Expand Up @@ -466,7 +467,7 @@ void MasterServerManager::RequestMainMenuPromos()
void MasterServerManager::AuthenticateWithOwnServer(const char* uid, const char* playerToken)
{
// dont wait, just stop if we're trying to do 2 auth requests at once
if (m_bAuthenticatingWithGameServer)
if (m_bAuthenticatingWithGameServer || g_pVanillaCompatibility->GetVanillaCompatibility())
return;

m_bAuthenticatingWithGameServer = true;
Expand Down Expand Up @@ -601,7 +602,7 @@ void MasterServerManager::AuthenticateWithOwnServer(const char* uid, const char*
void MasterServerManager::AuthenticateWithServer(const char* uid, const char* playerToken, RemoteServerInfo server, const char* password)
{
// dont wait, just stop if we're trying to do 2 auth requests at once
if (m_bAuthenticatingWithGameServer)
if (m_bAuthenticatingWithGameServer || g_pVanillaCompatibility->GetVanillaCompatibility())
return;

m_bAuthenticatingWithGameServer = true;
Expand Down
28 changes: 28 additions & 0 deletions NorthstarDLL/mods/modmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Mod::Mod(fs::path modDir, char* jsonBuf)
ParseScripts(modJson);
ParseLocalization(modJson);
ParseDependencies(modJson);
ParsePluginDependencies(modJson);
ParseInitScript(modJson);

// A mod is remote if it's located in the remote mods folder
Expand Down Expand Up @@ -483,6 +484,28 @@ void Mod::ParseDependencies(rapidjson_document& json)
}
}

void Mod::ParsePluginDependencies(rapidjson_document& json)
{
if (!json.HasMember("PluginDependencies"))
return;

if (!json["PluginDependencies"].IsArray())
{
spdlog::warn("'PluginDependencies' field is not an object, skipping...");
return;
}

for (auto& name : json["PluginDependencies"].GetArray())
{
if (!name.IsString())
continue;

spdlog::info("Plugin Constant {} defined by {}", name.GetString(), Name);

PluginDependencyConstants.push_back(name.GetString());
}
}

void Mod::ParseInitScript(rapidjson_document& json)
{
if (!json.HasMember("InitScript"))
Expand Down Expand Up @@ -688,6 +711,11 @@ void ModManager::LoadMods()
m_DependencyConstants.emplace(pair);
}

for (std::string& dependency : mod.PluginDependencyConstants)
{
m_PluginDependencyConstants.insert(dependency);
}

if (m_bHasEnabledModsCfg && m_EnabledModsCfg.HasMember(mod.Name.c_str()))
mod.m_bEnabled = m_EnabledModsCfg[mod.Name.c_str()].IsTrue();
else
Expand Down
4 changes: 4 additions & 0 deletions NorthstarDLL/mods/modmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <vector>
#include <filesystem>
#include <unordered_set>

const std::string MOD_FOLDER_SUFFIX = "\\mods";
const std::string THUNDERSTORE_MOD_FOLDER_SUFFIX = "\\packages";
Expand Down Expand Up @@ -124,6 +125,7 @@ class Mod
// hashed with STR_HASH

std::unordered_map<std::string, std::string> DependencyConstants;
std::vector<std::string> PluginDependencyConstants;

public:
Mod(fs::path modPath, char* jsonBuf);
Expand All @@ -134,6 +136,7 @@ class Mod
void ParseScripts(rapidjson_document& json);
void ParseLocalization(rapidjson_document& json);
void ParseDependencies(rapidjson_document& json);
void ParsePluginDependencies(rapidjson_document& json);
void ParseInitScript(rapidjson_document& json);
};

Expand All @@ -160,6 +163,7 @@ class ModManager
std::vector<Mod> m_LoadedMods;
std::unordered_map<std::string, ModOverrideFile> m_ModFiles;
std::unordered_map<std::string, std::string> m_DependencyConstants;
std::unordered_set<std::string> m_PluginDependencyConstants;

public:
ModManager();
Expand Down
9 changes: 9 additions & 0 deletions NorthstarDLL/plugins/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ std::optional<Plugin> PluginManager::LoadPlugin(fs::path path, PluginInitFuncs*
plugin.dependencyName = plugin.name;
}

if (std::find_if(
plugin.dependencyName.begin(),
plugin.dependencyName.end(),
[&](char c) -> bool { return !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_'); }) !=
plugin.dependencyName.end())
{
NS::log::PLUGINSYS->warn("Dependency string \"{}\" in {} is not valid a squirrel constant!", plugin.dependencyName, plugin.name);
}

plugin.init_sqvm_client = (PLUGIN_INIT_SQVM_TYPE)GetProcAddress(pluginLib, "PLUGIN_INIT_SQVM_CLIENT");
plugin.init_sqvm_server = (PLUGIN_INIT_SQVM_TYPE)GetProcAddress(pluginLib, "PLUGIN_INIT_SQVM_SERVER");
plugin.inform_sqvm_created = (PLUGIN_INFORM_SQVM_CREATED_TYPE)GetProcAddress(pluginLib, "PLUGIN_INFORM_SQVM_CREATED");
Expand Down
8 changes: 8 additions & 0 deletions NorthstarDLL/shared/exploit_fixes/exploitfixes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "engine/r2engine.h"
#include "client/r2client.h"
#include "core/math/vector.h"
#include "core/vanilla.h"

AUTOHOOK_INIT()

Expand Down Expand Up @@ -33,6 +34,8 @@ AUTOHOOK(CLC_Screenshot_WriteToBuffer, engine.dll + 0x22AF20,
bool, __fastcall, (void* thisptr, void* buffer)) // 48 89 5C 24 ? 57 48 83 EC 20 8B 42 10
// clang-format on
{
if (g_pVanillaCompatibility->GetVanillaCompatibility())
return CLC_Screenshot_WriteToBuffer(thisptr, buffer);
return false;
}

Expand All @@ -41,6 +44,8 @@ AUTOHOOK(CLC_Screenshot_ReadFromBuffer, engine.dll + 0x221F00,
bool, __fastcall, (void* thisptr, void* buffer)) // 48 89 5C 24 ? 48 89 6C 24 ? 48 89 74 24 ? 57 48 83 EC 20 48 8B DA 48 8B 52 38
// clang-format on
{
if (g_pVanillaCompatibility->GetVanillaCompatibility())
return CLC_Screenshot_ReadFromBuffer(thisptr, buffer);
return false;
}

Expand Down Expand Up @@ -261,6 +266,9 @@ bool, __fastcall, (const char* pModName)) // 48 83 EC 28 48 8B 0D ? ? ? ? 48 8D
R2::g_pModName = new char[iSize + 1];
strcpy(R2::g_pModName, pModName);

if (g_pVanillaCompatibility->GetVanillaCompatibility())
return false;

return (!strcmp("r2", pModName) || !strcmp("r1", pModName)) && !Tier0::CommandLine()->CheckParm("-norestrictservercommands");
}

Expand Down
11 changes: 11 additions & 0 deletions NorthstarDLL/squirrel/squirrel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "plugins/plugin_abi.h"
#include "plugins/plugins.h"
#include "ns_version.h"
#include "core/vanilla.h"

#include <any>

Expand Down Expand Up @@ -263,6 +264,13 @@ template <ScriptContext context> void SquirrelManager<context>::VMCreated(CSquir
defconst(m_pSQVM, pair.first.c_str(), bWasFound);
}

auto loadedPlugins = &g_pPluginManager->m_vLoadedPlugins;
for (const auto& pluginName : g_pModManager->m_PluginDependencyConstants)
{
auto f = [&](Plugin plugin) -> bool { return plugin.dependencyName == pluginName; };
defconst(m_pSQVM, pluginName.c_str(), std::find_if(loadedPlugins->begin(), loadedPlugins->end(), f) != loadedPlugins->end());
}

defconst(m_pSQVM, "MAX_FOLDER_SIZE", GetMaxSaveFolderSize() / 1024);

// define squirrel constants for northstar(.dll) version
Expand All @@ -272,6 +280,9 @@ template <ScriptContext context> void SquirrelManager<context>::VMCreated(CSquir
defconst(m_pSQVM, "NS_VERSION_PATCH", version[2]);
defconst(m_pSQVM, "NS_VERSION_DEV", version[3]);

// define squirrel constant for if we are in vanilla-compatibility mode
defconst(m_pSQVM, "VANILLA", g_pVanillaCompatibility->GetVanillaCompatibility());

g_pSquirrel<context>->messageBuffer = new SquirrelMessageBuffer();
g_pPluginManager->InformSQVMCreated(context, newSqvm);
}
Expand Down
2 changes: 1 addition & 1 deletion NorthstarLauncher/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ void PrependPath()
bool ShouldLoadNorthstar(int argc, char* argv[])
{
for (int i = 0; i < argc; i++)
if (!strcmp(argv[i], "-vanilla"))
if (!strcmp(argv[i], "-nonorthstardll"))
return false;

auto runNorthstarFile = std::ifstream("run_northstar.txt");
Expand Down
6 changes: 6 additions & 0 deletions cmake/Findminizip.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
if(NOT minizip_FOUND)
check_init_submodule(${PROJECT_SOURCE_DIR}/thirdparty/minizip)

set(MZ_ZLIB ON CACHE BOOL "Enable ZLIB compression, needed for DEFLATE")
set(MZ_BZIP2 OFF CACHE BOOL "Disable BZIP2 compression")
set(MZ_LZMA OFF CACHE BOOL "Disable LZMA & XZ compression")
set(MZ_PKCRYPT OFF CACHE BOOL "Disable PKWARE traditional encryption")
set(MZ_WZAES OFF CACHE BOOL "Disable WinZIP AES encryption")
set(MZ_ZSTD OFF CACHE BOOL "Disable ZSTD compression")
set(MZ_SIGNING OFF CACHE BOOL "Disable zip signing support")

add_subdirectory(${PROJECT_SOURCE_DIR}/thirdparty/minizip minizip)
set(minizip_FOUND 1 PARENT_SCOPE)
Expand Down

0 comments on commit d4ac223

Please sign in to comment.