diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/funcs.lua b/funcs.lua index 98d6835..dfd8aa3 100644 --- a/funcs.lua +++ b/funcs.lua @@ -113,6 +113,31 @@ function table.numkeys(table) return count end +function equals(x, y) + if type(x) ~= "table" or type(y) ~= "table" then + return x == y + else + for k in pairs(x) do + if not equals(x[k], y[k]) then return false end + end + for k in pairs(y) do + if not equals(x[k], y[k]) then return false end + end + return true + end +end + +function table.equalvalues(t1, t2) + if table.numkeys(t1) ~= table.numkeys(t2) then + return false + else + for _, v in pairs(t2) do + if not table.contains(t1, v) then return false end + end + return true + end +end + function clamp(x, min, max) if max < min then min, max = max, min diff --git a/libs/discordGameSDK.lua b/libs/discordGameSDK.lua new file mode 100644 index 0000000..1d5fce1 --- /dev/null +++ b/libs/discordGameSDK.lua @@ -0,0 +1,1048 @@ +--[[ +MIT License + +Copyright for portions of mpvcord are held by Wachid Adi Nugroho, 2018 as part of mpv-discordRPC (https://github.com/cniw/mpv-discordRPC). +All other copyright for mpvcord are held by Yuto Takano, 2021. + +Copyright (c) 2018 Wachid Adi Nugroho +Copyright (c) 2021 Yuto Takano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +--]] + +local ffi = require "ffi" + +-- Get the host os to load correct lib +local osname = love.system.getOS() +local libGameSDK = nil + +-- FFI requires the libraries really be files just sitting in the filesystem. It +-- can't load libraries from a .love archive, nor a fused executable on Windows. +-- Merely using love.filesystem.getSource() only works when running LOVE with +-- the game unarchived from command line, like "love .". +-- +-- The code here setting "source" will set the directory where the game was run +-- from, so FFI can load discordRPC. We assume that the discordRPC library's +-- libs directory is in the same directory as the .love archive; if it's +-- missing, it just won't load. +local source = love.filesystem.getSource() +if string.sub(source, -5) == ".love" or love.filesystem.isFused() then + source = love.filesystem.getSourceBaseDirectory() +end +local libName32 = "/libs/discordGameSDK/lib/x86/discord_game_sdk" +local libName64 = "/libs/discordGameSDK/lib/x86_64/discord_game_sdk" + +if osname == "Linux" and jit.arch == "x64" then + libGameSDK = ffi.load(source..libName64..".so") +elseif osname == "OS X" and jit.arch == "x64" then + libGameSDK = ffi.load(source..libName64..".dylib") +elseif osname == "Windows" and (jit.arch == "x86" or jit.arch == "x64") then + -- I would strongly advise never touching this. It was not trivial to get correct. -nightmareci + + ffi.cdef[[ + typedef uint32_t DWORD; + typedef char CHAR; + typedef CHAR *LPSTR; + typedef const CHAR *LPCSTR; + typedef wchar_t WCHAR; + typedef WCHAR *LPWSTR; + typedef LPWSTR PWSTR; + typedef const WCHAR *LPCWSTR; + + static const DWORD CP_UTF8 = 65001; + int32_t MultiByteToWideChar( + DWORD CodePage, + DWORD dwFlags, + LPCSTR lpMultiByteStr, + int32_t cbMultiByte, + LPWSTR lpWideCharStr, + int32_t cchWideChar + ); + + int32_t WideCharToMultiByte( + DWORD CodePage, + DWORD dwFlags, + LPCWSTR lpWideCharStr, + int32_t cchWideChar, + LPSTR lpMultiByteStr, + int32_t cbMultiByte, + void* lpDefaultChar, + void* lpUsedDefaultChar + ); + + DWORD GetShortPathNameW( + LPCWSTR lpszLongPath, + LPWSTR lpszShortPath, + DWORD cchBuffer + ); + ]] + + local originalWideSize = ffi.C.MultiByteToWideChar(ffi.C.CP_UTF8, 0, source, -1, nil, 0) + local originalWide = ffi.new('WCHAR[?]', originalWideSize) + ffi.C.MultiByteToWideChar(ffi.C.CP_UTF8, 0, source, -1, originalWide, originalWideSize) + + local sourceSize = ffi.C.GetShortPathNameW(originalWide, nil, 0) + local sourceWide = ffi.new('WCHAR[?]', sourceSize) + ffi.C.GetShortPathNameW(originalWide, sourceWide, sourceSize) + + local sourceChar = ffi.new('char[?]', sourceSize) + ffi.C.WideCharToMultiByte(ffi.C.CP_UTF8, 0, sourceWide, sourceSize, sourceChar, sourceSize, nil, nil) + + source = ffi.string(sourceChar) + + + if jit.arch == "x86" then + libGameSDK = ffi.load(source..libName32..".dll") + elseif jit.arch == "x64" then + libGameSDK = ffi.load(source..libName64..".dll") + end +else + error(string.format("Discord GameSDK not supported on OS \"%s\", CPU architecture \"%s\"", osname, jit.arch)) +end + +ffi.cdef[[ +void free(void *ptr); +void *malloc(size_t size); +void *memset(void *s, int c, size_t n); + +enum{DISCORD_VERSION = 2}; +enum{DISCORD_APPLICATION_MANAGER_VERSION = 1}; +enum{DISCORD_USER_MANAGER_VERSION = 1}; +enum{DISCORD_IMAGE_MANAGER_VERSION = 1}; +enum{DISCORD_ACTIVITY_MANAGER_VERSION = 1}; +enum{DISCORD_RELATIONSHIP_MANAGER_VERSION = 1}; +enum{DISCORD_LOBBY_MANAGER_VERSION = 1}; +enum{DISCORD_NETWORK_MANAGER_VERSION = 1}; +enum{DISCORD_OVERLAY_MANAGER_VERSION = 1}; +enum{DISCORD_STORAGE_MANAGER_VERSION = 1}; +enum{DISCORD_STORE_MANAGER_VERSION = 1}; +enum{DISCORD_VOICE_MANAGER_VERSION = 1}; +enum{DISCORD_ACHIEVEMENT_MANAGER_VERSION = 1}; + +enum EDiscordResult { + DiscordResult_Ok = 0, + DiscordResult_ServiceUnavailable = 1, + DiscordResult_InvalidVersion = 2, + DiscordResult_LockFailed = 3, + DiscordResult_InternalError = 4, + DiscordResult_InvalidPayload = 5, + DiscordResult_InvalidCommand = 6, + DiscordResult_InvalidPermissions = 7, + DiscordResult_NotFetched = 8, + DiscordResult_NotFound = 9, + DiscordResult_Conflict = 10, + DiscordResult_InvalidSecret = 11, + DiscordResult_InvalidJoinSecret = 12, + DiscordResult_NoEligibleActivity = 13, + DiscordResult_InvalidInvite = 14, + DiscordResult_NotAuthenticated = 15, + DiscordResult_InvalidAccessToken = 16, + DiscordResult_ApplicationMismatch = 17, + DiscordResult_InvalidDataUrl = 18, + DiscordResult_InvalidBase64 = 19, + DiscordResult_NotFiltered = 20, + DiscordResult_LobbyFull = 21, + DiscordResult_InvalidLobbySecret = 22, + DiscordResult_InvalidFilename = 23, + DiscordResult_InvalidFileSize = 24, + DiscordResult_InvalidEntitlement = 25, + DiscordResult_NotInstalled = 26, + DiscordResult_NotRunning = 27, + DiscordResult_InsufficientBuffer = 28, + DiscordResult_PurchaseCanceled = 29, + DiscordResult_InvalidGuild = 30, + DiscordResult_InvalidEvent = 31, + DiscordResult_InvalidChannel = 32, + DiscordResult_InvalidOrigin = 33, + DiscordResult_RateLimited = 34, + DiscordResult_OAuth2Error = 35, + DiscordResult_SelectChannelTimeout = 36, + DiscordResult_GetGuildTimeout = 37, + DiscordResult_SelectVoiceForceRequired = 38, + DiscordResult_CaptureShortcutAlreadyListening = 39, + DiscordResult_UnauthorizedForAchievement = 40, + DiscordResult_InvalidGiftCode = 41, + DiscordResult_PurchaseError = 42, + DiscordResult_TransactionAborted = 43, +}; + +enum EDiscordCreateFlags { + DiscordCreateFlags_Default = 0, + DiscordCreateFlags_NoRequireDiscord = 1, +}; + +enum EDiscordLogLevel { + DiscordLogLevel_Error = 1, + DiscordLogLevel_Warn, + DiscordLogLevel_Info, + DiscordLogLevel_Debug, +}; + +enum EDiscordUserFlag { + DiscordUserFlag_Partner = 2, + DiscordUserFlag_HypeSquadEvents = 4, + DiscordUserFlag_HypeSquadHouse1 = 64, + DiscordUserFlag_HypeSquadHouse2 = 128, + DiscordUserFlag_HypeSquadHouse3 = 256, +}; + +enum EDiscordPremiumType { + DiscordPremiumType_None = 0, + DiscordPremiumType_Tier1 = 1, + DiscordPremiumType_Tier2 = 2, +}; + +enum EDiscordImageType { + DiscordImageType_User, +}; + +enum EDiscordActivityType { + DiscordActivityType_Playing, + DiscordActivityType_Streaming, + DiscordActivityType_Listening, + DiscordActivityType_Watching, +}; + +enum EDiscordActivityActionType { + DiscordActivityActionType_Join = 1, + DiscordActivityActionType_Spectate, +}; + +enum EDiscordActivityJoinRequestReply { + DiscordActivityJoinRequestReply_No, + DiscordActivityJoinRequestReply_Yes, + DiscordActivityJoinRequestReply_Ignore, +}; + +enum EDiscordStatus { + DiscordStatus_Offline = 0, + DiscordStatus_Online = 1, + DiscordStatus_Idle = 2, + DiscordStatus_DoNotDisturb = 3, +}; + +enum EDiscordRelationshipType { + DiscordRelationshipType_None, + DiscordRelationshipType_Friend, + DiscordRelationshipType_Blocked, + DiscordRelationshipType_PendingIncoming, + DiscordRelationshipType_PendingOutgoing, + DiscordRelationshipType_Implicit, +}; + +enum EDiscordLobbyType { + DiscordLobbyType_Private = 1, + DiscordLobbyType_Public, +}; + +enum EDiscordLobbySearchComparison { + DiscordLobbySearchComparison_LessThanOrEqual = -2, + DiscordLobbySearchComparison_LessThan, + DiscordLobbySearchComparison_Equal, + DiscordLobbySearchComparison_GreaterThan, + DiscordLobbySearchComparison_GreaterThanOrEqual, + DiscordLobbySearchComparison_NotEqual, +}; + +enum EDiscordLobbySearchCast { + DiscordLobbySearchCast_String = 1, + DiscordLobbySearchCast_Number, +}; + +enum EDiscordLobbySearchDistance { + DiscordLobbySearchDistance_Local, + DiscordLobbySearchDistance_Default, + DiscordLobbySearchDistance_Extended, + DiscordLobbySearchDistance_Global, +}; + +enum EDiscordEntitlementType { + DiscordEntitlementType_Purchase = 1, + DiscordEntitlementType_PremiumSubscription, + DiscordEntitlementType_DeveloperGift, + DiscordEntitlementType_TestModePurchase, + DiscordEntitlementType_FreePurchase, + DiscordEntitlementType_UserGift, + DiscordEntitlementType_PremiumPurchase, +}; + +enum EDiscordSkuType { + DiscordSkuType_Application = 1, + DiscordSkuType_DLC, + DiscordSkuType_Consumable, + DiscordSkuType_Bundle, +}; + +enum EDiscordInputModeType { + DiscordInputModeType_VoiceActivity = 0, + DiscordInputModeType_PushToTalk, +}; + +typedef int64_t DiscordClientId; +typedef int32_t DiscordVersion; +typedef int64_t DiscordSnowflake; +typedef int64_t DiscordTimestamp; +typedef DiscordSnowflake DiscordUserId; +typedef char DiscordLocale[128]; +typedef char DiscordBranch[4096]; +typedef DiscordSnowflake DiscordLobbyId; +typedef char DiscordLobbySecret[128]; +typedef char DiscordMetadataKey[256]; +typedef char DiscordMetadataValue[4096]; +typedef uint64_t DiscordNetworkPeerId; +typedef uint8_t DiscordNetworkChannelId; +typedef char DiscordPath[4096]; +typedef char DiscordDateTime[64]; + +struct DiscordUser { + DiscordUserId id; + char username[256]; + char discriminator[8]; + char avatar[128]; + bool bot; +}; + +struct DiscordOAuth2Token { + char access_token[128]; + char scopes[1024]; + DiscordTimestamp expires; +}; + +struct DiscordImageHandle { + enum EDiscordImageType type; + int64_t id; + uint32_t size; +}; + +struct DiscordImageDimensions { + uint32_t width; + uint32_t height; +}; + + +struct DiscordActivityTimestamps { + DiscordTimestamp start; + DiscordTimestamp end; +}; + +struct DiscordActivityAssets { + char large_image[128]; + char large_text[128]; + char small_image[128]; + char small_text[128]; +}; + +struct DiscordPartySize { + int32_t current_size; + int32_t max_size; +}; + +struct DiscordActivityParty { + char id[128]; + struct DiscordPartySize size; +}; + +struct DiscordActivitySecrets { + char match[128]; + char join[128]; + char spectate[128]; +}; + +struct DiscordActivity { + enum EDiscordActivityType type; + int64_t application_id; + char name[128]; + char state[128]; + char details[128]; + struct DiscordActivityTimestamps timestamps; + struct DiscordActivityAssets assets; + struct DiscordActivityParty party; + struct DiscordActivitySecrets secrets; + bool instance; +}; + +struct DiscordPresence { + enum EDiscordStatus status; + struct DiscordActivity activity; +}; + +struct DiscordRelationship { + enum EDiscordRelationshipType type; + struct DiscordUser user; + struct DiscordPresence presence; +}; + +struct DiscordLobby { + DiscordLobbyId id; + enum EDiscordLobbyType type; + DiscordUserId owner_id; + DiscordLobbySecret secret; + uint32_t capacity; + bool locked; +}; + +struct DiscordFileStat { + const char filename[260]; + uint64_t size; + uint64_t last_modified; +}; + +struct DiscordEntitlement { + DiscordSnowflake id; + enum EDiscordEntitlementType type; + DiscordSnowflake sku_id; +}; + +struct DiscordSkuPrice { + uint32_t amount; + const char currency[16]; +}; + +struct DiscordSku { + DiscordSnowflake id; + enum EDiscordSkuType type; + const char name[256]; + struct DiscordSkuPrice price; +}; + +struct DiscordInputMode { + enum EDiscordInputModeType type; + const char shortcut[256]; +}; + +struct DiscordUserAchievement { + DiscordSnowflake user_id; + DiscordSnowflake achievement_id; + uint8_t percent_complete; + DiscordDateTime unlocked_at; +}; + +struct IDiscordLobbyTransaction { + enum EDiscordResult (*set_type)(struct IDiscordLobbyTransaction* lobby_transaction, enum EDiscordLobbyType type); + enum EDiscordResult (*set_owner)(struct IDiscordLobbyTransaction* lobby_transaction, DiscordUserId owner_id); + enum EDiscordResult (*set_capacity)(struct IDiscordLobbyTransaction* lobby_transaction, uint32_t capacity); + enum EDiscordResult (*set_metadata)(struct IDiscordLobbyTransaction* lobby_transaction, DiscordMetadataKey key, DiscordMetadataValue value); + enum EDiscordResult (*delete_metadata)(struct IDiscordLobbyTransaction* lobby_transaction, DiscordMetadataKey key); + enum EDiscordResult (*set_locked)(struct IDiscordLobbyTransaction* lobby_transaction, bool locked); +}; + +struct IDiscordLobbyMemberTransaction { + enum EDiscordResult (*set_metadata)(struct IDiscordLobbyMemberTransaction* lobby_member_transaction, DiscordMetadataKey key, DiscordMetadataValue value); + enum EDiscordResult (*delete_metadata)(struct IDiscordLobbyMemberTransaction* lobby_member_transaction, DiscordMetadataKey key); +}; + +struct IDiscordLobbySearchQuery { + enum EDiscordResult (*filter)(struct IDiscordLobbySearchQuery* lobby_search_query, DiscordMetadataKey key, enum EDiscordLobbySearchComparison comparison, enum EDiscordLobbySearchCast cast, DiscordMetadataValue value); + enum EDiscordResult (*sort)(struct IDiscordLobbySearchQuery* lobby_search_query, DiscordMetadataKey key, enum EDiscordLobbySearchCast cast, DiscordMetadataValue value); + enum EDiscordResult (*limit)(struct IDiscordLobbySearchQuery* lobby_search_query, uint32_t limit); + enum EDiscordResult (*distance)(struct IDiscordLobbySearchQuery* lobby_search_query, enum EDiscordLobbySearchDistance distance); +}; + +typedef void* IDiscordApplicationEvents; + +struct IDiscordApplicationManager { + void (*validate_or_exit)(struct IDiscordApplicationManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*get_current_locale)(struct IDiscordApplicationManager* manager, DiscordLocale* locale); + void (*get_current_branch)(struct IDiscordApplicationManager* manager, DiscordBranch* branch); + void (*get_oauth2_token)(struct IDiscordApplicationManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordOAuth2Token* oauth2_token)); + void (*get_ticket)(struct IDiscordApplicationManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, const char* data)); +}; + +struct IDiscordUserEvents { + void (*on_current_user_update)(void* event_data); +}; + +struct IDiscordUserManager { + enum EDiscordResult (*get_current_user)(struct IDiscordUserManager* manager, struct DiscordUser* current_user); + void (*get_user)(struct IDiscordUserManager* manager, DiscordUserId user_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordUser* user)); + enum EDiscordResult (*get_current_user_premium_type)(struct IDiscordUserManager* manager, enum EDiscordPremiumType* premium_type); + enum EDiscordResult (*current_user_has_flag)(struct IDiscordUserManager* manager, enum EDiscordUserFlag flag, bool* has_flag); +}; + +typedef void* IDiscordImageEvents; + +struct IDiscordImageManager { + void (*fetch)(struct IDiscordImageManager* manager, struct DiscordImageHandle handle, bool refresh, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordImageHandle handle_result)); + enum EDiscordResult (*get_dimensions)(struct IDiscordImageManager* manager, struct DiscordImageHandle handle, struct DiscordImageDimensions* dimensions); + enum EDiscordResult (*get_data)(struct IDiscordImageManager* manager, struct DiscordImageHandle handle, uint8_t* data, uint32_t data_length); +}; + +struct IDiscordActivityEvents { + void (*on_activity_join)(void* event_data, const char* secret); + void (*on_activity_spectate)(void* event_data, const char* secret); + void (*on_activity_join_request)(void* event_data, struct DiscordUser* user); + void (*on_activity_invite)(void* event_data, enum EDiscordActivityActionType type, struct DiscordUser* user, struct DiscordActivity* activity); +}; + +struct IDiscordActivityManager { + enum EDiscordResult (*register_command)(struct IDiscordActivityManager* manager, const char* command); + enum EDiscordResult (*register_steam)(struct IDiscordActivityManager* manager, uint32_t steam_id); + void (*update_activity)(struct IDiscordActivityManager* manager, struct DiscordActivity* activity, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*clear_activity)(struct IDiscordActivityManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*send_request_reply)(struct IDiscordActivityManager* manager, DiscordUserId user_id, enum EDiscordActivityJoinRequestReply reply, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*send_invite)(struct IDiscordActivityManager* manager, DiscordUserId user_id, enum EDiscordActivityActionType type, const char* content, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*accept_invite)(struct IDiscordActivityManager* manager, DiscordUserId user_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); +}; + +struct IDiscordRelationshipEvents { + void (*on_refresh)(void* event_data); + void (*on_relationship_update)(void* event_data, struct DiscordRelationship* relationship); +}; + +struct IDiscordRelationshipManager { + void (*filter)(struct IDiscordRelationshipManager* manager, void* filter_data, bool (*filter)(void* filter_data, struct DiscordRelationship* relationship)); + enum EDiscordResult (*count)(struct IDiscordRelationshipManager* manager, int32_t* count); + enum EDiscordResult (*get)(struct IDiscordRelationshipManager* manager, DiscordUserId user_id, struct DiscordRelationship* relationship); + enum EDiscordResult (*get_at)(struct IDiscordRelationshipManager* manager, uint32_t index, struct DiscordRelationship* relationship); +}; + +struct IDiscordLobbyEvents { + void (*on_lobby_update)(void* event_data, int64_t lobby_id); + void (*on_lobby_delete)(void* event_data, int64_t lobby_id, uint32_t reason); + void (*on_member_connect)(void* event_data, int64_t lobby_id, int64_t user_id); + void (*on_member_update)(void* event_data, int64_t lobby_id, int64_t user_id); + void (*on_member_disconnect)(void* event_data, int64_t lobby_id, int64_t user_id); + void (*on_lobby_message)(void* event_data, int64_t lobby_id, int64_t user_id, uint8_t* data, uint32_t data_length); + void (*on_speaking)(void* event_data, int64_t lobby_id, int64_t user_id, bool speaking); + void (*on_network_message)(void* event_data, int64_t lobby_id, int64_t user_id, uint8_t channel_id, uint8_t* data, uint32_t data_length); +}; + +struct IDiscordLobbyManager { + enum EDiscordResult (*get_lobby_create_transaction)(struct IDiscordLobbyManager* manager, struct IDiscordLobbyTransaction** transaction); + enum EDiscordResult (*get_lobby_update_transaction)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, struct IDiscordLobbyTransaction** transaction); + enum EDiscordResult (*get_member_update_transaction)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, struct IDiscordLobbyMemberTransaction** transaction); + void (*create_lobby)(struct IDiscordLobbyManager* manager, struct IDiscordLobbyTransaction* transaction, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordLobby* lobby)); + void (*update_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, struct IDiscordLobbyTransaction* transaction, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*delete_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*connect_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordLobbySecret secret, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordLobby* lobby)); + void (*connect_lobby_with_activity_secret)(struct IDiscordLobbyManager* manager, DiscordLobbySecret activity_secret, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordLobby* lobby)); + void (*disconnect_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*get_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, struct DiscordLobby* lobby); + enum EDiscordResult (*get_lobby_activity_secret)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordLobbySecret* secret); + enum EDiscordResult (*get_lobby_metadata_value)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordMetadataKey key, DiscordMetadataValue* value); + enum EDiscordResult (*get_lobby_metadata_key)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, int32_t index, DiscordMetadataKey* key); + enum EDiscordResult (*lobby_metadata_count)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, int32_t* count); + enum EDiscordResult (*member_count)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, int32_t* count); + enum EDiscordResult (*get_member_user_id)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, int32_t index, DiscordUserId* user_id); + enum EDiscordResult (*get_member_user)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, struct DiscordUser* user); + enum EDiscordResult (*get_member_metadata_value)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, DiscordMetadataKey key, DiscordMetadataValue* value); + enum EDiscordResult (*get_member_metadata_key)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, int32_t index, DiscordMetadataKey* key); + enum EDiscordResult (*member_metadata_count)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, int32_t* count); + void (*update_member)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, struct IDiscordLobbyMemberTransaction* transaction, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*send_lobby_message)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, uint8_t* data, uint32_t data_length, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*get_search_query)(struct IDiscordLobbyManager* manager, struct IDiscordLobbySearchQuery** query); + void (*search)(struct IDiscordLobbyManager* manager, struct IDiscordLobbySearchQuery* query, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*lobby_count)(struct IDiscordLobbyManager* manager, int32_t* count); + enum EDiscordResult (*get_lobby_id)(struct IDiscordLobbyManager* manager, int32_t index, DiscordLobbyId* lobby_id); + void (*connect_voice)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*disconnect_voice)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*connect_network)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id); + enum EDiscordResult (*disconnect_network)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id); + enum EDiscordResult (*flush_network)(struct IDiscordLobbyManager* manager); + enum EDiscordResult (*open_network_channel)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, uint8_t channel_id, bool reliable); + enum EDiscordResult (*send_network_message)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, uint8_t channel_id, uint8_t* data, uint32_t data_length); +}; + +struct IDiscordNetworkEvents { + void (*on_message)(void* event_data, DiscordNetworkPeerId peer_id, DiscordNetworkChannelId channel_id, uint8_t* data, uint32_t data_length); + void (*on_route_update)(void* event_data, const char* route_data); +}; + +struct IDiscordNetworkManager { + /** + * Get the local peer ID for this process. + */ + void (*get_peer_id)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId* peer_id); + /** + * Send pending network messages. + */ + enum EDiscordResult (*flush)(struct IDiscordNetworkManager* manager); + /** + * Open a connection to a remote peer. + */ + enum EDiscordResult (*open_peer)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, const char* route_data); + /** + * Update the route data for a connected peer. + */ + enum EDiscordResult (*update_peer)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, const char* route_data); + /** + * Close the connection to a remote peer. + */ + enum EDiscordResult (*close_peer)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id); + /** + * Open a message channel to a connected peer. + */ + enum EDiscordResult (*open_channel)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, DiscordNetworkChannelId channel_id, bool reliable); + /** + * Close a message channel to a connected peer. + */ + enum EDiscordResult (*close_channel)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, DiscordNetworkChannelId channel_id); + /** + * Send a message to a connected peer over an opened message channel. + */ + enum EDiscordResult (*send_message)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, DiscordNetworkChannelId channel_id, uint8_t* data, uint32_t data_length); +}; + +struct IDiscordOverlayEvents { + void (*on_toggle)(void* event_data, bool locked); +}; + +struct IDiscordOverlayManager { + void (*is_enabled)(struct IDiscordOverlayManager* manager, bool* enabled); + void (*is_locked)(struct IDiscordOverlayManager* manager, bool* locked); + void (*set_locked)(struct IDiscordOverlayManager* manager, bool locked, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*open_activity_invite)(struct IDiscordOverlayManager* manager, enum EDiscordActivityActionType type, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*open_guild_invite)(struct IDiscordOverlayManager* manager, const char* code, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*open_voice_settings)(struct IDiscordOverlayManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); +}; + +typedef void* IDiscordStorageEvents; + +struct IDiscordStorageManager { + enum EDiscordResult (*read)(struct IDiscordStorageManager* manager, const char* name, uint8_t* data, uint32_t data_length, uint32_t* read); + void (*read_async)(struct IDiscordStorageManager* manager, const char* name, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, uint8_t* data, uint32_t data_length)); + void (*read_async_partial)(struct IDiscordStorageManager* manager, const char* name, uint64_t offset, uint64_t length, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, uint8_t* data, uint32_t data_length)); + enum EDiscordResult (*write)(struct IDiscordStorageManager* manager, const char* name, uint8_t* data, uint32_t data_length); + void (*write_async)(struct IDiscordStorageManager* manager, const char* name, uint8_t* data, uint32_t data_length, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*delete_)(struct IDiscordStorageManager* manager, const char* name); + enum EDiscordResult (*exists)(struct IDiscordStorageManager* manager, const char* name, bool* exists); + void (*count)(struct IDiscordStorageManager* manager, int32_t* count); + enum EDiscordResult (*stat)(struct IDiscordStorageManager* manager, const char* name, struct DiscordFileStat* stat); + enum EDiscordResult (*stat_at)(struct IDiscordStorageManager* manager, int32_t index, struct DiscordFileStat* stat); + enum EDiscordResult (*get_path)(struct IDiscordStorageManager* manager, DiscordPath* path); +}; + +struct IDiscordStoreEvents { + void (*on_entitlement_create)(void* event_data, struct DiscordEntitlement* entitlement); + void (*on_entitlement_delete)(void* event_data, struct DiscordEntitlement* entitlement); +}; + +struct IDiscordStoreManager { + void (*fetch_skus)(struct IDiscordStoreManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*count_skus)(struct IDiscordStoreManager* manager, int32_t* count); + enum EDiscordResult (*get_sku)(struct IDiscordStoreManager* manager, DiscordSnowflake sku_id, struct DiscordSku* sku); + enum EDiscordResult (*get_sku_at)(struct IDiscordStoreManager* manager, int32_t index, struct DiscordSku* sku); + void (*fetch_entitlements)(struct IDiscordStoreManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*count_entitlements)(struct IDiscordStoreManager* manager, int32_t* count); + enum EDiscordResult (*get_entitlement)(struct IDiscordStoreManager* manager, DiscordSnowflake entitlement_id, struct DiscordEntitlement* entitlement); + enum EDiscordResult (*get_entitlement_at)(struct IDiscordStoreManager* manager, int32_t index, struct DiscordEntitlement* entitlement); + enum EDiscordResult (*has_sku_entitlement)(struct IDiscordStoreManager* manager, DiscordSnowflake sku_id, bool* has_entitlement); + void (*start_purchase)(struct IDiscordStoreManager* manager, DiscordSnowflake sku_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); +}; + +struct IDiscordVoiceEvents { + void (*on_settings_update)(void* event_data); +}; + +struct IDiscordVoiceManager { + enum EDiscordResult (*get_input_mode)(struct IDiscordVoiceManager* manager, struct DiscordInputMode* input_mode); + void (*set_input_mode)(struct IDiscordVoiceManager* manager, struct DiscordInputMode input_mode, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*is_self_mute)(struct IDiscordVoiceManager* manager, bool* mute); + enum EDiscordResult (*set_self_mute)(struct IDiscordVoiceManager* manager, bool mute); + enum EDiscordResult (*is_self_deaf)(struct IDiscordVoiceManager* manager, bool* deaf); + enum EDiscordResult (*set_self_deaf)(struct IDiscordVoiceManager* manager, bool deaf); + enum EDiscordResult (*is_local_mute)(struct IDiscordVoiceManager* manager, DiscordSnowflake user_id, bool* mute); + enum EDiscordResult (*set_local_mute)(struct IDiscordVoiceManager* manager, DiscordSnowflake user_id, bool mute); + enum EDiscordResult (*get_local_volume)(struct IDiscordVoiceManager* manager, DiscordSnowflake user_id, uint8_t* volume); + enum EDiscordResult (*set_local_volume)(struct IDiscordVoiceManager* manager, DiscordSnowflake user_id, uint8_t volume); +}; + +struct IDiscordAchievementEvents { + void (*on_user_achievement_update)(void* event_data, struct DiscordUserAchievement* user_achievement); +}; + +struct IDiscordAchievementManager { + void (*set_user_achievement)(struct IDiscordAchievementManager* manager, DiscordSnowflake achievement_id, uint8_t percent_complete, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*fetch_user_achievements)(struct IDiscordAchievementManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*count_user_achievements)(struct IDiscordAchievementManager* manager, int32_t* count); + enum EDiscordResult (*get_user_achievement)(struct IDiscordAchievementManager* manager, DiscordSnowflake user_achievement_id, struct DiscordUserAchievement* user_achievement); + enum EDiscordResult (*get_user_achievement_at)(struct IDiscordAchievementManager* manager, int32_t index, struct DiscordUserAchievement* user_achievement); +}; + +typedef void* IDiscordCoreEvents; + +struct IDiscordCore { + void (*destroy)(struct IDiscordCore* core); + enum EDiscordResult (*run_callbacks)(struct IDiscordCore* core); + void (*set_log_hook)(struct IDiscordCore* core, enum EDiscordLogLevel min_level, void* hook_data, void (*hook)(void* hook_data, enum EDiscordLogLevel level, const char* message)); + struct IDiscordApplicationManager* (*get_application_manager)(struct IDiscordCore* core); + struct IDiscordUserManager* (*get_user_manager)(struct IDiscordCore* core); + struct IDiscordImageManager* (*get_image_manager)(struct IDiscordCore* core); + struct IDiscordActivityManager* (*get_activity_manager)(struct IDiscordCore* core); + struct IDiscordRelationshipManager* (*get_relationship_manager)(struct IDiscordCore* core); + struct IDiscordLobbyManager* (*get_lobby_manager)(struct IDiscordCore* core); + struct IDiscordNetworkManager* (*get_network_manager)(struct IDiscordCore* core); + struct IDiscordOverlayManager* (*get_overlay_manager)(struct IDiscordCore* core); + struct IDiscordStorageManager* (*get_storage_manager)(struct IDiscordCore* core); + struct IDiscordStoreManager* (*get_store_manager)(struct IDiscordCore* core); + struct IDiscordVoiceManager* (*get_voice_manager)(struct IDiscordCore* core); + struct IDiscordAchievementManager* (*get_achievement_manager)(struct IDiscordCore* core); +}; + +struct DiscordCreateParams { + DiscordClientId client_id; + uint64_t flags; + IDiscordCoreEvents* events; + void* event_data; + IDiscordApplicationEvents* application_events; + DiscordVersion application_version; + struct IDiscordUserEvents* user_events; + DiscordVersion user_version; + IDiscordImageEvents* image_events; + DiscordVersion image_version; + struct IDiscordActivityEvents* activity_events; + DiscordVersion activity_version; + struct IDiscordRelationshipEvents* relationship_events; + DiscordVersion relationship_version; + struct IDiscordLobbyEvents* lobby_events; + DiscordVersion lobby_version; + struct IDiscordNetworkEvents* network_events; + DiscordVersion network_version; + struct IDiscordOverlayEvents* overlay_events; + DiscordVersion overlay_version; + IDiscordStorageEvents* storage_events; + DiscordVersion storage_version; + struct IDiscordStoreEvents* store_events; + DiscordVersion store_version; + struct IDiscordVoiceEvents* voice_events; + DiscordVersion voice_version; + struct IDiscordAchievementEvents* achievement_events; + DiscordVersion achievement_version; +}; + +static void* DiscordCreateParamsSetDefault(struct DiscordCreateParams* params); + +enum EDiscordResult DiscordCreate(DiscordVersion version, struct DiscordCreateParams* params, struct IDiscordCore** result); + +typedef void (*callbackPtr)(void* callback_data, enum EDiscordResult result); + +typedef void (*loggerPtr)(void* hook_data, enum EDiscordLogLevel level, const char* message); + +typedef void (*onUserUpdatedPtr)(void* data); + +typedef void (*onOAuth2Ptr)(void* data, enum EDiscordResult result, struct DiscordOAuth2Token* token); + +struct Application { + struct IDiscordCore* core; + struct IDiscordUserManager* users; + struct IDiscordAchievementManager* achievements; + struct IDiscordActivityManager* activities; + struct IDiscordRelationshipManager* relationships; + struct IDiscordApplicationManager* application; + struct IDiscordLobbyManager* lobbies; + DiscordUserId user_id; +}; +]] + +-- Implement set default function in Lua because +-- somehow I couldn't get FFI to call the static method +-- DiscordCreateParamsSetDefault +local function create_params_set_default(paramsPtr) + paramsPtr[0].application_version = libGameSDK.DISCORD_APPLICATION_MANAGER_VERSION + paramsPtr[0].user_version = libGameSDK.DISCORD_USER_MANAGER_VERSION + paramsPtr[0].image_version = libGameSDK.DISCORD_IMAGE_MANAGER_VERSION + paramsPtr[0].activity_version = libGameSDK.DISCORD_ACTIVITY_MANAGER_VERSION + paramsPtr[0].relationship_version = libGameSDK.DISCORD_RELATIONSHIP_MANAGER_VERSION + paramsPtr[0].lobby_version = libGameSDK.DISCORD_LOBBY_MANAGER_VERSION + paramsPtr[0].network_version = libGameSDK.DISCORD_NETWORK_MANAGER_VERSION + paramsPtr[0].overlay_version = libGameSDK.DISCORD_OVERLAY_MANAGER_VERSION + paramsPtr[0].storage_version = libGameSDK.DISCORD_STORAGE_MANAGER_VERSION + paramsPtr[0].store_version = libGameSDK.DISCORD_STORE_MANAGER_VERSION + paramsPtr[0].voice_version = libGameSDK.DISCORD_VOICE_MANAGER_VERSION + paramsPtr[0].achievement_version = libGameSDK.DISCORD_ACHIEVEMENT_MANAGER_VERSION + return params +end + +local discordGameSDK = {} -- module table + +-- proxy to detect garbage collection of the module +discordGameSDK.gcDummy = newproxy(true) + +local on_user_updated = ffi.cast("onUserUpdatedPtr", function(data) + local appPtr = ffi.cast("struct Application*", data) + local app = appPtr[0] + local user = ffi.new("struct DiscordUser") + local userPtr = ffi.new("struct DiscordUser[1]", user) + app.users.get_current_user(app.users, userPtr) + user = userPtr[0] + print(string.format("Displaying Discord Status on user: %s#%s", + ffi.string(user.username), + ffi.string(user.discriminator))) +end) + +local loggerCallback = ffi.cast("loggerPtr", function (data, level, message) + print(string.format("Discord reported an error of severity %s: %s", + tostring(level), + ffi.string(message))) +end) + +-- Helper function to make sure the input is a given type +function checkArg(arg, argType, argName, func, maybeNil) + assert( + type(arg) == argType or (maybeNil and arg == nil), + string.format("Argument \"%s\" to function \"%s\" has to be of type \"%s\"", + argName, + func, + argType) + ) +end + +-- Helper function to make sure the input is a string within length +function checkStrArg(arg, maxLen, argName, func, maybeNil) + if maxLen then + assert( + type(arg) == "string" and arg:len() <= maxLen or (maybeNil and arg == nil), + string.format("Argument \"%s\" of function \"%s\" has to be of type string with maximum length %d", + argName, + func, + maxLen) + ) + else + checkArg(arg, "string", argName, func, true) + end +end + +-- Helper function to make sure the input is within a max int value +function checkIntArg(arg, maxBits, argName, func, maybeNil) + maxBits = math.min(maxBits or 32, 52) -- lua number (double) can only store integers < 2^53 + local maxVal = 2^(maxBits-1) -- assuming signed integers, which, for now, are the only ones in use + assert( + type(arg) == "number" and math.floor(arg) == arg + and arg < maxVal and arg >= -maxVal + or (maybeNil and arg == nil), + string.format("Argument \"%s\" of function \"%s\" has to be a whole number <= %d", + argName, + func, + maxVal) + ) +end + +-- This function is basically a complete line by line port +-- from examples/c/main.c in the Game SDK. with Lua quirks +-- here and there. + +-- Pass by reference kinda works but kinda doesn't, and +-- so user_events != userEventsPtr[0], and app != appPtr[0]. +-- The desired one is always [0], so we simply do not use +-- the original objects after a pointer to them have been made. +function discordGameSDK.initialize(clientId) + local app = ffi.new("struct Application") + local appPtr = ffi.new("struct Application[1]", app) + ffi.C.memset(appPtr, 0, ffi.sizeof(app)) + + local user_events = ffi.new("struct IDiscordUserEvents") + local userEventsPtr = ffi.new("struct IDiscordUserEvents[1]", user_events) + ffi.C.memset(userEventsPtr, 0, ffi.sizeof(user_events)) + + userEventsPtr[0].on_current_user_update = on_user_updated + + local params = ffi.new("struct DiscordCreateParams") + local paramsPtr = ffi.new("struct DiscordCreateParams[1]", params) + ffi.C.memset(paramsPtr, 0, ffi.sizeof(params)) + create_params_set_default(paramsPtr) + paramsPtr[0].client_id = clientId + paramsPtr[0].flags = libGameSDK.DiscordCreateFlags_NoRequireDiscord + paramsPtr[0].event_data = appPtr + paramsPtr[0].user_events = userEventsPtr + + local corePtrPtr = ffi.new("struct IDiscordCore*[1]", app.core) + ffi.C.memset(corePtrPtr, 0, ffi.sizeof(app.core)) + + -- Attempt to create a Discord instance + x = libGameSDK.DiscordCreate(libGameSDK.DISCORD_VERSION, paramsPtr, corePtrPtr) + running = x == libGameSDK.DiscordResult_Ok + + if running then + appPtr[0].core = corePtrPtr[0] + + appPtr[0].core:set_log_hook(libGameSDK.DiscordLogLevel_Debug, appPtr, loggerCallback) + + appPtr[0].activities = appPtr[0].core[0]:get_activity_manager() + appPtr[0].application = appPtr[0].core[0]:get_application_manager() + appPtr[0].users = appPtr[0].core[0]:get_user_manager() + + discordGameSDK.runCallbacks(appPtr[0].core) + end + + -- By http://lua-users.org/lists/lua-l/2011-04/msg00516.html, + -- The LuaJIT FFI Garbage Collector doesn't follow pointers when + -- determining which references to keep. Therefore, "you must + -- not store the only reference to an object allocated with + -- ffi.new() as a pointer in a struct field." + -- Hence, a referencesTable is passed around instead of just + -- the app, although none of the keys are used apart from app. + local referencesTable = { + running = running, + clientId = clientId, + app = appPtr[0], + appPtr = appPtr, + userEvents = userEvents, + userEventsPtr = userEventsPtr, + corePtr = appPtr[0].core, + corePtrPtr = corePtrPtr, + activities = appPtr[0].activities, + application = appPtr[0].application, + users = appPtr[0].users + } + + -- Prevent memory leak when called multiple times by explicitly + -- calling the garbage collector (twice, because some SO user said so) + collectgarbage() + collectgarbage() + + return referencesTable + +end + +function discordGameSDK.shutdown(app) + app.core:destroy() +end + +function discordGameSDK.runCallbacks(core) + return core.run_callbacks(core) +end + +local updateActivityCallback = ffi.cast("callbackPtr", function(callback_data, discord_result) + if discord_result == libGameSDK.DiscordResult_Ok then + print("Successfully updated Discord activity") + else + print("Failed updating Discord activity: " .. tostring(discord_result)) + end +end) + +local clearActivityCallback = ffi.cast("callbackPtr", function(callback_data, discord_result) + if discord_result == libGameSDK.DiscordResult_Ok then + print("Successfully cleared Discord activity") + else + print("Failed clearing Discord activity: " .. tostring(discord_result)) + end +end) + +function discordGameSDK.updatePresence(referencesTable, presence) + -- If Discord isn't running, try initialising. + -- If it still fails, then return early. + if not referencesTable.running then + referencesTable = discordGameSDK.initialize(referencesTable.clientId) + if not referencesTable.running then + return referencesTable + end + end + + local func = "discordGameSDK.updatePresence" + + checkArg(presence, "table", "presence", func) + + -- -- -1 for string length because of 0-termination + checkStrArg(presence.state, 127, "presence.state", func, true) + checkStrArg(presence.details, 127, "presence.details", func, true) + + checkIntArg(presence.start_time, 64, "presence.start_time", func, true) + checkIntArg(presence.end_time, 64, "presence.end_time", func, true) + + checkStrArg(presence.large_image, 127, "presence.large_image", func, true) + checkStrArg(presence.large_text, 127, "presence.large_text", func, true) + checkStrArg(presence.small_image, 127, "presence.small_image", func, true) + checkStrArg(presence.small_text, 127, "presence.small_text", func, true) + + checkStrArg(presence.party_id, 127, "presence.party_id", func, true) + checkIntArg(presence.party_size, 32, "presence.party_size", func, true) + checkIntArg(presence.party_max, 32, "presence.party_max", func, true) + + checkStrArg(presence.match_secret, 127, "presence.match_secret", func, true) + checkStrArg(presence.join_secret, 127, "presence.join_secret", func, true) + checkStrArg(presence.spectate_secret, 127, "presence.spectate_secret", func, true) + + local app = referencesTable.app + app.activities = app.core[0]:get_activity_manager() + app.application = app.core[0]:get_application_manager() + app.users = app.core[0]:get_user_manager() + + local activity = ffi.new("struct DiscordActivity") + local activityPtr = ffi.new("struct DiscordActivity[1]", activity) + ffi.C.memset(activityPtr, 0, ffi.sizeof(activity)) + + activityPtr[0].type = libGameSDK.DiscordActivityType_Playing + activityPtr[0].state = presence.state or "" + activityPtr[0].details = presence.details or "" + activityPtr[0].timestamps.start = presence.start_time or 0 + activityPtr[0].timestamps["end"] = presence.end_time or 0 + activityPtr[0].assets.large_image = presence.large_image or "" + activityPtr[0].assets.large_text = presence.large_text or "" + activityPtr[0].assets.small_image = presence.small_image or "" + activityPtr[0].assets.small_text = presence.small_text or "" + activityPtr[0].party.id = presence.party_id or "" + activityPtr[0].party.size.current_size = presence.party_size or 0 + activityPtr[0].party.size.max_size = presence.party_max or 0 + activityPtr[0].secrets.match = presence.match_secret or "" + activityPtr[0].secrets.join = presence.join_secret or "" + activityPtr[0].secrets.spectate = presence.spectate_secret or "" + + app.activities:update_activity(activityPtr, app.core, updateActivityCallback) + x = discordGameSDK.runCallbacks(app.core) + running = x == libGameSDK.DiscordResult_Ok + + -- Make sure garbage memory is collected, or otherwise + -- this script will keep on increasming RAM usage by about 8KB + -- everytime updateActivity or clearActivity is run. + collectgarbage() + collectgarbage() + referencesTable.running = running + return referencesTable +end + +function discordGameSDK.clearPresence(referencesTable) + -- If Discord isn't running, don't even try. + if not referencesTable.running then + return referencesTable + end + + local app = referencesTable.app + app.activities = app.core[0]:get_activity_manager() + app.application = app.core[0]:get_application_manager() + app.users = app.core[0]:get_user_manager() + + app.activities:clear_activity(app.core, clearActivityCallback) + x = discordGameSDK.runCallbacks(app.core) + running = x == libGameSDK.DiscordResult_Ok + + collectgarbage() + collectgarbage() + referencesTable.running = running + return referencesTable +end + +-- Attach a finaliser to run when discordGameSDK is garbage +-- collected. This happens when mpv quits. +getmetatable(discordGameSDK.gcDummy).__gc = function () + discordGameSDK.shutdown() + updateActivityCallback:free() + clearActivityCallback:free() + loggerCallback:free() +end + +-- http://luajit.org/ext_ffi_semantics.html#callback : +-- It is by default not allowed for C to callback into Lua, when +-- Lua had originally called into C. jit.off() allows it, so any +-- function that calls a callback needs to be wrapped in it. +jit.off(discordGameSDK.updatePresence) +jit.off(discordGameSDK.clearPresence) +jit.off(discordGameSDK.runCallbacks) +jit.off(discordGameSDK.initialize) + +return discordGameSDK diff --git a/libs/discordGameSDK/README.md b/libs/discordGameSDK/README.md new file mode 100644 index 0000000..3e18fdc --- /dev/null +++ b/libs/discordGameSDK/README.md @@ -0,0 +1,13 @@ +## Discord Game SDK + +> The SDK is currently under extensive development and is subject to change. Suggestions +> about the current API are welcome. + +### Setup + +- Create an application on the Discord [developer site](https://discordapp.com/developers/applications/me). +- Set a redirect URL. If you don't have one right now, just use . +- Enable Rich Presence for the application. This enables whitelist access for the SDK. + - When you are ready to test with more people, add them to the whitelist. +- Copy the **Client ID**. + - Use this `CLIENT_ID` when initializing the SDK. diff --git a/libs/discordGameSDK/c/discord_game_sdk.h b/libs/discordGameSDK/c/discord_game_sdk.h new file mode 100644 index 0000000..81cc2bd --- /dev/null +++ b/libs/discordGameSDK/c/discord_game_sdk.h @@ -0,0 +1,646 @@ +#ifndef _DISCORD_GAME_SDK_H_ +#define _DISCORD_GAME_SDK_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#ifndef __cplusplus +#include +#endif + +#define DISCORD_VERSION 2 +#define DISCORD_APPLICATION_MANAGER_VERSION 1 +#define DISCORD_USER_MANAGER_VERSION 1 +#define DISCORD_IMAGE_MANAGER_VERSION 1 +#define DISCORD_ACTIVITY_MANAGER_VERSION 1 +#define DISCORD_RELATIONSHIP_MANAGER_VERSION 1 +#define DISCORD_LOBBY_MANAGER_VERSION 1 +#define DISCORD_NETWORK_MANAGER_VERSION 1 +#define DISCORD_OVERLAY_MANAGER_VERSION 1 +#define DISCORD_STORAGE_MANAGER_VERSION 1 +#define DISCORD_STORE_MANAGER_VERSION 1 +#define DISCORD_VOICE_MANAGER_VERSION 1 +#define DISCORD_ACHIEVEMENT_MANAGER_VERSION 1 + +enum EDiscordResult { + DiscordResult_Ok = 0, + DiscordResult_ServiceUnavailable = 1, + DiscordResult_InvalidVersion = 2, + DiscordResult_LockFailed = 3, + DiscordResult_InternalError = 4, + DiscordResult_InvalidPayload = 5, + DiscordResult_InvalidCommand = 6, + DiscordResult_InvalidPermissions = 7, + DiscordResult_NotFetched = 8, + DiscordResult_NotFound = 9, + DiscordResult_Conflict = 10, + DiscordResult_InvalidSecret = 11, + DiscordResult_InvalidJoinSecret = 12, + DiscordResult_NoEligibleActivity = 13, + DiscordResult_InvalidInvite = 14, + DiscordResult_NotAuthenticated = 15, + DiscordResult_InvalidAccessToken = 16, + DiscordResult_ApplicationMismatch = 17, + DiscordResult_InvalidDataUrl = 18, + DiscordResult_InvalidBase64 = 19, + DiscordResult_NotFiltered = 20, + DiscordResult_LobbyFull = 21, + DiscordResult_InvalidLobbySecret = 22, + DiscordResult_InvalidFilename = 23, + DiscordResult_InvalidFileSize = 24, + DiscordResult_InvalidEntitlement = 25, + DiscordResult_NotInstalled = 26, + DiscordResult_NotRunning = 27, + DiscordResult_InsufficientBuffer = 28, + DiscordResult_PurchaseCanceled = 29, + DiscordResult_InvalidGuild = 30, + DiscordResult_InvalidEvent = 31, + DiscordResult_InvalidChannel = 32, + DiscordResult_InvalidOrigin = 33, + DiscordResult_RateLimited = 34, + DiscordResult_OAuth2Error = 35, + DiscordResult_SelectChannelTimeout = 36, + DiscordResult_GetGuildTimeout = 37, + DiscordResult_SelectVoiceForceRequired = 38, + DiscordResult_CaptureShortcutAlreadyListening = 39, + DiscordResult_UnauthorizedForAchievement = 40, + DiscordResult_InvalidGiftCode = 41, + DiscordResult_PurchaseError = 42, + DiscordResult_TransactionAborted = 43, +}; + +enum EDiscordCreateFlags { + DiscordCreateFlags_Default = 0, + DiscordCreateFlags_NoRequireDiscord = 1, +}; + +enum EDiscordLogLevel { + DiscordLogLevel_Error = 1, + DiscordLogLevel_Warn, + DiscordLogLevel_Info, + DiscordLogLevel_Debug, +}; + +enum EDiscordUserFlag { + DiscordUserFlag_Partner = 2, + DiscordUserFlag_HypeSquadEvents = 4, + DiscordUserFlag_HypeSquadHouse1 = 64, + DiscordUserFlag_HypeSquadHouse2 = 128, + DiscordUserFlag_HypeSquadHouse3 = 256, +}; + +enum EDiscordPremiumType { + DiscordPremiumType_None = 0, + DiscordPremiumType_Tier1 = 1, + DiscordPremiumType_Tier2 = 2, +}; + +enum EDiscordImageType { + DiscordImageType_User, +}; + +enum EDiscordActivityType { + DiscordActivityType_Playing, + DiscordActivityType_Streaming, + DiscordActivityType_Listening, + DiscordActivityType_Watching, +}; + +enum EDiscordActivityActionType { + DiscordActivityActionType_Join = 1, + DiscordActivityActionType_Spectate, +}; + +enum EDiscordActivityJoinRequestReply { + DiscordActivityJoinRequestReply_No, + DiscordActivityJoinRequestReply_Yes, + DiscordActivityJoinRequestReply_Ignore, +}; + +enum EDiscordStatus { + DiscordStatus_Offline = 0, + DiscordStatus_Online = 1, + DiscordStatus_Idle = 2, + DiscordStatus_DoNotDisturb = 3, +}; + +enum EDiscordRelationshipType { + DiscordRelationshipType_None, + DiscordRelationshipType_Friend, + DiscordRelationshipType_Blocked, + DiscordRelationshipType_PendingIncoming, + DiscordRelationshipType_PendingOutgoing, + DiscordRelationshipType_Implicit, +}; + +enum EDiscordLobbyType { + DiscordLobbyType_Private = 1, + DiscordLobbyType_Public, +}; + +enum EDiscordLobbySearchComparison { + DiscordLobbySearchComparison_LessThanOrEqual = -2, + DiscordLobbySearchComparison_LessThan, + DiscordLobbySearchComparison_Equal, + DiscordLobbySearchComparison_GreaterThan, + DiscordLobbySearchComparison_GreaterThanOrEqual, + DiscordLobbySearchComparison_NotEqual, +}; + +enum EDiscordLobbySearchCast { + DiscordLobbySearchCast_String = 1, + DiscordLobbySearchCast_Number, +}; + +enum EDiscordLobbySearchDistance { + DiscordLobbySearchDistance_Local, + DiscordLobbySearchDistance_Default, + DiscordLobbySearchDistance_Extended, + DiscordLobbySearchDistance_Global, +}; + +enum EDiscordEntitlementType { + DiscordEntitlementType_Purchase = 1, + DiscordEntitlementType_PremiumSubscription, + DiscordEntitlementType_DeveloperGift, + DiscordEntitlementType_TestModePurchase, + DiscordEntitlementType_FreePurchase, + DiscordEntitlementType_UserGift, + DiscordEntitlementType_PremiumPurchase, +}; + +enum EDiscordSkuType { + DiscordSkuType_Application = 1, + DiscordSkuType_DLC, + DiscordSkuType_Consumable, + DiscordSkuType_Bundle, +}; + +enum EDiscordInputModeType { + DiscordInputModeType_VoiceActivity = 0, + DiscordInputModeType_PushToTalk, +}; + +typedef int64_t DiscordClientId; +typedef int32_t DiscordVersion; +typedef int64_t DiscordSnowflake; +typedef int64_t DiscordTimestamp; +typedef DiscordSnowflake DiscordUserId; +typedef char DiscordLocale[128]; +typedef char DiscordBranch[4096]; +typedef DiscordSnowflake DiscordLobbyId; +typedef char DiscordLobbySecret[128]; +typedef char DiscordMetadataKey[256]; +typedef char DiscordMetadataValue[4096]; +typedef uint64_t DiscordNetworkPeerId; +typedef uint8_t DiscordNetworkChannelId; +typedef char DiscordPath[4096]; +typedef char DiscordDateTime[64]; + +struct DiscordUser { + DiscordUserId id; + char username[256]; + char discriminator[8]; + char avatar[128]; + bool bot; +}; + +struct DiscordOAuth2Token { + char access_token[128]; + char scopes[1024]; + DiscordTimestamp expires; +}; + +struct DiscordImageHandle { + enum EDiscordImageType type; + int64_t id; + uint32_t size; +}; + +struct DiscordImageDimensions { + uint32_t width; + uint32_t height; +}; + +struct DiscordActivityTimestamps { + DiscordTimestamp start; + DiscordTimestamp end; +}; + +struct DiscordActivityAssets { + char large_image[128]; + char large_text[128]; + char small_image[128]; + char small_text[128]; +}; + +struct DiscordPartySize { + int32_t current_size; + int32_t max_size; +}; + +struct DiscordActivityParty { + char id[128]; + struct DiscordPartySize size; +}; + +struct DiscordActivitySecrets { + char match[128]; + char join[128]; + char spectate[128]; +}; + +struct DiscordActivity { + enum EDiscordActivityType type; + int64_t application_id; + char name[128]; + char state[128]; + char details[128]; + struct DiscordActivityTimestamps timestamps; + struct DiscordActivityAssets assets; + struct DiscordActivityParty party; + struct DiscordActivitySecrets secrets; + bool instance; +}; + +struct DiscordPresence { + enum EDiscordStatus status; + struct DiscordActivity activity; +}; + +struct DiscordRelationship { + enum EDiscordRelationshipType type; + struct DiscordUser user; + struct DiscordPresence presence; +}; + +struct DiscordLobby { + DiscordLobbyId id; + enum EDiscordLobbyType type; + DiscordUserId owner_id; + DiscordLobbySecret secret; + uint32_t capacity; + bool locked; +}; + +struct DiscordFileStat { + char filename[260]; + uint64_t size; + uint64_t last_modified; +}; + +struct DiscordEntitlement { + DiscordSnowflake id; + enum EDiscordEntitlementType type; + DiscordSnowflake sku_id; +}; + +struct DiscordSkuPrice { + uint32_t amount; + char currency[16]; +}; + +struct DiscordSku { + DiscordSnowflake id; + enum EDiscordSkuType type; + char name[256]; + struct DiscordSkuPrice price; +}; + +struct DiscordInputMode { + enum EDiscordInputModeType type; + char shortcut[256]; +}; + +struct DiscordUserAchievement { + DiscordSnowflake user_id; + DiscordSnowflake achievement_id; + uint8_t percent_complete; + DiscordDateTime unlocked_at; +}; + +struct IDiscordLobbyTransaction { + enum EDiscordResult (*set_type)(struct IDiscordLobbyTransaction* lobby_transaction, enum EDiscordLobbyType type); + enum EDiscordResult (*set_owner)(struct IDiscordLobbyTransaction* lobby_transaction, DiscordUserId owner_id); + enum EDiscordResult (*set_capacity)(struct IDiscordLobbyTransaction* lobby_transaction, uint32_t capacity); + enum EDiscordResult (*set_metadata)(struct IDiscordLobbyTransaction* lobby_transaction, DiscordMetadataKey key, DiscordMetadataValue value); + enum EDiscordResult (*delete_metadata)(struct IDiscordLobbyTransaction* lobby_transaction, DiscordMetadataKey key); + enum EDiscordResult (*set_locked)(struct IDiscordLobbyTransaction* lobby_transaction, bool locked); +}; + +struct IDiscordLobbyMemberTransaction { + enum EDiscordResult (*set_metadata)(struct IDiscordLobbyMemberTransaction* lobby_member_transaction, DiscordMetadataKey key, DiscordMetadataValue value); + enum EDiscordResult (*delete_metadata)(struct IDiscordLobbyMemberTransaction* lobby_member_transaction, DiscordMetadataKey key); +}; + +struct IDiscordLobbySearchQuery { + enum EDiscordResult (*filter)(struct IDiscordLobbySearchQuery* lobby_search_query, DiscordMetadataKey key, enum EDiscordLobbySearchComparison comparison, enum EDiscordLobbySearchCast cast, DiscordMetadataValue value); + enum EDiscordResult (*sort)(struct IDiscordLobbySearchQuery* lobby_search_query, DiscordMetadataKey key, enum EDiscordLobbySearchCast cast, DiscordMetadataValue value); + enum EDiscordResult (*limit)(struct IDiscordLobbySearchQuery* lobby_search_query, uint32_t limit); + enum EDiscordResult (*distance)(struct IDiscordLobbySearchQuery* lobby_search_query, enum EDiscordLobbySearchDistance distance); +}; + +typedef void* IDiscordApplicationEvents; + +struct IDiscordApplicationManager { + void (*validate_or_exit)(struct IDiscordApplicationManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*get_current_locale)(struct IDiscordApplicationManager* manager, DiscordLocale* locale); + void (*get_current_branch)(struct IDiscordApplicationManager* manager, DiscordBranch* branch); + void (*get_oauth2_token)(struct IDiscordApplicationManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordOAuth2Token* oauth2_token)); + void (*get_ticket)(struct IDiscordApplicationManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, const char* data)); +}; + +struct IDiscordUserEvents { + void (*on_current_user_update)(void* event_data); +}; + +struct IDiscordUserManager { + enum EDiscordResult (*get_current_user)(struct IDiscordUserManager* manager, struct DiscordUser* current_user); + void (*get_user)(struct IDiscordUserManager* manager, DiscordUserId user_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordUser* user)); + enum EDiscordResult (*get_current_user_premium_type)(struct IDiscordUserManager* manager, enum EDiscordPremiumType* premium_type); + enum EDiscordResult (*current_user_has_flag)(struct IDiscordUserManager* manager, enum EDiscordUserFlag flag, bool* has_flag); +}; + +typedef void* IDiscordImageEvents; + +struct IDiscordImageManager { + void (*fetch)(struct IDiscordImageManager* manager, struct DiscordImageHandle handle, bool refresh, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordImageHandle handle_result)); + enum EDiscordResult (*get_dimensions)(struct IDiscordImageManager* manager, struct DiscordImageHandle handle, struct DiscordImageDimensions* dimensions); + enum EDiscordResult (*get_data)(struct IDiscordImageManager* manager, struct DiscordImageHandle handle, uint8_t* data, uint32_t data_length); +}; + +struct IDiscordActivityEvents { + void (*on_activity_join)(void* event_data, const char* secret); + void (*on_activity_spectate)(void* event_data, const char* secret); + void (*on_activity_join_request)(void* event_data, struct DiscordUser* user); + void (*on_activity_invite)(void* event_data, enum EDiscordActivityActionType type, struct DiscordUser* user, struct DiscordActivity* activity); +}; + +struct IDiscordActivityManager { + enum EDiscordResult (*register_command)(struct IDiscordActivityManager* manager, const char* command); + enum EDiscordResult (*register_steam)(struct IDiscordActivityManager* manager, uint32_t steam_id); + void (*update_activity)(struct IDiscordActivityManager* manager, struct DiscordActivity* activity, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*clear_activity)(struct IDiscordActivityManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*send_request_reply)(struct IDiscordActivityManager* manager, DiscordUserId user_id, enum EDiscordActivityJoinRequestReply reply, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*send_invite)(struct IDiscordActivityManager* manager, DiscordUserId user_id, enum EDiscordActivityActionType type, const char* content, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*accept_invite)(struct IDiscordActivityManager* manager, DiscordUserId user_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); +}; + +struct IDiscordRelationshipEvents { + void (*on_refresh)(void* event_data); + void (*on_relationship_update)(void* event_data, struct DiscordRelationship* relationship); +}; + +struct IDiscordRelationshipManager { + void (*filter)(struct IDiscordRelationshipManager* manager, void* filter_data, bool (*filter)(void* filter_data, struct DiscordRelationship* relationship)); + enum EDiscordResult (*count)(struct IDiscordRelationshipManager* manager, int32_t* count); + enum EDiscordResult (*get)(struct IDiscordRelationshipManager* manager, DiscordUserId user_id, struct DiscordRelationship* relationship); + enum EDiscordResult (*get_at)(struct IDiscordRelationshipManager* manager, uint32_t index, struct DiscordRelationship* relationship); +}; + +struct IDiscordLobbyEvents { + void (*on_lobby_update)(void* event_data, int64_t lobby_id); + void (*on_lobby_delete)(void* event_data, int64_t lobby_id, uint32_t reason); + void (*on_member_connect)(void* event_data, int64_t lobby_id, int64_t user_id); + void (*on_member_update)(void* event_data, int64_t lobby_id, int64_t user_id); + void (*on_member_disconnect)(void* event_data, int64_t lobby_id, int64_t user_id); + void (*on_lobby_message)(void* event_data, int64_t lobby_id, int64_t user_id, uint8_t* data, uint32_t data_length); + void (*on_speaking)(void* event_data, int64_t lobby_id, int64_t user_id, bool speaking); + void (*on_network_message)(void* event_data, int64_t lobby_id, int64_t user_id, uint8_t channel_id, uint8_t* data, uint32_t data_length); +}; + +struct IDiscordLobbyManager { + enum EDiscordResult (*get_lobby_create_transaction)(struct IDiscordLobbyManager* manager, struct IDiscordLobbyTransaction** transaction); + enum EDiscordResult (*get_lobby_update_transaction)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, struct IDiscordLobbyTransaction** transaction); + enum EDiscordResult (*get_member_update_transaction)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, struct IDiscordLobbyMemberTransaction** transaction); + void (*create_lobby)(struct IDiscordLobbyManager* manager, struct IDiscordLobbyTransaction* transaction, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordLobby* lobby)); + void (*update_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, struct IDiscordLobbyTransaction* transaction, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*delete_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*connect_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordLobbySecret secret, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordLobby* lobby)); + void (*connect_lobby_with_activity_secret)(struct IDiscordLobbyManager* manager, DiscordLobbySecret activity_secret, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, struct DiscordLobby* lobby)); + void (*disconnect_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*get_lobby)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, struct DiscordLobby* lobby); + enum EDiscordResult (*get_lobby_activity_secret)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordLobbySecret* secret); + enum EDiscordResult (*get_lobby_metadata_value)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordMetadataKey key, DiscordMetadataValue* value); + enum EDiscordResult (*get_lobby_metadata_key)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, int32_t index, DiscordMetadataKey* key); + enum EDiscordResult (*lobby_metadata_count)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, int32_t* count); + enum EDiscordResult (*member_count)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, int32_t* count); + enum EDiscordResult (*get_member_user_id)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, int32_t index, DiscordUserId* user_id); + enum EDiscordResult (*get_member_user)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, struct DiscordUser* user); + enum EDiscordResult (*get_member_metadata_value)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, DiscordMetadataKey key, DiscordMetadataValue* value); + enum EDiscordResult (*get_member_metadata_key)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, int32_t index, DiscordMetadataKey* key); + enum EDiscordResult (*member_metadata_count)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, int32_t* count); + void (*update_member)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, struct IDiscordLobbyMemberTransaction* transaction, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*send_lobby_message)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, uint8_t* data, uint32_t data_length, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*get_search_query)(struct IDiscordLobbyManager* manager, struct IDiscordLobbySearchQuery** query); + void (*search)(struct IDiscordLobbyManager* manager, struct IDiscordLobbySearchQuery* query, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*lobby_count)(struct IDiscordLobbyManager* manager, int32_t* count); + enum EDiscordResult (*get_lobby_id)(struct IDiscordLobbyManager* manager, int32_t index, DiscordLobbyId* lobby_id); + void (*connect_voice)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*disconnect_voice)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*connect_network)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id); + enum EDiscordResult (*disconnect_network)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id); + enum EDiscordResult (*flush_network)(struct IDiscordLobbyManager* manager); + enum EDiscordResult (*open_network_channel)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, uint8_t channel_id, bool reliable); + enum EDiscordResult (*send_network_message)(struct IDiscordLobbyManager* manager, DiscordLobbyId lobby_id, DiscordUserId user_id, uint8_t channel_id, uint8_t* data, uint32_t data_length); +}; + +struct IDiscordNetworkEvents { + void (*on_message)(void* event_data, DiscordNetworkPeerId peer_id, DiscordNetworkChannelId channel_id, uint8_t* data, uint32_t data_length); + void (*on_route_update)(void* event_data, const char* route_data); +}; + +struct IDiscordNetworkManager { + /** + * Get the local peer ID for this process. + */ + void (*get_peer_id)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId* peer_id); + /** + * Send pending network messages. + */ + enum EDiscordResult (*flush)(struct IDiscordNetworkManager* manager); + /** + * Open a connection to a remote peer. + */ + enum EDiscordResult (*open_peer)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, const char* route_data); + /** + * Update the route data for a connected peer. + */ + enum EDiscordResult (*update_peer)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, const char* route_data); + /** + * Close the connection to a remote peer. + */ + enum EDiscordResult (*close_peer)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id); + /** + * Open a message channel to a connected peer. + */ + enum EDiscordResult (*open_channel)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, DiscordNetworkChannelId channel_id, bool reliable); + /** + * Close a message channel to a connected peer. + */ + enum EDiscordResult (*close_channel)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, DiscordNetworkChannelId channel_id); + /** + * Send a message to a connected peer over an opened message channel. + */ + enum EDiscordResult (*send_message)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId peer_id, DiscordNetworkChannelId channel_id, uint8_t* data, uint32_t data_length); +}; + +struct IDiscordOverlayEvents { + void (*on_toggle)(void* event_data, bool locked); +}; + +struct IDiscordOverlayManager { + void (*is_enabled)(struct IDiscordOverlayManager* manager, bool* enabled); + void (*is_locked)(struct IDiscordOverlayManager* manager, bool* locked); + void (*set_locked)(struct IDiscordOverlayManager* manager, bool locked, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*open_activity_invite)(struct IDiscordOverlayManager* manager, enum EDiscordActivityActionType type, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*open_guild_invite)(struct IDiscordOverlayManager* manager, const char* code, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*open_voice_settings)(struct IDiscordOverlayManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); +}; + +typedef void* IDiscordStorageEvents; + +struct IDiscordStorageManager { + enum EDiscordResult (*read)(struct IDiscordStorageManager* manager, const char* name, uint8_t* data, uint32_t data_length, uint32_t* read); + void (*read_async)(struct IDiscordStorageManager* manager, const char* name, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, uint8_t* data, uint32_t data_length)); + void (*read_async_partial)(struct IDiscordStorageManager* manager, const char* name, uint64_t offset, uint64_t length, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result, uint8_t* data, uint32_t data_length)); + enum EDiscordResult (*write)(struct IDiscordStorageManager* manager, const char* name, uint8_t* data, uint32_t data_length); + void (*write_async)(struct IDiscordStorageManager* manager, const char* name, uint8_t* data, uint32_t data_length, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*delete_)(struct IDiscordStorageManager* manager, const char* name); + enum EDiscordResult (*exists)(struct IDiscordStorageManager* manager, const char* name, bool* exists); + void (*count)(struct IDiscordStorageManager* manager, int32_t* count); + enum EDiscordResult (*stat)(struct IDiscordStorageManager* manager, const char* name, struct DiscordFileStat* stat); + enum EDiscordResult (*stat_at)(struct IDiscordStorageManager* manager, int32_t index, struct DiscordFileStat* stat); + enum EDiscordResult (*get_path)(struct IDiscordStorageManager* manager, DiscordPath* path); +}; + +struct IDiscordStoreEvents { + void (*on_entitlement_create)(void* event_data, struct DiscordEntitlement* entitlement); + void (*on_entitlement_delete)(void* event_data, struct DiscordEntitlement* entitlement); +}; + +struct IDiscordStoreManager { + void (*fetch_skus)(struct IDiscordStoreManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*count_skus)(struct IDiscordStoreManager* manager, int32_t* count); + enum EDiscordResult (*get_sku)(struct IDiscordStoreManager* manager, DiscordSnowflake sku_id, struct DiscordSku* sku); + enum EDiscordResult (*get_sku_at)(struct IDiscordStoreManager* manager, int32_t index, struct DiscordSku* sku); + void (*fetch_entitlements)(struct IDiscordStoreManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*count_entitlements)(struct IDiscordStoreManager* manager, int32_t* count); + enum EDiscordResult (*get_entitlement)(struct IDiscordStoreManager* manager, DiscordSnowflake entitlement_id, struct DiscordEntitlement* entitlement); + enum EDiscordResult (*get_entitlement_at)(struct IDiscordStoreManager* manager, int32_t index, struct DiscordEntitlement* entitlement); + enum EDiscordResult (*has_sku_entitlement)(struct IDiscordStoreManager* manager, DiscordSnowflake sku_id, bool* has_entitlement); + void (*start_purchase)(struct IDiscordStoreManager* manager, DiscordSnowflake sku_id, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); +}; + +struct IDiscordVoiceEvents { + void (*on_settings_update)(void* event_data); +}; + +struct IDiscordVoiceManager { + enum EDiscordResult (*get_input_mode)(struct IDiscordVoiceManager* manager, struct DiscordInputMode* input_mode); + void (*set_input_mode)(struct IDiscordVoiceManager* manager, struct DiscordInputMode input_mode, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*is_self_mute)(struct IDiscordVoiceManager* manager, bool* mute); + enum EDiscordResult (*set_self_mute)(struct IDiscordVoiceManager* manager, bool mute); + enum EDiscordResult (*is_self_deaf)(struct IDiscordVoiceManager* manager, bool* deaf); + enum EDiscordResult (*set_self_deaf)(struct IDiscordVoiceManager* manager, bool deaf); + enum EDiscordResult (*is_local_mute)(struct IDiscordVoiceManager* manager, DiscordSnowflake user_id, bool* mute); + enum EDiscordResult (*set_local_mute)(struct IDiscordVoiceManager* manager, DiscordSnowflake user_id, bool mute); + enum EDiscordResult (*get_local_volume)(struct IDiscordVoiceManager* manager, DiscordSnowflake user_id, uint8_t* volume); + enum EDiscordResult (*set_local_volume)(struct IDiscordVoiceManager* manager, DiscordSnowflake user_id, uint8_t volume); +}; + +struct IDiscordAchievementEvents { + void (*on_user_achievement_update)(void* event_data, struct DiscordUserAchievement* user_achievement); +}; + +struct IDiscordAchievementManager { + void (*set_user_achievement)(struct IDiscordAchievementManager* manager, DiscordSnowflake achievement_id, uint8_t percent_complete, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*fetch_user_achievements)(struct IDiscordAchievementManager* manager, void* callback_data, void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*count_user_achievements)(struct IDiscordAchievementManager* manager, int32_t* count); + enum EDiscordResult (*get_user_achievement)(struct IDiscordAchievementManager* manager, DiscordSnowflake user_achievement_id, struct DiscordUserAchievement* user_achievement); + enum EDiscordResult (*get_user_achievement_at)(struct IDiscordAchievementManager* manager, int32_t index, struct DiscordUserAchievement* user_achievement); +}; + +typedef void* IDiscordCoreEvents; + +struct IDiscordCore { + void (*destroy)(struct IDiscordCore* core); + enum EDiscordResult (*run_callbacks)(struct IDiscordCore* core); + void (*set_log_hook)(struct IDiscordCore* core, enum EDiscordLogLevel min_level, void* hook_data, void (*hook)(void* hook_data, enum EDiscordLogLevel level, const char* message)); + struct IDiscordApplicationManager* (*get_application_manager)(struct IDiscordCore* core); + struct IDiscordUserManager* (*get_user_manager)(struct IDiscordCore* core); + struct IDiscordImageManager* (*get_image_manager)(struct IDiscordCore* core); + struct IDiscordActivityManager* (*get_activity_manager)(struct IDiscordCore* core); + struct IDiscordRelationshipManager* (*get_relationship_manager)(struct IDiscordCore* core); + struct IDiscordLobbyManager* (*get_lobby_manager)(struct IDiscordCore* core); + struct IDiscordNetworkManager* (*get_network_manager)(struct IDiscordCore* core); + struct IDiscordOverlayManager* (*get_overlay_manager)(struct IDiscordCore* core); + struct IDiscordStorageManager* (*get_storage_manager)(struct IDiscordCore* core); + struct IDiscordStoreManager* (*get_store_manager)(struct IDiscordCore* core); + struct IDiscordVoiceManager* (*get_voice_manager)(struct IDiscordCore* core); + struct IDiscordAchievementManager* (*get_achievement_manager)(struct IDiscordCore* core); +}; + +struct DiscordCreateParams { + DiscordClientId client_id; + uint64_t flags; + IDiscordCoreEvents* events; + void* event_data; + IDiscordApplicationEvents* application_events; + DiscordVersion application_version; + struct IDiscordUserEvents* user_events; + DiscordVersion user_version; + IDiscordImageEvents* image_events; + DiscordVersion image_version; + struct IDiscordActivityEvents* activity_events; + DiscordVersion activity_version; + struct IDiscordRelationshipEvents* relationship_events; + DiscordVersion relationship_version; + struct IDiscordLobbyEvents* lobby_events; + DiscordVersion lobby_version; + struct IDiscordNetworkEvents* network_events; + DiscordVersion network_version; + struct IDiscordOverlayEvents* overlay_events; + DiscordVersion overlay_version; + IDiscordStorageEvents* storage_events; + DiscordVersion storage_version; + struct IDiscordStoreEvents* store_events; + DiscordVersion store_version; + struct IDiscordVoiceEvents* voice_events; + DiscordVersion voice_version; + struct IDiscordAchievementEvents* achievement_events; + DiscordVersion achievement_version; +}; + +#ifdef __cplusplus +inline +#else +static +#endif +void DiscordCreateParamsSetDefault(struct DiscordCreateParams* params) +{ + memset(params, 0, sizeof(struct DiscordCreateParams)); + params->application_version = DISCORD_APPLICATION_MANAGER_VERSION; + params->user_version = DISCORD_USER_MANAGER_VERSION; + params->image_version = DISCORD_IMAGE_MANAGER_VERSION; + params->activity_version = DISCORD_ACTIVITY_MANAGER_VERSION; + params->relationship_version = DISCORD_RELATIONSHIP_MANAGER_VERSION; + params->lobby_version = DISCORD_LOBBY_MANAGER_VERSION; + params->network_version = DISCORD_NETWORK_MANAGER_VERSION; + params->overlay_version = DISCORD_OVERLAY_MANAGER_VERSION; + params->storage_version = DISCORD_STORAGE_MANAGER_VERSION; + params->store_version = DISCORD_STORE_MANAGER_VERSION; + params->voice_version = DISCORD_VOICE_MANAGER_VERSION; + params->achievement_version = DISCORD_ACHIEVEMENT_MANAGER_VERSION; +} + +enum EDiscordResult DiscordCreate(DiscordVersion version, struct DiscordCreateParams* params, struct IDiscordCore** result); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libs/discordGameSDK/cpp/achievement_manager.cpp b/libs/discordGameSDK/cpp/achievement_manager.cpp new file mode 100644 index 0000000..7e3bc43 --- /dev/null +++ b/libs/discordGameSDK/cpp/achievement_manager.cpp @@ -0,0 +1,98 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "achievement_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +class AchievementEvents final { +public: + static void OnUserAchievementUpdate(void* callbackData, DiscordUserAchievement* userAchievement) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->AchievementManager(); + module.OnUserAchievementUpdate(*reinterpret_cast(userAchievement)); + } +}; + +IDiscordAchievementEvents AchievementManager::events_{ + &AchievementEvents::OnUserAchievementUpdate, +}; + +void AchievementManager::SetUserAchievement(Snowflake achievementId, + std::uint8_t percentComplete, + std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->set_user_achievement( + internal_, achievementId, percentComplete, cb.release(), wrapper); +} + +void AchievementManager::FetchUserAchievements(std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->fetch_user_achievements(internal_, cb.release(), wrapper); +} + +void AchievementManager::CountUserAchievements(std::int32_t* count) +{ + if (!count) { + return; + } + + internal_->count_user_achievements(internal_, reinterpret_cast(count)); +} + +Result AchievementManager::GetUserAchievement(Snowflake userAchievementId, + UserAchievement* userAchievement) +{ + if (!userAchievement) { + return Result::InternalError; + } + + auto result = internal_->get_user_achievement( + internal_, userAchievementId, reinterpret_cast(userAchievement)); + return static_cast(result); +} + +Result AchievementManager::GetUserAchievementAt(std::int32_t index, + UserAchievement* userAchievement) +{ + if (!userAchievement) { + return Result::InternalError; + } + + auto result = internal_->get_user_achievement_at( + internal_, index, reinterpret_cast(userAchievement)); + return static_cast(result); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/achievement_manager.h b/libs/discordGameSDK/cpp/achievement_manager.h new file mode 100644 index 0000000..1f58c8e --- /dev/null +++ b/libs/discordGameSDK/cpp/achievement_manager.h @@ -0,0 +1,34 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class AchievementManager final { +public: + ~AchievementManager() = default; + + void SetUserAchievement(Snowflake achievementId, + std::uint8_t percentComplete, + std::function callback); + void FetchUserAchievements(std::function callback); + void CountUserAchievements(std::int32_t* count); + Result GetUserAchievement(Snowflake userAchievementId, UserAchievement* userAchievement); + Result GetUserAchievementAt(std::int32_t index, UserAchievement* userAchievement); + + Event OnUserAchievementUpdate; + +private: + friend class Core; + + AchievementManager() = default; + AchievementManager(AchievementManager const& rhs) = delete; + AchievementManager& operator=(AchievementManager const& rhs) = delete; + AchievementManager(AchievementManager&& rhs) = delete; + AchievementManager& operator=(AchievementManager&& rhs) = delete; + + IDiscordAchievementManager* internal_; + static IDiscordAchievementEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/activity_manager.cpp b/libs/discordGameSDK/cpp/activity_manager.cpp new file mode 100644 index 0000000..b9b6760 --- /dev/null +++ b/libs/discordGameSDK/cpp/activity_manager.cpp @@ -0,0 +1,177 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "activity_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +class ActivityEvents final { +public: + static void OnActivityJoin(void* callbackData, char const* secret) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->ActivityManager(); + module.OnActivityJoin(static_cast(secret)); + } + + static void OnActivitySpectate(void* callbackData, char const* secret) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->ActivityManager(); + module.OnActivitySpectate(static_cast(secret)); + } + + static void OnActivityJoinRequest(void* callbackData, DiscordUser* user) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->ActivityManager(); + module.OnActivityJoinRequest(*reinterpret_cast(user)); + } + + static void OnActivityInvite(void* callbackData, + EDiscordActivityActionType type, + DiscordUser* user, + DiscordActivity* activity) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->ActivityManager(); + module.OnActivityInvite(static_cast(type), + *reinterpret_cast(user), + *reinterpret_cast(activity)); + } +}; + +IDiscordActivityEvents ActivityManager::events_{ + &ActivityEvents::OnActivityJoin, + &ActivityEvents::OnActivitySpectate, + &ActivityEvents::OnActivityJoinRequest, + &ActivityEvents::OnActivityInvite, +}; + +Result ActivityManager::RegisterCommand(char const* command) +{ + auto result = internal_->register_command(internal_, const_cast(command)); + return static_cast(result); +} + +Result ActivityManager::RegisterSteam(std::uint32_t steamId) +{ + auto result = internal_->register_steam(internal_, steamId); + return static_cast(result); +} + +void ActivityManager::UpdateActivity(Activity const& activity, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->update_activity(internal_, + reinterpret_cast(const_cast(&activity)), + cb.release(), + wrapper); +} + +void ActivityManager::ClearActivity(std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->clear_activity(internal_, cb.release(), wrapper); +} + +void ActivityManager::SendRequestReply(UserId userId, + ActivityJoinRequestReply reply, + std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->send_request_reply(internal_, + userId, + static_cast(reply), + cb.release(), + wrapper); +} + +void ActivityManager::SendInvite(UserId userId, + ActivityActionType type, + char const* content, + std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->send_invite(internal_, + userId, + static_cast(type), + const_cast(content), + cb.release(), + wrapper); +} + +void ActivityManager::AcceptInvite(UserId userId, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->accept_invite(internal_, userId, cb.release(), wrapper); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/activity_manager.h b/libs/discordGameSDK/cpp/activity_manager.h new file mode 100644 index 0000000..c107be2 --- /dev/null +++ b/libs/discordGameSDK/cpp/activity_manager.h @@ -0,0 +1,42 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class ActivityManager final { +public: + ~ActivityManager() = default; + + Result RegisterCommand(char const* command); + Result RegisterSteam(std::uint32_t steamId); + void UpdateActivity(Activity const& activity, std::function callback); + void ClearActivity(std::function callback); + void SendRequestReply(UserId userId, + ActivityJoinRequestReply reply, + std::function callback); + void SendInvite(UserId userId, + ActivityActionType type, + char const* content, + std::function callback); + void AcceptInvite(UserId userId, std::function callback); + + Event OnActivityJoin; + Event OnActivitySpectate; + Event OnActivityJoinRequest; + Event OnActivityInvite; + +private: + friend class Core; + + ActivityManager() = default; + ActivityManager(ActivityManager const& rhs) = delete; + ActivityManager& operator=(ActivityManager const& rhs) = delete; + ActivityManager(ActivityManager&& rhs) = delete; + ActivityManager& operator=(ActivityManager&& rhs) = delete; + + IDiscordActivityManager* internal_; + static IDiscordActivityEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/application_manager.cpp b/libs/discordGameSDK/cpp/application_manager.cpp new file mode 100644 index 0000000..0e05f3f --- /dev/null +++ b/libs/discordGameSDK/cpp/application_manager.cpp @@ -0,0 +1,78 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "application_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +void ApplicationManager::ValidateOrExit(std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->validate_or_exit(internal_, cb.release(), wrapper); +} + +void ApplicationManager::GetCurrentLocale(char locale[128]) +{ + if (!locale) { + return; + } + + internal_->get_current_locale(internal_, reinterpret_cast(locale)); +} + +void ApplicationManager::GetCurrentBranch(char branch[4096]) +{ + if (!branch) { + return; + } + + internal_->get_current_branch(internal_, reinterpret_cast(branch)); +} + +void ApplicationManager::GetOAuth2Token(std::function callback) +{ + static auto wrapper = + [](void* callbackData, EDiscordResult result, DiscordOAuth2Token* oauth2Token) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result), *reinterpret_cast(oauth2Token)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->get_oauth2_token(internal_, cb.release(), wrapper); +} + +void ApplicationManager::GetTicket(std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result, char const* data) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result), static_cast(data)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->get_ticket(internal_, cb.release(), wrapper); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/application_manager.h b/libs/discordGameSDK/cpp/application_manager.h new file mode 100644 index 0000000..ab0e856 --- /dev/null +++ b/libs/discordGameSDK/cpp/application_manager.h @@ -0,0 +1,30 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class ApplicationManager final { +public: + ~ApplicationManager() = default; + + void ValidateOrExit(std::function callback); + void GetCurrentLocale(char locale[128]); + void GetCurrentBranch(char branch[4096]); + void GetOAuth2Token(std::function callback); + void GetTicket(std::function callback); + +private: + friend class Core; + + ApplicationManager() = default; + ApplicationManager(ApplicationManager const& rhs) = delete; + ApplicationManager& operator=(ApplicationManager const& rhs) = delete; + ApplicationManager(ApplicationManager&& rhs) = delete; + ApplicationManager& operator=(ApplicationManager&& rhs) = delete; + + IDiscordApplicationManager* internal_; + static IDiscordApplicationEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/core.cpp b/libs/discordGameSDK/cpp/core.cpp new file mode 100644 index 0000000..110c9ef --- /dev/null +++ b/libs/discordGameSDK/cpp/core.cpp @@ -0,0 +1,182 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "core.h" + +#include +#include + +namespace discord { + +Result Core::Create(ClientId clientId, std::uint64_t flags, Core** instance) +{ + if (!instance) { + return Result::InternalError; + } + + (*instance) = new Core(); + DiscordCreateParams params{}; + DiscordCreateParamsSetDefault(¶ms); + params.client_id = clientId; + params.flags = flags; + params.events = nullptr; + params.event_data = *instance; + params.user_events = &UserManager::events_; + params.activity_events = &ActivityManager::events_; + params.relationship_events = &RelationshipManager::events_; + params.lobby_events = &LobbyManager::events_; + params.network_events = &NetworkManager::events_; + params.overlay_events = &OverlayManager::events_; + params.store_events = &StoreManager::events_; + params.voice_events = &VoiceManager::events_; + params.achievement_events = &AchievementManager::events_; + auto result = DiscordCreate(DISCORD_VERSION, ¶ms, &((*instance)->internal_)); + if (result != DiscordResult_Ok || !(*instance)->internal_) { + delete (*instance); + (*instance) = nullptr; + } + + return static_cast(result); +} + +Core::~Core() +{ + if (internal_) { + internal_->destroy(internal_); + internal_ = nullptr; + } +} + +Result Core::RunCallbacks() +{ + auto result = internal_->run_callbacks(internal_); + return static_cast(result); +} + +void Core::SetLogHook(LogLevel minLevel, std::function hook) +{ + setLogHook_.DisconnectAll(); + setLogHook_.Connect(std::move(hook)); + static auto wrapper = + [](void* callbackData, EDiscordLogLevel level, char const* message) -> void { + auto cb(reinterpret_cast(callbackData)); + if (!cb) { + return; + } + (*cb)(static_cast(level), static_cast(message)); + }; + + internal_->set_log_hook( + internal_, static_cast(minLevel), &setLogHook_, wrapper); +} + +discord::ApplicationManager& Core::ApplicationManager() +{ + if (!applicationManager_.internal_) { + applicationManager_.internal_ = internal_->get_application_manager(internal_); + } + + return applicationManager_; +} + +discord::UserManager& Core::UserManager() +{ + if (!userManager_.internal_) { + userManager_.internal_ = internal_->get_user_manager(internal_); + } + + return userManager_; +} + +discord::ImageManager& Core::ImageManager() +{ + if (!imageManager_.internal_) { + imageManager_.internal_ = internal_->get_image_manager(internal_); + } + + return imageManager_; +} + +discord::ActivityManager& Core::ActivityManager() +{ + if (!activityManager_.internal_) { + activityManager_.internal_ = internal_->get_activity_manager(internal_); + } + + return activityManager_; +} + +discord::RelationshipManager& Core::RelationshipManager() +{ + if (!relationshipManager_.internal_) { + relationshipManager_.internal_ = internal_->get_relationship_manager(internal_); + } + + return relationshipManager_; +} + +discord::LobbyManager& Core::LobbyManager() +{ + if (!lobbyManager_.internal_) { + lobbyManager_.internal_ = internal_->get_lobby_manager(internal_); + } + + return lobbyManager_; +} + +discord::NetworkManager& Core::NetworkManager() +{ + if (!networkManager_.internal_) { + networkManager_.internal_ = internal_->get_network_manager(internal_); + } + + return networkManager_; +} + +discord::OverlayManager& Core::OverlayManager() +{ + if (!overlayManager_.internal_) { + overlayManager_.internal_ = internal_->get_overlay_manager(internal_); + } + + return overlayManager_; +} + +discord::StorageManager& Core::StorageManager() +{ + if (!storageManager_.internal_) { + storageManager_.internal_ = internal_->get_storage_manager(internal_); + } + + return storageManager_; +} + +discord::StoreManager& Core::StoreManager() +{ + if (!storeManager_.internal_) { + storeManager_.internal_ = internal_->get_store_manager(internal_); + } + + return storeManager_; +} + +discord::VoiceManager& Core::VoiceManager() +{ + if (!voiceManager_.internal_) { + voiceManager_.internal_ = internal_->get_voice_manager(internal_); + } + + return voiceManager_; +} + +discord::AchievementManager& Core::AchievementManager() +{ + if (!achievementManager_.internal_) { + achievementManager_.internal_ = internal_->get_achievement_manager(internal_); + } + + return achievementManager_; +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/core.h b/libs/discordGameSDK/cpp/core.h new file mode 100644 index 0000000..8af6fca --- /dev/null +++ b/libs/discordGameSDK/cpp/core.h @@ -0,0 +1,64 @@ +#pragma once + +#include "types.h" +#include "application_manager.h" +#include "user_manager.h" +#include "image_manager.h" +#include "activity_manager.h" +#include "relationship_manager.h" +#include "lobby_manager.h" +#include "network_manager.h" +#include "overlay_manager.h" +#include "storage_manager.h" +#include "store_manager.h" +#include "voice_manager.h" +#include "achievement_manager.h" + +namespace discord { + +class Core final { +public: + static Result Create(ClientId clientId, std::uint64_t flags, Core** instance); + + ~Core(); + + Result RunCallbacks(); + void SetLogHook(LogLevel minLevel, std::function hook); + + discord::ApplicationManager& ApplicationManager(); + discord::UserManager& UserManager(); + discord::ImageManager& ImageManager(); + discord::ActivityManager& ActivityManager(); + discord::RelationshipManager& RelationshipManager(); + discord::LobbyManager& LobbyManager(); + discord::NetworkManager& NetworkManager(); + discord::OverlayManager& OverlayManager(); + discord::StorageManager& StorageManager(); + discord::StoreManager& StoreManager(); + discord::VoiceManager& VoiceManager(); + discord::AchievementManager& AchievementManager(); + +private: + Core() = default; + Core(Core const& rhs) = delete; + Core& operator=(Core const& rhs) = delete; + Core(Core&& rhs) = delete; + Core& operator=(Core&& rhs) = delete; + + IDiscordCore* internal_; + Event setLogHook_; + discord::ApplicationManager applicationManager_; + discord::UserManager userManager_; + discord::ImageManager imageManager_; + discord::ActivityManager activityManager_; + discord::RelationshipManager relationshipManager_; + discord::LobbyManager lobbyManager_; + discord::NetworkManager networkManager_; + discord::OverlayManager overlayManager_; + discord::StorageManager storageManager_; + discord::StoreManager storeManager_; + discord::VoiceManager voiceManager_; + discord::AchievementManager achievementManager_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/discord.h b/libs/discordGameSDK/cpp/discord.h new file mode 100644 index 0000000..c991212 --- /dev/null +++ b/libs/discordGameSDK/cpp/discord.h @@ -0,0 +1,16 @@ +#pragma once + +#include "types.h" +#include "core.h" +#include "application_manager.h" +#include "user_manager.h" +#include "image_manager.h" +#include "activity_manager.h" +#include "relationship_manager.h" +#include "lobby_manager.h" +#include "network_manager.h" +#include "overlay_manager.h" +#include "storage_manager.h" +#include "store_manager.h" +#include "voice_manager.h" +#include "achievement_manager.h" diff --git a/libs/discordGameSDK/cpp/event.h b/libs/discordGameSDK/cpp/event.h new file mode 100644 index 0000000..610887d --- /dev/null +++ b/libs/discordGameSDK/cpp/event.h @@ -0,0 +1,59 @@ +#pragma once + +#include +#include + +namespace discord { + +template +class Event final { +public: + using Token = int; + + Event() { slots_.reserve(4); } + + Event(Event const&) = default; + Event(Event&&) = default; + ~Event() = default; + + Event& operator=(Event const&) = default; + Event& operator=(Event&&) = default; + + template + Token Connect(EventHandler slot) + { + slots_.emplace_back(Slot{nextToken_, std::move(slot)}); + return nextToken_++; + } + + void Disconnect(Token token) + { + for (auto& slot : slots_) { + if (slot.token == token) { + slot = slots_.back(); + slots_.pop_back(); + break; + } + } + } + + void DisconnectAll() { slots_ = {}; } + + void operator()(Args... args) + { + for (auto const& slot : slots_) { + slot.fn(std::forward(args)...); + } + } + +private: + struct Slot { + Token token; + std::function fn; + }; + + Token nextToken_{}; + std::vector slots_{}; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/ffi.h b/libs/discordGameSDK/cpp/ffi.h new file mode 100644 index 0000000..765fde7 --- /dev/null +++ b/libs/discordGameSDK/cpp/ffi.h @@ -0,0 +1,942 @@ +#ifndef _DISCORD_GAME_SDK_H_ +#define _DISCORD_GAME_SDK_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#ifndef __cplusplus +#include +#endif + +#define DISCORD_VERSION 2 +#define DISCORD_APPLICATION_MANAGER_VERSION 1 +#define DISCORD_USER_MANAGER_VERSION 1 +#define DISCORD_IMAGE_MANAGER_VERSION 1 +#define DISCORD_ACTIVITY_MANAGER_VERSION 1 +#define DISCORD_RELATIONSHIP_MANAGER_VERSION 1 +#define DISCORD_LOBBY_MANAGER_VERSION 1 +#define DISCORD_NETWORK_MANAGER_VERSION 1 +#define DISCORD_OVERLAY_MANAGER_VERSION 1 +#define DISCORD_STORAGE_MANAGER_VERSION 1 +#define DISCORD_STORE_MANAGER_VERSION 1 +#define DISCORD_VOICE_MANAGER_VERSION 1 +#define DISCORD_ACHIEVEMENT_MANAGER_VERSION 1 + +enum EDiscordResult { + DiscordResult_Ok = 0, + DiscordResult_ServiceUnavailable = 1, + DiscordResult_InvalidVersion = 2, + DiscordResult_LockFailed = 3, + DiscordResult_InternalError = 4, + DiscordResult_InvalidPayload = 5, + DiscordResult_InvalidCommand = 6, + DiscordResult_InvalidPermissions = 7, + DiscordResult_NotFetched = 8, + DiscordResult_NotFound = 9, + DiscordResult_Conflict = 10, + DiscordResult_InvalidSecret = 11, + DiscordResult_InvalidJoinSecret = 12, + DiscordResult_NoEligibleActivity = 13, + DiscordResult_InvalidInvite = 14, + DiscordResult_NotAuthenticated = 15, + DiscordResult_InvalidAccessToken = 16, + DiscordResult_ApplicationMismatch = 17, + DiscordResult_InvalidDataUrl = 18, + DiscordResult_InvalidBase64 = 19, + DiscordResult_NotFiltered = 20, + DiscordResult_LobbyFull = 21, + DiscordResult_InvalidLobbySecret = 22, + DiscordResult_InvalidFilename = 23, + DiscordResult_InvalidFileSize = 24, + DiscordResult_InvalidEntitlement = 25, + DiscordResult_NotInstalled = 26, + DiscordResult_NotRunning = 27, + DiscordResult_InsufficientBuffer = 28, + DiscordResult_PurchaseCanceled = 29, + DiscordResult_InvalidGuild = 30, + DiscordResult_InvalidEvent = 31, + DiscordResult_InvalidChannel = 32, + DiscordResult_InvalidOrigin = 33, + DiscordResult_RateLimited = 34, + DiscordResult_OAuth2Error = 35, + DiscordResult_SelectChannelTimeout = 36, + DiscordResult_GetGuildTimeout = 37, + DiscordResult_SelectVoiceForceRequired = 38, + DiscordResult_CaptureShortcutAlreadyListening = 39, + DiscordResult_UnauthorizedForAchievement = 40, + DiscordResult_InvalidGiftCode = 41, + DiscordResult_PurchaseError = 42, + DiscordResult_TransactionAborted = 43, +}; + +enum EDiscordCreateFlags { + DiscordCreateFlags_Default = 0, + DiscordCreateFlags_NoRequireDiscord = 1, +}; + +enum EDiscordLogLevel { + DiscordLogLevel_Error = 1, + DiscordLogLevel_Warn, + DiscordLogLevel_Info, + DiscordLogLevel_Debug, +}; + +enum EDiscordUserFlag { + DiscordUserFlag_Partner = 2, + DiscordUserFlag_HypeSquadEvents = 4, + DiscordUserFlag_HypeSquadHouse1 = 64, + DiscordUserFlag_HypeSquadHouse2 = 128, + DiscordUserFlag_HypeSquadHouse3 = 256, +}; + +enum EDiscordPremiumType { + DiscordPremiumType_None = 0, + DiscordPremiumType_Tier1 = 1, + DiscordPremiumType_Tier2 = 2, +}; + +enum EDiscordImageType { + DiscordImageType_User, +}; + +enum EDiscordActivityType { + DiscordActivityType_Playing, + DiscordActivityType_Streaming, + DiscordActivityType_Listening, + DiscordActivityType_Watching, +}; + +enum EDiscordActivityActionType { + DiscordActivityActionType_Join = 1, + DiscordActivityActionType_Spectate, +}; + +enum EDiscordActivityJoinRequestReply { + DiscordActivityJoinRequestReply_No, + DiscordActivityJoinRequestReply_Yes, + DiscordActivityJoinRequestReply_Ignore, +}; + +enum EDiscordStatus { + DiscordStatus_Offline = 0, + DiscordStatus_Online = 1, + DiscordStatus_Idle = 2, + DiscordStatus_DoNotDisturb = 3, +}; + +enum EDiscordRelationshipType { + DiscordRelationshipType_None, + DiscordRelationshipType_Friend, + DiscordRelationshipType_Blocked, + DiscordRelationshipType_PendingIncoming, + DiscordRelationshipType_PendingOutgoing, + DiscordRelationshipType_Implicit, +}; + +enum EDiscordLobbyType { + DiscordLobbyType_Private = 1, + DiscordLobbyType_Public, +}; + +enum EDiscordLobbySearchComparison { + DiscordLobbySearchComparison_LessThanOrEqual = -2, + DiscordLobbySearchComparison_LessThan, + DiscordLobbySearchComparison_Equal, + DiscordLobbySearchComparison_GreaterThan, + DiscordLobbySearchComparison_GreaterThanOrEqual, + DiscordLobbySearchComparison_NotEqual, +}; + +enum EDiscordLobbySearchCast { + DiscordLobbySearchCast_String = 1, + DiscordLobbySearchCast_Number, +}; + +enum EDiscordLobbySearchDistance { + DiscordLobbySearchDistance_Local, + DiscordLobbySearchDistance_Default, + DiscordLobbySearchDistance_Extended, + DiscordLobbySearchDistance_Global, +}; + +enum EDiscordEntitlementType { + DiscordEntitlementType_Purchase = 1, + DiscordEntitlementType_PremiumSubscription, + DiscordEntitlementType_DeveloperGift, + DiscordEntitlementType_TestModePurchase, + DiscordEntitlementType_FreePurchase, + DiscordEntitlementType_UserGift, + DiscordEntitlementType_PremiumPurchase, +}; + +enum EDiscordSkuType { + DiscordSkuType_Application = 1, + DiscordSkuType_DLC, + DiscordSkuType_Consumable, + DiscordSkuType_Bundle, +}; + +enum EDiscordInputModeType { + DiscordInputModeType_VoiceActivity = 0, + DiscordInputModeType_PushToTalk, +}; + +typedef int64_t DiscordClientId; +typedef int32_t DiscordVersion; +typedef int64_t DiscordSnowflake; +typedef int64_t DiscordTimestamp; +typedef DiscordSnowflake DiscordUserId; +typedef char DiscordLocale[128]; +typedef char DiscordBranch[4096]; +typedef DiscordSnowflake DiscordLobbyId; +typedef char DiscordLobbySecret[128]; +typedef char DiscordMetadataKey[256]; +typedef char DiscordMetadataValue[4096]; +typedef uint64_t DiscordNetworkPeerId; +typedef uint8_t DiscordNetworkChannelId; +typedef char DiscordPath[4096]; +typedef char DiscordDateTime[64]; + +struct DiscordUser { + DiscordUserId id; + char username[256]; + char discriminator[8]; + char avatar[128]; + bool bot; +}; + +struct DiscordOAuth2Token { + char access_token[128]; + char scopes[1024]; + DiscordTimestamp expires; +}; + +struct DiscordImageHandle { + enum EDiscordImageType type; + int64_t id; + uint32_t size; +}; + +struct DiscordImageDimensions { + uint32_t width; + uint32_t height; +}; + +struct DiscordActivityTimestamps { + DiscordTimestamp start; + DiscordTimestamp end; +}; + +struct DiscordActivityAssets { + char large_image[128]; + char large_text[128]; + char small_image[128]; + char small_text[128]; +}; + +struct DiscordPartySize { + int32_t current_size; + int32_t max_size; +}; + +struct DiscordActivityParty { + char id[128]; + struct DiscordPartySize size; +}; + +struct DiscordActivitySecrets { + char match[128]; + char join[128]; + char spectate[128]; +}; + +struct DiscordActivity { + enum EDiscordActivityType type; + int64_t application_id; + char name[128]; + char state[128]; + char details[128]; + struct DiscordActivityTimestamps timestamps; + struct DiscordActivityAssets assets; + struct DiscordActivityParty party; + struct DiscordActivitySecrets secrets; + bool instance; +}; + +struct DiscordPresence { + enum EDiscordStatus status; + struct DiscordActivity activity; +}; + +struct DiscordRelationship { + enum EDiscordRelationshipType type; + struct DiscordUser user; + struct DiscordPresence presence; +}; + +struct DiscordLobby { + DiscordLobbyId id; + enum EDiscordLobbyType type; + DiscordUserId owner_id; + DiscordLobbySecret secret; + uint32_t capacity; + bool locked; +}; + +struct DiscordFileStat { + char filename[260]; + uint64_t size; + uint64_t last_modified; +}; + +struct DiscordEntitlement { + DiscordSnowflake id; + enum EDiscordEntitlementType type; + DiscordSnowflake sku_id; +}; + +struct DiscordSkuPrice { + uint32_t amount; + char currency[16]; +}; + +struct DiscordSku { + DiscordSnowflake id; + enum EDiscordSkuType type; + char name[256]; + struct DiscordSkuPrice price; +}; + +struct DiscordInputMode { + enum EDiscordInputModeType type; + char shortcut[256]; +}; + +struct DiscordUserAchievement { + DiscordSnowflake user_id; + DiscordSnowflake achievement_id; + uint8_t percent_complete; + DiscordDateTime unlocked_at; +}; + +struct IDiscordLobbyTransaction { + enum EDiscordResult (*set_type)(struct IDiscordLobbyTransaction* lobby_transaction, + enum EDiscordLobbyType type); + enum EDiscordResult (*set_owner)(struct IDiscordLobbyTransaction* lobby_transaction, + DiscordUserId owner_id); + enum EDiscordResult (*set_capacity)(struct IDiscordLobbyTransaction* lobby_transaction, + uint32_t capacity); + enum EDiscordResult (*set_metadata)(struct IDiscordLobbyTransaction* lobby_transaction, + DiscordMetadataKey key, + DiscordMetadataValue value); + enum EDiscordResult (*delete_metadata)(struct IDiscordLobbyTransaction* lobby_transaction, + DiscordMetadataKey key); + enum EDiscordResult (*set_locked)(struct IDiscordLobbyTransaction* lobby_transaction, + bool locked); +}; + +struct IDiscordLobbyMemberTransaction { + enum EDiscordResult (*set_metadata)( + struct IDiscordLobbyMemberTransaction* lobby_member_transaction, + DiscordMetadataKey key, + DiscordMetadataValue value); + enum EDiscordResult (*delete_metadata)( + struct IDiscordLobbyMemberTransaction* lobby_member_transaction, + DiscordMetadataKey key); +}; + +struct IDiscordLobbySearchQuery { + enum EDiscordResult (*filter)(struct IDiscordLobbySearchQuery* lobby_search_query, + DiscordMetadataKey key, + enum EDiscordLobbySearchComparison comparison, + enum EDiscordLobbySearchCast cast, + DiscordMetadataValue value); + enum EDiscordResult (*sort)(struct IDiscordLobbySearchQuery* lobby_search_query, + DiscordMetadataKey key, + enum EDiscordLobbySearchCast cast, + DiscordMetadataValue value); + enum EDiscordResult (*limit)(struct IDiscordLobbySearchQuery* lobby_search_query, + uint32_t limit); + enum EDiscordResult (*distance)(struct IDiscordLobbySearchQuery* lobby_search_query, + enum EDiscordLobbySearchDistance distance); +}; + +typedef void* IDiscordApplicationEvents; + +struct IDiscordApplicationManager { + void (*validate_or_exit)(struct IDiscordApplicationManager* manager, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*get_current_locale)(struct IDiscordApplicationManager* manager, DiscordLocale* locale); + void (*get_current_branch)(struct IDiscordApplicationManager* manager, DiscordBranch* branch); + void (*get_oauth2_token)(struct IDiscordApplicationManager* manager, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result, + struct DiscordOAuth2Token* oauth2_token)); + void (*get_ticket)(struct IDiscordApplicationManager* manager, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result, + const char* data)); +}; + +struct IDiscordUserEvents { + void (*on_current_user_update)(void* event_data); +}; + +struct IDiscordUserManager { + enum EDiscordResult (*get_current_user)(struct IDiscordUserManager* manager, + struct DiscordUser* current_user); + void (*get_user)(struct IDiscordUserManager* manager, + DiscordUserId user_id, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result, + struct DiscordUser* user)); + enum EDiscordResult (*get_current_user_premium_type)(struct IDiscordUserManager* manager, + enum EDiscordPremiumType* premium_type); + enum EDiscordResult (*current_user_has_flag)(struct IDiscordUserManager* manager, + enum EDiscordUserFlag flag, + bool* has_flag); +}; + +typedef void* IDiscordImageEvents; + +struct IDiscordImageManager { + void (*fetch)(struct IDiscordImageManager* manager, + struct DiscordImageHandle handle, + bool refresh, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result, + struct DiscordImageHandle handle_result)); + enum EDiscordResult (*get_dimensions)(struct IDiscordImageManager* manager, + struct DiscordImageHandle handle, + struct DiscordImageDimensions* dimensions); + enum EDiscordResult (*get_data)(struct IDiscordImageManager* manager, + struct DiscordImageHandle handle, + uint8_t* data, + uint32_t data_length); +}; + +struct IDiscordActivityEvents { + void (*on_activity_join)(void* event_data, const char* secret); + void (*on_activity_spectate)(void* event_data, const char* secret); + void (*on_activity_join_request)(void* event_data, struct DiscordUser* user); + void (*on_activity_invite)(void* event_data, + enum EDiscordActivityActionType type, + struct DiscordUser* user, + struct DiscordActivity* activity); +}; + +struct IDiscordActivityManager { + enum EDiscordResult (*register_command)(struct IDiscordActivityManager* manager, + const char* command); + enum EDiscordResult (*register_steam)(struct IDiscordActivityManager* manager, + uint32_t steam_id); + void (*update_activity)(struct IDiscordActivityManager* manager, + struct DiscordActivity* activity, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*clear_activity)(struct IDiscordActivityManager* manager, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*send_request_reply)(struct IDiscordActivityManager* manager, + DiscordUserId user_id, + enum EDiscordActivityJoinRequestReply reply, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*send_invite)(struct IDiscordActivityManager* manager, + DiscordUserId user_id, + enum EDiscordActivityActionType type, + const char* content, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*accept_invite)(struct IDiscordActivityManager* manager, + DiscordUserId user_id, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); +}; + +struct IDiscordRelationshipEvents { + void (*on_refresh)(void* event_data); + void (*on_relationship_update)(void* event_data, struct DiscordRelationship* relationship); +}; + +struct IDiscordRelationshipManager { + void (*filter)(struct IDiscordRelationshipManager* manager, + void* filter_data, + bool (*filter)(void* filter_data, struct DiscordRelationship* relationship)); + enum EDiscordResult (*count)(struct IDiscordRelationshipManager* manager, int32_t* count); + enum EDiscordResult (*get)(struct IDiscordRelationshipManager* manager, + DiscordUserId user_id, + struct DiscordRelationship* relationship); + enum EDiscordResult (*get_at)(struct IDiscordRelationshipManager* manager, + uint32_t index, + struct DiscordRelationship* relationship); +}; + +struct IDiscordLobbyEvents { + void (*on_lobby_update)(void* event_data, int64_t lobby_id); + void (*on_lobby_delete)(void* event_data, int64_t lobby_id, uint32_t reason); + void (*on_member_connect)(void* event_data, int64_t lobby_id, int64_t user_id); + void (*on_member_update)(void* event_data, int64_t lobby_id, int64_t user_id); + void (*on_member_disconnect)(void* event_data, int64_t lobby_id, int64_t user_id); + void (*on_lobby_message)(void* event_data, + int64_t lobby_id, + int64_t user_id, + uint8_t* data, + uint32_t data_length); + void (*on_speaking)(void* event_data, int64_t lobby_id, int64_t user_id, bool speaking); + void (*on_network_message)(void* event_data, + int64_t lobby_id, + int64_t user_id, + uint8_t channel_id, + uint8_t* data, + uint32_t data_length); +}; + +struct IDiscordLobbyManager { + enum EDiscordResult (*get_lobby_create_transaction)( + struct IDiscordLobbyManager* manager, + struct IDiscordLobbyTransaction** transaction); + enum EDiscordResult (*get_lobby_update_transaction)( + struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + struct IDiscordLobbyTransaction** transaction); + enum EDiscordResult (*get_member_update_transaction)( + struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordUserId user_id, + struct IDiscordLobbyMemberTransaction** transaction); + void (*create_lobby)(struct IDiscordLobbyManager* manager, + struct IDiscordLobbyTransaction* transaction, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result, + struct DiscordLobby* lobby)); + void (*update_lobby)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + struct IDiscordLobbyTransaction* transaction, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*delete_lobby)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*connect_lobby)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordLobbySecret secret, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result, + struct DiscordLobby* lobby)); + void (*connect_lobby_with_activity_secret)(struct IDiscordLobbyManager* manager, + DiscordLobbySecret activity_secret, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result, + struct DiscordLobby* lobby)); + void (*disconnect_lobby)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*get_lobby)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + struct DiscordLobby* lobby); + enum EDiscordResult (*get_lobby_activity_secret)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordLobbySecret* secret); + enum EDiscordResult (*get_lobby_metadata_value)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordMetadataKey key, + DiscordMetadataValue* value); + enum EDiscordResult (*get_lobby_metadata_key)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + int32_t index, + DiscordMetadataKey* key); + enum EDiscordResult (*lobby_metadata_count)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + int32_t* count); + enum EDiscordResult (*member_count)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + int32_t* count); + enum EDiscordResult (*get_member_user_id)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + int32_t index, + DiscordUserId* user_id); + enum EDiscordResult (*get_member_user)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordUserId user_id, + struct DiscordUser* user); + enum EDiscordResult (*get_member_metadata_value)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordUserId user_id, + DiscordMetadataKey key, + DiscordMetadataValue* value); + enum EDiscordResult (*get_member_metadata_key)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordUserId user_id, + int32_t index, + DiscordMetadataKey* key); + enum EDiscordResult (*member_metadata_count)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordUserId user_id, + int32_t* count); + void (*update_member)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordUserId user_id, + struct IDiscordLobbyMemberTransaction* transaction, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*send_lobby_message)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + uint8_t* data, + uint32_t data_length, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*get_search_query)(struct IDiscordLobbyManager* manager, + struct IDiscordLobbySearchQuery** query); + void (*search)(struct IDiscordLobbyManager* manager, + struct IDiscordLobbySearchQuery* query, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*lobby_count)(struct IDiscordLobbyManager* manager, int32_t* count); + enum EDiscordResult (*get_lobby_id)(struct IDiscordLobbyManager* manager, + int32_t index, + DiscordLobbyId* lobby_id); + void (*connect_voice)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*disconnect_voice)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*connect_network)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id); + enum EDiscordResult (*disconnect_network)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id); + enum EDiscordResult (*flush_network)(struct IDiscordLobbyManager* manager); + enum EDiscordResult (*open_network_channel)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + uint8_t channel_id, + bool reliable); + enum EDiscordResult (*send_network_message)(struct IDiscordLobbyManager* manager, + DiscordLobbyId lobby_id, + DiscordUserId user_id, + uint8_t channel_id, + uint8_t* data, + uint32_t data_length); +}; + +struct IDiscordNetworkEvents { + void (*on_message)(void* event_data, + DiscordNetworkPeerId peer_id, + DiscordNetworkChannelId channel_id, + uint8_t* data, + uint32_t data_length); + void (*on_route_update)(void* event_data, const char* route_data); +}; + +struct IDiscordNetworkManager { + /** + * Get the local peer ID for this process. + */ + void (*get_peer_id)(struct IDiscordNetworkManager* manager, DiscordNetworkPeerId* peer_id); + /** + * Send pending network messages. + */ + enum EDiscordResult (*flush)(struct IDiscordNetworkManager* manager); + /** + * Open a connection to a remote peer. + */ + enum EDiscordResult (*open_peer)(struct IDiscordNetworkManager* manager, + DiscordNetworkPeerId peer_id, + const char* route_data); + /** + * Update the route data for a connected peer. + */ + enum EDiscordResult (*update_peer)(struct IDiscordNetworkManager* manager, + DiscordNetworkPeerId peer_id, + const char* route_data); + /** + * Close the connection to a remote peer. + */ + enum EDiscordResult (*close_peer)(struct IDiscordNetworkManager* manager, + DiscordNetworkPeerId peer_id); + /** + * Open a message channel to a connected peer. + */ + enum EDiscordResult (*open_channel)(struct IDiscordNetworkManager* manager, + DiscordNetworkPeerId peer_id, + DiscordNetworkChannelId channel_id, + bool reliable); + /** + * Close a message channel to a connected peer. + */ + enum EDiscordResult (*close_channel)(struct IDiscordNetworkManager* manager, + DiscordNetworkPeerId peer_id, + DiscordNetworkChannelId channel_id); + /** + * Send a message to a connected peer over an opened message channel. + */ + enum EDiscordResult (*send_message)(struct IDiscordNetworkManager* manager, + DiscordNetworkPeerId peer_id, + DiscordNetworkChannelId channel_id, + uint8_t* data, + uint32_t data_length); +}; + +struct IDiscordOverlayEvents { + void (*on_toggle)(void* event_data, bool locked); +}; + +struct IDiscordOverlayManager { + void (*is_enabled)(struct IDiscordOverlayManager* manager, bool* enabled); + void (*is_locked)(struct IDiscordOverlayManager* manager, bool* locked); + void (*set_locked)(struct IDiscordOverlayManager* manager, + bool locked, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*open_activity_invite)(struct IDiscordOverlayManager* manager, + enum EDiscordActivityActionType type, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*open_guild_invite)(struct IDiscordOverlayManager* manager, + const char* code, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*open_voice_settings)(struct IDiscordOverlayManager* manager, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); +}; + +typedef void* IDiscordStorageEvents; + +struct IDiscordStorageManager { + enum EDiscordResult (*read)(struct IDiscordStorageManager* manager, + const char* name, + uint8_t* data, + uint32_t data_length, + uint32_t* read); + void (*read_async)(struct IDiscordStorageManager* manager, + const char* name, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result, + uint8_t* data, + uint32_t data_length)); + void (*read_async_partial)(struct IDiscordStorageManager* manager, + const char* name, + uint64_t offset, + uint64_t length, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result, + uint8_t* data, + uint32_t data_length)); + enum EDiscordResult (*write)(struct IDiscordStorageManager* manager, + const char* name, + uint8_t* data, + uint32_t data_length); + void (*write_async)(struct IDiscordStorageManager* manager, + const char* name, + uint8_t* data, + uint32_t data_length, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*delete_)(struct IDiscordStorageManager* manager, const char* name); + enum EDiscordResult (*exists)(struct IDiscordStorageManager* manager, + const char* name, + bool* exists); + void (*count)(struct IDiscordStorageManager* manager, int32_t* count); + enum EDiscordResult (*stat)(struct IDiscordStorageManager* manager, + const char* name, + struct DiscordFileStat* stat); + enum EDiscordResult (*stat_at)(struct IDiscordStorageManager* manager, + int32_t index, + struct DiscordFileStat* stat); + enum EDiscordResult (*get_path)(struct IDiscordStorageManager* manager, DiscordPath* path); +}; + +struct IDiscordStoreEvents { + void (*on_entitlement_create)(void* event_data, struct DiscordEntitlement* entitlement); + void (*on_entitlement_delete)(void* event_data, struct DiscordEntitlement* entitlement); +}; + +struct IDiscordStoreManager { + void (*fetch_skus)(struct IDiscordStoreManager* manager, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*count_skus)(struct IDiscordStoreManager* manager, int32_t* count); + enum EDiscordResult (*get_sku)(struct IDiscordStoreManager* manager, + DiscordSnowflake sku_id, + struct DiscordSku* sku); + enum EDiscordResult (*get_sku_at)(struct IDiscordStoreManager* manager, + int32_t index, + struct DiscordSku* sku); + void (*fetch_entitlements)(struct IDiscordStoreManager* manager, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*count_entitlements)(struct IDiscordStoreManager* manager, int32_t* count); + enum EDiscordResult (*get_entitlement)(struct IDiscordStoreManager* manager, + DiscordSnowflake entitlement_id, + struct DiscordEntitlement* entitlement); + enum EDiscordResult (*get_entitlement_at)(struct IDiscordStoreManager* manager, + int32_t index, + struct DiscordEntitlement* entitlement); + enum EDiscordResult (*has_sku_entitlement)(struct IDiscordStoreManager* manager, + DiscordSnowflake sku_id, + bool* has_entitlement); + void (*start_purchase)(struct IDiscordStoreManager* manager, + DiscordSnowflake sku_id, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); +}; + +struct IDiscordVoiceEvents { + void (*on_settings_update)(void* event_data); +}; + +struct IDiscordVoiceManager { + enum EDiscordResult (*get_input_mode)(struct IDiscordVoiceManager* manager, + struct DiscordInputMode* input_mode); + void (*set_input_mode)(struct IDiscordVoiceManager* manager, + struct DiscordInputMode input_mode, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + enum EDiscordResult (*is_self_mute)(struct IDiscordVoiceManager* manager, bool* mute); + enum EDiscordResult (*set_self_mute)(struct IDiscordVoiceManager* manager, bool mute); + enum EDiscordResult (*is_self_deaf)(struct IDiscordVoiceManager* manager, bool* deaf); + enum EDiscordResult (*set_self_deaf)(struct IDiscordVoiceManager* manager, bool deaf); + enum EDiscordResult (*is_local_mute)(struct IDiscordVoiceManager* manager, + DiscordSnowflake user_id, + bool* mute); + enum EDiscordResult (*set_local_mute)(struct IDiscordVoiceManager* manager, + DiscordSnowflake user_id, + bool mute); + enum EDiscordResult (*get_local_volume)(struct IDiscordVoiceManager* manager, + DiscordSnowflake user_id, + uint8_t* volume); + enum EDiscordResult (*set_local_volume)(struct IDiscordVoiceManager* manager, + DiscordSnowflake user_id, + uint8_t volume); +}; + +struct IDiscordAchievementEvents { + void (*on_user_achievement_update)(void* event_data, + struct DiscordUserAchievement* user_achievement); +}; + +struct IDiscordAchievementManager { + void (*set_user_achievement)(struct IDiscordAchievementManager* manager, + DiscordSnowflake achievement_id, + uint8_t percent_complete, + void* callback_data, + void (*callback)(void* callback_data, enum EDiscordResult result)); + void (*fetch_user_achievements)(struct IDiscordAchievementManager* manager, + void* callback_data, + void (*callback)(void* callback_data, + enum EDiscordResult result)); + void (*count_user_achievements)(struct IDiscordAchievementManager* manager, int32_t* count); + enum EDiscordResult (*get_user_achievement)(struct IDiscordAchievementManager* manager, + DiscordSnowflake user_achievement_id, + struct DiscordUserAchievement* user_achievement); + enum EDiscordResult (*get_user_achievement_at)(struct IDiscordAchievementManager* manager, + int32_t index, + struct DiscordUserAchievement* user_achievement); +}; + +typedef void* IDiscordCoreEvents; + +struct IDiscordCore { + void (*destroy)(struct IDiscordCore* core); + enum EDiscordResult (*run_callbacks)(struct IDiscordCore* core); + void (*set_log_hook)(struct IDiscordCore* core, + enum EDiscordLogLevel min_level, + void* hook_data, + void (*hook)(void* hook_data, + enum EDiscordLogLevel level, + const char* message)); + struct IDiscordApplicationManager* (*get_application_manager)(struct IDiscordCore* core); + struct IDiscordUserManager* (*get_user_manager)(struct IDiscordCore* core); + struct IDiscordImageManager* (*get_image_manager)(struct IDiscordCore* core); + struct IDiscordActivityManager* (*get_activity_manager)(struct IDiscordCore* core); + struct IDiscordRelationshipManager* (*get_relationship_manager)(struct IDiscordCore* core); + struct IDiscordLobbyManager* (*get_lobby_manager)(struct IDiscordCore* core); + struct IDiscordNetworkManager* (*get_network_manager)(struct IDiscordCore* core); + struct IDiscordOverlayManager* (*get_overlay_manager)(struct IDiscordCore* core); + struct IDiscordStorageManager* (*get_storage_manager)(struct IDiscordCore* core); + struct IDiscordStoreManager* (*get_store_manager)(struct IDiscordCore* core); + struct IDiscordVoiceManager* (*get_voice_manager)(struct IDiscordCore* core); + struct IDiscordAchievementManager* (*get_achievement_manager)(struct IDiscordCore* core); +}; + +struct DiscordCreateParams { + DiscordClientId client_id; + uint64_t flags; + IDiscordCoreEvents* events; + void* event_data; + IDiscordApplicationEvents* application_events; + DiscordVersion application_version; + struct IDiscordUserEvents* user_events; + DiscordVersion user_version; + IDiscordImageEvents* image_events; + DiscordVersion image_version; + struct IDiscordActivityEvents* activity_events; + DiscordVersion activity_version; + struct IDiscordRelationshipEvents* relationship_events; + DiscordVersion relationship_version; + struct IDiscordLobbyEvents* lobby_events; + DiscordVersion lobby_version; + struct IDiscordNetworkEvents* network_events; + DiscordVersion network_version; + struct IDiscordOverlayEvents* overlay_events; + DiscordVersion overlay_version; + IDiscordStorageEvents* storage_events; + DiscordVersion storage_version; + struct IDiscordStoreEvents* store_events; + DiscordVersion store_version; + struct IDiscordVoiceEvents* voice_events; + DiscordVersion voice_version; + struct IDiscordAchievementEvents* achievement_events; + DiscordVersion achievement_version; +}; + +#ifdef __cplusplus +inline +#else +static +#endif + void + DiscordCreateParamsSetDefault(struct DiscordCreateParams* params) +{ + memset(params, 0, sizeof(struct DiscordCreateParams)); + params->application_version = DISCORD_APPLICATION_MANAGER_VERSION; + params->user_version = DISCORD_USER_MANAGER_VERSION; + params->image_version = DISCORD_IMAGE_MANAGER_VERSION; + params->activity_version = DISCORD_ACTIVITY_MANAGER_VERSION; + params->relationship_version = DISCORD_RELATIONSHIP_MANAGER_VERSION; + params->lobby_version = DISCORD_LOBBY_MANAGER_VERSION; + params->network_version = DISCORD_NETWORK_MANAGER_VERSION; + params->overlay_version = DISCORD_OVERLAY_MANAGER_VERSION; + params->storage_version = DISCORD_STORAGE_MANAGER_VERSION; + params->store_version = DISCORD_STORE_MANAGER_VERSION; + params->voice_version = DISCORD_VOICE_MANAGER_VERSION; + params->achievement_version = DISCORD_ACHIEVEMENT_MANAGER_VERSION; +} + +enum EDiscordResult DiscordCreate(DiscordVersion version, + struct DiscordCreateParams* params, + struct IDiscordCore** result); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libs/discordGameSDK/cpp/image_manager.cpp b/libs/discordGameSDK/cpp/image_manager.cpp new file mode 100644 index 0000000..03b1db4 --- /dev/null +++ b/libs/discordGameSDK/cpp/image_manager.cpp @@ -0,0 +1,57 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "image_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +void ImageManager::Fetch(ImageHandle handle, + bool refresh, + std::function callback) +{ + static auto wrapper = + [](void* callbackData, EDiscordResult result, DiscordImageHandle handleResult) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result), *reinterpret_cast(&handleResult)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->fetch(internal_, + *reinterpret_cast(&handle), + (refresh ? 1 : 0), + cb.release(), + wrapper); +} + +Result ImageManager::GetDimensions(ImageHandle handle, ImageDimensions* dimensions) +{ + if (!dimensions) { + return Result::InternalError; + } + + auto result = internal_->get_dimensions(internal_, + *reinterpret_cast(&handle), + reinterpret_cast(dimensions)); + return static_cast(result); +} + +Result ImageManager::GetData(ImageHandle handle, std::uint8_t* data, std::uint32_t dataLength) +{ + auto result = internal_->get_data(internal_, + *reinterpret_cast(&handle), + reinterpret_cast(data), + dataLength); + return static_cast(result); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/image_manager.h b/libs/discordGameSDK/cpp/image_manager.h new file mode 100644 index 0000000..b096b17 --- /dev/null +++ b/libs/discordGameSDK/cpp/image_manager.h @@ -0,0 +1,28 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class ImageManager final { +public: + ~ImageManager() = default; + + void Fetch(ImageHandle handle, bool refresh, std::function callback); + Result GetDimensions(ImageHandle handle, ImageDimensions* dimensions); + Result GetData(ImageHandle handle, std::uint8_t* data, std::uint32_t dataLength); + +private: + friend class Core; + + ImageManager() = default; + ImageManager(ImageManager const& rhs) = delete; + ImageManager& operator=(ImageManager const& rhs) = delete; + ImageManager(ImageManager&& rhs) = delete; + ImageManager& operator=(ImageManager&& rhs) = delete; + + IDiscordImageManager* internal_; + static IDiscordImageEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/lobby_manager.cpp b/libs/discordGameSDK/cpp/lobby_manager.cpp new file mode 100644 index 0000000..6bf1a1a --- /dev/null +++ b/libs/discordGameSDK/cpp/lobby_manager.cpp @@ -0,0 +1,547 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "lobby_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +class LobbyEvents final { +public: + static void OnLobbyUpdate(void* callbackData, int64_t lobbyId) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->LobbyManager(); + module.OnLobbyUpdate(lobbyId); + } + + static void OnLobbyDelete(void* callbackData, int64_t lobbyId, uint32_t reason) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->LobbyManager(); + module.OnLobbyDelete(lobbyId, reason); + } + + static void OnMemberConnect(void* callbackData, int64_t lobbyId, int64_t userId) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->LobbyManager(); + module.OnMemberConnect(lobbyId, userId); + } + + static void OnMemberUpdate(void* callbackData, int64_t lobbyId, int64_t userId) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->LobbyManager(); + module.OnMemberUpdate(lobbyId, userId); + } + + static void OnMemberDisconnect(void* callbackData, int64_t lobbyId, int64_t userId) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->LobbyManager(); + module.OnMemberDisconnect(lobbyId, userId); + } + + static void OnLobbyMessage(void* callbackData, + int64_t lobbyId, + int64_t userId, + uint8_t* data, + uint32_t dataLength) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->LobbyManager(); + module.OnLobbyMessage(lobbyId, userId, data, dataLength); + } + + static void OnSpeaking(void* callbackData, int64_t lobbyId, int64_t userId, bool speaking) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->LobbyManager(); + module.OnSpeaking(lobbyId, userId, (speaking != 0)); + } + + static void OnNetworkMessage(void* callbackData, + int64_t lobbyId, + int64_t userId, + uint8_t channelId, + uint8_t* data, + uint32_t dataLength) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->LobbyManager(); + module.OnNetworkMessage(lobbyId, userId, channelId, data, dataLength); + } +}; + +IDiscordLobbyEvents LobbyManager::events_{ + &LobbyEvents::OnLobbyUpdate, + &LobbyEvents::OnLobbyDelete, + &LobbyEvents::OnMemberConnect, + &LobbyEvents::OnMemberUpdate, + &LobbyEvents::OnMemberDisconnect, + &LobbyEvents::OnLobbyMessage, + &LobbyEvents::OnSpeaking, + &LobbyEvents::OnNetworkMessage, +}; + +Result LobbyManager::GetLobbyCreateTransaction(LobbyTransaction* transaction) +{ + if (!transaction) { + return Result::InternalError; + } + + auto result = internal_->get_lobby_create_transaction(internal_, transaction->Receive()); + return static_cast(result); +} + +Result LobbyManager::GetLobbyUpdateTransaction(LobbyId lobbyId, LobbyTransaction* transaction) +{ + if (!transaction) { + return Result::InternalError; + } + + auto result = + internal_->get_lobby_update_transaction(internal_, lobbyId, transaction->Receive()); + return static_cast(result); +} + +Result LobbyManager::GetMemberUpdateTransaction(LobbyId lobbyId, + UserId userId, + LobbyMemberTransaction* transaction) +{ + if (!transaction) { + return Result::InternalError; + } + + auto result = + internal_->get_member_update_transaction(internal_, lobbyId, userId, transaction->Receive()); + return static_cast(result); +} + +void LobbyManager::CreateLobby(LobbyTransaction const& transaction, + std::function callback) +{ + static auto wrapper = + [](void* callbackData, EDiscordResult result, DiscordLobby* lobby) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result), *reinterpret_cast(lobby)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->create_lobby( + internal_, const_cast(transaction).Internal(), cb.release(), wrapper); +} + +void LobbyManager::UpdateLobby(LobbyId lobbyId, + LobbyTransaction const& transaction, + std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->update_lobby(internal_, + lobbyId, + const_cast(transaction).Internal(), + cb.release(), + wrapper); +} + +void LobbyManager::DeleteLobby(LobbyId lobbyId, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->delete_lobby(internal_, lobbyId, cb.release(), wrapper); +} + +void LobbyManager::ConnectLobby(LobbyId lobbyId, + LobbySecret secret, + std::function callback) +{ + static auto wrapper = + [](void* callbackData, EDiscordResult result, DiscordLobby* lobby) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result), *reinterpret_cast(lobby)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->connect_lobby(internal_, lobbyId, const_cast(secret), cb.release(), wrapper); +} + +void LobbyManager::ConnectLobbyWithActivitySecret( + LobbySecret activitySecret, + std::function callback) +{ + static auto wrapper = + [](void* callbackData, EDiscordResult result, DiscordLobby* lobby) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result), *reinterpret_cast(lobby)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->connect_lobby_with_activity_secret( + internal_, const_cast(activitySecret), cb.release(), wrapper); +} + +void LobbyManager::DisconnectLobby(LobbyId lobbyId, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->disconnect_lobby(internal_, lobbyId, cb.release(), wrapper); +} + +Result LobbyManager::GetLobby(LobbyId lobbyId, Lobby* lobby) +{ + if (!lobby) { + return Result::InternalError; + } + + auto result = internal_->get_lobby(internal_, lobbyId, reinterpret_cast(lobby)); + return static_cast(result); +} + +Result LobbyManager::GetLobbyActivitySecret(LobbyId lobbyId, char secret[128]) +{ + if (!secret) { + return Result::InternalError; + } + + auto result = internal_->get_lobby_activity_secret( + internal_, lobbyId, reinterpret_cast(secret)); + return static_cast(result); +} + +Result LobbyManager::GetLobbyMetadataValue(LobbyId lobbyId, MetadataKey key, char value[4096]) +{ + if (!value) { + return Result::InternalError; + } + + auto result = internal_->get_lobby_metadata_value( + internal_, lobbyId, const_cast(key), reinterpret_cast(value)); + return static_cast(result); +} + +Result LobbyManager::GetLobbyMetadataKey(LobbyId lobbyId, std::int32_t index, char key[256]) +{ + if (!key) { + return Result::InternalError; + } + + auto result = internal_->get_lobby_metadata_key( + internal_, lobbyId, index, reinterpret_cast(key)); + return static_cast(result); +} + +Result LobbyManager::LobbyMetadataCount(LobbyId lobbyId, std::int32_t* count) +{ + if (!count) { + return Result::InternalError; + } + + auto result = + internal_->lobby_metadata_count(internal_, lobbyId, reinterpret_cast(count)); + return static_cast(result); +} + +Result LobbyManager::MemberCount(LobbyId lobbyId, std::int32_t* count) +{ + if (!count) { + return Result::InternalError; + } + + auto result = internal_->member_count(internal_, lobbyId, reinterpret_cast(count)); + return static_cast(result); +} + +Result LobbyManager::GetMemberUserId(LobbyId lobbyId, std::int32_t index, UserId* userId) +{ + if (!userId) { + return Result::InternalError; + } + + auto result = + internal_->get_member_user_id(internal_, lobbyId, index, reinterpret_cast(userId)); + return static_cast(result); +} + +Result LobbyManager::GetMemberUser(LobbyId lobbyId, UserId userId, User* user) +{ + if (!user) { + return Result::InternalError; + } + + auto result = + internal_->get_member_user(internal_, lobbyId, userId, reinterpret_cast(user)); + return static_cast(result); +} + +Result LobbyManager::GetMemberMetadataValue(LobbyId lobbyId, + UserId userId, + MetadataKey key, + char value[4096]) +{ + if (!value) { + return Result::InternalError; + } + + auto result = + internal_->get_member_metadata_value(internal_, + lobbyId, + userId, + const_cast(key), + reinterpret_cast(value)); + return static_cast(result); +} + +Result LobbyManager::GetMemberMetadataKey(LobbyId lobbyId, + UserId userId, + std::int32_t index, + char key[256]) +{ + if (!key) { + return Result::InternalError; + } + + auto result = internal_->get_member_metadata_key( + internal_, lobbyId, userId, index, reinterpret_cast(key)); + return static_cast(result); +} + +Result LobbyManager::MemberMetadataCount(LobbyId lobbyId, UserId userId, std::int32_t* count) +{ + if (!count) { + return Result::InternalError; + } + + auto result = internal_->member_metadata_count( + internal_, lobbyId, userId, reinterpret_cast(count)); + return static_cast(result); +} + +void LobbyManager::UpdateMember(LobbyId lobbyId, + UserId userId, + LobbyMemberTransaction const& transaction, + std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->update_member(internal_, + lobbyId, + userId, + const_cast(transaction).Internal(), + cb.release(), + wrapper); +} + +void LobbyManager::SendLobbyMessage(LobbyId lobbyId, + std::uint8_t* data, + std::uint32_t dataLength, + std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->send_lobby_message( + internal_, lobbyId, reinterpret_cast(data), dataLength, cb.release(), wrapper); +} + +Result LobbyManager::GetSearchQuery(LobbySearchQuery* query) +{ + if (!query) { + return Result::InternalError; + } + + auto result = internal_->get_search_query(internal_, query->Receive()); + return static_cast(result); +} + +void LobbyManager::Search(LobbySearchQuery const& query, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->search( + internal_, const_cast(query).Internal(), cb.release(), wrapper); +} + +void LobbyManager::LobbyCount(std::int32_t* count) +{ + if (!count) { + return; + } + + internal_->lobby_count(internal_, reinterpret_cast(count)); +} + +Result LobbyManager::GetLobbyId(std::int32_t index, LobbyId* lobbyId) +{ + if (!lobbyId) { + return Result::InternalError; + } + + auto result = internal_->get_lobby_id(internal_, index, reinterpret_cast(lobbyId)); + return static_cast(result); +} + +void LobbyManager::ConnectVoice(LobbyId lobbyId, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->connect_voice(internal_, lobbyId, cb.release(), wrapper); +} + +void LobbyManager::DisconnectVoice(LobbyId lobbyId, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->disconnect_voice(internal_, lobbyId, cb.release(), wrapper); +} + +Result LobbyManager::ConnectNetwork(LobbyId lobbyId) +{ + auto result = internal_->connect_network(internal_, lobbyId); + return static_cast(result); +} + +Result LobbyManager::DisconnectNetwork(LobbyId lobbyId) +{ + auto result = internal_->disconnect_network(internal_, lobbyId); + return static_cast(result); +} + +Result LobbyManager::FlushNetwork() +{ + auto result = internal_->flush_network(internal_); + return static_cast(result); +} + +Result LobbyManager::OpenNetworkChannel(LobbyId lobbyId, std::uint8_t channelId, bool reliable) +{ + auto result = + internal_->open_network_channel(internal_, lobbyId, channelId, (reliable ? 1 : 0)); + return static_cast(result); +} + +Result LobbyManager::SendNetworkMessage(LobbyId lobbyId, + UserId userId, + std::uint8_t channelId, + std::uint8_t* data, + std::uint32_t dataLength) +{ + auto result = internal_->send_network_message( + internal_, lobbyId, userId, channelId, reinterpret_cast(data), dataLength); + return static_cast(result); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/lobby_manager.h b/libs/discordGameSDK/cpp/lobby_manager.h new file mode 100644 index 0000000..96380cb --- /dev/null +++ b/libs/discordGameSDK/cpp/lobby_manager.h @@ -0,0 +1,88 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class LobbyManager final { +public: + ~LobbyManager() = default; + + Result GetLobbyCreateTransaction(LobbyTransaction* transaction); + Result GetLobbyUpdateTransaction(LobbyId lobbyId, LobbyTransaction* transaction); + Result GetMemberUpdateTransaction(LobbyId lobbyId, + UserId userId, + LobbyMemberTransaction* transaction); + void CreateLobby(LobbyTransaction const& transaction, + std::function callback); + void UpdateLobby(LobbyId lobbyId, + LobbyTransaction const& transaction, + std::function callback); + void DeleteLobby(LobbyId lobbyId, std::function callback); + void ConnectLobby(LobbyId lobbyId, + LobbySecret secret, + std::function callback); + void ConnectLobbyWithActivitySecret(LobbySecret activitySecret, + std::function callback); + void DisconnectLobby(LobbyId lobbyId, std::function callback); + Result GetLobby(LobbyId lobbyId, Lobby* lobby); + Result GetLobbyActivitySecret(LobbyId lobbyId, char secret[128]); + Result GetLobbyMetadataValue(LobbyId lobbyId, MetadataKey key, char value[4096]); + Result GetLobbyMetadataKey(LobbyId lobbyId, std::int32_t index, char key[256]); + Result LobbyMetadataCount(LobbyId lobbyId, std::int32_t* count); + Result MemberCount(LobbyId lobbyId, std::int32_t* count); + Result GetMemberUserId(LobbyId lobbyId, std::int32_t index, UserId* userId); + Result GetMemberUser(LobbyId lobbyId, UserId userId, User* user); + Result GetMemberMetadataValue(LobbyId lobbyId, + UserId userId, + MetadataKey key, + char value[4096]); + Result GetMemberMetadataKey(LobbyId lobbyId, UserId userId, std::int32_t index, char key[256]); + Result MemberMetadataCount(LobbyId lobbyId, UserId userId, std::int32_t* count); + void UpdateMember(LobbyId lobbyId, + UserId userId, + LobbyMemberTransaction const& transaction, + std::function callback); + void SendLobbyMessage(LobbyId lobbyId, + std::uint8_t* data, + std::uint32_t dataLength, + std::function callback); + Result GetSearchQuery(LobbySearchQuery* query); + void Search(LobbySearchQuery const& query, std::function callback); + void LobbyCount(std::int32_t* count); + Result GetLobbyId(std::int32_t index, LobbyId* lobbyId); + void ConnectVoice(LobbyId lobbyId, std::function callback); + void DisconnectVoice(LobbyId lobbyId, std::function callback); + Result ConnectNetwork(LobbyId lobbyId); + Result DisconnectNetwork(LobbyId lobbyId); + Result FlushNetwork(); + Result OpenNetworkChannel(LobbyId lobbyId, std::uint8_t channelId, bool reliable); + Result SendNetworkMessage(LobbyId lobbyId, + UserId userId, + std::uint8_t channelId, + std::uint8_t* data, + std::uint32_t dataLength); + + Event OnLobbyUpdate; + Event OnLobbyDelete; + Event OnMemberConnect; + Event OnMemberUpdate; + Event OnMemberDisconnect; + Event OnLobbyMessage; + Event OnSpeaking; + Event OnNetworkMessage; + +private: + friend class Core; + + LobbyManager() = default; + LobbyManager(LobbyManager const& rhs) = delete; + LobbyManager& operator=(LobbyManager const& rhs) = delete; + LobbyManager(LobbyManager&& rhs) = delete; + LobbyManager& operator=(LobbyManager&& rhs) = delete; + + IDiscordLobbyManager* internal_; + static IDiscordLobbyEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/network_manager.cpp b/libs/discordGameSDK/cpp/network_manager.cpp new file mode 100644 index 0000000..97c219e --- /dev/null +++ b/libs/discordGameSDK/cpp/network_manager.cpp @@ -0,0 +1,103 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "network_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +class NetworkEvents final { +public: + static void OnMessage(void* callbackData, + DiscordNetworkPeerId peerId, + DiscordNetworkChannelId channelId, + uint8_t* data, + uint32_t dataLength) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->NetworkManager(); + module.OnMessage(peerId, channelId, data, dataLength); + } + + static void OnRouteUpdate(void* callbackData, char const* routeData) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->NetworkManager(); + module.OnRouteUpdate(static_cast(routeData)); + } +}; + +IDiscordNetworkEvents NetworkManager::events_{ + &NetworkEvents::OnMessage, + &NetworkEvents::OnRouteUpdate, +}; + +void NetworkManager::GetPeerId(NetworkPeerId* peerId) +{ + if (!peerId) { + return; + } + + internal_->get_peer_id(internal_, reinterpret_cast(peerId)); +} + +Result NetworkManager::Flush() +{ + auto result = internal_->flush(internal_); + return static_cast(result); +} + +Result NetworkManager::OpenPeer(NetworkPeerId peerId, char const* routeData) +{ + auto result = internal_->open_peer(internal_, peerId, const_cast(routeData)); + return static_cast(result); +} + +Result NetworkManager::UpdatePeer(NetworkPeerId peerId, char const* routeData) +{ + auto result = internal_->update_peer(internal_, peerId, const_cast(routeData)); + return static_cast(result); +} + +Result NetworkManager::ClosePeer(NetworkPeerId peerId) +{ + auto result = internal_->close_peer(internal_, peerId); + return static_cast(result); +} + +Result NetworkManager::OpenChannel(NetworkPeerId peerId, NetworkChannelId channelId, bool reliable) +{ + auto result = internal_->open_channel(internal_, peerId, channelId, (reliable ? 1 : 0)); + return static_cast(result); +} + +Result NetworkManager::CloseChannel(NetworkPeerId peerId, NetworkChannelId channelId) +{ + auto result = internal_->close_channel(internal_, peerId, channelId); + return static_cast(result); +} + +Result NetworkManager::SendMessage(NetworkPeerId peerId, + NetworkChannelId channelId, + std::uint8_t* data, + std::uint32_t dataLength) +{ + auto result = internal_->send_message( + internal_, peerId, channelId, reinterpret_cast(data), dataLength); + return static_cast(result); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/network_manager.h b/libs/discordGameSDK/cpp/network_manager.h new file mode 100644 index 0000000..e374670 --- /dev/null +++ b/libs/discordGameSDK/cpp/network_manager.h @@ -0,0 +1,63 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class NetworkManager final { +public: + ~NetworkManager() = default; + + /** + * Get the local peer ID for this process. + */ + void GetPeerId(NetworkPeerId* peerId); + /** + * Send pending network messages. + */ + Result Flush(); + /** + * Open a connection to a remote peer. + */ + Result OpenPeer(NetworkPeerId peerId, char const* routeData); + /** + * Update the route data for a connected peer. + */ + Result UpdatePeer(NetworkPeerId peerId, char const* routeData); + /** + * Close the connection to a remote peer. + */ + Result ClosePeer(NetworkPeerId peerId); + /** + * Open a message channel to a connected peer. + */ + Result OpenChannel(NetworkPeerId peerId, NetworkChannelId channelId, bool reliable); + /** + * Close a message channel to a connected peer. + */ + Result CloseChannel(NetworkPeerId peerId, NetworkChannelId channelId); + /** + * Send a message to a connected peer over an opened message channel. + */ + Result SendMessage(NetworkPeerId peerId, + NetworkChannelId channelId, + std::uint8_t* data, + std::uint32_t dataLength); + + Event OnMessage; + Event OnRouteUpdate; + +private: + friend class Core; + + NetworkManager() = default; + NetworkManager(NetworkManager const& rhs) = delete; + NetworkManager& operator=(NetworkManager const& rhs) = delete; + NetworkManager(NetworkManager&& rhs) = delete; + NetworkManager& operator=(NetworkManager&& rhs) = delete; + + IDiscordNetworkManager* internal_; + static IDiscordNetworkEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/overlay_manager.cpp b/libs/discordGameSDK/cpp/overlay_manager.cpp new file mode 100644 index 0000000..e1c91ce --- /dev/null +++ b/libs/discordGameSDK/cpp/overlay_manager.cpp @@ -0,0 +1,112 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "overlay_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +class OverlayEvents final { +public: + static void OnToggle(void* callbackData, bool locked) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->OverlayManager(); + module.OnToggle((locked != 0)); + } +}; + +IDiscordOverlayEvents OverlayManager::events_{ + &OverlayEvents::OnToggle, +}; + +void OverlayManager::IsEnabled(bool* enabled) +{ + if (!enabled) { + return; + } + + internal_->is_enabled(internal_, reinterpret_cast(enabled)); +} + +void OverlayManager::IsLocked(bool* locked) +{ + if (!locked) { + return; + } + + internal_->is_locked(internal_, reinterpret_cast(locked)); +} + +void OverlayManager::SetLocked(bool locked, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->set_locked(internal_, (locked ? 1 : 0), cb.release(), wrapper); +} + +void OverlayManager::OpenActivityInvite(ActivityActionType type, + std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->open_activity_invite( + internal_, static_cast(type), cb.release(), wrapper); +} + +void OverlayManager::OpenGuildInvite(char const* code, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->open_guild_invite(internal_, const_cast(code), cb.release(), wrapper); +} + +void OverlayManager::OpenVoiceSettings(std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->open_voice_settings(internal_, cb.release(), wrapper); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/overlay_manager.h b/libs/discordGameSDK/cpp/overlay_manager.h new file mode 100644 index 0000000..c452aed --- /dev/null +++ b/libs/discordGameSDK/cpp/overlay_manager.h @@ -0,0 +1,33 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class OverlayManager final { +public: + ~OverlayManager() = default; + + void IsEnabled(bool* enabled); + void IsLocked(bool* locked); + void SetLocked(bool locked, std::function callback); + void OpenActivityInvite(ActivityActionType type, std::function callback); + void OpenGuildInvite(char const* code, std::function callback); + void OpenVoiceSettings(std::function callback); + + Event OnToggle; + +private: + friend class Core; + + OverlayManager() = default; + OverlayManager(OverlayManager const& rhs) = delete; + OverlayManager& operator=(OverlayManager const& rhs) = delete; + OverlayManager(OverlayManager&& rhs) = delete; + OverlayManager& operator=(OverlayManager&& rhs) = delete; + + IDiscordOverlayManager* internal_; + static IDiscordOverlayEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/relationship_manager.cpp b/libs/discordGameSDK/cpp/relationship_manager.cpp new file mode 100644 index 0000000..005f2b0 --- /dev/null +++ b/libs/discordGameSDK/cpp/relationship_manager.cpp @@ -0,0 +1,90 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "relationship_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +class RelationshipEvents final { +public: + static void OnRefresh(void* callbackData) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->RelationshipManager(); + module.OnRefresh(); + } + + static void OnRelationshipUpdate(void* callbackData, DiscordRelationship* relationship) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->RelationshipManager(); + module.OnRelationshipUpdate(*reinterpret_cast(relationship)); + } +}; + +IDiscordRelationshipEvents RelationshipManager::events_{ + &RelationshipEvents::OnRefresh, + &RelationshipEvents::OnRelationshipUpdate, +}; + +void RelationshipManager::Filter(std::function filter) +{ + static auto wrapper = [](void* callbackData, DiscordRelationship* relationship) -> bool { + auto cb(reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return {}; + } + return (*cb)(*reinterpret_cast(relationship)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(filter))); + internal_->filter(internal_, cb.get(), wrapper); +} + +Result RelationshipManager::Count(std::int32_t* count) +{ + if (!count) { + return Result::InternalError; + } + + auto result = internal_->count(internal_, reinterpret_cast(count)); + return static_cast(result); +} + +Result RelationshipManager::Get(UserId userId, Relationship* relationship) +{ + if (!relationship) { + return Result::InternalError; + } + + auto result = + internal_->get(internal_, userId, reinterpret_cast(relationship)); + return static_cast(result); +} + +Result RelationshipManager::GetAt(std::uint32_t index, Relationship* relationship) +{ + if (!relationship) { + return Result::InternalError; + } + + auto result = + internal_->get_at(internal_, index, reinterpret_cast(relationship)); + return static_cast(result); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/relationship_manager.h b/libs/discordGameSDK/cpp/relationship_manager.h new file mode 100644 index 0000000..e9cd016 --- /dev/null +++ b/libs/discordGameSDK/cpp/relationship_manager.h @@ -0,0 +1,32 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class RelationshipManager final { +public: + ~RelationshipManager() = default; + + void Filter(std::function filter); + Result Count(std::int32_t* count); + Result Get(UserId userId, Relationship* relationship); + Result GetAt(std::uint32_t index, Relationship* relationship); + + Event<> OnRefresh; + Event OnRelationshipUpdate; + +private: + friend class Core; + + RelationshipManager() = default; + RelationshipManager(RelationshipManager const& rhs) = delete; + RelationshipManager& operator=(RelationshipManager const& rhs) = delete; + RelationshipManager(RelationshipManager&& rhs) = delete; + RelationshipManager& operator=(RelationshipManager&& rhs) = delete; + + IDiscordRelationshipManager* internal_; + static IDiscordRelationshipEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/storage_manager.cpp b/libs/discordGameSDK/cpp/storage_manager.cpp new file mode 100644 index 0000000..fbf9ca7 --- /dev/null +++ b/libs/discordGameSDK/cpp/storage_manager.cpp @@ -0,0 +1,158 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "storage_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +Result StorageManager::Read(char const* name, + std::uint8_t* data, + std::uint32_t dataLength, + std::uint32_t* read) +{ + if (!read) { + return Result::InternalError; + } + + auto result = internal_->read(internal_, + const_cast(name), + reinterpret_cast(data), + dataLength, + reinterpret_cast(read)); + return static_cast(result); +} + +void StorageManager::ReadAsync(char const* name, + std::function callback) +{ + static auto wrapper = + [](void* callbackData, EDiscordResult result, uint8_t* data, uint32_t dataLength) -> void { + std::unique_ptr> cb( + reinterpret_cast*>( + callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result), data, dataLength); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->read_async(internal_, const_cast(name), cb.release(), wrapper); +} + +void StorageManager::ReadAsyncPartial( + char const* name, + std::uint64_t offset, + std::uint64_t length, + std::function callback) +{ + static auto wrapper = + [](void* callbackData, EDiscordResult result, uint8_t* data, uint32_t dataLength) -> void { + std::unique_ptr> cb( + reinterpret_cast*>( + callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result), data, dataLength); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->read_async_partial( + internal_, const_cast(name), offset, length, cb.release(), wrapper); +} + +Result StorageManager::Write(char const* name, std::uint8_t* data, std::uint32_t dataLength) +{ + auto result = internal_->write( + internal_, const_cast(name), reinterpret_cast(data), dataLength); + return static_cast(result); +} + +void StorageManager::WriteAsync(char const* name, + std::uint8_t* data, + std::uint32_t dataLength, + std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->write_async(internal_, + const_cast(name), + reinterpret_cast(data), + dataLength, + cb.release(), + wrapper); +} + +Result StorageManager::Delete(char const* name) +{ + auto result = internal_->delete_(internal_, const_cast(name)); + return static_cast(result); +} + +Result StorageManager::Exists(char const* name, bool* exists) +{ + if (!exists) { + return Result::InternalError; + } + + auto result = + internal_->exists(internal_, const_cast(name), reinterpret_cast(exists)); + return static_cast(result); +} + +void StorageManager::Count(std::int32_t* count) +{ + if (!count) { + return; + } + + internal_->count(internal_, reinterpret_cast(count)); +} + +Result StorageManager::Stat(char const* name, FileStat* stat) +{ + if (!stat) { + return Result::InternalError; + } + + auto result = + internal_->stat(internal_, const_cast(name), reinterpret_cast(stat)); + return static_cast(result); +} + +Result StorageManager::StatAt(std::int32_t index, FileStat* stat) +{ + if (!stat) { + return Result::InternalError; + } + + auto result = internal_->stat_at(internal_, index, reinterpret_cast(stat)); + return static_cast(result); +} + +Result StorageManager::GetPath(char path[4096]) +{ + if (!path) { + return Result::InternalError; + } + + auto result = internal_->get_path(internal_, reinterpret_cast(path)); + return static_cast(result); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/storage_manager.h b/libs/discordGameSDK/cpp/storage_manager.h new file mode 100644 index 0000000..5d6d17b --- /dev/null +++ b/libs/discordGameSDK/cpp/storage_manager.h @@ -0,0 +1,46 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class StorageManager final { +public: + ~StorageManager() = default; + + Result Read(char const* name, + std::uint8_t* data, + std::uint32_t dataLength, + std::uint32_t* read); + void ReadAsync(char const* name, + std::function callback); + void ReadAsyncPartial(char const* name, + std::uint64_t offset, + std::uint64_t length, + std::function callback); + Result Write(char const* name, std::uint8_t* data, std::uint32_t dataLength); + void WriteAsync(char const* name, + std::uint8_t* data, + std::uint32_t dataLength, + std::function callback); + Result Delete(char const* name); + Result Exists(char const* name, bool* exists); + void Count(std::int32_t* count); + Result Stat(char const* name, FileStat* stat); + Result StatAt(std::int32_t index, FileStat* stat); + Result GetPath(char path[4096]); + +private: + friend class Core; + + StorageManager() = default; + StorageManager(StorageManager const& rhs) = delete; + StorageManager& operator=(StorageManager const& rhs) = delete; + StorageManager(StorageManager&& rhs) = delete; + StorageManager& operator=(StorageManager&& rhs) = delete; + + IDiscordStorageManager* internal_; + static IDiscordStorageEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/store_manager.cpp b/libs/discordGameSDK/cpp/store_manager.cpp new file mode 100644 index 0000000..d6bdad1 --- /dev/null +++ b/libs/discordGameSDK/cpp/store_manager.cpp @@ -0,0 +1,160 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "store_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +class StoreEvents final { +public: + static void OnEntitlementCreate(void* callbackData, DiscordEntitlement* entitlement) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->StoreManager(); + module.OnEntitlementCreate(*reinterpret_cast(entitlement)); + } + + static void OnEntitlementDelete(void* callbackData, DiscordEntitlement* entitlement) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->StoreManager(); + module.OnEntitlementDelete(*reinterpret_cast(entitlement)); + } +}; + +IDiscordStoreEvents StoreManager::events_{ + &StoreEvents::OnEntitlementCreate, + &StoreEvents::OnEntitlementDelete, +}; + +void StoreManager::FetchSkus(std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->fetch_skus(internal_, cb.release(), wrapper); +} + +void StoreManager::CountSkus(std::int32_t* count) +{ + if (!count) { + return; + } + + internal_->count_skus(internal_, reinterpret_cast(count)); +} + +Result StoreManager::GetSku(Snowflake skuId, Sku* sku) +{ + if (!sku) { + return Result::InternalError; + } + + auto result = internal_->get_sku(internal_, skuId, reinterpret_cast(sku)); + return static_cast(result); +} + +Result StoreManager::GetSkuAt(std::int32_t index, Sku* sku) +{ + if (!sku) { + return Result::InternalError; + } + + auto result = internal_->get_sku_at(internal_, index, reinterpret_cast(sku)); + return static_cast(result); +} + +void StoreManager::FetchEntitlements(std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->fetch_entitlements(internal_, cb.release(), wrapper); +} + +void StoreManager::CountEntitlements(std::int32_t* count) +{ + if (!count) { + return; + } + + internal_->count_entitlements(internal_, reinterpret_cast(count)); +} + +Result StoreManager::GetEntitlement(Snowflake entitlementId, Entitlement* entitlement) +{ + if (!entitlement) { + return Result::InternalError; + } + + auto result = internal_->get_entitlement( + internal_, entitlementId, reinterpret_cast(entitlement)); + return static_cast(result); +} + +Result StoreManager::GetEntitlementAt(std::int32_t index, Entitlement* entitlement) +{ + if (!entitlement) { + return Result::InternalError; + } + + auto result = internal_->get_entitlement_at( + internal_, index, reinterpret_cast(entitlement)); + return static_cast(result); +} + +Result StoreManager::HasSkuEntitlement(Snowflake skuId, bool* hasEntitlement) +{ + if (!hasEntitlement) { + return Result::InternalError; + } + + auto result = + internal_->has_sku_entitlement(internal_, skuId, reinterpret_cast(hasEntitlement)); + return static_cast(result); +} + +void StoreManager::StartPurchase(Snowflake skuId, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->start_purchase(internal_, skuId, cb.release(), wrapper); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/store_manager.h b/libs/discordGameSDK/cpp/store_manager.h new file mode 100644 index 0000000..8e0df8a --- /dev/null +++ b/libs/discordGameSDK/cpp/store_manager.h @@ -0,0 +1,38 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class StoreManager final { +public: + ~StoreManager() = default; + + void FetchSkus(std::function callback); + void CountSkus(std::int32_t* count); + Result GetSku(Snowflake skuId, Sku* sku); + Result GetSkuAt(std::int32_t index, Sku* sku); + void FetchEntitlements(std::function callback); + void CountEntitlements(std::int32_t* count); + Result GetEntitlement(Snowflake entitlementId, Entitlement* entitlement); + Result GetEntitlementAt(std::int32_t index, Entitlement* entitlement); + Result HasSkuEntitlement(Snowflake skuId, bool* hasEntitlement); + void StartPurchase(Snowflake skuId, std::function callback); + + Event OnEntitlementCreate; + Event OnEntitlementDelete; + +private: + friend class Core; + + StoreManager() = default; + StoreManager(StoreManager const& rhs) = delete; + StoreManager& operator=(StoreManager const& rhs) = delete; + StoreManager(StoreManager&& rhs) = delete; + StoreManager& operator=(StoreManager&& rhs) = delete; + + IDiscordStoreManager* internal_; + static IDiscordStoreEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/types.cpp b/libs/discordGameSDK/cpp/types.cpp new file mode 100644 index 0000000..66cdd49 --- /dev/null +++ b/libs/discordGameSDK/cpp/types.cpp @@ -0,0 +1,769 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "types.h" + +#include +#include + +namespace discord { + +void User::SetId(UserId id) +{ + internal_.id = id; +} + +UserId User::GetId() const +{ + return internal_.id; +} + +void User::SetUsername(char const* username) +{ + strncpy(internal_.username, username, 256); + internal_.username[256 - 1] = '\0'; +} + +char const* User::GetUsername() const +{ + return internal_.username; +} + +void User::SetDiscriminator(char const* discriminator) +{ + strncpy(internal_.discriminator, discriminator, 8); + internal_.discriminator[8 - 1] = '\0'; +} + +char const* User::GetDiscriminator() const +{ + return internal_.discriminator; +} + +void User::SetAvatar(char const* avatar) +{ + strncpy(internal_.avatar, avatar, 128); + internal_.avatar[128 - 1] = '\0'; +} + +char const* User::GetAvatar() const +{ + return internal_.avatar; +} + +void User::SetBot(bool bot) +{ + internal_.bot = bot; +} + +bool User::GetBot() const +{ + return internal_.bot != 0; +} + +void OAuth2Token::SetAccessToken(char const* accessToken) +{ + strncpy(internal_.access_token, accessToken, 128); + internal_.access_token[128 - 1] = '\0'; +} + +char const* OAuth2Token::GetAccessToken() const +{ + return internal_.access_token; +} + +void OAuth2Token::SetScopes(char const* scopes) +{ + strncpy(internal_.scopes, scopes, 1024); + internal_.scopes[1024 - 1] = '\0'; +} + +char const* OAuth2Token::GetScopes() const +{ + return internal_.scopes; +} + +void OAuth2Token::SetExpires(Timestamp expires) +{ + internal_.expires = expires; +} + +Timestamp OAuth2Token::GetExpires() const +{ + return internal_.expires; +} + +void ImageHandle::SetType(ImageType type) +{ + internal_.type = static_cast(type); +} + +ImageType ImageHandle::GetType() const +{ + return static_cast(internal_.type); +} + +void ImageHandle::SetId(std::int64_t id) +{ + internal_.id = id; +} + +std::int64_t ImageHandle::GetId() const +{ + return internal_.id; +} + +void ImageHandle::SetSize(std::uint32_t size) +{ + internal_.size = size; +} + +std::uint32_t ImageHandle::GetSize() const +{ + return internal_.size; +} + +void ImageDimensions::SetWidth(std::uint32_t width) +{ + internal_.width = width; +} + +std::uint32_t ImageDimensions::GetWidth() const +{ + return internal_.width; +} + +void ImageDimensions::SetHeight(std::uint32_t height) +{ + internal_.height = height; +} + +std::uint32_t ImageDimensions::GetHeight() const +{ + return internal_.height; +} + +void ActivityTimestamps::SetStart(Timestamp start) +{ + internal_.start = start; +} + +Timestamp ActivityTimestamps::GetStart() const +{ + return internal_.start; +} + +void ActivityTimestamps::SetEnd(Timestamp end) +{ + internal_.end = end; +} + +Timestamp ActivityTimestamps::GetEnd() const +{ + return internal_.end; +} + +void ActivityAssets::SetLargeImage(char const* largeImage) +{ + strncpy(internal_.large_image, largeImage, 128); + internal_.large_image[128 - 1] = '\0'; +} + +char const* ActivityAssets::GetLargeImage() const +{ + return internal_.large_image; +} + +void ActivityAssets::SetLargeText(char const* largeText) +{ + strncpy(internal_.large_text, largeText, 128); + internal_.large_text[128 - 1] = '\0'; +} + +char const* ActivityAssets::GetLargeText() const +{ + return internal_.large_text; +} + +void ActivityAssets::SetSmallImage(char const* smallImage) +{ + strncpy(internal_.small_image, smallImage, 128); + internal_.small_image[128 - 1] = '\0'; +} + +char const* ActivityAssets::GetSmallImage() const +{ + return internal_.small_image; +} + +void ActivityAssets::SetSmallText(char const* smallText) +{ + strncpy(internal_.small_text, smallText, 128); + internal_.small_text[128 - 1] = '\0'; +} + +char const* ActivityAssets::GetSmallText() const +{ + return internal_.small_text; +} + +void PartySize::SetCurrentSize(std::int32_t currentSize) +{ + internal_.current_size = currentSize; +} + +std::int32_t PartySize::GetCurrentSize() const +{ + return internal_.current_size; +} + +void PartySize::SetMaxSize(std::int32_t maxSize) +{ + internal_.max_size = maxSize; +} + +std::int32_t PartySize::GetMaxSize() const +{ + return internal_.max_size; +} + +void ActivityParty::SetId(char const* id) +{ + strncpy(internal_.id, id, 128); + internal_.id[128 - 1] = '\0'; +} + +char const* ActivityParty::GetId() const +{ + return internal_.id; +} + +PartySize& ActivityParty::GetSize() +{ + return reinterpret_cast(internal_.size); +} + +PartySize const& ActivityParty::GetSize() const +{ + return reinterpret_cast(internal_.size); +} + +void ActivitySecrets::SetMatch(char const* match) +{ + strncpy(internal_.match, match, 128); + internal_.match[128 - 1] = '\0'; +} + +char const* ActivitySecrets::GetMatch() const +{ + return internal_.match; +} + +void ActivitySecrets::SetJoin(char const* join) +{ + strncpy(internal_.join, join, 128); + internal_.join[128 - 1] = '\0'; +} + +char const* ActivitySecrets::GetJoin() const +{ + return internal_.join; +} + +void ActivitySecrets::SetSpectate(char const* spectate) +{ + strncpy(internal_.spectate, spectate, 128); + internal_.spectate[128 - 1] = '\0'; +} + +char const* ActivitySecrets::GetSpectate() const +{ + return internal_.spectate; +} + +void Activity::SetType(ActivityType type) +{ + internal_.type = static_cast(type); +} + +ActivityType Activity::GetType() const +{ + return static_cast(internal_.type); +} + +void Activity::SetApplicationId(std::int64_t applicationId) +{ + internal_.application_id = applicationId; +} + +std::int64_t Activity::GetApplicationId() const +{ + return internal_.application_id; +} + +void Activity::SetName(char const* name) +{ + strncpy(internal_.name, name, 128); + internal_.name[128 - 1] = '\0'; +} + +char const* Activity::GetName() const +{ + return internal_.name; +} + +void Activity::SetState(char const* state) +{ + strncpy(internal_.state, state, 128); + internal_.state[128 - 1] = '\0'; +} + +char const* Activity::GetState() const +{ + return internal_.state; +} + +void Activity::SetDetails(char const* details) +{ + strncpy(internal_.details, details, 128); + internal_.details[128 - 1] = '\0'; +} + +char const* Activity::GetDetails() const +{ + return internal_.details; +} + +ActivityTimestamps& Activity::GetTimestamps() +{ + return reinterpret_cast(internal_.timestamps); +} + +ActivityTimestamps const& Activity::GetTimestamps() const +{ + return reinterpret_cast(internal_.timestamps); +} + +ActivityAssets& Activity::GetAssets() +{ + return reinterpret_cast(internal_.assets); +} + +ActivityAssets const& Activity::GetAssets() const +{ + return reinterpret_cast(internal_.assets); +} + +ActivityParty& Activity::GetParty() +{ + return reinterpret_cast(internal_.party); +} + +ActivityParty const& Activity::GetParty() const +{ + return reinterpret_cast(internal_.party); +} + +ActivitySecrets& Activity::GetSecrets() +{ + return reinterpret_cast(internal_.secrets); +} + +ActivitySecrets const& Activity::GetSecrets() const +{ + return reinterpret_cast(internal_.secrets); +} + +void Activity::SetInstance(bool instance) +{ + internal_.instance = instance; +} + +bool Activity::GetInstance() const +{ + return internal_.instance != 0; +} + +void Presence::SetStatus(Status status) +{ + internal_.status = static_cast(status); +} + +Status Presence::GetStatus() const +{ + return static_cast(internal_.status); +} + +Activity& Presence::GetActivity() +{ + return reinterpret_cast(internal_.activity); +} + +Activity const& Presence::GetActivity() const +{ + return reinterpret_cast(internal_.activity); +} + +void Relationship::SetType(RelationshipType type) +{ + internal_.type = static_cast(type); +} + +RelationshipType Relationship::GetType() const +{ + return static_cast(internal_.type); +} + +User& Relationship::GetUser() +{ + return reinterpret_cast(internal_.user); +} + +User const& Relationship::GetUser() const +{ + return reinterpret_cast(internal_.user); +} + +Presence& Relationship::GetPresence() +{ + return reinterpret_cast(internal_.presence); +} + +Presence const& Relationship::GetPresence() const +{ + return reinterpret_cast(internal_.presence); +} + +void Lobby::SetId(LobbyId id) +{ + internal_.id = id; +} + +LobbyId Lobby::GetId() const +{ + return internal_.id; +} + +void Lobby::SetType(LobbyType type) +{ + internal_.type = static_cast(type); +} + +LobbyType Lobby::GetType() const +{ + return static_cast(internal_.type); +} + +void Lobby::SetOwnerId(UserId ownerId) +{ + internal_.owner_id = ownerId; +} + +UserId Lobby::GetOwnerId() const +{ + return internal_.owner_id; +} + +void Lobby::SetSecret(LobbySecret secret) +{ + strncpy(internal_.secret, secret, 128); + internal_.secret[128 - 1] = '\0'; +} + +LobbySecret Lobby::GetSecret() const +{ + return internal_.secret; +} + +void Lobby::SetCapacity(std::uint32_t capacity) +{ + internal_.capacity = capacity; +} + +std::uint32_t Lobby::GetCapacity() const +{ + return internal_.capacity; +} + +void Lobby::SetLocked(bool locked) +{ + internal_.locked = locked; +} + +bool Lobby::GetLocked() const +{ + return internal_.locked != 0; +} + +void FileStat::SetFilename(char const* filename) +{ + strncpy(internal_.filename, filename, 260); + internal_.filename[260 - 1] = '\0'; +} + +char const* FileStat::GetFilename() const +{ + return internal_.filename; +} + +void FileStat::SetSize(std::uint64_t size) +{ + internal_.size = size; +} + +std::uint64_t FileStat::GetSize() const +{ + return internal_.size; +} + +void FileStat::SetLastModified(std::uint64_t lastModified) +{ + internal_.last_modified = lastModified; +} + +std::uint64_t FileStat::GetLastModified() const +{ + return internal_.last_modified; +} + +void Entitlement::SetId(Snowflake id) +{ + internal_.id = id; +} + +Snowflake Entitlement::GetId() const +{ + return internal_.id; +} + +void Entitlement::SetType(EntitlementType type) +{ + internal_.type = static_cast(type); +} + +EntitlementType Entitlement::GetType() const +{ + return static_cast(internal_.type); +} + +void Entitlement::SetSkuId(Snowflake skuId) +{ + internal_.sku_id = skuId; +} + +Snowflake Entitlement::GetSkuId() const +{ + return internal_.sku_id; +} + +void SkuPrice::SetAmount(std::uint32_t amount) +{ + internal_.amount = amount; +} + +std::uint32_t SkuPrice::GetAmount() const +{ + return internal_.amount; +} + +void SkuPrice::SetCurrency(char const* currency) +{ + strncpy(internal_.currency, currency, 16); + internal_.currency[16 - 1] = '\0'; +} + +char const* SkuPrice::GetCurrency() const +{ + return internal_.currency; +} + +void Sku::SetId(Snowflake id) +{ + internal_.id = id; +} + +Snowflake Sku::GetId() const +{ + return internal_.id; +} + +void Sku::SetType(SkuType type) +{ + internal_.type = static_cast(type); +} + +SkuType Sku::GetType() const +{ + return static_cast(internal_.type); +} + +void Sku::SetName(char const* name) +{ + strncpy(internal_.name, name, 256); + internal_.name[256 - 1] = '\0'; +} + +char const* Sku::GetName() const +{ + return internal_.name; +} + +SkuPrice& Sku::GetPrice() +{ + return reinterpret_cast(internal_.price); +} + +SkuPrice const& Sku::GetPrice() const +{ + return reinterpret_cast(internal_.price); +} + +void InputMode::SetType(InputModeType type) +{ + internal_.type = static_cast(type); +} + +InputModeType InputMode::GetType() const +{ + return static_cast(internal_.type); +} + +void InputMode::SetShortcut(char const* shortcut) +{ + strncpy(internal_.shortcut, shortcut, 256); + internal_.shortcut[256 - 1] = '\0'; +} + +char const* InputMode::GetShortcut() const +{ + return internal_.shortcut; +} + +void UserAchievement::SetUserId(Snowflake userId) +{ + internal_.user_id = userId; +} + +Snowflake UserAchievement::GetUserId() const +{ + return internal_.user_id; +} + +void UserAchievement::SetAchievementId(Snowflake achievementId) +{ + internal_.achievement_id = achievementId; +} + +Snowflake UserAchievement::GetAchievementId() const +{ + return internal_.achievement_id; +} + +void UserAchievement::SetPercentComplete(std::uint8_t percentComplete) +{ + internal_.percent_complete = percentComplete; +} + +std::uint8_t UserAchievement::GetPercentComplete() const +{ + return internal_.percent_complete; +} + +void UserAchievement::SetUnlockedAt(DateTime unlockedAt) +{ + strncpy(internal_.unlocked_at, unlockedAt, 64); + internal_.unlocked_at[64 - 1] = '\0'; +} + +DateTime UserAchievement::GetUnlockedAt() const +{ + return internal_.unlocked_at; +} + +Result LobbyTransaction::SetType(LobbyType type) +{ + auto result = internal_->set_type(internal_, static_cast(type)); + return static_cast(result); +} + +Result LobbyTransaction::SetOwner(UserId ownerId) +{ + auto result = internal_->set_owner(internal_, ownerId); + return static_cast(result); +} + +Result LobbyTransaction::SetCapacity(std::uint32_t capacity) +{ + auto result = internal_->set_capacity(internal_, capacity); + return static_cast(result); +} + +Result LobbyTransaction::SetMetadata(MetadataKey key, MetadataValue value) +{ + auto result = + internal_->set_metadata(internal_, const_cast(key), const_cast(value)); + return static_cast(result); +} + +Result LobbyTransaction::DeleteMetadata(MetadataKey key) +{ + auto result = internal_->delete_metadata(internal_, const_cast(key)); + return static_cast(result); +} + +Result LobbyTransaction::SetLocked(bool locked) +{ + auto result = internal_->set_locked(internal_, (locked ? 1 : 0)); + return static_cast(result); +} + +Result LobbyMemberTransaction::SetMetadata(MetadataKey key, MetadataValue value) +{ + auto result = + internal_->set_metadata(internal_, const_cast(key), const_cast(value)); + return static_cast(result); +} + +Result LobbyMemberTransaction::DeleteMetadata(MetadataKey key) +{ + auto result = internal_->delete_metadata(internal_, const_cast(key)); + return static_cast(result); +} + +Result LobbySearchQuery::Filter(MetadataKey key, + LobbySearchComparison comparison, + LobbySearchCast cast, + MetadataValue value) +{ + auto result = internal_->filter(internal_, + const_cast(key), + static_cast(comparison), + static_cast(cast), + const_cast(value)); + return static_cast(result); +} + +Result LobbySearchQuery::Sort(MetadataKey key, LobbySearchCast cast, MetadataValue value) +{ + auto result = internal_->sort(internal_, + const_cast(key), + static_cast(cast), + const_cast(value)); + return static_cast(result); +} + +Result LobbySearchQuery::Limit(std::uint32_t limit) +{ + auto result = internal_->limit(internal_, limit); + return static_cast(result); +} + +Result LobbySearchQuery::Distance(LobbySearchDistance distance) +{ + auto result = + internal_->distance(internal_, static_cast(distance)); + return static_cast(result); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/types.h b/libs/discordGameSDK/cpp/types.h new file mode 100644 index 0000000..8c7cc2b --- /dev/null +++ b/libs/discordGameSDK/cpp/types.h @@ -0,0 +1,491 @@ +#pragma once + +#include "ffi.h" +#include "event.h" + +namespace discord { + +enum class Result { + Ok = 0, + ServiceUnavailable = 1, + InvalidVersion = 2, + LockFailed = 3, + InternalError = 4, + InvalidPayload = 5, + InvalidCommand = 6, + InvalidPermissions = 7, + NotFetched = 8, + NotFound = 9, + Conflict = 10, + InvalidSecret = 11, + InvalidJoinSecret = 12, + NoEligibleActivity = 13, + InvalidInvite = 14, + NotAuthenticated = 15, + InvalidAccessToken = 16, + ApplicationMismatch = 17, + InvalidDataUrl = 18, + InvalidBase64 = 19, + NotFiltered = 20, + LobbyFull = 21, + InvalidLobbySecret = 22, + InvalidFilename = 23, + InvalidFileSize = 24, + InvalidEntitlement = 25, + NotInstalled = 26, + NotRunning = 27, + InsufficientBuffer = 28, + PurchaseCanceled = 29, + InvalidGuild = 30, + InvalidEvent = 31, + InvalidChannel = 32, + InvalidOrigin = 33, + RateLimited = 34, + OAuth2Error = 35, + SelectChannelTimeout = 36, + GetGuildTimeout = 37, + SelectVoiceForceRequired = 38, + CaptureShortcutAlreadyListening = 39, + UnauthorizedForAchievement = 40, + InvalidGiftCode = 41, + PurchaseError = 42, + TransactionAborted = 43, +}; + +enum class CreateFlags { + Default = 0, + NoRequireDiscord = 1, +}; + +enum class LogLevel { + Error = 1, + Warn, + Info, + Debug, +}; + +enum class UserFlag { + Partner = 2, + HypeSquadEvents = 4, + HypeSquadHouse1 = 64, + HypeSquadHouse2 = 128, + HypeSquadHouse3 = 256, +}; + +enum class PremiumType { + None = 0, + Tier1 = 1, + Tier2 = 2, +}; + +enum class ImageType { + User, +}; + +enum class ActivityType { + Playing, + Streaming, + Listening, + Watching, +}; + +enum class ActivityActionType { + Join = 1, + Spectate, +}; + +enum class ActivityJoinRequestReply { + No, + Yes, + Ignore, +}; + +enum class Status { + Offline = 0, + Online = 1, + Idle = 2, + DoNotDisturb = 3, +}; + +enum class RelationshipType { + None, + Friend, + Blocked, + PendingIncoming, + PendingOutgoing, + Implicit, +}; + +enum class LobbyType { + Private = 1, + Public, +}; + +enum class LobbySearchComparison { + LessThanOrEqual = -2, + LessThan, + Equal, + GreaterThan, + GreaterThanOrEqual, + NotEqual, +}; + +enum class LobbySearchCast { + String = 1, + Number, +}; + +enum class LobbySearchDistance { + Local, + Default, + Extended, + Global, +}; + +enum class EntitlementType { + Purchase = 1, + PremiumSubscription, + DeveloperGift, + TestModePurchase, + FreePurchase, + UserGift, + PremiumPurchase, +}; + +enum class SkuType { + Application = 1, + DLC, + Consumable, + Bundle, +}; + +enum class InputModeType { + VoiceActivity = 0, + PushToTalk, +}; + +using ClientId = std::int64_t; +using Version = std::int32_t; +using Snowflake = std::int64_t; +using Timestamp = std::int64_t; +using UserId = Snowflake; +using Locale = char const*; +using Branch = char const*; +using LobbyId = Snowflake; +using LobbySecret = char const*; +using MetadataKey = char const*; +using MetadataValue = char const*; +using NetworkPeerId = std::uint64_t; +using NetworkChannelId = std::uint8_t; +using Path = char const*; +using DateTime = char const*; + +class User final { +public: + void SetId(UserId id); + UserId GetId() const; + void SetUsername(char const* username); + char const* GetUsername() const; + void SetDiscriminator(char const* discriminator); + char const* GetDiscriminator() const; + void SetAvatar(char const* avatar); + char const* GetAvatar() const; + void SetBot(bool bot); + bool GetBot() const; + +private: + DiscordUser internal_; +}; + +class OAuth2Token final { +public: + void SetAccessToken(char const* accessToken); + char const* GetAccessToken() const; + void SetScopes(char const* scopes); + char const* GetScopes() const; + void SetExpires(Timestamp expires); + Timestamp GetExpires() const; + +private: + DiscordOAuth2Token internal_; +}; + +class ImageHandle final { +public: + void SetType(ImageType type); + ImageType GetType() const; + void SetId(std::int64_t id); + std::int64_t GetId() const; + void SetSize(std::uint32_t size); + std::uint32_t GetSize() const; + +private: + DiscordImageHandle internal_; +}; + +class ImageDimensions final { +public: + void SetWidth(std::uint32_t width); + std::uint32_t GetWidth() const; + void SetHeight(std::uint32_t height); + std::uint32_t GetHeight() const; + +private: + DiscordImageDimensions internal_; +}; + +class ActivityTimestamps final { +public: + void SetStart(Timestamp start); + Timestamp GetStart() const; + void SetEnd(Timestamp end); + Timestamp GetEnd() const; + +private: + DiscordActivityTimestamps internal_; +}; + +class ActivityAssets final { +public: + void SetLargeImage(char const* largeImage); + char const* GetLargeImage() const; + void SetLargeText(char const* largeText); + char const* GetLargeText() const; + void SetSmallImage(char const* smallImage); + char const* GetSmallImage() const; + void SetSmallText(char const* smallText); + char const* GetSmallText() const; + +private: + DiscordActivityAssets internal_; +}; + +class PartySize final { +public: + void SetCurrentSize(std::int32_t currentSize); + std::int32_t GetCurrentSize() const; + void SetMaxSize(std::int32_t maxSize); + std::int32_t GetMaxSize() const; + +private: + DiscordPartySize internal_; +}; + +class ActivityParty final { +public: + void SetId(char const* id); + char const* GetId() const; + PartySize& GetSize(); + PartySize const& GetSize() const; + +private: + DiscordActivityParty internal_; +}; + +class ActivitySecrets final { +public: + void SetMatch(char const* match); + char const* GetMatch() const; + void SetJoin(char const* join); + char const* GetJoin() const; + void SetSpectate(char const* spectate); + char const* GetSpectate() const; + +private: + DiscordActivitySecrets internal_; +}; + +class Activity final { +public: + void SetType(ActivityType type); + ActivityType GetType() const; + void SetApplicationId(std::int64_t applicationId); + std::int64_t GetApplicationId() const; + void SetName(char const* name); + char const* GetName() const; + void SetState(char const* state); + char const* GetState() const; + void SetDetails(char const* details); + char const* GetDetails() const; + ActivityTimestamps& GetTimestamps(); + ActivityTimestamps const& GetTimestamps() const; + ActivityAssets& GetAssets(); + ActivityAssets const& GetAssets() const; + ActivityParty& GetParty(); + ActivityParty const& GetParty() const; + ActivitySecrets& GetSecrets(); + ActivitySecrets const& GetSecrets() const; + void SetInstance(bool instance); + bool GetInstance() const; + +private: + DiscordActivity internal_; +}; + +class Presence final { +public: + void SetStatus(Status status); + Status GetStatus() const; + Activity& GetActivity(); + Activity const& GetActivity() const; + +private: + DiscordPresence internal_; +}; + +class Relationship final { +public: + void SetType(RelationshipType type); + RelationshipType GetType() const; + User& GetUser(); + User const& GetUser() const; + Presence& GetPresence(); + Presence const& GetPresence() const; + +private: + DiscordRelationship internal_; +}; + +class Lobby final { +public: + void SetId(LobbyId id); + LobbyId GetId() const; + void SetType(LobbyType type); + LobbyType GetType() const; + void SetOwnerId(UserId ownerId); + UserId GetOwnerId() const; + void SetSecret(LobbySecret secret); + LobbySecret GetSecret() const; + void SetCapacity(std::uint32_t capacity); + std::uint32_t GetCapacity() const; + void SetLocked(bool locked); + bool GetLocked() const; + +private: + DiscordLobby internal_; +}; + +class FileStat final { +public: + void SetFilename(char const* filename); + char const* GetFilename() const; + void SetSize(std::uint64_t size); + std::uint64_t GetSize() const; + void SetLastModified(std::uint64_t lastModified); + std::uint64_t GetLastModified() const; + +private: + DiscordFileStat internal_; +}; + +class Entitlement final { +public: + void SetId(Snowflake id); + Snowflake GetId() const; + void SetType(EntitlementType type); + EntitlementType GetType() const; + void SetSkuId(Snowflake skuId); + Snowflake GetSkuId() const; + +private: + DiscordEntitlement internal_; +}; + +class SkuPrice final { +public: + void SetAmount(std::uint32_t amount); + std::uint32_t GetAmount() const; + void SetCurrency(char const* currency); + char const* GetCurrency() const; + +private: + DiscordSkuPrice internal_; +}; + +class Sku final { +public: + void SetId(Snowflake id); + Snowflake GetId() const; + void SetType(SkuType type); + SkuType GetType() const; + void SetName(char const* name); + char const* GetName() const; + SkuPrice& GetPrice(); + SkuPrice const& GetPrice() const; + +private: + DiscordSku internal_; +}; + +class InputMode final { +public: + void SetType(InputModeType type); + InputModeType GetType() const; + void SetShortcut(char const* shortcut); + char const* GetShortcut() const; + +private: + DiscordInputMode internal_; +}; + +class UserAchievement final { +public: + void SetUserId(Snowflake userId); + Snowflake GetUserId() const; + void SetAchievementId(Snowflake achievementId); + Snowflake GetAchievementId() const; + void SetPercentComplete(std::uint8_t percentComplete); + std::uint8_t GetPercentComplete() const; + void SetUnlockedAt(DateTime unlockedAt); + DateTime GetUnlockedAt() const; + +private: + DiscordUserAchievement internal_; +}; + +class LobbyTransaction final { +public: + Result SetType(LobbyType type); + Result SetOwner(UserId ownerId); + Result SetCapacity(std::uint32_t capacity); + Result SetMetadata(MetadataKey key, MetadataValue value); + Result DeleteMetadata(MetadataKey key); + Result SetLocked(bool locked); + + IDiscordLobbyTransaction** Receive() { return &internal_; } + IDiscordLobbyTransaction* Internal() { return internal_; } + +private: + IDiscordLobbyTransaction* internal_; +}; + +class LobbyMemberTransaction final { +public: + Result SetMetadata(MetadataKey key, MetadataValue value); + Result DeleteMetadata(MetadataKey key); + + IDiscordLobbyMemberTransaction** Receive() { return &internal_; } + IDiscordLobbyMemberTransaction* Internal() { return internal_; } + +private: + IDiscordLobbyMemberTransaction* internal_; +}; + +class LobbySearchQuery final { +public: + Result Filter(MetadataKey key, + LobbySearchComparison comparison, + LobbySearchCast cast, + MetadataValue value); + Result Sort(MetadataKey key, LobbySearchCast cast, MetadataValue value); + Result Limit(std::uint32_t limit); + Result Distance(LobbySearchDistance distance); + + IDiscordLobbySearchQuery** Receive() { return &internal_; } + IDiscordLobbySearchQuery* Internal() { return internal_; } + +private: + IDiscordLobbySearchQuery* internal_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/user_manager.cpp b/libs/discordGameSDK/cpp/user_manager.cpp new file mode 100644 index 0000000..e704e28 --- /dev/null +++ b/libs/discordGameSDK/cpp/user_manager.cpp @@ -0,0 +1,80 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "user_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +class UserEvents final { +public: + static void OnCurrentUserUpdate(void* callbackData) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->UserManager(); + module.OnCurrentUserUpdate(); + } +}; + +IDiscordUserEvents UserManager::events_{ + &UserEvents::OnCurrentUserUpdate, +}; + +Result UserManager::GetCurrentUser(User* currentUser) +{ + if (!currentUser) { + return Result::InternalError; + } + + auto result = + internal_->get_current_user(internal_, reinterpret_cast(currentUser)); + return static_cast(result); +} + +void UserManager::GetUser(UserId userId, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result, DiscordUser* user) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result), *reinterpret_cast(user)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->get_user(internal_, userId, cb.release(), wrapper); +} + +Result UserManager::GetCurrentUserPremiumType(PremiumType* premiumType) +{ + if (!premiumType) { + return Result::InternalError; + } + + auto result = internal_->get_current_user_premium_type( + internal_, reinterpret_cast(premiumType)); + return static_cast(result); +} + +Result UserManager::CurrentUserHasFlag(UserFlag flag, bool* hasFlag) +{ + if (!hasFlag) { + return Result::InternalError; + } + + auto result = internal_->current_user_has_flag( + internal_, static_cast(flag), reinterpret_cast(hasFlag)); + return static_cast(result); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/user_manager.h b/libs/discordGameSDK/cpp/user_manager.h new file mode 100644 index 0000000..d85de1b --- /dev/null +++ b/libs/discordGameSDK/cpp/user_manager.h @@ -0,0 +1,31 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class UserManager final { +public: + ~UserManager() = default; + + Result GetCurrentUser(User* currentUser); + void GetUser(UserId userId, std::function callback); + Result GetCurrentUserPremiumType(PremiumType* premiumType); + Result CurrentUserHasFlag(UserFlag flag, bool* hasFlag); + + Event<> OnCurrentUserUpdate; + +private: + friend class Core; + + UserManager() = default; + UserManager(UserManager const& rhs) = delete; + UserManager& operator=(UserManager const& rhs) = delete; + UserManager(UserManager&& rhs) = delete; + UserManager& operator=(UserManager&& rhs) = delete; + + IDiscordUserManager* internal_; + static IDiscordUserEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/voice_manager.cpp b/libs/discordGameSDK/cpp/voice_manager.cpp new file mode 100644 index 0000000..a92d4bd --- /dev/null +++ b/libs/discordGameSDK/cpp/voice_manager.cpp @@ -0,0 +1,124 @@ +#if !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "voice_manager.h" + +#include "core.h" + +#include +#include + +namespace discord { + +class VoiceEvents final { +public: + static void OnSettingsUpdate(void* callbackData) + { + auto* core = reinterpret_cast(callbackData); + if (!core) { + return; + } + + auto& module = core->VoiceManager(); + module.OnSettingsUpdate(); + } +}; + +IDiscordVoiceEvents VoiceManager::events_{ + &VoiceEvents::OnSettingsUpdate, +}; + +Result VoiceManager::GetInputMode(InputMode* inputMode) +{ + if (!inputMode) { + return Result::InternalError; + } + + auto result = + internal_->get_input_mode(internal_, reinterpret_cast(inputMode)); + return static_cast(result); +} + +void VoiceManager::SetInputMode(InputMode inputMode, std::function callback) +{ + static auto wrapper = [](void* callbackData, EDiscordResult result) -> void { + std::unique_ptr> cb( + reinterpret_cast*>(callbackData)); + if (!cb || !(*cb)) { + return; + } + (*cb)(static_cast(result)); + }; + std::unique_ptr> cb{}; + cb.reset(new std::function(std::move(callback))); + internal_->set_input_mode( + internal_, *reinterpret_cast(&inputMode), cb.release(), wrapper); +} + +Result VoiceManager::IsSelfMute(bool* mute) +{ + if (!mute) { + return Result::InternalError; + } + + auto result = internal_->is_self_mute(internal_, reinterpret_cast(mute)); + return static_cast(result); +} + +Result VoiceManager::SetSelfMute(bool mute) +{ + auto result = internal_->set_self_mute(internal_, (mute ? 1 : 0)); + return static_cast(result); +} + +Result VoiceManager::IsSelfDeaf(bool* deaf) +{ + if (!deaf) { + return Result::InternalError; + } + + auto result = internal_->is_self_deaf(internal_, reinterpret_cast(deaf)); + return static_cast(result); +} + +Result VoiceManager::SetSelfDeaf(bool deaf) +{ + auto result = internal_->set_self_deaf(internal_, (deaf ? 1 : 0)); + return static_cast(result); +} + +Result VoiceManager::IsLocalMute(Snowflake userId, bool* mute) +{ + if (!mute) { + return Result::InternalError; + } + + auto result = internal_->is_local_mute(internal_, userId, reinterpret_cast(mute)); + return static_cast(result); +} + +Result VoiceManager::SetLocalMute(Snowflake userId, bool mute) +{ + auto result = internal_->set_local_mute(internal_, userId, (mute ? 1 : 0)); + return static_cast(result); +} + +Result VoiceManager::GetLocalVolume(Snowflake userId, std::uint8_t* volume) +{ + if (!volume) { + return Result::InternalError; + } + + auto result = + internal_->get_local_volume(internal_, userId, reinterpret_cast(volume)); + return static_cast(result); +} + +Result VoiceManager::SetLocalVolume(Snowflake userId, std::uint8_t volume) +{ + auto result = internal_->set_local_volume(internal_, userId, volume); + return static_cast(result); +} + +} // namespace discord diff --git a/libs/discordGameSDK/cpp/voice_manager.h b/libs/discordGameSDK/cpp/voice_manager.h new file mode 100644 index 0000000..95b20e9 --- /dev/null +++ b/libs/discordGameSDK/cpp/voice_manager.h @@ -0,0 +1,37 @@ +#pragma once + +#include "types.h" + +namespace discord { + +class VoiceManager final { +public: + ~VoiceManager() = default; + + Result GetInputMode(InputMode* inputMode); + void SetInputMode(InputMode inputMode, std::function callback); + Result IsSelfMute(bool* mute); + Result SetSelfMute(bool mute); + Result IsSelfDeaf(bool* deaf); + Result SetSelfDeaf(bool deaf); + Result IsLocalMute(Snowflake userId, bool* mute); + Result SetLocalMute(Snowflake userId, bool mute); + Result GetLocalVolume(Snowflake userId, std::uint8_t* volume); + Result SetLocalVolume(Snowflake userId, std::uint8_t volume); + + Event<> OnSettingsUpdate; + +private: + friend class Core; + + VoiceManager() = default; + VoiceManager(VoiceManager const& rhs) = delete; + VoiceManager& operator=(VoiceManager const& rhs) = delete; + VoiceManager(VoiceManager&& rhs) = delete; + VoiceManager& operator=(VoiceManager&& rhs) = delete; + + IDiscordVoiceManager* internal_; + static IDiscordVoiceEvents events_; +}; + +} // namespace discord diff --git a/libs/discordGameSDK/csharp/ActivityManager.cs b/libs/discordGameSDK/csharp/ActivityManager.cs new file mode 100644 index 0000000..048e3b4 --- /dev/null +++ b/libs/discordGameSDK/csharp/ActivityManager.cs @@ -0,0 +1,12 @@ +using System; + +namespace Discord +{ + public partial class ActivityManager + { + public void RegisterCommand() + { + RegisterCommand(null); + } + } +} diff --git a/libs/discordGameSDK/csharp/Constants.cs b/libs/discordGameSDK/csharp/Constants.cs new file mode 100644 index 0000000..8e77e1b --- /dev/null +++ b/libs/discordGameSDK/csharp/Constants.cs @@ -0,0 +1,9 @@ +using System; + +namespace Discord +{ + static class Constants + { + public const string DllName = "discord_game_sdk"; + } +} diff --git a/libs/discordGameSDK/csharp/Core.cs b/libs/discordGameSDK/csharp/Core.cs new file mode 100644 index 0000000..fa95765 --- /dev/null +++ b/libs/discordGameSDK/csharp/Core.cs @@ -0,0 +1,4199 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace Discord +{ + public enum Result + { + Ok = 0, + ServiceUnavailable = 1, + InvalidVersion = 2, + LockFailed = 3, + InternalError = 4, + InvalidPayload = 5, + InvalidCommand = 6, + InvalidPermissions = 7, + NotFetched = 8, + NotFound = 9, + Conflict = 10, + InvalidSecret = 11, + InvalidJoinSecret = 12, + NoEligibleActivity = 13, + InvalidInvite = 14, + NotAuthenticated = 15, + InvalidAccessToken = 16, + ApplicationMismatch = 17, + InvalidDataUrl = 18, + InvalidBase64 = 19, + NotFiltered = 20, + LobbyFull = 21, + InvalidLobbySecret = 22, + InvalidFilename = 23, + InvalidFileSize = 24, + InvalidEntitlement = 25, + NotInstalled = 26, + NotRunning = 27, + InsufficientBuffer = 28, + PurchaseCanceled = 29, + InvalidGuild = 30, + InvalidEvent = 31, + InvalidChannel = 32, + InvalidOrigin = 33, + RateLimited = 34, + OAuth2Error = 35, + SelectChannelTimeout = 36, + GetGuildTimeout = 37, + SelectVoiceForceRequired = 38, + CaptureShortcutAlreadyListening = 39, + UnauthorizedForAchievement = 40, + InvalidGiftCode = 41, + PurchaseError = 42, + TransactionAborted = 43, + } + + public enum CreateFlags + { + Default = 0, + NoRequireDiscord = 1, + } + + public enum LogLevel + { + Error = 1, + Warn, + Info, + Debug, + } + + public enum UserFlag + { + Partner = 2, + HypeSquadEvents = 4, + HypeSquadHouse1 = 64, + HypeSquadHouse2 = 128, + HypeSquadHouse3 = 256, + } + + public enum PremiumType + { + None = 0, + Tier1 = 1, + Tier2 = 2, + } + + public enum ImageType + { + User, + } + + public enum ActivityType + { + Playing, + Streaming, + Listening, + Watching, + } + + public enum ActivityActionType + { + Join = 1, + Spectate, + } + + public enum ActivityJoinRequestReply + { + No, + Yes, + Ignore, + } + + public enum Status + { + Offline = 0, + Online = 1, + Idle = 2, + DoNotDisturb = 3, + } + + public enum RelationshipType + { + None, + Friend, + Blocked, + PendingIncoming, + PendingOutgoing, + Implicit, + } + + public enum LobbyType + { + Private = 1, + Public, + } + + public enum LobbySearchComparison + { + LessThanOrEqual = -2, + LessThan, + Equal, + GreaterThan, + GreaterThanOrEqual, + NotEqual, + } + + public enum LobbySearchCast + { + String = 1, + Number, + } + + public enum LobbySearchDistance + { + Local, + Default, + Extended, + Global, + } + + public enum EntitlementType + { + Purchase = 1, + PremiumSubscription, + DeveloperGift, + TestModePurchase, + FreePurchase, + UserGift, + PremiumPurchase, + } + + public enum SkuType + { + Application = 1, + DLC, + Consumable, + Bundle, + } + + public enum InputModeType + { + VoiceActivity = 0, + PushToTalk, + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct User + { + public Int64 Id; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] + public string Username; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)] + public string Discriminator; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string Avatar; + + public bool Bot; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct OAuth2Token + { + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string AccessToken; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] + public string Scopes; + + public Int64 Expires; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct ImageHandle + { + public ImageType Type; + + public Int64 Id; + + public UInt32 Size; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct ImageDimensions + { + public UInt32 Width; + + public UInt32 Height; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct ActivityTimestamps + { + public Int64 Start; + + public Int64 End; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct ActivityAssets + { + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string LargeImage; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string LargeText; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string SmallImage; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string SmallText; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct PartySize + { + public Int32 CurrentSize; + + public Int32 MaxSize; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct ActivityParty + { + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string Id; + + public PartySize Size; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct ActivitySecrets + { + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string Match; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string Join; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string Spectate; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct Activity + { + public ActivityType Type; + + public Int64 ApplicationId; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string Name; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string State; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string Details; + + public ActivityTimestamps Timestamps; + + public ActivityAssets Assets; + + public ActivityParty Party; + + public ActivitySecrets Secrets; + + public bool Instance; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct Presence + { + public Status Status; + + public Activity Activity; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct Relationship + { + public RelationshipType Type; + + public User User; + + public Presence Presence; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct Lobby + { + public Int64 Id; + + public LobbyType Type; + + public Int64 OwnerId; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] + public string Secret; + + public UInt32 Capacity; + + public bool Locked; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct FileStat + { + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] + public string Filename; + + public UInt64 Size; + + public UInt64 LastModified; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct Entitlement + { + public Int64 Id; + + public EntitlementType Type; + + public Int64 SkuId; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct SkuPrice + { + public UInt32 Amount; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] + public string Currency; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct Sku + { + public Int64 Id; + + public SkuType Type; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] + public string Name; + + public SkuPrice Price; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct InputMode + { + public InputModeType Type; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] + public string Shortcut; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct UserAchievement + { + public Int64 UserId; + + public Int64 AchievementId; + + public byte PercentComplete; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] + public string UnlockedAt; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct LobbyTransaction + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetTypeMethod(IntPtr methodsPtr, LobbyType type); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetOwnerMethod(IntPtr methodsPtr, Int64 ownerId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetCapacityMethod(IntPtr methodsPtr, UInt32 capacity); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetMetadataMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key, [MarshalAs(UnmanagedType.LPStr)]string value); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result DeleteMetadataMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetLockedMethod(IntPtr methodsPtr, bool locked); + + internal SetTypeMethod SetType; + + internal SetOwnerMethod SetOwner; + + internal SetCapacityMethod SetCapacity; + + internal SetMetadataMethod SetMetadata; + + internal DeleteMetadataMethod DeleteMetadata; + + internal SetLockedMethod SetLocked; + } + + internal IntPtr MethodsPtr; + + internal Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public void SetType(LobbyType type) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.SetType(MethodsPtr, type); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public void SetOwner(Int64 ownerId) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.SetOwner(MethodsPtr, ownerId); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public void SetCapacity(UInt32 capacity) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.SetCapacity(MethodsPtr, capacity); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public void SetMetadata(string key, string value) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.SetMetadata(MethodsPtr, key, value); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public void DeleteMetadata(string key) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.DeleteMetadata(MethodsPtr, key); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public void SetLocked(bool locked) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.SetLocked(MethodsPtr, locked); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct LobbyMemberTransaction + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetMetadataMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key, [MarshalAs(UnmanagedType.LPStr)]string value); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result DeleteMetadataMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key); + + internal SetMetadataMethod SetMetadata; + + internal DeleteMetadataMethod DeleteMetadata; + } + + internal IntPtr MethodsPtr; + + internal Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public void SetMetadata(string key, string value) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.SetMetadata(MethodsPtr, key, value); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public void DeleteMetadata(string key) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.DeleteMetadata(MethodsPtr, key); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public partial struct LobbySearchQuery + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result FilterMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key, LobbySearchComparison comparison, LobbySearchCast cast, [MarshalAs(UnmanagedType.LPStr)]string value); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SortMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key, LobbySearchCast cast, [MarshalAs(UnmanagedType.LPStr)]string value); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result LimitMethod(IntPtr methodsPtr, UInt32 limit); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result DistanceMethod(IntPtr methodsPtr, LobbySearchDistance distance); + + internal FilterMethod Filter; + + internal SortMethod Sort; + + internal LimitMethod Limit; + + internal DistanceMethod Distance; + } + + internal IntPtr MethodsPtr; + + internal Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public void Filter(string key, LobbySearchComparison comparison, LobbySearchCast cast, string value) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.Filter(MethodsPtr, key, comparison, cast, value); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public void Sort(string key, LobbySearchCast cast, string value) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.Sort(MethodsPtr, key, cast, value); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public void Limit(UInt32 limit) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.Limit(MethodsPtr, limit); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public void Distance(LobbySearchDistance distance) + { + if (MethodsPtr != IntPtr.Zero) + { + var res = Methods.Distance(MethodsPtr, distance); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + } + + public partial class ResultException : Exception + { + public readonly Result Result; + + public ResultException(Result result) : base(result.ToString()) + { + } + } + + public partial class Discord : IDisposable + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void DestroyHandler(IntPtr MethodsPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result RunCallbacksMethod(IntPtr methodsPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SetLogHookCallback(IntPtr ptr, LogLevel level, [MarshalAs(UnmanagedType.LPStr)]string message); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SetLogHookMethod(IntPtr methodsPtr, LogLevel minLevel, IntPtr callbackData, SetLogHookCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetApplicationManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetUserManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetImageManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetActivityManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetRelationshipManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetLobbyManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetNetworkManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetOverlayManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetStorageManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetStoreManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetVoiceManagerMethod(IntPtr discordPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate IntPtr GetAchievementManagerMethod(IntPtr discordPtr); + + internal DestroyHandler Destroy; + + internal RunCallbacksMethod RunCallbacks; + + internal SetLogHookMethod SetLogHook; + + internal GetApplicationManagerMethod GetApplicationManager; + + internal GetUserManagerMethod GetUserManager; + + internal GetImageManagerMethod GetImageManager; + + internal GetActivityManagerMethod GetActivityManager; + + internal GetRelationshipManagerMethod GetRelationshipManager; + + internal GetLobbyManagerMethod GetLobbyManager; + + internal GetNetworkManagerMethod GetNetworkManager; + + internal GetOverlayManagerMethod GetOverlayManager; + + internal GetStorageManagerMethod GetStorageManager; + + internal GetStoreManagerMethod GetStoreManager; + + internal GetVoiceManagerMethod GetVoiceManager; + + internal GetAchievementManagerMethod GetAchievementManager; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFICreateParams + { + internal Int64 ClientId; + + internal UInt64 Flags; + + internal IntPtr Events; + + internal IntPtr EventData; + + internal IntPtr ApplicationEvents; + + internal UInt32 ApplicationVersion; + + internal IntPtr UserEvents; + + internal UInt32 UserVersion; + + internal IntPtr ImageEvents; + + internal UInt32 ImageVersion; + + internal IntPtr ActivityEvents; + + internal UInt32 ActivityVersion; + + internal IntPtr RelationshipEvents; + + internal UInt32 RelationshipVersion; + + internal IntPtr LobbyEvents; + + internal UInt32 LobbyVersion; + + internal IntPtr NetworkEvents; + + internal UInt32 NetworkVersion; + + internal IntPtr OverlayEvents; + + internal UInt32 OverlayVersion; + + internal IntPtr StorageEvents; + + internal UInt32 StorageVersion; + + internal IntPtr StoreEvents; + + internal UInt32 StoreVersion; + + internal IntPtr VoiceEvents; + + internal UInt32 VoiceVersion; + + internal IntPtr AchievementEvents; + + internal UInt32 AchievementVersion; + } + + [DllImport(Constants.DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] + private static extern Result DiscordCreate(UInt32 version, ref FFICreateParams createParams, out IntPtr manager); + + public delegate void SetLogHookHandler(LogLevel level, string message); + + private GCHandle SelfHandle; + + private IntPtr EventsPtr; + + private FFIEvents Events; + + private IntPtr ApplicationEventsPtr; + + private ApplicationManager.FFIEvents ApplicationEvents; + + internal ApplicationManager ApplicationManagerInstance; + + private IntPtr UserEventsPtr; + + private UserManager.FFIEvents UserEvents; + + internal UserManager UserManagerInstance; + + private IntPtr ImageEventsPtr; + + private ImageManager.FFIEvents ImageEvents; + + internal ImageManager ImageManagerInstance; + + private IntPtr ActivityEventsPtr; + + private ActivityManager.FFIEvents ActivityEvents; + + internal ActivityManager ActivityManagerInstance; + + private IntPtr RelationshipEventsPtr; + + private RelationshipManager.FFIEvents RelationshipEvents; + + internal RelationshipManager RelationshipManagerInstance; + + private IntPtr LobbyEventsPtr; + + private LobbyManager.FFIEvents LobbyEvents; + + internal LobbyManager LobbyManagerInstance; + + private IntPtr NetworkEventsPtr; + + private NetworkManager.FFIEvents NetworkEvents; + + internal NetworkManager NetworkManagerInstance; + + private IntPtr OverlayEventsPtr; + + private OverlayManager.FFIEvents OverlayEvents; + + internal OverlayManager OverlayManagerInstance; + + private IntPtr StorageEventsPtr; + + private StorageManager.FFIEvents StorageEvents; + + internal StorageManager StorageManagerInstance; + + private IntPtr StoreEventsPtr; + + private StoreManager.FFIEvents StoreEvents; + + internal StoreManager StoreManagerInstance; + + private IntPtr VoiceEventsPtr; + + private VoiceManager.FFIEvents VoiceEvents; + + internal VoiceManager VoiceManagerInstance; + + private IntPtr AchievementEventsPtr; + + private AchievementManager.FFIEvents AchievementEvents; + + internal AchievementManager AchievementManagerInstance; + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + private GCHandle? setLogHook; + + public Discord(Int64 clientId, UInt64 flags) + { + FFICreateParams createParams; + createParams.ClientId = clientId; + createParams.Flags = flags; + Events = new FFIEvents(); + EventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Events)); + createParams.Events = EventsPtr; + SelfHandle = GCHandle.Alloc(this); + createParams.EventData = GCHandle.ToIntPtr(SelfHandle); + ApplicationEvents = new ApplicationManager.FFIEvents(); + ApplicationEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ApplicationEvents)); + createParams.ApplicationEvents = ApplicationEventsPtr; + createParams.ApplicationVersion = 1; + UserEvents = new UserManager.FFIEvents(); + UserEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(UserEvents)); + createParams.UserEvents = UserEventsPtr; + createParams.UserVersion = 1; + ImageEvents = new ImageManager.FFIEvents(); + ImageEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ImageEvents)); + createParams.ImageEvents = ImageEventsPtr; + createParams.ImageVersion = 1; + ActivityEvents = new ActivityManager.FFIEvents(); + ActivityEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ActivityEvents)); + createParams.ActivityEvents = ActivityEventsPtr; + createParams.ActivityVersion = 1; + RelationshipEvents = new RelationshipManager.FFIEvents(); + RelationshipEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(RelationshipEvents)); + createParams.RelationshipEvents = RelationshipEventsPtr; + createParams.RelationshipVersion = 1; + LobbyEvents = new LobbyManager.FFIEvents(); + LobbyEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(LobbyEvents)); + createParams.LobbyEvents = LobbyEventsPtr; + createParams.LobbyVersion = 1; + NetworkEvents = new NetworkManager.FFIEvents(); + NetworkEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(NetworkEvents)); + createParams.NetworkEvents = NetworkEventsPtr; + createParams.NetworkVersion = 1; + OverlayEvents = new OverlayManager.FFIEvents(); + OverlayEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(OverlayEvents)); + createParams.OverlayEvents = OverlayEventsPtr; + createParams.OverlayVersion = 1; + StorageEvents = new StorageManager.FFIEvents(); + StorageEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(StorageEvents)); + createParams.StorageEvents = StorageEventsPtr; + createParams.StorageVersion = 1; + StoreEvents = new StoreManager.FFIEvents(); + StoreEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(StoreEvents)); + createParams.StoreEvents = StoreEventsPtr; + createParams.StoreVersion = 1; + VoiceEvents = new VoiceManager.FFIEvents(); + VoiceEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(VoiceEvents)); + createParams.VoiceEvents = VoiceEventsPtr; + createParams.VoiceVersion = 1; + AchievementEvents = new AchievementManager.FFIEvents(); + AchievementEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(AchievementEvents)); + createParams.AchievementEvents = AchievementEventsPtr; + createParams.AchievementVersion = 1; + InitEvents(EventsPtr, ref Events); + var result = DiscordCreate(2, ref createParams, out MethodsPtr); + if (result != Result.Ok) + { + Dispose(); + throw new ResultException(result); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + Marshal.StructureToPtr(events, eventsPtr, false); + } + + public void Dispose() + { + if (MethodsPtr != IntPtr.Zero) + { + Methods.Destroy(MethodsPtr); + } + SelfHandle.Free(); + Marshal.FreeHGlobal(EventsPtr); + Marshal.FreeHGlobal(ApplicationEventsPtr); + Marshal.FreeHGlobal(UserEventsPtr); + Marshal.FreeHGlobal(ImageEventsPtr); + Marshal.FreeHGlobal(ActivityEventsPtr); + Marshal.FreeHGlobal(RelationshipEventsPtr); + Marshal.FreeHGlobal(LobbyEventsPtr); + Marshal.FreeHGlobal(NetworkEventsPtr); + Marshal.FreeHGlobal(OverlayEventsPtr); + Marshal.FreeHGlobal(StorageEventsPtr); + Marshal.FreeHGlobal(StoreEventsPtr); + Marshal.FreeHGlobal(VoiceEventsPtr); + Marshal.FreeHGlobal(AchievementEventsPtr); + if (setLogHook.HasValue) { + setLogHook.Value.Free(); + } + } + + public void RunCallbacks() + { + var res = Methods.RunCallbacks(MethodsPtr); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + [MonoPInvokeCallback] + private static void SetLogHookCallbackImpl(IntPtr ptr, LogLevel level, string message) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + SetLogHookHandler callback = (SetLogHookHandler)h.Target; + callback(level, message); + } + + public void SetLogHook(LogLevel minLevel, SetLogHookHandler callback) + { + if (setLogHook.HasValue) { + setLogHook.Value.Free(); + } + setLogHook = GCHandle.Alloc(callback); + Methods.SetLogHook(MethodsPtr, minLevel, GCHandle.ToIntPtr(setLogHook.Value), SetLogHookCallbackImpl); + } + + public ApplicationManager GetApplicationManager() + { + if (ApplicationManagerInstance == null) { + ApplicationManagerInstance = new ApplicationManager( + Methods.GetApplicationManager(MethodsPtr), + ApplicationEventsPtr, + ref ApplicationEvents + ); + } + return ApplicationManagerInstance; + } + + public UserManager GetUserManager() + { + if (UserManagerInstance == null) { + UserManagerInstance = new UserManager( + Methods.GetUserManager(MethodsPtr), + UserEventsPtr, + ref UserEvents + ); + } + return UserManagerInstance; + } + + public ImageManager GetImageManager() + { + if (ImageManagerInstance == null) { + ImageManagerInstance = new ImageManager( + Methods.GetImageManager(MethodsPtr), + ImageEventsPtr, + ref ImageEvents + ); + } + return ImageManagerInstance; + } + + public ActivityManager GetActivityManager() + { + if (ActivityManagerInstance == null) { + ActivityManagerInstance = new ActivityManager( + Methods.GetActivityManager(MethodsPtr), + ActivityEventsPtr, + ref ActivityEvents + ); + } + return ActivityManagerInstance; + } + + public RelationshipManager GetRelationshipManager() + { + if (RelationshipManagerInstance == null) { + RelationshipManagerInstance = new RelationshipManager( + Methods.GetRelationshipManager(MethodsPtr), + RelationshipEventsPtr, + ref RelationshipEvents + ); + } + return RelationshipManagerInstance; + } + + public LobbyManager GetLobbyManager() + { + if (LobbyManagerInstance == null) { + LobbyManagerInstance = new LobbyManager( + Methods.GetLobbyManager(MethodsPtr), + LobbyEventsPtr, + ref LobbyEvents + ); + } + return LobbyManagerInstance; + } + + public NetworkManager GetNetworkManager() + { + if (NetworkManagerInstance == null) { + NetworkManagerInstance = new NetworkManager( + Methods.GetNetworkManager(MethodsPtr), + NetworkEventsPtr, + ref NetworkEvents + ); + } + return NetworkManagerInstance; + } + + public OverlayManager GetOverlayManager() + { + if (OverlayManagerInstance == null) { + OverlayManagerInstance = new OverlayManager( + Methods.GetOverlayManager(MethodsPtr), + OverlayEventsPtr, + ref OverlayEvents + ); + } + return OverlayManagerInstance; + } + + public StorageManager GetStorageManager() + { + if (StorageManagerInstance == null) { + StorageManagerInstance = new StorageManager( + Methods.GetStorageManager(MethodsPtr), + StorageEventsPtr, + ref StorageEvents + ); + } + return StorageManagerInstance; + } + + public StoreManager GetStoreManager() + { + if (StoreManagerInstance == null) { + StoreManagerInstance = new StoreManager( + Methods.GetStoreManager(MethodsPtr), + StoreEventsPtr, + ref StoreEvents + ); + } + return StoreManagerInstance; + } + + public VoiceManager GetVoiceManager() + { + if (VoiceManagerInstance == null) { + VoiceManagerInstance = new VoiceManager( + Methods.GetVoiceManager(MethodsPtr), + VoiceEventsPtr, + ref VoiceEvents + ); + } + return VoiceManagerInstance; + } + + public AchievementManager GetAchievementManager() + { + if (AchievementManagerInstance == null) { + AchievementManagerInstance = new AchievementManager( + Methods.GetAchievementManager(MethodsPtr), + AchievementEventsPtr, + ref AchievementEvents + ); + } + return AchievementManagerInstance; + } + } + + internal partial class MonoPInvokeCallbackAttribute : Attribute + { + + } + + public partial class ApplicationManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ValidateOrExitCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ValidateOrExitMethod(IntPtr methodsPtr, IntPtr callbackData, ValidateOrExitCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void GetCurrentLocaleMethod(IntPtr methodsPtr, StringBuilder locale); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void GetCurrentBranchMethod(IntPtr methodsPtr, StringBuilder branch); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void GetOAuth2TokenCallback(IntPtr ptr, Result result, ref OAuth2Token oauth2Token); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void GetOAuth2TokenMethod(IntPtr methodsPtr, IntPtr callbackData, GetOAuth2TokenCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void GetTicketCallback(IntPtr ptr, Result result, [MarshalAs(UnmanagedType.LPStr)]ref string data); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void GetTicketMethod(IntPtr methodsPtr, IntPtr callbackData, GetTicketCallback callback); + + internal ValidateOrExitMethod ValidateOrExit; + + internal GetCurrentLocaleMethod GetCurrentLocale; + + internal GetCurrentBranchMethod GetCurrentBranch; + + internal GetOAuth2TokenMethod GetOAuth2Token; + + internal GetTicketMethod GetTicket; + } + + public delegate void ValidateOrExitHandler(Result result); + + public delegate void GetOAuth2TokenHandler(Result result, ref OAuth2Token oauth2Token); + + public delegate void GetTicketHandler(Result result, ref string data); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + internal ApplicationManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + Marshal.StructureToPtr(events, eventsPtr, false); + } + + [MonoPInvokeCallback] + private static void ValidateOrExitCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + ValidateOrExitHandler callback = (ValidateOrExitHandler)h.Target; + h.Free(); + callback(result); + } + + public void ValidateOrExit(ValidateOrExitHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.ValidateOrExit(MethodsPtr, GCHandle.ToIntPtr(wrapped), ValidateOrExitCallbackImpl); + } + + public string GetCurrentLocale() + { + var ret = new StringBuilder(128); + Methods.GetCurrentLocale(MethodsPtr, ret); + return ret.ToString(); + } + + public string GetCurrentBranch() + { + var ret = new StringBuilder(4096); + Methods.GetCurrentBranch(MethodsPtr, ret); + return ret.ToString(); + } + + [MonoPInvokeCallback] + private static void GetOAuth2TokenCallbackImpl(IntPtr ptr, Result result, ref OAuth2Token oauth2Token) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + GetOAuth2TokenHandler callback = (GetOAuth2TokenHandler)h.Target; + h.Free(); + callback(result, ref oauth2Token); + } + + public void GetOAuth2Token(GetOAuth2TokenHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.GetOAuth2Token(MethodsPtr, GCHandle.ToIntPtr(wrapped), GetOAuth2TokenCallbackImpl); + } + + [MonoPInvokeCallback] + private static void GetTicketCallbackImpl(IntPtr ptr, Result result, ref string data) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + GetTicketHandler callback = (GetTicketHandler)h.Target; + h.Free(); + callback(result, ref data); + } + + public void GetTicket(GetTicketHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.GetTicket(MethodsPtr, GCHandle.ToIntPtr(wrapped), GetTicketCallbackImpl); + } + } + + public partial class UserManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void CurrentUserUpdateHandler(IntPtr ptr); + + internal CurrentUserUpdateHandler OnCurrentUserUpdate; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetCurrentUserMethod(IntPtr methodsPtr, ref User currentUser); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void GetUserCallback(IntPtr ptr, Result result, ref User user); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void GetUserMethod(IntPtr methodsPtr, Int64 userId, IntPtr callbackData, GetUserCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetCurrentUserPremiumTypeMethod(IntPtr methodsPtr, ref PremiumType premiumType); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result CurrentUserHasFlagMethod(IntPtr methodsPtr, UserFlag flag, ref bool hasFlag); + + internal GetCurrentUserMethod GetCurrentUser; + + internal GetUserMethod GetUser; + + internal GetCurrentUserPremiumTypeMethod GetCurrentUserPremiumType; + + internal CurrentUserHasFlagMethod CurrentUserHasFlag; + } + + public delegate void GetUserHandler(Result result, ref User user); + + public delegate void CurrentUserUpdateHandler(); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public event CurrentUserUpdateHandler OnCurrentUserUpdate; + + internal UserManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + events.OnCurrentUserUpdate = OnCurrentUserUpdateImpl; + Marshal.StructureToPtr(events, eventsPtr, false); + } + + public User GetCurrentUser() + { + var ret = new User(); + var res = Methods.GetCurrentUser(MethodsPtr, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void GetUserCallbackImpl(IntPtr ptr, Result result, ref User user) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + GetUserHandler callback = (GetUserHandler)h.Target; + h.Free(); + callback(result, ref user); + } + + public void GetUser(Int64 userId, GetUserHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.GetUser(MethodsPtr, userId, GCHandle.ToIntPtr(wrapped), GetUserCallbackImpl); + } + + public PremiumType GetCurrentUserPremiumType() + { + var ret = new PremiumType(); + var res = Methods.GetCurrentUserPremiumType(MethodsPtr, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public bool CurrentUserHasFlag(UserFlag flag) + { + var ret = new bool(); + var res = Methods.CurrentUserHasFlag(MethodsPtr, flag, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void OnCurrentUserUpdateImpl(IntPtr ptr) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.UserManagerInstance.OnCurrentUserUpdate != null) + { + d.UserManagerInstance.OnCurrentUserUpdate.Invoke(); + } + } + } + + public partial class ImageManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FetchCallback(IntPtr ptr, Result result, ImageHandle handleResult); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FetchMethod(IntPtr methodsPtr, ImageHandle handle, bool refresh, IntPtr callbackData, FetchCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetDimensionsMethod(IntPtr methodsPtr, ImageHandle handle, ref ImageDimensions dimensions); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetDataMethod(IntPtr methodsPtr, ImageHandle handle, byte[] data, Int32 dataLen); + + internal FetchMethod Fetch; + + internal GetDimensionsMethod GetDimensions; + + internal GetDataMethod GetData; + } + + public delegate void FetchHandler(Result result, ImageHandle handleResult); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + internal ImageManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + Marshal.StructureToPtr(events, eventsPtr, false); + } + + [MonoPInvokeCallback] + private static void FetchCallbackImpl(IntPtr ptr, Result result, ImageHandle handleResult) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + FetchHandler callback = (FetchHandler)h.Target; + h.Free(); + callback(result, handleResult); + } + + public void Fetch(ImageHandle handle, bool refresh, FetchHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.Fetch(MethodsPtr, handle, refresh, GCHandle.ToIntPtr(wrapped), FetchCallbackImpl); + } + + public ImageDimensions GetDimensions(ImageHandle handle) + { + var ret = new ImageDimensions(); + var res = Methods.GetDimensions(MethodsPtr, handle, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public void GetData(ImageHandle handle, byte[] data) + { + var res = Methods.GetData(MethodsPtr, handle, data, data.Length); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + } + + public partial class ActivityManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ActivityJoinHandler(IntPtr ptr, [MarshalAs(UnmanagedType.LPStr)]string secret); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ActivitySpectateHandler(IntPtr ptr, [MarshalAs(UnmanagedType.LPStr)]string secret); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ActivityJoinRequestHandler(IntPtr ptr, ref User user); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ActivityInviteHandler(IntPtr ptr, ActivityActionType type, ref User user, ref Activity activity); + + internal ActivityJoinHandler OnActivityJoin; + + internal ActivitySpectateHandler OnActivitySpectate; + + internal ActivityJoinRequestHandler OnActivityJoinRequest; + + internal ActivityInviteHandler OnActivityInvite; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result RegisterCommandMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string command); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result RegisterSteamMethod(IntPtr methodsPtr, UInt32 steamId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void UpdateActivityCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void UpdateActivityMethod(IntPtr methodsPtr, ref Activity activity, IntPtr callbackData, UpdateActivityCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ClearActivityCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ClearActivityMethod(IntPtr methodsPtr, IntPtr callbackData, ClearActivityCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SendRequestReplyCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SendRequestReplyMethod(IntPtr methodsPtr, Int64 userId, ActivityJoinRequestReply reply, IntPtr callbackData, SendRequestReplyCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SendInviteCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SendInviteMethod(IntPtr methodsPtr, Int64 userId, ActivityActionType type, [MarshalAs(UnmanagedType.LPStr)]string content, IntPtr callbackData, SendInviteCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void AcceptInviteCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void AcceptInviteMethod(IntPtr methodsPtr, Int64 userId, IntPtr callbackData, AcceptInviteCallback callback); + + internal RegisterCommandMethod RegisterCommand; + + internal RegisterSteamMethod RegisterSteam; + + internal UpdateActivityMethod UpdateActivity; + + internal ClearActivityMethod ClearActivity; + + internal SendRequestReplyMethod SendRequestReply; + + internal SendInviteMethod SendInvite; + + internal AcceptInviteMethod AcceptInvite; + } + + public delegate void UpdateActivityHandler(Result result); + + public delegate void ClearActivityHandler(Result result); + + public delegate void SendRequestReplyHandler(Result result); + + public delegate void SendInviteHandler(Result result); + + public delegate void AcceptInviteHandler(Result result); + + public delegate void ActivityJoinHandler(string secret); + + public delegate void ActivitySpectateHandler(string secret); + + public delegate void ActivityJoinRequestHandler(ref User user); + + public delegate void ActivityInviteHandler(ActivityActionType type, ref User user, ref Activity activity); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public event ActivityJoinHandler OnActivityJoin; + + public event ActivitySpectateHandler OnActivitySpectate; + + public event ActivityJoinRequestHandler OnActivityJoinRequest; + + public event ActivityInviteHandler OnActivityInvite; + + internal ActivityManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + events.OnActivityJoin = OnActivityJoinImpl; + events.OnActivitySpectate = OnActivitySpectateImpl; + events.OnActivityJoinRequest = OnActivityJoinRequestImpl; + events.OnActivityInvite = OnActivityInviteImpl; + Marshal.StructureToPtr(events, eventsPtr, false); + } + + public void RegisterCommand(string command) + { + var res = Methods.RegisterCommand(MethodsPtr, command); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + public void RegisterSteam(UInt32 steamId) + { + var res = Methods.RegisterSteam(MethodsPtr, steamId); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + [MonoPInvokeCallback] + private static void UpdateActivityCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + UpdateActivityHandler callback = (UpdateActivityHandler)h.Target; + h.Free(); + callback(result); + } + + public void UpdateActivity(Activity activity, UpdateActivityHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.UpdateActivity(MethodsPtr, ref activity, GCHandle.ToIntPtr(wrapped), UpdateActivityCallbackImpl); + } + + [MonoPInvokeCallback] + private static void ClearActivityCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + ClearActivityHandler callback = (ClearActivityHandler)h.Target; + h.Free(); + callback(result); + } + + public void ClearActivity(ClearActivityHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.ClearActivity(MethodsPtr, GCHandle.ToIntPtr(wrapped), ClearActivityCallbackImpl); + } + + [MonoPInvokeCallback] + private static void SendRequestReplyCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + SendRequestReplyHandler callback = (SendRequestReplyHandler)h.Target; + h.Free(); + callback(result); + } + + public void SendRequestReply(Int64 userId, ActivityJoinRequestReply reply, SendRequestReplyHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.SendRequestReply(MethodsPtr, userId, reply, GCHandle.ToIntPtr(wrapped), SendRequestReplyCallbackImpl); + } + + [MonoPInvokeCallback] + private static void SendInviteCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + SendInviteHandler callback = (SendInviteHandler)h.Target; + h.Free(); + callback(result); + } + + public void SendInvite(Int64 userId, ActivityActionType type, string content, SendInviteHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.SendInvite(MethodsPtr, userId, type, content, GCHandle.ToIntPtr(wrapped), SendInviteCallbackImpl); + } + + [MonoPInvokeCallback] + private static void AcceptInviteCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + AcceptInviteHandler callback = (AcceptInviteHandler)h.Target; + h.Free(); + callback(result); + } + + public void AcceptInvite(Int64 userId, AcceptInviteHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.AcceptInvite(MethodsPtr, userId, GCHandle.ToIntPtr(wrapped), AcceptInviteCallbackImpl); + } + + [MonoPInvokeCallback] + private static void OnActivityJoinImpl(IntPtr ptr, string secret) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.ActivityManagerInstance.OnActivityJoin != null) + { + d.ActivityManagerInstance.OnActivityJoin.Invoke(secret); + } + } + + [MonoPInvokeCallback] + private static void OnActivitySpectateImpl(IntPtr ptr, string secret) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.ActivityManagerInstance.OnActivitySpectate != null) + { + d.ActivityManagerInstance.OnActivitySpectate.Invoke(secret); + } + } + + [MonoPInvokeCallback] + private static void OnActivityJoinRequestImpl(IntPtr ptr, ref User user) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.ActivityManagerInstance.OnActivityJoinRequest != null) + { + d.ActivityManagerInstance.OnActivityJoinRequest.Invoke(ref user); + } + } + + [MonoPInvokeCallback] + private static void OnActivityInviteImpl(IntPtr ptr, ActivityActionType type, ref User user, ref Activity activity) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.ActivityManagerInstance.OnActivityInvite != null) + { + d.ActivityManagerInstance.OnActivityInvite.Invoke(type, ref user, ref activity); + } + } + } + + public partial class RelationshipManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void RefreshHandler(IntPtr ptr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void RelationshipUpdateHandler(IntPtr ptr, ref Relationship relationship); + + internal RefreshHandler OnRefresh; + + internal RelationshipUpdateHandler OnRelationshipUpdate; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool FilterCallback(IntPtr ptr, ref Relationship relationship); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FilterMethod(IntPtr methodsPtr, IntPtr callbackData, FilterCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result CountMethod(IntPtr methodsPtr, ref Int32 count); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetMethod(IntPtr methodsPtr, Int64 userId, ref Relationship relationship); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetAtMethod(IntPtr methodsPtr, UInt32 index, ref Relationship relationship); + + internal FilterMethod Filter; + + internal CountMethod Count; + + internal GetMethod Get; + + internal GetAtMethod GetAt; + } + + public delegate bool FilterHandler(ref Relationship relationship); + + public delegate void RefreshHandler(); + + public delegate void RelationshipUpdateHandler(ref Relationship relationship); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public event RefreshHandler OnRefresh; + + public event RelationshipUpdateHandler OnRelationshipUpdate; + + internal RelationshipManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + events.OnRefresh = OnRefreshImpl; + events.OnRelationshipUpdate = OnRelationshipUpdateImpl; + Marshal.StructureToPtr(events, eventsPtr, false); + } + + [MonoPInvokeCallback] + private static bool FilterCallbackImpl(IntPtr ptr, ref Relationship relationship) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + FilterHandler callback = (FilterHandler)h.Target; + return callback(ref relationship); + } + + public void Filter(FilterHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.Filter(MethodsPtr, GCHandle.ToIntPtr(wrapped), FilterCallbackImpl); + wrapped.Free(); + } + + public Int32 Count() + { + var ret = new Int32(); + var res = Methods.Count(MethodsPtr, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public Relationship Get(Int64 userId) + { + var ret = new Relationship(); + var res = Methods.Get(MethodsPtr, userId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public Relationship GetAt(UInt32 index) + { + var ret = new Relationship(); + var res = Methods.GetAt(MethodsPtr, index, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void OnRefreshImpl(IntPtr ptr) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.RelationshipManagerInstance.OnRefresh != null) + { + d.RelationshipManagerInstance.OnRefresh.Invoke(); + } + } + + [MonoPInvokeCallback] + private static void OnRelationshipUpdateImpl(IntPtr ptr, ref Relationship relationship) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.RelationshipManagerInstance.OnRelationshipUpdate != null) + { + d.RelationshipManagerInstance.OnRelationshipUpdate.Invoke(ref relationship); + } + } + } + + public partial class LobbyManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void LobbyUpdateHandler(IntPtr ptr, Int64 lobbyId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void LobbyDeleteHandler(IntPtr ptr, Int64 lobbyId, UInt32 reason); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void MemberConnectHandler(IntPtr ptr, Int64 lobbyId, Int64 userId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void MemberUpdateHandler(IntPtr ptr, Int64 lobbyId, Int64 userId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void MemberDisconnectHandler(IntPtr ptr, Int64 lobbyId, Int64 userId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void LobbyMessageHandler(IntPtr ptr, Int64 lobbyId, Int64 userId, IntPtr dataPtr, Int32 dataLen); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SpeakingHandler(IntPtr ptr, Int64 lobbyId, Int64 userId, bool speaking); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void NetworkMessageHandler(IntPtr ptr, Int64 lobbyId, Int64 userId, byte channelId, IntPtr dataPtr, Int32 dataLen); + + internal LobbyUpdateHandler OnLobbyUpdate; + + internal LobbyDeleteHandler OnLobbyDelete; + + internal MemberConnectHandler OnMemberConnect; + + internal MemberUpdateHandler OnMemberUpdate; + + internal MemberDisconnectHandler OnMemberDisconnect; + + internal LobbyMessageHandler OnLobbyMessage; + + internal SpeakingHandler OnSpeaking; + + internal NetworkMessageHandler OnNetworkMessage; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetLobbyCreateTransactionMethod(IntPtr methodsPtr, ref IntPtr transaction); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetLobbyUpdateTransactionMethod(IntPtr methodsPtr, Int64 lobbyId, ref IntPtr transaction); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetMemberUpdateTransactionMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, ref IntPtr transaction); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void CreateLobbyCallback(IntPtr ptr, Result result, ref Lobby lobby); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void CreateLobbyMethod(IntPtr methodsPtr, IntPtr transaction, IntPtr callbackData, CreateLobbyCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void UpdateLobbyCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void UpdateLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr transaction, IntPtr callbackData, UpdateLobbyCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void DeleteLobbyCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void DeleteLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr callbackData, DeleteLobbyCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ConnectLobbyCallback(IntPtr ptr, Result result, ref Lobby lobby); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ConnectLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, [MarshalAs(UnmanagedType.LPStr)]string secret, IntPtr callbackData, ConnectLobbyCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ConnectLobbyWithActivitySecretCallback(IntPtr ptr, Result result, ref Lobby lobby); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ConnectLobbyWithActivitySecretMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string activitySecret, IntPtr callbackData, ConnectLobbyWithActivitySecretCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void DisconnectLobbyCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void DisconnectLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr callbackData, DisconnectLobbyCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, ref Lobby lobby); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetLobbyActivitySecretMethod(IntPtr methodsPtr, Int64 lobbyId, StringBuilder secret); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetLobbyMetadataValueMethod(IntPtr methodsPtr, Int64 lobbyId, [MarshalAs(UnmanagedType.LPStr)]string key, StringBuilder value); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetLobbyMetadataKeyMethod(IntPtr methodsPtr, Int64 lobbyId, Int32 index, StringBuilder key); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result LobbyMetadataCountMethod(IntPtr methodsPtr, Int64 lobbyId, ref Int32 count); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result MemberCountMethod(IntPtr methodsPtr, Int64 lobbyId, ref Int32 count); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetMemberUserIdMethod(IntPtr methodsPtr, Int64 lobbyId, Int32 index, ref Int64 userId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetMemberUserMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, ref User user); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetMemberMetadataValueMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, [MarshalAs(UnmanagedType.LPStr)]string key, StringBuilder value); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetMemberMetadataKeyMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, Int32 index, StringBuilder key); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result MemberMetadataCountMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, ref Int32 count); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void UpdateMemberCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void UpdateMemberMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, IntPtr transaction, IntPtr callbackData, UpdateMemberCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SendLobbyMessageCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SendLobbyMessageMethod(IntPtr methodsPtr, Int64 lobbyId, byte[] data, Int32 dataLen, IntPtr callbackData, SendLobbyMessageCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetSearchQueryMethod(IntPtr methodsPtr, ref IntPtr query); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SearchCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SearchMethod(IntPtr methodsPtr, IntPtr query, IntPtr callbackData, SearchCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void LobbyCountMethod(IntPtr methodsPtr, ref Int32 count); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetLobbyIdMethod(IntPtr methodsPtr, Int32 index, ref Int64 lobbyId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ConnectVoiceCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ConnectVoiceMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr callbackData, ConnectVoiceCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void DisconnectVoiceCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void DisconnectVoiceMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr callbackData, DisconnectVoiceCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result ConnectNetworkMethod(IntPtr methodsPtr, Int64 lobbyId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result DisconnectNetworkMethod(IntPtr methodsPtr, Int64 lobbyId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result FlushNetworkMethod(IntPtr methodsPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result OpenNetworkChannelMethod(IntPtr methodsPtr, Int64 lobbyId, byte channelId, bool reliable); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SendNetworkMessageMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, byte channelId, byte[] data, Int32 dataLen); + + internal GetLobbyCreateTransactionMethod GetLobbyCreateTransaction; + + internal GetLobbyUpdateTransactionMethod GetLobbyUpdateTransaction; + + internal GetMemberUpdateTransactionMethod GetMemberUpdateTransaction; + + internal CreateLobbyMethod CreateLobby; + + internal UpdateLobbyMethod UpdateLobby; + + internal DeleteLobbyMethod DeleteLobby; + + internal ConnectLobbyMethod ConnectLobby; + + internal ConnectLobbyWithActivitySecretMethod ConnectLobbyWithActivitySecret; + + internal DisconnectLobbyMethod DisconnectLobby; + + internal GetLobbyMethod GetLobby; + + internal GetLobbyActivitySecretMethod GetLobbyActivitySecret; + + internal GetLobbyMetadataValueMethod GetLobbyMetadataValue; + + internal GetLobbyMetadataKeyMethod GetLobbyMetadataKey; + + internal LobbyMetadataCountMethod LobbyMetadataCount; + + internal MemberCountMethod MemberCount; + + internal GetMemberUserIdMethod GetMemberUserId; + + internal GetMemberUserMethod GetMemberUser; + + internal GetMemberMetadataValueMethod GetMemberMetadataValue; + + internal GetMemberMetadataKeyMethod GetMemberMetadataKey; + + internal MemberMetadataCountMethod MemberMetadataCount; + + internal UpdateMemberMethod UpdateMember; + + internal SendLobbyMessageMethod SendLobbyMessage; + + internal GetSearchQueryMethod GetSearchQuery; + + internal SearchMethod Search; + + internal LobbyCountMethod LobbyCount; + + internal GetLobbyIdMethod GetLobbyId; + + internal ConnectVoiceMethod ConnectVoice; + + internal DisconnectVoiceMethod DisconnectVoice; + + internal ConnectNetworkMethod ConnectNetwork; + + internal DisconnectNetworkMethod DisconnectNetwork; + + internal FlushNetworkMethod FlushNetwork; + + internal OpenNetworkChannelMethod OpenNetworkChannel; + + internal SendNetworkMessageMethod SendNetworkMessage; + } + + public delegate void CreateLobbyHandler(Result result, ref Lobby lobby); + + public delegate void UpdateLobbyHandler(Result result); + + public delegate void DeleteLobbyHandler(Result result); + + public delegate void ConnectLobbyHandler(Result result, ref Lobby lobby); + + public delegate void ConnectLobbyWithActivitySecretHandler(Result result, ref Lobby lobby); + + public delegate void DisconnectLobbyHandler(Result result); + + public delegate void UpdateMemberHandler(Result result); + + public delegate void SendLobbyMessageHandler(Result result); + + public delegate void SearchHandler(Result result); + + public delegate void ConnectVoiceHandler(Result result); + + public delegate void DisconnectVoiceHandler(Result result); + + public delegate void LobbyUpdateHandler(Int64 lobbyId); + + public delegate void LobbyDeleteHandler(Int64 lobbyId, UInt32 reason); + + public delegate void MemberConnectHandler(Int64 lobbyId, Int64 userId); + + public delegate void MemberUpdateHandler(Int64 lobbyId, Int64 userId); + + public delegate void MemberDisconnectHandler(Int64 lobbyId, Int64 userId); + + public delegate void LobbyMessageHandler(Int64 lobbyId, Int64 userId, byte[] data); + + public delegate void SpeakingHandler(Int64 lobbyId, Int64 userId, bool speaking); + + public delegate void NetworkMessageHandler(Int64 lobbyId, Int64 userId, byte channelId, byte[] data); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public event LobbyUpdateHandler OnLobbyUpdate; + + public event LobbyDeleteHandler OnLobbyDelete; + + public event MemberConnectHandler OnMemberConnect; + + public event MemberUpdateHandler OnMemberUpdate; + + public event MemberDisconnectHandler OnMemberDisconnect; + + public event LobbyMessageHandler OnLobbyMessage; + + public event SpeakingHandler OnSpeaking; + + public event NetworkMessageHandler OnNetworkMessage; + + internal LobbyManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + events.OnLobbyUpdate = OnLobbyUpdateImpl; + events.OnLobbyDelete = OnLobbyDeleteImpl; + events.OnMemberConnect = OnMemberConnectImpl; + events.OnMemberUpdate = OnMemberUpdateImpl; + events.OnMemberDisconnect = OnMemberDisconnectImpl; + events.OnLobbyMessage = OnLobbyMessageImpl; + events.OnSpeaking = OnSpeakingImpl; + events.OnNetworkMessage = OnNetworkMessageImpl; + Marshal.StructureToPtr(events, eventsPtr, false); + } + + public LobbyTransaction GetLobbyCreateTransaction() + { + var ret = new LobbyTransaction(); + var res = Methods.GetLobbyCreateTransaction(MethodsPtr, ref ret.MethodsPtr); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public LobbyTransaction GetLobbyUpdateTransaction(Int64 lobbyId) + { + var ret = new LobbyTransaction(); + var res = Methods.GetLobbyUpdateTransaction(MethodsPtr, lobbyId, ref ret.MethodsPtr); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public LobbyMemberTransaction GetMemberUpdateTransaction(Int64 lobbyId, Int64 userId) + { + var ret = new LobbyMemberTransaction(); + var res = Methods.GetMemberUpdateTransaction(MethodsPtr, lobbyId, userId, ref ret.MethodsPtr); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void CreateLobbyCallbackImpl(IntPtr ptr, Result result, ref Lobby lobby) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + CreateLobbyHandler callback = (CreateLobbyHandler)h.Target; + h.Free(); + callback(result, ref lobby); + } + + public void CreateLobby(LobbyTransaction transaction, CreateLobbyHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.CreateLobby(MethodsPtr, transaction.MethodsPtr, GCHandle.ToIntPtr(wrapped), CreateLobbyCallbackImpl); + transaction.MethodsPtr = IntPtr.Zero; + } + + [MonoPInvokeCallback] + private static void UpdateLobbyCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + UpdateLobbyHandler callback = (UpdateLobbyHandler)h.Target; + h.Free(); + callback(result); + } + + public void UpdateLobby(Int64 lobbyId, LobbyTransaction transaction, UpdateLobbyHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.UpdateLobby(MethodsPtr, lobbyId, transaction.MethodsPtr, GCHandle.ToIntPtr(wrapped), UpdateLobbyCallbackImpl); + transaction.MethodsPtr = IntPtr.Zero; + } + + [MonoPInvokeCallback] + private static void DeleteLobbyCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + DeleteLobbyHandler callback = (DeleteLobbyHandler)h.Target; + h.Free(); + callback(result); + } + + public void DeleteLobby(Int64 lobbyId, DeleteLobbyHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.DeleteLobby(MethodsPtr, lobbyId, GCHandle.ToIntPtr(wrapped), DeleteLobbyCallbackImpl); + } + + [MonoPInvokeCallback] + private static void ConnectLobbyCallbackImpl(IntPtr ptr, Result result, ref Lobby lobby) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + ConnectLobbyHandler callback = (ConnectLobbyHandler)h.Target; + h.Free(); + callback(result, ref lobby); + } + + public void ConnectLobby(Int64 lobbyId, string secret, ConnectLobbyHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.ConnectLobby(MethodsPtr, lobbyId, secret, GCHandle.ToIntPtr(wrapped), ConnectLobbyCallbackImpl); + } + + [MonoPInvokeCallback] + private static void ConnectLobbyWithActivitySecretCallbackImpl(IntPtr ptr, Result result, ref Lobby lobby) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + ConnectLobbyWithActivitySecretHandler callback = (ConnectLobbyWithActivitySecretHandler)h.Target; + h.Free(); + callback(result, ref lobby); + } + + public void ConnectLobbyWithActivitySecret(string activitySecret, ConnectLobbyWithActivitySecretHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.ConnectLobbyWithActivitySecret(MethodsPtr, activitySecret, GCHandle.ToIntPtr(wrapped), ConnectLobbyWithActivitySecretCallbackImpl); + } + + [MonoPInvokeCallback] + private static void DisconnectLobbyCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + DisconnectLobbyHandler callback = (DisconnectLobbyHandler)h.Target; + h.Free(); + callback(result); + } + + public void DisconnectLobby(Int64 lobbyId, DisconnectLobbyHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.DisconnectLobby(MethodsPtr, lobbyId, GCHandle.ToIntPtr(wrapped), DisconnectLobbyCallbackImpl); + } + + public Lobby GetLobby(Int64 lobbyId) + { + var ret = new Lobby(); + var res = Methods.GetLobby(MethodsPtr, lobbyId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public string GetLobbyActivitySecret(Int64 lobbyId) + { + var ret = new StringBuilder(128); + var res = Methods.GetLobbyActivitySecret(MethodsPtr, lobbyId, ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret.ToString(); + } + + public string GetLobbyMetadataValue(Int64 lobbyId, string key) + { + var ret = new StringBuilder(4096); + var res = Methods.GetLobbyMetadataValue(MethodsPtr, lobbyId, key, ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret.ToString(); + } + + public string GetLobbyMetadataKey(Int64 lobbyId, Int32 index) + { + var ret = new StringBuilder(256); + var res = Methods.GetLobbyMetadataKey(MethodsPtr, lobbyId, index, ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret.ToString(); + } + + public Int32 LobbyMetadataCount(Int64 lobbyId) + { + var ret = new Int32(); + var res = Methods.LobbyMetadataCount(MethodsPtr, lobbyId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public Int32 MemberCount(Int64 lobbyId) + { + var ret = new Int32(); + var res = Methods.MemberCount(MethodsPtr, lobbyId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public Int64 GetMemberUserId(Int64 lobbyId, Int32 index) + { + var ret = new Int64(); + var res = Methods.GetMemberUserId(MethodsPtr, lobbyId, index, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public User GetMemberUser(Int64 lobbyId, Int64 userId) + { + var ret = new User(); + var res = Methods.GetMemberUser(MethodsPtr, lobbyId, userId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public string GetMemberMetadataValue(Int64 lobbyId, Int64 userId, string key) + { + var ret = new StringBuilder(4096); + var res = Methods.GetMemberMetadataValue(MethodsPtr, lobbyId, userId, key, ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret.ToString(); + } + + public string GetMemberMetadataKey(Int64 lobbyId, Int64 userId, Int32 index) + { + var ret = new StringBuilder(256); + var res = Methods.GetMemberMetadataKey(MethodsPtr, lobbyId, userId, index, ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret.ToString(); + } + + public Int32 MemberMetadataCount(Int64 lobbyId, Int64 userId) + { + var ret = new Int32(); + var res = Methods.MemberMetadataCount(MethodsPtr, lobbyId, userId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void UpdateMemberCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + UpdateMemberHandler callback = (UpdateMemberHandler)h.Target; + h.Free(); + callback(result); + } + + public void UpdateMember(Int64 lobbyId, Int64 userId, LobbyMemberTransaction transaction, UpdateMemberHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.UpdateMember(MethodsPtr, lobbyId, userId, transaction.MethodsPtr, GCHandle.ToIntPtr(wrapped), UpdateMemberCallbackImpl); + transaction.MethodsPtr = IntPtr.Zero; + } + + [MonoPInvokeCallback] + private static void SendLobbyMessageCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + SendLobbyMessageHandler callback = (SendLobbyMessageHandler)h.Target; + h.Free(); + callback(result); + } + + public void SendLobbyMessage(Int64 lobbyId, byte[] data, SendLobbyMessageHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.SendLobbyMessage(MethodsPtr, lobbyId, data, data.Length, GCHandle.ToIntPtr(wrapped), SendLobbyMessageCallbackImpl); + } + + public LobbySearchQuery GetSearchQuery() + { + var ret = new LobbySearchQuery(); + var res = Methods.GetSearchQuery(MethodsPtr, ref ret.MethodsPtr); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void SearchCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + SearchHandler callback = (SearchHandler)h.Target; + h.Free(); + callback(result); + } + + public void Search(LobbySearchQuery query, SearchHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.Search(MethodsPtr, query.MethodsPtr, GCHandle.ToIntPtr(wrapped), SearchCallbackImpl); + query.MethodsPtr = IntPtr.Zero; + } + + public Int32 LobbyCount() + { + var ret = new Int32(); + Methods.LobbyCount(MethodsPtr, ref ret); + return ret; + } + + public Int64 GetLobbyId(Int32 index) + { + var ret = new Int64(); + var res = Methods.GetLobbyId(MethodsPtr, index, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void ConnectVoiceCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + ConnectVoiceHandler callback = (ConnectVoiceHandler)h.Target; + h.Free(); + callback(result); + } + + public void ConnectVoice(Int64 lobbyId, ConnectVoiceHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.ConnectVoice(MethodsPtr, lobbyId, GCHandle.ToIntPtr(wrapped), ConnectVoiceCallbackImpl); + } + + [MonoPInvokeCallback] + private static void DisconnectVoiceCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + DisconnectVoiceHandler callback = (DisconnectVoiceHandler)h.Target; + h.Free(); + callback(result); + } + + public void DisconnectVoice(Int64 lobbyId, DisconnectVoiceHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.DisconnectVoice(MethodsPtr, lobbyId, GCHandle.ToIntPtr(wrapped), DisconnectVoiceCallbackImpl); + } + + public void ConnectNetwork(Int64 lobbyId) + { + var res = Methods.ConnectNetwork(MethodsPtr, lobbyId); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + public void DisconnectNetwork(Int64 lobbyId) + { + var res = Methods.DisconnectNetwork(MethodsPtr, lobbyId); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + public void FlushNetwork() + { + var res = Methods.FlushNetwork(MethodsPtr); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + public void OpenNetworkChannel(Int64 lobbyId, byte channelId, bool reliable) + { + var res = Methods.OpenNetworkChannel(MethodsPtr, lobbyId, channelId, reliable); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + public void SendNetworkMessage(Int64 lobbyId, Int64 userId, byte channelId, byte[] data) + { + var res = Methods.SendNetworkMessage(MethodsPtr, lobbyId, userId, channelId, data, data.Length); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + [MonoPInvokeCallback] + private static void OnLobbyUpdateImpl(IntPtr ptr, Int64 lobbyId) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.LobbyManagerInstance.OnLobbyUpdate != null) + { + d.LobbyManagerInstance.OnLobbyUpdate.Invoke(lobbyId); + } + } + + [MonoPInvokeCallback] + private static void OnLobbyDeleteImpl(IntPtr ptr, Int64 lobbyId, UInt32 reason) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.LobbyManagerInstance.OnLobbyDelete != null) + { + d.LobbyManagerInstance.OnLobbyDelete.Invoke(lobbyId, reason); + } + } + + [MonoPInvokeCallback] + private static void OnMemberConnectImpl(IntPtr ptr, Int64 lobbyId, Int64 userId) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.LobbyManagerInstance.OnMemberConnect != null) + { + d.LobbyManagerInstance.OnMemberConnect.Invoke(lobbyId, userId); + } + } + + [MonoPInvokeCallback] + private static void OnMemberUpdateImpl(IntPtr ptr, Int64 lobbyId, Int64 userId) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.LobbyManagerInstance.OnMemberUpdate != null) + { + d.LobbyManagerInstance.OnMemberUpdate.Invoke(lobbyId, userId); + } + } + + [MonoPInvokeCallback] + private static void OnMemberDisconnectImpl(IntPtr ptr, Int64 lobbyId, Int64 userId) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.LobbyManagerInstance.OnMemberDisconnect != null) + { + d.LobbyManagerInstance.OnMemberDisconnect.Invoke(lobbyId, userId); + } + } + + [MonoPInvokeCallback] + private static void OnLobbyMessageImpl(IntPtr ptr, Int64 lobbyId, Int64 userId, IntPtr dataPtr, Int32 dataLen) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.LobbyManagerInstance.OnLobbyMessage != null) + { + byte[] data = new byte[dataLen]; + Marshal.Copy(dataPtr, data, 0, (int)dataLen); + d.LobbyManagerInstance.OnLobbyMessage.Invoke(lobbyId, userId, data); + } + } + + [MonoPInvokeCallback] + private static void OnSpeakingImpl(IntPtr ptr, Int64 lobbyId, Int64 userId, bool speaking) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.LobbyManagerInstance.OnSpeaking != null) + { + d.LobbyManagerInstance.OnSpeaking.Invoke(lobbyId, userId, speaking); + } + } + + [MonoPInvokeCallback] + private static void OnNetworkMessageImpl(IntPtr ptr, Int64 lobbyId, Int64 userId, byte channelId, IntPtr dataPtr, Int32 dataLen) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.LobbyManagerInstance.OnNetworkMessage != null) + { + byte[] data = new byte[dataLen]; + Marshal.Copy(dataPtr, data, 0, (int)dataLen); + d.LobbyManagerInstance.OnNetworkMessage.Invoke(lobbyId, userId, channelId, data); + } + } + } + + public partial class NetworkManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void MessageHandler(IntPtr ptr, UInt64 peerId, byte channelId, IntPtr dataPtr, Int32 dataLen); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void RouteUpdateHandler(IntPtr ptr, [MarshalAs(UnmanagedType.LPStr)]string routeData); + + internal MessageHandler OnMessage; + + internal RouteUpdateHandler OnRouteUpdate; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void GetPeerIdMethod(IntPtr methodsPtr, ref UInt64 peerId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result FlushMethod(IntPtr methodsPtr); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result OpenPeerMethod(IntPtr methodsPtr, UInt64 peerId, [MarshalAs(UnmanagedType.LPStr)]string routeData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result UpdatePeerMethod(IntPtr methodsPtr, UInt64 peerId, [MarshalAs(UnmanagedType.LPStr)]string routeData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result ClosePeerMethod(IntPtr methodsPtr, UInt64 peerId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result OpenChannelMethod(IntPtr methodsPtr, UInt64 peerId, byte channelId, bool reliable); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result CloseChannelMethod(IntPtr methodsPtr, UInt64 peerId, byte channelId); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SendMessageMethod(IntPtr methodsPtr, UInt64 peerId, byte channelId, byte[] data, Int32 dataLen); + + internal GetPeerIdMethod GetPeerId; + + internal FlushMethod Flush; + + internal OpenPeerMethod OpenPeer; + + internal UpdatePeerMethod UpdatePeer; + + internal ClosePeerMethod ClosePeer; + + internal OpenChannelMethod OpenChannel; + + internal CloseChannelMethod CloseChannel; + + internal SendMessageMethod SendMessage; + } + + public delegate void MessageHandler(UInt64 peerId, byte channelId, byte[] data); + + public delegate void RouteUpdateHandler(string routeData); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public event MessageHandler OnMessage; + + public event RouteUpdateHandler OnRouteUpdate; + + internal NetworkManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + events.OnMessage = OnMessageImpl; + events.OnRouteUpdate = OnRouteUpdateImpl; + Marshal.StructureToPtr(events, eventsPtr, false); + } + + /// + /// Get the local peer ID for this process. + /// + public UInt64 GetPeerId() + { + var ret = new UInt64(); + Methods.GetPeerId(MethodsPtr, ref ret); + return ret; + } + + /// + /// Send pending network messages. + /// + public void Flush() + { + var res = Methods.Flush(MethodsPtr); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + /// + /// Open a connection to a remote peer. + /// + public void OpenPeer(UInt64 peerId, string routeData) + { + var res = Methods.OpenPeer(MethodsPtr, peerId, routeData); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + /// + /// Update the route data for a connected peer. + /// + public void UpdatePeer(UInt64 peerId, string routeData) + { + var res = Methods.UpdatePeer(MethodsPtr, peerId, routeData); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + /// + /// Close the connection to a remote peer. + /// + public void ClosePeer(UInt64 peerId) + { + var res = Methods.ClosePeer(MethodsPtr, peerId); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + /// + /// Open a message channel to a connected peer. + /// + public void OpenChannel(UInt64 peerId, byte channelId, bool reliable) + { + var res = Methods.OpenChannel(MethodsPtr, peerId, channelId, reliable); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + /// + /// Close a message channel to a connected peer. + /// + public void CloseChannel(UInt64 peerId, byte channelId) + { + var res = Methods.CloseChannel(MethodsPtr, peerId, channelId); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + /// + /// Send a message to a connected peer over an opened message channel. + /// + public void SendMessage(UInt64 peerId, byte channelId, byte[] data) + { + var res = Methods.SendMessage(MethodsPtr, peerId, channelId, data, data.Length); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + [MonoPInvokeCallback] + private static void OnMessageImpl(IntPtr ptr, UInt64 peerId, byte channelId, IntPtr dataPtr, Int32 dataLen) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.NetworkManagerInstance.OnMessage != null) + { + byte[] data = new byte[dataLen]; + Marshal.Copy(dataPtr, data, 0, (int)dataLen); + d.NetworkManagerInstance.OnMessage.Invoke(peerId, channelId, data); + } + } + + [MonoPInvokeCallback] + private static void OnRouteUpdateImpl(IntPtr ptr, string routeData) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.NetworkManagerInstance.OnRouteUpdate != null) + { + d.NetworkManagerInstance.OnRouteUpdate.Invoke(routeData); + } + } + } + + public partial class OverlayManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ToggleHandler(IntPtr ptr, bool locked); + + internal ToggleHandler OnToggle; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void IsEnabledMethod(IntPtr methodsPtr, ref bool enabled); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void IsLockedMethod(IntPtr methodsPtr, ref bool locked); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SetLockedCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SetLockedMethod(IntPtr methodsPtr, bool locked, IntPtr callbackData, SetLockedCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void OpenActivityInviteCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void OpenActivityInviteMethod(IntPtr methodsPtr, ActivityActionType type, IntPtr callbackData, OpenActivityInviteCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void OpenGuildInviteCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void OpenGuildInviteMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string code, IntPtr callbackData, OpenGuildInviteCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void OpenVoiceSettingsCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void OpenVoiceSettingsMethod(IntPtr methodsPtr, IntPtr callbackData, OpenVoiceSettingsCallback callback); + + internal IsEnabledMethod IsEnabled; + + internal IsLockedMethod IsLocked; + + internal SetLockedMethod SetLocked; + + internal OpenActivityInviteMethod OpenActivityInvite; + + internal OpenGuildInviteMethod OpenGuildInvite; + + internal OpenVoiceSettingsMethod OpenVoiceSettings; + } + + public delegate void SetLockedHandler(Result result); + + public delegate void OpenActivityInviteHandler(Result result); + + public delegate void OpenGuildInviteHandler(Result result); + + public delegate void OpenVoiceSettingsHandler(Result result); + + public delegate void ToggleHandler(bool locked); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public event ToggleHandler OnToggle; + + internal OverlayManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + events.OnToggle = OnToggleImpl; + Marshal.StructureToPtr(events, eventsPtr, false); + } + + public bool IsEnabled() + { + var ret = new bool(); + Methods.IsEnabled(MethodsPtr, ref ret); + return ret; + } + + public bool IsLocked() + { + var ret = new bool(); + Methods.IsLocked(MethodsPtr, ref ret); + return ret; + } + + [MonoPInvokeCallback] + private static void SetLockedCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + SetLockedHandler callback = (SetLockedHandler)h.Target; + h.Free(); + callback(result); + } + + public void SetLocked(bool locked, SetLockedHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.SetLocked(MethodsPtr, locked, GCHandle.ToIntPtr(wrapped), SetLockedCallbackImpl); + } + + [MonoPInvokeCallback] + private static void OpenActivityInviteCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + OpenActivityInviteHandler callback = (OpenActivityInviteHandler)h.Target; + h.Free(); + callback(result); + } + + public void OpenActivityInvite(ActivityActionType type, OpenActivityInviteHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.OpenActivityInvite(MethodsPtr, type, GCHandle.ToIntPtr(wrapped), OpenActivityInviteCallbackImpl); + } + + [MonoPInvokeCallback] + private static void OpenGuildInviteCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + OpenGuildInviteHandler callback = (OpenGuildInviteHandler)h.Target; + h.Free(); + callback(result); + } + + public void OpenGuildInvite(string code, OpenGuildInviteHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.OpenGuildInvite(MethodsPtr, code, GCHandle.ToIntPtr(wrapped), OpenGuildInviteCallbackImpl); + } + + [MonoPInvokeCallback] + private static void OpenVoiceSettingsCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + OpenVoiceSettingsHandler callback = (OpenVoiceSettingsHandler)h.Target; + h.Free(); + callback(result); + } + + public void OpenVoiceSettings(OpenVoiceSettingsHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.OpenVoiceSettings(MethodsPtr, GCHandle.ToIntPtr(wrapped), OpenVoiceSettingsCallbackImpl); + } + + [MonoPInvokeCallback] + private static void OnToggleImpl(IntPtr ptr, bool locked) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.OverlayManagerInstance.OnToggle != null) + { + d.OverlayManagerInstance.OnToggle.Invoke(locked); + } + } + } + + public partial class StorageManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result ReadMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, byte[] data, Int32 dataLen, ref UInt32 read); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ReadAsyncCallback(IntPtr ptr, Result result, IntPtr dataPtr, Int32 dataLen); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ReadAsyncMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, IntPtr callbackData, ReadAsyncCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ReadAsyncPartialCallback(IntPtr ptr, Result result, IntPtr dataPtr, Int32 dataLen); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ReadAsyncPartialMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, UInt64 offset, UInt64 length, IntPtr callbackData, ReadAsyncPartialCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result WriteMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, byte[] data, Int32 dataLen); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void WriteAsyncCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void WriteAsyncMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, byte[] data, Int32 dataLen, IntPtr callbackData, WriteAsyncCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result DeleteMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result ExistsMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, ref bool exists); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void CountMethod(IntPtr methodsPtr, ref Int32 count); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result StatMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, ref FileStat stat); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result StatAtMethod(IntPtr methodsPtr, Int32 index, ref FileStat stat); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetPathMethod(IntPtr methodsPtr, StringBuilder path); + + internal ReadMethod Read; + + internal ReadAsyncMethod ReadAsync; + + internal ReadAsyncPartialMethod ReadAsyncPartial; + + internal WriteMethod Write; + + internal WriteAsyncMethod WriteAsync; + + internal DeleteMethod Delete; + + internal ExistsMethod Exists; + + internal CountMethod Count; + + internal StatMethod Stat; + + internal StatAtMethod StatAt; + + internal GetPathMethod GetPath; + } + + public delegate void ReadAsyncHandler(Result result, byte[] data); + + public delegate void ReadAsyncPartialHandler(Result result, byte[] data); + + public delegate void WriteAsyncHandler(Result result); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + internal StorageManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + Marshal.StructureToPtr(events, eventsPtr, false); + } + + public UInt32 Read(string name, byte[] data) + { + var ret = new UInt32(); + var res = Methods.Read(MethodsPtr, name, data, data.Length, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void ReadAsyncCallbackImpl(IntPtr ptr, Result result, IntPtr dataPtr, Int32 dataLen) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + ReadAsyncHandler callback = (ReadAsyncHandler)h.Target; + h.Free(); + byte[] data = new byte[dataLen]; + Marshal.Copy(dataPtr, data, 0, (int)dataLen); + callback(result, data); + } + + public void ReadAsync(string name, ReadAsyncHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.ReadAsync(MethodsPtr, name, GCHandle.ToIntPtr(wrapped), ReadAsyncCallbackImpl); + } + + [MonoPInvokeCallback] + private static void ReadAsyncPartialCallbackImpl(IntPtr ptr, Result result, IntPtr dataPtr, Int32 dataLen) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + ReadAsyncPartialHandler callback = (ReadAsyncPartialHandler)h.Target; + h.Free(); + byte[] data = new byte[dataLen]; + Marshal.Copy(dataPtr, data, 0, (int)dataLen); + callback(result, data); + } + + public void ReadAsyncPartial(string name, UInt64 offset, UInt64 length, ReadAsyncPartialHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.ReadAsyncPartial(MethodsPtr, name, offset, length, GCHandle.ToIntPtr(wrapped), ReadAsyncPartialCallbackImpl); + } + + public void Write(string name, byte[] data) + { + var res = Methods.Write(MethodsPtr, name, data, data.Length); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + [MonoPInvokeCallback] + private static void WriteAsyncCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + WriteAsyncHandler callback = (WriteAsyncHandler)h.Target; + h.Free(); + callback(result); + } + + public void WriteAsync(string name, byte[] data, WriteAsyncHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.WriteAsync(MethodsPtr, name, data, data.Length, GCHandle.ToIntPtr(wrapped), WriteAsyncCallbackImpl); + } + + public void Delete(string name) + { + var res = Methods.Delete(MethodsPtr, name); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + public bool Exists(string name) + { + var ret = new bool(); + var res = Methods.Exists(MethodsPtr, name, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public Int32 Count() + { + var ret = new Int32(); + Methods.Count(MethodsPtr, ref ret); + return ret; + } + + public FileStat Stat(string name) + { + var ret = new FileStat(); + var res = Methods.Stat(MethodsPtr, name, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public FileStat StatAt(Int32 index) + { + var ret = new FileStat(); + var res = Methods.StatAt(MethodsPtr, index, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public string GetPath() + { + var ret = new StringBuilder(4096); + var res = Methods.GetPath(MethodsPtr, ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret.ToString(); + } + } + + public partial class StoreManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void EntitlementCreateHandler(IntPtr ptr, ref Entitlement entitlement); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void EntitlementDeleteHandler(IntPtr ptr, ref Entitlement entitlement); + + internal EntitlementCreateHandler OnEntitlementCreate; + + internal EntitlementDeleteHandler OnEntitlementDelete; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FetchSkusCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FetchSkusMethod(IntPtr methodsPtr, IntPtr callbackData, FetchSkusCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void CountSkusMethod(IntPtr methodsPtr, ref Int32 count); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetSkuMethod(IntPtr methodsPtr, Int64 skuId, ref Sku sku); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetSkuAtMethod(IntPtr methodsPtr, Int32 index, ref Sku sku); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FetchEntitlementsCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FetchEntitlementsMethod(IntPtr methodsPtr, IntPtr callbackData, FetchEntitlementsCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void CountEntitlementsMethod(IntPtr methodsPtr, ref Int32 count); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetEntitlementMethod(IntPtr methodsPtr, Int64 entitlementId, ref Entitlement entitlement); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetEntitlementAtMethod(IntPtr methodsPtr, Int32 index, ref Entitlement entitlement); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result HasSkuEntitlementMethod(IntPtr methodsPtr, Int64 skuId, ref bool hasEntitlement); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void StartPurchaseCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void StartPurchaseMethod(IntPtr methodsPtr, Int64 skuId, IntPtr callbackData, StartPurchaseCallback callback); + + internal FetchSkusMethod FetchSkus; + + internal CountSkusMethod CountSkus; + + internal GetSkuMethod GetSku; + + internal GetSkuAtMethod GetSkuAt; + + internal FetchEntitlementsMethod FetchEntitlements; + + internal CountEntitlementsMethod CountEntitlements; + + internal GetEntitlementMethod GetEntitlement; + + internal GetEntitlementAtMethod GetEntitlementAt; + + internal HasSkuEntitlementMethod HasSkuEntitlement; + + internal StartPurchaseMethod StartPurchase; + } + + public delegate void FetchSkusHandler(Result result); + + public delegate void FetchEntitlementsHandler(Result result); + + public delegate void StartPurchaseHandler(Result result); + + public delegate void EntitlementCreateHandler(ref Entitlement entitlement); + + public delegate void EntitlementDeleteHandler(ref Entitlement entitlement); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public event EntitlementCreateHandler OnEntitlementCreate; + + public event EntitlementDeleteHandler OnEntitlementDelete; + + internal StoreManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + events.OnEntitlementCreate = OnEntitlementCreateImpl; + events.OnEntitlementDelete = OnEntitlementDeleteImpl; + Marshal.StructureToPtr(events, eventsPtr, false); + } + + [MonoPInvokeCallback] + private static void FetchSkusCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + FetchSkusHandler callback = (FetchSkusHandler)h.Target; + h.Free(); + callback(result); + } + + public void FetchSkus(FetchSkusHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.FetchSkus(MethodsPtr, GCHandle.ToIntPtr(wrapped), FetchSkusCallbackImpl); + } + + public Int32 CountSkus() + { + var ret = new Int32(); + Methods.CountSkus(MethodsPtr, ref ret); + return ret; + } + + public Sku GetSku(Int64 skuId) + { + var ret = new Sku(); + var res = Methods.GetSku(MethodsPtr, skuId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public Sku GetSkuAt(Int32 index) + { + var ret = new Sku(); + var res = Methods.GetSkuAt(MethodsPtr, index, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void FetchEntitlementsCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + FetchEntitlementsHandler callback = (FetchEntitlementsHandler)h.Target; + h.Free(); + callback(result); + } + + public void FetchEntitlements(FetchEntitlementsHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.FetchEntitlements(MethodsPtr, GCHandle.ToIntPtr(wrapped), FetchEntitlementsCallbackImpl); + } + + public Int32 CountEntitlements() + { + var ret = new Int32(); + Methods.CountEntitlements(MethodsPtr, ref ret); + return ret; + } + + public Entitlement GetEntitlement(Int64 entitlementId) + { + var ret = new Entitlement(); + var res = Methods.GetEntitlement(MethodsPtr, entitlementId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public Entitlement GetEntitlementAt(Int32 index) + { + var ret = new Entitlement(); + var res = Methods.GetEntitlementAt(MethodsPtr, index, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public bool HasSkuEntitlement(Int64 skuId) + { + var ret = new bool(); + var res = Methods.HasSkuEntitlement(MethodsPtr, skuId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void StartPurchaseCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + StartPurchaseHandler callback = (StartPurchaseHandler)h.Target; + h.Free(); + callback(result); + } + + public void StartPurchase(Int64 skuId, StartPurchaseHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.StartPurchase(MethodsPtr, skuId, GCHandle.ToIntPtr(wrapped), StartPurchaseCallbackImpl); + } + + [MonoPInvokeCallback] + private static void OnEntitlementCreateImpl(IntPtr ptr, ref Entitlement entitlement) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.StoreManagerInstance.OnEntitlementCreate != null) + { + d.StoreManagerInstance.OnEntitlementCreate.Invoke(ref entitlement); + } + } + + [MonoPInvokeCallback] + private static void OnEntitlementDeleteImpl(IntPtr ptr, ref Entitlement entitlement) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.StoreManagerInstance.OnEntitlementDelete != null) + { + d.StoreManagerInstance.OnEntitlementDelete.Invoke(ref entitlement); + } + } + } + + public partial class VoiceManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SettingsUpdateHandler(IntPtr ptr); + + internal SettingsUpdateHandler OnSettingsUpdate; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetInputModeMethod(IntPtr methodsPtr, ref InputMode inputMode); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SetInputModeCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SetInputModeMethod(IntPtr methodsPtr, InputMode inputMode, IntPtr callbackData, SetInputModeCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result IsSelfMuteMethod(IntPtr methodsPtr, ref bool mute); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetSelfMuteMethod(IntPtr methodsPtr, bool mute); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result IsSelfDeafMethod(IntPtr methodsPtr, ref bool deaf); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetSelfDeafMethod(IntPtr methodsPtr, bool deaf); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result IsLocalMuteMethod(IntPtr methodsPtr, Int64 userId, ref bool mute); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetLocalMuteMethod(IntPtr methodsPtr, Int64 userId, bool mute); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetLocalVolumeMethod(IntPtr methodsPtr, Int64 userId, ref byte volume); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result SetLocalVolumeMethod(IntPtr methodsPtr, Int64 userId, byte volume); + + internal GetInputModeMethod GetInputMode; + + internal SetInputModeMethod SetInputMode; + + internal IsSelfMuteMethod IsSelfMute; + + internal SetSelfMuteMethod SetSelfMute; + + internal IsSelfDeafMethod IsSelfDeaf; + + internal SetSelfDeafMethod SetSelfDeaf; + + internal IsLocalMuteMethod IsLocalMute; + + internal SetLocalMuteMethod SetLocalMute; + + internal GetLocalVolumeMethod GetLocalVolume; + + internal SetLocalVolumeMethod SetLocalVolume; + } + + public delegate void SetInputModeHandler(Result result); + + public delegate void SettingsUpdateHandler(); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public event SettingsUpdateHandler OnSettingsUpdate; + + internal VoiceManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + events.OnSettingsUpdate = OnSettingsUpdateImpl; + Marshal.StructureToPtr(events, eventsPtr, false); + } + + public InputMode GetInputMode() + { + var ret = new InputMode(); + var res = Methods.GetInputMode(MethodsPtr, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void SetInputModeCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + SetInputModeHandler callback = (SetInputModeHandler)h.Target; + h.Free(); + callback(result); + } + + public void SetInputMode(InputMode inputMode, SetInputModeHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.SetInputMode(MethodsPtr, inputMode, GCHandle.ToIntPtr(wrapped), SetInputModeCallbackImpl); + } + + public bool IsSelfMute() + { + var ret = new bool(); + var res = Methods.IsSelfMute(MethodsPtr, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public void SetSelfMute(bool mute) + { + var res = Methods.SetSelfMute(MethodsPtr, mute); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + public bool IsSelfDeaf() + { + var ret = new bool(); + var res = Methods.IsSelfDeaf(MethodsPtr, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public void SetSelfDeaf(bool deaf) + { + var res = Methods.SetSelfDeaf(MethodsPtr, deaf); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + public bool IsLocalMute(Int64 userId) + { + var ret = new bool(); + var res = Methods.IsLocalMute(MethodsPtr, userId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public void SetLocalMute(Int64 userId, bool mute) + { + var res = Methods.SetLocalMute(MethodsPtr, userId, mute); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + public byte GetLocalVolume(Int64 userId) + { + var ret = new byte(); + var res = Methods.GetLocalVolume(MethodsPtr, userId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public void SetLocalVolume(Int64 userId, byte volume) + { + var res = Methods.SetLocalVolume(MethodsPtr, userId, volume); + if (res != Result.Ok) + { + throw new ResultException(res); + } + } + + [MonoPInvokeCallback] + private static void OnSettingsUpdateImpl(IntPtr ptr) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.VoiceManagerInstance.OnSettingsUpdate != null) + { + d.VoiceManagerInstance.OnSettingsUpdate.Invoke(); + } + } + } + + public partial class AchievementManager + { + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIEvents + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void UserAchievementUpdateHandler(IntPtr ptr, ref UserAchievement userAchievement); + + internal UserAchievementUpdateHandler OnUserAchievementUpdate; + } + + [StructLayout(LayoutKind.Sequential)] + internal partial struct FFIMethods + { + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SetUserAchievementCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SetUserAchievementMethod(IntPtr methodsPtr, Int64 achievementId, byte percentComplete, IntPtr callbackData, SetUserAchievementCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FetchUserAchievementsCallback(IntPtr ptr, Result result); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void FetchUserAchievementsMethod(IntPtr methodsPtr, IntPtr callbackData, FetchUserAchievementsCallback callback); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void CountUserAchievementsMethod(IntPtr methodsPtr, ref Int32 count); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetUserAchievementMethod(IntPtr methodsPtr, Int64 userAchievementId, ref UserAchievement userAchievement); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate Result GetUserAchievementAtMethod(IntPtr methodsPtr, Int32 index, ref UserAchievement userAchievement); + + internal SetUserAchievementMethod SetUserAchievement; + + internal FetchUserAchievementsMethod FetchUserAchievements; + + internal CountUserAchievementsMethod CountUserAchievements; + + internal GetUserAchievementMethod GetUserAchievement; + + internal GetUserAchievementAtMethod GetUserAchievementAt; + } + + public delegate void SetUserAchievementHandler(Result result); + + public delegate void FetchUserAchievementsHandler(Result result); + + public delegate void UserAchievementUpdateHandler(ref UserAchievement userAchievement); + + private IntPtr MethodsPtr; + + private Object MethodsStructure; + + private FFIMethods Methods + { + get + { + if (MethodsStructure == null) + { + MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods)); + } + return (FFIMethods)MethodsStructure; + } + + } + + public event UserAchievementUpdateHandler OnUserAchievementUpdate; + + internal AchievementManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events) + { + if (eventsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + InitEvents(eventsPtr, ref events); + MethodsPtr = ptr; + if (MethodsPtr == IntPtr.Zero) { + throw new ResultException(Result.InternalError); + } + } + + private void InitEvents(IntPtr eventsPtr, ref FFIEvents events) + { + events.OnUserAchievementUpdate = OnUserAchievementUpdateImpl; + Marshal.StructureToPtr(events, eventsPtr, false); + } + + [MonoPInvokeCallback] + private static void SetUserAchievementCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + SetUserAchievementHandler callback = (SetUserAchievementHandler)h.Target; + h.Free(); + callback(result); + } + + public void SetUserAchievement(Int64 achievementId, byte percentComplete, SetUserAchievementHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.SetUserAchievement(MethodsPtr, achievementId, percentComplete, GCHandle.ToIntPtr(wrapped), SetUserAchievementCallbackImpl); + } + + [MonoPInvokeCallback] + private static void FetchUserAchievementsCallbackImpl(IntPtr ptr, Result result) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + FetchUserAchievementsHandler callback = (FetchUserAchievementsHandler)h.Target; + h.Free(); + callback(result); + } + + public void FetchUserAchievements(FetchUserAchievementsHandler callback) + { + GCHandle wrapped = GCHandle.Alloc(callback); + Methods.FetchUserAchievements(MethodsPtr, GCHandle.ToIntPtr(wrapped), FetchUserAchievementsCallbackImpl); + } + + public Int32 CountUserAchievements() + { + var ret = new Int32(); + Methods.CountUserAchievements(MethodsPtr, ref ret); + return ret; + } + + public UserAchievement GetUserAchievement(Int64 userAchievementId) + { + var ret = new UserAchievement(); + var res = Methods.GetUserAchievement(MethodsPtr, userAchievementId, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + public UserAchievement GetUserAchievementAt(Int32 index) + { + var ret = new UserAchievement(); + var res = Methods.GetUserAchievementAt(MethodsPtr, index, ref ret); + if (res != Result.Ok) + { + throw new ResultException(res); + } + return ret; + } + + [MonoPInvokeCallback] + private static void OnUserAchievementUpdateImpl(IntPtr ptr, ref UserAchievement userAchievement) + { + GCHandle h = GCHandle.FromIntPtr(ptr); + Discord d = (Discord)h.Target; + if (d.AchievementManagerInstance.OnUserAchievementUpdate != null) + { + d.AchievementManagerInstance.OnUserAchievementUpdate.Invoke(ref userAchievement); + } + } + } +} diff --git a/libs/discordGameSDK/csharp/ImageManager.cs b/libs/discordGameSDK/csharp/ImageManager.cs new file mode 100644 index 0000000..292e230 --- /dev/null +++ b/libs/discordGameSDK/csharp/ImageManager.cs @@ -0,0 +1,53 @@ +using System; +using System.Runtime.InteropServices; +#if UNITY_EDITOR || UNITY_STANDALONE +using UnityEngine; +#endif + +namespace Discord +{ + public partial struct ImageHandle + { + static public ImageHandle User(Int64 id) + { + return User(id, 128); + } + + static public ImageHandle User(Int64 id, UInt32 size) + { + return new ImageHandle + { + Type = ImageType.User, + Id = id, + Size = size, + }; + } + } + + public partial class ImageManager + { + public void Fetch(ImageHandle handle, FetchHandler callback) + { + Fetch(handle, false, callback); + } + + public byte[] GetData(ImageHandle handle) + { + var dimensions = GetDimensions(handle); + var data = new byte[dimensions.Width * dimensions.Height * 4]; + GetData(handle, data); + return data; + } + +#if UNITY_EDITOR || UNITY_STANDALONE + public Texture2D GetTexture(ImageHandle handle) + { + var dimensions = GetDimensions(handle); + var texture = new Texture2D((int)dimensions.Width, (int)dimensions.Height, TextureFormat.RGBA32, false, true); + texture.LoadRawTextureData(GetData(handle)); + texture.Apply(); + return texture; + } +#endif + } +} diff --git a/libs/discordGameSDK/csharp/LobbyManager.cs b/libs/discordGameSDK/csharp/LobbyManager.cs new file mode 100644 index 0000000..c914ba8 --- /dev/null +++ b/libs/discordGameSDK/csharp/LobbyManager.cs @@ -0,0 +1,26 @@ +using System; +using System.Runtime.InteropServices; +using System.Collections.Generic; +using System.Text; + +namespace Discord +{ + public partial class LobbyManager + { + public IEnumerable GetMemberUsers(Int64 lobbyID) + { + var memberCount = MemberCount(lobbyID); + var members = new List(); + for (var i = 0; i < memberCount; i++) + { + members.Add(GetMemberUser(lobbyID, GetMemberUserId(lobbyID, i))); + } + return members; + } + + public void SendLobbyMessage(Int64 lobbyID, string data, SendLobbyMessageHandler handler) + { + SendLobbyMessage(lobbyID, Encoding.UTF8.GetBytes(data), handler); + } + } +} diff --git a/libs/discordGameSDK/csharp/StorageManager.cs b/libs/discordGameSDK/csharp/StorageManager.cs new file mode 100644 index 0000000..65cfe72 --- /dev/null +++ b/libs/discordGameSDK/csharp/StorageManager.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Discord +{ + public partial class StorageManager + { + public IEnumerable Files() + { + var fileCount = Count(); + var files = new List(); + for (var i = 0; i < fileCount; i++) + { + files.Add(StatAt(i)); + } + return files; + } + } +} diff --git a/libs/discordGameSDK/csharp/StoreManager.cs b/libs/discordGameSDK/csharp/StoreManager.cs new file mode 100644 index 0000000..4864576 --- /dev/null +++ b/libs/discordGameSDK/csharp/StoreManager.cs @@ -0,0 +1,32 @@ +using System; +using System.Runtime.InteropServices; +using System.Collections.Generic; +using System.Text; + +namespace Discord +{ + public partial class StoreManager + { + public IEnumerable GetEntitlements() + { + var count = CountEntitlements(); + var entitlements = new List(); + for (var i = 0; i < count; i++) + { + entitlements.Add(GetEntitlementAt(i)); + } + return entitlements; + } + + public IEnumerable GetSkus() + { + var count = CountSkus(); + var skus = new List(); + for (var i = 0; i < count; i++) + { + skus.Add(GetSkuAt(i)); + } + return skus; + } + } +} diff --git a/libs/discordGameSDK/examples/c/main.c b/libs/discordGameSDK/examples/c/main.c new file mode 100644 index 0000000..1aca717 --- /dev/null +++ b/libs/discordGameSDK/examples/c/main.c @@ -0,0 +1,155 @@ +#include +#include +#include +#include "discord_game_sdk.h" +#ifdef _WIN32 +#include +#else +#include +#include +#endif + +#define DISCORD_REQUIRE(x) assert(x == DiscordResult_Ok) + +struct Application { + struct IDiscordCore* core; + struct IDiscordUserManager* users; + struct IDiscordAchievementManager* achievements; + struct IDiscordActivityManager* activities; + struct IDiscordRelationshipManager* relationships; + struct IDiscordApplicationManager* application; + struct IDiscordLobbyManager* lobbies; + DiscordUserId user_id; +}; + +void UpdateActivityCallback(void* data, enum EDiscordResult result) +{ + DISCORD_REQUIRE(result); +} + +int RelationshipPassFilter(void* data, struct DiscordRelationship* relationship) +{ + return (relationship->type == DiscordRelationshipType_Friend); +} + +int RelationshipSnowflakeFilter(void* data, struct DiscordRelationship* relationship) +{ + struct Application* app = (struct Application*)data; + + return (relationship->type == DiscordRelationshipType_Friend && + relationship->user.id < app->user_id); +} + +void OnRelationshipsRefresh(void* data) +{ + struct Application* app = (struct Application*)data; + struct IDiscordRelationshipManager* module = app->relationships; + + module->filter(module, app, RelationshipPassFilter); + + int32_t unfiltered_count = 0; + DISCORD_REQUIRE(module->count(module, &unfiltered_count)); + + module->filter(module, app, RelationshipSnowflakeFilter); + + int32_t filtered_count = 0; + DISCORD_REQUIRE(module->count(module, &filtered_count)); + + printf("=== Cool Friends ===\n"); + for (int32_t i = 0; i < filtered_count; i += 1) { + struct DiscordRelationship relationship; + DISCORD_REQUIRE(module->get_at(module, i, &relationship)); + + printf("%lld %s#%s\n", + relationship.user.id, + relationship.user.username, + relationship.user.discriminator); + } + printf("(%d friends less cool than you omitted)\n", unfiltered_count - filtered_count); + + struct DiscordActivity activity; + memset(&activity, 0, sizeof(activity)); + sprintf(activity.details, "Cooler than %d friends", unfiltered_count - filtered_count); + sprintf(activity.state, "%d friends total", unfiltered_count); + + app->activities->update_activity(app->activities, &activity, app, UpdateActivityCallback); +} + +void OnUserUpdated(void* data) +{ + struct Application* app = (struct Application*)data; + struct DiscordUser user; + app->users->get_current_user(app->users, &user); + app->user_id = user.id; +} + +void OnOAuth2Token(void* data, enum EDiscordResult result, struct DiscordOAuth2Token* token) +{ + if (result == DiscordResult_Ok) { + printf("OAuth2 token: %s\n", token->access_token); + } + else { + printf("GetOAuth2Token failed with %d\n", (int)result); + } +} + +void OnLobbyConnect(void* data, enum EDiscordResult result, struct DiscordLobby* lobby) +{ + printf("LobbyConnect returned %d\n", (int)result); +} + +int main(int argc, char** argv) +{ + struct Application app; + memset(&app, 0, sizeof(app)); + + struct IDiscordUserEvents users_events; + memset(&users_events, 0, sizeof(users_events)); + users_events.on_current_user_update = OnUserUpdated; + + struct IDiscordActivityEvents activities_events; + memset(&activities_events, 0, sizeof(activities_events)); + + struct IDiscordRelationshipEvents relationships_events; + memset(&relationships_events, 0, sizeof(relationships_events)); + relationships_events.on_refresh = OnRelationshipsRefresh; + + struct DiscordCreateParams params; + DiscordCreateParamsSetDefault(¶ms); + params.client_id = 418559331265675294; + params.flags = DiscordCreateFlags_Default; + params.event_data = &app; + params.activity_events = &activities_events; + params.relationship_events = &relationships_events; + params.user_events = &users_events; + DISCORD_REQUIRE(DiscordCreate(DISCORD_VERSION, ¶ms, &app.core)); + + app.users = app.core->get_user_manager(app.core); + app.achievements = app.core->get_achievement_manager(app.core); + app.activities = app.core->get_activity_manager(app.core); + app.application = app.core->get_application_manager(app.core); + app.lobbies = app.core->get_lobby_manager(app.core); + + app.lobbies->connect_lobby_with_activity_secret( + app.lobbies, "invalid_secret", &app, OnLobbyConnect); + + app.application->get_oauth2_token(app.application, &app, OnOAuth2Token); + + DiscordBranch branch; + app.application->get_current_branch(app.application, &branch); + printf("Current branch %s\n", branch); + + app.relationships = app.core->get_relationship_manager(app.core); + + for (;;) { + DISCORD_REQUIRE(app.core->run_callbacks(app.core)); + +#ifdef _WIN32 + Sleep(16); +#else + usleep(16 * 1000); +#endif + } + + return 0; +} diff --git a/libs/discordGameSDK/examples/cpp/main.cpp b/libs/discordGameSDK/examples/cpp/main.cpp new file mode 100644 index 0000000..959cf94 --- /dev/null +++ b/libs/discordGameSDK/examples/cpp/main.cpp @@ -0,0 +1,297 @@ +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "discord.h" + +#if defined(_WIN32) +#pragma pack(push, 1) +struct BitmapImageHeader { + uint32_t const structSize{sizeof(BitmapImageHeader)}; + int32_t width{0}; + int32_t height{0}; + uint16_t const planes{1}; + uint16_t const bpp{32}; + uint32_t const pad0{0}; + uint32_t const pad1{0}; + uint32_t const hres{2835}; + uint32_t const vres{2835}; + uint32_t const pad4{0}; + uint32_t const pad5{0}; + + BitmapImageHeader& operator=(BitmapImageHeader const&) = delete; +}; + +struct BitmapFileHeader { + uint8_t const magic0{'B'}; + uint8_t const magic1{'M'}; + uint32_t size{0}; + uint32_t const pad{0}; + uint32_t const offset{sizeof(BitmapFileHeader) + sizeof(BitmapImageHeader)}; + + BitmapFileHeader& operator=(BitmapFileHeader const&) = delete; +}; +#pragma pack(pop) +#endif + +struct DiscordState { + discord::User currentUser; + + std::unique_ptr core; +}; + +namespace { +volatile bool interrupted{false}; +} + +int main(int, char**) +{ + DiscordState state{}; + + discord::Core* core{}; + auto result = discord::Core::Create(310270644849737729, DiscordCreateFlags_Default, &core); + state.core.reset(core); + if (!state.core) { + std::cout << "Failed to instantiate discord core! (err " << static_cast(result) + << ")\n"; + std::exit(-1); + } + + state.core->SetLogHook( + discord::LogLevel::Debug, [](discord::LogLevel level, const char* message) { + std::cerr << "Log(" << static_cast(level) << "): " << message << "\n"; + }); + + core->UserManager().OnCurrentUserUpdate.Connect([&state]() { + state.core->UserManager().GetCurrentUser(&state.currentUser); + + std::cout << "Current user updated: " << state.currentUser.GetUsername() << "#" + << state.currentUser.GetDiscriminator() << "\n"; + + state.core->UserManager().GetUser(130050050968518656, + [](discord::Result result, discord::User const& user) { + if (result == discord::Result::Ok) { + std::cout << "Get " << user.GetUsername() << "\n"; + } + else { + std::cout << "Failed to get David!\n"; + } + }); + + discord::ImageHandle handle{}; + handle.SetId(state.currentUser.GetId()); + handle.SetType(discord::ImageType::User); + handle.SetSize(256); + + state.core->ImageManager().Fetch( + handle, true, [&state](discord::Result res, discord::ImageHandle handle) { + if (res == discord::Result::Ok) { + discord::ImageDimensions dims{}; + state.core->ImageManager().GetDimensions(handle, &dims); + std::cout << "Fetched " << dims.GetWidth() << "x" << dims.GetHeight() + << " avatar!\n"; + + std::vector data; + data.reserve(dims.GetWidth() * dims.GetHeight() * 4); + uint8_t* d = data.data(); + state.core->ImageManager().GetData(handle, d, data.size()); + +#if defined(_WIN32) + auto fileSize = + data.size() + sizeof(BitmapImageHeader) + sizeof(BitmapFileHeader); + + BitmapImageHeader imageHeader; + imageHeader.width = static_cast(dims.GetWidth()); + imageHeader.height = static_cast(dims.GetHeight()); + + BitmapFileHeader fileHeader; + fileHeader.size = static_cast(fileSize); + + FILE* fp = fopen("avatar.bmp", "wb"); + fwrite(&fileHeader, sizeof(BitmapFileHeader), 1, fp); + fwrite(&imageHeader, sizeof(BitmapImageHeader), 1, fp); + + for (auto y = 0u; y < dims.GetHeight(); ++y) { + auto pixels = reinterpret_cast(data.data()); + auto invY = dims.GetHeight() - y - 1; + fwrite( + &pixels[invY * dims.GetWidth()], sizeof(uint32_t) * dims.GetWidth(), 1, fp); + } + + fflush(fp); + fclose(fp); +#endif + } + else { + std::cout << "Failed fetching avatar. (err " << static_cast(res) << ")\n"; + } + }); + }); + + state.core->ActivityManager().RegisterCommand("run/command/foo/bar/baz/here.exe"); + state.core->ActivityManager().RegisterSteam(123123321); + + state.core->ActivityManager().OnActivityJoin.Connect( + [](const char* secret) { std::cout << "Join " << secret << "\n"; }); + state.core->ActivityManager().OnActivitySpectate.Connect( + [](const char* secret) { std::cout << "Spectate " << secret << "\n"; }); + state.core->ActivityManager().OnActivityJoinRequest.Connect([](discord::User const& user) { + std::cout << "Join Request " << user.GetUsername() << "\n"; + }); + state.core->ActivityManager().OnActivityInvite.Connect( + [](discord::ActivityActionType, discord::User const& user, discord::Activity const&) { + std::cout << "Invite " << user.GetUsername() << "\n"; + }); + + state.core->LobbyManager().OnLobbyUpdate.Connect( + [](std::int64_t lobbyId) { std::cout << "Lobby update " << lobbyId << "\n"; }); + + state.core->LobbyManager().OnLobbyDelete.Connect( + [](std::int64_t lobbyId, std::uint32_t reason) { + std::cout << "Lobby delete " << lobbyId << " (reason: " << reason << ")\n"; + }); + + state.core->LobbyManager().OnMemberConnect.Connect( + [](std::int64_t lobbyId, std::int64_t userId) { + std::cout << "Lobby member connect " << lobbyId << " userId " << userId << "\n"; + }); + + state.core->LobbyManager().OnMemberUpdate.Connect( + [](std::int64_t lobbyId, std::int64_t userId) { + std::cout << "Lobby member update " << lobbyId << " userId " << userId << "\n"; + }); + + state.core->LobbyManager().OnMemberDisconnect.Connect( + [](std::int64_t lobbyId, std::int64_t userId) { + std::cout << "Lobby member disconnect " << lobbyId << " userId " << userId << "\n"; + }); + + state.core->LobbyManager().OnLobbyMessage.Connect([&](std::int64_t lobbyId, + std::int64_t userId, + std::uint8_t* payload, + std::uint32_t payloadLength) { + std::vector buffer{}; + buffer.resize(payloadLength); + memcpy(buffer.data(), payload, payloadLength); + std::cout << "Lobby message " << lobbyId << " from " << userId << " of length " + << payloadLength << " bytes.\n"; + + char fourtyNinetySix[4096]; + state.core->LobbyManager().GetLobbyMetadataValue(lobbyId, "foo", fourtyNinetySix); + + std::cout << "Metadata for key foo is " << fourtyNinetySix << "\n"; + }); + + state.core->LobbyManager().OnSpeaking.Connect( + [&](std::int64_t, std::int64_t userId, bool speaking) { + std::cout << "User " << userId << " is " << (speaking ? "" : "NOT ") << "speaking.\n"; + }); + + discord::Activity activity{}; + activity.SetDetails("Fruit Tarts"); + activity.SetState("Pop Snacks"); + activity.GetAssets().SetSmallImage("the"); + activity.GetAssets().SetSmallText("i mage"); + activity.GetAssets().SetLargeImage("the"); + activity.GetAssets().SetLargeText("u mage"); + activity.SetType(discord::ActivityType::Playing); + state.core->ActivityManager().UpdateActivity(activity, [](discord::Result result) { + std::cout << ((result == discord::Result::Ok) ? "Succeeded" : "Failed") + << " updating activity!\n"; + }); + + discord::LobbyTransaction lobby{}; + state.core->LobbyManager().GetLobbyCreateTransaction(&lobby); + lobby.SetCapacity(2); + lobby.SetMetadata("foo", "bar"); + lobby.SetMetadata("baz", "bat"); + lobby.SetType(discord::LobbyType::Public); + state.core->LobbyManager().CreateLobby( + lobby, [&state](discord::Result result, discord::Lobby const& lobby) { + if (result == discord::Result::Ok) { + std::cout << "Created lobby with secret " << lobby.GetSecret() << "\n"; + std::array data{}; + state.core->LobbyManager().SendLobbyMessage( + lobby.GetId(), + reinterpret_cast(data.data()), + data.size(), + [](discord::Result result) { + std::cout << "Sent message. Result: " << static_cast(result) << "\n"; + }); + } + else { + std::cout << "Failed creating lobby. (err " << static_cast(result) << ")\n"; + } + + discord::LobbySearchQuery query{}; + state.core->LobbyManager().GetSearchQuery(&query); + query.Limit(1); + state.core->LobbyManager().Search(query, [&state](discord::Result result) { + if (result == discord::Result::Ok) { + std::int32_t lobbyCount{}; + state.core->LobbyManager().LobbyCount(&lobbyCount); + std::cout << "Lobby search succeeded with " << lobbyCount << " lobbies.\n"; + for (auto i = 0; i < lobbyCount; ++i) { + discord::LobbyId lobbyId{}; + state.core->LobbyManager().GetLobbyId(i, &lobbyId); + std::cout << " " << lobbyId << "\n"; + } + } + else { + std::cout << "Lobby search failed. (err " << static_cast(result) << ")\n"; + } + }); + }); + + state.core->RelationshipManager().OnRefresh.Connect([&]() { + std::cout << "Relationships refreshed!\n"; + + state.core->RelationshipManager().Filter( + [](discord::Relationship const& relationship) -> bool { + return relationship.GetType() == discord::RelationshipType::Friend; + }); + + std::int32_t friendCount{0}; + state.core->RelationshipManager().Count(&friendCount); + + state.core->RelationshipManager().Filter( + [&](discord::Relationship const& relationship) -> bool { + return relationship.GetType() == discord::RelationshipType::Friend && + relationship.GetUser().GetId() < state.currentUser.GetId(); + }); + + std::int32_t filteredCount{0}; + state.core->RelationshipManager().Count(&filteredCount); + + discord::Relationship relationship{}; + for (auto i = 0; i < filteredCount; ++i) { + state.core->RelationshipManager().GetAt(i, &relationship); + std::cout << relationship.GetUser().GetId() << " " + << relationship.GetUser().GetUsername() << "#" + << relationship.GetUser().GetDiscriminator() << "\n"; + } + }); + + state.core->RelationshipManager().OnRelationshipUpdate.Connect( + [](discord::Relationship const& relationship) { + std::cout << "Relationship with " << relationship.GetUser().GetUsername() + << " updated!\n"; + }); + + std::signal(SIGINT, [](int) { interrupted = true; }); + + do { + state.core->RunCallbacks(); + + std::this_thread::sleep_for(std::chrono::milliseconds(16)); + } while (!interrupted); + + return 0; +} diff --git a/libs/discordGameSDK/examples/csharp/Program.cs b/libs/discordGameSDK/examples/csharp/Program.cs new file mode 100644 index 0000000..7e009ad --- /dev/null +++ b/libs/discordGameSDK/examples/csharp/Program.cs @@ -0,0 +1,412 @@ +using System; +using System.Linq; +using System.Threading; +using System.Text; +using System.Runtime.InteropServices; + +class Program +{ + // Request user's avatar data. Sizes can be powers of 2 between 16 and 2048 + static void FetchAvatar(Discord.ImageManager imageManager, Int64 userID) + { + imageManager.Fetch(Discord.ImageHandle.User(userID), (result, handle) => + { + { + if (result == Discord.Result.Ok) + { + // You can also use GetTexture2D within Unity. + // These return raw RGBA. + var data = imageManager.GetData(handle); + Console.WriteLine("image updated {0} {1}", handle.Id, data.Length); + } + else + { + Console.WriteLine("image error {0}", handle.Id); + } + } + }); + } + + // Update user's activity for your game. + // Party and secrets are vital. + // Read https://discordapp.com/developers/docs/rich-presence/how-to for more details. + static void UpdateActivity(Discord.Discord discord, Discord.Lobby lobby) + { + var activityManager = discord.GetActivityManager(); + var lobbyManager = discord.GetLobbyManager(); + + var activity = new Discord.Activity + { + State = "olleh", + Details = "foo details", + Timestamps = + { + Start = 5, + End = 6, + }, + Assets = + { + LargeImage = "foo largeImageKey", + LargeText = "foo largeImageText", + SmallImage = "foo smallImageKey", + SmallText = "foo smallImageText", + }, + Party = { + Id = lobby.Id.ToString(), + Size = { + CurrentSize = lobbyManager.MemberCount(lobby.Id), + MaxSize = (int)lobby.Capacity, + }, + }, + Secrets = { + Join = lobbyManager.GetLobbyActivitySecret(lobby.Id), + }, + Instance = true, + }; + + activityManager.UpdateActivity(activity, result => + { + Console.WriteLine("Update Activity {0}", result); + + // Send an invite to another user for this activity. + // Receiver should see an invite in their DM. + // Use a relationship user's ID for this. + // activityManager + // .SendInvite( + // 364843917537050624, + // Discord.ActivityActionType.Join, + // "", + // inviteResult => + // { + // Console.WriteLine("Invite {0}", inviteResult); + // } + // ); + }); + } + + static void Main(string[] args) + { + // Use your client ID from Discord's developer site. + var clientID = Environment.GetEnvironmentVariable("DISCORD_CLIENT_ID"); + if (clientID == null) + { + clientID = "418559331265675294"; + } + var discord = new Discord.Discord(Int64.Parse(clientID), (UInt64)Discord.CreateFlags.Default); + discord.SetLogHook(Discord.LogLevel.Debug, (level, message) => + { + Console.WriteLine("Log[{0}] {1}", level, message); + }); + + var applicationManager = discord.GetApplicationManager(); + // Get the current locale. This can be used to determine what text or audio the user wants. + Console.WriteLine("Current Locale: {0}", applicationManager.GetCurrentLocale()); + // Get the current branch. For example alpha or beta. + Console.WriteLine("Current Branch: {0}", applicationManager.GetCurrentBranch()); + // If you want to verify information from your game's server then you can + // grab the access token and send it to your server. + // + // This automatically looks for an environment variable passed by the Discord client, + // if it does not exist the Discord client will focus itself for manual authorization. + // + // By-default the SDK grants the identify and rpc scopes. + // Read more at https://discordapp.com/developers/docs/topics/oauth2 + // applicationManager.GetOAuth2Token((Discord.Result result, ref Discord.OAuth2Token oauth2Token) => + // { + // Console.WriteLine("Access Token {0}", oauth2Token.AccessToken); + // }); + + var activityManager = discord.GetActivityManager(); + var lobbyManager = discord.GetLobbyManager(); + // Received when someone accepts a request to join or invite. + // Use secrets to receive back the information needed to add the user to the group/party/match + activityManager.OnActivityJoin += secret => + { + Console.WriteLine("OnJoin {0}", secret); + lobbyManager.ConnectLobbyWithActivitySecret(secret, (Discord.Result result, ref Discord.Lobby lobby) => + { + Console.WriteLine("Connected to lobby: {0}", lobby.Id); + lobbyManager.ConnectNetwork(lobby.Id); + lobbyManager.OpenNetworkChannel(lobby.Id, 0, true); + foreach (var user in lobbyManager.GetMemberUsers(lobby.Id)) + { + lobbyManager.SendNetworkMessage(lobby.Id, user.Id, 0, + Encoding.UTF8.GetBytes(String.Format("Hello, {0}!", user.Username))); + } + UpdateActivity(discord, lobby); + }); + }; + // Received when someone accepts a request to spectate + activityManager.OnActivitySpectate += secret => + { + Console.WriteLine("OnSpectate {0}", secret); + }; + // A join request has been received. Render the request on the UI. + activityManager.OnActivityJoinRequest += (ref Discord.User user) => + { + Console.WriteLine("OnJoinRequest {0} {1}", user.Id, user.Username); + }; + // An invite has been received. Consider rendering the user / activity on the UI. + activityManager.OnActivityInvite += (Discord.ActivityActionType Type, ref Discord.User user, ref Discord.Activity activity2) => + { + Console.WriteLine("OnInvite {0} {1} {2}", Type, user.Username, activity2.Name); + // activityManager.AcceptInvite(user.Id, result => + // { + // Console.WriteLine("AcceptInvite {0}", result); + // }); + }; + // This is used to register the game in the registry such that Discord can find it. + // This is only needed by games acquired from other platforms, like Steam. + // activityManager.RegisterCommand(); + + var imageManager = discord.GetImageManager(); + + var userManager = discord.GetUserManager(); + // The auth manager fires events as information about the current user changes. + // This event will fire once on init. + // + // GetCurrentUser will error until this fires once. + userManager.OnCurrentUserUpdate += () => + { + var currentUser = userManager.GetCurrentUser(); + Console.WriteLine(currentUser.Username); + Console.WriteLine(currentUser.Id); + }; + // If you store Discord user ids in a central place like a leaderboard and want to render them. + // The users manager can be used to fetch arbitrary Discord users. This only provides basic + // information and does not automatically update like relationships. + userManager.GetUser(450795363658366976, (Discord.Result result, ref Discord.User user) => + { + if (result == Discord.Result.Ok) + { + Console.WriteLine("user fetched: {0}", user.Username); + + // Request users's avatar data. + // This can only be done after a user is successfully fetched. + FetchAvatar(imageManager, user.Id); + } + else + { + Console.WriteLine("user fetch error: {0}", result); + } + }); + + var relationshipManager = discord.GetRelationshipManager(); + // It is important to assign this handle right away to get the initial relationships refresh. + // This callback will only be fired when the whole list is initially loaded or was reset + relationshipManager.OnRefresh += () => + { + // Filter a user's relationship list to be just friends + relationshipManager.Filter((ref Discord.Relationship relationship) => { return relationship.Type == Discord.RelationshipType.Friend; }); + // Loop over all friends a user has. + Console.WriteLine("relationships updated: {0}", relationshipManager.Count()); + for (var i = 0; i < Math.Min(relationshipManager.Count(), 10); i++) + { + // Get an individual relationship from the list + var r = relationshipManager.GetAt((uint)i); + Console.WriteLine("relationships: {0} {1} {2} {3}", r.Type, r.User.Username, r.Presence.Status, r.Presence.Activity.Name); + + // Request relationship's avatar data. + FetchAvatar(imageManager, r.User.Id); + } + }; + // All following relationship updates are delivered individually. + // These are fired when a user gets a new friend, removes a friend, or a relationship's presence changes. + relationshipManager.OnRelationshipUpdate += (ref Discord.Relationship r) => + { + Console.WriteLine("relationship updated: {0} {1} {2} {3}", r.Type, r.User.Username, r.Presence.Status, r.Presence.Activity.Name); + }; + + lobbyManager.OnLobbyMessage += (lobbyID, userID, data) => + { + Console.WriteLine("lobby message: {0} {1}", lobbyID, Encoding.UTF8.GetString(data)); + }; + lobbyManager.OnNetworkMessage += (lobbyId, userId, channelId, data) => + { + Console.WriteLine("network message: {0} {1} {2} {3}", lobbyId, userId, channelId, Encoding.UTF8.GetString(data)); + }; + lobbyManager.OnSpeaking += (lobbyID, userID, speaking) => + { + Console.WriteLine("lobby speaking: {0} {1} {2}", lobbyID, userID, speaking); + }; + // Create a lobby. + var transaction = lobbyManager.GetLobbyCreateTransaction(); + transaction.SetCapacity(6); + transaction.SetType(Discord.LobbyType.Public); + transaction.SetMetadata("a", "123"); + transaction.SetMetadata("a", "456"); + transaction.SetMetadata("b", "111"); + transaction.SetMetadata("c", "222"); + + lobbyManager.CreateLobby(transaction, (Discord.Result result, ref Discord.Lobby lobby) => + { + if (result != Discord.Result.Ok) + { + return; + } + + // Check the lobby's configuration. + Console.WriteLine("lobby {0} with capacity {1} and secret {2}", lobby.Id, lobby.Capacity, lobby.Secret); + + // Check lobby metadata. + foreach (var key in new string[] { "a", "b", "c" }) + { + Console.WriteLine("{0} = {1}", key, lobbyManager.GetLobbyMetadataValue(lobby.Id, key)); + } + + // Print all the members of the lobby. + foreach (var user in lobbyManager.GetMemberUsers(lobby.Id)) + { + Console.WriteLine("lobby member: {0}", user.Username); + } + + // Send everyone a message. + lobbyManager.SendLobbyMessage(lobby.Id, "Hello from C#!", (_) => + { + Console.WriteLine("sent message"); + }); + + // Update lobby. + var lobbyTransaction = lobbyManager.GetLobbyUpdateTransaction(lobby.Id); + lobbyTransaction.SetMetadata("d", "e"); + lobbyTransaction.SetCapacity(16); + lobbyManager.UpdateLobby(lobby.Id, lobbyTransaction, (_) => + { + Console.WriteLine("lobby has been updated"); + }); + + // Update a member. + var lobbyID = lobby.Id; + var userID = lobby.OwnerId; + var memberTransaction = lobbyManager.GetMemberUpdateTransaction(lobbyID, userID); + memberTransaction.SetMetadata("hello", "there"); + lobbyManager.UpdateMember(lobbyID, userID, memberTransaction, (_) => + { + Console.WriteLine("lobby member has been updated: {0}", lobbyManager.GetMemberMetadataValue(lobbyID, userID, "hello")); + }); + + // Search lobbies. + var query = lobbyManager.GetSearchQuery(); + // Filter by a metadata value. + query.Filter("metadata.a", Discord.LobbySearchComparison.GreaterThan, Discord.LobbySearchCast.Number, "455"); + query.Sort("metadata.a", Discord.LobbySearchCast.Number, "0"); + // Only return 1 result max. + query.Limit(1); + lobbyManager.Search(query, (_) => + { + Console.WriteLine("search returned {0} lobbies", lobbyManager.LobbyCount()); + if (lobbyManager.LobbyCount() == 1) + { + Console.WriteLine("first lobby secret: {0}", lobbyManager.GetLobby(lobbyManager.GetLobbyId(0)).Secret); + } + }); + + // Connect to voice chat. + lobbyManager.ConnectVoice(lobby.Id, (_) => + { + Console.WriteLine("Connected to voice chat!"); + }); + + // Setup networking. + lobbyManager.ConnectNetwork(lobby.Id); + lobbyManager.OpenNetworkChannel(lobby.Id, 0, true); + + // Update activity. + UpdateActivity(discord, lobby); + }); + + /* + var overlayManager = discord.GetOverlayManager(); + overlayManager.OnOverlayLocked += locked => + { + Console.WriteLine("Overlay Locked: {0}", locked); + }; + overlayManager.SetLocked(false); + */ + + var storageManager = discord.GetStorageManager(); + var contents = new byte[20000]; + var random = new Random(); + random.NextBytes(contents); + Console.WriteLine("storage path: {0}", storageManager.GetPath()); + storageManager.WriteAsync("foo", contents, res => + { + var files = storageManager.Files(); + foreach (var file in files) + { + Console.WriteLine("file: {0} size: {1} last_modified: {2}", file.Filename, file.Size, file.LastModified); + } + storageManager.ReadAsyncPartial("foo", 400, 50, (result, data) => + { + Console.WriteLine("partial contents of foo match {0}", Enumerable.SequenceEqual(data, new ArraySegment(contents, 400, 50))); + }); + storageManager.ReadAsync("foo", (result, data) => + { + Console.WriteLine("length of contents {0} data {1}", contents.Length, data.Length); + Console.WriteLine("contents of foo match {0}", Enumerable.SequenceEqual(data, contents)); + Console.WriteLine("foo exists? {0}", storageManager.Exists("foo")); + storageManager.Delete("foo"); + Console.WriteLine("post-delete foo exists? {0}", storageManager.Exists("foo")); + }); + }); + + var storeManager = discord.GetStoreManager(); + storeManager.OnEntitlementCreate += (ref Discord.Entitlement entitlement) => + { + Console.WriteLine("Entitlement Create1: {0}", entitlement.Id); + }; + + // Start a purchase flow. + // storeManager.StartPurchase(487507201519255552, result => + // { + // if (result == Discord.Result.Ok) + // { + // Console.WriteLine("Purchase Complete"); + // } + // else + // { + // Console.WriteLine("Purchase Canceled"); + // } + // }); + + // Get all entitlements. + storeManager.FetchEntitlements(result => + { + if (result == Discord.Result.Ok) + { + foreach (var entitlement in storeManager.GetEntitlements()) + { + Console.WriteLine("entitlement: {0} - {1} {2}", entitlement.Id, entitlement.Type, entitlement.SkuId); + } + } + }); + + // Get all SKUs. + storeManager.FetchSkus(result => + { + if (result == Discord.Result.Ok) + { + foreach (var sku in storeManager.GetSkus()) + { + Console.WriteLine("sku: {0} - {1} {2}", sku.Name, sku.Price.Amount, sku.Price.Currency); + } + } + }); + + // Pump the event look to ensure all callbacks continue to get fired. + try + { + while (true) + { + discord.RunCallbacks(); + lobbyManager.FlushNetwork(); + Thread.Sleep(1000 / 60); + } + } + finally + { + discord.Dispose(); + } + + } +} diff --git a/libs/discordGameSDK/lib/x86/discord_game_sdk.dll b/libs/discordGameSDK/lib/x86/discord_game_sdk.dll new file mode 100644 index 0000000..8f1ee0a Binary files /dev/null and b/libs/discordGameSDK/lib/x86/discord_game_sdk.dll differ diff --git a/libs/discordGameSDK/lib/x86/discord_game_sdk.dll.lib b/libs/discordGameSDK/lib/x86/discord_game_sdk.dll.lib new file mode 100644 index 0000000..58ec282 Binary files /dev/null and b/libs/discordGameSDK/lib/x86/discord_game_sdk.dll.lib differ diff --git a/libs/discordGameSDK/lib/x86_64/discord_game_sdk.bundle b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.bundle new file mode 100644 index 0000000..24045f7 Binary files /dev/null and b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.bundle differ diff --git a/libs/discordGameSDK/lib/x86_64/discord_game_sdk.dll b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.dll new file mode 100644 index 0000000..10a8928 Binary files /dev/null and b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.dll differ diff --git a/libs/discordGameSDK/lib/x86_64/discord_game_sdk.dll.lib b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.dll.lib new file mode 100644 index 0000000..8ab3d4c Binary files /dev/null and b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.dll.lib differ diff --git a/libs/discordGameSDK/lib/x86_64/discord_game_sdk.dylib b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.dylib new file mode 100644 index 0000000..24045f7 Binary files /dev/null and b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.dylib differ diff --git a/libs/discordGameSDK/lib/x86_64/discord_game_sdk.so b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.so new file mode 100644 index 0000000..e465760 Binary files /dev/null and b/libs/discordGameSDK/lib/x86_64/discord_game_sdk.so differ diff --git a/load/gamesdk.lua b/load/gamesdk.lua new file mode 100644 index 0000000..f50fe88 --- /dev/null +++ b/load/gamesdk.lua @@ -0,0 +1,13 @@ + +print("Loading Discord GameSDK...") +DiscordGameSDK = { + loaded = false +} +local success, libDiscordGameSDK = pcall(require, "libs.discordGameSDK") +if success then + DiscordGameSDK.loaded = true + + print("Discord GameSDK successfully loaded") +else + print("Discord GameSDK failed to load!") +end \ No newline at end of file diff --git a/load/graphics.lua b/load/graphics.lua index e1297d1..e351b17 100644 --- a/load/graphics.lua +++ b/load/graphics.lua @@ -20,6 +20,7 @@ backgrounds = { love.graphics.newImage("res/backgrounds/1800.png"), love.graphics.newImage("res/backgrounds/1900.png"), title = love.graphics.newImage("res/backgrounds/title.png"), + title_night = love.graphics.newImage("res/backgrounds/title-night.jpg"), snow = love.graphics.newImage("res/backgrounds/snow.png"), input_config = love.graphics.newImage("res/backgrounds/options-input.png"), game_config = love.graphics.newImage("res/backgrounds/options-game.png"), @@ -118,5 +119,6 @@ misc_graphics = { go = love.graphics.newImage("res/img/go.png"), select_mode = love.graphics.newImage("res/img/select_mode.png"), strike = love.graphics.newImage("res/img/strike.png"), - santa = love.graphics.newImage("res/img/santa.png") + santa = love.graphics.newImage("res/img/santa.png"), + icon = love.graphics.newImage("res/img/cambridge_transparent.png") } \ No newline at end of file diff --git a/load/sounds.lua b/load/sounds.lua index 0dfae21..6e03b1f 100644 --- a/load/sounds.lua +++ b/load/sounds.lua @@ -1,63 +1,98 @@ -sounds = { +sound_paths = { blocks = { - I = love.audio.newSource("res/se/piece_i.wav", "static"), - J = love.audio.newSource("res/se/piece_j.wav", "static"), - L = love.audio.newSource("res/se/piece_l.wav", "static"), - O = love.audio.newSource("res/se/piece_o.wav", "static"), - S = love.audio.newSource("res/se/piece_s.wav", "static"), - T = love.audio.newSource("res/se/piece_t.wav", "static"), - Z = love.audio.newSource("res/se/piece_z.wav", "static") + I = "res/se/piece_i.wav", + J = "res/se/piece_j.wav", + L = "res/se/piece_l.wav", + O = "res/se/piece_o.wav", + S = "res/se/piece_s.wav", + T = "res/se/piece_t.wav", + Z = "res/se/piece_z.wav" }, - move = love.audio.newSource("res/se/move.wav", "static"), - bottom = love.audio.newSource("res/se/bottom.wav", "static"), - cursor = love.audio.newSource("res/se/cursor.wav", "static"), - cursor_lr = love.audio.newSource("res/se/cursor_lr.wav", "static"), - main_decide = love.audio.newSource("res/se/main_decide.wav", "static"), - mode_decide = love.audio.newSource("res/se/mode_decide.wav", "static"), - lock = love.audio.newSource("res/se/lock.wav", "static"), - hold = love.audio.newSource("res/se/hold.wav", "static"), - erase = love.audio.newSource("res/se/erase.wav", "static"), - fall = love.audio.newSource("res/se/fall.wav", "static"), - ready = love.audio.newSource("res/se/ready.wav", "static"), - go = love.audio.newSource("res/se/go.wav", "static"), - irs = love.audio.newSource("res/se/irs.wav", "static"), - ihs = love.audio.newSource("res/se/ihs.wav", "static"), + move = "res/se/move.wav", + rotate = "res/se/rotate.wav", + kick = "res/se/kick.wav", + bottom = "res/se/bottom.wav", + cursor = "res/se/cursor.wav", + cursor_lr = "res/se/cursor_lr.wav", + main_decide = "res/se/main_decide.wav", + mode_decide = "res/se/mode_decide.wav", + lock = "res/se/lock.wav", + hold = "res/se/hold.wav", + erase = { + single = "res/se/single.wav", + double = "res/se/double.wav", + triple = "res/se/triple.wav", + quad = "res/se/quad.wav" + }, + fall = "res/se/fall.wav", + ready = "res/se/ready.wav", + go = "res/se/go.wav", + irs = "res/se/irs.wav", + ihs = "res/se/ihs.wav", -- a secret sound! - welcome = love.audio.newSource("res/se/welcomeToCambridge.wav", "static"), + welcome = "res/se/welcomeToCambridge.wav", } +sounds = {} +-- Replace each sound effect string with its love audiosource counterpart, but only if it exists. This lets the game handle missing SFX. +for k,v in pairs(sound_paths) do + if(type(v) == "table") then + -- list of subsounds + for k2,v2 in pairs(v) do + if(love.filesystem.getInfo(sound_paths[k][k2])) then + -- this file exists + sounds[k] = sounds[k] or {} + sounds[k][k2] = love.audio.newSource(sound_paths[k][k2], "static") + end + end + else + if(love.filesystem.getInfo(sound_paths[k])) then + -- this file exists + sounds[k] = love.audio.newSource(sound_paths[k], "static") + end + end +end + function playSE(sound, subsound) if sound ~= nil then - if subsound ~= nil then - sounds[sound][subsound]:setVolume(config.sfx_volume) - if sounds[sound][subsound]:isPlaying() then - sounds[sound][subsound]:stop() + if sounds[sound] then + if subsound ~= nil then + if sounds[sound][subsound] then + sounds[sound][subsound]:setVolume(config.sfx_volume) + if sounds[sound][subsound]:isPlaying() then + sounds[sound][subsound]:stop() + end + sounds[sound][subsound]:play() + end + else + sounds[sound]:setVolume(config.sfx_volume) + if sounds[sound]:isPlaying() then + sounds[sound]:stop() + end + sounds[sound]:play() end - sounds[sound][subsound]:play() - else - sounds[sound]:setVolume(config.sfx_volume) - if sounds[sound]:isPlaying() then - sounds[sound]:stop() - end - sounds[sound]:play() end end end function playSEOnce(sound, subsound) if sound ~= nil then - if subsound ~= nil then - sounds[sound][subsound]:setVolume(config.sfx_volume) - if sounds[sound][subsound]:isPlaying() then - return + if sounds[sound] then + if subsound ~= nil then + if sounds[sound][subsound] then + sounds[sound][subsound]:setVolume(config.sfx_volume) + if sounds[sound][subsound]:isPlaying() then + return + end + sounds[sound][subsound]:play() + end + else + sounds[sound]:setVolume(config.sfx_volume) + if sounds[sound]:isPlaying() then + return + end + sounds[sound]:play() end - sounds[sound][subsound]:play() - else - sounds[sound]:setVolume(config.sfx_volume) - if sounds[sound]:isPlaying() then - return - end - sounds[sound]:play() end end end \ No newline at end of file diff --git a/load/version.lua b/load/version.lua index fa69955..fc8a2fa 100644 --- a/load/version.lua +++ b/load/version.lua @@ -1 +1 @@ -version = "v0.3-beta7-hj" +version = "v0.3" diff --git a/main.lua b/main.lua index de88b5a..f54b3c1 100644 --- a/main.lua +++ b/main.lua @@ -1,6 +1,7 @@ function love.load() math.randomseed(os.time()) highscores = {} + love.graphics.setDefaultFilter("linear", "nearest") require "load.rpc" require "load.graphics" require "load.fonts" @@ -63,7 +64,6 @@ function love.draw() love.graphics.push() -- get offset matrix - love.graphics.setDefaultFilter("linear", "nearest") local width = love.graphics.getWidth() local height = love.graphics.getHeight() local scale_factor = math.min(width / 640, height / 480) @@ -75,8 +75,14 @@ function love.draw() scene:render() - love.graphics.setFont(font_3x5_2) - love.graphics.printf(version, 0, 460, 635, "right") + if config.gamesettings.display_gamemode == 1 or scene.title == "Title" then + love.graphics.setFont(font_3x5_2) + love.graphics.setColor(1, 1, 1, 1) + love.graphics.printf( + string.format("%.2f", 1 / love.timer.getAverageDelta()) .. + "fps - " .. version, 0, 460, 635, "right" + ) + end love.graphics.pop() @@ -94,6 +100,7 @@ function love.keypressed(key, scancode) elseif scancode == "f2" and scene.title ~= "Input Config" and scene.title ~= "Game" then scene = InputConfigScene() switchBGM(nil) + loadSave() -- secret sound playing :eyes: elseif scancode == "f8" and scene.title == "Title" then config.secret = not config.secret @@ -102,8 +109,8 @@ function love.keypressed(key, scancode) if config.secret then playSE("mode_decide") else playSE("erase") end -- f12 is reserved for saving screenshots - elseif scancode == "f12" then - local ss_name = os.date("ss/%Y-%m-%d_%H-%M-%S.png") + elseif scancode == "f12" then + local ss_name = os.date("ss/%Y-%m-%d_%H-%M-%S.png") local info = love.filesystem.getInfo("ss", "directory") if not info then love.filesystem.remove("ss") diff --git a/res/backgrounds/title-night.jpg b/res/backgrounds/title-night.jpg new file mode 100644 index 0000000..1ddbbaa Binary files /dev/null and b/res/backgrounds/title-night.jpg differ diff --git a/res/img/cambridge_transparent.png b/res/img/cambridge_transparent.png new file mode 100644 index 0000000..c7ac026 Binary files /dev/null and b/res/img/cambridge_transparent.png differ diff --git a/res/img/rpc/1year.jpg b/res/img/rpc/1year.jpg new file mode 100644 index 0000000..9d62977 Binary files /dev/null and b/res/img/rpc/1year.jpg differ diff --git a/res/se/double.wav b/res/se/double.wav new file mode 100644 index 0000000..ca326cf Binary files /dev/null and b/res/se/double.wav differ diff --git a/res/se/quad.wav b/res/se/quad.wav new file mode 100644 index 0000000..ca326cf Binary files /dev/null and b/res/se/quad.wav differ diff --git a/res/se/single.wav b/res/se/single.wav new file mode 100644 index 0000000..ca326cf Binary files /dev/null and b/res/se/single.wav differ diff --git a/res/se/triple.wav b/res/se/triple.wav new file mode 100644 index 0000000..ca326cf Binary files /dev/null and b/res/se/triple.wav differ diff --git a/scene/game_config.lua b/scene/game_config.lua index a161cf8..c71aa17 100644 --- a/scene/game_config.lua +++ b/scene/game_config.lua @@ -12,7 +12,7 @@ ConfigScene.options = { {"piece_colour", "Piece Colours", false, {"Per ruleset", "Arika", "TTC"}}, {"world_reverse", "A Button Rotation", false, {"Left", "Auto", "Right"}}, {"spawn_positions", "Spawn Positions", false, {"Per ruleset", "In field", "Out of field"}}, - {"display_gamemode", "Display Gamemode", false, {"On", "Off"}}, + {"display_gamemode", "Debug Info", false, {"On", "Off"}}, {"das_last_key", "DAS Last Key", false, {"Off", "On"}}, {"smooth_movement", "Smooth Piece Drop", false, {"On", "Off"}}, {"synchroes_allowed", "Synchroes", false, {"Per ruleset", "On", "Off"}}, diff --git a/scene/mode_select.lua b/scene/mode_select.lua index 3d2425f..d35594c 100755 --- a/scene/mode_select.lua +++ b/scene/mode_select.lua @@ -27,14 +27,7 @@ function ModeSelectScene:new() ruleset = current_ruleset, select = "mode", } - self.secret_inputs = { - rotate_left = false, - rotate_left2 = false, - rotate_right = false, - rotate_right2 = false, - rotate_180 = false, - hold = false, - } + self.secret_inputs = {} self.das = 0 DiscordRPC:update({ details = "In menus", @@ -157,12 +150,12 @@ function ModeSelectScene:onInputPress(e) end function ModeSelectScene:onInputRelease(e) - if e.input == "hold" or (e.input and string.sub(e.input, 1, 7) == "rotate_") then - self.secret_inputs[e.input] = false - elseif e.input == "up" or e.scancode == "up" then + if e.input == "up" or e.scancode == "up" then self.das_up = nil elseif e.input == "down" or e.scancode == "down" then self.das_down = nil + elseif e.input then + self.secret_inputs[e.input] = false end end diff --git a/scene/title.lua b/scene/title.lua index b9f12b4..05df3bb 100644 --- a/scene/title.lua +++ b/scene/title.lua @@ -44,8 +44,8 @@ function TitleScene:new() DiscordRPC:update({ details = "In menus", state = mainmenuidle[math.random(#mainmenuidle)], - largeImageKey = "icon2", - largeImageText = version + largeImageKey = "1year", + largeImageText = version.." | Thanks for 1 year!" }) end @@ -60,15 +60,28 @@ function TitleScene:update() end function TitleScene:render() - love.graphics.setFont(font_3x5_2) - + love.graphics.setFont(font_3x5_4) love.graphics.setColor(1, 1, 1, 1 - self.snow_bg_opacity) + --[[ love.graphics.draw( backgrounds["title"], 0, 0, 0, 0.5, 0.5 ) + ]] + love.graphics.draw( + backgrounds["title_night"], + 0, 0, 0, + 0.5, 0.5 + ) + love.graphics.draw( + misc_graphics["icon"], + 460, 170, 0, + 2, 2 + ) + love.graphics.printf("Thanks for 1 year!", 430, 280, 160, "center") + love.graphics.setFont(font_3x5_2) love.graphics.setColor(1, 1, 1, self.snow_bg_opacity) love.graphics.draw( backgrounds["snow"], @@ -112,14 +125,17 @@ function TitleScene:onInputPress(e) playSE("cursor") elseif e.input == "menu_back" or e.scancode == "backspace" or e.scancode == "delete" then love.event.quit() + -- no winter easter egg for now + --[[ else - self.text = self.text .. (e.scancode ~= nil and e.scancode or "") + self.text = self.text .. (e.scancode or "") if self.text == "ffffff" then self.text_flag = true DiscordRPC:update({ largeImageKey = "snow" }) end + ]] end end diff --git a/tetris/modes/gamemode.lua b/tetris/modes/gamemode.lua index a4b7c6b..2c805e3 100644 --- a/tetris/modes/gamemode.lua +++ b/tetris/modes/gamemode.lua @@ -5,7 +5,7 @@ local playedReadySE = false local playedGoSE = false local Grid = require 'tetris.components.grid' -local Randomizer = require 'tetris.randomizers.bag7' +local Randomizer = require 'tetris.randomizers.randomizer' local BagRandomizer = require 'tetris.randomizers.bag' local GameMode = Object:extend() @@ -100,13 +100,11 @@ end function GameMode:initialize(ruleset) -- generate next queue self.used_randomizer = ( - ruleset.pieces == self.randomizer.possible_pieces and - self.randomizer or - ( - ruleset.pieces == 7 and - Randomizer() or - BagRandomizer(ruleset.pieces) - ) + table.equalvalues( + table.keys(ruleset.colourscheme), + self.randomizer.possible_pieces + ) and + self.randomizer or BagRandomizer(table.keys(ruleset.colourscheme)) ) self.ruleset = ruleset for i = 1, math.max(self.next_queue_length, 1) do @@ -126,6 +124,8 @@ function GameMode:update(inputs, ruleset) if inputs["left"] or inputs["right"] then inputs["up"] = false inputs["down"] = false + elseif inputs["down"] then + inputs["up"] = false end end @@ -149,7 +149,6 @@ function GameMode:update(inputs, ruleset) else -- perform active frame actions such as fading out the next queue self:whilePieceActive() - local gravity = self:getGravity() if self.enable_hold and inputs["hold"] == true and self.held == false and self.prev_inputs["hold"] == false then self:hold(inputs, ruleset) @@ -269,7 +268,8 @@ function GameMode:update(inputs, ruleset) end if cleared_row_count > 0 then - playSE("erase") + local row_count_names = {"single","double","triple","quad"} + playSE("erase",row_count_names[cleared_row_count] or "quad") self.lcd = self:getLineClearDelay() self.last_lcd = self.lcd self.are = ( @@ -540,12 +540,12 @@ end function GameMode:initializeNextPiece( inputs, ruleset, piece_data, generate_next_piece ) - if not inputs.hold and not self.buffer_soft_drop and self.lock_drop or ( + if not self.buffer_soft_drop and self.lock_drop or ( not ruleset.are or self:getARE() == 0 ) then self.drop_locked = true end - if not inputs.hold and not self.buffer_hard_drop and self.lock_hard_drop or ( + if not self.buffer_hard_drop and self.lock_hard_drop or ( not ruleset.are or self:getARE() == 0 ) then self.hard_drop_locked = true @@ -566,13 +566,12 @@ function GameMode:initializeNextPiece( self.piece.locked = self.lock_on_hard_drop self:onHardDrop(self.piece.position.y - prev_y) end - if self.buffer_soft_drop then - if ( - self.lock_on_soft_drop and - self.piece:isDropBlocked(self.grid) - ) then - self.piece.locked = true - end + if ( + self.buffer_soft_drop and + self.lock_on_soft_drop and + self:getGravity() >= self.grid.height - 4 + ) then + self.piece.locked = true end end self.piece_hard_dropped = false @@ -700,7 +699,10 @@ end function GameMode:drawNextQueue(ruleset) local colourscheme - if ruleset.pieces == 7 then + if table.equalvalues( + self.used_randomizer.possible_pieces, + {"I", "J", "L", "O", "S", "T", "Z"} + ) then colourscheme = ({ruleset.colourscheme, ColourSchemes.Arika, ColourSchemes.TTC})[config.gamesettings.piece_colour] else colourscheme = ruleset.colourscheme @@ -920,13 +922,13 @@ function GameMode:draw(paused) self:drawFrame() self:drawGrid() self:drawPiece() + if self:canDrawLCA() then + self:drawLineClearAnimation() + end self:drawNextQueue(self.ruleset) self:drawScoringInfo() self:drawReadyGo() self:drawCustom() - if self:canDrawLCA() then - self:drawLineClearAnimation() - end love.graphics.setColor(1, 1, 1, 1) love.graphics.setFont(font_3x5_2) diff --git a/tetris/modes/survival_a2.lua b/tetris/modes/survival_a2.lua index 1ebec70..5a6faf8 100644 --- a/tetris/modes/survival_a2.lua +++ b/tetris/modes/survival_a2.lua @@ -157,8 +157,6 @@ function SurvivalA2Game:drawScoringInfo() love.graphics.setFont(font_3x5_3) love.graphics.printf(self.score, text_x, 220, 90, "left") - if self.roll_frames > 1800 then love.graphics.setColor(1, 0.5, 0, 1) - elseif self.level >= 999 and self.clear then love.graphics.setColor(0, 1, 0, 1) end if self:getLetterGrade() ~= "" then love.graphics.printf(self:getLetterGrade(), text_x, 140, 90, "left") end love.graphics.setColor(1, 1, 1, 1) love.graphics.printf(self.level, text_x, 340, 40, "right") diff --git a/tetris/randomizers/bag.lua b/tetris/randomizers/bag.lua index 98b4c3f..e7a60bc 100644 --- a/tetris/randomizers/bag.lua +++ b/tetris/randomizers/bag.lua @@ -2,19 +2,15 @@ local Randomizer = require 'tetris.randomizers.randomizer' local BagRandomizer = Randomizer:extend() -function BagRandomizer:new(pieces) +function BagRandomizer:new(piece_table) self.bag = {} - self.possible_pieces = pieces - self.pieces = pieces - for i = 1, self.pieces do - table.insert(self.bag, i) - end + self.possible_pieces = piece_table end function BagRandomizer:generatePiece() if next(self.bag) == nil then - for i = 1, self.pieces do - table.insert(self.bag, i) + for _, v in pairs(self.possible_pieces) do + table.insert(self.bag, v) end end local x = math.random(table.getn(self.bag)) diff --git a/tetris/randomizers/randomizer.lua b/tetris/randomizers/randomizer.lua index fe4472e..aa0931f 100644 --- a/tetris/randomizers/randomizer.lua +++ b/tetris/randomizers/randomizer.lua @@ -3,7 +3,7 @@ local Object = require 'libs.classic' local Randomizer = Object:extend() function Randomizer:new() - self.possible_pieces = 7 + self.possible_pieces = {"I", "J", "L", "O", "S", "T", "Z"} self:initialize() end @@ -15,10 +15,8 @@ function Randomizer:initialize() -- do nothing end -local shapes = {"I", "J", "L", "O", "S", "T", "Z"} - function Randomizer:generatePiece() - return shapes[math.random(7)] + return self.possible_pieces[math.random(7)] end return Randomizer diff --git a/tetris/rulesets/ruleset.lua b/tetris/rulesets/ruleset.lua index 33c7986..48b1da0 100644 --- a/tetris/rulesets/ruleset.lua +++ b/tetris/rulesets/ruleset.lua @@ -35,8 +35,6 @@ Ruleset.next_sounds = { T = "T" } -Ruleset.pieces = 7 - -- Component functions. function Ruleset:new(game_mode) @@ -100,10 +98,12 @@ function Ruleset:attemptRotate(new_inputs, piece, grid, initial) if (grid:canPlacePiece(new_piece)) then piece:setRelativeRotation(rot_dir) self:onPieceRotate(piece, grid) + playSE("rotate") else if not(initial and self.enable_IRS_wallkicks == false) then self:attemptWallkicks(piece, new_piece, rot_dir, grid) end + playSE("kick") end end @@ -208,7 +208,9 @@ function Ruleset:initializePiece( end local colours - if self.pieces == 7 then + if table.equalvalues( + self.colourscheme, {"I", "J", "L", "O", "S", "T", "Z"} + ) then colours = ({self.colourscheme, ColourSchemes.Arika, ColourSchemes.TTC})[config.gamesettings.piece_colour] else colours = self.colourscheme