コード例 #1
0
        public override void Execute(ModScriptDatabaseHelper database)
        {
            VltCollection collection = GetCollection(database, ClassName, CollectionName);
            VltClassField field      = GetField(collection.Class, FieldName);

            if (!field.IsArray)
            {
                throw new ModScriptCommandExecutionException($"Field {ClassName}[{FieldName}] is not an array!");
            }

            if (!collection.HasEntry(FieldName))
            {
                throw new ModScriptCommandExecutionException($"Collection {collection.ShortPath} does not have an entry for {FieldName}.");
            }

            VLTArrayType array = collection.GetRawValue <VLTArrayType>(FieldName);

            if (array.Items.Count == array.Capacity && field.IsInLayout)
            {
                throw new ModScriptCommandExecutionException("Cannot append to a full array when it is a layout field");
            }

            if (array.Items.Count + 1 > field.MaxCount)
            {
                throw new ModScriptCommandExecutionException("Appending to this array would cause it to exceed the maximum number of allowed elements.");
            }

            var itemToEdit = TypeRegistry.ConstructInstance(array.ItemType, collection.Class, field, collection);

            if (_hasValue)
            {
                switch (itemToEdit)
                {
                case PrimitiveTypeBase primitiveTypeBase:
                    ValueConversionUtils.DoPrimitiveConversion(primitiveTypeBase, Value);
                    break;

                case IStringValue stringValue:
                    stringValue.SetString(Value);
                    break;

                case BaseRefSpec refSpec:
                    // NOTE: This is a compatibility feature for certain types, such as GCollectionKey, which are technically a RefSpec.
                    refSpec.CollectionKey = Value;
                    break;

                default:
                    throw new ModScriptCommandExecutionException($"Object stored in {collection.Class.Name}[{field.Name}] is not a simple type and cannot be used in a value-append command");
                }
            }

            array.Items.Add(itemToEdit);

            if (!field.IsInLayout)
            {
                array.Capacity++;
            }
        }
コード例 #2
0
        public override void Execute(ModScriptDatabaseHelper database)
        {
            VltCollection srcCollection = GetCollection(database, ClassName, SourceCollectionName);
            VltCollection dstCollection = GetCollection(database, ClassName, DestinationCollectionName);
            Dictionary <VltClassField, VLTBaseType> values = new Dictionary <VltClassField, VLTBaseType>();

            if ((Options & CopyOptions.Base) != 0)
            {
                foreach (var baseField in srcCollection.Class.BaseFields)
                {
                    values.Add(baseField,
                               ValueCloningUtils.CloneValue(database.Database, srcCollection.GetRawValue(baseField.Name), srcCollection.Class,
                                                            baseField, dstCollection));
                }
            }

            if ((Options & CopyOptions.Optional) != 0)
            {
                foreach (var(key, value) in srcCollection.GetData())
                {
                    var field = srcCollection.Class[key];

                    if (!field.IsInLayout)
                    {
                        values.Add(field, ValueCloningUtils.CloneValue(database.Database, value, srcCollection.Class, field, dstCollection));
                    }
                }
            }

            // base will always overwrite
            // optional by itself will copy anything that doesn't exist
            // optional + overwrite will copy nonexistent fields and overwrite the other ones(optional only)
            if ((Options & CopyOptions.Base) != 0)
            {
                foreach (var(key, value) in values)
                {
                    if (key.IsInLayout)
                    {
                        dstCollection.SetRawValue(key.Name, value);
                    }
                }
            }

            if ((Options & CopyOptions.Optional) != 0)
            {
                foreach (var(field, value) in values)
                {
                    if (!field.IsInLayout && (!dstCollection.HasEntry(field.Name) || (Options & CopyOptions.OverwriteOptional) != 0))
                    {
                        dstCollection.SetRawValue(field.Name, value);
                    }
                }
            }
        }
コード例 #3
0
        public override void Execute(ModScriptDatabaseHelper database)
        {
            VltCollection collection = GetCollection(database, ClassName, CollectionName);
            VltClassField field      = GetField(collection.Class, FieldName);

            if (!field.IsArray)
            {
                throw new ModScriptCommandExecutionException($"Field {ClassName}[{FieldName}] is not an array!");
            }

            if (field.MaxCount < NewCapacity)
            {
                throw new ModScriptCommandExecutionException(
                          $"Cannot resize field {ClassName}[{FieldName}] beyond maximum count (requested {NewCapacity} but limit is {field.MaxCount})");
            }

            if (!collection.HasEntry(FieldName))
            {
                throw new ModScriptCommandExecutionException($"Collection {collection.ShortPath} does not have an entry for {FieldName}.");
            }

            VLTArrayType array = collection.GetRawValue <VLTArrayType>(FieldName);

            if (NewCapacity < array.Items.Count)
            {
                while (NewCapacity < array.Items.Count)
                {
                    array.Items.RemoveAt(array.Items.Count - 1);
                }
            }
            else if (NewCapacity > array.Items.Count)
            {
                while (NewCapacity > array.Items.Count)
                {
                    array.Items.Add(TypeRegistry.ConstructInstance(array.ItemType, collection.Class, field, collection));
                }
            }

            if (!field.IsInLayout)
            {
                array.Capacity = NewCapacity;
            }
        }