]> git.draconx.ca Git - upkg.git/blob - src/engine/sound.gob
Improve GOB rebuild rules for VPATH builds.
[upkg.git] / src / engine / sound.gob
1 %alltop{
2 /*
3  *  upkg: tool for manipulating Unreal Tournament packages.
4  *  Copyright © 2011 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 #include <strings.h>
25 #include <uobject/exportable.h>
26 #include "pack.h"
27 %}
28
29 %h{
30 #include <uobject/uobject.h>
31 %}
32
33 class Engine:Sound from U:Object (dynamic)
34         (interface U:Object:Exportable)
35 {
36         interface U:Object:Exportable
37         private int export(U:Object *uo, FILE *outf)
38         {
39                 struct upkg_file *f = uo->pkg_file;
40                 unsigned char buf[1024];
41                 size_t buflen;
42
43                 if (upkg_export_seek(f, 0, SEEK_SET) != 0)
44                         return -1;
45
46                 do {
47                         buflen = upkg_export_read(f, buf, sizeof buf);
48                         if (fwrite(buf, buflen, 1, outf) != 1)
49                                 return -1;
50                 } while (buflen == sizeof buf);
51
52                 if (!f->eof)
53                         return -1;
54                 return 0;
55         }
56
57         interface U:Object:Exportable
58         private int export_name(U:Object *uo, char *buf, size_t n)
59         {
60                 return snprintf(buf, n, "%s.wav", uo->pkg_file->name);
61         }
62
63         override (U:Object) int deserialize(U:Object *uo)
64         {
65                 struct upkg_file *f = uo->pkg_file;
66                 size_t rc, pos = 0, buflen;
67                 unsigned char buf[32];
68                 unsigned long end_offset;
69                 const char *fmt_name;
70                 long size, fmt;
71
72                 PARENT_HANDLER(uo);
73
74                 buflen = upkg_export_read(f, buf, sizeof buf);
75
76                 /* This seems to be the name of the format.  The only format
77                  * that actually gets used is WAV, so we can check that. */
78                 rc = upkg_decode_index(&fmt, buf+pos, buflen-pos);
79                 if (rc == 0)
80                         return -1;
81                 pos += rc;
82
83                 fmt_name = upkg_get_name(f->pkg, fmt);
84                 if (!fmt_name || strcasecmp(fmt_name, "wav") != 0) {
85                         u_err(uo, "format name is not WAV");
86                         return -1;
87                 }
88
89                 /* Like textures, there's an (apparently useless) offset to
90                  * the end of the sound data which is implied by the next
91                  * field.  Just validate it. */
92                 if (f->pkg->version >= 63) {
93                         if (pos + 4 > buflen)
94                                 return -1;
95                         end_offset = unpack_32_le(buf+pos);
96                         pos += 4;
97                 }
98
99                 rc = upkg_decode_index(&size, buf+pos, buflen-pos);
100                 if (rc == 0 || size < 0 || size > f->len - pos)
101                         return -1;
102                 pos += rc;
103
104                 f->base += pos;
105                 f->len   = size;
106                 upkg_export_seek(f, 0, SEEK_SET);
107
108                 if (f->pkg->version >= 63) {
109                         if (end_offset != f->base + f->len) {
110                                 u_err(uo, "end offset does not match size");
111                                 return -1;
112                         }
113                 }
114
115                 return 0;
116         }
117 }