Пример #1
0
        public ConfigFileInfo TryHotload(ConfigFileInfo finfo)
        {
            var filename = m_baseDir + "/" + finfo.Name;

            filename = System.IO.Path.ChangeExtension(filename, null);
            TextAsset asset = (TextAsset)Resources.Load(filename);

            if (asset == null)
            {
                Config.Log(LogVerbosity.Error, "Null when loading file", filename);
                return(null);
            }
            var contents = asset.text;
            var checksum = ConfigFileManager.Checksum(contents);

            if (checksum == finfo.Checksum)
            {
                // early-out with a false result
                return(null);
            }

            var parsed = Config.LoadDocFromString(contents, finfo.Name);

            return(new ConfigFileInfo {
                Name = finfo.Name,
                Size = contents.Length,
                Checksum = checksum,
                Parsed = parsed
            });
        }
Пример #2
0
        public ConfigFileInfo ReadFile(string fname, string shortName)
        {
            try {
                // for some reason Unity prefers resource names without extensions
                var       filename = System.IO.Path.ChangeExtension(fname, null);
                TextAsset asset    = (TextAsset)Resources.Load(filename);
                if (asset == null)
                {
                    Config.Log(LogVerbosity.Error, "Null loading file", fname);
                    return(null);
                }
                var contents = asset.text;

                var parsed = Config.LoadDocFromString(contents, fname);
                return(new ConfigFileInfo {
                    Name = shortName,
                    Size = contents.Length,
                    Checksum = ConfigFileManager.Checksum(contents),
                    Parsed = parsed
                });
            } catch (Exception e) {
                Config.Log(LogVerbosity.Error, "Exception loading file", fname, e);
                throw e;
            }
        }
Пример #3
0
        public ConfigFileInfo ReadFile(string fname, string shortName)
        {
            try {
                var filename = fname + ".bytes";
                using (var fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                    var checksum = ConfigFileManager.Checksum(fileStream);
                    fileStream.Seek(0, SeekOrigin.Begin);

                    var parsed = Config.LoadDocFromStream(fileStream, fname);
                    return(new ConfigFileInfo {
                        Name = shortName,
                        Size = (int)fileStream.Length,
                        Modified = File.GetLastWriteTimeUtc(filename),
                        Checksum = checksum,
                        Parsed = parsed
                    });
                }
            } catch (Exception e) {
                Config.Log(LogVerbosity.Error, "Exception loading file", fname, e);
                throw e;
            }
        }
Пример #4
0
        public ConfigFileInfo TryHotload(ConfigFileInfo finfo)
        {
            var filename = m_baseDir + "/" + finfo.Name + ".bytes";

            if (!File.Exists(filename))
            {
                return(null);
            }
            var systemInfo   = new System.IO.FileInfo(filename);
            var modifiedTime = File.GetLastWriteTimeUtc(filename);
            var fileLength   = systemInfo.Length;

            if (fileLength == finfo.Size && AreTimestampsEquivalent(modifiedTime, finfo.Modified))
            {
                return(null);
            }

            // size and modified time differ; have to open the whole file to see if it's actually different
            using (var fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                var size     = (int)fileStream.Length;
                var checksum = ConfigFileManager.Checksum(fileStream);
                if (checksum == finfo.Checksum)
                {
                    if (!AreTimestampsEquivalent(modifiedTime, finfo.Modified))
                    {
                        // set the mtime on the file so that we don't have to re-check it later
                        Config.Log(LogVerbosity.Info, "Setting mtime on file", finfo, "prev", modifiedTime, "new", finfo.Modified);
                        try {
                            File.SetLastWriteTimeUtc(filename, finfo.Modified);
                        } catch (Exception e) {
                            Config.Log(LogVerbosity.Info, "Error setting mtime on file", finfo, e.ToString());
                            // if we can't modify the file then let's at least store the mtime in memory for next time
                            finfo.Modified = modifiedTime;
                        }
                    }

                    if (fileLength != finfo.Size)
                    {
                        // for some reason the file's length is different, but the checksum is the same, so let's remember the size so next time we won't have to reload
                        Config.Log(LogVerbosity.Info, "Saving size of file", finfo, "prev", finfo.Size, "new", fileLength);
                        finfo.Size = (int)fileLength;
                    }

                    return(null); // checksum same, can skip parsing/hotloading this file
                }

                fileStream.Seek(0, SeekOrigin.Begin);

                var parsed = Config.LoadDocFromStream(fileStream, finfo.Name);

                var newInfo = new ConfigFileInfo {
                    Name     = finfo.Name,
                    Size     = size,
                    Modified = modifiedTime,
                    Checksum = checksum,
                    Parsed   = parsed
                };
                if (newInfo.Name == "index")
                {
                    HotloadIndex(newInfo);  // index loading should trigger loading other files
                }
                return(newInfo);
            }
        }