Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #5

Merged
merged 3 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
45 changes: 45 additions & 0 deletions src/common.cpp
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
33 changes: 33 additions & 0 deletions src/common.hpp
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
47 changes: 47 additions & 0 deletions src/core.cpp
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
13 changes: 13 additions & 0 deletions src/core.hpp
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
86 changes: 86 additions & 0 deletions src/friends.cpp
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
16 changes: 16 additions & 0 deletions src/friends.hpp
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
13 changes: 13 additions & 0 deletions src/main.cpp
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;
}
Loading