Beispiel #1
0
        public static OpsParsedStatement[] ParseScript(string script)
        {
            if (script == null || script.Length == 0)
            {
                return(null);
            }

            if (OpsConsole.Verbose)
            {
                OpsConsole.WriteLine("Parsing script starting.");
            }


            char lastChar = script[script.Length - 1];

            if (lastChar != ';' && lastChar != '\n')
            {
                script += '\n';
            }

            ArrayList parsedStatements = new ArrayList();

            int iStatement = 0;

            for (Match statementMatch = StatementReg.Match(script); statementMatch.Success; iStatement++, statementMatch = statementMatch.NextMatch())
            {
                try
                {
                    OpsParsedStatement parsedStatement = ParseStatement(statementMatch);

                    //statements can be null  such as a ;
                    if (parsedStatement != null)
                    {
                        parsedStatements.Add(parsedStatement);

                        if (OpsConsole.Verbose)
                        {
                            OpsConsole.WriteLine("Parsing statement: \"{0}\"", parsedStatement);
                        }
                    }
                }
                catch (Exception e)
                {
                    OpsConsole.WriteError(iStatement + 1, e, "Could not parse statement: {0}", statementMatch.Result("${statement}"));
                    throw;
                }
            }

            OpsParsedStatement[] result = new OpsParsedStatement[parsedStatements.Count];
            parsedStatements.CopyTo(result, 0);


            if (OpsConsole.Verbose)
            {
                OpsConsole.WriteLine("Found {0} statements.", result.Length);
                OpsConsole.WriteLine("Parsing script finished.");
            }

            return(result);
        }
Beispiel #2
0
        public static int Main(string[] args)
        {
            int result = 0;

            try
            {
                string unparsedScript = ParseCommandLine(args);

                CommandLibrary.FindCommands();

                OpsContext context = new OpsContext(CreateDevice());

                OpsScript script = new OpsScript(CommandLibrary, unparsedScript);

                script.Run(context);

                if (OpsConsole.Verbose)
                {
                    OpsConsole.WriteLine("Success");
                }
            }

            catch
            {
                OpsConsole.WriteLine("Application cannot continue due to errors.");
                result = -1;
            }
            finally
            {
                Close();
            }

            return(result);
        }
Beispiel #3
0
        public static void PrintHelp()
        {
            if (!PrintedHelpOnce)
            {
                PrintedHelpOnce = true;

                Version version = Assembly.GetEntryAssembly().GetName().Version;

                string verStr = String.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision.ToString("D4"));

                OpsConsole.WriteLine("Microsoft (R) DirectX Operations Tool v" + verStr);
                OpsConsole.WriteLine("Copyright (C) Microsoft Corporation 2005. All rights reserved.");
                OpsConsole.WriteLine("");
                OpsConsole.WriteLine("Usage: dxops [-?] [-log file] [[-s \"script\"] -s \"script2\"] [[-f file] -f file2]");
                OpsConsole.WriteLine("     -?              Shows this help message.");
                OpsConsole.WriteLine("     -log \"file\"     Specifies the location of the log output to create.");
                OpsConsole.WriteLine("     -loga \"file\"    Specifies the location of the log output to append.");
                OpsConsole.WriteLine("     -s \"script\"     Execute the specified script statements.");//dont remove the extra spaces... they make it line up.
                OpsConsole.WriteLine("     -f \"file\"       Execute the script in the specified file.");
                OpsConsole.WriteLine("     -m \"path\"       Add a media path to search for file specified without");
                OpsConsole.WriteLine("                     a full path.");
                OpsConsole.WriteLine("     -verbose        Verbose printing of info, warning, and errors.");
                OpsConsole.WriteLine("");
                OpsConsole.WriteLine("For details on this tool and its operations view the DirectX SDK documentation.");
            }
        }
Beispiel #4
0
        public void Run(OpsContext context)
        {
            if (Statements == null)
            {
                return;
            }

            if (OpsConsole.Verbose)
            {
                OpsConsole.WriteLine("Processing script starting");
            }

            for (int i = 0; i < Statements.Length; i++)
            {
                try
                {
                    if (OpsConsole.Verbose)
                    {
                        OpsConsole.WriteLine("Processing statement #{0}: \"{1}\"", i + 1, Statements[i].Parsed);
                    }

                    Statements[i].Run(context);
                }
                catch (Exception e)
                {
                    OpsConsole.WriteError(i + 1, e, "Failure processing statement.");
                    throw;
                }
            }

            if (OpsConsole.Verbose)
            {
                OpsConsole.WriteLine("Processing script finished");
            }
        }
Beispiel #5
0
        private static void PrintableExceptionInternal(StringBuilder sb, Exception e)
        {
            if (e is OpsException)
            {
                if (sb.Length != 0)
                {
                    sb.Append(" -AND- ");
                }

                if (e.Message != null)
                {
                    sb.Append(e.Message);
                }
            }
#if (DEBUG_DPFALL)
            else
            {
                sb.Append(e.ToString());
            }
#endif

            if (e.InnerException != null)
            {
                OpsConsole.PrintableExceptionInternal(sb, e.InnerException);
            }
        }
Beispiel #6
0
        private static void Close()
        {
            OpsConsole.Close();

#if ( DEBUG_PRESSKEY )
            OpsConsole.WriteLine("Press ENTER to continue.");
            Console.ReadLine();
#endif
        }
Beispiel #7
0
        public void FindCommands()
        {
            //This code will hit a broader set of assemblies
            //I left it here encase it is needed by the user or for future work.
            //AppDomain myDomain = AppDomain.CurrentDomain;
            //Assembly[] assemblies = myDomain.GetAssemblies();
            // foreach (Assembly assembly in assemblies)
            //{
            Assembly assembly = Assembly.GetExecutingAssembly();

            try
            {
                Type[] types = assembly.GetTypes();

                if (types != null)
                {
                    foreach (Type type in types)
                    {
                        if (type.Namespace == "Microsoft.DirectX.Solutions.DxOps.Commands")
                        {
                            if (type.IsClass &&
                                !type.IsAbstract &&
                                typeof(IOpsCommand).IsAssignableFrom(type))
                            {
                                //valid command;
                                try
                                {
                                    IOpsCommand commandObj = Activator.CreateInstance(type) as IOpsCommand;

                                    AddCommand(commandObj);
                                }
                                catch
                                {
                                    OpsConsole.WriteError(null, "Could not create instance of type: " + type.FullName);
                                    throw;
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Reflection.ReflectionTypeLoadException e)
            {
                OpsConsole.WriteError(e, "Could not get type from assembly: " + assembly.FullName);
                throw;
            }
            //}
        }
Beispiel #8
0
        public static string PrintableException(Exception e)
        {
            if (e == null)
            {
                return(null);
            }

            StringBuilder sb = new StringBuilder();

            OpsConsole.PrintableExceptionInternal(sb, e);

            if (sb.Length == 0)
            {
                sb.Append("Internal Error");
            }

            return(sb.ToString());
        }
Beispiel #9
0
        private static string ParseCommandLine(string[] args)
        {
            StringBuilder scriptBuilder = new StringBuilder();
            bool          help          = false;

            ArrayList argList = new ArrayList(args);

            //force use of arglist isntead of args
            args = null;

            string arg1 = null;
            string arg2 = null;

            try
            {
                //expand all environment variables
                for (int iArg = 0; iArg < argList.Count; iArg++)
                {
                    arg1 = argList[iArg] as string;

                    argList[iArg] = System.Environment.ExpandEnvironmentVariables(arg1);
                }

                //process verbose first so that
                for (int iArg = 0; iArg < argList.Count; iArg++)
                {
                    arg1 = argList[iArg] as string;
                    arg2 = null;

                    if (arg1 == "-verbose" || arg1 == "/verbose")
                    {
                        OpsConsole.Verbose = true;

                        if (OpsConsole.Verbose)
                        {
                            OpsConsole.WriteLine("Activating verbose mode.");
                        }

                        argList.RemoveAt(iArg);
                        break;
                    }
                }

                if (OpsConsole.Verbose)
                {
                    OpsConsole.WriteLine("Command-line processing starting.");
                }

                //search for -log or -loga first so that console out is always the same
                //we do not support more than one -log or -loga
                for (int iArg = 0; iArg < argList.Count; iArg++)
                {
                    arg1 = argList[iArg] as string;
                    arg2 = null;

                    if (arg1 == "-log" || arg1 == "/log")
                    {
                        arg2 = ((string)argList[iArg + 1]).Trim();

                        OpsConsole.RouteConsoleToFile(arg2, false);

                        if (OpsConsole.Verbose)
                        {
                            OpsConsole.WriteLine("Logging to new file: \"{0}\"", arg2);
                        }

                        argList.RemoveRange(iArg, 2);
                        break;
                    }
                    else if (arg1 == "-loga" || arg1 == "/loga")
                    {
                        arg2 = ((string)argList[iArg + 1]).Trim();

                        OpsConsole.RouteConsoleToFile(arg2, true);

                        if (OpsConsole.Verbose)
                        {
                            OpsConsole.WriteLine("Logging to end of file: \"{0}\"", arg2);
                        }

                        argList.RemoveRange(iArg, 2);
                        break;
                    }
                }

                //process the rest of the commands
                for (int iArg = 0; iArg < argList.Count; iArg++)
                {
                    arg1 = argList[iArg] as string;
                    arg2 = null;

                    if (arg1 == "-?" || arg1 == "/?" || arg1 == "-help" || arg1 == "/help")
                    {
                        help = true;

                        if (OpsConsole.Verbose)
                        {
                            OpsConsole.WriteLine("Found help request.");
                        }
                    }
                    else if (arg1 == "-s" || arg1 == "/s")
                    {
                        arg2 = ((string)argList[++iArg]).Trim();

                        scriptBuilder.Append(arg2);
                        scriptBuilder.Append(';');

                        if (OpsConsole.Verbose)
                        {
                            OpsConsole.WriteLine("Adding command-line script.");
                        }
                    }
                    else if (arg1 == "-f" || arg1 == "/f")
                    {
                        arg2 = ((string)argList[++iArg]).Trim();

                        StreamReader sr = File.OpenText(arg2);
                        if (sr == null)
                        {
                            throw new OpsException("Could not read script file:" + arg2);
                        }

                        scriptBuilder.Append(sr.ReadToEnd().Trim());
                        scriptBuilder.Append(';');

                        if (OpsConsole.Verbose)
                        {
                            OpsConsole.WriteLine("Adding script from file: \"{0}\"", arg2);
                        }
                    }
                    else if (arg1 == "-m" || arg1 == "/m")
                    {
                        arg2 = ((string)argList[++iArg]).Trim();

                        string searchPath = System.IO.Path.GetFullPath(arg2);

                        if (System.IO.Directory.Exists(searchPath))
                        {
                            SearchPaths.Push(searchPath);
                        }
                        else
                        {
                            OpsConsole.WriteLine("Search path does not exist: \"{0}\"", searchPath);
                        }

                        if (OpsConsole.Verbose)
                        {
                            OpsConsole.WriteLine("Adding search path: \"{0}\"", arg2);
                        }
                    }
                    else
                    {
                        switch (arg1)
                        {
                        case "-log":
                        case "/log":
                        case "-loga":
                        case "/loga":
                        case "-verbose":
                        case "/verbose":
                            throw new OpsException(String.Format("Duplicate command line argument not allowed: {0}", arg1));

                        default:
                            throw new OpsException(String.Format("Invalid command line argument: {0}", arg1));
                        }
                        ;
                    }
                }

                if (OpsConsole.Verbose)
                {
                    OpsConsole.WriteLine("Command-line processing finished.");
                }

                if (help || (scriptBuilder.Length == 0))
                {
                    PrintHelp();
                }
            }
            catch (Exception e)
            {
                OpsConsole.WriteError(e, String.Format("An error was found durring command-line processing of {0} {1}", arg1, arg2));
                throw;
            }

            return(scriptBuilder.ToString());
        }
Beispiel #10
0
        public OpsScript(OpsCommandLibrary library, string script)
        {
            OpsParsedStatement[] parsedStatements = OpsParser.ParseScript(script);
            if (parsedStatements != null)
            {
                if (OpsConsole.Verbose)
                {
                    OpsConsole.WriteLine("Prevalidating script starting.");
                }

                Statements = new OpsStatement[parsedStatements.Length];
                for (int i = 0; i < parsedStatements.Length; i++)
                {
                    OpsParsedStatement parsed    = parsedStatements[i] as OpsParsedStatement;
                    IOpsCommand        command   = null;
                    object             arguments = null;

                    if (OpsConsole.Verbose)
                    {
                        OpsConsole.WriteLine("Prevalidating statement #{0}: \"{1}\"", i, parsed);
                    }


                    try
                    {
                        command = library.GetCommand(parsed.Command);
                    }
                    catch (Exception e)
                    {
                        OpsConsole.WriteError(i + 1, e, "Unable to fetch command: " + parsed.Command);
                        throw;
                    }

                    if (command is IOpsRemappable)
                    {
                        try
                        {
                            command = (command as IOpsRemappable).RemapCommand(parsed);
                        }
                        catch (Exception e)
                        {
                            OpsConsole.WriteError(i + 1, e, "Unable to remap command: " + parsed.Command);
                            throw;
                        }
                    }

                    try
                    {
                        arguments = command.ParseArguments(parsed);
                    }
                    catch (Exception e)
                    {
                        OpsConsole.WriteError(i + 1, e, "Command failed to interpret the arguments.");
                        throw;
                    }

                    Statements[i] = new OpsStatement(parsed, command, arguments);
                }

                if (OpsConsole.Verbose)
                {
                    OpsConsole.WriteLine("Prevalidating script finished.");
                }
            }
        }