Exemplo n.º 1
0
    static void COM_AddGameDirectory(string dir)
    {
        com_gamedir = dir;

        //
        // add the directory to the search path
        //
        com_searchpaths.Insert(0, new searchpath_t(dir));

        //
        // add any pak files in the format pak0.pak pak1.pak, ...
        //
        for (int i = 0; ; i++)
        {
            string pakfile = String.Format("{0}/pak{1}.pak", dir, i);
            pack_t pak     = COM_LoadPackFile(pakfile);
            if (pak == null)
            {
                break;
            }

            com_searchpaths.Insert(0, new searchpath_t(pak));
        }
    }
Exemplo n.º 2
0
    static int COM_FindFile(string filename, out DisposableWrapper <BinaryReader> file, bool duplicateStream)
    {
        file = null;

        string cachepath = String.Empty;

        //
        // search through the path, one element at a time
        //
        foreach (searchpath_t sp in com_searchpaths)
        {
            // is the element a pak file?
            if (sp.pack != null)
            {
                // look through all the pak file elements
                pack_t pak = sp.pack;
                foreach (packfile_t pfile in pak.files)
                {
                    if (pfile.name.Equals(filename))
                    {
                        // found it!
                        Con_DPrintf("PackFile: {0} : {1}\n", sp.pack.filename, filename);
                        if (duplicateStream)
                        {
                            FileStream pfs = (FileStream)pak.stream.BaseStream;
                            FileStream fs  = new FileStream(pfs.Name, FileMode.Open, FileAccess.Read, FileShare.Read);
                            file = new DisposableWrapper <BinaryReader>(new BinaryReader(fs, Encoding.ASCII), true);
                        }
                        else
                        {
                            file = new DisposableWrapper <BinaryReader>(pak.stream, false);
                        }

                        file.Object.BaseStream.Seek(pfile.filepos, SeekOrigin.Begin);
                        return(pfile.filelen);
                    }
                }
            }
            else
            {
                // check a file in the directory tree
                if (!static_registered)
                {
                    // if not a registered version, don't ever go beyond base
                    if (filename.IndexOfAny(_Slashes) != -1) // strchr (filename, '/') || strchr (filename,'\\'))
                    {
                        continue;
                    }
                }

                string   netpath  = sp.filename + "/" + filename; //sprintf (netpath, "%s/%s",search->filename, filename);
                DateTime findtime = Sys_FileTime(netpath);
                if (findtime == DateTime.MinValue)
                {
                    continue;
                }

                // see if the file needs to be updated in the cache
                if (String.IsNullOrEmpty(com_cachedir)) // !com_cachedir[0])
                {
                    cachepath = netpath;                //  strcpy(cachepath, netpath);
                }
                else
                {
                    if (IsWindows)
                    {
                        if (netpath.Length < 2 || netpath[1] != ':')
                        {
                            cachepath = com_cachedir + netpath;
                        }
                        else
                        {
                            cachepath = com_cachedir + netpath.Substring(2);
                        }
                    }
                    else
                    {
                        cachepath = com_cachedir + netpath;
                    }

                    DateTime cachetime = Sys_FileTime(cachepath);
                    if (cachetime < findtime)
                    {
                        COM_CopyFile(netpath, cachepath);
                    }
                    netpath = cachepath;
                }

                Con_DPrintf("FindFile: {0}\n", netpath);
                FileStream fs = Sys_FileOpenRead(netpath);
                if (fs == null)
                {
                    file = null;
                    return(-1);
                }
                file = new DisposableWrapper <BinaryReader>(new BinaryReader(fs, Encoding.ASCII), true);
                return((int)fs.Length);
            }
        }

        Con_DPrintf("FindFile: can't find {0}\n", filename);
        return(-1);
    }
Exemplo n.º 3
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);
    }
Exemplo n.º 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;
            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;
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }