Skip to content

Commit

Permalink
feat(steampp): add C wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Jul 10, 2024
1 parent 69706cc commit 8bd6761
Show file tree
Hide file tree
Showing 19 changed files with 246 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ add_sourcepp_library(dmxpp)
add_sourcepp_library(fgdpp)
add_sourcepp_library(kvpp)
add_sourcepp_library(mdlpp)
add_sourcepp_library(steampp)
add_sourcepp_library(steampp C)
add_sourcepp_library(vicepp C CSHARP)
add_sourcepp_library(vpkpp C CSHARP)
add_sourcepp_library(vtfpp)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<td align="center">n/a</td>
<td align="center">✅</td>
<td align="center">n/a</td>
<td align="center"></td>
<td align="center">C</td>
<td>Based on the <a href="https://github.com/Trico-Everfire/SteamAppPathProvider">SteamAppPathProvider</a> library by <a href="https://github.com/Trico-Everfire">@Trice Everfire</a> and <a href="https://momentum-mod.org">Momentum Mod</a> contributors.</td>
</tr>
<tr>
Expand Down
13 changes: 13 additions & 0 deletions lang/c/include/sourceppc/Convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <string>
#include <string_view>
#include <type_traits>
#include <vector>

#include "Buffer.h"
Expand All @@ -27,4 +28,16 @@ size_t writeStringToMem(std::string_view str, char* buffer, size_t bufferLen);

size_t writeVectorToMem(const std::vector<std::byte>& vec, unsigned char* buffer, size_t bufferLen);

// requires clause copied from BufferStream - not including here because that header is HEAVY
template<typename T>
requires std::is_trivial_v<T> && std::is_standard_layout_v<T> && (!std::is_pointer_v<T>)
size_t writeVectorToMem(const std::vector<T>& vec, T* buffer, size_t bufferLen) {
if (vec.size() >= bufferLen) {
std::memcpy(buffer, vec.data(), sizeof(T) * bufferLen);
return bufferLen;
}
std::memcpy(buffer, vec.data(), sizeof(T) * vec.size());
return vec.size();
}

} // namespace Convert
23 changes: 23 additions & 0 deletions lang/c/include/steamppc/Convert.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

/*
* This is a header designed to be included in C++ source code.
* It should not be included in applications using any C wrapper libraries!
*/
#ifndef __cplusplus
#error "This header can only be used in C++!"
#endif

#include "steampp.h"

namespace steampp {

class Steam;

} // namespace steampp

namespace Convert {

steampp::Steam* steam(steampp_steam_handle_t handle);

} // namespace Convert
56 changes: 56 additions & 0 deletions lang/c/include/steamppc/steampp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <sourceppc/Buffer.h>
#include <sourceppc/String.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef uint32_t AppID;

typedef void* steampp_steam_handle_t;

#ifdef __cplusplus
} // extern "C"
#endif

// REQUIRES MANUAL FREE: steampp_steam_free
SOURCEPP_API steampp_steam_handle_t steampp_steam_new();

SOURCEPP_API void steampp_steam_free(steampp_steam_handle_t* handle);

SOURCEPP_API const char* steampp_get_install_dir(steampp_steam_handle_t handle);

// REQUIRES MANUAL FREE: sourcepp_string_array_free
SOURCEPP_API sourcepp_string_array_t steampp_get_library_dirs(steampp_steam_handle_t handle);

// REQUIRES MANUAL FREE: sourcepp_string_free
SOURCEPP_API sourcepp_string_t steampp_get_sourcemod_dir(steampp_steam_handle_t handle);

SOURCEPP_API size_t steampp_get_installed_apps(steampp_steam_handle_t handle, AppID* array, size_t arrayLen);

SOURCEPP_API size_t steampp_get_installed_apps_count(steampp_steam_handle_t handle);

SOURCEPP_API bool steampp_is_app_installed(steampp_steam_handle_t handle, AppID appID);

SOURCEPP_API const char* steampp_get_app_name(steampp_steam_handle_t handle, AppID appID);

// REQUIRES MANUAL FREE: sourcepp_string_free
SOURCEPP_API sourcepp_string_t steampp_get_app_install_dir(steampp_steam_handle_t handle, AppID appID);

// REQUIRES MANUAL FREE: sourcepp_string_free
SOURCEPP_API sourcepp_string_t steampp_get_app_icon_path(steampp_steam_handle_t handle, AppID appID);

// REQUIRES MANUAL FREE: sourcepp_string_free
SOURCEPP_API sourcepp_string_t steampp_get_app_logo_path(steampp_steam_handle_t handle, AppID appID);

// REQUIRES MANUAL FREE: sourcepp_string_free
SOURCEPP_API sourcepp_string_t steampp_get_app_box_art_path(steampp_steam_handle_t handle, AppID appID);

// REQUIRES MANUAL FREE: sourcepp_string_free
SOURCEPP_API sourcepp_string_t steampp_get_app_store_art_path(steampp_steam_handle_t handle, AppID appID);

SOURCEPP_API bool steampp_is_app_using_source_engine(steampp_steam_handle_t handle, AppID appID);

SOURCEPP_API bool steampp_is_app_using_source_2_engine(steampp_steam_handle_t handle, AppID appID);
5 changes: 3 additions & 2 deletions lang/c/include/vpkppc/Convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#endif

#include <vpkpp/Options.h>
#include <vpkppc/Entry.h>
#include <vpkppc/PackFile.h>

#include "Entry.h"
#include "PackFile.h"

namespace vpkpp {

Expand Down
2 changes: 2 additions & 0 deletions lang/c/include/vpkppc/PackFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ typedef void* vpkpp_pack_file_handle_t;
} // extern "C"
#endif

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_open_with_options(const char* path, vpkpp_pack_file_options_t options);

SOURCEPP_API vpkpp_pack_file_type_e vpkpp_get_type(vpkpp_pack_file_handle_t handle);
Expand Down
2 changes: 2 additions & 0 deletions lang/c/include/vpkppc/format/BSP.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../PackFile.h"

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_bsp_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_bsp_open_with_options(const char* path, vpkpp_pack_file_options_t options);
2 changes: 2 additions & 0 deletions lang/c/include/vpkppc/format/FPX.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../PackFile.h"

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_fpx_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_fpx_open_with_options(const char* path, vpkpp_pack_file_options_t options);
2 changes: 2 additions & 0 deletions lang/c/include/vpkppc/format/GCF.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../PackFile.h"

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_gcf_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_gcf_open_with_options(const char* path, vpkpp_pack_file_options_t options);
2 changes: 2 additions & 0 deletions lang/c/include/vpkppc/format/GMA.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../PackFile.h"

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_gma_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_gma_open_with_options(const char* path, vpkpp_pack_file_options_t options);
2 changes: 2 additions & 0 deletions lang/c/include/vpkppc/format/GRP.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../PackFile.h"

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_grp_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_grp_open_with_options(const char* path, vpkpp_pack_file_options_t options);
2 changes: 2 additions & 0 deletions lang/c/include/vpkppc/format/PAK.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../PackFile.h"

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_pak_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_pak_open_with_options(const char* path, vpkpp_pack_file_options_t options);
2 changes: 2 additions & 0 deletions lang/c/include/vpkppc/format/PCK.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../PackFile.h"

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_pck_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_pck_open_with_options(const char* path, vpkpp_pack_file_options_t options);
6 changes: 6 additions & 0 deletions lang/c/include/vpkppc/format/VPK.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

#include "../PackFile.h"

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_vpk_create_empty(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_vpk_create_empty_with_options(const char* path, vpkpp_pack_file_options_t options);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_vpk_create_from_directory(const char* vpkPath, const char* contentPath, bool saveToDir);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_vpk_create_from_directory_with_options(const char* vpkPath, const char* contentPath, bool saveToDir, vpkpp_pack_file_options_t options);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_vpk_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_vpk_open_with_options(const char* path, vpkpp_pack_file_options_t options);

SOURCEPP_API bool vpkpp_vpk_generate_keypair_files(const char* path);
Expand Down
2 changes: 2 additions & 0 deletions lang/c/include/vpkppc/format/ZIP.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../PackFile.h"

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_zip_open(const char* path);

// REQUIRES MANUAL FREE: vpkpp_close
SOURCEPP_API vpkpp_pack_file_handle_t vpkpp_zip_open_with_options(const char* path, vpkpp_pack_file_options_t options);
9 changes: 9 additions & 0 deletions lang/c/src/steamppc/Convert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <steamppc/Convert.hpp>

#include <steampp/steampp.h>

using namespace steampp;

Steam* Convert::steam(steampp_steam_handle_t handle) {
return static_cast<Steam*>(handle);
}
5 changes: 5 additions & 0 deletions lang/c/src/steamppc/_steamppc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_pretty_parser(steampp C SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/lang/c/include/steamppc/Convert.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/lang/c/include/steamppc/steampp.h"
"${CMAKE_CURRENT_LIST_DIR}/Convert.cpp"
"${CMAKE_CURRENT_LIST_DIR}/steampp.cpp")
111 changes: 111 additions & 0 deletions lang/c/src/steamppc/steampp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <steamppc/steampp.h>

#include <steampp/steampp.h>

#include <sourceppc/Convert.hpp>
#include <sourceppc/Helpers.h>
#include <steamppc/Convert.hpp>

using namespace steampp;

SOURCEPP_API steampp_steam_handle_t steampp_steam_new() {
auto* steam = new Steam{};
if (!*steam) {
delete steam;
steam = nullptr;
}
return steam;
}

SOURCEPP_API void steampp_steam_free(steampp_steam_handle_t* handle) {
SOURCEPP_EARLY_RETURN(handle);

delete Convert::steam(*handle);
*handle = nullptr;
}

SOURCEPP_API const char* steampp_get_install_dir(steampp_steam_handle_t handle) {
SOURCEPP_EARLY_RETURN_VAL(handle, "");

return Convert::steam(handle)->getInstallDir().data();
}

SOURCEPP_API sourcepp_string_array_t steampp_get_library_dirs(steampp_steam_handle_t handle) {
SOURCEPP_EARLY_RETURN_VAL(handle, SOURCEPP_STRING_ARRAY_INVALID);

return Convert::toStringArray(Convert::steam(handle)->getLibraryDirs());
}

SOURCEPP_API sourcepp_string_t steampp_get_sourcemod_dir(steampp_steam_handle_t handle) {
SOURCEPP_EARLY_RETURN_VAL(handle, SOURCEPP_STRING_INVALID);

return Convert::toString(Convert::steam(handle)->getSourceModDir());
}

SOURCEPP_API size_t steampp_get_installed_apps(steampp_steam_handle_t handle, AppID* array, size_t arrayLen) {
SOURCEPP_EARLY_RETURN_VAL(handle, 0);
SOURCEPP_EARLY_RETURN_VAL(array, 0);
SOURCEPP_EARLY_RETURN_VAL(arrayLen, 0);

return Convert::writeVectorToMem<AppID>(Convert::steam(handle)->getInstalledApps(), array, arrayLen);
}

SOURCEPP_API size_t steampp_get_installed_apps_count(steampp_steam_handle_t handle) {
SOURCEPP_EARLY_RETURN_VAL(handle, 0);

return Convert::steam(handle)->getInstalledApps().size();
}

SOURCEPP_API bool steampp_is_app_installed(steampp_steam_handle_t handle, AppID appID) {
SOURCEPP_EARLY_RETURN_VAL(handle, false);

return Convert::steam(handle)->isAppInstalled(appID);
}

SOURCEPP_API const char* steampp_get_app_name(steampp_steam_handle_t handle, AppID appID) {
SOURCEPP_EARLY_RETURN_VAL(handle, "");

return Convert::steam(handle)->getAppName(appID).data();
}

SOURCEPP_API sourcepp_string_t steampp_get_app_install_dir(steampp_steam_handle_t handle, AppID appID) {
SOURCEPP_EARLY_RETURN_VAL(handle, SOURCEPP_STRING_INVALID);

return Convert::toString(Convert::steam(handle)->getAppInstallDir(appID));
}

SOURCEPP_API sourcepp_string_t steampp_get_app_icon_path(steampp_steam_handle_t handle, AppID appID) {
SOURCEPP_EARLY_RETURN_VAL(handle, SOURCEPP_STRING_INVALID);

return Convert::toString(Convert::steam(handle)->getAppIconPath(appID));
}

SOURCEPP_API sourcepp_string_t steampp_get_app_logo_path(steampp_steam_handle_t handle, AppID appID) {
SOURCEPP_EARLY_RETURN_VAL(handle, SOURCEPP_STRING_INVALID);

return Convert::toString(Convert::steam(handle)->getAppLogoPath(appID));
}

SOURCEPP_API sourcepp_string_t steampp_get_app_box_art_path(steampp_steam_handle_t handle, AppID appID) {
SOURCEPP_EARLY_RETURN_VAL(handle, SOURCEPP_STRING_INVALID);

return Convert::toString(Convert::steam(handle)->getAppBoxArtPath(appID));
}

SOURCEPP_API sourcepp_string_t steampp_get_app_store_art_path(steampp_steam_handle_t handle, AppID appID) {
SOURCEPP_EARLY_RETURN_VAL(handle, SOURCEPP_STRING_INVALID);

return Convert::toString(Convert::steam(handle)->getAppStoreArtPath(appID));
}

SOURCEPP_API bool steampp_is_app_using_source_engine(steampp_steam_handle_t handle, AppID appID) {
SOURCEPP_EARLY_RETURN_VAL(handle, false);

return Convert::steam(handle)->isAppUsingSourceEngine(appID);
}

SOURCEPP_API bool steampp_is_app_using_source_2_engine(steampp_steam_handle_t handle, AppID appID) {
SOURCEPP_EARLY_RETURN_VAL(handle, false);

return Convert::steam(handle)->isAppUsingSource2Engine(appID);
}

0 comments on commit 8bd6761

Please sign in to comment.