Example #1
0
 public static Voicebank ParseCharacterTxt(Stream stream, string filePath, string basePath, Encoding encoding)
 {
     using (var reader = new StreamReader(stream, encoding)) {
         var voicebank = new Voicebank()
         {
             File = filePath,
         };
         var otherLines = new List <string>();
         while (!reader.EndOfStream)
         {
             string line = reader.ReadLine().Trim();
             var    s    = line.Split(new char[] { '=' });
             if (s.Length != 2)
             {
                 s = line.Split(new char[] { ':' });
             }
             Array.ForEach(s, temp => temp.Trim());
             if (s.Length == 2)
             {
                 s[0] = s[0].ToLowerInvariant();
                 if (s[0] == "name")
                 {
                     voicebank.Name = s[1];
                 }
                 else if (s[0] == "image")
                 {
                     voicebank.Image = s[1];
                 }
                 else if (s[0] == "author" || s[0] == "created by")
                 {
                     voicebank.Author = s[1];
                 }
                 else if (s[0] == "sample")
                 {
                 }
                 else if (s[0] == "web")
                 {
                     voicebank.Web = s[1];
                 }
                 else
                 {
                     otherLines.Add(line);
                 }
             }
             else
             {
                 otherLines.Add(line);
             }
         }
         voicebank.OtherInfo = string.Join("\n", otherLines);
         voicebank.Id        = Path.GetRelativePath(basePath, Path.GetDirectoryName(voicebank.File));
         if (string.IsNullOrEmpty(voicebank.Name))
         {
             voicebank.Name = $"No Name ({voicebank.Id})";
         }
         return(voicebank);
     }
 }
Example #2
0
        public static void ParseCharacterConfig(Stream stream, Voicebank bank)
        {
            var bankConfig = VoicebankConfig.Load(stream);

            foreach (var otoSet in bank.OtoSets)
            {
                var subbank = bankConfig.Subbanks.FirstOrDefault(b => b.Dir == otoSet.Name);
                if (subbank != null)
                {
                    otoSet.Prefix = subbank.Prefix;
                    otoSet.Suffix = subbank.Suffix;
                    otoSet.Flavor = subbank.Flavor;
                    if (!string.IsNullOrEmpty(subbank.Prefix))
                    {
                        foreach (var oto in otoSet.Otos)
                        {
                            string phonetic = oto.Alias;
                            if (phonetic.StartsWith(subbank.Prefix))
                            {
                                phonetic = phonetic.Substring(subbank.Prefix.Length);
                            }
                            oto.Phonetic = phonetic;
                        }
                    }
                    if (!string.IsNullOrEmpty(subbank.Suffix))
                    {
                        foreach (var oto in otoSet.Otos)
                        {
                            string phonetic = oto.Phonetic ?? oto.Alias;
                            if (phonetic.EndsWith(subbank.Suffix))
                            {
                                phonetic = phonetic.Substring(0, phonetic.Length - subbank.Suffix.Length);
                            }
                            oto.Phonetic = phonetic;
                        }
                    }
                }
            }
        }
Example #3
0
        public void LoadArchive(string path)
        {
            var encoding = Encoding.GetEncoding(932);

            if (encoding == null)
            {
                throw new Exception($"Failed to detect encoding of {path}.");
            }
            progress.Invoke(0, "Analyzing archive...");
            var readerOptions = new ReaderOptions {
                ArchiveEncoding = new ArchiveEncoding(encoding, encoding)
            };
            var extractionOptions = new ExtractionOptions {
                Overwrite = true,
            };
            var jsonSeriSettings = new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            };
            var    copied        = new string[] { ".bmp", ".jpg", ".gif" };
            var    hashed        = new string[] { ".wav", "_wav.frq" };
            string characterPath = null;

            using (var archive = ArchiveFactory.Open(path, readerOptions)) {
                foreach (var entry in archive.Entries)
                {
                    if (Path.GetFileName(entry.Key) == "character.txt")
                    {
                        characterPath = Path.GetDirectoryName(entry.Key);
                        break;
                    }
                }
            }
            using (var archive = ArchiveFactory.Open(path, readerOptions)) {
                int total = archive.Entries.Count();
                int count = 0;
                foreach (var entry in archive.Entries)
                {
                    progress.Invoke(++count * 100.0 / total, entry.Key);
                    if (entry.IsDirectory)
                    {
                        continue;
                    }
                    if (Path.GetFileName(entry.Key) == "oto.ini")
                    {
                        OtoSet otoSet;
                        using (var streamReader = new StreamReader(entry.OpenEntryStream(), encoding)) {
                            otoSet = ParseOtoSet(streamReader, new FileLoc {
                                file = entry.Key, lineNumber = 0
                            });
                        }
                        otoSet.OrigFile = entry.Key;
                        string filePath = Path.Combine(HashPath(Path.GetDirectoryName(entry.Key)), "_oto.json");
                        otoSet.File = filePath;
                        otoSet.Name = Path.GetDirectoryName(entry.Key).Replace(characterPath, "").Trim(new char[] { '/', '\\' });
                        filePath    = Path.Combine(basePath, filePath);
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        File.WriteAllText(filePath, JsonConvert.SerializeObject(otoSet, Formatting.Indented, jsonSeriSettings));
                        continue;
                    }
                    if (Path.GetFileName(entry.Key) == "character.txt")
                    {
                        Voicebank voicebank = new Voicebank {
                            OrigFile = entry.Key
                        };
                        using (var streamReader = new StreamReader(entry.OpenEntryStream(), encoding)) {
                            var otherLines = new List <string>();
                            while (!streamReader.EndOfStream)
                            {
                                string line = streamReader.ReadLine().Trim();
                                var    s    = line.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                                if (s.Length == 2)
                                {
                                    s[0] = s[0].ToLowerInvariant();
                                    if (s[0] == "name")
                                    {
                                        voicebank.Name = s[1];
                                    }
                                    else if (s[0] == "image")
                                    {
                                        voicebank.Image = s[1];
                                    }
                                    else if (s[0] == "author")
                                    {
                                        voicebank.Author = s[1];
                                    }
                                    else if (s[0] == "web")
                                    {
                                        voicebank.Web = s[1];
                                    }
                                    else
                                    {
                                        otherLines.Add(line);
                                    }
                                }
                                else
                                {
                                    otherLines.Add(line);
                                }
                            }
                            voicebank.OtherInfo = string.Join("\n", otherLines);
                        }
                        if (string.IsNullOrEmpty(voicebank.Name))
                        {
                            throw new FileFormatException(string.Format("Failed to load character.txt using encoding {0}", encoding.EncodingName));
                        }
                        string filePath = Path.Combine(HashPath(Path.GetDirectoryName(entry.Key)), "_voicebank.json");
                        voicebank.File = filePath;
                        filePath       = Path.Combine(basePath, filePath);
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        File.WriteAllText(filePath, JsonConvert.SerializeObject(voicebank, Formatting.Indented, jsonSeriSettings));
                        continue;
                    }
                    if (Path.GetFileName(entry.Key) == "prefix.map")
                    {
                        var prefixMap = new PrefixMap {
                            OrigFile = entry.Key
                        };
                        using (var streamReader = new StreamReader(entry.OpenEntryStream(), encoding)) {
                            while (!streamReader.EndOfStream)
                            {
                                var s = streamReader.ReadLine().Trim().Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                                if (s.Length == 2)
                                {
                                    string source = s[0];
                                    string target = s[1];
                                    prefixMap.Map[source] = target;
                                }
                            }
                        }
                        string filePath = Path.Combine(HashPath(Path.GetDirectoryName(entry.Key)), "_prefix_map.json");
                        prefixMap.File = filePath;
                        filePath       = Path.Combine(basePath, filePath);
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        File.WriteAllText(filePath, JsonConvert.SerializeObject(prefixMap, Formatting.Indented, jsonSeriSettings));
                        continue;
                    }
                    bool handled = false;
                    foreach (var nameEnd in copied)
                    {
                        if (entry.Key.EndsWith(nameEnd))
                        {
                            string dir      = Path.GetDirectoryName(entry.Key);
                            string fileName = Path.GetFileName(entry.Key);
                            string filePath = Path.Combine(basePath, HashPath(dir), fileName);
                            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                            entry.WriteToFile(filePath, extractionOptions);
                            handled = true;
                            break;
                        }
                    }
                    if (handled)
                    {
                        continue;
                    }
                    foreach (var nameEnd in hashed)
                    {
                        if (entry.Key.EndsWith(nameEnd))
                        {
                            string filePath = Path.Combine(basePath, HashPath(entry.Key.Substring(0, entry.Key.Length - nameEnd.Length)) + nameEnd);
                            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                            entry.WriteToFile(filePath, extractionOptions);
                            break;
                        }
                    }
                }
            }
        }
Example #4
0
        public void LoadArchive(string path)
        {
            var encoding = DetectArchiveFileEncoding(path);

            if (encoding == null)
            {
                throw new Exception($"Failed to detect encoding of {path}.");
            }
            progress.Invoke(0, "Analyzing archive...");
            var readerOptions = new ReaderOptions {
                ArchiveEncoding = new ArchiveEncoding(encoding, encoding)
            };
            var extractionOptions = new ExtractionOptions {
                Overwrite = true,
            };
            var jsonSeriSettings = new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            };
            var copied = new string[] { ".bmp", ".jpg", ".gif" };
            var hashed = new string[] { ".wav", "_wav.frq" };

            using (var archive = ArchiveFactory.Open(path, readerOptions)) {
                int total = archive.Entries.Count();
                int count = 0;
                foreach (var entry in archive.Entries)
                {
                    progress.Invoke(++count * 100 / total, entry.Key);
                    if (entry.IsDirectory)
                    {
                        continue;
                    }
                    if (Path.GetFileName(entry.Key) == "oto.ini")
                    {
                        OtoSet otoSet;
                        using (var streamReader = new StreamReader(entry.OpenEntryStream(), encoding)) {
                            otoSet = ParseOtoSet(streamReader);
                        }
                        otoSet.OrigFile = entry.Key;
                        string filePath = Path.Combine(HashPath(Path.GetDirectoryName(entry.Key)), "_oto.json");
                        otoSet.File = filePath;
                        filePath    = Path.Combine(basePath, filePath);
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        File.WriteAllText(filePath, JsonConvert.SerializeObject(otoSet, Formatting.Indented, jsonSeriSettings));
                        continue;
                    }
                    if (Path.GetFileName(entry.Key) == "character.txt")
                    {
                        var     iniParser = new StreamIniDataParser();
                        IniData iniData;
                        using (var streamReader = new StreamReader(entry.OpenEntryStream(), encoding)) {
                            iniData = iniParser.ReadData(streamReader);
                        }
                        Voicebank voicebank = new Voicebank {
                            OrigFile = entry.Key,
                            Name     = iniData.Global["name"],
                            Image    = iniData.Global["image"],
                            Author   = iniData.Global["author"],
                            Web      = iniData.Global["web"],
                        };
                        string filePath = Path.Combine(HashPath(Path.GetDirectoryName(entry.Key)), "_voicebank.json");
                        voicebank.File = filePath;
                        filePath       = Path.Combine(basePath, filePath);
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        File.WriteAllText(filePath, JsonConvert.SerializeObject(voicebank, Formatting.Indented, jsonSeriSettings));
                        continue;
                    }
                    if (Path.GetFileName(entry.Key) == "prefix.map")
                    {
                        var prefixMap = new PrefixMap();
                        prefixMap.OrigFile = entry.Key;
                        using (var streamReader = new StreamReader(entry.OpenEntryStream(), encoding)) {
                            while (!streamReader.EndOfStream)
                            {
                                var s = streamReader.ReadLine().Trim().Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
                                if (s.Length == 2)
                                {
                                    string source = s[0];
                                    string target = s[1];
                                    prefixMap.Map[source] = target;
                                }
                            }
                        }
                        string filePath = Path.Combine(HashPath(Path.GetDirectoryName(entry.Key)), "_prefix_map.json");
                        prefixMap.File = filePath;
                        filePath       = Path.Combine(basePath, filePath);
                        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        File.WriteAllText(filePath, JsonConvert.SerializeObject(prefixMap, Formatting.Indented, jsonSeriSettings));
                        continue;
                    }
                    bool handled = false;
                    foreach (var nameEnd in copied)
                    {
                        if (entry.Key.EndsWith(nameEnd))
                        {
                            string dir      = Path.GetDirectoryName(entry.Key);
                            string fileName = Path.GetFileName(entry.Key);
                            string filePath = Path.Combine(basePath, HashPath(dir), fileName);
                            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                            entry.WriteToFile(filePath, extractionOptions);
                            handled = true;
                            break;
                        }
                    }
                    if (handled)
                    {
                        continue;
                    }
                    foreach (var nameEnd in hashed)
                    {
                        if (entry.Key.EndsWith(nameEnd))
                        {
                            string filePath = Path.Combine(basePath, HashPath(entry.Key.Substring(0, entry.Key.Length - nameEnd.Length)) + nameEnd);
                            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                            entry.WriteToFile(filePath, extractionOptions);
                            break;
                        }
                    }
                }
            }
        }