Beispiel #1
0
 private void textBoxOutput_LinkClicked(object sender, LinkClickedEventArgs e)
 {
     Cmd.Process("start(\"" + e.LinkText + "\");");
 }
Beispiel #2
0
        public static void Break()
        {
            if (GlobalVars.UsingAPI)
            {
                Cmd.Process("ThreadAbortAll();");
                EConsole.WriteLine("API_BREAK");
                return;
            }
            try
            {
                while (true)
                {
                    Cmd.Process("ShowConsole();");

                    if (Variables.GetBool("abortAfterBreak"))
                    {
                        Cmd.Process("ThreadAbortAll();");
                        Cmd.Process("HideStatus();");
                    }

                    if (Variables.GetBool("exitAfterBreak"))
                    {
                        Cmd.Process("exit(0);");
                    }

                    ChoiceWindow c = new ChoiceWindow();

                    c.SetText("Choose something or press cancel to exit");
                    c.SetCaption("CmdSharp - break mode");

                    ChoiceWndButton[] buttons =
                    {
                        new ChoiceWndButton("Restart cmd#"),
                        new ChoiceWndButton("Open log in a text editor"),
                        new ChoiceWndButton("Display variables list"),
                        new ChoiceWndButton("Enter debug mode and attach debugger"),
                        new ChoiceWndButton("Go to single-command interpreter mode"),
                        new ChoiceWndButton("Exit")
                    };
                    c.SetButtons(buttons);

                    c.ShowDialog();
                    var result = c.GetAnswer();

                    if (result.ChoiceWndButton == null)
                    {
                        Cmd.Process("exit(0);");
                    }
                    else if (result.ChoiceWndButton == buttons[0]) // restart
                    {
                        Cmd.Process("Restart();");
                    }
                    else if (result.ChoiceWndButton == buttons[1]) // log
                    {
                        Cmd.Process($"start(\"notepad\", \"{(string)Variables.Get("logFile")}\");");
                    }
                    else if (result.ChoiceWndButton == buttons[2]) // set
                    {
                        Cmd.Process("VarList();");
                    }
                    else if (result.ChoiceWndButton == buttons[3]) // debug
                    {
                        Cmd.Process("Debug(true);");
                    }
                    else if (result.ChoiceWndButton == buttons[4]) // commandline
                    {
                        EConsole.WriteLine("Use: \"Break();\" to return back.", ConsoleColor.Gray);
                        CommandLine();
                    }
                    else  // exit
                    {
                        Cmd.Process("exit(0);");
                    }
                }
            }
            catch (Exception ex)
            {
                BachokMessage(ex);
            }
            Break();
        }
Beispiel #3
0
 private void CustomConsoleWindow_FormClosing(object sender, FormClosingEventArgs e)
 {
     Cmd.Process("exit();");
 }
Beispiel #4
0
        //public static void PrintResult(string result, string prefix = "null")
        //{
        //    Cmd.Process("PrintResult {#" + result + "}||" + prefix, null, true);
        //}

        public static void Init(string[] args, APICore api = null)
        {
            GlobalVars.API = api;

            Variables.Initialize(true, args);
            EnvironmentManager.Initialize();


            if (args.Contains <string>("-noUiConsole") || (bool)Cmd.Process("IsKeyDown(\"RShiftKey\");"))
            {
                Variables.Set("useCustomConsole", false, new List <string>()
                {
                    "CmdSharp", "Hidden", "ReadOnly"
                });
            }
            else
            {
                Variables.Set("useCustomConsole", true, new List <string>()
                {
                    "CmdSharp", "Hidden", "ReadOnly"
                });
            }

            try
            {
                string f = GetLogFile();
                if (f == null)
                {
                    throw new Exception();
                }
                log           = new StreamWriter(f);
                log.AutoFlush = true;

                Variables.Set("logFile", new FileInfo(f).FullName, new List <string>()
                {
                });

#if DEBUG
                Debugger.Launch();
#endif
            }
            catch
            {
                log = null;
            }

#if DEBUG
            Cmd.Process("Debug();");
#endif


            if (args.Contains <string>("-console") || (bool)Cmd.Process("IsKeyDown(\"LControlKey\");"))
            {
                Variables.SetVariableObject("forceConsole", true);
                Cmd.Process("ShowConsole();");
            }


            if (args.Contains <string>("-debug") || (bool)Cmd.Process("IsKeyDown(\"LShiftKey\");"))
            {
                Cmd.Process("Debug();");
            }

#if ESCODE_IMPLEMENTED
            //new ESCode(ESCode.SplitCode(ReadResource("escript.Functions.Functions.es")));
            //Debug.DebugText("escript.Functions.Functions.es imported");
#else
            Debug.DebugText("Functions.es is *not* imported: not implemented", ConsoleColor.DarkRed);
#endif

            if (args.Contains <string>("-ignoreRunasRestart"))
            {
                GlobalVars.IgnoreRunAsAdmin = true;
            }

            if (args.Contains <string>("/help") || args.Contains <string>("/?") || args.Contains <string>("--h") || args.Contains <string>("-help"))
            {
                Cmd.Process("ShowConsole();");
                EConsole.ForegroundColor = ConsoleColor.Gray;
                EConsole.WriteLine("CmdSharp <Script Path> [/help] [/convert] [-ignoreRunasRestart] [-console] [-debug] [-cmd] [-close] [-install]");
                EConsole.WriteLine("");
                EConsole.WriteLine("/help\t\t\tShow this article");
                EConsole.WriteLine("-ignoreRunAsRestart\tIgnore 'Restart 1'");
                EConsole.WriteLine("-console\t\tUse ConsoleMessageBox instead of Windows Forms");
                EConsole.WriteLine("-debug\t\t\tStart with debug mode");
                EConsole.WriteLine("-cmd\t\t\tStart command interpretator");
                EConsole.WriteLine("-install\t\tStart installation");
                EConsole.WriteLine(" ");
                EConsole.WriteLine("<Script Path> /convert <Out Path> <Icon Path>\tConvert .es to .exe");
                EConsole.WriteLine("Press any key...");
                EConsole.ReadKey();
                Environment.Exit(0);
            }

            if (args.Contains <string>("/convert"))
            {
                try
                {
                    FileInfo s = new FileInfo(args[0]);

                    if (!File.Exists(s.FullName))
                    {
                        throw new Exception("Script " + args[0] + " not found");
                    }
                    string outPath = "";

                    if (s.Name.Contains(".es"))
                    {
                        outPath = s.Name.Replace(".es", ".exe");
                    }
                    else
                    {
                        outPath = s.Name + ".exe";
                    }
                    try
                    {
                        if (args[2].Length >= 1)
                        {
                            outPath = args[2];
                        }
                    }
                    catch { }

                    string iconArg = "";
                    try
                    {
                        FileInfo ii = new FileInfo(args[3]);
                        if (File.Exists(ii.FullName))
                        {
                            iconArg = ii.FullName;
                        }
                    }
                    catch { }
                    FileInfo outFile = new FileInfo(outPath);

                    bool anykey = false;
                    if (args.Contains("-anykey"))
                    {
                        anykey = true;
                    }

                    CompileScript(s.FullName, outFile.FullName, iconArg, anykey);
                    Environment.Exit(0);
                }
                catch (Exception ex)
                {
                    Cmd.Process("ShowConsole();");
                    EConsole.ForegroundColor = ConsoleColor.Gray;
                    EConsole.WriteLine("ERROR: " + ex.Message);
                    EConsole.WriteLine("");
                    EConsole.WriteLine("Use: CmdSharp <Script Path> /convert (<Compiled File Path>) (<Icon Path>)");
                    EConsole.WriteLine("Examples:");
                    EConsole.WriteLine("CmdSharp \"MyScript.es\" /convert\twill be converted in MyScript.exe");
                    EConsole.WriteLine("CmdSharp \"MyScript.es\" /convert \"C:\\Programs\\MyScript.exe\"");
                    EConsole.WriteLine("CmdSharp \"MyScript.es\" /convert \"C:\\Programs\\MyScript.exe\" \"myicon.ico\"");
                    EConsole.WriteLine("");
                    EConsole.WriteLine("If you want to insert icon, you must set <Compiled File Path>");

                    EConsole.ForegroundColor = ConsoleColor.Gray;
                    EConsole.WriteLine("Press any key to close this window...");
                    EConsole.ReadKey();
                    Environment.Exit(404);
                }
            }

            //if (!args.Contains<string>("-ignoreLib"))
            //{
            //    foreach (var f in GlobalVars.GetAboutMe().Directory.EnumerateFiles())
            //    {
            //        if (f.Name.Contains("ESLib"))
            //        {
            //            Cmd.Process("Import(\"" + f.FullName + "\");");
            //        }
            //    }
            //}

#if !IsCore
            IntPtr resourceInfo = GlobalVars.FindResource(IntPtr.Zero, "script", (IntPtr)10);

            if (resourceInfo != IntPtr.Zero)
            {
                Debug.DebugText("SCRIPT Resource: 0x" + resourceInfo.ToString("X4"));

                uint   size = GlobalVars.SizeofResource(IntPtr.Zero, resourceInfo);
                IntPtr pt   = GlobalVars.LoadResource(IntPtr.Zero, resourceInfo);
                GlobalVars.IsCompiledScript = true;
                Variables.Set("isCompiledScript", "1");
                byte[] bPtr = new byte[size];
                Marshal.Copy(pt, bPtr, 0, (int)size);
                string code = Encoding.ASCII.GetString(bPtr);
                Debug.DebugText("SCRIPT:\r\n" + code);

#if ESCODE_IMPLEMENTED
                ESCode script = new ESCode(ESCode.SplitCode(code));
                script.RunScript();

                Break();
#else
                throw new NotImplementedException("ESCode is not implemented");
#endif
            }
#endif
            if (args.Contains <string>("-install"))
            {
                if (GlobalVars.IsCompiledScript)
                {
                    throw new Exception("Can't install compiled version. Please, use clean ESCRIPT or remove script information from resources manually.");
                }
                string InstallScript = "InstallScript.es";

                WriteResourceToFile("escript.Stuff.InstallScript.es", InstallScript);

#if ESCODE_IMPLEMENTED
                ESCode script = new ESCode(InstallScript);
                script.RunScript();
#else
                throw new NotImplementedException("ESCode is not implemented");
#endif

                Environment.Exit(0);
            }
            if (args.Contains <string>("-assoc"))
            {
                if (GlobalVars.IsCompiledScript)
                {
                    throw new Exception("Can't install compiled version. Please, use clean cmd# or remove script information from resources manually.");
                }
                string me = System.Reflection.Assembly.GetExecutingAssembly().Location;
#if !IsCore
                //Cmd.Process("ShowConsole");

                EConsole.WriteLine("Associating cmd# files...");
                try
                {
                    FileAssociation.AssociateESCRIPT("cmd# Script", me, ".cmdsharp");
                    FileAssociation.AssociateESCRIPT("cmd# Script", me, ".cmdsharp");
                    //FileAssociation.AssociateESCRIPT("ESCRIPT Header File", me, ".cmdsharp", "escriptheader", false);
#if !IsCore
                    FileAssociation.SHChangeNotify(FileAssociation.SHCNE_ASSOCCHANGED, FileAssociation.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
#endif
                }
                catch (Exception ex)
                {
                    EConsole.WriteLine("ERROR: " + ex.Message);
                    Environment.Exit(1);
                }
                EConsole.WriteLine("Creating desktop scripts...");
                try
                {
                    string desktop = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CmdSharp Console.cmdsharp");
                    WriteResourceToFile("CmdSharp.Core.Scripts.Scripts.DesktopShortcut.cmdsharp", desktop);

                    desktop = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CmdSharp Folder.cmdsharp");
                    WriteResourceToFile("CmdSharp.Core.Scripts.Scripts.ExampleFolder.cmdsharp", desktop);
                }
                catch (Exception ex)
                {
                    EConsole.WriteLine("ERROR: " + ex.Message);
                    Environment.Exit(21);
                }
#else
                EConsole.ForegroundColor = ConsoleColor.Red;
                EConsole.WriteLine("WARNING: ESCRIPT Core cannot be installed");
#endif
                EConsole.ForegroundColor = ConsoleColor.Green;
                EConsole.WriteLine("CmdSharp installed!");
                //Thread.Sleep(2000);
                EConsole.WriteLine("");
                Environment.Exit(0);
            }

            try
            {
                if (GlobalVars.UsingAPI)
                {
                    return;
                }

                if ((args.Length <= 0 && !GlobalVars.UsingAPI) || args.Contains <string>("-cmd"))
                {
                    CommandLine();
                }
                else if (File.Exists(args[0]))
                {
#if ESCODE_IMPLEMENTED
                    ESCode script = new ESCode(args[0]);
                    script.RunScript();
#else
                    throw new NotImplementedException("ESCode is not implemented");
#endif
                }
                else if (!File.Exists(args[0]))
                {
                    CommandLine();
                }
            }
            catch (Exception ex)
            {
                BachokMessage(ex);
            }

            Break();
        }
Beispiel #5
0
        public static string CheckForImports(IntPtr resourceUpdateHandle, string scriptContent, string mname, string mainPath)
        {
            string result = scriptContent;
            var    h      = resourceUpdateHandle;

            string[] scriptLines = null;// ESCode.SplitCode(scriptContent);
            foreach (var line in scriptLines)
            {
                if (line.StartsWith("Import "))
                {
                    FileInfo importScriptFile = new FileInfo(line.Remove(0, "Import ".Length));
                    if (!importScriptFile.Exists)
                    {
                        importScriptFile = new FileInfo(Path.Combine(mainPath, line.Remove(0, "Import ".Length)));
                    }

                    if (!importScriptFile.Exists)
                    {
                        EConsole.WriteLine($"[ERROR] File {importScriptFile.FullName} not found ({line})", true, ConsoleColor.Red);
                        continue;
                    }
                    if (importScriptFile.Extension.ToLower() != ".es" && importScriptFile.Extension.ToLower() != ".escript" && importScriptFile.Extension.ToLower() != ".esh")
                    {
                        EConsole.WriteLine($"Import: Unsupported extension: {importScriptFile.Extension}", true, ConsoleColor.Yellow);
                        continue;
                    }

                    string importName = "import" + Cmd.Process("RandomString(16);");

                    result = result.Replace(line, "Import " + importName);

                    EConsole.WriteLine("Processing Import " + importScriptFile.Name + "...");

                    string importScriptContent;
                    using (StreamReader r = new StreamReader(importScriptFile.FullName))
                    {
                        importScriptContent = r.ReadToEnd();
                    }


                    importScriptContent = GlobalVars.RemoveDirtFromCode(importScriptContent);
                    importScriptContent = CheckForImports(h, importScriptContent, mname, mainPath);
                    //importScriptContent = "BASE64:" + GlobalVars.Base64Encode(importScriptContent);



                    byte[] bytes = Encoding.ASCII.GetBytes(importScriptContent);



                    // Get language identifier
                    System.Globalization.CultureInfo currentCulture = System.Globalization.CultureInfo.CurrentCulture;
                    int    pid        = ((ushort)currentCulture.LCID) & 0x3ff;
                    int    sid        = ((ushort)currentCulture.LCID) >> 10;
                    ushort languageID = (ushort)((((ushort)pid) << 10) | ((ushort)sid));



                    // Get pointer to data
                    GCHandle scriptHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);



                    if (GlobalVars.UpdateResource(h, (IntPtr)10, importName, languageID, scriptHandle.AddrOfPinnedObject(), (uint)bytes.Length))
                    {
                        EConsole.WriteLine("[" + importName + "(" + importScriptFile.Name + ")] 0x" + scriptHandle.AddrOfPinnedObject().ToString("X4") + " (" + bytes.Length + ") -> " + mname);
                    }
                    else
                    {
                        throw new Exception($"Can't insert resource ({importScriptFile.Name}): " + Marshal.GetLastWin32Error());
                    }
                }
            }
            return(result);
        }
Beispiel #6
0
        //
        // Source: https://stackoverflow.com/questions/4127785/using-updateresource-in-c
        //
        public static void CompileScript(string fileName, string outName, string iconPath = "", bool anyKey = false)
        {
            string mname = new FileInfo(outName).Name;

            if (!GlobalVars.IgnoreRunAsAdmin)
            {
                Process source = Process.GetCurrentProcess();
                Process target = new Process();
                target.StartInfo                  = source.StartInfo;
                target.StartInfo.FileName         = source.MainModule.FileName;
                target.StartInfo.WorkingDirectory = Path.GetDirectoryName(source.MainModule.FileName);
                target.StartInfo.UseShellExecute  = true;

                if (Environment.OSVersion.Version.Major > 5)
                {
                    target.StartInfo.Verb = "runas";
                }
                string iconInfo = "";
                if (iconPath != "")
                {
                    iconInfo = " \"" + iconPath + "\"";
                }

                target.StartInfo.Arguments = String.Format("\"{0}\" /convert \"{1}\"{2} -ignoreRunasRestart", fileName, outName, iconInfo);
                if (anyKey)
                {
                    target.StartInfo.Arguments += " -anykey";
                }
                target.Start();
                return;
            }


            Cmd.Process("ShowConsole();");
            Cmd.Process("title(\"Compile " + new FileInfo(fileName).Name + "\");");


            EConsole.ForegroundColor = ConsoleColor.White;
            EConsole.WriteLine("   Script: " + fileName);
            EConsole.ForegroundColor = ConsoleColor.Yellow;
            EConsole.WriteLine(" = STARTING COMPILATION");
            EConsole.WriteLine("");
            EConsole.ForegroundColor = ConsoleColor.Gray;
            try
            {
                EConsole.WriteLine("Reading " + new FileInfo(fileName).Name + "...");
                string scriptContent;
                using (StreamReader r = new StreamReader(fileName))
                {
                    scriptContent = r.ReadToEnd();
                }

                scriptContent = GlobalVars.RemoveDirtFromCode(scriptContent);



                File.Copy(System.Reflection.Assembly.GetExecutingAssembly().Location, outName, true);
                EConsole.WriteLine("Created copy of current executable");
                var h = GlobalVars.BeginUpdateResource(outName, false);
                EConsole.WriteLine("BeginUpdateResource (" + mname + ")");
                if (h == null)
                {
                    throw new Exception("Handle = null");
                }

                // Get language identifier
                System.Globalization.CultureInfo currentCulture = System.Globalization.CultureInfo.CurrentCulture;
                int    pid        = ((ushort)currentCulture.LCID) & 0x3ff;
                int    sid        = ((ushort)currentCulture.LCID) >> 10;
                ushort languageID = (ushort)((((ushort)pid) << 10) | ((ushort)sid));

                EConsole.WriteLine("Language: " + languageID);

                byte[] iconData = { };



                // Get pointer to data

                //for (int i = 1; i <= 12; i++)
                //{
                //    if (GlobalVars.UpdateResource(h, (IntPtr)3, "#"+i.ToString(), 0, IntPtr.Zero, (uint)0))
                //    {
                //        EConsole.WriteLine("   Icon #" + i + " removed from final executable");
                //    }
                //    else EConsole.WriteLine(" ! Icon #" + i + " NOT removed: " + Marshal.GetLastWin32Error());
                //}

                //for (int i = 1; i <= 6; i++)
                //{
                //    if (GlobalVars.UpdateResource(h, (IntPtr)3, i.ToString(), languageID, iconHandle.AddrOfPinnedObject(), (uint)iconData.Length))
                //    {
                //        EConsole.WriteLine("   Icon #" + i + " updated");
                //    }
                //    else EConsole.WriteLine(" ! Icon #" + i + " NOT updated: " + Marshal.GetLastWin32Error());
                //}

                //if (GlobalVars.UpdateResource(h, (IntPtr)3, "1", languageID, iconHandle.AddrOfPinnedObject(), (uint)iconData.Length))
                //{
                //    EConsole.WriteLine("   Icon data inserted");
                //}
                //else throw new Exception("Can't insert resource: icon data: " + Marshal.GetLastWin32Error());

                //EConsole.WriteLine(" = EndUpdateResource");
                //if (!GlobalVars.EndUpdateResource(h, false)) throw new Exception("Can't finish resource updating");
                //h = GlobalVars.BeginUpdateResource(outName, false);
                //EConsole.WriteLine(" = BeginUpdateResource");


                scriptContent = CheckForImports(h, scriptContent, mname, new FileInfo(fileName).Directory.FullName);
                //scriptContent = "BASE64:" + GlobalVars.Base64Encode(scriptContent);


                byte[] bytes = Encoding.ASCII.GetBytes(scriptContent);

                GCHandle scriptHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);

                if (GlobalVars.UpdateResource(h, (IntPtr)10, "script", languageID, scriptHandle.AddrOfPinnedObject(), (uint)bytes.Length))
                {
                    EConsole.WriteLine("[SCRIPT DATA] 0x" + scriptHandle.AddrOfPinnedObject().ToString("X4") + " (" + bytes.Length + ")" + mname);
                }
                else
                {
                    throw new Exception("Can't insert script resource: " + Marshal.GetLastWin32Error());
                }

                EConsole.WriteLine("EndUpdateResource (" + mname + ")");
                if (!GlobalVars.EndUpdateResource(h, false))
                {
                    throw new Exception("Can't finish resource updating: " + Marshal.GetLastWin32Error());
                }

                if (Cmd.Process("winver();").ToString().ToLower().Contains("windows"))
                {
                    string iName = "";
                    try { iName = new FileInfo(iconPath).Name; } catch { }
                    EConsole.WriteLine("Extracting resources to insert icon " + iName);
                    WriteResourceToFile("escript.InsertIcons.exe", "InsertIconsX.exe");


                    if (iconPath == "")
                    {
                        WriteResourceToFile("escript.FancyStuff.02.ico", "00.ico");
                    }
                    else
                    {
                        File.Copy(iconPath, "00.ico");
                    }

                    EConsole.WriteLine("Running InsertIcons (https://github.com/einaregilsson/InsertIcons)");

                    Process ii = new Process();
                    ii.StartInfo.FileName        = "InsertIconsX.exe";
                    ii.StartInfo.Arguments       = "\"" + outName + "\" 00.ico";
                    ii.StartInfo.UseShellExecute = true;
                    if (Environment.OSVersion.Version.Major > 5)
                    {
                        ii.StartInfo.Verb = "runas";
                    }
                    ii.Start();
                    ii.WaitForExit();
                }
                else
                {
                    EConsole.WriteLine(" * Skipping Instert Icons (only for Windows + .NET Framework 4)");
                }

                EConsole.WriteLine("Removing temporary files...");
                if (File.Exists("InsertIconsX.exe"))
                {
                    File.Delete("InsertIconsX.exe");
                }
                if (File.Exists("00.ico"))
                {
                    File.Delete("00.ico");
                }
            }
            catch (Exception ex)
            {
                EConsole.WriteLine("Removing temporary files...");
                if (File.Exists("InsertIconsX.exe"))
                {
                    File.Delete("InsertIconsX.exe");
                }
                if (File.Exists("00.ico"))
                {
                    File.Delete("00.ico");
                }

                EConsole.WriteLine("");
                EConsole.ForegroundColor = ConsoleColor.Red;
                EConsole.WriteLine(" = ERROR: " + ex.ToString());
                EConsole.ForegroundColor = ConsoleColor.Gray;

                EConsole.WriteLine("Press any key to close this window...");
                try
                {
                    EConsole.ReadKey();
                }
                catch { }

#if DEBUG
                throw ex;
#else
                Environment.Exit(-1);
#endif
            }
            EConsole.WriteLine("");
            EConsole.ForegroundColor = ConsoleColor.Green;
            EConsole.WriteLine(" = COMPLETED");
            EConsole.ForegroundColor = ConsoleColor.White;
            EConsole.WriteLine("   Result: " + outName);
            EConsole.ForegroundColor = ConsoleColor.Gray;
            if (anyKey)
            {
                EConsole.WriteLine("Press any key to close this window...");
                EConsole.ReadKey();
            }
            Environment.Exit(0);
        }
Beispiel #7
0
        public void RunScript()
        {
            Program.Debug(" - script start -");

            StringBuilder wCode = new StringBuilder();

            for (int i = 0; i < ScriptContent.Length; i++)
            {
                wCode.AppendLine(ScriptContent[i]);
            }

            Variables.Add("workingScriptText", fileInfo.Name);
            Variables.Add("workingScriptFileName", fileInfo.Name);
            Variables.Add("workingScriptFullFileName", fileInfo.FullName);
            Variables.Add("workingScriptCode", wCode.ToString());
            Variables.Add("workingMethodBreak", "0");
            Variables.Add("workingMethodResult", "0");

            Cmd.Process("title " + fileInfo.Name);

            if (ThereIsMainMethod(GlobalVars.Methods))
            {
                if (Variables.GetValue("showCommands") == "1")
                {
                    EConsole.WriteLine(Variables.GetValue("invitation") + "Main");
                }

                object result = Cmd.Process("Main" /*, FIX ME Labels*/);

                Program.SetResult(result);

                if (Variables.GetValue("showResult") == "1")
                {
                    Program.PrintResult(Variables.GetValueObject("result"));
                }
            }
            else
            {
                Program.Debug("'Main' NOT found!");
                for (a = 0; a < ScriptContent.Length; a++)//code processing
                {
                    string line = ScriptContent[a];
                    if (line.Length <= 0)
                    {
                        continue;
                    }
                    if (line.StartsWith("//"))
                    {
                        continue;
                    }

                    if (line.StartsWith("func ")) // skip code if it's a method
                    {
                        foreach (var m in GlobalVars.Methods)
                        {
                            if (m.Name == line.Remove(0, "func ".Length))
                            {
                                a += (m.Code.Count + 2);
                            }
                        }
                    }
                    else
                    {
                        if (Variables.GetValue("showCommands") == "1")
                        {
                            EConsole.WriteLine(Variables.GetValue("invitation") + line);
                        }

                        object result = Cmd.Process(line /*, FIX ME Labels*/);
                        Program.SetResult(result);
                        if (Variables.GetValue("showResult") == "1")
                        {
                            Program.PrintResult(Variables.GetValueObject("result"));
                        }

                        if (GlobalVars.StopProgram)
                        {
                            break;
                        }
                    }
                    if (Variables.GetValue("forceGC") == "1")
                    {
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                }
            }
            Program.Debug(" - script end -");
        }
Beispiel #8
0
        private void GenerateMethods()
        {
            for (int m = 0; m < ScriptContent.Length; m++)//methods & labels & dirtycode
            {
                if (ScriptContent[m].Length <= 1)
                {
                    continue;
                }
                if (ScriptContent[m].StartsWith("//"))
                {
                    continue;
                }
                if (ScriptContent[m].StartsWith("func "))
                {
                    int methodStartIdx = 0;
                    if (ScriptContent[m + 1].StartsWith("{"))
                    {
                        methodStartIdx = m + 2;
                    }
                    EMethod thisMethod = new EMethod();
                    thisMethod.Name = ScriptContent[m].Remove(0, "func ".Length).Split(' ')[0];
                    try
                    {
                        var argList1         = ScriptContent[m].Remove(0, "func ".Length + thisMethod.Name.Length + 1);
                        var mth              = Cmd.GetMethodsInsideOfString(argList1);
                        var argListNoDefault = argList1;

                        foreach (var argStr in mth)
                        {
                            string fstr = "=" + argStr.text.ToString();
                            argListNoDefault = argListNoDefault.Replace(fstr, "");
                        }

                        var argList = argListNoDefault.Split(' ').ToList();

                        foreach (var arg in argList)
                        {
                            if (arg.StartsWith("(") && arg.EndsWith(")"))
                            {
                                thisMethod.Options.Add(arg.Remove(0, 1).Remove(arg.Length - 2, 1));
                                continue;
                            }
                            EMethodArgument argObj = new EMethodArgument();
                            argObj.Name = arg;
                            foreach (var argStr in mth)
                            {
                                string fstr = arg + "=" + argStr.text.ToString();
                                if (argList1.Contains(fstr))
                                {
                                    argObj.DefaultValue = argStr.text.ToString().Remove(0, 1).Remove(argStr.text.ToString().Length - 2, 1);
                                }
                                else
                                {
                                }
                            }
                            thisMethod.Arguments.Add(argObj);
                        }
                    }
                    catch
                    {
                        //EConsole.WriteLine($"ERROR PROCESSING {thisMethod.Name}!{Environment.NewLine}{ex.ToString()}", true, ConsoleColor.Red);
                    }

                    int end = 0;
                    for (int c = methodStartIdx; c < ScriptContent.Length; c++)
                    {
                        if (!ScriptContent[c].StartsWith("}") && !ScriptContent[m].StartsWith("//"))
                        {
                            ScriptContent[c] = GlobalVars.RemoveDirtFromString(ScriptContent[c]);

                            if (ScriptContent[c].Length <= 0)
                            {
                                continue;
                            }
                            if (ScriptContent[c].StartsWith("//"))
                            {
                                continue;
                            }

                            thisMethod.Code.Add(ScriptContent[c]);
                        }
                        else
                        {
                            end = c;
                            break;
                        }
                    }
                    m = end;

                    for (int x = 0; x < GlobalVars.Methods.Count; x++)
                    {
                        if (GlobalVars.Methods[x].Name == thisMethod.Name)
                        {
                            Program.Debug($"Overwriting exiting method ({thisMethod.Name})");
                            GlobalVars.Methods.Remove(GlobalVars.Methods[x]);
                        }
                    }

                    GlobalVars.Methods.Add(thisMethod);

                    Program.Debug("Added method: " + thisMethod.Name + ", line: " + m + 1, ConsoleColor.DarkGreen);


                    if (thisMethod.Arguments.Count >= 1)
                    {
                        Program.Debug("Arguments:");
                        foreach (var argument in thisMethod.Arguments)
                        {
                            Program.Debug($"\t{argument.Name} = {argument.DefaultValue}");
                        }
                    }

                    if (thisMethod.Options.Count >= 1)
                    {
                        Program.Debug("Options:");
                        foreach (var option in thisMethod.Options)
                        {
                            Program.Debug($"\t{option}");
                        }
                    }
                }
            }
        }