From: Nick Bowler Date: Mon, 28 Mar 2011 22:55:07 +0000 (-0400) Subject: music: Migrate to GOB2. X-Git-Url: https://git.draconx.ca/gitweb/upkg.git/commitdiff_plain/c5c6df2eb7c99ad0a82b20f584920d9018b1d3a2 music: Migrate to GOB2. --- diff --git a/src/engine/.gitignore b/src/engine/.gitignore index b611981..85b5830 100644 --- a/src/engine/.gitignore +++ b/src/engine/.gitignore @@ -1,2 +1,3 @@ palette.[ch] texture.[ch] +music.[ch] diff --git a/src/engine/Makefile.inc b/src/engine/Makefile.inc index 3ffcb33..a5ef99b 100644 --- a/src/engine/Makefile.inc +++ b/src/engine/Makefile.inc @@ -4,9 +4,9 @@ # notice and this notice are preserved. This file is offered as-is, # without any warranty. -noinst_HEADERS += engine/music.h engine/music-module.h +noinst_HEADERS += engine/music-module.h -engine_GOBS = engine/palette.gob engine/texture.gob +engine_GOBS = engine/palette.gob engine/texture.gob engine/music.gob EXTRA_DIST += $(engine_GOBS) $(engine_GOBS:.gob=.gobstamp) noinst_HEADERS += $(engine_GOBS:.gob=.h) @@ -14,8 +14,7 @@ MAINTAINERCLEANFILES += $(engine_GOBS:.gob=.gobstamp) \ $(engine_GOBS:.gob=.c) $(engine_GOBS:.gob=.h) pkglib_LTLIBRARIES += engine.la -engine_la_SOURCES = engine/engine.c engine/music.c \ - $(engine_GOBS:.gob=.c) +engine_la_SOURCES = engine/engine.c $(engine_GOBS:.gob=.c) engine_la_CFLAGS = $(GLIB_CFLAGS) engine_la_LIBADD = $(GLIB_LIBS) engine_la_LDFLAGS = -module -avoid-version -export-symbols-regex _LTX_ diff --git a/src/engine/engine.c b/src/engine/engine.c index 09f248d..3bf27e7 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -28,7 +28,7 @@ int init(GTypeModule *m) { - music_register(m); + engine_music_register_type(m); engine_texture_register_type(m); engine_palette_register_type(m); return 0; diff --git a/src/engine/music.c b/src/engine/music.c deleted file mode 100644 index 345fc35..0000000 --- a/src/engine/music.c +++ /dev/null @@ -1,198 +0,0 @@ -/* - * upkg: tool for manipulating Unreal Tournament packages. - * Copyright © 2009-2011 Nick Bowler - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include - -#include -#include -#include -#include -#include "music-module.h" -#include "upkg.h" - -#define MUSIC_GET_PRIV(o) \ - G_TYPE_INSTANCE_GET_PRIVATE(o, ENGINE_MUSIC_TYPE, struct music_priv) - -struct music_priv { - struct music_mod *mod; - unsigned loaded; -}; - -static void exportable_init(UObjectExportableIface *); -static void loadable_init(UObjectLoadableIface *); - -G_DEFINE_DYNAMIC_TYPE_EXTENDED(EngineMusic, engine_music, U_TYPE_OBJECT, 0, - G_IMPLEMENT_INTERFACE(U_TYPE_OBJECT_EXPORTABLE, exportable_init) - G_IMPLEMENT_INTERFACE(U_TYPE_OBJECT_LOADABLE, loadable_init) -); - -static int load(GObject *o) -{ - struct music_priv *priv = MUSIC_GET_PRIV(o); - struct upkg_file *f = U_OBJECT(o)->pkg_file; - - if (!priv->loaded) { - g_return_val_if_fail(f != NULL, -1); - - if (upkg_export_seek(f, 0, SEEK_SET) != 0) { - return -1; - } - - priv->mod = music_mod_open(f); - if (!priv->mod) { - return -1; - } - } - - priv->loaded++; - return 0; -} - -static void unload(GObject *o) -{ - struct music_priv *priv = MUSIC_GET_PRIV(o); - g_return_if_fail(priv->loaded > 0); - - if (--priv->loaded == 0) { - music_mod_close(priv->mod); - } -} - -static int export(GObject *o, FILE *f) -{ - struct music_priv *priv = MUSIC_GET_PRIV(o); - int rc; - - if (load(o) != 0) - return -1; - - rc = music_mod_dump(priv->mod, f); - - unload(o); - - return rc; -} - -static int export_name(GObject *o, char *buf, size_t n) -{ - struct music_priv *priv = MUSIC_GET_PRIV(o); - const char *type; - int rc; - - if (load(o) != 0) { - if (n > 0) *buf = 0; - return 0; - } - - type = music_mod_type(priv->mod); - rc = snprintf(buf, n, "%s.%s", U_OBJECT(o)->pkg_file->name, type); - - unload(o); - - return rc; -} - -static void exportable_init(UObjectExportableIface *e) -{ - e->export = export; - e->export_name = export_name; -} - -static void loadable_init(UObjectLoadableIface *l) -{ - l->load = load; - l->unload = unload; -} - -static int deserialize(UObject *uo) -{ - struct music_priv *priv = MUSIC_GET_PRIV(uo); - struct upkg_file *f = uo->pkg_file; - size_t rc, pos = 0, buflen; - unsigned char buf[32]; - long size; - - U_OBJECT_CLASS(engine_music_parent_class)->deserialize(uo); - - buflen = upkg_export_read(f, buf, sizeof buf); - - /* Random field #1 */ - if (buflen - pos < 1) - return -1; - pos += 1; - - if (uo->pkg->version > 61) { - /* Random field #2 */ - if (buflen - pos < 4) - return -1; - pos += 4; - } - - rc = upkg_decode_index(&size, buf+pos, buflen-pos); - if (rc == 0 || size < 0) - return -1; - pos += rc; - - f->base += pos; - f->len = size; - upkg_export_seek(f, 0, SEEK_SET); - - return 0; -} - -void music_register(GTypeModule *m) -{ - engine_music_register_type(m); -} - -static void engine_music_init(EngineMusic *m) -{ - struct music_priv *priv = MUSIC_GET_PRIV(m); - *priv = (struct music_priv){0}; -} - -static void engine_music_finalize(GObject *o) -{ - struct music_priv *priv = MUSIC_GET_PRIV(o); - - if (priv->loaded >= 1) { - priv->loaded = 1; - unload(o); - } - - G_OBJECT_CLASS(engine_music_parent_class)->finalize(o); -} - -static void engine_music_class_init(EngineMusicClass *class) -{ - UObjectClass *uo = U_OBJECT_CLASS(class); - GObjectClass *go = G_OBJECT_CLASS(class); - g_type_class_add_private(class, sizeof (struct music_priv)); - - uo->deserialize = deserialize; - go->finalize = engine_music_finalize; - - music_mod_init(); -} - -static void engine_music_class_finalize(EngineMusicClass *class) -{ - music_mod_exit(); -} diff --git a/src/engine/music.gob b/src/engine/music.gob new file mode 100644 index 0000000..920e23c --- /dev/null +++ b/src/engine/music.gob @@ -0,0 +1,143 @@ +%alltop{ +/* + * upkg: tool for manipulating Unreal Tournament packages. + * Copyright © 2009-2011 Nick Bowler + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +%} + +%{ +#include +#include +#include +#include +#include +#include +%} + +%h{ +#include +%} + +class Engine:Music from U:Object (dynamic) + (interface U:Object:Exportable) + (interface U:Object:Loadable) +{ + private struct music_mod *mod = NULL; + private unsigned loaded = 0; + + interface U:Object:Loadable + private int load(G:Object *go) + { + struct upkg_file *f = U_OBJECT(go)->pkg_file; + Self *self = SELF(go); + + if (!self->_priv->loaded) { + g_return_val_if_fail(f != NULL, -1); + + if (upkg_export_seek(f, 0, SEEK_SET) != 0) + return -1; + + self->_priv->mod = music_mod_open(f); + if (!self->_priv->mod) + return -1; + } + + self->_priv->loaded++; + return 0; + } + + interface U:Object:Loadable + private void unload(G:Object *go) + { + Self *self = SELF(go); + + g_return_if_fail(self->_priv->loaded > 0); + + if (--self->_priv->loaded == 0) + music_mod_close(self->_priv->mod); + } + + interface U:Object:Exportable + private int export(G:Object *go, FILE *f) + { + Self *self = SELF(go); + int rc; + + if (self_load(go) != 0) + return -1; + + rc = music_mod_dump(self->_priv->mod, f); + + self_unload(go); + return rc; + } + + interface U:Object:Exportable + private int export_name(G:Object *go, char *buf, size_t n) + { + Self *self = SELF(go); + const char *type; + int rc; + + if (self_load(go) != 0) { + if (n > 0) + buf[0] = '\0'; + return 0; + } + + type = music_mod_type(self->_priv->mod); + rc = snprintf(buf, n, "%s.%s", U_OBJECT(go)->pkg_file->name, type); + + self_unload(go); + return rc; + } + + override (U:Object) int deserialize(U:Object *uo) + { + struct upkg_file *f = uo->pkg_file; + Self *self = SELF(uo); + size_t rc, pos, buflen; + unsigned char buf[32]; + long size; + + PARENT_HANDLER(uo); + + buflen = upkg_export_read(f, buf, sizeof buf); + + /* Unknown field #1 */ + if (buflen - pos < 1) + return -1; + pos += 1; + + if (uo->pkg->version > 61) { + /* Unknown field #2 */ + if (buflen - pos < 4) + return -1; + pos += 4; + } + + rc = upkg_decode_index(&size, buf+pos, buflen-pos); + if (rc == 0 || size < 0) + return -1; + pos += rc; + + f->base += pos; + f->len = size; + upkg_export_seek(f, 0, SEEK_SET); + + return 0; + } +} diff --git a/src/engine/music.h b/src/engine/music.h deleted file mode 100644 index f74c1ab..0000000 --- a/src/engine/music.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * upkg: tool for manipulating Unreal Tournament packages. - * Copyright © 2009-2011 Nick Bowler - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef MUSIC_H_ -#define MUSIC_H_ - -#include -#include - -#define ENGINE_MUSIC_TYPE (engine_music_get_type()) -#define ENGINE_MUSIC(obj) \ - G_TYPE_CHECK_INSTANCE_CAST(obj, ENGINE_MUSIC_TYPE, EngineMusic) -#define ENGINE_MUSIC_CLASS(class) \ - G_TYPE_CHECK_CLASS_CAST(class, ENGINE_MUSIC_TYPE, EngineMusicClass) -#define ENGINE_IS_MUSIC(obj) \ - G_TYPE_CHECK_INSTANCE_TYPE(obj, ENGINE_MUSIC_TYPE) -#define ENGINE_IS_MUSIC_CLASS(class) \ - G_TYPE_CHECK_CLASS_TYPE(class, ENGINE_MUSIC_TYPE, EngineMusicClass) - -typedef struct EngineMusic EngineMusic; -typedef struct EngineMusicClass EngineMusicClass; - -struct EngineMusic { - UObject parent; -}; - -struct EngineMusicClass { - UObjectClass parent; -}; - -GType engine_music_get_type(void); -void music_register(GTypeModule *m); - -#endif