]> git.draconx.ca Git - upkg.git/blob - src/engine/palette.gob
Stop using gnulib's flexmember module.
[upkg.git] / src / engine / palette.gob
1 %alltop{
2 /*
3  * upkg: tool for manipulating Unreal Tournament packages.
4  * Copyright © 2011, 2020, 2022 Nick Bowler
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 %}
20
21 %ctop{
22 #include <config.h>
23 %}
24
25 %{
26 #include <stdio.h>
27 #include <stdlib.h>
28 %}
29
30 %h{
31 #include <uobject/uobject.h>
32
33 /* Hack to work around broken type parsing in GOB2. */
34 typedef unsigned char engine_palette_rgba[4];
35 %}
36
37 class Engine:Palette from U:Object (dynamic)
38 {
39         public unsigned entries = 0;
40         public engine_palette_rgba *rgba = NULL destroywith free;
41
42         override (U:Object) int deserialize(U:Object *uo)
43         {
44                 struct upkg_file *f = uo->pkg_file;
45                 Self *self = SELF(uo);
46                 unsigned char buf[16];
47                 size_t rc, buflen;
48                 long entries;
49
50                 PARENT_HANDLER(uo);
51
52                 buflen = upkg_export_read(f, buf, sizeof buf);
53                 rc = upkg_decode_index(&entries, buf, buflen);
54                 if (rc == 0 || entries < 0)
55                         return -1;
56                 self->entries = entries;
57
58                 if (upkg_export_seek(f, rc, SEEK_SET) != 0)
59                         return -1;
60                 buflen = entries * sizeof self->rgba[0];
61                 self->rgba = malloc(buflen);
62                 if (!self->rgba)
63                         return -1;
64
65                 rc = upkg_export_read(f, self->rgba, buflen);
66                 if (rc != buflen) {
67                         free(self->rgba);
68                         return -1;
69                 }
70
71                 return 0;
72         }
73 }