Exemple #1
0
 public IndexedVar(WorkshopArrayBuilder arrayBuilder, ScopeGroup scopeGroup, string name, bool isGlobal, WorkshopVariable variable, Element[] index, Node node)
     : base(name, scopeGroup, node)
 {
     this.ArrayBuilder = arrayBuilder;
     IsGlobal          = isGlobal;
     Variable          = variable;
     Index             = index;
 }
        public VarCollection(Variable global, Variable player, Variable builder)
        {
            Global = global;
            Player = player;

            IndexedVar tempArrayBuilderVar = AssignVar(null, "Multidimensional Array Builder", true, null);

            WorkshopArrayBuilder             = new WorkshopArrayBuilder(builder, tempArrayBuilderVar);
            tempArrayBuilderVar.ArrayBuilder = WorkshopArrayBuilder;
        }
Exemple #3
0
 private static IndexedVar CreateVar(WorkshopArrayBuilder builder, ScopeGroup scope, string name, bool isGlobal, WorkshopVariable variable, Element[] index, Node node)
 {
     if (scope == null || !scope.Recursive)
     {
         return(new IndexedVar(builder, scope, name, isGlobal, variable, index, node));
     }
     else
     {
         return(new RecursiveVar(builder, scope, name, isGlobal, variable, index, node));
     }
 }
        public void Setup()
        {
            global = AssignWorkshopVariable("_extendedGlobalCollection", true);
            player = AssignWorkshopVariable("_extendedPlayerCollection", false);
            var builder = AssignWorkshopVariable("_arrayBuilder", true);

            IndexReference store = Assign("_arrayBuilderStore", true, true);

            ArrayBuilder = new WorkshopArrayBuilder(builder, store);
            // The store shouldn't require an instance of the WorkshopArrayBuilder, but if for some reason it does uncomment the line below.
            // store.ArrayBuilder = arrayBuilder;
        }
Exemple #5
0
        public VarCollection(int[] reservedGlobalIDs, string[] reservedGlobalNames, int[] reservedPlayerIDs, string[] reservedPlayerNames)
        {
            ReservedGlobalIDs   = reservedGlobalIDs;
            ReservedPlayerIDs   = reservedPlayerIDs;
            ReservedGlobalNames = reservedGlobalNames;
            ReservedPlayerNames = reservedPlayerNames;

            Global = Assign("_extendedGlobalCollection", true);
            Player = Assign("_extendedPlayerCollection", false);
            var builder = Assign("_arrayBuilder", true);

            IndexedVar tempArrayBuilderVar = IndexedVar.AssignInternalVar(this, null, "_arrayBuilderStore", true);

            WorkshopArrayBuilder             = new WorkshopArrayBuilder(builder, tempArrayBuilderVar);
            tempArrayBuilderVar.ArrayBuilder = WorkshopArrayBuilder;
        }
        override public Element New(CreateObjectNode node, ScopeGroup getter, ScopeGroup scope, TranslateRule context)
        {
            // Get the index to store the class.
            IndexedVar index = context.VarCollection.AssignVar(scope, "New " + Name + " class index", context.IsGlobal, null); // Assigns the index variable.

            // Get the index of the next free spot in the class array.
            context.Actions.AddRange(
                index.SetVariable(
                    Element.Part <V_IndexOfArrayValue>(
                        WorkshopArrayBuilder.GetVariable(true, null, Variable.C),
                        new V_Null()
                        )
                    )
                );
            // Set the index to the count of the class array if the index equals -1.
            context.Actions.AddRange(
                index.SetVariable(
                    Element.TernaryConditional(
                        new V_Compare(index.GetVariable(), Operators.Equal, new V_Number(-1)),
                        Element.Part <V_CountOf>(
                            WorkshopArrayBuilder.GetVariable(true, null, Variable.C)
                            ),
                        index.GetVariable()
                        )
                    )
                );
            // The direct reference to the class variable.
            IndexedVar store = new IndexedVar(
                scope,
                Name + " root",
                true,
                Variable.C,
                new Element[] { index.GetVariable() },
                context.VarCollection.WorkshopArrayBuilder,
                null
                );

            store.Index[0].SupportedType = store;
            store.Type = this;

            ScopeGroup typeScope = GetRootScope(store, context.ParserData);

            SetupNew(getter, scope, store, typeScope, context, node);

            return(index.GetVariable());
        }
Exemple #7
0
 public RecursiveIndexReference(WorkshopArrayBuilder arrayBuilder, WorkshopVariable workshopVariable, params Element[] index) : base(arrayBuilder, workshopVariable, index)
 {
 }
Exemple #8
0
 public virtual Element[] ModifyVariable(Operation operation, Element value, Element targetPlayer = null, params Element[] index)
 {
     return(WorkshopArrayBuilder.ModifyVariable(ArrayBuilder, operation, value, targetPlayer, WorkshopVariable, ArrayBuilder <Element> .Build(Index, index)));
 }
Exemple #9
0
 public virtual Element[] SetVariable(Element value, Element targetPlayer = null, params Element[] index)
 {
     return(WorkshopArrayBuilder.SetVariable(ArrayBuilder, value, targetPlayer, WorkshopVariable, false, ArrayBuilder <Element> .Build(Index, index)));
 }
Exemple #10
0
 public virtual IWorkshopTree GetVariable(Element targetPlayer = null)
 {
     return(WorkshopArrayBuilder.GetVariable(targetPlayer, WorkshopVariable, Index));
 }
Exemple #11
0
 public IndexReference(WorkshopArrayBuilder arrayBuilder, WorkshopVariable workshopVariable, params Element[] index)
 {
     ArrayBuilder     = arrayBuilder;
     WorkshopVariable = workshopVariable;
     Index            = index;
 }
Exemple #12
0
 public void Setup()
 {
     ArrayBuilder = new WorkshopArrayBuilder(this);
 }
Exemple #13
0
        public static Element[] SetVariable(WorkshopArrayBuilder builder, Element value, Element targetPlayer, WorkshopVariable variable, bool flat2ndDim, params Element[] index)
        {
            if (index == null || index.Length == 0)
            {
                if (variable.IsGlobal)
                {
                    return new Element[] { Element.Part <A_SetGlobalVariable>(variable, value) }
                }
                ;
                else
                {
                    return new Element[] { Element.Part <A_SetPlayerVariable>(targetPlayer, variable, value) }
                };
            }

            if (index.Length == 1)
            {
                if (variable.IsGlobal)
                {
                    return new Element[] { Element.Part <A_SetGlobalVariableAtIndex>(variable, index[0], value) }
                }
                ;
                else
                {
                    return new Element[] { Element.Part <A_SetPlayerVariableAtIndex>(targetPlayer, variable, index[0], value) }
                };
            }

            if (flat2ndDim && index.Length > 2)
            {
                throw new ArgumentOutOfRangeException("index", "Can't set more than 2 dimensions if flat2ndDim is true.");
            }

            if (index.Length == 2 && flat2ndDim)
            {
                Element baseArray      = GetVariable(targetPlayer, variable, index[0]);
                Element baseArrayValue = Element.Part <V_Append>(
                    Element.Part <V_Append>(
                        Element.Part <V_ArraySlice>(
                            baseArray,
                            new V_Number(0),
                            index[1]
                            ),
                        value
                        ),
                    Element.Part <V_ArraySlice>(
                        baseArray,
                        index[1] + 1,
                        new V_Number(Constants.MAX_ARRAY_LENGTH)
                        )
                    );

                return(SetVariable(null, baseArrayValue, targetPlayer, variable, false, index[0]));
            }

            if (builder == null)
            {
                throw new ArgumentNullException("builder", "Can't set multidimensional array if builder is null.");
            }

            List <Element> actions = new List <Element>();

            Element root = GetRoot(targetPlayer, variable);

            // index is 2 or greater
            int dimensions = index.Length - 1;

            // Get the last array in the index path and copy it to variable B.
            actions.AddRange(
                SetVariable(builder, ValueInArrayPath(root, index.Take(index.Length - 1).ToArray()), targetPlayer, builder.Constructor, false)
                );

            // Set the value in the array.
            actions.AddRange(
                SetVariable(builder, value, targetPlayer, builder.Constructor, false, index.Last())
                );

            // Reconstruct the multidimensional array.
            for (int i = 1; i < dimensions; i++)
            {
                // Copy the array to the C variable
                actions.AddRange(
                    builder.Store.SetVariable(GetRoot(targetPlayer, builder.Constructor), targetPlayer)
                    );

                // Copy the next array dimension
                Element array = ValueInArrayPath(root, index.Take(dimensions - i).ToArray());

                actions.AddRange(
                    SetVariable(builder, array, targetPlayer, builder.Constructor, false)
                    );

                // Copy back the variable at C to the correct index
                actions.AddRange(
                    SetVariable(builder, (Element)builder.Store.GetVariable(targetPlayer), targetPlayer, builder.Constructor, false, index[i])
                    );
            }
            // Set the final variable using Set At Index.
            actions.AddRange(
                SetVariable(builder, GetRoot(targetPlayer, builder.Constructor), targetPlayer, variable, false, index[0])
                );
            return(actions.ToArray());
        }
Exemple #14
0
        public static Element[] ModifyVariable(WorkshopArrayBuilder builder, Operation operation, Element value, Element targetPlayer, WorkshopVariable variable, params Element[] index)
        {
            if (index == null || index.Length == 0)
            {
                if (variable.IsGlobal)
                {
                    return new Element[] { Element.Part <A_ModifyGlobalVariable>(variable, EnumData.GetEnumValue(operation), value) }
                }
                ;
                else
                {
                    return new Element[] { Element.Part <A_ModifyPlayerVariable>(targetPlayer, variable, EnumData.GetEnumValue(operation), value) }
                };
            }

            if (index.Length == 1)
            {
                if (variable.IsGlobal)
                {
                    return new Element[] { Element.Part <A_ModifyGlobalVariableAtIndex>(variable, index[0], EnumData.GetEnumValue(operation), value) }
                }
                ;
                else
                {
                    return new Element[] { Element.Part <A_ModifyPlayerVariableAtIndex>(targetPlayer, variable, index[0], EnumData.GetEnumValue(operation), value) }
                };
            }

            if (builder == null)
            {
                throw new ArgumentNullException("builder", "Can't modify multidimensional array if builder is null.");
            }

            List <Element> actions = new List <Element>();

            Element root = GetRoot(targetPlayer, variable);

            // index is 2 or greater
            int dimensions = index.Length - 1;

            // Get the last array in the index path and copy it to variable B.
            actions.AddRange(
                SetVariable(builder, ValueInArrayPath(root, index.Take(index.Length - 1).ToArray()), targetPlayer, builder.Constructor, false)
                );

            // Modify the value in the array.
            actions.AddRange(
                ModifyVariable(builder, operation, value, targetPlayer, builder.Constructor, index.Last())
                );

            // Reconstruct the multidimensional array.
            for (int i = 1; i < dimensions; i++)
            {
                // Copy the array to the C variable
                actions.AddRange(
                    builder.Store.SetVariable(GetRoot(targetPlayer, builder.Constructor), targetPlayer)
                    );

                // Copy the next array dimension
                Element array = ValueInArrayPath(root, index.Take(dimensions - i).ToArray());

                actions.AddRange(
                    SetVariable(builder, array, targetPlayer, builder.Constructor, false)
                    );

                // Copy back the variable at C to the correct index
                actions.AddRange(
                    SetVariable(builder, (Element)builder.Store.GetVariable(targetPlayer), targetPlayer, builder.Constructor, false, index[i])
                    );
            }
            // Set the final variable using Set At Index.
            actions.AddRange(
                SetVariable(builder, GetRoot(targetPlayer, builder.Constructor), targetPlayer, variable, false, index[0])
                );
            return(actions.ToArray());
        }
    }
}
Exemple #15
0
 public RecursiveVar(WorkshopArrayBuilder arrayBuilder, ScopeGroup scopeGroup, string name, bool isGlobal, WorkshopVariable variable, Element[] index, Node node)
     : base(arrayBuilder, scopeGroup, name, isGlobal, variable, index, node)
 {
 }
Exemple #16
0
 public virtual Element[] SetVariable(Element value, Element targetPlayer = null, params Element[] setAtIndex)
 {
     return(WorkshopArrayBuilder.SetVariable(ArrayBuilder, value, targetPlayer, Variable, Optimize2ndDim, ArrayBuilder <Element> .Build(Index, setAtIndex)));
 }
Exemple #17
0
 protected virtual Element Get(Element targetPlayer = null)
 {
     return(WorkshopArrayBuilder.GetVariable(targetPlayer, Variable, Index));
 }