Example #1
0
        public static void Main(string[] args)
        {
            App.args = args;

            mConsole = WinConsole.Initialize(TestArg("-console") || TestArg("-console-debug"));

            if (TestArg("-help") || TestArg("/?"))
            {
                ShowHelp();
                return;
            }
            else if (TestArg("-dbg_wait"))
            {
                MessageBox.Show("Waiting for debugger. (press ok when attached)");
            }

            Thread.CurrentThread.Name = "Main";

            Console.WriteLine("Starting...");

            AppLog Log = new AppLog();

            exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(exePath);

            mVersion = fvi.FileMajorPart + "." + fvi.FileMinorPart;
            if (fvi.FileBuildPart != 0)
            {
                mVersion += (char)('a' + (fvi.FileBuildPart - 1));
            }
            appPath  = Path.GetDirectoryName(exePath);
            mSession = Process.GetCurrentProcess().SessionId;

            Translate.Load();

            svc = new Service(mSvcName);

            if (TestArg("-engine"))
            {
                engine = new Engine();

                engine.Run();
                return;
            }
            else if (TestArg("-svc"))
            {
                if (TestArg("-install"))
                {
                    Console.WriteLine("Installing service...");
                    svc.Install(TestArg("-start"));
                    Console.WriteLine("... done");
                }
                else if (TestArg("-remove"))
                {
                    Console.WriteLine("Removing service...");
                    svc.Uninstall();
                    Console.WriteLine("... done");
                }
                else
                {
                    engine = new Engine();

                    ServiceBase.Run(svc);
                }
                return;
            }

            tweaks = new Tweaks();

            client = new PipeClient();

            if (!AdminFunc.IsDebugging())
            {
                Console.WriteLine("Trying to connect to Engine...");
                if (!client.Connect(1000))
                {
                    if (!AdminFunc.IsAdministrator())
                    {
                        Console.WriteLine("Trying to obtain Administrative proivilegs...");
                        if (AdminFunc.SkipUacRun(mName, App.args))
                        {
                            return;
                        }

                        Console.WriteLine("Trying to start with 'runas'...");
                        // Restart program and run as admin
                        var              exeName   = Process.GetCurrentProcess().MainModule.FileName;
                        string           arguments = "\"" + string.Join("\" \"", args) + "\"";
                        ProcessStartInfo startInfo = new ProcessStartInfo(exeName, arguments);
                        startInfo.UseShellExecute = true;
                        startInfo.Verb            = "runas";
                        try
                        {
                            Process.Start(startInfo);
                            return; // we restarted as admin
                        }
                        catch
                        {
                            MessageBox.Show(Translate.fmt("msg_admin_rights", mName), mName);
                            return; // no point in cintinuing without admin rights or an already running engine
                        }
                    }
                    else if (svc.IsInstalled())
                    {
                        Console.WriteLine("Trying to start service...");
                        if (svc.Startup())
                        {
                            Console.WriteLine("Trying to connect to service...");

                            if (client.Connect())
                            {
                                Console.WriteLine("Connected to service...");
                            }
                            else
                            {
                                Console.WriteLine("Failed to connect to service...");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Failed to start service...");
                        }
                    }
                }
            }

            // if we couldn't connect to the engine start it and connect
            if (!client.IsConnected() && AdminFunc.IsAdministrator())
            {
                Console.WriteLine("Starting Engine Thread...");

                engine = new Engine();

                engine.Start();

                Console.WriteLine("... engine started.");

                client.Connect();
            }

            // ToDo: use a more direct communication when running in one process

            itf = client;
            cb  = client;

            /*if (TestArg("-console-debug"))
             * {
             *  Console.WriteLine("Private WinTen reporting for duty, sir!");
             *  Console.WriteLine("");
             *
             *  for (bool running = true; running;)
             *  {
             *      String Line = Console.ReadLine();
             *      if (Line.Length == 0)
             *          continue;
             *
             *      String Command = TextHelpers.GetLeft(ref Line).ToLower();
             *
             *      if (Command == "quit" || Command == "exit")
             *          running = false;
             *
             *      if (Command == "test")
             *      {
             *      }
             *      else
             *      {
             *          Console.WriteLine("Unknown Command, sir!");
             *          continue;
             *      }
             *      Console.WriteLine("Yes, sir!");
             *  }
             *
             *  return;
             * }*/

            Console.WriteLine("Preparing GUI...");

            var app = new App();

            app.InitializeComponent();

            InitLicense();

            mTray         = new TrayIcon();
            mTray.Action += TrayAction;
            mTray.Visible = GetConfigInt("Startup", "Tray", 0) != 0;

            mMainWnd = new MainWindow();
            if (!App.TestArg("-autorun") || !mTray.Visible)
            {
                mMainWnd.Show();
            }

            app.Run();

            mTray.DestroyNotifyicon();

            client.Close();

            if (engine != null)
            {
                engine.Stop();
            }
        }