Exemple #1
0
        public DatReader GetReaderForFile(uint fileId)
        {
            if (AllFiles.TryGetValue(fileId, out var file))
            {
                DatReader dr;

                if (stream != null)
                {
                    lock (streamMutex)
                        dr = new DatReader(stream, file.FileOffset, file.FileSize, Header.BlockSize);
                }
                else
                {
                    dr = new DatReader(FilePath, file.FileOffset, file.FileSize, Header.BlockSize);
                }

                return(dr);
            }

            if ((fileId & 0xFFFF) == 0xFFFE) // These are LandBlockInfo objects. Not every landblock has extra info (buildings, etc..)
            {
                log.DebugFormat("Unable to find object_id {0:X8} in {1}", fileId, Enum.GetName(typeof(DatDatabaseType), Header.DataSet));
            }
            else
            {
                log.InfoFormat("Unable to find object_id {0:X8} in {1}", fileId, Enum.GetName(typeof(DatDatabaseType), Header.DataSet));
            }

            return(null);
        }
Exemple #2
0
        public override void Hotload(List <string> changedFiles)
        {
            // TODO smarter hotloading.  Handle removed files.
            var loadedFileNames = new HashSet <string>(AllFiles.Keys);

            foreach (string file in FindConfigsInBaseDir())
            {
                string fileName = GetFileNameFromPath(file);
                loadedFileNames.Remove(fileName);
                if (!AllFiles.TryGetValue(fileName, out var fileInfo))
                {
                    // New file, add it.
                    var newFileInfo = ReadFile(file);
                    AllFiles.Add(newFileInfo.Name, newFileInfo);
                    changedFiles.Add(newFileInfo.Name);
                    continue;
                }

                var fileSize = new FileInfo(file).Length;
                var modified = File.GetLastWriteTimeUtc(file);

                // Timestamp or size need to differ before we bother generating a checksum of the file.
                // Timestamps are considered different if there's at least 1 second between them.
                if (fileSize == fileInfo.Size && Math.Abs((modified - fileInfo.Modified).TotalSeconds) < 1f)
                {
                    continue;
                }

                using (var fileStream = File.OpenRead(file)) {
                    int checksum = Internal.ChecksumUtils.Checksum(fileStream);
                    fileStream.Seek(0, SeekOrigin.Begin);

                    // Update the modified timestamp and file size even if the checksum is the same.
                    // Because we didn't early out a few lines above, we know that at least one of these values
                    // is stale.
                    fileInfo.Modified = modified;
                    fileInfo.Size     = fileSize;

                    if (checksum == fileInfo.Checksum)
                    {
                        // The files are identical.
                        continue;
                    }

                    // File has changed. Hotload it.
                    fileInfo.Checksum = checksum;
                    fileInfo.Modified = modified;
                    fileInfo.Parsed   = Configs.LoadDocFromStream(fileStream, file);

                    changedFiles.Add(fileName);
                }
            }

            foreach (string deletedFile in loadedFileNames)
            {
                AllFiles.Remove(deletedFile);
                changedFiles.Add(deletedFile);
            }
        }
        public DatReader GetReaderForFile(uint fileId)
        {
            if (AllFiles.TryGetValue(fileId, out var file))
            {
                DatReader dr = new DatReader(FilePath, file.FileOffset, file.FileSize, Header.BlockSize);
                return(dr);
            }

            log.InfoFormat("Unable to find object_id {0} in {1}", fileId, Enum.GetName(typeof(DatDatabaseType), Header.DataSet));
            return(null);
        }
Exemple #4
0
        public DatReader GetReaderForFile(uint fileId)
        {
            uint       override_offset;
            uint       override_filesize;
            FileStream override_filestream;

            if (GameChangerServer.ServerPluginCore.DatFileOverride(FilePath, fileId, out override_filestream, out override_offset, out override_filesize))
            {
                if (override_filesize > 0)
                {
                    DatReader dr = new DatReader(override_filestream, override_offset, override_filesize, uint.MaxValue);
                    return(dr);
                }
                else
                {
                    return(null);
                }
            }

            if (AllFiles.TryGetValue(fileId, out var file))
            {
                DatReader dr;

                if (stream != null)
                {
                    lock (streamMutex)
                        dr = new DatReader(stream, file.FileOffset, file.FileSize, Header.BlockSize);
                }
                else
                {
                    dr = new DatReader(FilePath, file.FileOffset, file.FileSize, Header.BlockSize);
                }

                return(dr);
            }

            if ((fileId & 0xFFFF) == 0xFFFE) // These are LandBlockInfo objects. Not every landblock has extra info (buildings, etc..)
            {
                log.DebugFormat("Unable to find object_id {0:X8} in {1}", fileId, Enum.GetName(typeof(DatDatabaseType), Header.DataSet));
            }
            else
            {
                log.InfoFormat("Unable to find object_id {0:X8} in {1}", fileId, Enum.GetName(typeof(DatDatabaseType), Header.DataSet));
            }

            return(null);
        }
Exemple #5
0
        public long FindFile(string name)
        {
            try
            {
                if (AllFiles.TryGetValue(name, out var result))
                {
                    return(result.Ptr);
                }
            }
            catch (KeyNotFoundException)
            {
                const string MESSAGE_FORMAT = "Couldn't find the file in memory: {0}\nTry to restart the game.";
                MessageBox.Show(string.Format(MESSAGE_FORMAT, name), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(1);
            }

            return(0);
        }
Exemple #6
0
        public DatReader GetReaderForFile(uint fileId)
        {
            if (AllFiles.TryGetValue(fileId, out var file))
            {
                DatReader dr;

                if (stream != null)
                {
                    lock (streamMutex)
                        dr = new DatReader(stream, file.FileOffset, file.FileSize, Blocksize);
                }
                else
                {
                    dr = new DatReader(FilePath, file.FileOffset, file.FileSize, Blocksize);
                }

                return(dr);
            }

            return(null);
        }
Exemple #7
0
 public bool TryGetFile(string path, out FileInfo fileInfo)
 {
     return(AllFiles.TryGetValue(Hash(path), out fileInfo));
 }