3 * upkg: tool for manipulating Unreal Tournament packages.
4 * Copyright © 2009-2011 Nick Bowler
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.
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.
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/>.
24 #include <uobject/loadable.h>
25 #include <uobject/exportable.h>
26 #include <engine/music-module.h>
31 #include <uobject/uobject.h>
34 class Engine:Music from U:Object (dynamic)
35 (interface U:Object:Exportable)
36 (interface U:Object:Loadable)
38 private struct music_mod *mod = NULL;
39 private unsigned loaded = 0;
41 interface U:Object:Loadable
42 private int load(U:Object *uo)
44 struct upkg_file *f = uo->pkg_file;
45 Self *self = SELF(uo);
47 if (!self->_priv->loaded) {
48 g_return_val_if_fail(f != NULL, -1);
50 if (upkg_export_seek(f, 0, SEEK_SET) != 0)
53 self->_priv->mod = music_mod_open(f);
54 if (!self->_priv->mod)
58 self->_priv->loaded++;
62 interface U:Object:Loadable
63 private void unload(U:Object *uo)
65 Self *self = SELF(uo);
67 g_return_if_fail(self->_priv->loaded > 0);
69 if (--self->_priv->loaded == 0)
70 music_mod_close(self->_priv->mod);
73 interface U:Object:Exportable
74 private int export(U:Object *uo, FILE *f)
76 Self *self = SELF(uo);
79 if (self_load(uo) != 0)
82 rc = music_mod_dump(self->_priv->mod, f);
88 interface U:Object:Exportable
89 private int export_name(U:Object *uo, char *buf, size_t n)
91 Self *self = SELF(uo);
95 if (self_load(uo) != 0) {
101 type = music_mod_type(self->_priv->mod);
102 rc = snprintf(buf, n, "%s.%s", uo->pkg_file->name, type);
108 override (U:Object) int deserialize(U:Object *uo)
110 struct upkg_file *f = uo->pkg_file;
111 Self *self = SELF(uo);
112 size_t rc, pos = 0, buflen;
113 unsigned char buf[32];
118 buflen = upkg_export_read(f, buf, sizeof buf);
120 /* Unknown field #1 */
121 if (buflen - pos < 1)
125 if (f->pkg->version > 61) {
126 /* Unknown field #2 */
127 if (buflen - pos < 4)
132 rc = upkg_decode_index(&size, buf+pos, buflen-pos);
133 if (rc == 0 || size < 0)
139 upkg_export_seek(f, 0, SEEK_SET);