From 72a5b2f91f133f6bde99652733f37a0a815cde7b Mon Sep 17 00:00:00 2001 From: Yan Couto Date: Wed, 19 Dec 2018 18:52:48 -0200 Subject: [PATCH 1/3] Splitting behavior in multiple files --- Makefile | 2 +- src/common.cpp | 39 +++++ src/common.hpp | 32 +++++ src/core.cpp | 45 ++++++ src/core.hpp | 12 ++ src/friends.cpp | 78 ++++++++++ src/friends.hpp | 12 ++ src/main.cpp | 17 +++ luasteam.cxx => src/user_stats.cpp | 224 ++++------------------------- src/user_stats.hpp | 12 ++ 10 files changed, 275 insertions(+), 198 deletions(-) create mode 100644 src/common.cpp create mode 100644 src/common.hpp create mode 100644 src/core.cpp create mode 100644 src/core.hpp create mode 100644 src/friends.cpp create mode 100644 src/friends.hpp create mode 100644 src/main.cpp rename luasteam.cxx => src/user_stats.cpp (70%) create mode 100644 src/user_stats.hpp diff --git a/Makefile b/Makefile index b621f7f..5b43067 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -SRC=luasteam.cxx +SRC=src/*.cpp STDLIB_VER=-std=c++11 # -Wno-invalid-offsetof prevents STEAM_CALLBACK from giving out warnings diff --git a/src/common.cpp b/src/common.cpp new file mode 100644 index 0000000..e35f399 --- /dev/null +++ b/src/common.cpp @@ -0,0 +1,39 @@ +#include "common.hpp" + +namespace { +int uint64Metatable_ref = LUA_NOREF; +} // namespace + +namespace luasteam { + +lua_State *global_lua_state = nullptr; + +inline void pushuint64(lua_State *L, uint64 v) { + uint64 *ptr = reinterpret_cast(lua_newuserdata(L, sizeof(uint64))); + *ptr = v; + lua_rawgeti(L, LUA_REGISTRYINDEX, uint64Metatable_ref); + lua_setmetatable(L, -2); +} + +inline uint64 checkuint64(lua_State *L, int nParam) { + luaL_argcheck(L, lua_isuserdata(L, nParam), nParam, "must be userdata"); + lua_rawgeti(L, LUA_REGISTRYINDEX, uint64Metatable_ref); + lua_getmetatable(L, nParam); + luaL_argcheck(L, lua_rawequal(L, -2, -1), nParam, "must be uint64"); + lua_pop(L, 2); + uint64 *ptr = reinterpret_cast(lua_touserdata(L, nParam)); + return *ptr; +} + +inline void add_func(lua_State *L, const char *name, lua_CFunction func) { + lua_pushcfunction(L, func); + lua_setfield(L, -2, name); +} + +void init_common(lua_State *L) { + global_lua_state = L; + lua_createtable(L, 0, 0); + uint64Metatable_ref = luaL_ref(L, LUA_REGISTRYINDEX); +} + +} // namespace luasteam diff --git a/src/common.hpp b/src/common.hpp new file mode 100644 index 0000000..18c73c2 --- /dev/null +++ b/src/common.hpp @@ -0,0 +1,32 @@ +#ifndef LUASTEAM_COMMON +#define LUASTEAM_COMMON + +#include "../sdk/public/steam/steam_api.h" +extern "C" { +#include +#include +} + +#ifdef _WIN32 +# define EXTERN extern "C" __declspec(dllexport) +#else +# define EXTERN extern "C" +#endif + +namespace luasteam { + +// Use this with care. Should only be used for Callbacks and CallResults. +extern lua_State *global_lua_state; + +// using userdata since Lua's number can't safely hold 64-bit integers +void pushuint64(lua_State *L, uint64 v); +uint64 checkuint64(lua_State *L, int nParam); + +// Adds a C function to the table on top of the stack, with given name +void add_func(lua_State *L, const char *name, lua_CFunction func); + +void init_common(lua_State *L); + +} // namespace luasteam + +#endif // LUASTEAM_COMMON diff --git a/src/core.cpp b/src/core.cpp new file mode 100644 index 0000000..65ad3a9 --- /dev/null +++ b/src/core.cpp @@ -0,0 +1,45 @@ +#include "core.hpp" + +// ======================== +// ======= SteamAPI ======= +// ======================== + +// bool SteamAPI_Init(); +EXTERN int luasteam_init(lua_State *L) { + bool success = SteamAPI_Init(); + if (!success) { + fprintf(stderr, "Couldn't connect to steam...\nDo you have Steam turned on?\nIf not running from steam, do you have a correct steam_appid.txt file?\n"); + } + lua_pushboolean(L, success); + return 1; +} + +// void SteamAPI_Shutdown(); +EXTERN int luasteam_shutdown(lua_State *L) { + SteamAPI_Shutdown(); + // Cleaning up + // global_lua_state = nullptr; + // luaL_unref(L, LUA_REGISTRYINDEX, friends_ref); + // friends_ref = LUA_NOREF; + // delete friends_listener; + // friends_listener = nullptr; + // luaL_unref(L, LUA_REGISTRYINDEX, uint64Metatable_ref); + // uint64Metatable_ref = LUA_NOREF; + return 0; +} + +// void SteamAPI_RunCallbacks(); +EXTERN int luasteam_runCallbacks(lua_State *L) { + SteamAPI_RunCallbacks(); + return 0; +} + +namespace luasteam { + +void add_core(lua_State *L) { + add_func(L, "init", luasteam_init); + add_func(L, "shutdown", luasteam_shutdown); + add_func(L, "runCallbacks", luasteam_runCallbacks); +} + +} // namespace luasteam diff --git a/src/core.hpp b/src/core.hpp new file mode 100644 index 0000000..02a63db --- /dev/null +++ b/src/core.hpp @@ -0,0 +1,12 @@ +#ifndef LUASTEAM_CORE +#define LUASTEAM_CORE + +#include "common.hpp" + +namespace luasteam { + +void add_core(lua_State *L); + +} // namespace luasteam + +#endif // LUASTEAM_CORE diff --git a/src/friends.cpp b/src/friends.cpp new file mode 100644 index 0000000..534ba72 --- /dev/null +++ b/src/friends.cpp @@ -0,0 +1,78 @@ +#include "friends.hpp" + +// ============================ +// ======= SteamFriends ======= +// ============================ + +namespace { + +class SteamFriendsListener; +SteamFriendsListener *friends_listener = nullptr; +int friends_ref = LUA_NOREF; + +const char *dialog_types[] = {"friends", "community", "players", "settings", "officialgamegroup", "stats", "achievements", NULL}; + +class SteamFriendsListener { + private: + STEAM_CALLBACK(SteamFriendsListener, OnGameOverlayActivated, GameOverlayActivated_t); +}; + +void SteamFriendsListener::OnGameOverlayActivated(GameOverlayActivated_t *data) { + if (data == nullptr) { + return; + } + lua_State *L = luasteam::global_lua_state; + if (!lua_checkstack(L, 4)) { + return; + } + lua_rawgeti(L, LUA_REGISTRYINDEX, friends_ref); + lua_getfield(L, -1, "onGameOverlayActivated"); + if (lua_isnil(L, -1)) { + lua_pop(L, 2); + } else { + lua_createtable(L, 0, 1); + lua_pushboolean(L, data->m_bActive); + lua_setfield(L, -2, "active"); + lua_call(L, 1, 0); + lua_pop(L, 1); + } +} + +} // namespace + +// void ActivateGameOverlay( const char *pchDialog ); +EXTERN int luasteam_activateGameOverlay(lua_State *L) { + const char *dialog = dialog_types[luaL_checkoption(L, 1, NULL, dialog_types)]; + SteamFriends()->ActivateGameOverlay(dialog); + return 0; +} + +// void ActivateGameOverlayToWebPage( const char *pchURL ); +EXTERN int luasteam_activateGameOverlayToWebPage(lua_State *L) { + const char *url = luaL_checkstring(L, 1); + SteamFriends()->ActivateGameOverlayToWebPage(url); + return 0; +} + +// const char * GetFriendPersonaName( CSteamID steamIDFriend ); +EXTERN int luasteam_getFriendPersonaName(lua_State *L) { + CSteamID id(luasteam::checkuint64(L, 1)); + const char *name = SteamFriends()->GetFriendPersonaName(id); + lua_pushstring(L, name); + return 1; +} + +namespace luasteam { + +void add_friends(lua_State *L) { + lua_createtable(L, 0, 3); + add_func(L, "activateGameOverlay", luasteam_activateGameOverlay); + add_func(L, "activateGameOverlayToWebPage", luasteam_activateGameOverlayToWebPage); + add_func(L, "getFriendPersonaName", luasteam_getFriendPersonaName); + lua_pushvalue(L, -1); + friends_ref = luaL_ref(L, LUA_REGISTRYINDEX); + friends_listener = new SteamFriendsListener(); + lua_setfield(L, -2, "friends"); +} + +} // namespace luasteam diff --git a/src/friends.hpp b/src/friends.hpp new file mode 100644 index 0000000..493de94 --- /dev/null +++ b/src/friends.hpp @@ -0,0 +1,12 @@ +#ifndef LUASTEAM_FRIENDS +#define LUASTEAM_FRIENDS + +#include "common.hpp" + +namespace luasteam { + +void add_friends(lua_State *L); + +} // namespace luasteam + +#endif // LUASTEAM_FRIENDS diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..42c047c --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,17 @@ +#include "common.hpp" +#include "core.hpp" +#include "friends.hpp" +#include "user_stats.hpp" + +// ==================================== +// ======= End of API functions ======= +// ==================================== + +EXTERN int luaopen_luasteam(lua_State *L) { + luasteam::init_common(L); + lua_createtable(L, 0, 5); + luasteam::add_core(L); + luasteam::add_user_stats(L); + luasteam::add_friends(L); + return 1; +} diff --git a/luasteam.cxx b/src/user_stats.cpp similarity index 70% rename from luasteam.cxx rename to src/user_stats.cpp index 991b2c2..46be78c 100644 --- a/luasteam.cxx +++ b/src/user_stats.cpp @@ -1,49 +1,4 @@ -#include "sdk/public/steam/steam_api.h" -#include -#include - -#ifdef _WIN32 -# define EXTERN extern "C" __declspec(dllexport) -#else -# define EXTERN extern "C" -#endif - -extern "C" { -#include -#include -} - -namespace { - -lua_State *global_lua_state = nullptr; -class SteamFriendsListener; -SteamFriendsListener *friends_listener = nullptr; -int friends_ref = LUA_NOREF; -class SteamUserStatsListener; -SteamUserStatsListener *userStats_listener = nullptr; -int userStats_ref = LUA_NOREF; - -int uint64Metatable_ref = LUA_NOREF; - -// using userdata since Lua's number can't safely hold 64-bit integers -inline void pushuint64(lua_State *L, uint64 v) { - uint64 *ptr = reinterpret_cast(lua_newuserdata(L, sizeof(uint64))); - *ptr = v; - lua_rawgeti(L, LUA_REGISTRYINDEX, uint64Metatable_ref); - lua_setmetatable(L, -2); -} - -inline uint64 checkuint64(lua_State *L, int nParam) { - luaL_argcheck(L, lua_isuserdata(L, nParam), nParam, "must be userdata"); - lua_rawgeti(L, LUA_REGISTRYINDEX, uint64Metatable_ref); - lua_getmetatable(L, nParam); - luaL_argcheck(L, lua_rawequal(L, -2, -1), nParam, "must be uint64"); - lua_pop(L, 2); - uint64 *ptr = reinterpret_cast(lua_touserdata(L, nParam)); - return *ptr; -} - -} // namespace +#include "user_stats.hpp" // ============================== // ======= SteamUserStats ======= @@ -51,6 +6,10 @@ inline uint64 checkuint64(lua_State *L, int nParam) { namespace { +class SteamUserStatsListener; +SteamUserStatsListener *userStats_listener = nullptr; +int userStats_ref = LUA_NOREF; + const char *sort_methods[] = {"Ascending", "Descending", nullptr}; const char *display_types[] = {"Numeric", "TimeSeconds", "TimeMilliSeconds", nullptr}; const char *upload_methods[] = {"KeepBest", "ForceUpdate", nullptr}; @@ -78,7 +37,7 @@ class SteamUserStatsListener { }; void SteamUserStatsListener::OnLeaderboardFindResult(LeaderboardFindResult_t *data, bool io_fail) { - lua_State *L = global_lua_state; + lua_State *L = luasteam::global_lua_state; // getting stored callback function lua_rawgeti(L, LUA_REGISTRYINDEX, leaderboardFindResultCallback_ref); luaL_unref(L, LUA_REGISTRYINDEX, leaderboardFindResultCallback_ref); @@ -88,7 +47,7 @@ void SteamUserStatsListener::OnLeaderboardFindResult(LeaderboardFindResult_t *da lua_pushnil(L); } else { lua_createtable(L, 0, 2); - pushuint64(L, data->m_hSteamLeaderboard); + luasteam::pushuint64(L, data->m_hSteamLeaderboard); lua_setfield(L, -2, "steamLeaderboard"); lua_pushboolean(L, data->m_bLeaderboardFound != 0); lua_setfield(L, -2, "leaderboardFound"); @@ -98,7 +57,7 @@ void SteamUserStatsListener::OnLeaderboardFindResult(LeaderboardFindResult_t *da } void SteamUserStatsListener::OnLeaderboardScoreUploaded(LeaderboardScoreUploaded_t *data, bool io_fail) { - lua_State *L = global_lua_state; + lua_State *L = luasteam::global_lua_state; // getting stored callback function lua_rawgeti(L, LUA_REGISTRYINDEX, leaderboardScoreUploadedCallback_ref); luaL_unref(L, LUA_REGISTRYINDEX, leaderboardScoreUploadedCallback_ref); @@ -110,7 +69,7 @@ void SteamUserStatsListener::OnLeaderboardScoreUploaded(LeaderboardScoreUploaded lua_createtable(L, 0, 2); lua_pushboolean(L, data->m_bSuccess != 0); lua_setfield(L, -2, "success"); - pushuint64(L, data->m_hSteamLeaderboard); + luasteam::pushuint64(L, data->m_hSteamLeaderboard); lua_setfield(L, -2, "steamLeaderboard"); lua_pushnumber(L, data->m_nScore); lua_setfield(L, -2, "score"); @@ -126,7 +85,7 @@ void SteamUserStatsListener::OnLeaderboardScoreUploaded(LeaderboardScoreUploaded } void SteamUserStatsListener::OnLeaderboardScoresDownloaded(LeaderboardScoresDownloaded_t *data, bool io_fail) { - lua_State *L = global_lua_state; + lua_State *L = luasteam::global_lua_state; // getting store callback function lua_rawgeti(L, LUA_REGISTRYINDEX, leaderboardScoresDownloadedCallback_ref); luaL_unref(L, LUA_REGISTRYINDEX, leaderboardScoresDownloadedCallback_ref); @@ -143,7 +102,7 @@ void SteamUserStatsListener::OnLeaderboardScoresDownloaded(LeaderboardScoresDown bool success = SteamUserStats()->GetDownloadedLeaderboardEntry(data->m_hSteamLeaderboardEntries, i, &entry, details, k_cLeaderboardDetailsMax); if (success) { lua_createtable(L, 0, 5); - pushuint64(L, entry.m_steamIDUser.ConvertToUint64()); + luasteam::pushuint64(L, entry.m_steamIDUser.ConvertToUint64()); lua_setfield(L, -2, "steamIDUser"); lua_pushnumber(L, entry.m_nGlobalRank); lua_setfield(L, -2, "globalRank"); @@ -151,7 +110,7 @@ void SteamUserStatsListener::OnLeaderboardScoresDownloaded(LeaderboardScoresDown lua_setfield(L, -2, "score"); lua_pushlstring(L, reinterpret_cast(details), entry.m_cDetails * 4); lua_setfield(L, -2, "details"); - pushuint64(L, entry.m_hUGC); + luasteam::pushuint64(L, entry.m_hUGC); lua_setfield(L, -2, "UGC"); lua_rawseti(L, -2, ++count); } else { @@ -168,7 +127,7 @@ void SteamUserStatsListener::OnUserStatsReceived(UserStatsReceived_t *data) { if (data == nullptr) { return; } - lua_State *L = global_lua_state; + lua_State *L = luasteam::global_lua_state; if (!lua_checkstack(L, 4)) { return; } @@ -178,11 +137,11 @@ void SteamUserStatsListener::OnUserStatsReceived(UserStatsReceived_t *data) { lua_pop(L, 2); } else { lua_createtable(L, 0, 3); - pushuint64(L, data->m_nGameID); + luasteam::pushuint64(L, data->m_nGameID); lua_setfield(L, -2, "gameID"); lua_pushnumber(L, static_cast(data->m_eResult)); lua_setfield(L, -2, "result"); - pushuint64(L, data->m_steamIDUser.ConvertToUint64()); + luasteam::pushuint64(L, data->m_steamIDUser.ConvertToUint64()); lua_setfield(L, -2, "steamIDUser"); lua_call(L, 1, 0); lua_pop(L, 1); @@ -193,7 +152,7 @@ void SteamUserStatsListener::OnUserStatsStored(UserStatsStored_t *data) { if (data == nullptr) { return; } - lua_State *L = global_lua_state; + lua_State *L = luasteam::global_lua_state; if (!lua_checkstack(L, 4)) { return; } @@ -203,7 +162,7 @@ void SteamUserStatsListener::OnUserStatsStored(UserStatsStored_t *data) { lua_pop(L, 2); } else { lua_createtable(L, 0, 3); - pushuint64(L, data->m_nGameID); + luasteam::pushuint64(L, data->m_nGameID); lua_setfield(L, -2, "gameID"); lua_pushnumber(L, static_cast(data->m_eResult)); lua_setfield(L, -2, "result"); @@ -216,7 +175,7 @@ void SteamUserStatsListener::OnUserAchievementStored(UserAchievementStored_t *da if (data == nullptr) { return; } - lua_State *L = global_lua_state; + lua_State *L = luasteam::global_lua_state; if (!lua_checkstack(L, 4)) { return; } @@ -226,7 +185,7 @@ void SteamUserStatsListener::OnUserAchievementStored(UserAchievementStored_t *da lua_pop(L, 2); } else { lua_createtable(L, 0, 3); - pushuint64(L, data->m_nGameID); + luasteam::pushuint64(L, data->m_nGameID); lua_setfield(L, -2, "gameID"); lua_pushstring(L, data->m_rgchAchievementName); lua_setfield(L, -2, "achievementName"); @@ -310,7 +269,7 @@ EXTERN int luasteam_findOrCreateLeaderboard(lua_State *L) { // ELeaderboardSortMethod GetLeaderboardSortMethod( SteamLeaderboard_t hSteamLeaderboard ); EXTERN int luasteam_getLeaderboardDisplayType(lua_State *L) { - SteamLeaderboard_t leaderboard = checkuint64(L, 1); + SteamLeaderboard_t leaderboard = luasteam::checkuint64(L, 1); ELeaderboardDisplayType m = SteamUserStats()->GetLeaderboardDisplayType(leaderboard); if (m == k_ELeaderboardDisplayTypeNone) { lua_pushnil(L); @@ -322,7 +281,7 @@ EXTERN int luasteam_getLeaderboardDisplayType(lua_State *L) { // ELeaderboardSortMethod GetLeaderboardSortMethod( SteamLeaderboard_t hSteamLeaderboard ); EXTERN int luasteam_getLeaderboardSortMethod(lua_State *L) { - SteamLeaderboard_t leaderboard = checkuint64(L, 1); + SteamLeaderboard_t leaderboard = luasteam::checkuint64(L, 1); ELeaderboardSortMethod m = SteamUserStats()->GetLeaderboardSortMethod(leaderboard); if (m == k_ELeaderboardSortMethodNone) { lua_pushnil(L); @@ -334,7 +293,7 @@ EXTERN int luasteam_getLeaderboardSortMethod(lua_State *L) { // ELeaderboardSortMethod GetLeaderboardSortMethod( SteamLeaderboard_t hSteamLeaderboard ); EXTERN int luasteam_getLeaderboardEntryCount(lua_State *L) { - SteamLeaderboard_t leaderboard = checkuint64(L, 1); + SteamLeaderboard_t leaderboard = luasteam::checkuint64(L, 1); int count = SteamUserStats()->GetLeaderboardEntryCount(leaderboard); lua_pushnumber(L, count); return 1; @@ -342,7 +301,7 @@ EXTERN int luasteam_getLeaderboardEntryCount(lua_State *L) { // ELeaderboardSortMethod GetLeaderboardSortMethod( SteamLeaderboard_t hSteamLeaderboard ); EXTERN int luasteam_getLeaderboardName(lua_State *L) { - SteamLeaderboard_t leaderboard = checkuint64(L, 1); + SteamLeaderboard_t leaderboard = luasteam::checkuint64(L, 1); const char *name = SteamUserStats()->GetLeaderboardName(leaderboard); if (name == nullptr || *name == '\0') { lua_pushnil(L); @@ -353,7 +312,7 @@ EXTERN int luasteam_getLeaderboardName(lua_State *L) { } EXTERN int luasteam_uploadLeaderboardScore(lua_State *L) { - SteamLeaderboard_t leaderboard = checkuint64(L, 1); + SteamLeaderboard_t leaderboard = luasteam::checkuint64(L, 1); ELeaderboardUploadScoreMethod upload_method = static_cast(luaL_checkoption(L, 2, nullptr, upload_methods) + 1); int32 score = luaL_checkint(L, 3); size_t size; @@ -372,7 +331,7 @@ EXTERN int luasteam_uploadLeaderboardScore(lua_State *L) { } EXTERN int luasteam_downloadLeaderboardEntries(lua_State *L) { - SteamLeaderboard_t handle = checkuint64(L, 1); + SteamLeaderboard_t handle = luasteam::checkuint64(L, 1); ELeaderboardDataRequest data_request = static_cast(luaL_checkoption(L, 2, nullptr, data_requests)); int start = 0, end = 0; if (data_request != k_ELeaderboardDataRequestFriends) { @@ -390,114 +349,7 @@ EXTERN int luasteam_downloadLeaderboardEntries(lua_State *L) { return 0; } -// ============================ -// ======= SteamFriends ======= -// ============================ - -namespace { - -const char *dialog_types[] = {"friends", "community", "players", "settings", "officialgamegroup", "stats", "achievements", NULL}; - -class SteamFriendsListener { - private: - STEAM_CALLBACK(SteamFriendsListener, OnGameOverlayActivated, GameOverlayActivated_t); -}; - -void SteamFriendsListener::OnGameOverlayActivated(GameOverlayActivated_t *data) { - if (data == nullptr) { - return; - } - lua_State *L = global_lua_state; - if (!lua_checkstack(L, 4)) { - return; - } - lua_rawgeti(L, LUA_REGISTRYINDEX, friends_ref); - lua_getfield(L, -1, "onGameOverlayActivated"); - if (lua_isnil(L, -1)) { - lua_pop(L, 2); - } else { - lua_createtable(L, 0, 1); - lua_pushboolean(L, data->m_bActive); - lua_setfield(L, -2, "active"); - lua_call(L, 1, 0); - lua_pop(L, 1); - } -} - -} // namespace - -// void ActivateGameOverlay( const char *pchDialog ); -EXTERN int luasteam_activateGameOverlay(lua_State *L) { - const char *dialog = dialog_types[luaL_checkoption(L, 1, NULL, dialog_types)]; - SteamFriends()->ActivateGameOverlay(dialog); - return 0; -} - -// void ActivateGameOverlayToWebPage( const char *pchURL ); -EXTERN int luasteam_activateGameOverlayToWebPage(lua_State *L) { - const char *url = luaL_checkstring(L, 1); - SteamFriends()->ActivateGameOverlayToWebPage(url); - return 0; -} - -// const char * GetFriendPersonaName( CSteamID steamIDFriend ); -EXTERN int luasteam_getFriendPersonaName(lua_State *L) { - CSteamID id(checkuint64(L, 1)); - const char *name = SteamFriends()->GetFriendPersonaName(id); - lua_pushstring(L, name); - return 1; -} - -// ======================== -// ======= SteamAPI ======= -// ======================== - -// bool SteamAPI_Init(); -EXTERN int luasteam_init(lua_State *L) { - bool success = SteamAPI_Init(); - if (!success) { - fprintf(stderr, "Couldn't connect to steam...\nDo you have Steam turned on?\nIf not running from steam, do you have a correct steam_appid.txt file?\n"); - } - lua_pushboolean(L, success); - return 1; -} - -// void SteamAPI_Shutdown(); -EXTERN int luasteam_shutdown(lua_State *L) { - SteamAPI_Shutdown(); - // Cleaning up - global_lua_state = nullptr; - luaL_unref(L, LUA_REGISTRYINDEX, friends_ref); - friends_ref = LUA_NOREF; - delete friends_listener; - friends_listener = nullptr; - luaL_unref(L, LUA_REGISTRYINDEX, uint64Metatable_ref); - uint64Metatable_ref = LUA_NOREF; - return 0; -} - -// void SteamAPI_RunCallbacks(); -EXTERN int luasteam_runCallbacks(lua_State *L) { - SteamAPI_RunCallbacks(); - return 0; -} - -// ==================================== -// ======= End of API functions ======= -// ==================================== - -namespace { - -void add_func(lua_State *L, const char *name, lua_CFunction func) { - lua_pushcfunction(L, func); - lua_setfield(L, -2, name); -} - -void add_base(lua_State *L) { - add_func(L, "init", luasteam_init); - add_func(L, "shutdown", luasteam_shutdown); - add_func(L, "runCallbacks", luasteam_runCallbacks); -} +namespace luasteam { void add_user_stats(lua_State *L) { lua_createtable(L, 0, 13); @@ -520,26 +372,4 @@ void add_user_stats(lua_State *L) { lua_setfield(L, -2, "userStats"); } -void add_friends(lua_State *L) { - lua_createtable(L, 0, 3); - add_func(L, "activateGameOverlay", luasteam_activateGameOverlay); - add_func(L, "activateGameOverlayToWebPage", luasteam_activateGameOverlayToWebPage); - add_func(L, "getFriendPersonaName", luasteam_getFriendPersonaName); - lua_pushvalue(L, -1); - friends_ref = luaL_ref(L, LUA_REGISTRYINDEX); - friends_listener = new SteamFriendsListener(); - lua_setfield(L, -2, "friends"); -} - -} // namespace - -EXTERN int luaopen_luasteam(lua_State *L) { - global_lua_state = L; - lua_createtable(L, 0, 0); - uint64Metatable_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_createtable(L, 0, 5); - add_base(L); - add_user_stats(L); - add_friends(L); - return 1; -} +} // namespace luasteam diff --git a/src/user_stats.hpp b/src/user_stats.hpp new file mode 100644 index 0000000..171dc33 --- /dev/null +++ b/src/user_stats.hpp @@ -0,0 +1,12 @@ +#ifndef LUASTEAM_USER_STATS +#define LUASTEAM_USER_STATS + +#include "common.hpp" + +namespace luasteam { + +void add_user_stats(lua_State *L); + +} // namespace luasteam + +#endif // LUASTEAM_USER_STATS From 8267fcce73c8ba5b72ae8b5be67170ae31176d4e Mon Sep 17 00:00:00 2001 From: Yan Couto Date: Wed, 19 Dec 2018 19:39:34 -0200 Subject: [PATCH 2/3] Proper init and shutdown Also fixed some problems with init --- src/common.cpp | 12 +++++++++--- src/common.hpp | 1 + src/core.cpp | 18 ++++++++++-------- src/friends.cpp | 10 +++++++++- src/friends.hpp | 3 +++ src/user_stats.cpp | 10 +++++++++- src/user_stats.hpp | 3 +++ 7 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index e35f399..72884a6 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -8,14 +8,14 @@ namespace luasteam { lua_State *global_lua_state = nullptr; -inline void pushuint64(lua_State *L, uint64 v) { +void pushuint64(lua_State *L, uint64 v) { uint64 *ptr = reinterpret_cast(lua_newuserdata(L, sizeof(uint64))); *ptr = v; lua_rawgeti(L, LUA_REGISTRYINDEX, uint64Metatable_ref); lua_setmetatable(L, -2); } -inline uint64 checkuint64(lua_State *L, int nParam) { +uint64 checkuint64(lua_State *L, int nParam) { luaL_argcheck(L, lua_isuserdata(L, nParam), nParam, "must be userdata"); lua_rawgeti(L, LUA_REGISTRYINDEX, uint64Metatable_ref); lua_getmetatable(L, nParam); @@ -25,7 +25,7 @@ inline uint64 checkuint64(lua_State *L, int nParam) { return *ptr; } -inline void add_func(lua_State *L, const char *name, lua_CFunction func) { +void add_func(lua_State *L, const char *name, lua_CFunction func) { lua_pushcfunction(L, func); lua_setfield(L, -2, name); } @@ -36,4 +36,10 @@ void init_common(lua_State *L) { uint64Metatable_ref = luaL_ref(L, LUA_REGISTRYINDEX); } +void shutdown_common(lua_State *L) { + global_lua_state = nullptr; + luaL_unref(L, LUA_REGISTRYINDEX, uint64Metatable_ref); + uint64Metatable_ref = LUA_NOREF; +} + } // namespace luasteam diff --git a/src/common.hpp b/src/common.hpp index 18c73c2..cd7a4a2 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -26,6 +26,7 @@ uint64 checkuint64(lua_State *L, int nParam); void add_func(lua_State *L, const char *name, lua_CFunction func); void init_common(lua_State *L); +void shutdown_common(lua_State *L); } // namespace luasteam diff --git a/src/core.cpp b/src/core.cpp index 65ad3a9..00ed3c2 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -1,4 +1,6 @@ #include "core.hpp" +#include "friends.hpp" +#include "user_stats.hpp" // ======================== // ======= SteamAPI ======= @@ -7,7 +9,11 @@ // bool SteamAPI_Init(); EXTERN int luasteam_init(lua_State *L) { bool success = SteamAPI_Init(); - if (!success) { + if (success) { + luasteam::init_common(L); + luasteam::init_friends(L); + luasteam::init_user_stats(L); + } else { fprintf(stderr, "Couldn't connect to steam...\nDo you have Steam turned on?\nIf not running from steam, do you have a correct steam_appid.txt file?\n"); } lua_pushboolean(L, success); @@ -18,13 +24,9 @@ EXTERN int luasteam_init(lua_State *L) { EXTERN int luasteam_shutdown(lua_State *L) { SteamAPI_Shutdown(); // Cleaning up - // global_lua_state = nullptr; - // luaL_unref(L, LUA_REGISTRYINDEX, friends_ref); - // friends_ref = LUA_NOREF; - // delete friends_listener; - // friends_listener = nullptr; - // luaL_unref(L, LUA_REGISTRYINDEX, uint64Metatable_ref); - // uint64Metatable_ref = LUA_NOREF; + luasteam::shutdown_user_stats(L); + luasteam::shutdown_friends(L); + luasteam::shutdown_common(L); return 0; } diff --git a/src/friends.cpp b/src/friends.cpp index 534ba72..9684496 100644 --- a/src/friends.cpp +++ b/src/friends.cpp @@ -71,8 +71,16 @@ void add_friends(lua_State *L) { add_func(L, "getFriendPersonaName", luasteam_getFriendPersonaName); lua_pushvalue(L, -1); friends_ref = luaL_ref(L, LUA_REGISTRYINDEX); - friends_listener = new SteamFriendsListener(); lua_setfield(L, -2, "friends"); } +void init_friends(lua_State *L) { friends_listener = new SteamFriendsListener(); } + +void shutdown_friends(lua_State *L) { + luaL_unref(L, LUA_REGISTRYINDEX, friends_ref); + friends_ref = LUA_NOREF; + delete friends_listener; + friends_listener = nullptr; +} + } // namespace luasteam diff --git a/src/friends.hpp b/src/friends.hpp index 493de94..f41edc9 100644 --- a/src/friends.hpp +++ b/src/friends.hpp @@ -7,6 +7,9 @@ namespace luasteam { void add_friends(lua_State *L); +void init_friends(lua_State *L); +void shutdown_friends(lua_State *L); + } // namespace luasteam #endif // LUASTEAM_FRIENDS diff --git a/src/user_stats.cpp b/src/user_stats.cpp index 46be78c..f11dc9f 100644 --- a/src/user_stats.cpp +++ b/src/user_stats.cpp @@ -368,8 +368,16 @@ void add_user_stats(lua_State *L) { add_func(L, "downloadLeaderboardEntries", luasteam_downloadLeaderboardEntries); lua_pushvalue(L, -1); userStats_ref = luaL_ref(L, LUA_REGISTRYINDEX); - userStats_listener = new SteamUserStatsListener(); lua_setfield(L, -2, "userStats"); } +void init_user_stats(lua_State *L) { userStats_listener = new SteamUserStatsListener(); } + +void shutdown_user_stats(lua_State *L) { + luaL_unref(L, LUA_REGISTRYINDEX, userStats_ref); + userStats_ref = LUA_NOREF; + delete userStats_listener; + userStats_listener = nullptr; +} + } // namespace luasteam diff --git a/src/user_stats.hpp b/src/user_stats.hpp index 171dc33..d4cf1f7 100644 --- a/src/user_stats.hpp +++ b/src/user_stats.hpp @@ -7,6 +7,9 @@ namespace luasteam { void add_user_stats(lua_State *L); +void init_user_stats(lua_State *L); +void shutdown_user_stats(lua_State *L); + } // namespace luasteam #endif // LUASTEAM_USER_STATS From e29fc270789f01ee9ee6da3e95f83e2c37391500 Mon Sep 17 00:00:00 2001 From: Yan Couto Date: Wed, 19 Dec 2018 19:55:08 -0200 Subject: [PATCH 3/3] Improving comments and fixing double init --- src/core.hpp | 1 + src/friends.hpp | 1 + src/main.cpp | 6 +----- src/user_stats.hpp | 1 + 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/core.hpp b/src/core.hpp index 02a63db..f44013d 100644 --- a/src/core.hpp +++ b/src/core.hpp @@ -5,6 +5,7 @@ namespace luasteam { +// Adds functions SteamAPI_* void add_core(lua_State *L); } // namespace luasteam diff --git a/src/friends.hpp b/src/friends.hpp index f41edc9..836f692 100644 --- a/src/friends.hpp +++ b/src/friends.hpp @@ -5,6 +5,7 @@ namespace luasteam { +// Adds functions from ISteamFriends void add_friends(lua_State *L); void init_friends(lua_State *L); diff --git a/src/main.cpp b/src/main.cpp index 42c047c..12493b6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,12 +3,8 @@ #include "friends.hpp" #include "user_stats.hpp" -// ==================================== -// ======= End of API functions ======= -// ==================================== - +// Creates and returns a table with all functions EXTERN int luaopen_luasteam(lua_State *L) { - luasteam::init_common(L); lua_createtable(L, 0, 5); luasteam::add_core(L); luasteam::add_user_stats(L); diff --git a/src/user_stats.hpp b/src/user_stats.hpp index d4cf1f7..e252c67 100644 --- a/src/user_stats.hpp +++ b/src/user_stats.hpp @@ -5,6 +5,7 @@ namespace luasteam { +// Adds functions from ISteamUserStats void add_user_stats(lua_State *L); void init_user_stats(lua_State *L);