Skip to content

Commit

Permalink
HTTP client & basic API interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaKeks committed Jul 22, 2024
1 parent 41cea20 commit aed5bdf
Show file tree
Hide file tree
Showing 36 changed files with 2,077 additions and 22 deletions.
28 changes: 24 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,39 @@ AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: false
AlwaysBreakTemplateDeclarations: true
BitFieldColonSpacing: After

BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true

BreakArrays: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Allman
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
AlwaysBreakTemplateDeclarations: true
ColumnLimit: 150
CompactNamespaces: false
EmptyLineBeforeAccessModifier: Always
IncludeBlocks: Preserve
IndentCaseLabels: true
IndentWidth: 4
InsertBraces: true
InsertNewlineAtEOF: true
KeepEmptyLinesAtTheStartOfBlocks: false
Expand All @@ -45,6 +66,5 @@ SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpacesInAngles: Never
SpacesInSquareBrackets: false
UseTab: ForContinuationAndIndentation
IndentWidth: 4
TabWidth: 4
UseTab: ForContinuationAndIndentation
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
[submodule "vendor/ClientCvarValue"]
path = vendor/ClientCvarValue
url = https://github.com/komashchenko/ClientCvarValue.git
[submodule "vendor/json"]
path = vendor/json
url = https://github.com/nlohmann/json
7 changes: 7 additions & 0 deletions AMBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ for sdk_target in MMSPlugin.sdk_targets:
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'funchook.lib'),
os.path.join(builder.sourcePath, 'vendor', 'funchook', 'lib', 'distorm.lib'),
os.path.join(sdk['path'], 'lib', 'public', 'win64', 'mathlib.lib'),
os.path.join(sdk['path'], 'lib', 'public', 'win64', 'steam_api64.lib')
]
binary.sources += [
'src/utils/plat_win.cpp'
Expand All @@ -76,6 +77,7 @@ for sdk_target in MMSPlugin.sdk_targets:
os.path.join(builder.sourcePath, 'src', 'utils', 'utils_print.cpp'),
os.path.join(builder.sourcePath, 'src', 'utils', 'gameconfig.cpp'),
os.path.join(builder.sourcePath, 'src', 'utils', 'hooks.cpp'),
os.path.join(builder.sourcePath, 'src', 'utils', 'http.cpp'),
os.path.join(builder.sourcePath, 'src', 'utils', 'detours.cpp'),
os.path.join(builder.sourcePath, 'src', 'utils', 'schema.cpp'),
os.path.join(builder.sourcePath, 'src', 'utils', 'simplecmds.cpp'),
Expand All @@ -97,6 +99,11 @@ for sdk_target in MMSPlugin.sdk_targets:
os.path.join(builder.sourcePath, 'src', 'kz', 'checkpoint', 'kz_checkpoint.cpp'),
os.path.join(builder.sourcePath, 'src', 'kz', 'checkpoint', 'commands.cpp'),
os.path.join(builder.sourcePath, 'src', 'kz', 'global', 'kz_global.cpp'),
os.path.join(builder.sourcePath, 'src', 'kz', 'global', 'commands.cpp'),
os.path.join(builder.sourcePath, 'src', 'kz', 'global', 'error.cpp'),
os.path.join(builder.sourcePath, 'src', 'kz', 'global', 'modes.cpp'),
os.path.join(builder.sourcePath, 'src', 'kz', 'global', 'players.cpp'),
os.path.join(builder.sourcePath, 'src', 'kz', 'global', 'maps.cpp'),
os.path.join(builder.sourcePath, 'src', 'kz', 'hud', 'kz_hud.cpp'),

os.path.join(builder.sourcePath, 'src', 'kz', 'jumpstats', 'kz_jumpstats.cpp'),
Expand Down
15 changes: 8 additions & 7 deletions cfg/cs2kz-server-config.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"ServerCfg"
{
"defaultMode" "Classic"
"defaultStyle" "Normal"
"defaultLanguage" "en"
"tipInterval" "75"
"defaultJSBroadcastMinTier" "4"
"defaultJSSoundMinTier" "4"
"chatPrefix" "{lime}KZ {grey}|{default}"
"defaultMode" "Classic"
"defaultStyle" "Normal"
"defaultLanguage" "en"
"tipInterval" "75"
"defaultJSBroadcastMinTier" "4"
"defaultJSSoundMinTier" "4"
"chatPrefix" "{lime}KZ {grey}|{default}"
"apiUrl" "http://localhost:42069"
}
2 changes: 2 additions & 0 deletions src/cs2kz.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "kz/global/kz_global.h"
#include "cs2kz.h"

#include "entity2/entitysystem.h"
Expand Down Expand Up @@ -58,6 +59,7 @@ bool KZPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool

KZOptionService::InitOptions();
KZTipService::InitTips();
KZGlobalService::Init();
return true;
}

Expand Down
81 changes: 81 additions & 0 deletions src/kz/global/commands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "kz_global.h"
#include "kz/language/kz_language.h"
#include "utils/simplecmds.h"

static_function SCMD_CALLBACK(Command_KzProfile)
{
KZPlayer *player = g_pKZPlayerManager->ToPlayer(controller);

auto onSuccess = [player](std::optional<KZ::API::Player> info) {
if (!info)
{
player->languageService->PrintChat(true, false, "Player not found");
return;
}

const char *name = info->name.c_str();
const char *steamID = info->steamID.c_str();
const char *isBanned = info->isBanned ? info->isBanned.value() ? "" : "not " : "maybe ";

player->languageService->PrintChat(true, false, "Display PlayerInfo", name, steamID, isBanned);
};

auto onError = [player](KZ::API::Error error) {
player->languageService->PrintError(error);
};

const char *playerIdentifier = args->Arg(1);

if (playerIdentifier[0] == '\0')
{
KZGlobalService::FetchPlayer(player->GetSteamId64(), onSuccess, onError);
}
else
{
KZGlobalService::FetchPlayer(playerIdentifier, onSuccess, onError);
}

return MRES_SUPERCEDE;
}

static_function SCMD_CALLBACK(Command_KzMapInfo)
{
KZPlayer *player = g_pKZPlayerManager->ToPlayer(controller);
const char *mapIdentifier = args->Arg(1);

if (mapIdentifier[0] != '\0')
{
auto onSuccess = [player](std::optional<KZ::API::Map> map) {
if (!map)
{
player->languageService->PrintChat(true, false, "MapNotGlobal");
return;
}

player->languageService->PrintMap(map.value());
};

auto onError = [player](KZ::API::Error error) {
player->languageService->PrintError(error);
};

KZGlobalService::FetchMap(mapIdentifier, onSuccess, onError);
}
else if (KZGlobalService::currentMap)
{
player->languageService->PrintMap(KZGlobalService::currentMap.value());
}
else
{
player->languageService->PrintChat(true, false, "MapNotGlobal");
}

return MRES_SUPERCEDE;
}

void KZGlobalService::RegisterCommands()
{
scmd::RegisterCmd("kz_profile", Command_KzProfile);
scmd::RegisterCmd("kz_mapinfo", Command_KzMapInfo);
scmd::RegisterCmd("kz_minfo", Command_KzMapInfo);
}
41 changes: 41 additions & 0 deletions src/kz/global/error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "error.h"
#include "kz/language/kz_language.h"

namespace KZ::API
{
Error::Error(u16 status, std::string message) : status(status)
{
if (!json::accept(message))
{
this->message = message;
return;
}

const json error = json::parse(message);

if (!error.is_object())
{
META_CONPRINTF("[KZ::Global] API error is not an object: `%s`\n", error.dump().c_str());
return;
}

if (!error.contains("message"))
{
META_CONPRINTF("[KZ::Global] API error does not contain a message: `%s`\n", error.dump().c_str());
return;
}

if (!error["message"].is_string())
{
META_CONPRINTF("[KZ::Global] API error message is not a string: `%s`\n", error.dump().c_str());
return;
}

this->message = error["message"];

if (error.contains("details"))
{
this->details = error["details"];
}
}
} // namespace KZ::API
36 changes: 36 additions & 0 deletions src/kz/global/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <optional>
#include <string>
#include "common.h"
#include "utils/json.h"

namespace KZ::API
{
/// An error object returned by the API.
struct Error
{
/// The HTTP status code returned alongside the error.
u16 status;

/// The error message.
std::string message;

/// Additional details that may or may not exist.
json details;

/// Parses an error from a response.
///
/// If `message` is JSON, it will be deserialized.
Error(u16 status, std::string message);
};

/// An error that occurred while parsing JSON.
struct ParseError
{
/// The reason we failed.
std::string reason;

ParseError(std::string reason) : reason(reason) {}
};
} // namespace KZ::API
Loading

0 comments on commit aed5bdf

Please sign in to comment.