misc/libphysfs/archiver_mvl.c
author unc0rr
Fri, 17 May 2013 00:06:01 +0400
changeset 9003 896c6b21c87f
parent 8524 a65e9bcf0a03
child 12213 bb5522e88ab2
permissions -rw-r--r--
Best level AI should maximize the score at all costs.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * MVL support routines for PhysicsFS.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
 * This driver handles Descent II Movielib archives.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 * The file format of MVL is quite easy...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
 *   //MVL File format - Written by Heiko Herrmann
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
 *   char sig[4] = {'D','M', 'V', 'L'}; // "DMVL"=Descent MoVie Library
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
 *   int num_files; // the number of files in this MVL
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
 *   struct {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
 *    char file_name[13]; // Filename, padded to 13 bytes with 0s
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
 *    int file_size; // filesize in bytes
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
 *   }DIR_STRUCT[num_files];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
 *   struct {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
 *    char data[file_size]; // The file data
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
 *   }FILE_STRUCT[num_files];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
 * (That info is from http://www.descent2.com/ddn/specs/mvl/)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
 * Please see the file LICENSE.txt in the source's root directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
 *  This file written by Bradley Bell.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
 *  Based on grp.c by Ryan C. Gordon.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
#if PHYSFS_SUPPORTS_MVL
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
    PHYSFS_uint32 location = 8;  /* sizeof sig. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
    UNPKentry *entries = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
    UNPKentry *entry = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
    entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
    BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
    location += (17 * fileCount);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
    for (entry = entries; fileCount > 0; fileCount--, entry++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
        if (!__PHYSFS_readAll(io, &entry->name, 13)) goto failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
        if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
        entry->size = PHYSFS_swapULE32(entry->size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
        entry->startPos = location;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
        location += entry->size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
    return entries;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
    allocator.Free(entries);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
} /* mvlLoadEntries */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
static void *MVL_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
    PHYSFS_uint8 buf[4];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
    PHYSFS_uint32 count = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
    UNPKentry *entries = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
    assert(io != NULL);  /* shouldn't ever happen. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
    BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 4), ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
    BAIL_IF_MACRO(memcmp(buf, "DMVL", 4) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof(count)), ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
    count = PHYSFS_swapULE32(count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
    entries = mvlLoadEntries(io, count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
    return (!entries) ? NULL : UNPK_openArchive(io, entries, count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
} /* MVL_openArchive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
const PHYSFS_Archiver __PHYSFS_Archiver_MVL =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
        "MVL",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
        "Descent II Movielib format",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
        "Bradley Bell <btb@icculus.org>",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
        "http://icculus.org/physfs/",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
    },
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
    MVL_openArchive,        /* openArchive() method    */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
    UNPK_enumerateFiles,     /* enumerateFiles() method */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
    UNPK_openRead,           /* openRead() method       */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
    UNPK_openWrite,          /* openWrite() method      */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
    UNPK_openAppend,         /* openAppend() method     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
    UNPK_remove,             /* remove() method         */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
    UNPK_mkdir,              /* mkdir() method          */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
    UNPK_closeArchive,       /* closeArchive() method   */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
    UNPK_stat                /* stat() method           */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
#endif  /* defined PHYSFS_SUPPORTS_MVL */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
/* end of mvl.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103