Last active
May 7, 2023 23:59
-
-
Save B-Y-P/e7617bb785530220df76e610a5b85148 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// There might be some functionality you need to write for when the App closes | |
/// If so, I never bothered to implement it so... it's left as an exercise to the reader :) | |
#pragma pack(push, 8) | |
// Get Discord game SDK from https://discord.com/developers/docs/game-sdk/sdk-starter-guide | |
// Just use the C api, it's single-file and infinitely easier to implement | |
#include "path/to/discord_game_sdk.h" | |
#pragma pack(pop) | |
// Make sure the contents of the /lib/x86_64 is placed next to 4ed.exe | |
#pragma comment (lib, "path/to/discord_game_sdk.dll.lib") | |
//#define DC_CLIENT_ID <application_id> | |
#ifndef DC_CLIENT_ID | |
#error "Create a New Discord App at https://discord.com/developers/applications/ then define DC_CLIENT_ID to the Application ID" | |
#endif | |
function void DC_ActivityUpdate_CB(void *data, EDiscordResult result){ ; } | |
struct DC_Variables{ | |
IDiscordCore *core; | |
IDiscordUserManager *users; | |
IDiscordActivityManager *activities; | |
IDiscordApplicationManager *application; | |
IDiscordCoreEvents events; | |
IDiscordActivityEvents activity_events; | |
IDiscordUserEvents user_events; | |
DiscordUserId user_id; | |
Buffer_ID buffer; | |
i64 start_timestamp; | |
b32 is_valid; | |
}; | |
global DC_Variables discord; | |
#define StringCopy(d,s) block_copy(d, s.str, s.size) | |
/// Call in HookID_Tick | |
function void DC_Tick(Application_Links *app){ | |
if(!discord.is_valid){ return; } | |
View_ID view = get_active_view(app, Access_ReadVisible); | |
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible); | |
Scratch_Block scratch(app); | |
String_Const_u8 file_name = push_buffer_unique_name(app, scratch, buffer); | |
String_Const_u8 details_string = push_stringf(scratch, "Editing: %.*s", string_expand(file_name)); | |
/// Rich Presence -> Art Assets -> Add Image called "4ed_logo" | |
DiscordActivity activity = {}; | |
DiscordActivityAssets assets = {}; | |
StringCopy(assets.large_image, string_u8_litexpr("4ed_logo")); | |
StringCopy(assets.large_text, string_u8_litexpr("4ed")); | |
activity.type = DiscordActivityType_Playing; | |
activity.application_id = DC_CLIENT_ID; | |
activity.assets = assets; | |
activity.timestamps = { discord.start_timestamp }; | |
StringCopy(activity.name, string_u8_litexpr("4coder")); | |
Variable_Handle prj_var = vars_read_key(vars_get_root(), vars_save_string_lit("prj_config")); | |
Variable_Handle proj_name_var = vars_read_key(prj_var, vars_save_string_lit("project_name")); | |
String_ID proj_name_id = vars_string_id_from_var(proj_name_var); | |
if(proj_name_id != 0){ | |
String8 proj_name = vars_read_string(scratch, proj_name_id); | |
StringCopy(activity.details, proj_name); | |
} | |
StringCopy(activity.state, details_string); | |
discord.activities->update_activity(discord.activities, &activity, &discord, DC_ActivityUpdate_CB); | |
animate_in_n_milliseconds(app, Thousand(1)); | |
discord.core->run_callbacks(discord.core); | |
} | |
// Call in CoreCode_Startup | |
function void DC_Init(){ | |
DiscordCreateParams params = {}; | |
DiscordCreateParamsSetDefault(¶ms); | |
params.client_id = DC_CLIENT_ID; | |
params.flags = DiscordCreateFlags_NoRequireDiscord; | |
params.event_data = &discord; | |
params.events = &discord.events; | |
params.activity_events = &discord.activity_events; | |
params.user_events = &discord.user_events; | |
if(DiscordCreate(DISCORD_VERSION, ¶ms, &discord.core) == DiscordResult_Ok){ | |
discord.is_valid = true; | |
discord.users = discord.core->get_user_manager(discord.core); | |
discord.activities = discord.core->get_activity_manager(discord.core); | |
discord.start_timestamp = time(NULL); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "4coder_default_include.cpp" | |
#include "4coder_discord.hpp" | |
#if !defined(META_PASS) | |
#include "generated/managed_id_metadata.cpp" | |
#endif | |
function void | |
STUB_tick(Application_Links *app, Frame_Info frame_info){ | |
default_tick(app, frame_info); | |
DC_Tick(app); | |
} | |
CUSTOM_COMMAND_SIG(STUB_startup){ | |
default_startup(app); | |
DC_Init(); | |
} | |
function void | |
STUB_essential_mapping(Mapping *mapping, i64 global_id){ | |
MappingScope(); | |
SelectMapping(mapping); | |
SelectMap(global_id); | |
BindCore(STUB_startup, CoreCode_Startup); | |
} | |
void | |
custom_layer_init(Application_Links *app){ | |
default_framework_init(app); | |
set_all_default_hooks(app); | |
set_custom_hook(app, HookID_Tick, STUB_tick); | |
Thread_Context *tctx = get_thread_context(app); | |
mapping_init(tctx, &framework_mapping); | |
String_ID global_map_id = vars_save_string_lit("keys_global"); | |
String_ID file_map_id = vars_save_string_lit("keys_file"); | |
String_ID code_map_id = vars_save_string_lit("keys_code"); | |
setup_default_mapping(&framework_mapping, global_map_id, file_map_id, code_map_id); | |
setup_essential_mapping(&framework_mapping, global_map_id, file_map_id, code_map_id); | |
STUB_essential_mapping(&framework_mapping, global_map_id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment