コード例 #1
0
        public static string Show(string caption, string text)
        {
            string       result = "OK";
            ConsoleColor s      = EConsole.ForegroundColor;

            //Console.Clear();
            EConsole.ForegroundColor = ConsoleColor.White;
            EConsole.WriteLine("");
            EConsole.ForegroundColor = ConsoleColor.Cyan;
            EConsole.WriteLine(caption);
            EConsole.Write(ConsoleMessageBox.GetTest(caption));
            EConsole.WriteLine("");
            EConsole.ForegroundColor = ConsoleColor.White;
            EConsole.WriteLine(text);

            EConsole.ForegroundColor = ConsoleColor.Gray;
            EConsole.WriteLine("");

            Variables.Set("dialogInput", Console.ReadLine());

            EConsole.WriteLine("");
            EConsole.WriteLine("");
            EConsole.ForegroundColor = s;
            return(result);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: attackgithub/escript
        public static void BachokMessage(Exception ex, bool isUnhandled = false)
        {
            //
            // BACHOK POTIK ZONE
            //
            if (!GlobalVars.UsingAPI)
            {
                Cmd.Process("ShowConsole");
            }
            EConsole.ForegroundColor = ConsoleColor.White;
            ConsoleColor b = EConsole.BackgroundColor;

            EConsole.BackgroundColor = ConsoleColor.DarkRed;
            if (!isUnhandled)
            {
                EConsole.WriteLine("[BACHOK POTIK ERROR]");
            }
            else
            {
                EConsole.WriteLine("[UNHANDLED EXCEPTION]");
            }
            EConsole.ForegroundColor = ConsoleColor.Red;
            EConsole.BackgroundColor = b;
            EConsole.WriteLine(ex.ToString());
#if DEBUG
            throw ex;
#endif
            //if(args.Contains<string>("-install") && !args.Contains<string>("-close"))
            //{
            //    EConsole.WriteLine("If you have some troubles with installation");
            //    EConsole.WriteLine("You should try to run ESCRIPT with -createInstallation argument");
            //}
            EConsole.BackgroundColor = b;
        }
コード例 #3
0
ファイル: TCPConnection.cs プロジェクト: attackgithub/escript
 private static void msg()
 {
     try
     {
         srReceiver = new StreamReader(tcpServer.GetStream());
         while (true)
         {
             string msg = srReceiver.ReadLine();
             Variables.Set("tcpMsg", msg);
             if (msg == null)
             {
                 throw new Exception("TCP says null");
             }
             for (int i = 0; i < GlobalVars.Methods.Count; i++)
             {
                 var m = GlobalVars.Methods[i];
                 if (m.Name == Variables.GetValue("TCP_triggerMsg"))
                 {
                     Cmd.Process(m.Name, currentLabels);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         EConsole.WriteLine("TCP ERROR: " + ex.Message); Disconnect();
     }
 }
コード例 #4
0
ファイル: Cmd.cs プロジェクト: attackgithub/escript
 public static string ReadConsoleLine()
 {
     if (Variables.GetValue("inputText") != "null")
     {
         Console.Write(Variables.GetValue("inputText"));
     }
     return(EConsole.ReadLine());
 }
コード例 #5
0
ファイル: Program.cs プロジェクト: attackgithub/escript
        public static void CheckUpdates()
        {
            try
            {
                Version cVer           = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                string  currentVersion = cVer.ToString();
                string  verUrl         = GlobalVars.StuffServer + "UpdateFiles/%vName%-version.txt";
                string  verX           = "latest";
                switch (cVer.Revision)
                {
                case 0: { verX = "latest"; break; }

                case 1: { verX = "beta"; break; }

                default: { verX = "latest"; break; }
                }

                verUrl = verUrl.Replace("%vName%", verX);

                string latestVersion = new System.Net.WebClient().DownloadString(verUrl);
                int    major         = int.Parse(latestVersion.Split('.')[0]);
                int    minor         = int.Parse(latestVersion.Split('.')[1]);
                int    build         = int.Parse(latestVersion.Split('.')[2]);
                int    revision      = int.Parse(latestVersion.Split('.')[3]);
                if (currentVersion != latestVersion && Variables.GetValue("checkUpdates") == "1")
                {
                    if (cVer.Build >= build && cVer.Minor >= minor && cVer.Major >= major)
                    {
                        Debug("CheckUpdates: current version is newer than on the server", ConsoleColor.DarkRed);
                        return;
                    }


                    string add = "";
                    if (cVer.Revision != 0)
                    {
                        add = " " + verX;
                    }

                    if (GlobalVars.IsCompiledScript || Variables.GetValue("workingScriptFullFileName").Length > 1)
                    {
                        string uText = "ESCRIPT " + latestVersion + " is available. You can install it using \"update" + add + "\" command.";
                        EConsole.WriteLine(uText);
                    }
                    else
                    {
                        Cmd.Process("update" + add);
                    }
                    //
                }
            }
            catch (Exception ex)
            {
                Debug("CheckUpdates: " + ex.ToString(), ConsoleColor.DarkRed);
            }
        }
コード例 #6
0
ファイル: EConsole.cs プロジェクト: attackgithub/escript
        public static void PrintIntro()
        {
            Version version   = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            string  eVer      = version.ToString();
            string  buildInfo = "";

            if (version.Revision != 0)
            {
                switch (version.Revision)
                {
                default: { buildInfo = "UNSTABLE VERSION";  break; }

                case 1: { buildInfo = "BETA VERSION (only for testing)"; break; }
                }
            }

            EConsole.ForegroundColor = ConsoleColor.Green;



            EConsole.WriteLine(" ");
            EConsole.ForegroundColor = ConsoleColor.Green;
            Version cVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

            EConsole.Write(String.Format("   ESCRIPT {0}.{1}", cVer.Major, cVer.Minor));
#if IsCore
            EConsole.ForegroundColor = ConsoleColor.Magenta;
            EConsole.WriteLine(" Core");
            EConsole.ForegroundColor = ConsoleColor.Green;
#else
            EConsole.ForegroundColor = ConsoleColor.White;
            EConsole.WriteLine(" Standard");
            EConsole.ForegroundColor = ConsoleColor.Green;
#endif
            EConsole.ForegroundColor = ConsoleColor.Gray;
            EConsole.WriteLine("   Copyright (C) Dz3n 2017-2019");
            EConsole.ForegroundColor = ConsoleColor.DarkGray;
            EConsole.Write("   Build " + version.Build + " ");
            EConsole.ForegroundColor = ConsoleColor.Yellow;
            EConsole.WriteLine(buildInfo);
            if (cVer.Revision != 0)
            {
                EConsole.WriteLine("   Type \"update\" to install the latest stable version", true, ConsoleColor.DarkYellow);
            }

            EConsole.WriteLine("");

            EConsole.ForegroundColor = ConsoleColor.DarkGray;


            EConsole.WriteLine("   OS: " + Environment.OSVersion.VersionString);
            EConsole.WriteLine("   https://github.com/feel-the-dz3n/escript | https://discord.gg/jXcjuqv");
            EConsole.WriteLine("   https://vk.com/dz3n.escript");
            EConsole.WriteLine("");
        }
コード例 #7
0
ファイル: Cmd.cs プロジェクト: attackgithub/escript
        public static string ReadConsoleKey()
        {
            if (Variables.GetValue("inputText") != "null")
            {
                Console.Write(Variables.GetValue("inputText"));
            }
            string k = EConsole.ReadKey().KeyChar.ToString();

            EConsole.WriteLine("");
            return(k);
        }
コード例 #8
0
        private void textBoxOutput_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.F4)
            {
                EConsole.Console_CancelKeyPress(null, null);
            }

            if (isOneKey)
            {
                button1_Click(null, null);
                oneKeyKey = e.KeyCode;
            }
            //if(commandListIdx == -1)
            //{
            //    commandListIdx = commandList.Count - 1;
            //}
            //textBoxOutput.AppendText(commandList[commandListIdx]);
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: attackgithub/escript
        public static void Debug(string text, ConsoleColor col = ConsoleColor.DarkGray, bool PrintInConsole = true)
        {
            if (Variables.GetValue("ignoreLog") == "1")
            {
                return;
            }

            string result = "[DEBUG] " + text;

            if (log != null)
            {
                log.WriteLine("[" + DateTime.Now.ToString() + "] " + result);
            }
            if (Variables.GetValue("programDebug") == "1" && PrintInConsole)
            {
                ConsoleColor a = EConsole.ForegroundColor;
                EConsole.ForegroundColor = col;
                EConsole.WriteLine(result, false);
                EConsole.ForegroundColor = a;
            }
        }
コード例 #10
0
ファイル: EConsole.cs プロジェクト: attackgithub/escript
 public static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
 {
     if (!GlobalVars.StopProgram)
     {
         EConsole.WriteLine(" ");
         EConsole.WriteLine(" [!] Cancel Key Pressed, performing last action before Break");
         EConsole.WriteLine(" ");
         GlobalVars.StopProgram = true;
         if (e != null)
         {
             e.Cancel = true;
         }
     }
     else
     {
         EConsole.WriteLine(" [!] Cancel Key Pressed, StopProgram is true, performing default action");
         if (e == null)
         {
             Environment.Exit(0);
         }
     }
 }
コード例 #11
0
ファイル: Program.cs プロジェクト: attackgithub/escript
 public static void CommandLine()
 {
     ScriptColor = ConsoleColor.White;
     Cmd.Process("ShowConsole");
     new Thread(CheckUpdates).Start();
     Variables.Set("showResult", "1");
     GlobalVars.StopProgram   = false;
     EConsole.ForegroundColor = ConsoleColor.White;
     EConsole.WriteLine("Need help? Type: help");
     if (!GlobalVars.IsCompiledScript && !System.Reflection.Assembly.GetExecutingAssembly().Location.Contains("\\ESCRIPT\\escript.exe"))
     {
         EConsole.WriteLine("To install this copy of ESCRIPT type: install");
     }
     while (true)
     {
         EConsole.ForegroundColor = ConsoleColor.Green;
         EConsole.Write(Variables.GetValue("invitation"));
         EConsole.ForegroundColor = ConsoleColor.White;
         string line = EConsole.ReadLine();
         EConsole.ForegroundColor = ScriptColor;
         SetResult(Cmd.Process(GlobalVars.RemoveDirtFromString(line)));
         if (Variables.GetValue("showResult") == "1")
         {
             PrintResult(Variables.GetValue("result"));
         }
         if (Variables.GetValue("forceGC") == "1")
         {
             GC.Collect();
             GC.WaitForPendingFinalizers();
         }
         if (GlobalVars.StopProgram)
         {
             break;
         }
     }
 }
コード例 #12
0
        public static string Show(string caption, string text, int icon, int buttons)
        {
            string       result = "0";
            ConsoleColor s      = EConsole.ForegroundColor;

            //Console.Clear();
            EConsole.ForegroundColor = ConsoleColor.White;
            EConsole.WriteLine("");
            EConsole.ForegroundColor = ConsoleColor.Cyan;
            if (icon == 3)
            {
                EConsole.ForegroundColor = ConsoleColor.Red;
            }
            if (icon == 2 || icon == 4)
            {
                EConsole.ForegroundColor = ConsoleColor.Yellow;
            }
            EConsole.WriteLine(caption);
            EConsole.Write(GetTest(caption));
            EConsole.WriteLine("");
            EConsole.ForegroundColor = ConsoleColor.White;
            EConsole.WriteLine(text);

            EConsole.ForegroundColor = ConsoleColor.Gray;
            EConsole.WriteLine("");
            if (buttons == 0)
            {
                EConsole.Write("   OK [Enter] ");
                while (true)
                {
                    var key = EConsole.ReadKey(true);
                    if (key.Key == ConsoleKey.Enter)
                    {
                        result = "OK";
                        break;
                    }
                }
            }
            else if (buttons == 1)
            {
                EConsole.Write("   OK [Enter] | Cancel [C] ");
                while (true)
                {
                    var key = EConsole.ReadKey(true);
                    if (key.Key == ConsoleKey.Enter)
                    {
                        result = "OK";
                        break;
                    }
                    else if (key.Key == ConsoleKey.C)
                    {
                        result = "Cancel";
                        break;
                    }
                }
            }
            else if (buttons == 2)
            {
                EConsole.Write("   Yes [Y] | No [N] ");
                while (true)
                {
                    var key = EConsole.ReadKey(true);
                    if (key.Key == ConsoleKey.Y)
                    {
                        result = "Yes";
                        break;
                    }
                    else if (key.Key == ConsoleKey.N)
                    {
                        result = "No";
                        break;
                    }
                }
            }
            else if (buttons == 3)
            {
                EConsole.Write("   Yes [Y] | No [N] | Cancel [C] ");
                while (true)
                {
                    var key = EConsole.ReadKey(true);
                    if (key.Key == ConsoleKey.Y)
                    {
                        result = "Yes";
                        break;
                    }
                    else if (key.Key == ConsoleKey.N)
                    {
                        result = "No";
                        break;
                    }
                    else if (key.Key == ConsoleKey.C)
                    {
                        result = "Cancel";
                        break;
                    }
                }
            }
            else if (buttons == 4)
            {
                EConsole.Write("   Retry [R] | Cancel [C] ");
                while (true)
                {
                    var key = EConsole.ReadKey(true);
                    if (key.Key == ConsoleKey.R)
                    {
                        result = "Retry";
                        break;
                    }
                    else if (key.Key == ConsoleKey.C)
                    {
                        result = "Cancel";
                        break;
                    }
                }
            }
            else if (buttons == 5)
            {
                EConsole.Write("   Abort [A] | Retry [R] | Ignore [I] ");
                while (true)
                {
                    var key = EConsole.ReadKey(true);
                    if (key.Key == ConsoleKey.R)
                    {
                        result = "Retry";
                        break;
                    }
                    else if (key.Key == ConsoleKey.I)
                    {
                        result = "Ignore";
                        break;
                    }
                    else if (key.Key == ConsoleKey.A)
                    {
                        result = "Abort";
                        break;
                    }
                }
            }
            else
            {
                EConsole.WriteLine("");
                EConsole.Write("   ERROR: Unknown keys");
            }
            EConsole.WriteLine("");
            EConsole.WriteLine("");
            EConsole.ForegroundColor = s;
            return(result);
        }
コード例 #13
0
ファイル: Program.cs プロジェクト: attackgithub/escript
        //
        // 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").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.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;
#endif
                Environment.Exit(-1);
            }
            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);
        }
コード例 #14
0
ファイル: ESCode.cs プロジェクト: attackgithub/escript
        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");
                }

                string result = Cmd.Process("Main", Labels).ToString();

                Program.SetResult(result);

                if (Variables.GetValue("showResult") == "1")
                {
                    Program.PrintResult(Variables.GetValue("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);
                        }

                        string result = Cmd.Process(line, Labels).ToString();
                        Program.SetResult(result);
                        if (Variables.GetValue("showResult") == "1")
                        {
                            Program.PrintResult(Variables.GetValue("result"));
                        }

                        if (GlobalVars.StopProgram)
                        {
                            break;
                        }
                    }
                    if (Variables.GetValue("forceGC") == "1")
                    {
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                }
            }
            Program.Debug(" - script end -");
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: attackgithub/escript
        //public static void PrintResult(string result, string prefix = "null")
        //{
        //    Cmd.Process("PrintResult {#" + result + "}||" + prefix, null, true);
        //}

        public static void Init(string[] args)
        {
            EConsole.ForegroundColor = ScriptColor;


            new ESCode(ESCode.SplitCode(ReadResource("escript.Functions.es")));
            Debug("escript.Functions.es imported");

            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("ESCRIPT <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(100);
            }
            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: ESCRIPT <Script Path> /convert (<Compiled File Path>) (<Icon Path>)");
                    EConsole.WriteLine("Examples:");
                    EConsole.WriteLine("escript \"MyScript.es\" /convert\twill be converted in MyScript.exe");
                    EConsole.WriteLine("escript \"MyScript.es\" /convert \"C:\\Programs\\MyScript.exe\"");
                    EConsole.WriteLine("escript \"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);
                }
            }

            //Cmd.Process("SetStatus ESCRIPT Initializing||Please wait...");
            //Cmd.Process("sleep 2000");
            //Cmd.Process("HideStatus");


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

            Debug("Environment formed", ConsoleColor.DarkGreen);

#if !IsCore
            IntPtr resourceInfo = GlobalVars.FindResource(IntPtr.Zero, "script", (IntPtr)10);
            Debug("SCRIPT Resource: 0x" + resourceInfo.ToString("X4"));

            if (resourceInfo != IntPtr.Zero)
            {
                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("SCRIPT:\r\n" + code);

                ESCode script = new ESCode(ESCode.SplitCode(code));
                script.RunScript();

                Break();
            }
#endif

            if (args.Contains <string>("-close"))
            {
                if (GlobalVars.IsCompiledScript)
                {
                    throw new Exception("Can't install compiled version. Please, use clean ESCRIPT or remove script information from resources manually.");
                }
                foreach (var p in Process.GetProcesses())
                {
                    try
                    {
                        if (p.ProcessName.ToLower() == "escript" && p.Id != Process.GetCurrentProcess().Id)
                        {
                            p.Kill();
                        }
                        if (p.ProcessName.ToLower() == "escript-update" && p.Id != Process.GetCurrentProcess().Id)
                        {
                            p.Kill();
                        }
                    }
                    catch { }
                }
            }
            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.InstallScript.es", InstallScript);

                ESCode script = new ESCode(InstallScript);
                script.RunScript();

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

                EConsole.WriteLine("Associating ESCRIPT files...");
                try
                {
                    FileAssociation.AssociateESCRIPT("ESCRIPT File", me, ".es");
                    FileAssociation.AssociateESCRIPT("ESCRIPT File", me, ".escript");
                    FileAssociation.AssociateESCRIPT("ESCRIPT Header File", me, ".esh", "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(21);
                }
                EConsole.WriteLine("Creating a script on the desktop...");
                try
                {
                    string desktop = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ESCRIPT.es");
                    using (StreamWriter w = new StreamWriter(desktop))
                    {
                        w.WriteLine("start {GetProgramPath}");
                        w.WriteLine("Exit");
                    }

                    desktop = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ESCRIPT Folder.es");
                    WriteResourceToFile("escript.ExampleFolder.es", 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("ESCRIPT was 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]))
                {
                    ESCode script = new ESCode(args[0]);
                    script.RunScript();
                }
                else if (!File.Exists(args[0]))
                {
                    CommandLine();
                }
                //                    if (args.Contains<string>("-installNext"))
                //                    {
                //                        foreach (var p in Process.GetProcesses())
                //                        {
                //                            if (p.ProcessName.ToLower() == "escript" && p.Id != Process.GetCurrentProcess().Id) p.Kill();
                //                        }

                //                        string me = System.Reflection.Assembly.GetExecutingAssembly().Location;
                //                        string destination = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "ESCRIPT");
                //                        FileInfo aboutme = GetAboutMe();

                //                        if (aboutme.DirectoryName == destination && aboutme.Name.ToLower().StartsWith("escript.exe"))
                //                        {
                //                            EConsole.WriteLine("You can't do this. Use escript-install.exe or UpdateProgram method.");
                //                        }
                //                        else
                //                        {

                //                            EConsole.WriteLine("Installing ESCRIPT...");
                //#if !IsCore
                //                            if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),"ESCRIPT"))) Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "ESCRIPT"));
                //                            File.Copy(me, Path.Combine(destination, "escript.exe"), true);
                //#else
                //                            EConsole.WriteLine("Can't install ESCRIPT Core");
                //#endif
                //                            new Process() { StartInfo = { FileName = Path.Combine(destination, "escript.exe"), Arguments = "-close -assoc" } }.Start();
                //                            Cmd.Process("HideConsole", null, null);
                //                            Environment.Exit(0);
                //                        }
                //                    }
            }
            catch (Exception ex)
            {
                BachokMessage(ex);
            }

            Break();
        }
コード例 #16
0
ファイル: Program.cs プロジェクト: attackgithub/escript
        public static void PrintResult(string result, string prefix = "")
        {
            if (prefix != "")
            {
                EConsole.Write(prefix, ConsoleColor.DarkGray);
            }
            EConsole.Write("Result: ", ConsoleColor.DarkGray);
            EConsole.Write(result, ConsoleColor.Gray);

            /*if (result == "-1")
             * {
             *  EConsole.ForegroundColor = ConsoleColor.DarkRed;
             *  EConsole.Write(" (error/-1)");
             * }
             * else if (result == "0" || result.ToLower() == "false")
             * {
             *  EConsole.ForegroundColor = ConsoleColor.DarkYellow;
             *  EConsole.Write(" (false/error/0)");
             * }
             * else if (result == "1" || result.ToLower() == "true")
             * {
             *  EConsole.ForegroundColor = ConsoleColor.DarkGreen;
             *  EConsole.Write(" (true/ok/1)");
             * }
             * else */if (result == "CMD_NOT_FOUND")
            {
                EConsole.Write(" (command not found)", ConsoleColor.DarkGray);
            }
            else if (result.Length == 0)
            {
                EConsole.Write(" (nothing or null)", ConsoleColor.DarkGray);
            }
            else if (result == "ESCRIPT_ERROR_EXCEPTION")
            {
                EConsole.Write(" (invalid arguments or ESCRIPT issue)", ConsoleColor.DarkRed);
            }
            else if (result.StartsWith("SYNTAX_ERROR"))
            {
                EConsole.Write(" (syntax error)", ConsoleColor.DarkRed);
            }
            else if (result.StartsWith("ESCRIPT_ERROR"))
            {
                EConsole.Write(" (ESCRIPT error)", ConsoleColor.DarkRed);
            }
            else if (result.StartsWith("CMD_ERROR_NULL"))
            {
                EConsole.Write(" (unknown error)", ConsoleColor.DarkRed);
            }
            else if (result.StartsWith("CMD_ERROR"))
            {
                EConsole.Write(" (error)", ConsoleColor.DarkRed);
            }
            else if (result.StartsWith("CMD_DONE"))
            {
                EConsole.Write(" (done)", ConsoleColor.DarkGreen);
            }
            else if (result.StartsWith("CMD_FAIL"))
            {
                EConsole.Write(" (fail)", ConsoleColor.DarkYellow);
            }
            EConsole.WriteLine("");
        }
コード例 #17
0
ファイル: EConsole.cs プロジェクト: attackgithub/escript
        public static void CreateConsole()
        {
            if (IsConsoleOk)
            {
                return;
            }


#if IsCore
            currentStdout = (IntPtr)1;
            Console.Title = conTitle;
            PrintIntro();
            return;
#else
#endif
            if (Variables.GetValue("useCustomConsole") == "1")
            {
                WaitingForForm = true;
                new Thread(delegate()
                {
                    if (cWnd == null)
                    {
                        cWnd = new CustomConsoleWindow();
                        cWnd.ShowDialog();
                        cWnd.Activate();
                    }
                }).Start();
                while (cWnd == null)
                {
                    Thread.Sleep(10);
                }
                currentStdout  = (IntPtr)1;
                WaitingForForm = false;
            }
            else
            {
                AllocConsole();
                Console.CancelKeyPress += Console_CancelKeyPress;


                // stdout's handle seems to always be equal to 7
                IntPtr defaultStdout = new IntPtr(7);
                currentStdout = GetStdHandle(StdOutputHandle);

                if (currentStdout != defaultStdout)
                {
                    // reset stdout
                    SetStdHandle(StdOutputHandle, defaultStdout);
                }



                try
                {
                    // reopen stdout
                    TextWriter writer = new StreamWriter(Console.OpenStandardOutput())
                    {
                        AutoFlush = true
                    };
                    Console.SetOut(writer);
                    Console.OutputEncoding = System.Text.Encoding.UTF8;
                }
                catch (Exception ex)
                {
                    EConsole.WriteLine("Can't set Console Output Encoding: " + ex.ToString(), true, ConsoleColor.Red);
                }

                try
                {
                    TextReader reader = new StreamReader(Console.OpenStandardInput());
                    Console.SetIn(reader);
                    Console.InputEncoding = System.Text.Encoding.Unicode;
                }
                catch (Exception ex)
                {
                    EConsole.WriteLine("Can't set Console Input Encoding: " + ex.ToString(), true, ConsoleColor.Red);
                }
            }
            try
            {
                PrintIntro();
            }
            catch (Exception ex)
            {
                Program.BachokMessage(ex);
            }
            Version cVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

            Title = String.Format("ESCRIPT {0}.{1}", cVer.Major, cVer.Minor);
        }
コード例 #18
0
ファイル: Program.cs プロジェクト: attackgithub/escript
        public static string CheckForImports(IntPtr resourceUpdateHandle, string scriptContent, string mname, string mainPath)
        {
            string result = scriptContent;
            var    h      = resourceUpdateHandle;

            string[] scriptLines = 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);
        }
コード例 #19
0
ファイル: Program.cs プロジェクト: attackgithub/escript
        public static void Break()
        {
            if (GlobalVars.UsingAPI)
            {
                Cmd.Process("ThreadAbortAll");
                EConsole.WriteLine("STOP. (WAITING FOR API)");
                return;
            }
            try
            {
                while (true)
                {
                    Cmd.Process("ShowConsole");
                    EConsole.ForegroundColor = ConsoleColor.Red;
                    EConsole.Write("\nSTOP. ");
                    if (Variables.GetBool("abortAfterBreak"))
                    {
                        Cmd.Process("ThreadAbortAll");
                        Cmd.Process("HideStatus");
                    }
                    EConsole.ForegroundColor = ConsoleColor.White;

                    if (Variables.GetBool("exitAfterBreak"))
                    {
                        Environment.Exit(0);
                    }

                    EConsole.Write("[L]og, [R]estart, [S]et, [C]ommand Interpreter, another key - exit ");
                    var key = EConsole.ReadKey().Key;

                    EConsole.WriteLine("\n");
                    if (key == ConsoleKey.R)
                    {
                        Cmd.Process("HideConsole");
                        Cmd.Process("Restart");
                        Environment.Exit(0);
                    }
                    else if (key == ConsoleKey.S)
                    {
                        Cmd.Process("set");
                    }
                    else if (key == ConsoleKey.F1 || key == ConsoleKey.L)
                    {
                        Cmd.Process("start notepad||$logFile||0");
                    }
                    else if (key == ConsoleKey.C)
                    {
                        EConsole.ForegroundColor = ConsoleColor.Gray;
                        EConsole.WriteLine("Use: \"Break\" to back to stop menu");
                        CommandLine();
                    }
                    else
                    {
                        Environment.Exit(0);
                    }
                }
            }
            catch (Exception ex)
            {
                BachokMessage(ex);
            }
            Break();
        }
コード例 #20
0
ファイル: Cmd.cs プロジェクト: attackgithub/escript
        //
        // This method processes command
        // Methods, escript.Functions Processing
        //
        private static string ProcessEx(string command, Dictionary <string, int> Labels = null, bool displayNoCmd = true)
        {
            if (String.IsNullOrWhiteSpace(command))
            {
                return("");
            }
            Variables.Set("previousCommand", command);
            if (command.StartsWith("help") || command == "?")
            {
                return(ProcessEx("Help", Labels));
            }
            // we can't use "if" and "for" in escript.Functions class as method name, so let's replace "if" to "If" and "for" to "For" lol
            else if (command.StartsWith("if "))
            {
                return(ProcessEx(command.Replace("if ", "If "), Labels));
            }
            else if (command.StartsWith("for "))
            {
                return(ProcessEx(command.Replace("for ", "For "), Labels));
            }
            else if (command.StartsWith("return "))
            {
                return(ProcessEx(command.Replace("return ", "Return "), Labels));
            }
            else if (command.StartsWith("#"))
            {
                return(command.Remove(0, 1));                              // remove first symbol
            }
            else if (command.StartsWith(":"))
            {
                return("0");                              // for labels
            }
            else if (command.StartsWith("-"))
            {
                return("1");                              //
            }
            else
            {
                string eAp = EverythingAfter(command, ' ', 1);
                foreach (var m in GlobalVars.Methods)
                {
                    if (command.Split(' ')[0] == m.Name)
                    {
                        //Program.Debug("Running " + m.Name + " method!", ConsoleColor.DarkCyan);
                        Variables.SetVariable("workingMethodResult", "1");

                        object[] objs = eAp.Split(new string[] { Variables.GetValue("splitArgs") }, StringSplitOptions.RemoveEmptyEntries).ToArray <object>(); // split by "||" (default)


                        for (int aIdx = 0; aIdx < m.Arguments.Count; aIdx++)
                        {
                            var argObj   = m.Arguments[aIdx];
                            var varName  = argObj.Name;
                            var varValue = "";
                            try
                            {
                                varValue = objs[aIdx].ToString();
                            }
                            catch
                            {
                                varValue = argObj.DefaultValue;
                            }
                            varValue = ProcessArgument(varValue, Labels).ToString();
                            Variables.SetVariable(argObj.Name, varValue, new List <string>()
                            {
                                "Method=" + m.Name
                            });
                        }

                        for (int idx = 0; idx < m.Code.Count; idx++)
                        {
                            Variables.SetVariable("workingMethod", m.Name);
                            if (Variables.GetValue("showCommands") == "1")
                            {
                                EConsole.WriteLine(Variables.GetValue("invitation") + m.Name + "> " + m.Code[idx]);
                            }
                            bool ignoreLogOption = false;
                            if (m.Options.Contains("IgnoreLog"))
                            {
                                ignoreLogOption = true;
                            }

                            string res = Cmd.Process(m.Code[idx], Labels, ignoreLogOption).ToString();

                            Program.SetResult(res);
                            if (Variables.GetValue("showResult") == "1" && !m.Code[idx].ToLower().StartsWith("return") && !m.Options.Contains("IgnorePrintResult"))
                            {
                                Program.PrintResult(Variables.GetValue("result"));
                            }



                            if (GlobalVars.StopProgram)
                            {
                                break;
                            }
                            if (Variables.GetValue("workingMethodBreak") == "1")
                            {
                                Variables.Set("workingMethodBreak", "0");
                                break;
                            }
                        }



                        //for (int aIdx = 0; aIdx < m.Arguments.Count; aIdx++)
                        //{
                        //    var argObj = m.Arguments[aIdx];
                        //    Variables.Remove(Variables.GetVariable(argObj.Name));
                        //}

                        if (m.Options.Contains("Cleanup"))
                        {
                            MethodCleanup(m.Name);
                        }

                        return(Variables.GetValue("workingMethodResult"));
                    }
                    if (GlobalVars.StopProgram)
                    {
                        break;
                    }
                }

                if (Labels != null)
                {
                    if (command.StartsWith("goto")) // for labels, PLEASE REWRITE
                    {
                        string label = Cmd.EverythingAfter(command, ' ', 1);
                        ESCode.a = Labels[label];
                        return("1");
                    }
                }
                //
                // Here we invoking methods in escript.Functions class
                //
                try
                {
                    fnc.Labels = Labels;
                    MethodInfo mth    = null;
                    object     target = fnc;

                    try
                    {
                        mth = fnc.GetType().GetMethod(command.Split(' ')[0]); // split command by space, used to split first argument. If there are no spaces then we will get just only command, without errors
                    }
                    catch { }


                    for (int i = 0; i < GlobalVars.LoadedLibs.Count; i++)
                    {
                        ImportLibClass imp = GlobalVars.LoadedLibs[i];
                        if (imp.obj != null)
                        {
                            if (imp.funcType.GetMethod(command.Split(' ')[0]) != null)
                            {
                                Program.Debug("Method " + command.Split(' ')[0] + " found in " + imp.funcType.FullName);
                                mth    = imp.funcType.GetMethod(command.Split(' ')[0]);
                                target = imp.obj;
                            }
                        }
                    }

                    if (mth == null)
                    {
                        throw new Exception("Method not found");
                    }

                    try
                    {
                        string   p    = EverythingAfter(command, ' ', 1);                                                                                    // Get everything after space
                        object[] objs = p.Split(new string[] { Variables.GetValue("splitArgs") }, StringSplitOptions.RemoveEmptyEntries).ToArray <object>(); // split by "||" (default)
                        for (int i = 0; i < objs.Length; i++)
                        {
                            objs[i] = ProcessArgument(objs[i], Labels);
                        }
                        var Args = mth.GetParameters();

                        if (objs.Length < Args.Length) // what the f**k is this idk
                        {
                            for (int i = 0; i < Args.Length; i++)
                            {
                                try
                                {
                                    object aaa = objs[i];
                                    if (aaa == null)
                                    {
                                        objs[i] = Args[i].DefaultValue;
                                    }
                                }
                                catch
                                {
                                    var ox = objs.ToList();
                                    ox.Add(Args[i].DefaultValue);
                                    objs = ox.ToArray();
                                }
                            }
                        }

                        object theResult = "";

                        // INVOKE THE METHOD!!!!!!
                        theResult = mth.Invoke(target, objs);
                        if (theResult == null)
                        {
                            theResult = "null";
                        }

                        if (theResult.GetType() == typeof(bool))
                        {
                            if ((bool)theResult)
                            {
                                return("1");
                            }
                            else
                            {
                                return("0");
                            }
                        }

                        return(theResult.ToString());
                    }
                    catch (System.NullReferenceException)
                    {
                    }
                    catch (ArgumentException ex)
                    {
                        EConsole.WriteLine("ERROR: Invalid arguments", ConsoleColor.Red);
                        Program.Debug(ex.ToString(), ConsoleColor.DarkRed);
                        Process("UseTextTest " + command.Split(' ')[0], Labels); // Call UseTextTest method to show arguments of command

                        return(EResult.Syntax.GetError("Invalid arguments"));
                    }
                    catch (TargetInvocationException ex)
                    {
                        EConsole.WriteLine("ERROR: Can't invoke \"" + command.Split(' ')[0] + "\" method because of exception:", ConsoleColor.DarkRed);
                        EConsole.WriteLine(ex.InnerException.ToString(), ConsoleColor.Red);
                        Process("UseTextTest " + command.Split(' ')[0], Labels); // Call UseTextTest method to show arguments of command

                        return(EResult.ESCRIPT.GetError("Exception"));
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            var           Args          = mth.GetParameters();
                            List <object> defaultParams = new List <object>();
                            for (int i = 0; i < Args.Length; i++)
                            {
                                defaultParams.Add(Args[i].DefaultValue);
                            }
                            return(mth.Invoke(target, defaultParams.ToArray()).ToString());
                        }
                        catch
                        {
                            EConsole.WriteLine(ex.GetType().Name + ": " + ex.Message, ConsoleColor.Red);
                            Program.Debug(ex.StackTrace, ConsoleColor.DarkRed);
                            Process("UseTextTest " + command.Split(' ')[0], Labels, true); // Call UseTextTest method to show arguments of command

                            return(EResult.Cmd.GetError(ex.Message));
                        }
                    }
                }
                catch { }
            }
            string lt = command;

            if (command.Contains(' '))
            {
                lt = command.Split(' ')[0];
            }
            if (lt == "if")
            {
                Process("UseTextTest If", Labels, true);
            }
            else if (lt == "for")
            {
                Process("UseTextTest For", Labels, true);
            }
            else if (lt == "return")
            {
                Process("UseTextTest Return", Labels);
            }
            else
            {
                if (displayNoCmd && Variables.GetValue("displayNoCmd") == "1")
                {
                    EConsole.WriteLine("No such method/command: " + lt);
                    EConsole.WriteLine("Need help? Type: help");
                }
            }

            return(EResult.Cmd.Get("Not found").ToString());
        }
コード例 #21
0
ファイル: Program.cs プロジェクト: attackgithub/escript
        public static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            //EConsole.CreateConsole();
            //Cmd.HideConsoleTest();
            //#if !IsCore
            //            Thread mThread = new Thread(FormThread);
            //            mThread.SetApartmentState(ApartmentState.STA);
            //            mThread.Start();
            //#endif


            Variables.Initialize(true, args);
            try
            {
                string f = GetLogFile();
                if (f == null)
                {
                    throw new Exception();
                }
                log                = new StreamWriter(f);
                log.AutoFlush      = true;
                GlobalVars.LogFile = new FileInfo(f).FullName;

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

            if (args.Contains <string>("-noUiConsole") || Cmd.Process("IsKeyDown RShiftKey") == "1")
            {
                Variables.Set("useCustomConsole", "0");
            }

            if (args.Contains <string>("-console") || Cmd.Process("IsKeyDown LControlKey") == "1")
            {
                Variables.Set("forceConsole", "1");
                Cmd.Process("ShowConsole");
            }


#if !DEBUG
            if (args.Contains <string>("-debug"))
#endif
            {
                Cmd.Process("Debug");
            }


#if !IsCore
            if (Cmd.Process("IsKeyDown LShiftKey") == "1")
            {
                Cmd.Process("Debug");
                EConsole.WriteLine("    (shift key)");
            }
#endif
            if (args.Contains <string>("-wait"))
            {
                Thread.Sleep(500);
            }

            Init(args);
        }