// Wild Magic Source Code
// David Eberly
// http://www.geometrictools.com
// Copyright (c) 1998-2007
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or (at
// your option) any later version.  The license is available for reading at
// either of the locations:
//     http://www.gnu.org/copyleft/lgpl.html
//     http://www.geometrictools.com/License/WildMagicLicense.pdf
//
// Version: 4.4.0 (2007/06/07)

#include "ConvertToVersion403.h"
using namespace Wm4;

WM4_CONSOLE_APPLICATION(ConvertToVersion403);

extern bool gHackStreamRead;

// Modify these global variables as needed for the files you want to
// convert from stream version 4.02 to 4.03.

//const int gs_iNumFiles = 6;
//const char* gs_acName[gs_iNumFiles] =
//{
//    "BrainP.wmof",
//    "Castle.wmof",
//    "FacePN.wmof",
//    "SkinnedBipedPN.wmof",
//    "SmallBrainPT2.wmof",
//    "SkyDome.wmof"
//};

//----------------------------------------------------------------------------
int ConvertToVersion403::Main (int, char**)
{
    for (int i = 0; i < gs_iNumFiles; i++)
    {
        // Check whether the file may be converted.
        FILE* pkIFile = System::Fopen(gs_acName[i], "rb");
        if (!pkIFile)
        {
            // File does not exist.
            continue;
        }

        char acHeader[28];
        size_t iNumRead = fread(acHeader, sizeof(char), 28, pkIFile);
        if (iNumRead != 28 || acHeader[27] != 0)
        {
            // File does not contain the .wmof header.
            fclose(pkIFile);
            continue;
        }

        if (strcmp(acHeader,"Wild Magic Object File 4.02") != 0)
        {
            // You can only convert version 4.02 files to 4.03.  Versions
            // 4.01 and earlier cannot be converted.
            fclose(pkIFile);
            continue;
        }

        fclose(pkIFile);

        // Load the version 4.02 files, using 4-bytes per pointer when
        // reading from disk.
        Stream kStream;
        gHackStreamRead = true;
        kStream.Load(gs_acName[i]);

        // Save the version 4.03 files, using 8-bytes per pointer when
        // writing to disk.
        char acNewName[128];
        System::Sprintf(acNewName,128,"%s.new",gs_acName[i]);
        gHackStreamRead = false;
        kStream.Save(acNewName);
    }
    return 0;
}
//----------------------------------------------------------------------------



