2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright (C) 2009 Nick Bowler
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <glib-object.h>
24 #include <uobject/uobject.h>
25 #include <uobject/exportable.h>
26 #include <uobject/loadable.h>
27 #include "music-module.h"
31 #define MUSIC_GET_PRIV(o) \
32 G_TYPE_INSTANCE_GET_PRIVATE(o, ENGINE_MUSIC_TYPE, struct music_priv)
36 struct music_mod *mod;
40 static void exportable_init(UObjectExportable *);
41 static void loadable_init(UObjectLoadable *);
43 G_DEFINE_DYNAMIC_TYPE_EXTENDED(EngineMusic, engine_music, U_OBJECT_TYPE, 0,
44 G_IMPLEMENT_INTERFACE(U_OBJECT_TYPE_EXPORTABLE, exportable_init)
45 G_IMPLEMENT_INTERFACE(U_OBJECT_TYPE_LOADABLE, loadable_init)
48 static int load(GObject *o)
50 struct music_priv *priv = MUSIC_GET_PRIV(o);
53 g_return_val_if_fail(priv->f != NULL, -1);
55 if (upkg_export_seek(priv->f, 0, SEEK_SET) != 0) {
59 priv->mod = music_mod_open(priv->f);
69 static void unload(GObject *o)
71 struct music_priv *priv = MUSIC_GET_PRIV(o);
72 g_return_if_fail(priv->loaded > 0);
74 if (--priv->loaded == 0) {
75 music_mod_close(priv->mod);
79 static int export(GObject *o, FILE *f)
81 struct music_priv *priv = MUSIC_GET_PRIV(o);
87 rc = music_mod_dump(priv->mod, f);
94 static int export_name(GObject *o, char *buf, size_t n)
96 struct music_priv *priv = MUSIC_GET_PRIV(o);
105 type = music_mod_type(priv->mod);
106 rc = snprintf(buf, n, "%s.%s", priv->f->name, type);
113 static void exportable_init(UObjectExportable *e)
116 e->export_name = export_name;
119 static void loadable_init(UObjectLoadable *l)
125 static int deserialize(UObject *o, struct upkg_file *f)
127 struct music_priv *priv = MUSIC_GET_PRIV(o);
128 size_t rc, pos = 0, buflen;
129 unsigned char buf[32];
132 U_OBJECT_CLASS(engine_music_parent_class)->deserialize(o, f);
134 buflen = upkg_export_read(f, buf, sizeof buf);
136 /* Random field #1 */
137 if (buflen - pos < 1)
141 if (f->pkg->version > 61) {
142 /* Random field #2 */
143 if (buflen - pos < 4)
148 rc = upkg_decode_index(&size, buf+pos, buflen-pos);
149 if (rc == 0 || size < 0)
155 upkg_export_seek(f, 0, SEEK_SET);
161 void music_register(GTypeModule *m)
163 engine_music_register_type(m);
166 static void engine_music_init(EngineMusic *m)
168 struct music_priv *priv = MUSIC_GET_PRIV(m);
169 *priv = (struct music_priv){0};
172 static void engine_music_finalize(GObject *o)
174 struct music_priv *priv = MUSIC_GET_PRIV(o);
176 if (priv->loaded >= 1) {
181 G_OBJECT_CLASS(engine_music_parent_class)->finalize(o);
184 static void engine_music_class_init(EngineMusicClass *class)
186 UObjectClass *uo = U_OBJECT_CLASS(class);
187 GObjectClass *go = G_OBJECT_CLASS(class);
188 g_type_class_add_private(class, sizeof (struct music_priv));
190 uo->deserialize = deserialize;
191 go->finalize = engine_music_finalize;
196 static void engine_music_class_finalize(EngineMusicClass *class)