nappgui-sys 0.2.0

Rust raw bindings to NAppGUI
Documentation
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
/*
 * NAppGUI Cross-platform C SDK
 * 2015-2025 Francisco Garcia Collado
 * MIT Licence
 * https://nappgui.com/en/legal/license.html
 *
 * File: respack.c
 *
 */

/* Resource Packages */

#include "respack.h"
#include "respackh.h"
#include "arrpt.h"
#include "arrst.h"
#include "buffer.h"
#include "heap.h"
#include "hfile.h"
#include "stream.h"
#include "strings.h"
#include <osbs/bfile.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>

typedef struct i_resource_t i_Resource;

enum i_type_t
{
    i_ekTYPE_EMBEDDED = 0,
    i_ekTYPE_PACKED = 1
};

struct i_resource_t
{
    uint32_t type;
    const byte_t *data;
    uint32_t size;
    void *object;
    FPtr_destroy func_destroy;
};

struct _respack
{
    enum i_type_t type;
    String *name;
    Buffer *buffer;
    ArrSt(i_Resource) *resources;
};

DeclSt(i_Resource);

/*---------------------------------------------------------------------------*/

static void i_init_resource(
    i_Resource *resource,
    const uint32_t type,
    const byte_t *data,
    const uint32_t size,
    void **object)
{
    cassert_no_null(resource);
    resource->type = type;
    resource->data = data;
    resource->size = size;
    resource->object = ptr_dget(object, void);
    resource->func_destroy = NULL;
}

/*---------------------------------------------------------------------------*/

static void i_remove_resource(i_Resource *resource)
{
    cassert_no_null(resource);
    if (resource->object != NULL)
    {
        cassert_no_nullf(resource->func_destroy);
        resource->func_destroy(&resource->object);
    }
}

/*---------------------------------------------------------------------------*/

static ResPack *i_create_respack(const enum i_type_t type, String **name, Buffer **buffer, ArrSt(i_Resource) **resources)
{
    ResPack *pack = heap_new(ResPack);
    pack->type = type;
    pack->name = ptr_dget_no_null(name, String);
    pack->buffer = ptr_dget(buffer, Buffer);
    pack->resources = ptr_dget_no_null(resources, ArrSt(i_Resource));
    return pack;
}

/*---------------------------------------------------------------------------*/

void respack_destroy(ResPack **pack)
{
    cassert_no_null(pack);
    cassert_no_null(*pack);
    str_destroy(&(*pack)->name);
    arrst_destroy(&(*pack)->resources, i_remove_resource, i_Resource);
    if ((*pack)->type == i_ekTYPE_PACKED)
        buffer_destroy(&(*pack)->buffer);
    else
        cassert((*pack)->buffer == NULL);
    heap_delete(pack, ResPack);
}

/*---------------------------------------------------------------------------*/

ResPack *respack_embedded(const char_t *name)
{
    String *lname = str_c(name);
    Buffer *buffer = NULL;
    ArrSt(i_Resource) *resources = arrst_create(i_Resource);
    return i_create_respack(i_ekTYPE_EMBEDDED, &lname, &buffer, &resources);
}

/*---------------------------------------------------------------------------*/

static void i_load_object(Stream *stream, const byte_t **data, uint32_t *size, const uint32_t type)
{
    /* String */
    if (type == 0)
    {
        uint32_t strsize = stm_read_u32(stream);
        *data = stm_buffer(stream);
        *size = 0;
        stm_skip(stream, strsize + 1);
    }
    /* Other file */
    else
    {
        *size = stm_read_u32(stream);
        *data = stm_buffer(stream);
        stm_skip(stream, *size);
    }
}

/*---------------------------------------------------------------------------*/

static void i_load_resource(Stream *stream, const uint32_t locale_code, i_Resource *resource)
{
    uint32_t type;
    const byte_t *data = NULL;
    uint32_t size;
    void *object = NULL;
    uint32_t i, num_localized;
    type = stm_read_u32(stream);
    i_load_object(stream, &data, &size, type);
    num_localized = stm_read_u32(stream);
    for (i = 0; i < num_localized; ++i)
    {
        uint32_t lcode = stm_read_u32(stream);
        if (lcode == locale_code)
        {
            i_load_object(stream, &data, &size, type);
        }
        else
        {
            const byte_t *jump_data = NULL;
            uint32_t jump_size;
            i_load_object(stream, &jump_data, &jump_size, type);
        }
    }

    i_init_resource(resource, type, data, size, &object);
}

/*---------------------------------------------------------------------------*/

static Buffer *i_load_pack(ArrSt(i_Resource) *resources, const char_t *name, const char_t *locale)
{
    String *resfile = NULL;
    Buffer *buffer = NULL;

    {
        String *path;
        char_t pathname[512];
        bfile_dir_exec(pathname, 512);
        str_split_pathname(pathname, &path, NULL);
#if defined(__WINDOWS__)
        resfile = str_printf("%s%cres%c%s.res", tc(path), DIR_SEPARATOR, DIR_SEPARATOR, name);
#elif defined(__MACOS__)
        resfile = str_printf("%s%c..%cresources%c%s.res", tc(path), DIR_SEPARATOR, DIR_SEPARATOR, DIR_SEPARATOR, name);
#elif defined(__IOS__)
        cassert(FALSE);
        resfile = str_printf("%s%c..%cresources%c%s.res", tc(path), DIR_SEPARATOR, DIR_SEPARATOR, DIR_SEPARATOR, name);
#elif defined(__LINUX__)
        resfile = str_printf("%s%cres%c%s.res", tc(path), DIR_SEPARATOR, DIR_SEPARATOR, name);
#endif
        str_destroy(&path);
    }

    buffer = hfile_buffer(tc(resfile), NULL);
    if (buffer != NULL)
    {
        uint32_t locale_code = UINT32_MAX;
        Stream *stream = stm_from_block(buffer_data(buffer), buffer_size(buffer));
        uint32_t num_resources;
        uint32_t i, num_locales = stm_read_u32(stream);
        for (i = 0; i < num_locales; ++i)
        {
            uint32_t locale_size = stm_read_u32(stream);
            if (locale_code == UINT32_MAX)
            {
                const char_t *locale_name = cast_const(stm_buffer(stream), char_t);
                if (str_equ_c(locale, locale_name) == TRUE)
                    locale_code = i;
            }

            stm_skip(stream, locale_size);
        }

        num_resources = stm_read_u32(stream);
        for (i = 0; i < num_resources; ++i)
        {
            i_Resource *resource = arrst_new(resources, i_Resource);
            i_load_resource(stream, locale_code, resource);
        }

        stm_close(&stream);
    }
    else
    {
        cassert_fatal_msg(FALSE, "Resource pack can't be loaded.");
    }

    str_destroy(&resfile);
    return buffer;
}

/*---------------------------------------------------------------------------*/

ResPack *respack_packed(const char_t *name, const char_t *locale)
{
    String *lname = str_c(name);
    ArrSt(i_Resource) *resources = arrst_create(i_Resource);
    Buffer *buffer = i_load_pack(resources, name, locale);
    return i_create_respack(i_ekTYPE_PACKED, &lname, &buffer, &resources);
}

/*---------------------------------------------------------------------------*/

void respack_add_msg(ResPack *pack, const char_t *msg)
{
    i_Resource *resource;
    void *object = NULL;
    cassert_no_null(pack);
    cassert(pack->type == i_ekTYPE_EMBEDDED);
    resource = arrst_new(pack->resources, i_Resource);
    i_init_resource(resource, 0, cast_const(msg, byte_t), UINT32_MAX, &object);
}

/*---------------------------------------------------------------------------*/

void respack_add_cdata(ResPack *pack, const uint32_t type, const byte_t *data, const uint32_t data_size)
{
    i_Resource *resource;
    void *object = NULL;
    cassert_no_null(pack);
    cassert(pack->type == i_ekTYPE_EMBEDDED);
    resource = arrst_new(pack->resources, i_Resource);
    i_init_resource(resource, type, data, data_size, &object);
}

/*---------------------------------------------------------------------------*/

static ___INLINE const char_t *i_magic(const ResId id)
{
    const char_t *magic = str_str(cast_const(id, char_t), "::");
    if (magic == NULL)
        return NULL;
    if (str_equ_cn(cast_const(id, char_t), "N23R3C75", (uint32_t)(magic - cast_const(id, char_t))) == FALSE)
        return NULL;
    if (str_str(magic + 2, "::") == NULL)
        return NULL;
    return magic + 2;
}

/*---------------------------------------------------------------------------*/

static ___INLINE uint32_t i_index(ResId id, const String *name)
{
    const char_t *idr = i_magic(id);
    const char_t *packid = NULL;
    cassert_no_null(idr);
    packid = str_str(idr, "::");
    cassert_no_null(packid);
    cassert_unref(str_equ_cn(tc(name), idr, (uint32_t)(packid - idr)) == TRUE, name);
    return str_to_u32(packid + 2, 10, NULL);
}

/*---------------------------------------------------------------------------*/

const char_t *respack_text(const ResPack *pack, const ResId id)
{
    i_Resource *resource = NULL;
    cassert_no_null(pack);
    resource = arrst_get(pack->resources, i_index(id, pack->name), i_Resource);
    cassert_no_null(resource);
    cassert(resource->type == 0);
    return cast_const(resource->data, char_t);
}

/*---------------------------------------------------------------------------*/

const byte_t *respack_file(const ResPack *pack, const ResId id, uint32_t *size)
{
    i_Resource *resource = NULL;
    cassert_no_null(pack);
    resource = arrst_get(pack->resources, i_index(id, pack->name), i_Resource);
    cassert_no_null(resource);
    cassert(resource->type == 2);
    ptr_assign(size, resource->size);
    return resource->data;
}

/*---------------------------------------------------------------------------*/

void *respack_object_imp(const ResPack *pack, const ResId id, FPtr_from_data func_create, FPtr_destroy func_destroy)
{
    i_Resource *resource = NULL;
    cassert_no_null(pack);
    resource = arrst_get(pack->resources, i_index(id, pack->name), i_Resource);
    cassert_no_null(resource);
    if (resource->object == NULL)
    {
        cassert_no_nullf(func_create);
        cassert_no_nullf(func_destroy);
        cassert(resource->func_destroy == NULL);
        resource->object = func_create(resource->data, resource->size);
        resource->func_destroy = func_destroy;
    }

    return resource->object;
}

/*---------------------------------------------------------------------------*/

static ___INLINE i_Resource *i_resource(const ArrPt(ResPack) *packs, const ResId id, bool_t *is_resid)
{
    const char_t *idr = i_magic(id);
    const char_t *packid = NULL;
    cassert_no_null(is_resid);
    *is_resid = (bool_t)(idr != NULL);

    if (idr == NULL)
        return NULL;

    packid = str_str(idr, "::");
    if (packid != NULL)
    {
        arrpt_foreach_const(pack, packs, ResPack)
            if (str_cmp_cn(tc(pack->name), idr, (uint32_t)(packid - idr)) == 0)
            {
                uint32_t idx;
                idx = str_to_u32(packid + 2, 10, NULL);
                return arrst_get(pack->resources, idx, i_Resource);
            }
        arrpt_end()
    }
    return NULL;
}

/*---------------------------------------------------------------------------*/

const char_t *respack_atext(const ArrPt(ResPack) *packs, const ResId id, bool_t *is_resid)
{
    const i_Resource *resource = i_resource(packs, id, is_resid);
    if (resource != NULL)
    {
        cassert(resource->type == 0);
        return cast_const(resource->data, char_t);
    }
    else
    {
        return NULL;
    }
}

/*---------------------------------------------------------------------------*/

const byte_t *respack_afile(const ArrPt(ResPack) *packs, const ResId id, uint32_t *size, bool_t *is_resid)
{
    i_Resource *resource = i_resource(packs, id, is_resid);
    if (resource != NULL)
    {
        cassert(resource->type == 2);
        ptr_assign(size, resource->size);
        return resource->data;
    }

    ptr_assign(size, 0);
    return NULL;
}

/*---------------------------------------------------------------------------*/

void *respack_aobj_imp(const ArrPt(ResPack) *packs, const ResId id, FPtr_from_data func_create, FPtr_destroy func_destroy, bool_t *is_resid)
{
    i_Resource *resource = i_resource(packs, id, is_resid);
    if (resource != NULL)
    {
        if (resource->object == NULL)
        {
            cassert_no_nullf(func_create);
            cassert_no_nullf(func_destroy);
            cassert(resource->func_destroy == NULL);
            resource->object = func_create(resource->data, resource->size);
            resource->func_destroy = func_destroy;
        }

        return resource->object;
    }

    return NULL;
}