Exemplo n.º 1
0
 /// <summary>
 /// Create a CmdS from a string array
 /// </summary>
 /// <param name="script">An array of strings </param>
 /// <returns>A valid Cmd or null</returns>
 public static CmdS CreateFromStrings(string[] script)
 {
     // must be 2 at least
     if (script.Length < 2)
     {
         return(null);               // ERROR exit
     }
     if (double.TryParse(script[1], out double gs))
     {
         var cmd = new CmdS(gs);
         if (script.Length > 2)
         {
             // may have accel
             if (double.TryParse(script[2], out double accel))
             {
                 cmd.Accel = accel;
             }
             if (script.Length > 3)
             {
                 // may have immediate
                 if (int.TryParse(script[3], out int immediate))
                 {
                     cmd.Immediate = (immediate > 0);
                 }
             }
         }
         return(cmd);
     }
     return(null); // ERROR number parse
 }
Exemplo n.º 2
0
        /// <summary>
        /// Reads one Script file
        /// NOTE: Does not check for command inconsistencies whatsoever
        /// only a limited format check is done
        /// </summary>
        /// <param name="filename">The file to read</param>
        /// <returns>A CmdList either populated or empty on Error</returns>
        public static CmdList ReadCmdScript(string filename)
        {
            var list = new CmdList( );

            if (!File.Exists(filename))
            {
                return(list);                     // ERROR return an empty list
            }
            using (var sr = new StreamReader(filename)) {
                bool expectCmdA = true;
                do
                {
                    string   buffer = sr.ReadLine( ).Trim( );
                    string[] x      = buffer.Split(new char[] { '#' }); // cut comments
                    if (x.Length > 0 && x[0].Length > 0)
                    {
                        // use first part only
                        string[] e = x[0].Split(new char[] { '=', ';' });
                        if (e.Length < 2)
                        {
                            continue;     // at least 2 items should be there... Cmd and Arg1
                        }
                        if (expectCmdA)
                        {
                            // start of script expected - cmd A accepted only
                            var a = CmdA.CreateFromStrings(e);
                            if (a == null)
                            {
                                return(list); // ERROR in CmdA
                            }
                            list.Enqueue(a);
                            expectCmdA = false; // we have it
                        }

                        else
                        {
                            // rest of script
                            switch (e[0].Substring(0, 1))
                            {
                            case "D":
                                var d = CmdD.CreateFromStrings(e);
                                if (d != null)
                                {
                                    list.Enqueue(d);
                                }
                                break;

                            case "T":
                                var t = CmdT.CreateFromStrings(e);
                                if (t != null)
                                {
                                    list.Enqueue(t);
                                }
                                break;

                            case "H":
                                var h = CmdH.CreateFromStrings(e);
                                if (h != null)
                                {
                                    list.Enqueue(h);
                                }
                                break;

                            case "G":
                                var g = CmdG.CreateFromStrings(e);
                                if (g != null)
                                {
                                    list.Enqueue(g);
                                }
                                break;

                            case "S":
                                var s = CmdS.CreateFromStrings(e);
                                if (s != null)
                                {
                                    list.Enqueue(s);
                                }
                                break;

                            case "V":
                                var v = CmdV.CreateFromStrings(e);
                                if (v != null)
                                {
                                    list.Enqueue(v);
                                }
                                break;

                            case "M":
                                var m = CmdM.CreateFromStrings(e);
                                if (m != null)
                                {
                                    list.Enqueue(m);
                                }
                                break;

                            default:
                                // not supported ..
                                break;
                            }
                        }
                    }
                } while (!sr.EndOfStream);
            }
            list.Enqueue(new CmdE( )); // MUST be the last one
            return(list);
        }