Skip to content

Commit 38e9a7f

Browse files
committed
Add C API for use in language bindings
1 parent 5eb57f4 commit 38e9a7f

File tree

10 files changed

+1158
-327
lines changed

10 files changed

+1158
-327
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ jobs:
2222
uses: actions/checkout@v5
2323
with:
2424
persist-credentials: false
25-
submodules: recursive
2625

2726
- name: Setup MSVC
2827
if: runner.os == 'Windows'

CMakePresets.json

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,108 @@
309309
]
310310
}
311311
],
312+
"buildPresets": [
313+
{
314+
"name": "base",
315+
"hidden": true,
316+
"targets": [
317+
"all",
318+
"all_verify_interface_header_sets"
319+
]
320+
},
321+
{
322+
"name": "windows-base",
323+
"hidden": true,
324+
"inherits": "base",
325+
"condition": {
326+
"type": "equals",
327+
"lhs": "${hostSystemName}",
328+
"rhs": "Windows"
329+
}
330+
},
331+
{
332+
"name": "unix-base",
333+
"hidden": true,
334+
"inherits": "base",
335+
"condition": {
336+
"type": "notEquals",
337+
"lhs": "${hostSystemName}",
338+
"rhs": "Windows"
339+
}
340+
},
341+
{
342+
"name": "x64-debug-windows",
343+
"displayName": "x64 Debug",
344+
"inherits": "windows-base",
345+
"configurePreset": "x64-debug-windows"
346+
},
347+
{
348+
"name": "x64-debug-unix",
349+
"displayName": "x64 Debug",
350+
"inherits": "unix-base",
351+
"configurePreset": "x64-debug-unix"
352+
},
353+
{
354+
"name": "x64-release-windows",
355+
"displayName": "x64 Release",
356+
"inherits": "windows-base",
357+
"configurePreset": "x64-release-windows"
358+
},
359+
{
360+
"name": "x64-release-unix",
361+
"displayName": "x64 Release",
362+
"inherits": "unix-base",
363+
"configurePreset": "x64-release-unix"
364+
},
365+
{
366+
"name": "x86-debug-windows",
367+
"displayName": "x86 Debug",
368+
"inherits": "windows-base",
369+
"configurePreset": "x86-debug-windows"
370+
},
371+
{
372+
"name": "x86-debug-unix",
373+
"displayName": "x86 Debug",
374+
"inherits": "unix-base",
375+
"configurePreset": "x86-debug-unix"
376+
},
377+
{
378+
"name": "x86-release-windows",
379+
"displayName": "x86 Release",
380+
"inherits": "windows-base",
381+
"configurePreset": "x86-release-windows"
382+
},
383+
{
384+
"name": "x86-release-unix",
385+
"displayName": "x86 Release",
386+
"inherits": "unix-base",
387+
"configurePreset": "x86-release-unix"
388+
},
389+
{
390+
"name": "arm64-debug-windows",
391+
"displayName": "arm64 Debug",
392+
"inherits": "windows-base",
393+
"configurePreset": "arm64-debug-windows"
394+
},
395+
{
396+
"name": "arm64-debug-unix",
397+
"displayName": "arm64 Debug",
398+
"inherits": "unix-base",
399+
"configurePreset": "arm64-debug-unix"
400+
},
401+
{
402+
"name": "arm64-release-windows",
403+
"displayName": "arm64 Release",
404+
"inherits": "windows-base",
405+
"configurePreset": "arm64-release-windows"
406+
},
407+
{
408+
"name": "arm64-release-unix",
409+
"displayName": "arm64 Release",
410+
"inherits": "unix-base",
411+
"configurePreset": "arm64-release-unix"
412+
}
413+
],
312414
"testPresets": [
313415
{
314416
"name": "base",
@@ -415,7 +517,7 @@
415517
],
416518
"vendor": {
417519
"qt-project.org/Presets": {
418-
"checksum": "Vvury/6B6y8sAEcGfFRgwMMpPdE="
520+
"checksum": "gHC3Ye35P5d7RkRa5FRN6hZz3jo="
419521
}
420522
}
421523
}

include/libbndl/bundle.h

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#pragma once
2+
#ifdef __cplusplus
3+
#include <libbndl/bundle.hpp>
4+
#endif
5+
#include <libbndl/internal/export.h>
6+
#include <stdbool.h>
7+
#include <stdint.h>
8+
9+
#ifdef __has_feature
10+
# define LIBBNDL_HAS_FEATURE(feat) __has_feature(feat)
11+
#else
12+
# define LIBBNDL_HAS_FEATURE(feat) 0
13+
#endif
14+
15+
#if LIBBNDL_HAS_FEATURE(nullability)
16+
# define LIBBNDL_NONNULL _Nonnull
17+
# define LIBBNDL_NULLABLE _Nullable
18+
#else
19+
# define LIBBNDL_NONNULL
20+
# define LIBBNDL_NULLABLE
21+
#endif
22+
23+
#ifdef __cplusplus
24+
extern "C"
25+
{
26+
#endif
27+
typedef struct libbndl_bundle libbndl_bundle;
28+
29+
typedef enum
30+
{
31+
LIBBNDL_ERROR_SUCCESS = 0,
32+
33+
LIBBNDL_ERROR_INVALID_BUNDLE = 1,
34+
LIBBNDL_ERROR_GENERIC_FAILURE = 2,
35+
LIBBNDL_ERROR_RESOURCE_NOT_FOUND = 3,
36+
LIBBNDL_ERROR_DEBUG_DATA_NOT_FOUND = 4,
37+
LIBBNDL_ERROR_OUT_OF_RANGE = 5,
38+
39+
LIBBNDL_ERROR_MEMORY_ALLOCATION = -1,
40+
} libbndl_error;
41+
42+
enum {
43+
#define LIBBNDL_ENUM_MAGIC(_, name, value) LIBBNDL_MAGIC_##name = value,
44+
#include <libbndl/internal/enum.inc>
45+
#undef LIBBNDL_ENUM_MAGIC
46+
};
47+
typedef uint8_t libbndl_magic;
48+
49+
enum
50+
{
51+
#define LIBBNDL_ENUM_PLATFORM(_, name, value) LIBBNDL_PLATFORM_##name = value,
52+
#include <libbndl/internal/enum.inc>
53+
#undef LIBBNDL_ENUM_PLATFORM
54+
};
55+
typedef uint16_t libbndl_platform;
56+
57+
enum
58+
{
59+
#define LIBBNDL_ENUM_FLAGS(_, name, value) LIBBNDL_FLAGS_##name = value,
60+
#include <libbndl/internal/enum.inc>
61+
#undef LIBBNDL_ENUM_FLAGS
62+
};
63+
typedef uint32_t libbndl_flags;
64+
65+
66+
LIBBNDL_EXPORT libbndl_error libbndl_load(libbndl_bundle *LIBBNDL_NULLABLE *LIBBNDL_NONNULL bundle, const char *LIBBNDL_NONNULL path);
67+
LIBBNDL_EXPORT libbndl_error libbndl_create(libbndl_bundle *LIBBNDL_NULLABLE *LIBBNDL_NONNULL bundle, libbndl_magic magic, uint16_t version, libbndl_platform platform, libbndl_flags flags);
68+
LIBBNDL_EXPORT void libbndl_free(libbndl_bundle *LIBBNDL_NULLABLE bundle);
69+
70+
LIBBNDL_EXPORT libbndl_error libbndl_save(libbndl_bundle *LIBBNDL_NONNULL bundle, const char *LIBBNDL_NONNULL path);
71+
72+
LIBBNDL_EXPORT libbndl_magic libbndl_get_magic(const libbndl_bundle *LIBBNDL_NONNULL bundle);
73+
LIBBNDL_EXPORT uint16_t libbndl_get_version(const libbndl_bundle *LIBBNDL_NONNULL bundle);
74+
LIBBNDL_EXPORT libbndl_platform libbndl_get_platform(const libbndl_bundle *LIBBNDL_NONNULL bundle);
75+
LIBBNDL_EXPORT libbndl_flags libbndl_get_flags(const libbndl_bundle *LIBBNDL_NONNULL bundle);
76+
77+
LIBBNDL_EXPORT bool libbndl_is_burnout_era(const libbndl_bundle *LIBBNDL_NONNULL bundle);
78+
LIBBNDL_EXPORT bool libbndl_is_need_for_speed_era(const libbndl_bundle *LIBBNDL_NONNULL bundle);
79+
80+
81+
/* Resource ID */
82+
typedef uint64_t libbndl_resource_id;
83+
84+
enum
85+
{
86+
#define LIBBNDL_ENUM_ID_TYPE(_, name, value) LIBBNDL_RESOURCE_ID_TYPE_##name = value,
87+
#include <libbndl/internal/enum.inc>
88+
#undef LIBBNDL_ENUM_ID_TYPE
89+
};
90+
typedef uint8_t libbndl_resource_id_type;
91+
92+
LIBBNDL_EXPORT libbndl_resource_id libbndl_resource_id_from_name(const char *LIBBNDL_NONNULL name);
93+
LIBBNDL_EXPORT libbndl_resource_id libbndl_resource_id_from_game_changer(uint32_t id, uint16_t resourceType, uint8_t index);
94+
LIBBNDL_EXPORT libbndl_resource_id libbndl_resource_id_from_components(uint32_t id, uint16_t resourceType, uint8_t index, libbndl_resource_id_type idType);
95+
96+
LIBBNDL_EXPORT uint32_t libbndl_resource_id_get_game_changer_id(libbndl_resource_id resourceID);
97+
# define libbndl_resource_id_get_id32(resourceID) libbndl_resource_id_get_game_changer_id(resourceID)
98+
LIBBNDL_EXPORT uint16_t libbndl_resource_id_get_resource_type_id(libbndl_resource_id resourceID);
99+
LIBBNDL_EXPORT uint8_t libbndl_resource_id_get_index(libbndl_resource_id resourceID);
100+
LIBBNDL_EXPORT libbndl_resource_id_type libbndl_resource_id_get_id_type(libbndl_resource_id resourceID);
101+
102+
103+
/* Resource debug data */
104+
typedef struct libbndl_resource_debug_data libbndl_resource_debug_data;
105+
106+
LIBBNDL_EXPORT libbndl_error libbndl_get_resource_debug_data(const libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_resource_debug_data *LIBBNDL_NULLABLE *LIBBNDL_NONNULL debugData, libbndl_resource_id resourceID, uint8_t streamIndex);
107+
108+
LIBBNDL_EXPORT libbndl_error libbndl_resource_debug_data_create(libbndl_resource_debug_data *LIBBNDL_NULLABLE *LIBBNDL_NONNULL debugData, const char *LIBBNDL_NONNULL name, const char *LIBBNDL_NONNULL typeName);
109+
LIBBNDL_EXPORT void libbndl_resource_debug_data_free(libbndl_resource_debug_data *LIBBNDL_NULLABLE debugData);
110+
111+
LIBBNDL_EXPORT libbndl_error libbndl_resource_debug_data_get_name(const libbndl_resource_debug_data *LIBBNDL_NONNULL debugData, char *LIBBNDL_NONNULL buffer, size_t length);
112+
LIBBNDL_EXPORT libbndl_error libbndl_resource_debug_data_get_type_name(const libbndl_resource_debug_data *LIBBNDL_NONNULL debugData, char *LIBBNDL_NONNULL buffer, size_t length);
113+
114+
LIBBNDL_EXPORT void libbndl_add_resource_debug_data(libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_resource_id resourceID, const libbndl_resource_debug_data *LIBBNDL_NONNULL debugData, uint8_t streamIndex);
115+
116+
117+
/* Resource type */
118+
enum
119+
{
120+
#define LIBBNDL_ENUM_RESOURCE_TYPE_BURNOUT(_, name, value) LIBBNDL_RESOURCE_TYPE_BURNOUT_##name = value,
121+
#include <libbndl/internal/enum.inc>
122+
#undef LIBBNDL_ENUM_RESOURCE_TYPE_BURNOUT
123+
};
124+
enum
125+
{
126+
#define LIBBNDL_ENUM_RESOURCE_TYPE_NFS(_, name, value) LIBBNDL_RESOURCE_TYPE_NFS_##name = value,
127+
#include <libbndl/internal/enum.inc>
128+
#undef LIBBNDL_ENUM_RESOURCE_TYPE_NFS
129+
};
130+
typedef uint32_t libbndl_resource_type;
131+
132+
LIBBNDL_EXPORT libbndl_error libbndl_get_resource_type(const libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_resource_type *LIBBNDL_NONNULL resourceType, libbndl_resource_id resourceID, uint8_t streamIndex);
133+
134+
135+
/* Buffer */
136+
typedef struct libbndl_buffer libbndl_buffer;
137+
138+
enum
139+
{
140+
#define LIBBNDL_ENUM_MEMORY_TYPE(_, name, value) LIBBNDL_MEMORY_TYPE_##name = value,
141+
#include <libbndl/internal/enum.inc>
142+
#undef LIBBNDL_ENUM_MEMORY_TYPE
143+
};
144+
typedef uint8_t libbndl_memory_type;
145+
146+
LIBBNDL_EXPORT libbndl_error libbndl_buffer_create(libbndl_buffer *LIBBNDL_NULLABLE *LIBBNDL_NONNULL buffer, const void *LIBBNDL_NONNULL data, size_t size, uint32_t alignment);
147+
LIBBNDL_EXPORT void libbndl_buffer_free(libbndl_buffer *LIBBNDL_NULLABLE buffer);
148+
149+
LIBBNDL_EXPORT libbndl_error libbndl_copy_binary(const libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_buffer *LIBBNDL_NULLABLE *LIBBNDL_NONNULL buffer, libbndl_resource_id resourceID, libbndl_memory_type memoryType, uint8_t streamIndex);
150+
151+
LIBBNDL_EXPORT void *LIBBNDL_NULLABLE libbndl_buffer_get_data(const libbndl_buffer *LIBBNDL_NONNULL buffer);
152+
LIBBNDL_EXPORT size_t libbndl_buffer_get_size(const libbndl_buffer *LIBBNDL_NONNULL buffer);
153+
LIBBNDL_EXPORT uint32_t libbndl_buffer_get_alignment(const libbndl_buffer *LIBBNDL_NONNULL buffer);
154+
155+
156+
/* Import */
157+
typedef struct libbndl_import libbndl_import;
158+
159+
enum
160+
{
161+
#define LIBBNDL_ENUM_IMPORT_TYPE(_, name, value) LIBBNDL_IMPORT_TYPE_##name = value,
162+
#include <libbndl/internal/enum.inc>
163+
#undef LIBBNDL_ENUM_IMPORT_TYPE
164+
};
165+
typedef uint8_t libbndl_import_type;
166+
167+
LIBBNDL_EXPORT libbndl_error libbndl_import_create(libbndl_import *LIBBNDL_NULLABLE *LIBBNDL_NONNULL import, libbndl_resource_id resourceID, uint32_t offset, libbndl_import_type importType);
168+
LIBBNDL_EXPORT void libbndl_import_free(libbndl_import *LIBBNDL_NULLABLE import);
169+
170+
LIBBNDL_EXPORT libbndl_resource_id libbndl_import_get_resource_id(const libbndl_import *LIBBNDL_NONNULL import);
171+
LIBBNDL_EXPORT uint32_t libbndl_import_get_offset(const libbndl_import *LIBBNDL_NONNULL import);
172+
LIBBNDL_EXPORT libbndl_import_type libbndl_import_get_import_type(const libbndl_import *LIBBNDL_NONNULL import);
173+
174+
175+
/* Resource */
176+
typedef struct libbndl_resource libbndl_resource;
177+
178+
LIBBNDL_EXPORT libbndl_error libbndl_resource_create(libbndl_resource *LIBBNDL_NULLABLE *LIBBNDL_NONNULL resource, libbndl_resource_type resourceType);
179+
LIBBNDL_EXPORT void libbndl_resource_free(libbndl_resource *LIBBNDL_NULLABLE resource);
180+
181+
LIBBNDL_EXPORT libbndl_error libbndl_copy_resource(const libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_resource *LIBBNDL_NULLABLE *LIBBNDL_NONNULL resource, libbndl_resource_id resourceID, uint8_t streamIndex);
182+
183+
LIBBNDL_EXPORT libbndl_error libbndl_resource_get_binary_mut(libbndl_resource *LIBBNDL_NONNULL resource, libbndl_buffer *LIBBNDL_NULLABLE *LIBBNDL_NONNULL buffer, libbndl_memory_type memoryType);
184+
LIBBNDL_EXPORT libbndl_error libbndl_resource_get_binary_const(const libbndl_resource *LIBBNDL_NONNULL resource, const libbndl_buffer *LIBBNDL_NULLABLE *LIBBNDL_NONNULL buffer, libbndl_memory_type memoryType);
185+
# define libbndl_resource_get_binary(resource, buffer, memoryType) _Generic((resource), \
186+
const libbndl_resource *: libbndl_resource_get_binary_const, \
187+
libbndl_resource *: libbndl_resource_get_binary_mut)(resource, buffer, memoryType)
188+
189+
LIBBNDL_EXPORT size_t libbndl_resource_get_import_count(const libbndl_resource *LIBBNDL_NONNULL resource);
190+
LIBBNDL_EXPORT libbndl_error libbndl_resource_copy_import(const libbndl_resource *LIBBNDL_NONNULL resource, libbndl_import *LIBBNDL_NULLABLE *LIBBNDL_NONNULL import, size_t index);
191+
192+
LIBBNDL_EXPORT libbndl_error libbndl_resource_replace_binary(libbndl_resource *LIBBNDL_NONNULL resource, const libbndl_buffer *LIBBNDL_NONNULL buffer, libbndl_memory_type memoryType);
193+
LIBBNDL_EXPORT void libbndl_resource_add_import(libbndl_resource *LIBBNDL_NONNULL resource, libbndl_import *LIBBNDL_NONNULL import);
194+
195+
LIBBNDL_EXPORT libbndl_error libbndl_add_resource(libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_resource_id resourceID, const libbndl_resource *LIBBNDL_NONNULL resource, uint8_t streamIndex);
196+
197+
LIBBNDL_EXPORT libbndl_error libbndl_replace_resource(libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_resource_id resourceID, const libbndl_resource *LIBBNDL_NONNULL resource, uint8_t streamIndex);
198+
199+
200+
/* Bundle helpers */
201+
LIBBNDL_EXPORT uint32_t libbndl_get_resource_count(const libbndl_bundle *LIBBNDL_NONNULL bundle);
202+
LIBBNDL_EXPORT libbndl_error libbndl_get_resource_id_at_index(const libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_resource_id *LIBBNDL_NONNULL resourceID, uint32_t index);
203+
204+
# define LIBBNDL_STREAM_MAX_COUNT 4
205+
LIBBNDL_EXPORT bool libbndl_is_populated_resource_stream_index(const libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_resource_id resourceID, uint8_t streamIndex);
206+
207+
LIBBNDL_EXPORT libbndl_resource_id libbndl_get_default_resource_id(const libbndl_bundle *LIBBNDL_NONNULL bundle);
208+
LIBBNDL_EXPORT int32_t libbndl_get_default_resource_stream_index(const libbndl_bundle *LIBBNDL_NONNULL bundle);
209+
210+
# define LIBBNDL_STREAM_NAME_MAX_LENGTH 15
211+
LIBBNDL_EXPORT libbndl_error libbndl_get_stream_name(const libbndl_bundle *LIBBNDL_NONNULL bundle, char *LIBBNDL_NONNULL buffer, size_t length, uint8_t streamIndex);
212+
213+
LIBBNDL_EXPORT bool libbndl_is_valid_memory_type(const libbndl_bundle *LIBBNDL_NONNULL bundle, libbndl_memory_type memoryType);
214+
#ifdef __cplusplus
215+
}
216+
#endif

0 commit comments

Comments
 (0)