FSO.Files.Formats.IFF.Chunks.STR.InsertString C# (CSharp) Method

InsertString() public method

public InsertString ( int index, STRItem item ) : void
index int
item STRItem
return void
        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++;
            }
        }

Usage Example

Exemplo n.º 1
0
        private void OKButton_Click(object sender, EventArgs e)
        {
            var name = ChunkLabelEntry.Text;
            var guidT = GUIDEntry.Text;
            uint guid;
            var objProvider = Content.Content.Get().WorldObjects;
            if (name == "") MessageBox.Show("Name cannot be empty!", "Invalid Object Name");
            else if (guidT == "") MessageBox.Show("GUID cannot be empty!", "Invalid GUID");
            else if (!uint.TryParse(guidT, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out guid))
                MessageBox.Show("GUID is invalid! Make sure it is a hex string of size 8. (eg. 6789ABCD)", "Invalid GUID");
            else
            {
                lock (objProvider.Entries)
                {
                    if (objProvider.Entries.ContainsKey(guid))
                    {
                        MessageBox.Show("This GUID is already being used!", "GUID is Taken!");
                        return;
                    }

                    //OK, it's valid. Now to add it to the objects system...
                    //This is a little tricky because we want to add an object that does not exist yet.
                    //There's a special function just for this! But first, we need an OBJD...

                    var obj = new OBJD()
                    {
                        GUID = guid,
                        ObjectType = OBJDType.Normal,
                        ChunkLabel = name,
                        ChunkID = 1,
                        ChunkProcessed = true,
                        ChunkType = "OBJD",
                        ChunkParent = TargetIff,
                        AnimationTableID = 128
                    };

                    Content.Content.Get().Changes.BlockingResMod(new ResAction(() =>
                    {
                        //find a free space to place the object
                        ushort id = 16807; //todo: why???
                        var list = TargetIff.List<OBJD>();
                        if (list != null)
                        {
                            foreach (var chk in list.OrderBy(x => x.ChunkID))
                            {
                                if (chk.ChunkID == id)
                                    id++;
                            }
                        }
                        obj.ChunkID = id;
                        //add it to the iff file
                        TargetIff.AddChunk(obj);
                    }, obj));

                    if (IsNew)
                    {
                        //add a default animation table, for quality of life reasons

                        var anim = new STR()
                        {
                            ChunkLabel = name,
                            ChunkID = 128,
                            ChunkProcessed = true,
                            ChunkType = "STR#",
                            ChunkParent = TargetIff,
                        };

                        anim.InsertString(0, new STRItem { Value = "", Comment = "" });
                        TargetIff.AddChunk(anim);

                        var filename = TargetIff.RuntimeInfo.Path;
                        Directory.CreateDirectory(Path.GetDirectoryName(filename));
                        using (var stream = new FileStream(filename, FileMode.Create))
                            TargetIff.Write(stream);
                    }

                    //add it to the provider
                    objProvider.AddObject(TargetIff, obj);

                    DialogResult = DialogResult.OK;
                    ResultGUID = guid;
                    Close();
                }
            }
        }