Beispiel #1
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();
        }