backbeat_c_sdk 0.5.0-beta

C SDK for Backbeat. Trivially integratable into all good games.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
use std::os::raw::c_char;
use std::ptr;

use backbeat_core::{CollectionHeader, ValidGamemodeIdentifier};
use backbeat_sdk::Backbeat;
use backbeat_sdk::assets::AssetData;
use backbeat_sdk::collections::{CollectionUpsertResult, CollectionUpsertStatus};
use backbeat_sdk::store::stats::StoreStats;

mod ffi_types;
pub(crate) mod util;

pub use ffi_types::*;

use self::util::{
	collection_download_data_report, collection_kind, download_list_result, download_overview,
	download_snapshot_list, has_collection_kind, optional_download_progress, parse_asset_id,
	parse_bundle_id, parse_chart_id, parse_data, path_string, run_store, server_config, with_store,
	with_store_async, with_store_spawn,
};
use crate::error::{BKB_ERR_NULL_ARG, bkb_error_code};
use crate::functions::bkb_string_free;
use crate::types::{
	bkb_bb, bkb_bundle_search_result, bkb_bytes, bkb_collection_metadata_list, bkb_course,
	bkb_pack, bkb_server_config, bkb_store, bkb_string, bkb_table, bkb_timestamp, cstr_to_str,
};
use crate::util::run_ffi;

unsafe fn parse_gamemodes(
	values: *const *const c_char,
	len: usize,
) -> Result<Vec<ValidGamemodeIdentifier>, bkb_error_code> {
	if len == 0 {
		return Ok(Vec::new());
	}
	if values.is_null() {
		return Err(BKB_ERR_NULL_ARG);
	}
	unsafe { std::slice::from_raw_parts(values, len) }
		.iter()
		.map(|value| {
			ValidGamemodeIdentifier::new(unsafe { cstr_to_str(*value) }?)
				.map_err(|_| crate::error::BKB_ERR_PARSE)
		})
		.collect()
}

fn collection_header(value: CollectionHeader) -> bkb_collection_header {
	bkb_collection_header {
		last_modified: bkb_timestamp {
			seconds: value.timestamp.timestamp(),
			nanoseconds: value.timestamp.timestamp_subsec_nanos(),
		},
		kind: collection_kind(value.kind),
	}
}

fn collection_upsert_status(value: CollectionUpsertStatus) -> bkb_collection_upsert_status {
	match value {
		CollectionUpsertStatus::Inserted => BKB_COLLECTION_UPSERT_INSERTED,
		CollectionUpsertStatus::Updated => BKB_COLLECTION_UPSERT_UPDATED,
		CollectionUpsertStatus::TimestampUnchanged => BKB_COLLECTION_UPSERT_TIMESTAMP_UNCHANGED,
	}
}

fn collection_upsert_result(value: CollectionUpsertResult) -> bkb_collection_upsert_result {
	bkb_collection_upsert_result {
		kind: collection_kind(value.kind),
		status: collection_upsert_status(value.status),
	}
}

// n.b. bkb_store_open with args is deliberately not implemented - that is still an internal only thing
// and users DO NOT NEED IT.

/// Open the backbeat store. This will create it if it does not yet exist.
///
/// The final program must provide one SQLite implementation for Backbeat and
/// every other native consumer that accesses the live database. Do not call
/// sqlite3_shutdown while a Backbeat store is alive.
///
/// # Safety
///
/// out_store must be non-null and point to writable storage for one bkb_store pointer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_open(out_store: *mut *mut bkb_store) -> bkb_error_code {
	run_ffi(|| {
		if out_store.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		crate::sqlite::ensure_compatible()?;
		let inner = run_store(Backbeat::open)?;
		let store = Box::into_raw(Box::new(bkb_store { inner }));
		unsafe { out_store.write(store) };
		Ok(())
	})
}

/// Close a backbeat store and release its resources.
///
/// # Safety
///
/// store must be null or a pointer returned by bkb_store_open that has not already been freed. After this call, the pointer must not be used again.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_free(store: *mut bkb_store) {
	if !store.is_null() {
		unsafe { drop(Box::from_raw(store)) };
	}
}

/// Return whether the store changed since last_revision, along with its current revision.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_should_refresh(
	store: *const bkb_store,
	last_revision: i64,
	out_should_refresh: *mut bool,
	out_revision: *mut i64,
) -> bkb_error_code {
	run_ffi(|| {
		if out_should_refresh.is_null() || out_revision.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let (should_refresh, revision) =
			unsafe { with_store(store, |store| store.should_refresh(last_revision)) }?;
		unsafe { out_should_refresh.write(should_refresh) };
		unsafe { out_revision.write(revision) };
		Ok(())
	})
}

/// Where the user has configured their store to be.
///
/// Note that you probably do not need to care about this, much less do anything with this information.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_dir(
	store: *const bkb_store,
	out: *mut bkb_string,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.store_dir().to_path_buf())) }?;
		let value = path_string(value)?;
		unsafe { bkb_string::write(out, value) }
	})
}

/// The path to the logs dir. This is where backbeat logs get written to.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_logs_dir(
	store: *const bkb_store,
	out: *mut bkb_string,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.logs_dir())) }?;
		let value = path_string(value)?;
		unsafe { bkb_string::write(out, value) }
	})
}

/// The path to the config dir. This contains a backbeat.toml file.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_config_dir(
	store: *const bkb_store,
	out: *mut bkb_string,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.config_dir().to_path_buf())) }?;
		let value = path_string(value)?;
		unsafe { bkb_string::write(out, value) }
	})
}

/// Add a data server to the config and live server list.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// server must be non-null and point to a valid bkb_server_config whose URL is a valid UTF-8 C string for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_server_add(
	store: *const bkb_store,
	server: *const bkb_server_config,
) -> bkb_error_code {
	run_ffi(|| {
		let server = unsafe { server_config(server) }?;
		unsafe { with_store(store, |store| store.server_add(&server)) }?;
		Ok(())
	})
}

/// Remove a data server from the config and live server list.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// server must be non-null and point to a valid bkb_server_config whose URL is a valid UTF-8 C string for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_server_rm(
	store: *const bkb_store,
	server: *const bkb_server_config,
) -> bkb_error_code {
	run_ffi(|| {
		let server = unsafe { server_config(server) }?;
		unsafe { with_store(store, |store| store.server_rm(&server)) }?;
		Ok(())
	})
}

/// Does this store have absolutely zero data servers set up?
///
/// This is probably the weirdest public backbeat function, but it is useful to quickly indicate that this user has literally no way of installing content.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_has_zero_data_servers(
	store: *const bkb_store,
	out: *mut bool,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.has_zero_data_servers())) }?;
		unsafe { out.write(value) };
		Ok(())
	})
}

/// Get the SQLite connection URL for attaching the store read-only.
///
/// This returns file:///path/to/backbeat.db?mode=ro.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_sqlite_connection_url(
	store: *const bkb_store,
	out: *mut bkb_string,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.sqlite_connection_url())) }?;
		unsafe { bkb_string::write(out, value) }
	})
}

/// Get a SQL statement that will attach the backbeat database read-only as backbeat.
///
/// Evaluate this string in sqlite to get a database backbeat attached to your database.
///
/// More specifically, this function returns ATTACH DATABASE 'path_to_backbeat.db' AS backbeat;.
///
/// Execute it on a SQLite connection opened with URI filename handling, such
/// as SQLITE_OPEN_URI or a build configured with SQLITE_USE_URI.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_sqlite_attach_command(
	store: *const bkb_store,
	out: *mut bkb_string,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.sqlite_attach_command())) }?;
		unsafe { bkb_string::write(out, value) }
	})
}

/// Get a SQL statement that will detach backbeat from your database.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_sqlite_detach_command(
	store: *const bkb_store,
	out: *mut bkb_string,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.sqlite_detach_command())) }?;
		unsafe { bkb_string::write(out, value) }
	})
}

/// Add a backbeat file to the store. This does not queue up any downloads. Call bkb_store_bundle_download_assets after this to queue up downloads.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// bb must be non-null and point to a valid bkb_bb whose borrowed fields remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_import_bundle(
	store: *const bkb_store,
	bb: *const bkb_bb,
	out_bundle_id: *mut bkb_string,
) -> bkb_error_code {
	run_ffi(|| {
		if bb.is_null() || out_bundle_id.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let id = unsafe { with_store(store, |store| store.import_bundle((*bb).inner())) }?;
		unsafe { bkb_string::write(out_bundle_id, id.to_string()) }
	})
}

/// Import a .bbzip file into your store.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_import_bbzip(
	store: *const bkb_store,
	path: *const c_char,
) -> bkb_error_code {
	run_ffi(|| {
		let path = unsafe { cstr_to_str(path) }?;
		unsafe { with_store(store, |store| store.import_bbzip(path)) }?;
		Ok(())
	})
}

/// Take a path and add it to the store as an asset.
///
/// This API is honestly just provided to flesh out the API. It is very rare you will need this, but if this did not exist, you would have to make a temporary .bbzip or something.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_import_asset(
	store: *const bkb_store,
	path: *const c_char,
) -> bkb_error_code {
	run_ffi(|| {
		let path = unsafe { cstr_to_str(path) }?;
		unsafe { with_store(store, |store| store.import_asset(path)) }?;
		Ok(())
	})
}

/// Export a chart and all its assets to a folder or .bbzip file.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// The ID and output path pointers must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_export_chart(
	store: *const bkb_store,
	chart_id: *const c_char,
	output: *const c_char,
	bbzip: bool,
) -> bkb_error_code {
	run_ffi(|| {
		let chart_id = unsafe { parse_chart_id(chart_id) }?;
		let output = unsafe { cstr_to_str(output) }?;
		unsafe { with_store(store, |store| store.export_chart(&chart_id, output, bbzip)) }?;
		Ok(())
	})
}

/// Export a bundle and all its assets to a folder or .bbzip file.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// The ID and output path pointers must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_export_bundle(
	store: *const bkb_store,
	bundle_id: *const c_char,
	output: *const c_char,
	bbzip: bool,
) -> bkb_error_code {
	run_ffi(|| {
		let bundle_id = unsafe { parse_bundle_id(bundle_id) }?;
		let output = unsafe { cstr_to_str(output) }?;
		unsafe { with_store(store, |store| store.export_bundle(bundle_id, output, bbzip)) }?;
		Ok(())
	})
}

/// Queue up downloads for all missing assets in this bundle. This bundle has to be installed in your store.
///
/// This is a useful function for repairing bundles with incomplete assets, or bundles that have just been manually added with bkb_store_import_bundle.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_bundle_download_assets(
	store: *const bkb_store,
	bundle_id: *const c_char,
) -> bkb_error_code {
	run_ffi(|| {
		let bundle_id = unsafe { parse_bundle_id(bundle_id) }?;
		unsafe { with_store(store, |store| store.bundle_download_assets(bundle_id)) }?;
		Ok(())
	})
}

/// Download an asset from your configured data servers and install it into the store.
///
/// No-op if the asset is already present locally. Concurrent calls for the same asset are coalesced onto a single network fetch by the download manager.
///
/// This starts the download and returns immediately. Completion is reported through store events.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_server_download_asset(
	store: *const bkb_store,
	asset_id: *const c_char,
) -> bkb_error_code {
	run_ffi(|| {
		let asset_id = unsafe { parse_asset_id(asset_id) }?;
		unsafe {
			with_store_spawn(store, |store| async move {
				store.server_download_asset(asset_id).await
			})
		}
	})
}

/// Download a bundle from your configured data servers and install it.
/// This will also download all of the assets referenced by this chart, too.
///
/// No-op if the asset is already present locally. Concurrent calls for the same bundle are coalesced onto a single network fetch by the download manager.
///
/// This starts the download and returns immediately. Completion is reported through store events.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_server_download_bundle(
	store: *const bkb_store,
	bundle_id: *const c_char,
) -> bkb_error_code {
	run_ffi(|| {
		let bundle_id = unsafe { parse_bundle_id(bundle_id) }?;
		unsafe {
			with_store_spawn(store, |store| async move {
				store.server_download_bundle(bundle_id).await
			})
		}
	})
}

/// Download a chart from your configured data servers and install it.
/// This will also download all of the assets referenced by this bundle, too.
///
/// No-op if the chart is already present locally. Concurrent calls for the same chart are coalesced onto a single network fetch by the download manager.
///
/// This starts the download and returns immediately. Completion is reported through store events.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_server_download_chart(
	store: *const bkb_store,
	chart_id: *const c_char,
) -> bkb_error_code {
	run_ffi(|| {
		let chart_id = unsafe { parse_chart_id(chart_id) }?;
		unsafe {
			with_store_spawn(store, |store| async move {
				store.server_download_chart(&chart_id).await
			})
		}
	})
}

/// Download charts, bundles, and assets referenced by an installed collection.
///
/// The collection at url must already be stored locally.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_collection_fetch_download_data(
	store: *const bkb_store,
	url: *const c_char,
	out_report: *mut bkb_collection_download_data_report,
) -> bkb_error_code {
	run_ffi(|| {
		if out_report.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let url = unsafe { cstr_to_str(url) }?.to_owned();
		let report = unsafe {
			with_store_async(store, |store| async move {
				store.collection_fetch_download_data(&url, |_| {}).await
			})
		}?;
		let report = collection_download_data_report(report)?;
		unsafe { out_report.write(report) };
		Ok(())
	})
}

/// Fetch only the header for this collection. Doubles up as a way of checking whether a URL is a valid collection.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_collection_fetch_header(
	store: *const bkb_store,
	url: *const c_char,
	out_header: *mut bkb_collection_header,
) -> bkb_error_code {
	run_ffi(|| {
		if out_header.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let url = unsafe { cstr_to_str(url) }?.to_owned();
		let value = unsafe {
			with_store_async(store, |store| async move {
				store.collection_fetch_header(&url).await
			})
		}?;
		unsafe { out_header.write(collection_header(value)) };
		Ok(())
	})
}

/// Fetch and insert this collection to your backbeat store.
///
/// Updates it if already installed.
///
/// To install the contents inside this collection, use bkb_store_collection_fetch_download_data.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_collection_fetch_upsert(
	store: *const bkb_store,
	url: *const c_char,
	out_result: *mut bkb_collection_upsert_result,
) -> bkb_error_code {
	run_ffi(|| {
		if out_result.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let url = unsafe { cstr_to_str(url) }?.to_owned();
		let value = unsafe {
			with_store_async(store, |store| async move {
				store.collection_fetch_upsert(&url).await
			})
		}?;
		unsafe { out_result.write(collection_upsert_result(value)) };
		Ok(())
	})
}

/// Release a collection download data report returned by bkb_store_collection_fetch_download_data.
///
/// # Safety
///
/// The value must have been returned by the corresponding Backbeat C SDK function and must not have already been released.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_collection_download_data_report_free(
	report: bkb_collection_download_data_report,
) {
	if report.errors.is_null() {
		return;
	}
	let errors = ptr::slice_from_raw_parts_mut(report.errors, report.errors_len);
	for error in unsafe { &mut *errors } {
		unsafe {
			bkb_string_free(error.item.value);
			bkb_string_free(error.message);
		}
	}
	unsafe { drop(Box::from_raw(errors)) };
}

unsafe fn download_snapshots_free(items: *const bkb_download_snapshot, items_len: usize) {
	if items.is_null() {
		return;
	}
	let items = ptr::slice_from_raw_parts_mut(items.cast_mut(), items_len);
	for item in unsafe { &mut *items } {
		unsafe {
			bkb_string_free(item.key.value);
			bkb_string_free(item.error);
		}
	}
	unsafe { drop(Box::from_raw(items)) };
}

/// Release a download snapshot list returned by bkb_store_download_all_progress.
///
/// # Safety
///
/// The value must have been returned by the corresponding Backbeat C SDK function and must not have already been released.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_download_snapshot_list_free(value: bkb_download_snapshot_list) {
	unsafe { download_snapshots_free(value.items, value.items_len) };
}

/// Release a download overview returned by bkb_store_download_overview.
///
/// # Safety
///
/// The value must have been returned by the corresponding Backbeat C SDK function and must not have already been released.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_download_overview_free(value: bkb_download_overview) {
	unsafe { bkb_string_free(value.first_error) };
}

/// Release a paginated download list returned by bkb_store_download_list or bkb_store_collection_download_list.
///
/// # Safety
///
/// The value must have been returned by the corresponding Backbeat C SDK function and must not have already been released.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_download_list_result_free(value: bkb_download_list_result) {
	unsafe {
		bkb_string_free(value.first_error);
		download_snapshots_free(value.downloads, value.downloads_len);
	}
}

/// Collect statistics about the store.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_stats(
	store: *const bkb_store,
	out_stats: *mut bkb_stats,
) -> bkb_error_code {
	run_ffi(|| {
		if out_stats.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let StoreStats {
			charts,
			tables,
			courses,
			packs,
			asset_count,
			asset_bytes,
			db_bytes,
		} = unsafe { with_store(store, backbeat_sdk::Backbeat::stats) }?;
		unsafe {
			out_stats.write(bkb_stats {
				charts,
				tables,
				courses,
				packs,
				asset_count,
				asset_bytes,
				db_bytes,
			});
		};
		Ok(())
	})
}

/// Check for store corruption.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_corruption_check(
	store: *const bkb_store,
	out_report: *mut *mut bkb_corruption_report,
) -> bkb_error_code {
	run_ffi(|| {
		if out_report.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| store.corruption_check()) }?;
		unsafe { out_report.write(bkb_corruption_report::alloc(value)) };
		Ok(())
	})
}

/// Release a corruption report returned by bkb_store_corruption_check.
///
/// # Safety
///
/// The value must be null or a pointer returned by the corresponding Backbeat C SDK function, and it must not have already been released.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_corruption_report_free(value: *mut bkb_corruption_report) {
	unsafe { bkb_corruption_report::free(value) };
}

/// Repair some store corruption.
///
/// - For all charts, re-inspect it, and save the new inspected metadata.
/// - Delete corrupt or missing assets so they can be downloaded again.
/// - Delete charts whose contents are corrupt or cannot be inspected.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_corruption_repair(store: *const bkb_store) -> bkb_error_code {
	run_ffi(|| {
		unsafe { with_store(store, |store| store.corruption_repair()) }?;
		Ok(())
	})
}

/// Delete all assets that are not referenced by a chart or collection.
///
/// Returns the amount of things that were removed, or will be removed if dry_run is true.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_asset_prune(
	store: *const bkb_store,
	dry_run: bool,
	out_count: *mut u64,
) -> bkb_error_code {
	run_ffi(|| {
		if out_count.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| store.asset_prune(dry_run)) }?;
		unsafe { out_count.write(value) };
		Ok(())
	})
}

/// Delete any assets that are stored on disk, but do not exist in the store.
/// This also wipes files in .downloading that have not been touched in the past hour.
/// Under normal circumstances, this should never happen.
///
/// Returns the amount of things that were removed, or will be removed if dry_run is true.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_disk_prune(
	store: *const bkb_store,
	dry_run: bool,
	out_count: *mut u64,
) -> bkb_error_code {
	run_ffi(|| {
		if out_count.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| store.disk_prune(dry_run)) }?;
		unsafe { out_count.write(value) };
		Ok(())
	})
}

/// List installed tables, optionally restricted to gamemodes.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// If gamemodes_len is greater than zero, gamemodes must point to an array of that many non-null, NUL-terminated, valid UTF-8 C strings. If gamemodes_len is zero, gamemodes may be null.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_list_tables(
	store: *const bkb_store,
	gamemodes: *const *const c_char,
	gamemodes_len: usize,
	out_list: *mut *mut bkb_collection_metadata_list,
) -> bkb_error_code {
	run_ffi(|| {
		if out_list.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let gamemodes = unsafe { parse_gamemodes(gamemodes, gamemodes_len) }?;
		let values = unsafe { with_store(store, |store| store.list_tables(&gamemodes)) }?;
		unsafe { out_list.write(bkb_collection_metadata_list::alloc(values)) };
		Ok(())
	})
}

/// List installed courses, optionally restricted to gamemodes.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// If gamemodes_len is greater than zero, gamemodes must point to an array of that many non-null, NUL-terminated, valid UTF-8 C strings. If gamemodes_len is zero, gamemodes may be null.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_list_courses(
	store: *const bkb_store,
	gamemodes: *const *const c_char,
	gamemodes_len: usize,
	out_list: *mut *mut bkb_collection_metadata_list,
) -> bkb_error_code {
	run_ffi(|| {
		if out_list.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let gamemodes = unsafe { parse_gamemodes(gamemodes, gamemodes_len) }?;
		let values = unsafe { with_store(store, |store| store.list_courses(&gamemodes)) }?;
		unsafe { out_list.write(bkb_collection_metadata_list::alloc(values)) };
		Ok(())
	})
}

/// List installed packs, optionally restricted to gamemodes.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// If gamemodes_len is greater than zero, gamemodes must point to an array of that many non-null, NUL-terminated, valid UTF-8 C strings. If gamemodes_len is zero, gamemodes may be null.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_list_packs(
	store: *const bkb_store,
	gamemodes: *const *const c_char,
	gamemodes_len: usize,
	out_list: *mut *mut bkb_collection_metadata_list,
) -> bkb_error_code {
	run_ffi(|| {
		if out_list.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let gamemodes = unsafe { parse_gamemodes(gamemodes, gamemodes_len) }?;
		let values = unsafe { with_store(store, |store| store.list_packs(&gamemodes)) }?;
		unsafe { out_list.write(bkb_collection_metadata_list::alloc(values)) };
		Ok(())
	})
}

/// Search bundles by query, optionally restricted to file extensions. An empty extension list does not filter results.
///
/// Results are listed alphabetically if query is empty, that is, null or blank.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// query may be null to search without a query; otherwise it must be NUL-terminated and valid UTF-8. If extensions_len is greater than zero, extensions must point to an array of that many non-null, NUL-terminated, valid UTF-8 C strings. If extensions_len is zero, extensions may be null.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_search_bundles(
	store: *const bkb_store,
	query: *const c_char,
	offset: u64,
	limit: u32,
	extensions: *const *const c_char,
	extensions_len: usize,
	out_result: *mut *mut bkb_bundle_search_result,
) -> bkb_error_code {
	run_ffi(|| {
		if out_result.is_null() || (extensions_len > 0 && extensions.is_null()) {
			return Err(BKB_ERR_NULL_ARG);
		}
		let query = if query.is_null() {
			None
		} else {
			Some(unsafe { cstr_to_str(query) }?)
		};
		let extensions = if extensions_len == 0 {
			Vec::new()
		} else {
			unsafe { std::slice::from_raw_parts(extensions, extensions_len) }
				.iter()
				.map(|value| unsafe { cstr_to_str(*value) })
				.collect::<Result<Vec<_>, _>>()?
		};
		let value = unsafe {
			with_store(store, |store| {
				store.search_bundles(query, offset, limit, &extensions)
			})
		}?;
		unsafe { out_result.write(bkb_bundle_search_result::alloc(value)) };
		Ok(())
	})
}

/// Release a bundle search result returned by bkb_store_search_bundles.
///
/// # Safety
///
/// The value must be null or a pointer returned by the corresponding Backbeat C SDK function, and it must not have already been released.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_bundle_search_result_free(value: *mut bkb_bundle_search_result) {
	unsafe { bkb_bundle_search_result::free(value) };
}

/// Release a collection metadata list returned by bkb_store_list_tables, bkb_store_list_courses, or bkb_store_list_packs.
///
/// # Safety
///
/// The value must be null or a pointer returned by the corresponding Backbeat C SDK function, and it must not have already been released.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_collection_metadata_list_free(
	value: *mut bkb_collection_metadata_list,
) {
	unsafe { bkb_collection_metadata_list::free(value) };
}

/// Get this pack from your database by URL.
///
/// This function does not do a network call; collections are just identified by URL. This fetches information from your store.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_get_pack(
	store: *const bkb_store,
	url: *const c_char,
	out_pack: *mut *mut bkb_pack,
) -> bkb_error_code {
	run_ffi(|| {
		if out_pack.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let url = unsafe { cstr_to_str(url) }?;
		let inner = unsafe { with_store(store, |store| store.get_pack(url)) }?;
		unsafe { out_pack.write(bkb_pack::alloc(inner)) };
		Ok(())
	})
}

/// Get this table from your database by URL.
///
/// This function does not do a network call. This fetches information from your store.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_get_table(
	store: *const bkb_store,
	url: *const c_char,
	out_table: *mut *mut bkb_table,
) -> bkb_error_code {
	run_ffi(|| {
		if out_table.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let url = unsafe { cstr_to_str(url) }?;
		let inner = unsafe { with_store(store, |store| store.get_table(url)) }?;
		unsafe { out_table.write(bkb_table::alloc(inner)) };
		Ok(())
	})
}

/// Get this course from your database by URL.
///
/// This function does not do a network call. This fetches information from your store.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_get_course(
	store: *const bkb_store,
	url: *const c_char,
	out_course: *mut *mut bkb_course,
) -> bkb_error_code {
	run_ffi(|| {
		if out_course.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let url = unsafe { cstr_to_str(url) }?;
		let inner = unsafe { with_store(store, |store| store.get_course(url)) }?;
		unsafe { out_course.write(bkb_course::alloc(inner)) };
		Ok(())
	})
}

/// Given a chart ID, get the chart file bytes.
///
/// To get the backbeat file for a chart, use bkb_store_get_chart.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_get_chart_data(
	store: *const bkb_store,
	chart_id: *const c_char,
	out_data: *mut bkb_bytes,
) -> bkb_error_code {
	run_ffi(|| {
		if out_data.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let chart_id = unsafe { parse_chart_id(chart_id) }?;
		let data = unsafe { with_store(store, |store| store.get_chart_data(&chart_id)) }?;
		unsafe { bkb_bytes::write(out_data, data) }
	})
}

/// Get the backbeat file for this chart ID. If this request is ambiguous, there are two bundles with the same chart_sha256, an unspecified bundle with the correct chart ID will be returned.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_get_chart(
	store: *const bkb_store,
	chart_id: *const c_char,
	out_bb: *mut *mut bkb_bb,
) -> bkb_error_code {
	run_ffi(|| {
		if out_bb.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let chart_id = unsafe { parse_chart_id(chart_id) }?;
		let inner = unsafe { with_store(store, |store| store.get_chart(&chart_id)) }?;
		unsafe { out_bb.write(bkb_bb::alloc(inner)) };
		Ok(())
	})
}

/// Get a bundle by its bundle ID.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_get_bundle(
	store: *const bkb_store,
	bundle_id: *const c_char,
	out_bb: *mut *mut bkb_bb,
) -> bkb_error_code {
	run_ffi(|| {
		if out_bb.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let bundle_id = unsafe { parse_bundle_id(bundle_id) }?;
		let inner = unsafe { with_store(store, |store| store.get_bundle(bundle_id)) }?;
		unsafe { out_bb.write(bkb_bb::alloc(inner)) };
		Ok(())
	})
}

impl bkb_asset_data {
	fn new(data: AssetData) -> Result<Self, bkb_error_code> {
		Ok(match data {
			AssetData::Bytes(bytes) => Self {
				kind: BKB_ASSET_DATA_BYTES,
				value: bkb_asset_data_value {
					bytes: bkb_bytes::new(bytes),
				},
			},
			AssetData::File(path) => Self {
				kind: BKB_ASSET_DATA_FILE,
				value: bkb_asset_data_value {
					file: bkb_string::new(path_string(path)?)?,
				},
			},
		})
	}
}

/// Given an asset ID, get the actual data for this asset.
///
/// This returns bkb_asset_data, which either contains the entire file already in a buffer if small or a reader to get the file bytes off disk. See the bkb_asset_data type for more functionality and how to work with the bytes.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_get_asset(
	store: *const bkb_store,
	asset_id: *const c_char,
	out_data: *mut bkb_asset_data,
) -> bkb_error_code {
	run_ffi(|| {
		if out_data.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let asset_id = unsafe { parse_asset_id(asset_id) }?;
		let data = unsafe { with_store(store, |store| store.get_asset(asset_id)) }?;
		unsafe { out_data.write(bkb_asset_data::new(data)?) };
		Ok(())
	})
}

/// Resolve asset data for a bundle plus path.
///
/// This is like resolving a path in a backbeat file, but easy.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// bundle_id and asset_path must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_resolve_path(
	store: *const bkb_store,
	bundle_id: *const c_char,
	asset_path: *const c_char,
	out_data: *mut bkb_asset_data,
) -> bkb_error_code {
	run_ffi(|| {
		if out_data.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let bundle_id = unsafe { parse_bundle_id(bundle_id) }?;
		let asset_path = unsafe { cstr_to_str(asset_path) }?;
		let data =
			unsafe { with_store(store, |store| store.resolve_asset(bundle_id, asset_path)) }?;
		unsafe { out_data.write(bkb_asset_data::new(data)?) };
		Ok(())
	})
}

/// Release asset data returned by bkb_store_get_asset or bkb_store_resolve_path.
///
/// # Safety
///
/// The value must have been returned by the corresponding Backbeat C SDK function and must not have already been released.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_asset_data_free(data: bkb_asset_data) {
	match data.kind {
		BKB_ASSET_DATA_BYTES => unsafe { crate::functions::bkb_bytes_free(data.value.bytes) },
		BKB_ASSET_DATA_FILE => unsafe { crate::functions::bkb_string_free(data.value.file) },
		_ => {}
	}
}

/// Return true if this asset is installed.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_has_asset(
	store: *const bkb_store,
	asset_id: *const c_char,
	out: *mut bool,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let asset_id = unsafe { parse_asset_id(asset_id) }?;
		let value = unsafe { with_store(store, |store| store.has_asset(asset_id)) }?;
		unsafe { out.write(value) };
		Ok(())
	})
}

/// Return true if a chart with this ID is installed.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_has_chart(
	store: *const bkb_store,
	chart_id: *const c_char,
	out: *mut bool,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let chart_id = unsafe { parse_chart_id(chart_id) }?;
		let value = unsafe { with_store(store, |store| store.has_chart(&chart_id)) }?;
		unsafe { out.write(value) };
		Ok(())
	})
}

/// Return true if this bundle is installed.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_has_bundle(
	store: *const bkb_store,
	bundle_id: *const c_char,
	out: *mut bool,
) -> bkb_error_code {
	run_ffi(|| {
		if out.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let bundle_id = unsafe { parse_bundle_id(bundle_id) }?;
		let value = unsafe { with_store(store, |store| store.has_bundle(bundle_id)) }?;
		unsafe { out.write(value) };
		Ok(())
	})
}

/// Return true if a collection with this URL is installed.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_has_collection(
	store: *const bkb_store,
	url: *const c_char,
	out: *mut bool,
) -> bkb_error_code {
	unsafe { has_collection_kind(store, url, out, |store, url| store.has_collection(url)) }
}

/// Return true if a pack with this URL is installed.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_has_pack(
	store: *const bkb_store,
	url: *const c_char,
	out: *mut bool,
) -> bkb_error_code {
	unsafe { has_collection_kind(store, url, out, |store, url| store.has_pack(url)) }
}

/// Return true if a table with this URL is installed.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_has_table(
	store: *const bkb_store,
	url: *const c_char,
	out: *mut bool,
) -> bkb_error_code {
	unsafe { has_collection_kind(store, url, out, |store, url| store.has_table(url)) }
}

/// Return true if a course with this URL is installed.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_has_course(
	store: *const bkb_store,
	url: *const c_char,
	out: *mut bool,
) -> bkb_error_code {
	unsafe { has_collection_kind(store, url, out, |store, url| store.has_course(url)) }
}

/// Get the progress of a download, if any.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// data_id must be non-null and point to a valid bkb_data_id for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_download_progress(
	store: *const bkb_store,
	data_id: *const bkb_data_id,
	out_progress: *mut bkb_optional_download_progress,
) -> bkb_error_code {
	run_ffi(|| {
		if out_progress.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let data_id = unsafe { parse_data(data_id) }?;
		let value = unsafe { with_store(store, |store| Ok(store.download_progress(&data_id))) }?;
		let value = optional_download_progress(value);
		unsafe { out_progress.write(value) };
		Ok(())
	})
}

/// What is the current state of all downloads?
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_download_all_progress(
	store: *const bkb_store,
	out_progress: *mut bkb_download_snapshot_list,
) -> bkb_error_code {
	run_ffi(|| {
		if out_progress.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.download_all_progress())) }?;
		let value = download_snapshot_list(value)?;
		unsafe { out_progress.write(value) };
		Ok(())
	})
}

/// Get an overview of current download stats.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_download_overview(
	store: *const bkb_store,
	out_overview: *mut bkb_download_overview,
) -> bkb_error_code {
	run_ffi(|| {
		if out_overview.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.download_overview())) }?;
		let value = download_overview(value)?;
		unsafe { out_overview.write(value) };
		Ok(())
	})
}

/// Paginated download list for the GUI.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_download_list(
	store: *const bkb_store,
	offset: u64,
	limit: u32,
	out_result: *mut bkb_download_list_result,
) -> bkb_error_code {
	run_ffi(|| {
		if out_result.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.download_list(offset, limit))) }?;
		let value = download_list_result(value)?;
		unsafe { out_result.write(value) };
		Ok(())
	})
}

/// Paginated chart and bundle downloads for the Downloads view.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_collection_download_list(
	store: *const bkb_store,
	offset: u64,
	limit: u32,
	out_result: *mut bkb_download_list_result,
) -> bkb_error_code {
	run_ffi(|| {
		if out_result.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe {
			with_store(store, |store| {
				Ok(store.collection_download_list(offset, limit))
			})
		}?;
		let value = download_list_result(value)?;
		unsafe { out_result.write(value) };
		Ok(())
	})
}

/// Remove every terminal download from the download manager.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_download_clear_finished(
	store: *const bkb_store,
	out_count: *mut usize,
) -> bkb_error_code {
	run_ffi(|| {
		if out_count.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let value = unsafe { with_store(store, |store| Ok(store.download_clear_finished())) }?;
		unsafe { out_count.write(value) };
		Ok(())
	})
}

/// Cancel an in-flight download.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// data_id must be non-null and point to a valid bkb_data_id for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_download_cancel(
	store: *const bkb_store,
	data_id: *const bkb_data_id,
	out_cancelled: *mut bool,
) -> bkb_error_code {
	run_ffi(|| {
		if out_cancelled.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let data_id = unsafe { parse_data(data_id) }?;
		let value = unsafe { with_store(store, |store| Ok(store.download_cancel(data_id))) }?;
		unsafe { out_cancelled.write(value) };
		Ok(())
	})
}

/// Cancel an in-flight asset download.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_download_asset_cancel(
	store: *const bkb_store,
	asset_id: *const c_char,
	out_cancelled: *mut bool,
) -> bkb_error_code {
	run_ffi(|| {
		if out_cancelled.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let asset_id = unsafe { parse_asset_id(asset_id) }?;
		let value =
			unsafe { with_store(store, |store| Ok(store.download_asset_cancel(asset_id))) }?;
		unsafe { out_cancelled.write(value) };
		Ok(())
	})
}

/// Cancel an in-flight chart download.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_download_chart_cancel(
	store: *const bkb_store,
	chart_id: *const c_char,
	out_cancelled: *mut bool,
) -> bkb_error_code {
	run_ffi(|| {
		if out_cancelled.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let chart_id = unsafe { parse_chart_id(chart_id) }?;
		let value =
			unsafe { with_store(store, |store| Ok(store.download_chart_cancel(&chart_id))) }?;
		unsafe { out_cancelled.write(value) };
		Ok(())
	})
}

/// Cancel an in-flight bundle download.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_download_bundle_cancel(
	store: *const bkb_store,
	bundle_id: *const c_char,
	out_cancelled: *mut bool,
) -> bkb_error_code {
	run_ffi(|| {
		if out_cancelled.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let bundle_id = unsafe { parse_bundle_id(bundle_id) }?;
		let value =
			unsafe { with_store(store, |store| Ok(store.download_bundle_cancel(bundle_id))) }?;
		unsafe { out_cancelled.write(value) };
		Ok(())
	})
}

/// Remove this bundle from your store.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_bundle_rm(
	store: *const bkb_store,
	bundle_id: *const c_char,
) -> bkb_error_code {
	run_ffi(|| {
		let bundle_id = unsafe { parse_bundle_id(bundle_id) }?;
		unsafe { with_store(store, |store| store.bundle_rm(bundle_id)) }
	})
}

/// Remove a collection from your store. Optionally, decide whether you want to remove its charts, too.
///
/// Charts will only be removed if there is not another collection referencing the chart.
///
/// # Safety
///
/// The store pointer must be non-null and point to a live bkb_store returned by bkb_store_open. It must remain valid for the duration of the call.
/// Every C string pointer must be non-null, NUL-terminated, and point to valid UTF-8 for the duration of the call.
/// Every output pointer must be non-null and point to writable storage of the type shown. If this function returns an error, output storage may be unchanged.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bkb_store_collection_rm(
	store: *const bkb_store,
	url: *const c_char,
	remove_charts_too: bool,
	out_kind: *mut bkb_collection_kind,
) -> bkb_error_code {
	run_ffi(|| {
		if out_kind.is_null() {
			return Err(BKB_ERR_NULL_ARG);
		}
		let url = unsafe { cstr_to_str(url) }?;
		let value =
			unsafe { with_store(store, |store| store.collection_rm(url, remove_charts_too)) }?;
		unsafe { out_kind.write(collection_kind(value)) };
		Ok(())
	})
}

#[cfg(test)]
#[path = "store/method_tests.rs"]
mod method_tests;

#[cfg(test)]
#[path = "store/tests.rs"]
mod tests;