All pastes #832395 Raw Edit

Someone

public cpp v1 · immutable
#832395 ·published 2007-12-26 04:14 UTC
rendered paste body
/*    TriggerVFS.hpp    CTriggerVfs    Class that fully implements the use of TriggerVFS.dll    Written by iddqd42       iddqd42 (at) gmail (dot) com*/#ifndef IDDQD_CODE#define IDDQD_CODE#endif#ifndef TRIGGER_VFS_DEF#define TRIGGER_VFS_DEF#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <iostream>#include <string>#include <vector>#include <list>#include <algorithm>#include <cstdio>typedef DWORD INDEXHANDLE;typedef DWORD FILEHANDLE;struct FileInfo{    int attribute;    int checksum;};struct FileTree{    DWORD filecount;    char **files;};class CTriggerVFS{    typedef INDEXHANDLE    (__stdcall *EXPORT_OpenVFS)            (const char *idxfile, const char *mode);    typedef DWORD        (__stdcall *EXPORT_CloseVFS)        (INDEXHANDLE handle);    typedef DWORD        (__stdcall *EXPORT_VGetVfsCount)    (INDEXHANDLE handle);    typedef void        (__stdcall *EXPORT_VGetVfsNames)    (INDEXHANDLE handle, char **names, DWORD maxcount, DWORD maxlen);    typedef DWORD        (__stdcall *EXPORT_VGetFileCount)    (INDEXHANDLE handle, const char *vfsfile);    typedef void        (__stdcall *EXPORT_VGetFileNames)    (INDEXHANDLE handle, const char *name, char **files, DWORD &maxfiles, DWORD maxlen);    typedef void        (__stdcall *EXPORT_VGetFileInfo)    (INDEXHANDLE handle, const char *filename, FileInfo *info, DWORD somehandle);    typedef DWORD        (__stdcall *EXPORT_VAddFile)        (INDEXHANDLE handle, const char *insertvfs, const char *localpath, const char *vfspath, DWORD attribute, DWORD checksum, DWORD reserved, DWORD reserved2, DWORD reserved3);    typedef DWORD        (__stdcall *EXPORT_VFileExists)        (INDEXHANDLE handle, const char *filename);    typedef DWORD        (__stdcall *EXPORT_VRemoveFile)        (INDEXHANDLE handle, const char *filename);    typedef WORD        (__stdcall *EXPORT_VGetError)        ();    typedef DWORD        (__stdcall *EXPORT_VGetTotFileCount)(INDEXHANDLE handle);    typedef DWORD        (__stdcall *EXPORT_VGetCurVersion)    (INDEXHANDLE handle);    typedef DWORD        (__stdcall *EXPORT_VAddVfs)            (INDEXHANDLE handle, const char *vfsname);    typedef FILEHANDLE    (__stdcall *EXPORT_VOpenFile)        (const char *path, INDEXHANDLE handle);    typedef void        (__stdcall *EXPORT_VCloseFile)        (FILEHANDLE handle);    typedef DWORD        (__stdcall *EXPORT_VFGetSize)        (FILEHANDLE handle);    typedef DWORD        (__stdcall *EXPORT_VFRead)            (void *buffer, DWORD size, DWORD count, FILEHANDLE handle);    public:        CTriggerVFS(HMODULE dll) : hDll(dll)        {            vfsnames = 0;            vfscount = 0;            handle = 0;            tree = 0;            pOpenVFS            = (EXPORT_OpenVFS)            GetProcAddress(hDll, "_OpenVFS@8");            pCloseVFS            = (EXPORT_CloseVFS)            GetProcAddress(hDll, "_CloseVFS@4");            pGetVfsCount        = (EXPORT_VGetVfsCount)        GetProcAddress(hDll, "_VGetVfsCount@4");            pGetVfsNames        = (EXPORT_VGetVfsNames)        GetProcAddress(hDll, "_VGetVfsNames@16");            pGetFileCount        = (EXPORT_VGetFileCount)    GetProcAddress(hDll, "_VGetFileCount@8");            pGetFileNames        = (EXPORT_VGetFileNames)    GetProcAddress(hDll, "_VGetFileNames@20");            pAddFile            = (EXPORT_VAddFile)            GetProcAddress(hDll, "_VAddFile@36");            pGetFileInfo        = (EXPORT_VGetFileInfo)        GetProcAddress(hDll, "_VGetFileInfo@16");            pFileExists            = (EXPORT_VFileExists)        GetProcAddress(hDll, "_VFileExists@8");            pRemoveFile            = (EXPORT_VRemoveFile)        GetProcAddress(hDll, "_VRemoveFile@8");            pGetError            = (EXPORT_VGetError)        GetProcAddress(hDll, "_VGetError@0");            pGetTotalFileCount    = (EXPORT_VGetTotFileCount)    GetProcAddress(hDll, "_VGetTotFileCount@4");            pGetCurVersion        = (EXPORT_VGetCurVersion)    GetProcAddress(hDll, "_VGetCurVersion@4");            pOpenFile            = (EXPORT_VOpenFile)        GetProcAddress(hDll, "_VOpenFile@8");            pCloseFile            = (EXPORT_VCloseFile)        GetProcAddress(hDll, "_VCloseFile@4");            pGetFileSize        = (EXPORT_VFGetSize)        GetProcAddress(hDll, "_vfgetsize@4");            pReadFile            = (EXPORT_VFRead)            GetProcAddress(hDll, "_vfread@16");            pAddVfs                = (EXPORT_VAddVfs)            GetProcAddress(hDll, "_VAddVfs@8");        }        bool openIndex(const std::string &indexfile, bool readonly)        {            std::string mode;                       mode = readonly ? "r" : "r+";            handle = pOpenVFS(indexfile.c_str(), mode.c_str());            vfscount = pGetVfsCount(handle);            vfsnames = new char *[vfscount];                       for(int i = 0; i < vfscount; i++)                vfsnames[i] = new char [MAX_PATH];            pGetVfsNames(handle, vfsnames, vfscount, MAX_PATH);            vfscount--;            // Remove the damned ROOT.VFS            return (handle != 0);        }        DWORD buildFileTree()        {            DWORD totalfiles = 0;            if(tree)            {                for(int ivfs = 0; ivfs < vfscount; ivfs++)                {                    for(int ifile = 0; ifile < tree[ivfs].filecount; ifile++)                        delete [] tree[ivfs].files[ifile];                    delete [] tree[ivfs].files;                }                delete [] tree;                tree = 0;            }            for(int i = 0; i < vfscount; i++)                delete [] vfsnames[i];                           delete [] vfsnames;                           vfscount = pGetVfsCount(handle);            vfsnames = new char *[vfscount];                       for(int i = 0; i < vfscount; i++)                vfsnames[i] = new char [MAX_PATH];            pGetVfsNames(handle, vfsnames, vfscount, MAX_PATH);            vfscount--;            // Remove the damned ROOT.VFS                       tree = new FileTree[vfscount];            for(int ivfs = 0; ivfs < vfscount; ivfs++)            {                tree[ivfs].filecount = pGetFileCount(handle, vfsnames[ivfs]);                tree[ivfs].files = new char *[tree[ivfs].filecount];                               for(int ifile = 0; ifile < tree[ivfs].filecount; ifile++)                    tree[ivfs].files[ifile] = new char[MAX_PATH];                               pGetFileNames(handle, vfsnames[ivfs], tree[ivfs].files, tree[ivfs].filecount, MAX_PATH);                totalfiles += tree[ivfs].filecount;            }                       return totalfiles;        }        DWORD getCurrentVersion() const        {            return pGetCurVersion(handle);        }        DWORD getTotalFileCount() const        {            return pGetTotalFileCount(handle);        }        DWORD getVfsCount() const        {            return vfscount;        }        void getVfsNames(std::vector<std::string> &vf)        {            vf.clear();            for(int i = 0; i < vfscount; i++)                vf.push_back(vfsnames[i]);        }        bool getFileInfo(const std::string &filename, FileInfo *info)        {            DWORD somehandle = pFileExists(handle, filename.c_str());                       if(somehandle & 0xFF)            {                pGetFileInfo(handle, filename.c_str(), info, somehandle);                return true;            }            return false;        }        DWORD getVfsArchive(const std::string &filename, std::string &invfs)        {            for(int i = 0; i < vfscount; i++)            {                for(int ifile = 0; ifile < tree[i].filecount; ifile++)                {                    if(filename == tree[i].files[ifile])                    {                        invfs.assign(vfsnames[i]);                        return i;                    }                }            }            invfs.clear();            return -1;        }        bool addVfs(const std::string &vfsname, bool create = false)        {            FILE *excist = fopen(vfsname.c_str(), "r");            if(excist == NULL)            {                if(create)                    excist = fopen(vfsname.c_str(), "w");                               if(excist == NULL)                    return false;            }            fclose(excist);                   return (bool)(pAddVfs(handle, vfsname.c_str()));        }        DWORD addFile(const std::string &localpath, const std::string &vfspath, const std::string &tovfs)        {            FileInfo info;                       if(getFileInfo(vfspath, &info))                removeFile(vfspath);            info.attribute = 0x64;                // Meh, it works fine;)            info.checksum = 0;            return pAddFile(handle, tovfs.c_str(), localpath.c_str(), vfspath.c_str(), info.attribute, info.checksum, 0, 0, 0);        }        WORD getLastError()        {            return pGetError();        }               bool removeFile(const std::string &vfspath) const        {            return !pRemoveFile(handle, vfspath.c_str());        }        FileTree *getFileTree() const        {            return tree;        }        DWORD getFileSize(const std::string &filename)        {            FILEHANDLE h = pOpenFile(filename.c_str(), handle);            DWORD size = pGetFileSize(h);            pCloseFile(h);            return size;        }        bool extractFile(const std::string &filename, const char *localfile = NULL)        {            FILE *out;            FILEHANDLE in;            bool allok = true;            std::string writeto;            if(filename.empty())                return false;            if(!localfile)            {                int pos = filename.find_last_of("\\");                pos = (pos == -1) ? 0 : pos;                writeto.assign(filename.substr(pos + 1));            }            else            {                writeto.assign(localfile);            }               if(!(in = pOpenFile(filename.c_str(), handle)))                return false;                       DWORD size = pGetFileSize(in);            char *buffer = new char[size];                           if(pReadFile(buffer, 1, size, in) == size)            {                pCloseFile(in);                if((out = fopen(writeto.c_str(), "wb")) == NULL)                    allok = false;                else                {                    if(fwrite(buffer, 1, size, out) != size)                        allok = false;                               fclose(out);                }                delete [] buffer;                           }            else                allok = false;                       return allok;        }        void closeIndex()        {            pCloseVFS(handle);            handle = 0;        }        ~CTriggerVFS()        {            if(vfsnames)            {                for(int i = 0; i < vfscount; i++)                    delete [] vfsnames[i];                delete [] vfsnames;            }            if(tree)            {                for(int i = 0; i < vfscount; i++)                {                    for(int ifile = 0; ifile < tree[i].filecount; ifile++)                        delete [] tree[i].files[ifile];                    delete [] tree[i].files;                }                delete [] tree;            }        }    private:        FileTree *tree;        DWORD vfscount;        char **vfsnames;        INDEXHANDLE handle;        EXPORT_OpenVFS                pOpenVFS;        EXPORT_CloseVFS                pCloseVFS;        EXPORT_VGetVfsCount            pGetVfsCount;        EXPORT_VGetVfsNames            pGetVfsNames;        EXPORT_VGetFileCount        pGetFileCount;        EXPORT_VGetFileNames        pGetFileNames;        EXPORT_VAddFile                pAddFile;        EXPORT_VRemoveFile            pRemoveFile;        EXPORT_VGetFileInfo            pGetFileInfo;        EXPORT_VFileExists            pFileExists;        EXPORT_VGetError            pGetError;        EXPORT_VGetTotFileCount        pGetTotalFileCount;        EXPORT_VGetCurVersion        pGetCurVersion;        EXPORT_VOpenFile            pOpenFile;        EXPORT_VCloseFile            pCloseFile;        EXPORT_VFGetSize            pGetFileSize;        EXPORT_VFRead                pReadFile;        EXPORT_VAddVfs                pAddVfs;        HMODULE hDll;};#endif