コード例 #1
0
ファイル: common.cs プロジェクト: sbrown345/quakejs
        /*
        =================
        COM_LoadPackFile

        Takes an explicit (not game tree related) path to a pak file.

        Loads the header and directory, adding the files at the beginning
        of the list so they override previous pack files.
        =================
        */
        static pack_t COM_LoadPackFile(string packfile)
        {
	        dpackheader_t           header = new dpackheader_t();
	        int                     i;
	        packfile_t[]            newfiles;
	        int                     numpackfiles;
	        pack_t                  pack = null;
	        int                     packhandle = -1;
	        dpackfile_t[]           info = new dpackfile_t[MAX_FILES_IN_PACK];
	        ushort                  crc;

            int     kk;
            Uint8Array  buf;
            int     ofs;

	        if (sys_linux.Sys_FileOpenRead (packfile, ref packhandle) == -1)
                return null;
            buf = new Uint8Array(sizeof_dpackheader_t);
            sys_linux.Sys_FileRead(packhandle, buf, buf.Length);
            ofs = 0;
            header.id = parseString(buf, ref ofs, 4);
	        if (header.id != "PACK")
		        sys_linux.Sys_Error (packfile + " is not a packfile");
            header.dirofs = parseInt(buf, ref ofs);
            header.dirlen = parseInt(buf, ref ofs);

            numpackfiles = header.dirlen / sizeof_dpackfile_t;

            if (numpackfiles > MAX_FILES_IN_PACK)
                sys_linux.Sys_Error (packfile + " has " + numpackfiles + " files");

            if (numpackfiles != PAK0_COUNT)
                com_modified = true;    // not the original file

            newfiles = new packfile_t[numpackfiles];
            for(kk = 0; kk < numpackfiles; kk++) newfiles[kk] = new packfile_t();

            sys_linux.Sys_FileSeek(packhandle, header.dirofs);
            buf = new Uint8Array(header.dirlen);
            sys_linux.Sys_FileRead (packhandle, buf, header.dirlen);
            ofs = 0;
            for (kk = 0; kk < numpackfiles; kk++)
            {
                info[kk] = new dpackfile_t();
                info[kk].name = parseString(buf, ref ofs, 56);
                info[kk].filepos = parseInt(buf, ref ofs);
                info[kk].filelen = parseInt(buf, ref ofs);
            }


        // crc the directory to check for modifications
            /*CRC_Init (&crc);
            for (i=0 ; i<header.dirlen ; i++)
                CRC_ProcessByte (&crc, ((byte *)info)[i]);
            if (crc != PAK0_CRC)
                com_modified = true;*/

        // parse the directory
            for (i=0 ; i<numpackfiles ; i++)
            {
                newfiles[i].name = info[i].name;
                newfiles[i].filepos = info[i].filepos;
                newfiles[i].filelen = info[i].filelen;
            }

            pack = new pack_t();
            pack.filename = packfile;
            pack.handle = packhandle;
            pack.numfiles = numpackfiles;
            pack.files = newfiles;
        	
            /*Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);*/
	        return pack;
        }
コード例 #2
0
    public static pack_t COM_LoadPackFile(string packfile)
    {
        FileStream file = Sys_FileOpenRead(packfile);

        if (file == null)
        {
            return(null);
        }

        dpackheader_t header = ReadStructure <dpackheader_t>(file);

        string id = Encoding.ASCII.GetString(header.id);

        if (id != "PACK")
        {
            Sys_Error("{0} is not a packfile", packfile);
        }

        header.dirofs = LittleLong(header.dirofs);
        header.dirlen = LittleLong(header.dirlen);

        int numpackfiles = header.dirlen / Marshal.SizeOf(typeof(dpackfile_t));

        if (numpackfiles > q_shared.MAX_FILES_IN_PACK)
        {
            Sys_Error("{0} has {1} files", packfile, numpackfiles);
        }

        //if (numpackfiles != PAK0_COUNT)
        //    _IsModified = true;    // not the original file

        file.Seek(header.dirofs, SeekOrigin.Begin);
        byte[] buf = new byte[header.dirlen];
        if (file.Read(buf, 0, buf.Length) != buf.Length)
        {
            Sys_Error("{0} buffering failed!", packfile);
        }
        List <dpackfile_t> info   = new List <dpackfile_t>(q_shared.MAX_FILES_IN_PACK);
        GCHandle           handle = GCHandle.Alloc(buf, GCHandleType.Pinned);

        try
        {
            IntPtr ptr = handle.AddrOfPinnedObject();
            int    count = 0, structSize = Marshal.SizeOf(typeof(dpackfile_t));
            while (count < header.dirlen)
            {
                dpackfile_t tmp = (dpackfile_t)Marshal.PtrToStructure(ptr, typeof(dpackfile_t));
                info.Add(tmp);
                ptr    = new IntPtr(ptr.ToInt64() + structSize);
                count += structSize;
            }
            if (numpackfiles != info.Count)
            {
                Sys_Error("{0} directory reading failed!", packfile);
            }
        }
        finally
        {
            handle.Free();
        }


        // crc the directory to check for modifications
        //ushort crc;
        //CRC.Init(out crc);
        //for (int i = 0; i < buf.Length; i++)
        //    CRC.ProcessByte(ref crc, buf[i]);
        //if (crc != PAK0_CRC)
        //    _IsModified = true;

        buf = null;

        // parse the directory
        packfile_t[] newfiles = new packfile_t[numpackfiles];
        for (int i = 0; i < numpackfiles; i++)
        {
            packfile_t pf = new packfile_t();
            pf.name     = GetString(info[i].name);
            pf.filepos  = LittleLong(info[i].filepos);
            pf.filelen  = LittleLong(info[i].filelen);
            newfiles[i] = pf;
        }

        pack_t pack = new pack_t(packfile, new BinaryReader(file, Encoding.ASCII), newfiles);

        Con_Printf("Added packfile {0} ({1} files)\n", packfile, numpackfiles);
        return(pack);
    }
コード例 #3
0
ファイル: common.cs プロジェクト: weimingtom/quakelight
        /*
         * =================
         * COM_LoadPackFile
         *
         * Takes an explicit (not game tree related) path to a pak file.
         *
         * Loads the header and directory, adding the files at the beginning
         * of the list so they override previous pack files.
         * =================
         */
        static pack_t COM_LoadPackFile(string packfile)
        {
            dpackheader_t header = new dpackheader_t();
            int           i;

            packfile_t[] newfiles;
            int          numpackfiles;
            pack_t       pack       = null;
            int          packhandle = -1;

            dpackfile_t[] info = new dpackfile_t[MAX_FILES_IN_PACK];
            ushort        crc;

            int kk;

            byte[] buf;
            int    ofs;

            if (sys_linux.Sys_FileOpenRead(packfile, ref packhandle) == -1)
            {
                return(null);
            }
            buf = new byte[sizeof_dpackheader_t];
            sys_linux.Sys_FileRead(packhandle, buf, buf.Length);
            ofs       = 0;
            header.id = parseString(buf, ref ofs, 4);
            if (header.id != "PACK")
            {
                sys_linux.Sys_Error(packfile + " is not a packfile");
            }
            header.dirofs = parseInt(buf, ref ofs);
            header.dirlen = parseInt(buf, ref ofs);

            numpackfiles = header.dirlen / sizeof_dpackfile_t;

            if (numpackfiles > MAX_FILES_IN_PACK)
            {
                sys_linux.Sys_Error(packfile + " has " + numpackfiles + " files");
            }

            if (numpackfiles != PAK0_COUNT)
            {
                com_modified = true;    // not the original file
            }
            newfiles = new packfile_t[numpackfiles];
            for (kk = 0; kk < numpackfiles; kk++)
            {
                newfiles[kk] = new packfile_t();
            }

            sys_linux.Sys_FileSeek(packhandle, header.dirofs);
            buf = new byte[header.dirlen];
            sys_linux.Sys_FileRead(packhandle, buf, header.dirlen);
            ofs = 0;
            for (kk = 0; kk < numpackfiles; kk++)
            {
                info[kk]         = new dpackfile_t();
                info[kk].name    = parseString(buf, ref ofs, 56);
                info[kk].filepos = parseInt(buf, ref ofs);
                info[kk].filelen = parseInt(buf, ref ofs);
            }


            // crc the directory to check for modifications

            /*CRC_Init (&crc);
             * for (i=0 ; i<header.dirlen ; i++)
             *  CRC_ProcessByte (&crc, ((byte *)info)[i]);
             * if (crc != PAK0_CRC)
             *  com_modified = true;*/

            // parse the directory
            for (i = 0; i < numpackfiles; i++)
            {
                newfiles[i].name    = info[i].name;
                newfiles[i].filepos = info[i].filepos;
                newfiles[i].filelen = info[i].filelen;
            }

            pack          = new pack_t();
            pack.filename = packfile;
            pack.handle   = packhandle;
            pack.numfiles = numpackfiles;
            pack.files    = newfiles;

            /*Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);*/
            return(pack);
        }