rdiscord_sdk 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +13 -0
  3. data/.vscode/c_cpp_properties.json +24 -0
  4. data/.vscode/settings.json +102 -0
  5. data/Gemfile +11 -0
  6. data/Gemfile.lock +46 -0
  7. data/INTERNALS.md +0 -0
  8. data/LICENSE +674 -0
  9. data/README.md +187 -0
  10. data/Rakefile +17 -0
  11. data/bin/console +15 -0
  12. data/bin/setup +8 -0
  13. data/ext/rdiscord_sdk/activity.cpp +297 -0
  14. data/ext/rdiscord_sdk/activity.h +17 -0
  15. data/ext/rdiscord_sdk/common.cpp +65 -0
  16. data/ext/rdiscord_sdk/common.h +88 -0
  17. data/ext/rdiscord_sdk/extconf.rb +54 -0
  18. data/ext/rdiscord_sdk/gem_activity_manager.cpp +273 -0
  19. data/ext/rdiscord_sdk/gem_activity_manager.h +8 -0
  20. data/ext/rdiscord_sdk/gem_user_manager.cpp +114 -0
  21. data/ext/rdiscord_sdk/gem_user_manager.h +8 -0
  22. data/ext/rdiscord_sdk/rdiscord_sdk.cpp +77 -0
  23. data/ext/rdiscord_sdk/rdiscord_sdk.h +17 -0
  24. data/ext/rdiscord_sdk/user.cpp +98 -0
  25. data/ext/rdiscord_sdk/user.h +19 -0
  26. data/lib/rdiscord_sdk/enums.rb +162 -0
  27. data/lib/rdiscord_sdk/rdiscord_sdk.so +0 -0
  28. data/lib/rdiscord_sdk/version.rb +16 -0
  29. data/lib/rdiscord_sdk.rb +3 -0
  30. data/rdiscord_sdk.gemspec +34 -0
  31. data/third-party/include/achievement_manager.cpp +98 -0
  32. data/third-party/include/achievement_manager.h +34 -0
  33. data/third-party/include/activity_manager.cpp +177 -0
  34. data/third-party/include/activity_manager.h +42 -0
  35. data/third-party/include/application_manager.cpp +78 -0
  36. data/third-party/include/application_manager.h +30 -0
  37. data/third-party/include/core.cpp +182 -0
  38. data/third-party/include/core.h +64 -0
  39. data/third-party/include/discord.h +16 -0
  40. data/third-party/include/event.h +59 -0
  41. data/third-party/include/ffi.h +942 -0
  42. data/third-party/include/image_manager.cpp +57 -0
  43. data/third-party/include/image_manager.h +28 -0
  44. data/third-party/include/lobby_manager.cpp +547 -0
  45. data/third-party/include/lobby_manager.h +88 -0
  46. data/third-party/include/network_manager.cpp +103 -0
  47. data/third-party/include/network_manager.h +63 -0
  48. data/third-party/include/overlay_manager.cpp +112 -0
  49. data/third-party/include/overlay_manager.h +33 -0
  50. data/third-party/include/relationship_manager.cpp +90 -0
  51. data/third-party/include/relationship_manager.h +32 -0
  52. data/third-party/include/storage_manager.cpp +158 -0
  53. data/third-party/include/storage_manager.h +46 -0
  54. data/third-party/include/store_manager.cpp +160 -0
  55. data/third-party/include/store_manager.h +38 -0
  56. data/third-party/include/types.cpp +769 -0
  57. data/third-party/include/types.h +492 -0
  58. data/third-party/include/user_manager.cpp +80 -0
  59. data/third-party/include/user_manager.h +31 -0
  60. data/third-party/include/voice_manager.cpp +124 -0
  61. data/third-party/include/voice_manager.h +37 -0
  62. data/third-party/lib/x86/discord_game_sdk.dll +0 -0
  63. data/third-party/lib/x86/discord_game_sdk.dll.lib +0 -0
  64. data/third-party/lib/x86_64/discord_game_sdk.bundle +0 -0
  65. data/third-party/lib/x86_64/discord_game_sdk.dll +0 -0
  66. data/third-party/lib/x86_64/discord_game_sdk.dll.lib +0 -0
  67. data/third-party/lib/x86_64/discord_game_sdk.dylib +0 -0
  68. data/third-party/lib/x86_64/discord_game_sdk.so +0 -0
  69. metadata +114 -0
@@ -0,0 +1,54 @@
1
+ require "mkmf-rice"
2
+
3
+ # Add compiler and linker flags
4
+ def add_flags(type, flags)
5
+ case type
6
+ when :c
7
+ $CFLAGS << " #{flags} "
8
+ when :ld
9
+ $LDFLAGS << " #{flags} "
10
+ end
11
+ end
12
+
13
+ THIRDPARTY_DIR = File.join(File.dirname(__FILE__), "../../third-party")
14
+ puts "THIRDPARTY_DIR: #{THIRDPARTY_DIR}"
15
+ HEADER_DIR = File.join(THIRDPARTY_DIR, "include")
16
+ LIBRARY_DIR = if RUBY_PLATFORM.start_with?("x86_64") or RUBY_PLATFORM.start_with?("x64")
17
+ File.join(THIRDPARTY_DIR, "lib/x86_64")
18
+ else
19
+ puts "Blindly assuming x86. If you get linker errors, this is why."
20
+ File.join(THIRDPARTY_DIR, "lib/x86")
21
+ end
22
+
23
+ add_flags(:ld, "-L#{LIBRARY_DIR}")
24
+ add_flags(:ld, "-Wl,-R. -Wl,-R./lib")
25
+ add_flags(:ld, "-l:discord_game_sdk#{RUBY_PLATFORM.end_with?("linux") ? ".so" : ".dll.lib"}")
26
+ add_flags(:c, "-g")
27
+ $LDFLAGS.gsub!(/\n/, " ")
28
+
29
+ # Grab all discord source files because ruby is dumb
30
+
31
+ discord_srcs = Dir.glob("#{THIRDPARTY_DIR}/**/*.cpp").map { |path| File.basename(path) }
32
+ Dir.glob("#{THIRDPARTY_DIR}/*/") do |path|
33
+ puts path
34
+ dir = File.basename(path)
35
+ $VPATH << "#{THIRDPARTY_DIR}/#{dir}"
36
+ $INCFLAGS << " -I#{THIRDPARTY_DIR}/#{dir}"
37
+ end
38
+ ext_srcs = Dir.glob("#{$srcdir}/**/*.cpp").map { |path| File.basename(path) }
39
+ Dir.glob("#{$srcdir}/*/") do |path|
40
+ dir = File.basename(path)
41
+ next if excluded.include?(dir)
42
+ $VPATH << "$(srcdir)/#{dir}"
43
+ $INCFLAGS << " -I$(srcdir)/#{dir}"
44
+ end
45
+
46
+ $srcs = discord_srcs + ext_srcs
47
+
48
+ puts "LDFLAGS: #{$LDFLAGS}"
49
+ puts "CFLAGS: #{$CFLAGS}"
50
+ puts "VPATH: #{$VPATH}"
51
+ puts "INCFLAGS: #{$INCFLAGS}"
52
+ puts "SRCS: #{$srcs}"
53
+
54
+ create_makefile("rdiscord_sdk")
@@ -0,0 +1,273 @@
1
+ #include "gem_activity_manager.h"
2
+ #include "common.h"
3
+ #include "activity.h"
4
+ #include "user.h"
5
+ #include "discord.h"
6
+
7
+ using namespace Rice;
8
+
9
+ Module rb_mActivityManager;
10
+
11
+ Object rb_activity_manager_register_command(const char* command) {
12
+ CHECK_CORE_INITIALIZED;
13
+ return rb_result_to_obj(
14
+ DiscordSDK.core->ActivityManager().RegisterCommand(command)
15
+ );
16
+ }
17
+
18
+ Object rb_activity_manager_register_steam(int steam_id) {
19
+ CHECK_CORE_INITIALIZED;
20
+ return rb_result_to_obj(
21
+ DiscordSDK.core->ActivityManager().RegisterSteam(steam_id)
22
+ );
23
+ }
24
+
25
+ Object rb_activity_manager_update_activity(Object rb_activity) {
26
+ CHECK_CORE_INITIALIZED;
27
+ VALUE callback_proc = rb_common_get_callback_proc(1);
28
+
29
+ auto fn = rb_discord_callback_wrapper_basic(callback_proc);
30
+
31
+ GET_ACTIVITY(rb_activity);
32
+ DiscordSDK.core->ActivityManager().UpdateActivity(
33
+ *activity,
34
+ fn
35
+ );
36
+
37
+ return Qnil;
38
+ }
39
+
40
+ Object rb_activity_manager_clear_activity() {
41
+ CHECK_CORE_INITIALIZED;
42
+ VALUE callback_proc = rb_common_get_callback_proc(1);
43
+
44
+ auto fn = rb_discord_callback_wrapper_basic(callback_proc);
45
+ DiscordSDK.core->ActivityManager().ClearActivity(fn);
46
+
47
+ return Qnil;
48
+ }
49
+
50
+ Object rb_activity_manager_send_request_reply(std::int64_t user_id, int reply) {
51
+ CHECK_CORE_INITIALIZED;
52
+ VALUE callback_proc = rb_common_get_callback_proc(1);
53
+
54
+ auto fn = rb_discord_callback_wrapper_basic(callback_proc);
55
+
56
+ DiscordSDK.core->ActivityManager().SendRequestReply(
57
+ user_id,
58
+ (discord::ActivityJoinRequestReply) reply,
59
+ fn
60
+ );
61
+
62
+ return Qnil;
63
+ }
64
+
65
+ Object rb_activity_manager_send_invite(std::int64_t user_id, int type, const char* content) {
66
+ CHECK_CORE_INITIALIZED;
67
+ VALUE callback_proc = rb_common_get_callback_proc(1);
68
+
69
+ auto fn = rb_discord_callback_wrapper_basic(callback_proc);
70
+
71
+ DiscordSDK.core->ActivityManager().SendInvite(
72
+ user_id,
73
+ (discord::ActivityActionType) type,
74
+ content,
75
+ fn
76
+ );
77
+
78
+ return Qnil;
79
+ }
80
+
81
+ Object rb_activity_manager_accept_invite(std::int64_t user_id) {
82
+ CHECK_CORE_INITIALIZED;
83
+ VALUE callback_proc = rb_common_get_callback_proc(1);
84
+
85
+ auto fn = rb_discord_callback_wrapper_basic(callback_proc);
86
+
87
+ DiscordSDK.core->ActivityManager().AcceptInvite(
88
+ user_id,
89
+ fn
90
+ );
91
+
92
+ return Qnil;
93
+ }
94
+
95
+ int rb_activity_manager_on_activity_join_connect() {
96
+ CHECK_CORE_INITIALIZED;
97
+
98
+ VALUE event_proc = rb_common_get_event_proc(1);
99
+ int token = DiscordSDK.core->ActivityManager().OnActivityJoin.Connect(
100
+ [event_proc](const char* secret) {
101
+ if (event_proc == Qnil)
102
+ return;
103
+
104
+ if (!rb_obj_is_kind_of(event_proc, rb_cProc))
105
+ rb_raise(rb_eTypeError, "Proc expected in callback, got %d instead", rb_obj_classname(event_proc));
106
+
107
+ VALUE array = rb_ary_new_from_args(2, event_proc, rb_str_new_cstr(secret));
108
+ int state;
109
+ rb_protect(rb_discord_call_proc, array, &state);
110
+
111
+ LOG_ERROR_IF_STATE;
112
+ }
113
+ );
114
+ rb_common_add_proc_to_hash(Symbol("activity_join_connect" + token), event_proc);
115
+
116
+ return token;
117
+ }
118
+
119
+ Object rb_activity_manager_on_activity_join_disconnect(int token) {
120
+ CHECK_CORE_INITIALIZED;
121
+
122
+ DiscordSDK.core->ActivityManager().OnActivityJoin.Disconnect(token);
123
+ rb_hash_delete(rb_oProcEvents, Symbol("on_activity_join" + token)); // Delete event proc, we use hashes instead of arrays to delete specific entries
124
+
125
+ return Qnil;
126
+ }
127
+
128
+ int rb_activity_manager_on_activity_spectate_connect() {
129
+ CHECK_CORE_INITIALIZED;
130
+
131
+ VALUE event_proc = rb_common_get_event_proc(1);
132
+ int token = DiscordSDK.core->ActivityManager().OnActivitySpectate.Connect(
133
+ [event_proc](const char* secret) {
134
+ if (event_proc == Qnil)
135
+ return;
136
+
137
+ if (!rb_obj_is_kind_of(event_proc, rb_cProc))
138
+ rb_raise(rb_eTypeError, "Proc expected in callback, got %d instead", rb_obj_classname(event_proc));
139
+
140
+ VALUE array = rb_ary_new_from_args(2, event_proc, rb_str_new_cstr(secret));
141
+ int state;
142
+ rb_protect(rb_discord_call_proc, array, &state);
143
+
144
+ LOG_ERROR_IF_STATE;
145
+ }
146
+ );
147
+ rb_common_add_proc_to_hash(Symbol("on_activity_spectate" + token), event_proc);
148
+
149
+ return token;
150
+ }
151
+
152
+ Object rb_activity_manager_on_activity_spectate_disconnect(int token) {
153
+ CHECK_CORE_INITIALIZED;
154
+
155
+ DiscordSDK.core->ActivityManager().OnActivitySpectate.Disconnect(token);
156
+ rb_hash_delete(rb_oProcEvents, Symbol("on_activity_spectate" + token));
157
+
158
+ return Qnil;
159
+ }
160
+
161
+ int rb_activity_manager_on_activity_join_request_connect() {
162
+ CHECK_CORE_INITIALIZED;
163
+
164
+ VALUE event_proc = rb_common_get_event_proc(1);
165
+ int token = DiscordSDK.core->ActivityManager().OnActivityJoinRequest.Connect(
166
+ [event_proc](const discord::User &user) {
167
+ if (event_proc == Qnil)
168
+ return;
169
+
170
+ if (!rb_obj_is_kind_of(event_proc, rb_cProc))
171
+ rb_raise(rb_eTypeError, "Proc expected in callback, got %d instead", rb_obj_classname(event_proc));
172
+
173
+ VALUE argv = rb_ary_new();
174
+ Object rb_user = rb_class_new_instance(0, &argv, rb_cUser);
175
+ setPrivateData(rb_user, (void*) &user);
176
+
177
+ VALUE array = rb_ary_new_from_args(2, event_proc, rb_user);
178
+ int state;
179
+ rb_protect(rb_discord_call_proc, array, &state);
180
+
181
+ LOG_ERROR_IF_STATE;
182
+ }
183
+ );
184
+ rb_common_add_proc_to_hash(Symbol("on_activity_join_request" + token), event_proc);
185
+
186
+ return token;
187
+ }
188
+
189
+ Object rb_activity_manager_on_activity_join_request_disconnect(int token) {
190
+ CHECK_CORE_INITIALIZED;
191
+
192
+ DiscordSDK.core->ActivityManager().OnActivityJoinRequest.Disconnect(token);
193
+ rb_hash_delete(rb_oProcEvents, Symbol("on_activity_join_request" + token));
194
+
195
+ return Qnil;
196
+ }
197
+
198
+ int rb_activity_manager_on_activity_invite_connect() {
199
+ CHECK_CORE_INITIALIZED;
200
+
201
+ VALUE event_proc = rb_common_get_event_proc(1);
202
+ int token = DiscordSDK.core->ActivityManager().OnActivityInvite.Connect(
203
+ [event_proc](discord::ActivityActionType action_type, const discord::User &user, const discord::Activity &activity) {
204
+ if (event_proc == Qnil)
205
+ return;
206
+
207
+ if (!rb_obj_is_kind_of(event_proc, rb_cProc))
208
+ rb_raise(rb_eTypeError, "Proc expected in callback, got %d instead", rb_obj_classname(event_proc));
209
+
210
+ VALUE argv = rb_ary_new();
211
+ Object rb_user = rb_class_new_instance(0, &argv, rb_cUser);
212
+ setPrivateData(rb_user, (void*) &user);
213
+
214
+ VALUE argv2 = rb_ary_new();
215
+ Object rb_activity = rb_class_new_instance(0, &argv2, rb_cActivity);
216
+ setPrivateData(rb_activity, (void*) &activity);
217
+
218
+ VALUE array = rb_ary_new_from_args(4, event_proc, rb_user, rb_activity, INT2NUM((int) action_type));
219
+ int state;
220
+ rb_protect(rb_discord_call_proc, array, &state);
221
+
222
+ LOG_ERROR_IF_STATE;
223
+ }
224
+ );
225
+ rb_common_add_proc_to_hash(Symbol("on_activity_invite" + token), event_proc);
226
+
227
+ return token;
228
+ }
229
+
230
+ Object rb_activity_manager_on_activity_invite_disconnect(int token) {
231
+ CHECK_CORE_INITIALIZED;
232
+
233
+ DiscordSDK.core->ActivityManager().OnActivityInvite.Disconnect(token);
234
+ rb_hash_delete(rb_oProcEvents, Symbol("on_activity_invite" + token));
235
+
236
+ return Qnil;
237
+ }
238
+
239
+ void rb_activity_init_manager(Module module) {
240
+ rb_mActivityManager = define_module_under(module, "ActivityManager");
241
+ rb_mActivityManager.define_module_function(
242
+ "register_command",
243
+ &rb_activity_manager_register_command
244
+ );
245
+ rb_mActivityManager.define_module_function(
246
+ "register_steam",
247
+ &rb_activity_manager_register_steam
248
+ );
249
+ rb_mActivityManager.define_module_function(
250
+ "update_activity",
251
+ &rb_activity_manager_update_activity
252
+ );
253
+ rb_mActivityManager.define_module_function(
254
+ "clear_activity",
255
+ &rb_activity_manager_clear_activity
256
+ );
257
+ rb_mActivityManager.define_module_function(
258
+ "send_request_reply",
259
+ &rb_activity_manager_send_request_reply
260
+ );
261
+ rb_mActivityManager.define_module_function(
262
+ "send_invite",
263
+ &rb_activity_manager_send_invite
264
+ );
265
+ rb_mActivityManager.define_module_function(
266
+ "accept_invite",
267
+ &rb_activity_manager_accept_invite
268
+ );
269
+ DEF_METHOD_EVENT(rb_mActivityManager, activity_join, activity_manager);
270
+ DEF_METHOD_EVENT(rb_mActivityManager, activity_spectate, activity_manager);
271
+ DEF_METHOD_EVENT(rb_mActivityManager, activity_join_request, activity_manager);
272
+ DEF_METHOD_EVENT(rb_mActivityManager, activity_invite, activity_manager);
273
+ }
@@ -0,0 +1,8 @@
1
+ #pragma once
2
+
3
+ #include "rdiscord_sdk.h"
4
+
5
+ using namespace Rice;
6
+
7
+ extern Module rb_mActivityManager;
8
+ void rb_activity_init_manager(Module module);
@@ -0,0 +1,114 @@
1
+ #include "gem_user_manager.h"
2
+ #include "common.h"
3
+ #include "user.h"
4
+ #include "discord.h"
5
+
6
+ using namespace Rice;
7
+
8
+ Module rb_mUserManager;
9
+
10
+
11
+
12
+ // Thanks discord for not making this behave like a callback!!
13
+ Object rb_user_manager_get_current_user() {
14
+ CHECK_CORE_INITIALIZED;
15
+
16
+ discord::User* user = new discord::User();
17
+ discord::Result result = DiscordSDK.core->UserManager().GetCurrentUser(user);
18
+
19
+ VALUE argv = rb_ary_new();
20
+ VALUE rb_user = rb_class_new_instance(0, &argv, rb_cUser);
21
+ setPrivateData(rb_user, (void*) user);
22
+
23
+ return rb_ary_new_from_args(2, (int) result, rb_user);
24
+ }
25
+
26
+ Object rb_user_manager_get_user(std::int64_t id) {
27
+ CHECK_CORE_INITIALIZED;
28
+
29
+ VALUE callback_proc = rb_common_get_callback_proc(2);
30
+ DiscordSDK.core->UserManager().GetUser(id,
31
+ [callback_proc](discord::Result result, const discord::User &user){
32
+ if (callback_proc == Qnil)
33
+ return;
34
+
35
+ if (!rb_obj_is_kind_of(callback_proc, rb_cProc)) {
36
+ rb_raise(rb_eTypeError, "Proc expected in callback, got %d instead", rb_obj_classname(callback_proc));
37
+ }
38
+
39
+ VALUE argv = rb_ary_new();
40
+ VALUE rb_user = rb_class_new_instance(0, &argv, rb_cUser);
41
+ setPrivateData(rb_user, (void*) &user);
42
+
43
+ rb_ary_delete(rb_oPendingCallbacks, callback_proc);
44
+ VALUE ary = rb_ary_new_from_args(3, callback_proc, INT2NUM((int) result), rb_user);
45
+
46
+ int state;
47
+ rb_protect(rb_discord_call_proc, ary, &state);
48
+ LOG_ERROR_IF_STATE;
49
+ }
50
+ );
51
+
52
+ return Qnil;
53
+ }
54
+
55
+ Object rb_user_manager_get_current_user_premium_type() {
56
+ CHECK_CORE_INITIALIZED;
57
+
58
+ discord::PremiumType premium_type;
59
+ discord::Result result = DiscordSDK.core->UserManager().GetCurrentUserPremiumType(&premium_type);
60
+
61
+ return rb_ary_new_from_args(2, INT2NUM((int) result), INT2NUM((int) premium_type));
62
+ }
63
+
64
+ Object rb_user_manager_current_user_has_flag(int flag) {
65
+ CHECK_CORE_INITIALIZED;
66
+
67
+ bool has_flag;
68
+ discord::Result result = DiscordSDK.core->UserManager().CurrentUserHasFlag((discord::UserFlag) flag, &has_flag);
69
+
70
+ return rb_ary_new_from_args(2, INT2NUM((int) result), BOOL2RB(has_flag));
71
+ }
72
+
73
+ int rb_user_manager_on_current_user_update_connect() {
74
+ CHECK_CORE_INITIALIZED;
75
+
76
+ VALUE event_proc = rb_common_get_event_proc(0);
77
+ int token = DiscordSDK.core->UserManager().OnCurrentUserUpdate.Connect(
78
+ [event_proc]() {
79
+ if (event_proc == Qnil)
80
+ return;
81
+
82
+ if (!rb_obj_is_kind_of(event_proc, rb_cProc))
83
+ rb_raise(rb_eTypeError, "Proc expected in callback, got %d instead", rb_obj_classname(event_proc));
84
+
85
+
86
+ VALUE array = rb_ary_new_from_args(1, event_proc);
87
+ int state;
88
+ rb_protect(rb_discord_call_proc, array, &state);
89
+
90
+ LOG_ERROR_IF_STATE;
91
+ }
92
+ );
93
+ rb_common_add_proc_to_hash(Symbol("on_current_user_update" + token), event_proc);
94
+
95
+ return token;
96
+ }
97
+
98
+ Object rb_user_manager_on_current_user_update_disconnect(int token) {
99
+ CHECK_CORE_INITIALIZED;
100
+
101
+ DiscordSDK.core->UserManager().OnCurrentUserUpdate.Disconnect(token);
102
+ rb_hash_delete(rb_oPendingCallbacks, Symbol("on_current_user_update" + token));
103
+
104
+ return Qnil;
105
+ }
106
+
107
+ void rb_user_init_manager(Module module) {
108
+ rb_mUserManager = define_module_under(module, "UserManager");
109
+ rb_mUserManager.define_module_function("get_current_user", &rb_user_manager_get_current_user);
110
+ rb_mUserManager.define_module_function("get_user", &rb_user_manager_get_user);
111
+ rb_mUserManager.define_module_function("get_current_user_premium_type", &rb_user_manager_get_current_user_premium_type);
112
+ rb_mUserManager.define_module_function("current_user_has_flag", &rb_user_manager_current_user_has_flag);
113
+ DEF_METHOD_EVENT(rb_mUserManager, current_user_update, user_manager);
114
+ }
@@ -0,0 +1,8 @@
1
+ #pragma once
2
+
3
+ #include "rdiscord_sdk.h"
4
+
5
+ using namespace Rice;
6
+
7
+ extern Module rb_mUserManager;
8
+ void rb_user_init_manager(Module module);
@@ -0,0 +1,77 @@
1
+ #include "rdiscord_sdk.h"
2
+ #include "common.h"
3
+ #include "gem_activity_manager.h"
4
+ #include "activity.h"
5
+ #include "user.h"
6
+ #include "gem_user_manager.h"
7
+
8
+ using namespace Rice;
9
+
10
+ VALUE rb_oPendingCallbacks;
11
+ VALUE rb_oProcEvents;
12
+
13
+ Module rb_mDiscord;
14
+ DiscordStruct DiscordSDK;
15
+
16
+ Object rb_core_initialize(long long client_id, long flags) {
17
+ if (DiscordSDK.core) {
18
+ rb_raise(rb_eRuntimeError, "Discord already initialized!");
19
+ }
20
+ discord::Core* core;
21
+ auto result = discord::Core::Create(client_id, flags, &core);
22
+ DiscordSDK.core.reset(core);
23
+ return rb_result_to_obj(result);
24
+ }
25
+
26
+ void rb_core_log_hook(discord::LogLevel minLevel, const char* message) {
27
+ if (!DiscordSDK.log_hook) {
28
+ return;
29
+ }
30
+ VALUE args = rb_ary_new_from_args(2, minLevel, message);
31
+ rb_proc_call(DiscordSDK.log_hook, args);
32
+ }
33
+
34
+ Object rb_core_set_log_hook(int loglevel) {
35
+ CHECK_CORE_INITIALIZED;
36
+ if (DiscordSDK.log_hook) {
37
+ rb_hash_delete(rb_oProcEvents, Symbol("log_hook"));
38
+ }
39
+
40
+ DiscordSDK.log_hook = rb_common_get_event_proc(2);
41
+ rb_common_add_proc_to_hash(Symbol("log_hook"), DiscordSDK.log_hook);
42
+
43
+ DiscordSDK.core->SetLogHook(
44
+ (discord::LogLevel) loglevel,
45
+ rb_core_log_hook
46
+ );
47
+
48
+ return Qnil;
49
+ }
50
+
51
+ Object rb_core_run_callbacks() {
52
+ CHECK_CORE_INITIALIZED;
53
+ return rb_result_to_obj(DiscordSDK.core->RunCallbacks());
54
+ }
55
+
56
+ extern "C"
57
+ void Init_rdiscord_sdk()
58
+ {
59
+ rb_oPendingCallbacks = rb_ary_new();
60
+ rb_oProcEvents = rb_hash_new();
61
+
62
+ rb_mDiscord = define_module("Discord");
63
+ rb_mDiscord.define_module_function("init", &rb_core_initialize);
64
+ rb_mDiscord.define_module_function("set_log_hook", &rb_core_set_log_hook);
65
+ rb_mDiscord.define_module_function("run_callbacks", &rb_core_run_callbacks);
66
+
67
+ rb_global_variable(&rb_oProcEvents);
68
+ rb_global_variable(&rb_oPendingCallbacks);
69
+
70
+ rb_gv_set("$discord_proc_events", rb_oProcEvents);
71
+ rb_gv_set("$discord_pending_callbacks", rb_oPendingCallbacks);
72
+
73
+ rb_activity_init_manager(rb_mDiscord);
74
+ rb_activity_init_class(rb_mDiscord);
75
+ rb_user_init_class(rb_mDiscord);
76
+ rb_user_init_manager(rb_mDiscord);
77
+ }
@@ -0,0 +1,17 @@
1
+ #pragma once
2
+
3
+ #include <rice/rice.hpp>
4
+ #include <discord.h>
5
+
6
+ using namespace Rice;
7
+
8
+ extern Module rb_mDiscord;
9
+
10
+ struct DiscordStruct {
11
+ std::unique_ptr<discord::Core> core;
12
+ Object log_hook;
13
+ };
14
+ extern DiscordStruct DiscordSDK;
15
+
16
+ extern "C"
17
+ void Init_rdiscord_sdk();
@@ -0,0 +1,98 @@
1
+ #include "user.h"
2
+ #include <rice/rice.hpp>
3
+ #include "discord.h"
4
+ #include "common.h"
5
+ #include <cstdint>
6
+ using namespace Rice;
7
+
8
+ Class rb_cUser;
9
+
10
+ DEF_TYPE_CUSTOMTYPENAME(discord::User, User_rbType);
11
+
12
+ Object rb_user_initialize(Object self) {
13
+ discord::User* user = new discord::User();
14
+ setPrivateData(self, user);
15
+
16
+ return self;
17
+ }
18
+
19
+ std::int64_t rb_user_get_id(Object self) {
20
+ GET_USER(self);
21
+
22
+ return user->GetId();
23
+ }
24
+
25
+ Object rb_user_set_id(Object self, std::int64_t id) {
26
+ GET_USER(self);
27
+
28
+ user->SetId(id);
29
+
30
+ return Qnil;
31
+ }
32
+
33
+ const char* rb_user_get_username(Object self) {
34
+ GET_USER(self);
35
+
36
+ return user->GetUsername();
37
+ }
38
+
39
+ Object rb_user_set_username(Object self, const char* username) {
40
+ GET_USER(self);
41
+
42
+ user->SetUsername(username);
43
+
44
+ return Qnil;
45
+ }
46
+
47
+ const char* rb_user_get_discriminator(Object self) {
48
+ GET_USER(self);
49
+
50
+ return user->GetDiscriminator();
51
+ }
52
+
53
+ Object rb_user_set_discriminator(Object self, const char* discriminator) {
54
+ GET_USER(self);
55
+
56
+ user->SetDiscriminator(discriminator);
57
+
58
+ return Qnil;
59
+ }
60
+
61
+ const char* rb_user_get_avatar(Object self) {
62
+ GET_USER(self);
63
+
64
+ return user->GetAvatar();
65
+ }
66
+
67
+ Object rb_user_set_avatar(Object self, const char* avatar) {
68
+ GET_USER(self);
69
+
70
+ user->SetAvatar(avatar);
71
+
72
+ return Qnil;
73
+ }
74
+
75
+ bool rb_user_get_bot(Object self) {
76
+ GET_USER(self);
77
+
78
+ return user->GetBot();
79
+ }
80
+
81
+ Object rb_user_set_bot(Object self, bool bot) {
82
+ GET_USER(self);
83
+
84
+ user->SetBot(bot);
85
+
86
+ return Qnil;
87
+ }
88
+
89
+ void rb_user_init_class(Module module) {
90
+ rb_cUser = define_class_under(module, "User");
91
+ rb_define_alloc_func(rb_cUser, classAllocate<&User_rbType>);
92
+ rb_cUser.define_method("initialize", &rb_user_initialize);
93
+ DEF_METHOD_GET_SET(rb_cUser, id, user);
94
+ DEF_METHOD_GET_SET(rb_cUser, username, user);
95
+ DEF_METHOD_GET_SET(rb_cUser, discriminator, user);
96
+ DEF_METHOD_GET_SET(rb_cUser, avatar, user);
97
+ DEF_METHOD_GET_SET(rb_cUser, bot, user);
98
+ }
@@ -0,0 +1,19 @@
1
+ #pragma once
2
+
3
+ #include <rice/rice.hpp>
4
+ #include "rdiscord_sdk.h"
5
+ #include "discord.h"
6
+ #include "common.h"
7
+
8
+ using namespace Rice;
9
+
10
+ extern Class rb_cUser;
11
+ void rb_user_init_class(Module module);
12
+
13
+ Object rb_user_initialize(Object self);
14
+
15
+ #define GET_USER(obj) \
16
+ if (!obj.is_a(rb_cUser)) { \
17
+ rb_raise(rb_eTypeError, "User is not a Discord::User"); \
18
+ } \
19
+ discord::User* user = getPrivateData<discord::User>(obj);