Item in a STR chunk.
示例#1
0
文件: STR.cs 项目: RHY3756547/FreeSO
        /// <summary>
        /// Reads a STR chunk from a stream.
        /// </summary>
        /// <param name="iff">An Iff instance.</param>
        /// <param name="stream">A Stream object holding a STR chunk.</param>
        public override void Read(IffFile iff, Stream stream)
        {
            using (var io = IoBuffer.FromStream(stream, ByteOrder.LITTLE_ENDIAN))
            {
                var formatCode = io.ReadInt16();
                LanguageSets = new STRLanguageSet[20];
                if (!io.HasMore){
                    for (int i = 0; i < 20; i++) LanguageSets[i] = new STRLanguageSet { Strings = new STRItem[0] };
                    return;
                }

                if (formatCode == 0)
                {
                    var numStrings = io.ReadUInt16();
                    for (int i = 0; i < 20; i++) LanguageSets[i] = new STRLanguageSet { Strings = new STRItem[0] };
                    LanguageSets[0].Strings = new STRItem[numStrings];
                    for (var i = 0; i < numStrings; i++)
                    {
                        LanguageSets[0].Strings[i] = new STRItem
                        {
                            Value = io.ReadPascalString()
                        };
                    }
                }
                //This format changed 00 00 to use C strings rather than Pascal strings.
                else if (formatCode == -1)
                {
                    var numStrings = io.ReadUInt16();
                    for (int i = 0; i < 20; i++) LanguageSets[i] = new STRLanguageSet { Strings = new STRItem[0] };
                    LanguageSets[0].Strings = new STRItem[numStrings];
                    for (var i = 0; i < numStrings; i++)
                    {
                        LanguageSets[0].Strings[i] = new STRItem
                        {
                            Value = io.ReadNullTerminatedUTF8()
                        };
                    }
                }
                //This format changed FF FF to use string pairs rather than single strings.
                else if (formatCode == -2)
                {
                    var numStrings = io.ReadUInt16();
                    for (int i = 0; i < 20; i++) LanguageSets[i] = new STRLanguageSet { Strings = new STRItem[0] };
                    LanguageSets[0].Strings = new STRItem[numStrings];
                    for (var i = 0; i < numStrings; i++)
                    {
                        LanguageSets[0].Strings[i] = new STRItem
                        {
                            Value = io.ReadNullTerminatedString(),
                            Comment = io.ReadNullTerminatedString()
                        };
                    }
                }
                //This format changed FD FF to use a language code.
                else if (formatCode == -3)
                {
                    var numStrings = io.ReadUInt16();
                    for (int i = 0; i < 20; i++) LanguageSets[i] = new STRLanguageSet { Strings = new STRItem[0] };
                    List<STRItem>[] LangSort = new List<STRItem>[20];
                    for (var i = 0; i < numStrings; i++)
                    {
                        var item = new STRItem
                        {
                            LanguageCode = io.ReadByte(),
                            Value = io.ReadNullTerminatedString(),
                            Comment = io.ReadNullTerminatedString()
                        };

                        var lang = item.LanguageCode;
                        if (lang == 0) lang = 1;
                        else if (lang < 0 || lang > 20) continue; //???
                        if (LangSort[lang - 1] == null)
                        {
                            LangSort[lang-1] = new List<STRItem>();
                        }

                        LangSort[lang - 1].Add(item);
                    }
                    for (int i=0; i<LanguageSets.Length; i++)
                    {
                        if (LangSort[i] != null) LanguageSets[i].Strings = LangSort[i].ToArray();
                    }
                }

                //This format is only used in The Sims Online. The format is essentially a performance improvement:
                //it counteracts both the short string limit of 255 characters found in 00 00 and the inherent slowness
                //of null-terminated strings in the other formats (which requires two passes over each string), and it
                //also provides a string pair count for each language set which eliminates the need for two passes over
                //each language set.
                else if (formatCode == -4)
                {
                    var numLanguageSets = io.ReadByte();
                    this.LanguageSets = new STRLanguageSet[numLanguageSets];

                    for(var i=0; i < numLanguageSets; i++)
                    {
                        var item = new STRLanguageSet();
                        var numStringPairs = io.ReadUInt16();
                        item.Strings = new STRItem[numStringPairs];
                        for (var x = 0; x < numStringPairs; x++)
                        {
                            item.Strings[x] = new STRItem
                            {
                                LanguageCode = (byte)(io.ReadByte() + 1),
                                Value = io.ReadVariableLengthPascalString(),
                                Comment = io.ReadVariableLengthPascalString()
                            };
                        }
                        this.LanguageSets[i] = item;
                    }
                }
            }
        }
示例#2
0
文件: STR.cs 项目: RHY3756547/FreeSO
 public void RemoveString(int index)
 {
     foreach (var languageSet in LanguageSets)
     {
         if (languageSet.Strings.Length == 0) continue; //language not initialized
         var newStr = new STRItem[languageSet.Strings.Length - 1];
         Array.Copy(languageSet.Strings, newStr, index); //copy before strings
         Array.Copy(languageSet.Strings, index + 1, newStr, index, (languageSet.Strings.Length - (index + 1)));
         languageSet.Strings = newStr;
     }
 }
示例#3
0
文件: STR.cs 项目: RHY3756547/FreeSO
 public void InsertString(int index, STRItem item)
 {
     byte i = 1;
     foreach (var languageSet in LanguageSets) {
         if (languageSet.Strings.Length == 0 && i > 1)
         {
             i++;
             continue; //language not initialized
         }
         var newStr = new STRItem[languageSet.Strings.Length + 1];
         Array.Copy(languageSet.Strings, newStr, index); //copy before strings
         newStr[index] = new STRItem()
         {
             LanguageCode = i,
             Value = item.Value,
             Comment = item.Comment
         };
         Array.Copy(languageSet.Strings, index, newStr, index + 1, (languageSet.Strings.Length - index));
         languageSet.Strings = newStr;
         i++;
     }
 }