]> git.draconx.ca Git - upkg.git/blobdiff - src/engine/music.c
Implement support for libmodplug in Engine.Music.
[upkg.git] / src / engine / music.c
index f770662995e8f63e2ea359117b005850783b7fc6..c562911af25aee00772452674e2565b403e58024 100644 (file)
@@ -5,6 +5,7 @@
 #include "exportable.h"
 #include "loadable.h"
 #include "uobject.h"
+#include "music-module.h"
 #include "music.h"
 #include "upkg.h"
 
@@ -13,6 +14,7 @@
 
 struct music_priv {
        struct upkg_file *f;
+       struct music_mod *mod;
        unsigned loaded;
 };
 
@@ -34,6 +36,11 @@ static int load(GObject *o)
                if (upkg_export_seek(priv->f, 0, SEEK_SET) != 0) {
                        return -1;
                }
+
+               priv->mod = music_mod_open(priv->f);
+               if (!priv->mod) {
+                       return -1;
+               }
        }
 
        priv->loaded++;
@@ -45,35 +52,43 @@ static void unload(GObject *o)
        struct music_priv *priv = MUSIC_GET_PRIV(o);
        g_return_if_fail(priv->loaded > 0);
 
-       --priv->loaded;
+       if (--priv->loaded == 0) {
+               music_mod_close(priv->mod);
+       }
 }
 
 static int export(GObject *o, FILE *f)
 {
        struct music_priv *priv = MUSIC_GET_PRIV(o);
-       unsigned char buf[1024];
+       int rc;
 
-       if (!priv->f || upkg_export_seek(priv->f, 0, SEEK_SET) != 0)
+       if (load(o) != 0)
                return -1;
 
-       while (!priv->f->eof) {
-               size_t rc = upkg_export_read(priv->f, buf, sizeof buf);
-               if (rc == 0) {
-                       if (priv->f->eof) break;
-                       return -1;
-               }
+       rc = music_mod_dump(priv->mod, f);
 
-               if (fwrite(buf, 1, rc, f) != rc)
-                       return -1;
-       }
+       unload(o);
 
-       return 0;
+       return rc;
 }
 
 static int export_name(GObject *o, char *buf, size_t n)
 {
        struct music_priv *priv = MUSIC_GET_PRIV(o);
-       return snprintf(buf, n, "%s", priv->f ? priv->f->name : "");
+       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", priv->f->name, type);
+
+       unload(o);
+
+       return rc;
 }
 
 static void exportable_init(UObjectExportable *e)
@@ -91,7 +106,6 @@ static void loadable_init(UObjectLoadable *l)
 static int deserialize(UObject *o, struct upkg_file *f)
 {
        struct music_priv *priv = MUSIC_GET_PRIV(o);
-       EngineMusic *m = ENGINE_MUSIC(o);
        size_t rc, pos = 0, buflen;
        unsigned char buf[32];
        long size;
@@ -120,7 +134,6 @@ static int deserialize(UObject *o, struct upkg_file *f)
        f->base += pos;
        f->len   = size;
        upkg_export_seek(f, 0, SEEK_SET);
-
        priv->f = f;
 
        return 0;
@@ -155,8 +168,11 @@ static void engine_music_class_init(EngineMusicClass *class)
 
        uo->deserialize = deserialize;
        go->finalize    = engine_music_finalize;
+
+       music_mod_init();
 }
 
 static void engine_music_class_finalize(EngineMusicClass *class)
 {
+       music_mod_exit();
 }