Beispiel #1
0
        public static Int32 FileLength(String filename)
        {
            searchpath_t search;
            String       netpath;
            pack_t       pak;

            file_from_pak = 0;
            foreach (var link in fs_links)
            {
                if (filename.RegionMatches(0, link.from, 0, link.fromlength, StringComparison.InvariantCulture))
                {
                    netpath = link.to + filename.Substring(link.fromlength);
                    FileInfo file = new FileInfo(netpath);
                    if (file.Attributes == FileAttributes.ReadOnly && file.Attributes == FileAttributes.Normal)
                    {
                        Com.DPrintf("link file: " + netpath + '\\');
                        return(( Int32 )file.Length);
                    }

                    return(-1);
                }
            }

            for (search = fs_searchpaths; search != null; search = search.next)
            {
                if (search.pack != null)
                {
                    pak      = search.pack;
                    filename = filename.ToLower();
                    packfile_t entry = ( packfile_t )pak.files[filename];
                    if (entry != null)
                    {
                        file_from_pak = 1;
                        Com.DPrintf("PackFile: " + pak.filename + " : " + filename + '\\');
                        FileInfo file = new FileInfo(pak.filename);
                        if (file.Attributes != FileAttributes.ReadOnly && file.Attributes != FileAttributes.Normal)
                        {
                            Com.Error(Defines.ERR_FATAL, "Couldn't reopen " + pak.filename);
                        }

                        return(entry.filelen);
                    }
                }
                else
                {
                    netpath = search.filename + '/' + filename;
                    FileInfo file = new FileInfo(netpath);
                    if (file.Attributes != FileAttributes.ReadOnly && file.Attributes != FileAttributes.Normal)
                    {
                        continue;
                    }
                    Com.DPrintf("FindFile: " + netpath + '\\');
                    return(( Int32 )file.Length);
                }
            }

            Com.DPrintf("FindFile: can't find " + filename + '\\');
            return(-1);
        }
Beispiel #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);
    }
Beispiel #3
0
        /*
        =================
        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;
        }
Beispiel #4
0
        /*
         * =================
         * 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);
        }
Beispiel #5
0
        static pack_t LoadPackFile(String packfile)
        {
            dpackheader_t header;
            Hashtable     newfiles;
            FileStream    file;
            var           numpackfiles = 0;
            pack_t        pack         = null;

            try
            {
                file = File.OpenRead(packfile);

                var pb = new Byte[file.Length];
                file.Read(pb, 0, ( Int32 )file.Length);
                ByteBuffer packhandle = ByteBuffer.Wrap(pb);
                packhandle.Order = ByteOrder.LittleEndian;
                file.Close();
                if (packhandle == null || packhandle.Limit < 1)
                {
                    return(null);
                }
                header        = new dpackheader_t();
                header.ident  = packhandle.GetInt32();
                header.dirofs = packhandle.GetInt32();
                header.dirlen = packhandle.GetInt32();
                if (header.ident != IDPAKHEADER)
                {
                    Com.Error(Defines.ERR_FATAL, packfile + " is not a packfile");
                }
                numpackfiles = header.dirlen / packfile_t.SIZE;
                if (numpackfiles > MAX_FILES_IN_PACK)
                {
                    Com.Error(Defines.ERR_FATAL, packfile + " has " + numpackfiles + " files");
                }
                newfiles            = new Hashtable(numpackfiles);
                packhandle.Position = header.dirofs;
                packfile_t entry = null;
                for (var i = 0; i < numpackfiles; i++)
                {
                    packhandle.Get(tmpText);
                    entry         = new packfile_t();
                    entry.name    = Encoding.ASCII.GetString(tmpText).Trim();
                    entry.filepos = packhandle.GetInt32();
                    entry.filelen = packhandle.GetInt32();
                    newfiles.Add(entry.name.ToLower(), entry);
                }
            }
            catch (Exception e)
            {
                Com.DPrintf(e.Message + '\\');
                return(null);
            }

            pack          = new pack_t();
            pack.filename = new String(packfile);
            pack.handle   = file;
            pack.numfiles = numpackfiles;
            pack.files    = newfiles;
            Com.Printf("Added packfile " + packfile + " (" + numpackfiles + " files)\\n");
            return(pack);
        }
Beispiel #6
0
        public static ByteBuffer LoadMappedFile(String filename)
        {
            searchpath_t search;
            String       netpath;
            pack_t       pak;
            FileInfo     file       = null;
            var          fileLength = 0;
            FileStream   input      = null;
            ByteBuffer   buffer     = null;

            file_from_pak = 0;
            try
            {
                foreach (var link in fs_links)
                {
                    if (filename.RegionMatches(0, link.from, 0, link.fromlength, StringComparison.InvariantCulture))
                    {
                        netpath = link.to + filename.Substring(link.fromlength);
                        file    = new FileInfo(netpath);
                        if (file.Attributes == FileAttributes.ReadOnly || file.Attributes == FileAttributes.Normal)
                        {
                            input      = File.OpenRead(file.FullName);
                            fileLength = ( Int32 )file.Length;
                            var b = new Byte[fileLength];
                            input.Read(b, 0, fileLength);
                            buffer = ByteBuffer.Wrap(b);
                            input.Close();
                            return(buffer);
                        }

                        return(null);
                    }
                }

                for (search = fs_searchpaths; search != null; search = search.next)
                {
                    if (search.pack != null)
                    {
                        pak      = search.pack;
                        filename = filename.ToLower();
                        packfile_t entry = ( packfile_t )pak.files[filename];
                        if (entry != null)
                        {
                            file_from_pak = 1;
                            var fileInfo = new FileInfo(pak.filename);
                            if (!(fileInfo.Attributes == FileAttributes.ReadOnly || fileInfo.Attributes == FileAttributes.Normal))
                            {
                                Com.Error(Defines.ERR_FATAL, "Couldn't reopen " + pak.filename);
                            }
                            if (pak.handle == null || !pak.handle.CanRead)
                            {
                                pak.handle = File.OpenRead(pak.filename);
                            }

                            if (pak.backbuffer == null)
                            {
                                var pb = new Byte[pak.handle.Length];
                                pak.handle.Read(pb, 0, ( Int32 )pak.handle.Length);
                                pak.backbuffer = ByteBuffer.Wrap(pb);
                                pak.handle.Dispose();
                            }

                            pak.backbuffer.Position = entry.filepos;
                            buffer       = pak.backbuffer.Slice();
                            buffer.Limit = entry.filelen;
                            return(buffer);
                        }
                    }
                    else
                    {
                        netpath = search.filename + '/' + filename;
                        file    = new FileInfo(netpath);
                        if (file.Attributes != FileAttributes.ReadOnly && file.Attributes != FileAttributes.Normal)
                        {
                            continue;
                        }

                        input      = File.OpenRead(file.FullName);
                        fileLength = ( Int32 )file.Length;
                        var b = new Byte[fileLength];
                        input.Read(b, 0, fileLength);
                        buffer = ByteBuffer.Wrap(b);
                        input.Close();
                        return(buffer);
                    }
                }
            }
            catch (Exception e)
            {
            }

            try
            {
                if (input != null)
                {
                    input.Dispose();
                }
            }
            catch (IOException ioe)
            {
            }

            return(null);
        }
Beispiel #7
0
        public static FileStream FOpenFile(String filename)
        {
            searchpath_t search;
            String       netpath;
            pack_t       pak;
            FileInfo     file = null;

            file_from_pak = 0;
            foreach (var link in fs_links)
            {
                if (filename.RegionMatches(0, link.from, 0, link.fromlength, StringComparison.InvariantCulture))
                {
                    netpath = link.to + filename.Substring(link.fromlength);
                    file    = new FileInfo(netpath);
                    if (file.Attributes == FileAttributes.ReadOnly || file.Attributes == FileAttributes.Normal)
                    {
                        return(File.OpenRead(file.FullName));
                    }

                    return(null);
                }
            }

            for (search = fs_searchpaths; search != null; search = search.next)
            {
                if (search.pack != null)
                {
                    pak      = search.pack;
                    filename = filename.ToLower();
                    packfile_t entry = ( packfile_t )pak.files[filename];
                    if (entry != null)
                    {
                        file_from_pak = 1;
                        file          = new FileInfo(pak.filename);
                        if (file.Attributes != FileAttributes.ReadOnly && file.Attributes != FileAttributes.Normal)
                        {
                            Com.Error(Defines.ERR_FATAL, "Couldn't reopen " + pak.filename);
                        }
                        if (pak.handle == null || !pak.handle.CanRead)
                        {
                            pak.handle = File.OpenRead(pak.filename);
                        }

                        FileStream raf = File.OpenRead(file.FullName);
                        raf.Position = entry.filepos;
                        return(raf);
                    }
                }
                else
                {
                    netpath = search.filename + '/' + filename;
                    file    = new FileInfo(netpath);
                    if (file.Attributes != FileAttributes.ReadOnly && file.Attributes != FileAttributes.Normal)
                    {
                        continue;
                    }
                    return(File.OpenRead(file.FullName));
                }
            }

            return(null);
        }