Exemple #1
0
 public PropertyEditor(ActionManager manager)
 {
     ContextActionManager = manager;
 }
Exemple #2
0
 public static MassEditResult PerformMassEdit(string csvString, ActionManager actionManager, string param)
 {
     try
     {
         PARAM p = ParamBank.Params[param];
         if (p == null)
         {
             return(new MassEditResult(MassEditResultType.PARSEERROR, "No Param selected"));
         }
         int              csvLength   = p.AppliedParamdef.Fields.Count + 2;// Include ID and name
         string[]         csvLines    = csvString.Split('\n');
         int              changeCount = 0;
         int              addedCount  = 0;
         List <Action>    actions     = new List <Action>();
         List <PARAM.Row> addedParams = new List <PARAM.Row>();
         foreach (string csvLine in csvLines)
         {
             if (csvLine.Trim().Equals(""))
             {
                 continue;
             }
             string[] csvs = csvLine.Split(',');
             if (csvs.Length != csvLength || csvs.Length < 2)
             {
                 return(new MassEditResult(MassEditResultType.PARSEERROR, "CSV has wrong number of values"));
             }
             int       id   = int.Parse(csvs[0]);
             string    name = csvs[1];
             PARAM.Row row  = p[id];
             if (row == null)
             {
                 row = new PARAM.Row(id, name, p.AppliedParamdef);
                 addedParams.Add(row);
             }
             if (row.Name == null || !row.Name.Equals(name))
             {
                 actions.Add(new PropertiesChangedAction(row.GetType().GetProperty("Name"), -1, row, name));
             }
             int index = 2;
             foreach (PARAM.Cell c in row.Cells)
             {
                 string v = csvs[index];
                 index++;
                 // Array types are unhandled
                 if (c.Value.GetType().IsArray)
                 {
                     continue;
                 }
                 object newval = PerformOperation(c, "=", v);
                 if (newval == null)
                 {
                     return(new MassEditResult(MassEditResultType.OPERATIONERROR, $@"Could not assign {v} to field {c.Def.DisplayName}"));
                 }
                 if (!c.Value.Equals(newval))
                 {
                     actions.Add(new PropertiesChangedAction(c.GetType().GetProperty("Value"), -1, c, newval));
                 }
             }
         }
         changeCount = actions.Count;
         addedCount  = addedParams.Count;
         actions.Add(new AddParamsAction(p, "legacystring", addedParams, false));
         if (changeCount != 0 || addedCount != 0)
         {
             actionManager.ExecuteAction(new CompoundAction(actions));
         }
         return(new MassEditResult(MassEditResultType.SUCCESS, $@"{changeCount} cells affected, {addedCount} rows added"));
     }
     catch
     {
         return(new MassEditResult(MassEditResultType.PARSEERROR, "Unable to parse CSV into correct data types"));
     }
 }
Exemple #3
0
 public static MassEditResult PerformSingleMassEdit(string csvString, ActionManager actionManager, string param, string field, bool useSpace)
 {
     try
     {
         PARAM p = ParamBank.Params[param];
         if (p == null)
         {
             return(new MassEditResult(MassEditResultType.PARSEERROR, "No Param selected"));
         }
         string[]      csvLines    = csvString.Split('\n');
         int           changeCount = 0;
         List <Action> actions     = new List <Action>();
         foreach (string csvLine in csvLines)
         {
             if (csvLine.Trim().Equals(""))
             {
                 continue;
             }
             string[] csvs = csvLine.Split(useSpace ? ' ' : ',', 2, StringSplitOptions.RemoveEmptyEntries);
             if (csvs.Length != 2)
             {
                 return(new MassEditResult(MassEditResultType.PARSEERROR, "CSV has wrong number of values"));
             }
             int       id    = int.Parse(csvs[0]);
             string    value = csvs[1];
             PARAM.Row row   = p[id];
             if (row == null)
             {
                 return(new MassEditResult(MassEditResultType.OPERATIONERROR, $@"Could not locate row {id}"));
             }
             if (field.Equals("Name"))
             {
                 if (row.Name == null || !row.Name.Equals(value))
                 {
                     actions.Add(new PropertiesChangedAction(row.GetType().GetProperty("Name"), -1, row, value));
                 }
             }
             else
             {
                 PARAM.Cell cell = row[field];
                 if (cell == null)
                 {
                     return(new MassEditResult(MassEditResultType.OPERATIONERROR, $@"Could not locate field {field}"));
                 }
                 // Array types are unhandled
                 if (cell.Value.GetType().IsArray)
                 {
                     continue;
                 }
                 object newval = PerformOperation(cell, "=", value);
                 if (newval == null)
                 {
                     return(new MassEditResult(MassEditResultType.OPERATIONERROR, $@"Could not assign {value} to field {cell.Def.DisplayName}"));
                 }
                 if (!cell.Value.Equals(newval))
                 {
                     actions.Add(new PropertiesChangedAction(cell.GetType().GetProperty("Value"), -1, cell, newval));
                 }
             }
         }
         changeCount = actions.Count;
         if (changeCount != 0)
         {
             actionManager.ExecuteAction(new CompoundAction(actions));
         }
         return(new MassEditResult(MassEditResultType.SUCCESS, $@"{changeCount} rows affected"));
     }
     catch
     {
         return(new MassEditResult(MassEditResultType.PARSEERROR, "Unable to parse CSV into correct data types"));
     }
 }
Exemple #4
0
        public static MassEditResult PerformMassEdit(string commandsString, ActionManager actionManager, string contextActiveParam, List <PARAM.Row> contextActiveRows)
        {
            string[]      commands    = commandsString.Split('\n');
            int           changeCount = 0;
            List <Action> actions     = new List <Action>();

            foreach (string command in commands)
            {
                Match comm = commandRx.Match(command);
                if (comm.Success)
                {
                    Group  paramrx        = comm.Groups["paramrx"];
                    Regex  fieldRx        = new Regex($@"^{comm.Groups["fieldrx"].Value}$");
                    string op             = comm.Groups["op"].Value;
                    bool   isopparamField = comm.Groups["fieldtype"].Success;
                    string opparam        = comm.Groups["opparam"].Value;

                    List <PARAM> affectedParams = new List <PARAM>();
                    if (paramrx.Success)
                    {
                        affectedParams = GetMatchingParams(new Regex($@"^{paramrx.Value}$"));
                    }
                    else
                    {
                        affectedParams.Add(ParamBank.Params[contextActiveParam]);
                    }

                    List <PARAM.Row> affectedRows = new List <PARAM.Row>();
                    foreach (PARAM param in affectedParams)
                    {
                        if (!paramrx.Success)
                        {
                            affectedRows = contextActiveRows;
                        }
                        else
                        {
                            affectedRows.AddRange(GetMatchingParamRows(param, comm, false, false));
                        }
                    }
                    foreach (PARAM.Row row in affectedRows)
                    {
                        List <PARAM.Cell> affectedCells  = GetMatchingCells(row, fieldRx);
                        string            opparamcontext = opparam;
                        if (isopparamField)
                        {
                            foreach (PARAM.Cell cell in row.Cells)
                            {
                                if (cell.Def.InternalName.Equals(opparam))
                                {
                                    opparamcontext = cell.Value.ToString();
                                    break;
                                }
                            }
                            if (opparamcontext.Equals(opparam))
                            {
                                return(new MassEditResult(MassEditResultType.OPERATIONERROR, $@"Could not look up field {opparam} in row {row.Name}"));
                            }
                        }
                        changeCount += affectedCells.Count;
                        foreach (PARAM.Cell cell in affectedCells)
                        {
                            object newval = PerformOperation(cell, op, opparamcontext);
                            if (newval == null)
                            {
                                return(new MassEditResult(MassEditResultType.OPERATIONERROR, $@"Could not perform operation {op} {opparamcontext} on field {cell.Def.DisplayName}"));
                            }
                            actions.Add(new PropertiesChangedAction(cell.GetType().GetProperty("Value"), -1, cell, newval));
                        }
                    }
                }
                else
                {
                    return(new MassEditResult(MassEditResultType.PARSEERROR, $@"Unrecognised command {command}"));
                }
            }
            actionManager.ExecuteAction(new CompoundAction(actions));
            return(new MassEditResult(MassEditResultType.SUCCESS, $@"{changeCount} cells affected"));
        }