Example #1
0
 public static PrefixMap ParsePrefixMap(Stream stream, string filePath, Encoding encoding)
 {
     using (var reader = new StreamReader(stream, encoding)) {
         var prefixMap = new PrefixMap {
             File = filePath,
         };
         while (!reader.EndOfStream)
         {
             var s = reader.ReadLine().Split('\t');
             if (s.Length == 3)
             {
                 string source = s[0];
                 string prefix = s[1];
                 string suffix = s[2];
                 prefixMap.Map[source] = new Tuple <string, string>(prefix, suffix);
             }
         }
         return(prefixMap);
     }
 }
Example #2
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 #3
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;
                        }
                    }
                }
            }
        }