]> git.draconx.ca Git - upkg.git/blob - src/engine/music.gob
uobject: Make interface functions take UObjects directly.
[upkg.git] / src / engine / music.gob
1 %alltop{
2 /*
3  *  upkg: tool for manipulating Unreal Tournament packages.
4  *  Copyright © 2009-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 <string.h>
24 #include <uobject/loadable.h>
25 #include <uobject/exportable.h>
26 #include <engine/music-module.h>
27 #include <upkg.h>
28 %}
29
30 %h{
31 #include <uobject/uobject.h>
32 %}
33
34 class Engine:Music from U:Object (dynamic)
35         (interface U:Object:Exportable)
36         (interface U:Object:Loadable)
37 {
38         private struct music_mod *mod = NULL;
39         private unsigned loaded = 0;
40
41         interface U:Object:Loadable
42         private int load(U:Object *uo)
43         {
44                 struct upkg_file *f = uo->pkg_file;
45                 Self *self = SELF(uo);
46
47                 if (!self->_priv->loaded) {
48                         g_return_val_if_fail(f != NULL, -1);
49
50                         if (upkg_export_seek(f, 0, SEEK_SET) != 0)
51                                 return -1;
52
53                         self->_priv->mod = music_mod_open(f);
54                         if (!self->_priv->mod)
55                                 return -1;
56                 }
57
58                 self->_priv->loaded++;
59                 return 0;
60         }
61
62         interface U:Object:Loadable
63         private void unload(U:Object *uo)
64         {
65                 Self *self = SELF(uo);
66
67                 g_return_if_fail(self->_priv->loaded > 0);
68
69                 if (--self->_priv->loaded == 0)
70                         music_mod_close(self->_priv->mod);
71         }
72
73         interface U:Object:Exportable
74         private int export(U:Object *uo, FILE *f)
75         {
76                 Self *self = SELF(uo);
77                 int rc;
78
79                 if (self_load(uo) != 0)
80                         return -1;
81
82                 rc = music_mod_dump(self->_priv->mod, f);
83
84                 self_unload(uo);
85                 return rc;
86         }
87
88         interface U:Object:Exportable
89         private int export_name(U:Object *uo, char *buf, size_t n)
90         {
91                 Self *self = SELF(uo);
92                 const char *type;
93                 int rc;
94
95                 if (self_load(uo) != 0) {
96                         if (n > 0)
97                                 buf[0] = '\0';
98                         return 0;
99                 }
100
101                 type = music_mod_type(self->_priv->mod);
102                 rc = snprintf(buf, n, "%s.%s", uo->pkg_file->name, type);
103
104                 self_unload(uo);
105                 return rc;
106         }
107
108         override (U:Object) int deserialize(U:Object *uo)
109         {
110                 struct upkg_file *f = uo->pkg_file;
111                 Self *self = SELF(uo);
112                 size_t rc, pos, buflen;
113                 unsigned char buf[32];
114                 long size;
115
116                 PARENT_HANDLER(uo);
117
118                 buflen = upkg_export_read(f, buf, sizeof buf);
119
120                 /* Unknown field #1 */
121                 if (buflen - pos < 1)
122                         return -1;
123                 pos += 1;
124
125                 if (uo->pkg->version > 61) {
126                         /* Unknown field #2 */
127                         if (buflen - pos < 4)
128                                 return -1;
129                         pos += 4;
130                 }
131
132                 rc = upkg_decode_index(&size, buf+pos, buflen-pos);
133                 if (rc == 0 || size < 0)
134                         return -1;
135                 pos += rc;
136
137                 f->base += pos;
138                 f->len   = size;
139                 upkg_export_seek(f, 0, SEEK_SET);
140
141                 return 0;
142         }
143 }