Subversion Repositories spk

Rev

Rev 125 | Rev 196 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#ifndef __CATFILE_H__
#define __CATFILE_H__

#include "File.h"
#include "File_IO.h"
#include "lists.h"
#include "spkdll.h"

enum {CATERR_NONE, CATERR_NODATFILE, CATERR_NOCATFILE, CATERR_FILEEMPTY, CATERR_READCAT, CATERR_DECRYPT, CATERR_MISMATCH, CATERR_NOFILE, CATERR_CANTREAD, CATERR_CANTCREATEDIR, CATERR_INVALIDDEST,
                CATERR_CREATED, CATERR_MALLOC};
enum {CATFILE_NONE, CATFILE_READ, CATFILE_DECYPTED};
enum {CATREAD_JUSTCONTENTS, CATREAD_CAT, CATREAD_CATDECRYPT, CATREAD_DAT};

typedef struct SInCatFile {
        SInCatFile () { lSize = 0; sData = 0; lOffset = 0; bDelete = false; bDecrypted = false; }
        Utils::String sFile;
        size_t lSize;
        unsigned char  *sData;
        size_t lOffset;
        bool    bDelete;
        bool    bDecrypted;
} SInCatFile;

unsigned char SPKEXPORT *PCKData ( unsigned char *data, size_t oldsize, size_t *newsize, bool bXor = true );

class SPKEXPORT CCatFile
{
public:
        enum CatFiletype {
                FILETYPE_PLAIN,
                FILETYPE_DEFLATE,
                FILETYPE_PCK,
        };

public:
        static Utils::String PckChangeExtension(const Utils::String &f);
        static Utils::String RenameFileExtension(SInCatFile *f);
        static bool IsAddonDir(const Utils::String &dir);
        static bool CheckPackedExtension(const Utils::String &sFilename);

public:
        CCatFile ();
        ~CCatFile ();

        int  open(const Utils::String &catfile, const Utils::String &addon, int readtype = CATREAD_CATDECRYPT, bool create = true);
        bool DecryptData ();
        bool DecryptData ( unsigned char *data, size_t size );
        void DecryptDAT(unsigned char *buffer, size_t size);
        void DecryptDAT(SInCatFile *pFile);
        bool readFiles ();
        void LoadDatFile ();

        bool checkExtensionPck(const Utils::String &filename) const;

        unsigned char *readData ( CyString filename, size_t *size );
        unsigned char *readData ( SInCatFile *c, size_t *size );
        SInCatFile *FindData(CyString filename);
        SInCatFile *findData(const Utils::String &filename) const;
        bool ReadFileToData ( CyString filename );
        bool ReadFileToData ( SInCatFile *c );

        bool MarkRemoveFile ( SInCatFile *f );
        bool MarkRemoveFile ( CyString filename );

        int GetEndOffset();
        size_t GetNumFiles() const;
        SInCatFile *GetFile(unsigned int num) const;

        unsigned char *UnpackFile ( SInCatFile *c, size_t *size);

        static bool Opened(int error, bool allowCreate = true);
        bool removeFile(SInCatFile *f);
        bool removeFile(const Utils::String &filename);
        bool WriteCatFile ();
        bool AppendFile(const Utils::String &sFilename, const Utils::String &to, bool pck = true, bool bXor = true, Utils::String *sChangeTo = NULL);
        bool AppendData ( unsigned char *data, size_t size, CyString to, bool pck = true, bool bXor = true );
        bool AddData ( CyString catfile, unsigned char *data, size_t size, CyString to, bool pck = true, bool create = true );

        CyString ChangeExtension ( CyString f );
        std::vector<SInCatFile *> *GetFiles() const;

        bool ExtractFile ( CyString filename, CyString to = NullString, bool preserve = false );
        bool ExtractFile ( SInCatFile *f, CyString to = NullString, bool preserve = false );
        bool ExtractAll (CyString dir);

        void clearError () { m_iError = CATERR_NONE; _sError.clear(); }
        int error () const { return m_iError; }
        const Utils::String &errorString() const { return _sError; }
        Utils::String getErrorString () const;

        const Utils::String &internalDatFilename() const;
        const Utils::String &catFilename() const { return m_fCatFile.fullFilename(); }
        const Utils::String &datFilename() const { return m_fDatFile.fullFilename(); }

        bool WriteFromCat ( CyString catfile, CyString file );
        bool WriteFromCat ( CCatFile *fcat, CyString file );

        void FindFiles(CyStringList *list, CyString filemask);


private:
        void _clearFiles();
        int _checkFiletype(const unsigned char *pBuffer, int iSize);
        void RemoveData ();

        CFileIO m_fCatFile;
        CFileIO m_fDatFile;

        unsigned char *m_sData;
        size_t m_lSize;
        char m_iDataType;

        std::vector<SInCatFile *> *_lFiles;
        Utils::String _sError;
        Utils::String _sAddonDir;

        Utils::String _sReadFilename;
        int m_iError;

        bool m_bCreate;
        bool m_bCatChanged;
};

#endif //__CATFILE_H__