Example #1
0
        public void Save(Stream stream)
        {
            var map = LanguageUtility.GetEncodeCharMap(GameInstance, Language);

            int total = 0;

            foreach (var bucket in Buckets)
            {
                total += bucket.Count;
            }
            Header.StringCount = (UInt32)total;

            stream.WriteStruct <StringHeader>(Header);
            int nextBucketData = Buckets.Count * Marshal.SizeOf(typeof(StringBucket)) + Marshal.SizeOf(typeof(StringHeader));
            int nextStringPos  = Buckets.Count * Marshal.SizeOf(typeof(StringBucket)) + Marshal.SizeOf(typeof(StringHeader)) + Marshal.SizeOf(typeof(UInt32)) * total;

            foreach (var bucket in Buckets)
            {
                long         bucketPos = stream.Position;
                StringBucket strBucket = new StringBucket();
                strBucket.StringCount  = (UInt32)bucket.Count;
                strBucket.StringOffset = (UInt32)nextBucketData;

                foreach (var pair in bucket)
                {
                    stream.Seek(nextBucketData, SeekOrigin.Begin);
                    stream.WriteUInt32((UInt32)nextStringPos);
                    nextBucketData = (int)stream.Position;
                    stream.Seek(nextStringPos, SeekOrigin.Begin);
                    UInt32 hash = FileIsSaintsRow2 ? pair.Key.Swap() : pair.Key;
                    stream.WriteUInt32(hash);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        for (int i = 0; i < pair.Value.Length; i++)
                        {
                            char src   = pair.Value[i];
                            char value = src;
                            if (map.ContainsKey(src))
                            {
                                value = map[src];
                            }

                            UInt16 charValue = (UInt16)value;
                            if (FileIsSaintsRow2)
                            {
                                charValue = charValue.Swap();
                            }

                            ms.WriteUInt16(charValue);
                        }
                        ms.WriteInt16(0);
                        ms.Seek(0, SeekOrigin.Begin);
                        ms.CopyTo(stream);
                    }

                    nextStringPos = (int)stream.Position;
                }

                stream.Seek(bucketPos, SeekOrigin.Begin);
                stream.WriteStruct(strBucket);
            }
        }
Example #2
0
        public void Save(Stream stream)
        {
            Header.Signature     = 0x56414d44;
            Header.LipsyncOffset = 0;
            if (LipsyncData != null)
            {
                Header.LipsyncSize = (uint)LipsyncData.Length;
                stream.Seek(0x24, SeekOrigin.Begin);
                stream.Write(LipsyncData, 0, LipsyncData.Length);
            }
            else
            {
                Header.LipsyncSize = 0;
            }

            Header.SubtitleOffset = Header.LipsyncSize;

            uint startOfSubtitles = 0x24 + Header.LipsyncSize + 0x74;

            uint nextSubtitleOffset = 0;

            if (Subtitles.Count != 0)
            {
                SubtitleHeader         = new AudioMetadataSubtitleHeader();
                SubtitleHeader.Version = 3;
                SubtitleHeader.LocalizedVoiceSubtitleHeaders = new LocalizedVoiceSubtitleHeader[14];
                for (int i = 0; i < SubtitleHeader.LocalizedVoiceSubtitleHeaders.Length; i++)
                {
                    Language language = (Language)i;

                    string subtitle = Subtitles[language];
                    if (subtitle == "")
                    {
                        SubtitleHeader.LocalizedVoiceSubtitleHeaders[i].Offset = nextSubtitleOffset;
                        SubtitleHeader.LocalizedVoiceSubtitleHeaders[i].Length = 0;
                    }
                    else
                    {
                        var map = LanguageUtility.GetEncodeCharMap(Instance, language);

                        byte[] subtitleData;
                        using (MemoryStream ms = new MemoryStream())
                        {
                            for (int pos = 0; pos < subtitle.Length; pos++)
                            {
                                char src   = subtitle[pos];
                                char value = src;
                                if (map.ContainsKey(src))
                                {
                                    value = map[src];
                                }

                                byte[] data = BitConverter.GetBytes(value);
                                ms.Write(data, 0, data.Length);
                            }
                            ms.WriteUInt16(0);
                            subtitleData = ms.ToArray();
                        }
                        SubtitleHeader.LocalizedVoiceSubtitleHeaders[i].Offset = nextSubtitleOffset;
                        SubtitleHeader.LocalizedVoiceSubtitleHeaders[i].Length = (uint)subtitleData.Length;
                        stream.Seek(startOfSubtitles + nextSubtitleOffset, SeekOrigin.Begin);
                        stream.Write(subtitleData, 0, subtitleData.Length);

                        nextSubtitleOffset += (uint)subtitleData.Length;
                    }
                }
                Header.SubtitleSize = nextSubtitleOffset + 0x74;
                stream.Seek(0x24 + Header.SubtitleOffset, SeekOrigin.Begin);
                stream.WriteStruct(SubtitleHeader);
            }
            else
            {
                Header.SubtitleSize = 0;
            }
            stream.Seek(0, SeekOrigin.Begin);
            stream.WriteStruct(Header);
        }
Example #3
0
        public void Save(Stream stream)
        {
            var map = LanguageUtility.GetEncodeCharMap(GameInstance, Language);

            UInt16 bucketCount = (UInt16)(Strings.Count / 5);

            if (bucketCount < 32)
            {
                bucketCount = 32;
            }
            else if (bucketCount < 64)
            {
                bucketCount = 64;
            }
            else if (bucketCount < 128)
            {
                bucketCount = 128;
            }
            else if (bucketCount < 256)
            {
                bucketCount = 256;
            }
            else if (bucketCount < 512)
            {
                bucketCount = 512;
            }
            else
            {
                bucketCount = 1024;
            }

            Dictionary <uint, string>[] buckets = new Dictionary <uint, string> [bucketCount];
            for (int i = 0; i < bucketCount; i++)
            {
                buckets[i] = new Dictionary <uint, string>();
            }

            foreach (var pair in Strings)
            {
                uint   hash = pair.Key;
                string text = pair.Value;

                UInt32 mask      = (UInt32)(buckets.Length - 1);
                UInt32 bucketIdx = (UInt32)(hash & mask);
                buckets[(int)bucketIdx].Add(hash, text);
            }

            Header.StringCount = (UInt32)Strings.Count;
            Header.BucketCount = bucketCount;

            stream.WriteStruct <StringHeader>(Header);
            int nextBucketData = buckets.Length * Marshal.SizeOf(typeof(StringBucket)) + Marshal.SizeOf(typeof(StringHeader));
            int nextStringPos  = buckets.Length * Marshal.SizeOf(typeof(StringBucket)) + Marshal.SizeOf(typeof(StringHeader)) + Marshal.SizeOf(typeof(UInt32)) * Strings.Count;

            foreach (var bucket in buckets)
            {
                long         bucketPos = stream.Position;
                StringBucket strBucket = new StringBucket();
                strBucket.StringCount  = (UInt32)bucket.Count;
                strBucket.StringOffset = (UInt32)nextBucketData;

                foreach (var pair in bucket)
                {
                    stream.Seek(nextBucketData, SeekOrigin.Begin);
                    stream.WriteUInt32((UInt32)nextStringPos);
                    nextBucketData = (int)stream.Position;
                    stream.Seek(nextStringPos, SeekOrigin.Begin);
                    UInt32 hash = FileIsSaintsRow2 ? pair.Key.Swap() : pair.Key;
                    stream.WriteUInt32(hash);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        for (int i = 0; i < pair.Value.Length; i++)
                        {
                            char src   = pair.Value[i];
                            char value = src;
                            if (map.ContainsKey(src))
                            {
                                value = map[src];
                            }

                            UInt16 charValue = (UInt16)value;
                            if (FileIsSaintsRow2)
                            {
                                charValue = charValue.Swap();
                            }

                            ms.WriteUInt16(charValue);
                        }
                        ms.WriteInt16(0);
                        ms.Seek(0, SeekOrigin.Begin);
                        ms.CopyTo(stream);
                    }

                    nextStringPos = (int)stream.Position;
                }

                stream.Seek(bucketPos, SeekOrigin.Begin);
                stream.WriteStruct(strBucket);
            }
        }