Skip to content

Commit b3a4890

Browse files
committed
feat: add proper android support for discord social sdk
1 parent d5d13b0 commit b3a4890

File tree

6 files changed

+275
-88
lines changed

6 files changed

+275
-88
lines changed

platforms/android/app/build.gradle

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,19 @@ String getVersion() {
159159

160160
if (versionResult.getV3() != 0) {
161161
throw new Exception(
162-
'An error occured while trying to detect the version: process exited with exit code: ' +
162+
'An error occurred while trying to detect the version: process exited with exit code: ' +
163163
versionResult.getV3() +
164164
' and stderr:\n' +
165165
versionResult.getV2().join("\n")
166166
);
167167
}
168168

169169
if (versionResult.getV2() != null && !versionResult.getV2().isEmpty()) {
170-
throw new Exception('An error occured while trying to detect the version (code 1): ' + versionResult.getV2().join("\n"));
170+
throw new Exception('An error occurred while trying to detect the version (code 1): ' + versionResult.getV2().join("\n"));
171171
}
172172

173173
if (versionResult.getV1() == null || versionResult.getV1().size() != 1) {
174-
throw new Exception('An error occured while trying to detect the version (code 2): ' + versionResult.getV1().join("\n"));
174+
throw new Exception('An error occurred while trying to detect the version (code 2): ' + versionResult.getV1().join("\n"));
175175
}
176176

177177
String version = versionResult.getV1()[0];
@@ -182,7 +182,7 @@ String getVersion() {
182182

183183
for (version1 in versions) {
184184
if (version != version1) {
185-
throw new Exception("Recievd two versions, that didn't match: " + version1 + " != " + version);
185+
throw new Exception("Received two versions, that didn't match: " + version1 + " != " + version);
186186
}
187187
}
188188

@@ -192,7 +192,7 @@ String getVersion() {
192192

193193

194194
if (versions.size() == 0) {
195-
throw new Exception("ERROR: at least one version has to be detected (at least one abi has to be returend by 'getAndroidABIs()')");
195+
throw new Exception("ERROR: at least one version has to be detected (at least one abi has to be returned by 'getAndroidABIs()')");
196196
}
197197

198198
// we guarantee, that every string in there is the same, and that it has at least one entry
@@ -224,7 +224,81 @@ Boolean shouldBuildUniversalApk(List<String> abisToUse) {
224224

225225
return true;
226226

227+
}
228+
229+
230+
/**
231+
* get the meson build type
232+
* @return String
233+
*/
234+
String getMesonBuildType() {
235+
List<String> abis = getAndroidABIs();
236+
List<String> buildtypes = new ArrayList<String>();
237+
238+
for (abi in abis) {
239+
File abiDir = getABIDir(abi);
240+
241+
Tuple3<List<String>, List<String>, Integer> buildtypeResult = executePipeline(Arrays.asList(
242+
Arrays.asList("meson", "introspect", "--buildoptions", abiDir.getAbsolutePath()),
243+
Arrays.asList("jq", "-r", ".[] | select(.name == \"buildtype\") | .value")
244+
));
245+
246+
if (buildtypeResult.getV3() != 0) {
247+
throw new Exception(
248+
'An error occurred while trying to detect the buildtype: process exited with exit code: ' +
249+
buildtypeResult.getV3() +
250+
' and stderr:\n' +
251+
buildtypeResult.getV2().join("\n")
252+
);
253+
}
254+
255+
if (buildtypeResult.getV2() != null && !buildtypeResult.getV2().isEmpty()) {
256+
throw new Exception('An error occurred while trying to detect the buildtype (code 1): ' + buildtypeResult.getV2().join("\n"));
257+
}
258+
259+
if (buildtypeResult.getV1() == null || buildtypeResult.getV1().size() != 1) {
260+
throw new Exception('An error occurred while trying to detect the buildtype (code 2): ' + buildtypeResult.getV1().join("\n"));
261+
}
262+
263+
String buildtype = buildtypeResult.getV1()[0];
227264

265+
if (buildtype == "") {
266+
throw new Exception("Auto detection of buildtype returned invalid buildtype: '" + version + "'");
267+
}
268+
269+
for (buildtype1 in buildtypes) {
270+
if (buildtype != buildtype1) {
271+
throw new Exception("Received two buildtypes, that didn't match: " + buildtype1 + " != " + buildtype);
272+
}
273+
}
274+
275+
buildtypes.add(buildtype);
276+
277+
}
278+
279+
280+
if (buildtypes.size() == 0) {
281+
throw new Exception("ERROR: at least one buildtype has to be detected (at least one abi has to be returned by 'getAndroidABIs()')");
282+
}
283+
284+
// we guarantee, that every string in there is the same, and that it has at least one entry
285+
return buildtypes[0];
286+
287+
}
288+
289+
/**
290+
* Returns the aar file for discord
291+
* @return File
292+
*/
293+
File getDiscordLib() {
294+
String target = getMesonBuildType() == 'release' ? 'release' : 'debug'
295+
String path = "../../../../subprojects/discord_social_sdk/lib/" + target + "/discord_partner_sdk.aar";
296+
File file = project.file(project.getLayout().getBuildDirectory().file(path));
297+
if(!file.exists()){
298+
throw new Exception("ERROR: discord aar is not present at the location we expected it to be!")
299+
}
300+
301+
return file;
228302
}
229303

230304

@@ -315,4 +389,5 @@ android {
315389

316390
dependencies {
317391
implementation fileTree(include: ['*.jar'], dir: 'libs')
392+
implementation files(getDiscordLib())
318393
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.github.oopetris;
22

33
import org.libsdl.app.SDLActivity;
4+
import com.discord.socialsdk.DiscordSocialSdkInit;
5+
import android.os.Bundle;
46

5-
/**
6-
* A sample wrapper class that just calls SDLActivity
7-
*/
7+
public class MainActivity extends SDLActivity {
88

9-
public class MainActivity extends SDLActivity { }
9+
// Setup
10+
@Override
11+
protected void onCreate(Bundle savedInstanceState) {
12+
super.onCreate(savedInstanceState);
13+
DiscordSocialSdkInit.setEngineActivity(this);
14+
}
15+
16+
}
1017

src/discord/core.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace constants::discord {
2727

2828
//TODO(Totto): this isn't correct for all platforms and needs to be tested
2929
#if defined(__ANDROID__)
30-
#error "Not supported"
30+
constexpr const char* platform_dependent_launch_arguments = "TODO";
3131
#elif defined(__CONSOLE__)
3232
#error "Not supported"
3333
#elif defined(FLATPAK_BUILD)
@@ -88,9 +88,9 @@ struct DiscordActivityWrapper {
8888
template<typename T>
8989
DiscordActivityWrapper& set_start_timestamp(const std::chrono::time_point<T>& point) {
9090

91-
const auto seconds_since_epoch =
92-
static_cast<u64>(std::chrono::duration_cast<std::chrono::milliseconds>(point.time_since_epoch()).count()
93-
);
91+
const auto seconds_since_epoch = static_cast<u64>(
92+
std::chrono::duration_cast<std::chrono::milliseconds>(point.time_since_epoch()).count()
93+
);
9494

9595
auto timestamps = this->get_timestamps();
9696

@@ -103,9 +103,9 @@ struct DiscordActivityWrapper {
103103
template<typename T>
104104
DiscordActivityWrapper& set_end_timestamp(const std::chrono::time_point<T>& point) {
105105

106-
const auto seconds_since_epoch =
107-
static_cast<u64>(std::chrono::duration_cast<std::chrono::milliseconds>(point.time_since_epoch()).count()
108-
);
106+
const auto seconds_since_epoch = static_cast<u64>(
107+
std::chrono::duration_cast<std::chrono::milliseconds>(point.time_since_epoch()).count()
108+
);
109109

110110
auto timestamps = this->get_timestamps();
111111

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
##note: this file "parses" and uses the prefab data from the discord social sdk arr, read more on the prefab concept here: https://developer.android.com/build/native-dependencies
2+
3+
# this isn't feature complete and doesn't care about actual defintion and config files, it uses a fixed static layout from the .aar file and uses it hard coded
4+
5+
aar_file = meson.project_source_root() / 'lib' / (get_option('buildtype') == 'release' ? 'release' : 'debug') / 'discord_partner_sdk.aar'
6+
7+
unzip = find_program('unzip')
8+
9+
unzip_base_dir = meson.current_source_dir()
10+
11+
destination_directory = unzip_base_dir / 'prefab' / 'discord'
12+
13+
message(destination_directory)
14+
15+
fs = import('fs')
16+
17+
if not fs.exists(destination_directory)
18+
19+
mkdir = find_program('mkdir')
20+
21+
run_command(
22+
mkdir,
23+
'-pv', destination_directory,
24+
check: true,
25+
)
26+
27+
run_command(
28+
unzip,
29+
aar_file,
30+
'-d', destination_directory,
31+
check: true,
32+
)
33+
34+
endif
35+
36+
base_prefab_dir = destination_directory / 'prefab' / 'modules' / 'discord_partner_sdk'
37+
38+
inc_dirs = include_directories(fs.relative_to(base_prefab_dir, unzip_base_dir) / 'include')
39+
40+
header_files = files(
41+
base_prefab_dir / 'include' / 'cdiscord.h',
42+
base_prefab_dir / 'include' / 'discordpp.h',
43+
)
44+
45+
install_headers(
46+
header_files,
47+
subdir: 'discord',
48+
)
49+
50+
arch_name = host_machine.cpu_family()
51+
52+
if host_machine.cpu_family() == 'arm'
53+
arch_name = 'armeabi-v7a'
54+
elif host_machine.cpu_family() == 'aarch64'
55+
arch_name = 'arm64-v8a'
56+
elif host_machine.cpu_family() == 'x86' or ost_machine.cpu_family() == 'x86_64'
57+
# name is already correct
58+
else
59+
error('unsupported android architecture: ' + host_machine.cpu_family())
60+
endif
61+
62+
lib_dir = (base_prefab_dir / 'libs' / ('android.' + arch_name))
63+
64+
c = meson.get_compiler('c')
65+
66+
discord_partner_sdk = c.find_library(
67+
'discord_partner_sdk',
68+
dirs: [lib_dir],
69+
required: true,
70+
)
71+
72+
discord_social_sdk_lib = static_library(
73+
'discord_social_sdk',
74+
files('../impl.cpp'),
75+
include_directories: inc_dirs,
76+
dependencies: discord_partner_sdk,
77+
install: true,
78+
)
79+
80+
discord_social_sdk_dep = declare_dependency(
81+
include_directories: inc_dirs,
82+
version: meson.project_version(),
83+
link_with: discord_social_sdk_lib,
84+
)
85+
86+
meson.override_dependency('discord-social-sdk', discord_social_sdk_dep)

0 commit comments

Comments
 (0)