-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from uspgamedev/refactor
Refactor
- Loading branch information
Showing
10 changed files
with
304 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "common.hpp" | ||
|
||
namespace { | ||
int uint64Metatable_ref = LUA_NOREF; | ||
} // namespace | ||
|
||
namespace luasteam { | ||
|
||
lua_State *global_lua_state = nullptr; | ||
|
||
void pushuint64(lua_State *L, uint64 v) { | ||
uint64 *ptr = reinterpret_cast<uint64 *>(lua_newuserdata(L, sizeof(uint64))); | ||
*ptr = v; | ||
lua_rawgeti(L, LUA_REGISTRYINDEX, uint64Metatable_ref); | ||
lua_setmetatable(L, -2); | ||
} | ||
|
||
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<uint64 *>(lua_touserdata(L, nParam)); | ||
return *ptr; | ||
} | ||
|
||
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); | ||
} | ||
|
||
void shutdown_common(lua_State *L) { | ||
global_lua_state = nullptr; | ||
luaL_unref(L, LUA_REGISTRYINDEX, uint64Metatable_ref); | ||
uint64Metatable_ref = LUA_NOREF; | ||
} | ||
|
||
} // namespace luasteam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef LUASTEAM_COMMON | ||
#define LUASTEAM_COMMON | ||
|
||
#include "../sdk/public/steam/steam_api.h" | ||
extern "C" { | ||
#include <lauxlib.h> | ||
#include <lua.h> | ||
} | ||
|
||
#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); | ||
void shutdown_common(lua_State *L); | ||
|
||
} // namespace luasteam | ||
|
||
#endif // LUASTEAM_COMMON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "core.hpp" | ||
#include "friends.hpp" | ||
#include "user_stats.hpp" | ||
|
||
// ======================== | ||
// ======= SteamAPI ======= | ||
// ======================== | ||
|
||
// bool SteamAPI_Init(); | ||
EXTERN int luasteam_init(lua_State *L) { | ||
bool success = SteamAPI_Init(); | ||
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); | ||
return 1; | ||
} | ||
|
||
// void SteamAPI_Shutdown(); | ||
EXTERN int luasteam_shutdown(lua_State *L) { | ||
SteamAPI_Shutdown(); | ||
// Cleaning up | ||
luasteam::shutdown_user_stats(L); | ||
luasteam::shutdown_friends(L); | ||
luasteam::shutdown_common(L); | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef LUASTEAM_CORE | ||
#define LUASTEAM_CORE | ||
|
||
#include "common.hpp" | ||
|
||
namespace luasteam { | ||
|
||
// Adds functions SteamAPI_* | ||
void add_core(lua_State *L); | ||
|
||
} // namespace luasteam | ||
|
||
#endif // LUASTEAM_CORE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#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); | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef LUASTEAM_FRIENDS | ||
#define LUASTEAM_FRIENDS | ||
|
||
#include "common.hpp" | ||
|
||
namespace luasteam { | ||
|
||
// Adds functions from ISteamFriends | ||
void add_friends(lua_State *L); | ||
|
||
void init_friends(lua_State *L); | ||
void shutdown_friends(lua_State *L); | ||
|
||
} // namespace luasteam | ||
|
||
#endif // LUASTEAM_FRIENDS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "common.hpp" | ||
#include "core.hpp" | ||
#include "friends.hpp" | ||
#include "user_stats.hpp" | ||
|
||
// Creates and returns a table with all functions | ||
EXTERN int luaopen_luasteam(lua_State *L) { | ||
lua_createtable(L, 0, 5); | ||
luasteam::add_core(L); | ||
luasteam::add_user_stats(L); | ||
luasteam::add_friends(L); | ||
return 1; | ||
} |
Oops, something went wrong.