Exemplo n.º 1
0
        protected override IList <Subtitle> GetSubtitles()
        {
            var result = new List <Subtitle>();

            using (var fs = new FileStream(Path, FileMode.Open))
                using (var input = new ExtendedBinaryReader(fs, FileEncoding))
                {
                    var patternPos = new List <int>();
                    var pos        = input.FindPattern(SearchPattern);
                    while (pos != -1)
                    {
                        patternPos.Add(pos);
                        pos = input.FindPattern(SearchPattern);
                    }

                    if (patternPos.Count > 0)
                    {
                        input.Seek(patternPos[patternPos.Count - 1] + 3, SeekOrigin.Begin);
                        var count = input.ReadByte();

                        for (var i = 0; i < count; i++)
                        {
                            input.Skip(0x1C);
                            var txt = ReadSubtitle(input);
                            if (!string.IsNullOrWhiteSpace(txt.Text))
                            {
                                txt.PropertyChanged += SubtitlePropertyChanged;
                                result.Add(txt);
                            }

                            txt = ReadSubtitle(input);
                            if (!string.IsNullOrWhiteSpace(txt.Text))
                            {
                                txt.PropertyChanged += SubtitlePropertyChanged;
                                result.Add(txt);
                            }
                        }

                        input.Skip(4);
                        var subtitle = ReadSubtitle(input);
                        if (!string.IsNullOrWhiteSpace(subtitle.Text))
                        {
                            subtitle.PropertyChanged += SubtitlePropertyChanged;
                            result.Add(subtitle);
                        }

                        subtitle = ReadSubtitle(input);
                        if (!string.IsNullOrWhiteSpace(subtitle.Text))
                        {
                            subtitle.PropertyChanged += SubtitlePropertyChanged;
                            result.Add(subtitle);
                        }
                    }
                }

            LoadChanges(result);

            return(result);
        }
Exemplo n.º 2
0
        private byte[] Rebuild(byte[] inputBytes, int offsetBase, IList <Subtitle> subtitles)
        {
            using (var fsInput = new MemoryStream(inputBytes))
                using (var input = new ExtendedBinaryReader(fsInput, customEncoding))
                    using (var fsOutput = new MemoryStream())
                        using (var output = new ExtendedBinaryWriter(fsOutput, customEncoding))
                        {
                            int magic = input.ReadInt32();

                            int count         = 0x08;
                            var searchPattern = new byte[] { 0x00, 0x00, 0x00, 0x08 };
                            if (magic == 0x00100187)
                            {
                                count         = 0x0E;
                                searchPattern = new byte[] { 0x00, 0x00, 0x00, 0x0E };
                            }

                            var patternPos = new List <int>();
                            int pos        = input.FindPattern(searchPattern);
                            while (pos != -1)
                            {
                                patternPos.Add(pos);
                                pos = input.FindPattern(searchPattern);
                            }

                            if (patternPos.Count > 0)
                            {
                                input.Seek(0, SeekOrigin.Begin);
                                output.Write(input.ReadBytes(patternPos[patternPos.Count - 1] + 4));

                                for (int i = 0; i < count; i++)
                                {
                                    output.Write(input.ReadBytes(0x1C));
                                    Subtitle txt = ReadSubtitle(input);

                                    Subtitle sub = subtitles.FirstOrDefault(x => x.Offset == txt.Offset + offsetBase);
                                    output.WriteString(sub != null ? sub.Translation : txt.Text);

                                    txt = ReadSubtitle(input);
                                    sub = subtitles.FirstOrDefault(x => x.Offset == txt.Offset + offsetBase);
                                    output.WriteString(sub != null ? sub.Translation : txt.Text);
                                }

                                output.Write(input.ReadBytes(4));
                                Subtitle subtitle = ReadSubtitle(input);
                                Subtitle sub2     = subtitles.FirstOrDefault(x => x.Offset == subtitle.Offset + offsetBase);
                                output.WriteString(sub2 != null ? sub2.Translation : subtitle.Text);

                                subtitle = ReadSubtitle(input);
                                sub2     = subtitles.FirstOrDefault(x => x.Offset == subtitle.Offset + offsetBase);
                                output.WriteString(sub2 != null ? sub2.Translation : subtitle.Text);
                            }

                            return(fsOutput.ToArray());
                        }
        }
Exemplo n.º 3
0
        protected override IList <TF.Core.TranslationEntities.Subtitle> GetSubtitles()
        {
            var temp = new List <Subtitle>();

            using (var fs = new FileStream(Path, FileMode.Open))
                using (var input = new ExtendedBinaryReader(fs, FileEncoding, Endianness.BigEndian))
                {
                    var currentIndex = input.FindPattern(SearchPattern);

                    while (currentIndex != -1)
                    {
                        input.ReadBytes(12); //0x0C

                        var type = input.ReadUInt64();

                        var subtitles = type == 0 ? ReadLongSubtitles(input) : ReadShortSubtitles(input);

                        if (subtitles != null)
                        {
                            temp.AddRange(subtitles);
                        }

                        currentIndex = input.FindPattern(SearchPattern);
                    }
                }

            var englishSubtitles  = new List <Subtitle>();
            var japaneseSubtitles = new List <Subtitle>();

            foreach (var subtitle in temp)
            {
                switch (subtitle.Language)
                {
                case SubtitleLanguage.English:
                    englishSubtitles.Add(subtitle);
                    break;

                case SubtitleLanguage.Japanese:
                    japaneseSubtitles.Add(subtitle);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            var result = new List <TF.Core.TranslationEntities.Subtitle>();

            result.AddRange(englishSubtitles);
            result.AddRange(japaneseSubtitles);

            LoadChanges(result);

            return(result);
        }
Exemplo n.º 4
0
        public override void Rebuild(string outputFolder)
        {
            var outputPath = System.IO.Path.Combine(outputFolder, RelativePath);

            Directory.CreateDirectory(System.IO.Path.GetDirectoryName(outputPath));

            var subtitles = GetSubtitles();

            using (var fsInput = new FileStream(Path, FileMode.Open))
                using (var input = new ExtendedBinaryReader(fsInput, FileEncoding))
                    using (var fsOutput = new FileStream(outputPath, FileMode.Create))
                        using (var output = new ExtendedBinaryWriter(fsOutput, FileEncoding))
                        {
                            var patternPos = new List <int>();
                            var pos        = input.FindPattern(SearchPattern);
                            while (pos != -1)
                            {
                                patternPos.Add(pos);
                                pos = input.FindPattern(SearchPattern);
                            }

                            if (patternPos.Count > 0)
                            {
                                input.Seek(0, SeekOrigin.Begin);
                                output.Write(input.ReadBytes(patternPos[patternPos.Count - 1] + 3));

                                var count = input.ReadByte();
                                output.Write(count);

                                for (var i = 0; i < count; i++)
                                {
                                    output.Write(input.ReadBytes(0x1C));
                                    var txt = ReadSubtitle(input);

                                    var sub = subtitles.FirstOrDefault(x => x.Offset == txt.Offset);
                                    output.WriteString(sub != null ? sub.Translation : txt.Text);

                                    txt = ReadSubtitle(input);
                                    sub = subtitles.FirstOrDefault(x => x.Offset == txt.Offset);
                                    output.WriteString(sub != null ? sub.Translation : txt.Text);
                                }

                                output.Write(input.ReadBytes(4));
                                var subtitle = ReadSubtitle(input);
                                var sub2     = subtitles.FirstOrDefault(x => x.Offset == subtitle.Offset);
                                output.WriteString(sub2 != null ? sub2.Translation : subtitle.Text);

                                subtitle = ReadSubtitle(input);
                                sub2     = subtitles.FirstOrDefault(x => x.Offset == subtitle.Offset);
                                output.WriteString(sub2 != null ? sub2.Translation : subtitle.Text);
                            }
                        }
        }
Exemplo n.º 5
0
        private IList <Subtitle> ReadSubtitles(byte[] bytes, int offsetBase)
        {
            var result = new List <Subtitle>();

            using (var fs = new MemoryStream(bytes))
                using (var input = new ExtendedBinaryReader(fs, customEncoding))
                {
                    int magic         = input.ReadInt32();
                    int count         = 0x08;
                    var searchPattern = new byte[] { 0x00, 0x00, 0x00, 0x08 };
                    if (magic == 0x00100187)
                    {
                        count         = 0x0E;
                        searchPattern = new byte[] { 0x00, 0x00, 0x00, 0x0E };
                    }
                    var patternPos = new List <int>();
                    int pos        = input.FindPattern(searchPattern);
                    while (pos != -1)
                    {
                        patternPos.Add(pos);
                        pos = input.FindPattern(searchPattern);
                    }

                    if (patternPos.Count > 0)
                    {
                        input.Seek(patternPos[patternPos.Count - 1] + 4, SeekOrigin.Begin);

                        for (int i = 0; i < count; i++)
                        {
                            input.Skip(0x1C);
                            Subtitle txt = ReadSubtitle(input);
                            if (!string.IsNullOrWhiteSpace(txt.Text))
                            {
                                txt.Offset          += offsetBase;
                                txt.PropertyChanged += SubtitlePropertyChanged;
                                result.Add(txt);
                            }

                            txt = ReadSubtitle(input);
                            if (!string.IsNullOrWhiteSpace(txt.Text))
                            {
                                txt.Offset          += offsetBase;
                                txt.PropertyChanged += SubtitlePropertyChanged;
                                result.Add(txt);
                            }
                        }

                        input.Skip(4);
                        Subtitle subtitle = ReadSubtitle(input);
                        if (!string.IsNullOrWhiteSpace(subtitle.Text))
                        {
                            subtitle.Offset          += offsetBase;
                            subtitle.PropertyChanged += SubtitlePropertyChanged;
                            result.Add(subtitle);
                        }

                        subtitle = ReadSubtitle(input);
                        if (!string.IsNullOrWhiteSpace(subtitle.Text))
                        {
                            subtitle.Offset          += offsetBase;
                            subtitle.PropertyChanged += SubtitlePropertyChanged;
                            result.Add(subtitle);
                        }
                    }
                }

            return(result);
        }