| 1 | using System;
 | 
|---|
| 2 | using System.Collections.Generic;
 | 
|---|
| 3 | using System.Reflection;
 | 
|---|
| 4 | using System.Text;
 | 
|---|
| 5 | using CLAP;
 | 
|---|
| 6 | using CLAP.Interception;
 | 
|---|
| 7 | using CLAP.Validation;
 | 
|---|
| 8 | 
 | 
|---|
| 9 | namespace xmlTools
 | 
|---|
| 10 | {
 | 
|---|
| 11 |     // Define a class to receive parsed values
 | 
|---|
| 12 |     class ParametersParser
 | 
|---|
| 13 |     {
 | 
|---|
| 14 |         private static string globalFileName="";
 | 
|---|
| 15 |         private static string globalElement="";
 | 
|---|
| 16 |         private static string globalParentElement="";
 | 
|---|
| 17 | 
 | 
|---|
| 18 |         [Verb]
 | 
|---|
| 19 |         public static void addValue(
 | 
|---|
| 20 |             [Parameter(Required = true, Description = "Values to add to Element. Separated multiple by spaces.")] string value
 | 
|---|
| 21 |             )
 | 
|---|
| 22 |         {
 | 
|---|
| 23 |             initialChecks();
 | 
|---|
| 24 |             XmlTools myTools = new XmlTools(globalElement, globalParentElement);
 | 
|---|
| 25 |             List<string> filesToProcess = getFilesToProcess(globalFileName);
 | 
|---|
| 26 |             foreach (string currentFile in filesToProcess)
 | 
|---|
| 27 |             {
 | 
|---|
| 28 |                 myTools.addValues(currentFile, value);
 | 
|---|
| 29 |             }
 | 
|---|
| 30 |             printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
 | 
|---|
| 31 |         }
 | 
|---|
| 32 | 
 | 
|---|
| 33 |         [Verb]
 | 
|---|
| 34 |         public static void removeValue(
 | 
|---|
| 35 |             [Parameter(Required = true, Description = "Values to remove of Element. Separated multiple by spaces.")] string value
 | 
|---|
| 36 |             )
 | 
|---|
| 37 |         {
 | 
|---|
| 38 |             initialChecks();
 | 
|---|
| 39 |             XmlTools myTools = new XmlTools(globalElement, globalParentElement);
 | 
|---|
| 40 |             List<string> filesToProcess = getFilesToProcess(globalFileName);
 | 
|---|
| 41 |             foreach (string currentFile in filesToProcess)
 | 
|---|
| 42 |             {
 | 
|---|
| 43 |                 myTools.removeValues(currentFile, value);
 | 
|---|
| 44 |             }
 | 
|---|
| 45 |             printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
 | 
|---|
| 46 |         }
 | 
|---|
| 47 | 
 | 
|---|
| 48 |         [Verb]
 | 
|---|
| 49 |         public static void updateChainValues(
 | 
|---|
| 50 |             [Parameter(Required = true, Description = "The new first value of the chain. All the chain will be updated based on this value")] string newValue,
 | 
|---|
| 51 |             [Parameter(Description = "Value which have some kind of relation with -newVal \n Together with -newVal updates all the values based on the" +
 | 
|---|
| 52 |                 "-newVal and another position specified on -valRelation parameter (basically starts with (newVal-valRelation) ) Is especially useful when" +
 | 
|---|
| 53 |                 "updating multiple related chains (on different files), like multiple objects from one position to another. Don't use with -filename, because" +
 | 
|---|
| 54 |                 "it will only update one file. \nExample: xmlTools.exe -newVal \"1 1 1\" -valRelation \"4 4 4\" -valElement Translation -valParentElement" +
 | 
|---|
| 55 |                 "OBANKeyFrame")] string valRelation,
 | 
|---|
| 56 |             [Parameter(Description = "Only update specific positions. Positions starts with 0, separted multiple positions with space. Example: valPositions=0 1 4")] [MoreThan(-1)]  string valPositions
 | 
|---|
| 57 |             )
 | 
|---|
| 58 |         {
 | 
|---|
| 59 |             initialChecks();
 | 
|---|
| 60 |             XmlTools myTools = new XmlTools(globalElement, globalParentElement);
 | 
|---|
| 61 |             List<string> filesToProcess = getFilesToProcess(globalFileName);
 | 
|---|
| 62 |             foreach (string currentFile in filesToProcess)
 | 
|---|
| 63 |             {
 | 
|---|
| 64 |                 myTools.changeValue(currentFile, newValue, valRelation, valPositions);
 | 
|---|
| 65 |             }
 | 
|---|
| 66 |             printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
 | 
|---|
| 67 |         }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |         [Verb(Description = "Inverts a chain (like an OBAN animation). Example: xmlTools.exe -filename OBANheli_rotorblades08.xml -invert -valElement Translation -valParentElement OBANKeyFrame (inverts translation chain)")]
 | 
|---|
| 70 |         public static void Invert()
 | 
|---|
| 71 |         {
 | 
|---|
| 72 |             initialChecks();
 | 
|---|
| 73 |             XmlTools myTools = new XmlTools(globalElement, globalParentElement);
 | 
|---|
| 74 |             List<string> filesToProcess = getFilesToProcess(globalFileName);
 | 
|---|
| 75 |             foreach (string currentFile in filesToProcess)
 | 
|---|
| 76 |             {
 | 
|---|
| 77 |                 myTools.invert(currentFile); //Inverting the element order
 | 
|---|
| 78 |             }
 | 
|---|
| 79 |             printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
 | 
|---|
| 80 |         }
 | 
|---|
| 81 | 
 | 
|---|
| 82 |         [Verb]
 | 
|---|
| 83 |         public static void replaceValue(
 | 
|---|
| 84 |             [Parameter(Required = true, Description = "Old value to replace in Element.")] string oldValue,
 | 
|---|
| 85 |             [Parameter(Required = true, Description = "New value to replace in Element.")] string newValue
 | 
|---|
| 86 |             )
 | 
|---|
| 87 |         {
 | 
|---|
| 88 |             XmlTools myTools = new XmlTools(globalElement, globalParentElement);
 | 
|---|
| 89 |             List<string> filesToProcess = getFilesToProcess(globalFileName);
 | 
|---|
| 90 |             foreach (string currentFile in filesToProcess)
 | 
|---|
| 91 |             {
 | 
|---|
| 92 |                 myTools.replaceValue(currentFile, oldValue, newValue);
 | 
|---|
| 93 |             }
 | 
|---|
| 94 |             printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
 | 
|---|
| 95 |         }
 | 
|---|
| 96 | 
 | 
|---|
| 97 |         [Verb]
 | 
|---|
| 98 |         public static void replaceAll(
 | 
|---|
| 99 |             [Parameter(Required = true, Description = "Value to replace in Element. Replace all values of a element by another value.")] string value,
 | 
|---|
| 100 |             [Parameter(Description = "Only replace specific positions. Positions starts with 0, separted multiple positions with space. Example: valPositions=0 1 4")] [MoreThan(-1)] string valPositions
 | 
|---|
| 101 |             )
 | 
|---|
| 102 |         {
 | 
|---|
| 103 |             initialChecks();
 | 
|---|
| 104 |             XmlTools myTools = new XmlTools(globalElement, globalParentElement);
 | 
|---|
| 105 |             List<string> filesToProcess = getFilesToProcess(globalFileName);
 | 
|---|
| 106 |             foreach (string currentFile in filesToProcess)
 | 
|---|
| 107 |             {
 | 
|---|
| 108 |                 if (valPositions != null)
 | 
|---|
| 109 |                 {
 | 
|---|
| 110 |                     myTools.replaceAll(currentFile, value, valPositions);
 | 
|---|
| 111 |                 }
 | 
|---|
| 112 |                 else
 | 
|---|
| 113 |                 {
 | 
|---|
| 114 |                     myTools.replaceAll(currentFile, value);
 | 
|---|
| 115 |                 }
 | 
|---|
| 116 |             }
 | 
|---|
| 117 |             printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
 | 
|---|
| 118 |         }
 | 
|---|
| 119 | 
 | 
|---|
| 120 |         /// <summary>
 | 
|---|
| 121 |         /// Patch in files can be used with wildcard or empty filename instead
 | 
|---|
| 122 |         /// </summary>
 | 
|---|
| 123 |         /// <param name="filename"></param>
 | 
|---|
| 124 |         [Verb]
 | 
|---|
| 125 |         public static void patchFile(
 | 
|---|
| 126 |             [Parameter(Description = "Force the specified patch to run in specified files")] string forceInFiles
 | 
|---|
| 127 |             )
 | 
|---|
| 128 |         {
 | 
|---|
| 129 |             XmlPatch myPatch;
 | 
|---|
| 130 | 
 | 
|---|
| 131 |             if (forceInFiles != "")
 | 
|---|
| 132 |             {
 | 
|---|
| 133 |                 myPatch = new XmlPatch(globalFileName, forceInFiles);
 | 
|---|
| 134 |             }
 | 
|---|
| 135 |             else
 | 
|---|
| 136 |             {
 | 
|---|
| 137 |                 myPatch = new XmlPatch(globalFileName);
 | 
|---|
| 138 |             }
 | 
|---|
| 139 | 
 | 
|---|
| 140 |             myPatch.startPatch();
 | 
|---|
| 141 |         }
 | 
|---|
| 142 | 
 | 
|---|
| 143 |         [Verb]
 | 
|---|
| 144 |         public static void version()
 | 
|---|
| 145 |         {
 | 
|---|
| 146 |             Console.WriteLine("xmlTools v" + Program.toolsVersion);
 | 
|---|
| 147 |             Console.WriteLine("\nWritten by s10k");
 | 
|---|
| 148 |         }
 | 
|---|
| 149 | 
 | 
|---|
| 150 |         // Global Parameters
 | 
|---|
| 151 |         [Global]
 | 
|---|
| 152 |         public static void fileName(
 | 
|---|
| 153 |              [Parameter(Required = true, Description = "Filename to apply the operations (with patchFile specifies the patch filename). Wildcards accepted for multiple files. No filename = search all .xml files in current path.")] string filename // xml filename. Wildcards accepted.
 | 
|---|
| 154 |             )
 | 
|---|
| 155 |         {
 | 
|---|
| 156 |             globalFileName = filename;
 | 
|---|
| 157 |         }
 | 
|---|
| 158 | 
 | 
|---|
| 159 |         [Global]
 | 
|---|
| 160 |         public static void element(
 | 
|---|
| 161 |              [Parameter(Required = true, Description = "Element to apply the operation.")] string element
 | 
|---|
| 162 |             )
 | 
|---|
| 163 |         {
 | 
|---|
| 164 |             globalElement = element;
 | 
|---|
| 165 |         }
 | 
|---|
| 166 | 
 | 
|---|
| 167 |         [Global]
 | 
|---|
| 168 |         public static void parElement(
 | 
|---|
| 169 |              [Parameter(Required = true, Description = "Parent of the Element to apply the operation.")] string parentElement
 | 
|---|
| 170 |             )
 | 
|---|
| 171 |         {
 | 
|---|
| 172 |             globalParentElement = parentElement;
 | 
|---|
| 173 |         }
 | 
|---|
| 174 | 
 | 
|---|
| 175 |         //[PreVerbExecution]
 | 
|---|
| 176 |         //private static void InitialChecks(PreVerbExecutionContext context)
 | 
|---|
| 177 |         //{
 | 
|---|
| 178 |         //    bool elementFound = false;
 | 
|---|
| 179 |         //    foreach(ParameterAndValue param in context.Parameters){
 | 
|---|
| 180 |         //        if (param.Parameter.Names[0] == "element")
 | 
|---|
| 181 |         //        {
 | 
|---|
| 182 |         //            if(!String.IsNullOrEmpty(param.Value.ToString().Trim())){
 | 
|---|
| 183 |         //                elementFound = true;
 | 
|---|
| 184 |         //            }
 | 
|---|
| 185 |         //            break;
 | 
|---|
| 186 |         //        }
 | 
|---|
| 187 |         //    }
 | 
|---|
| 188 |         //    if (!elementFound)
 | 
|---|
| 189 |         //    {
 | 
|---|
| 190 |         //        Console.Error.WriteLine("You must specify the element parameter where the operations will be processed.");
 | 
|---|
| 191 |         //        Console.ReadLine();
 | 
|---|
| 192 |         //        System.Environment.Exit(1);
 | 
|---|
| 193 |         //    }
 | 
|---|
| 194 |         //}
 | 
|---|
| 195 | 
 | 
|---|
| 196 |         // Private functions
 | 
|---|
| 197 |         private static List<String> getFilesToProcess(String filename)
 | 
|---|
| 198 |         {
 | 
|---|
| 199 |             List<String> filesToProccess = new List<String>();
 | 
|---|
| 200 | 
 | 
|---|
| 201 |             if (filename == "") // No filename? Process everything xml file found.
 | 
|---|
| 202 |             {
 | 
|---|
| 203 |                 List<string> allXmlFiles = Util.getAllXmlFiles();
 | 
|---|
| 204 |                 foreach (String file in allXmlFiles)
 | 
|---|
| 205 |                 {
 | 
|---|
| 206 |                     filesToProccess.Add(file);
 | 
|---|
| 207 |                 }
 | 
|---|
| 208 |             }
 | 
|---|
| 209 |             else if (Util.containsWildcard(filename)) // Contains wildcards? Get all files that match it.
 | 
|---|
| 210 |             {
 | 
|---|
| 211 |                 List<string> matchingWildcardFiles = Util.getXmlFilesWildcard(filename);
 | 
|---|
| 212 |                 foreach (String file in matchingWildcardFiles)
 | 
|---|
| 213 |                 {
 | 
|---|
| 214 |                     filesToProccess.Add(file);
 | 
|---|
| 215 |                 }
 | 
|---|
| 216 |             }
 | 
|---|
| 217 |             else // Add the file specified
 | 
|---|
| 218 |             {
 | 
|---|
| 219 |                 if (System.IO.File.Exists(filename))
 | 
|---|
| 220 |                 {
 | 
|---|
| 221 |                     filesToProccess.Add(filename);
 | 
|---|
| 222 |                 }
 | 
|---|
| 223 |                 else
 | 
|---|
| 224 |                 {
 | 
|---|
| 225 |                     Program.printAppError(Program.appErrors.FILE_NOT_FOUND, "The file specified: " + filename + " doesn't exists.", true);
 | 
|---|
| 226 |                 }
 | 
|---|
| 227 |             }
 | 
|---|
| 228 |             return filesToProccess;
 | 
|---|
| 229 |         }
 | 
|---|
| 230 | 
 | 
|---|
| 231 |         private static void printProcessedMessage(int count, string methodName)
 | 
|---|
| 232 |         {
 | 
|---|
| 233 |             Console.WriteLine(count + " files processed with " + methodName + " command.");
 | 
|---|
| 234 |         }
 | 
|---|
| 235 | 
 | 
|---|
| 236 |         private static void initialChecks()
 | 
|---|
| 237 |         {
 | 
|---|
| 238 |             if (String.IsNullOrEmpty(globalElement.Trim()))
 | 
|---|
| 239 |             {
 | 
|---|
| 240 |                 Program.printAppError(Program.appErrors.ELEMENT_NOT_FOUND, "You must specify the element parameter where the operations will be processed.", true);
 | 
|---|
| 241 |             }
 | 
|---|
| 242 | 
 | 
|---|
| 243 |         }
 | 
|---|
| 244 |     }
 | 
|---|
| 245 | }
 | 
|---|