]> git.draconx.ca Git - upkg.git/blob - src/engine/palette.gob
Improve GOB rebuild rules for VPATH builds.
[upkg.git] / src / engine / palette.gob
1 %alltop{
2 /*
3  *  upkg: tool for manipulating Unreal Tournament packages.
4  *  Copyright © 2011, 2020 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 <http://www.gnu.org/licenses/>.
18  */
19 %}
20
21 %{
22 #include <stdio.h>
23 #include <stdlib.h>
24 %}
25
26 %h{
27 #include <uobject/uobject.h>
28
29 /* Hack to work around broken type parsing in GOB2. */
30 typedef unsigned char engine_palette_rgba[4];
31 %}
32
33 class Engine:Palette from U:Object (dynamic)
34 {
35         public unsigned entries = 0;
36         public engine_palette_rgba *rgba = NULL destroywith free;
37
38         override (U:Object) int deserialize(U:Object *uo)
39         {
40                 struct upkg_file *f = uo->pkg_file;
41                 Self *self = SELF(uo);
42                 unsigned char buf[16];
43                 size_t rc, buflen;
44                 long entries;
45
46                 PARENT_HANDLER(uo);
47
48                 buflen = upkg_export_read(f, buf, sizeof buf);
49                 rc = upkg_decode_index(&entries, buf, buflen);
50                 if (rc == 0 || entries < 0)
51                         return -1;
52                 self->entries = entries;
53
54                 if (upkg_export_seek(f, rc, SEEK_SET) != 0)
55                         return -1;
56                 buflen = entries * sizeof self->rgba[0];
57                 self->rgba = malloc(buflen);
58                 if (!self->rgba)
59                         return -1;
60
61                 rc = upkg_export_read(f, self->rgba, buflen);
62                 if (rc != buflen) {
63                         free(self->rgba);
64                         return -1;
65                 }
66
67                 return 0;
68         }
69 }