Skip to content

Commit fcc113e

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

File tree

10 files changed

+1175
-327
lines changed

10 files changed

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

0 commit comments

Comments
 (0)