コード例 #1
0
        /// <summary>
        /// Loads the cache index file.
        /// </summary>
        private void LoadIndex()
        {
            var indexPath = Path.Combine(settings.PhraseCacheFolder, IndexFileName);

            try
            {
                if (!File.Exists(indexPath))
                {
                    // No index exists yet, so create an empty one.

                    SaveIndex(false);
                    return;
                }

                using (var fs = new EnhancedFileStream(indexPath, FileMode.Open))
                {
                    int count;

                    if (fs.ReadInt32() != Magic)
                    {
                        throw new SwitchException("[PhraseCache] cannot read index file [{0}] due to an invalid magic number.  The existing cache will be purged.", indexPath);
                    }

                    count = fs.ReadInt32();
                    for (int i = 0; i < count; i++)
                    {
                        string        text;
                        string        voice;
                        string        actualVoice;
                        PhraseType    phraseType;
                        TtsEncoding   encoding;
                        TtsSampleRate rate;
                        DateTime      lastAccessUtc;
                        string        path;
                        Phrase        phrase;

                        phraseType    = (PhraseType)Enum.Parse(typeof(PhraseType), fs.ReadString16());
                        text          = fs.ReadString32();
                        voice         = fs.ReadString16();
                        actualVoice   = fs.ReadString16();
                        encoding      = (TtsEncoding)Enum.Parse(typeof(TtsEncoding), fs.ReadString16());
                        rate          = (TtsSampleRate)Enum.Parse(typeof(TtsSampleRate), fs.ReadString16());
                        lastAccessUtc = new DateTime(fs.ReadInt64());
                        path          = fs.ReadString16();

                        phrase = new Phrase(phraseType, voice, encoding, rate, text)
                        {
                            Path          = path,
                            ActualVoice   = actualVoice,
                            LastAccessUtc = lastAccessUtc,
                        };

                        index[GetCacheKey(phrase)] = phrase;
                    }
                }
            }
            catch (Exception e)
            {
                // We're going to handle all exceptions leaving the loaded phrase
                // table empty.  This will have the effect of starting the cache
                // from scratch.  Eventually, all of the existing files will be
                // purged and the presumably bad index file will be overwritten.

                index.Clear();
                SysLog.LogException(e);
            }
        }