Esempio n. 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++;
            }
        }
Esempio n. 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);
                    }
                }
            }
        }
        public override void Execute(ModScriptDatabaseHelper database)
        {
            VltCollection collection = GetCollection(database, ClassName, CollectionName);

            if (collection.HasEntry(FieldName))
            {
                collection.RemoveValue(FieldName);
            }
            else
            {
                string hashed = $"0x{VLT32Hasher.Hash(FieldName):X8}";

                if (collection.HasEntry(hashed))
                {
                    collection.RemoveValue(hashed);
                }
                else
                {
                    throw new ModScriptCommandExecutionException($"Could not delete field: {ClassName}/{CollectionName}[{FieldName}]");
                }
            }
        }
        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;
            }
        }
        public override void Execute(ModScriptDatabaseHelper database)
        {
            VltCollection collection = GetCollection(database, ClassName, CollectionName);
            VltClassField field      = collection.Class[FieldName];

            if (field.IsInLayout)
            {
                throw new InvalidDataException($"add_field failed because field '{field.Name}' is a base field");
            }

            if (collection.HasEntry(field.Name))
            {
                throw new InvalidDataException($"add_field failed because collection '{collection.ShortPath}' already has field '{field.Name}'");
            }

            var vltBaseType = TypeRegistry.CreateInstance(database.Database.Options.GameId, collection.Class, field, collection);

            if (vltBaseType is VLTArrayType array)
            {
                if (ArrayCapacity > field.MaxCount)
                {
                    throw new ModScriptCommandExecutionException(
                              $"Cannot add field {ClassName}[{FieldName}] with capacity beyond maximum (requested {ArrayCapacity} but limit is {field.MaxCount})");
                }

                array.Capacity      = ArrayCapacity;
                array.ItemAlignment = field.Alignment;
                array.FieldSize     = field.Size;
                array.Items         = new List <VLTBaseType>();

                for (var i = 0; i < ArrayCapacity; i++)
                {
                    array.Items.Add(TypeRegistry.ConstructInstance(array.ItemType, collection.Class, field, collection));
                }
            }

            collection.SetRawValue(field.Name, vltBaseType);
        }