Beispiel #1
0
        private static void Main()
        {
            string[] args = Environment.GetCommandLineArgs();

            if (args.Length > 1 && args[1] == "-setpermissions")
            {
                System.Security.Principal.SecurityIdentifier sid  = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null);
                System.Security.Principal.NTAccount          acct = sid.Translate(typeof(System.Security.Principal.NTAccount)) as System.Security.Principal.NTAccount;
                string strEveryoneAccount = acct.ToString();
                GeneralHelper.AddDirectorySecurity(".\\", strEveryoneAccount, FileSystemRights.FullControl, InheritanceFlags.None, PropagationFlags.NoPropagateInherit, AccessControlType.Allow);
                GeneralHelper.AddDirectorySecurity(".\\", strEveryoneAccount, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);

                GeneralHelper.RemoveReadOnlyRecursive(".\\");

                return;
            }

            string name = Path.GetFileNameWithoutExtension(Application.ExecutablePath);

            bool moved = false;

#if DEBUG
            bool firstInstance = true;
#else
            bool firstInstance = Process.GetProcessesByName(name).Length < 2;
#endif

            string file = "";

            if (args.Length > 1)
            {
                for (int i = 1; i < args.Length; i++)
                {
                    file += args[i] + " ";
                }
                file = file.Trim();

                moved = true;
            }

            if (!firstInstance)
            {
                Thread.Sleep(1500);
                firstInstance = Process.GetProcessesByName(name).Length < 2;
                if (!firstInstance && !moved)
                {
                    if (DialogResult.OK == MessageBox.Show("osu! is already running!\nWould you like to force-kill the other copy?", name,
                                                           MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1))
                    {
                        KillStuckProcesses(name);
                        int count = 0;
                        while (count++ < 20 && !firstInstance)
                        {
                            Thread.Sleep(100);
                            firstInstance = Process.GetProcessesByName(name).Length < 2;
                        }
                        if (!firstInstance)
                        {
                            MessageBox.Show("Killing failed.  Please manually kill the process using Task Manager.", name,
                                            MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }

            if (moved || firstInstance)
            {
                if (firstInstance)
                {
                    try
                    {
                        IPC.AcceptConnections();
                    }
                    catch (Exception)
                    {
                    }
                }
                else
                {
                    try
                    {
                        TcpClient    c = new TcpClient("localhost", 13373);
                        StreamWriter w = new StreamWriter(c.GetStream());
                        w.WriteLine(file);

                        w.Flush();

                        c.Close();
                    }
                    catch (Exception)
                    {
                    }
                    return;
                }
            }


            Environment.CurrentDirectory = Application.StartupPath;

            using (GameBase game = new GameBase(file))
            {
                if (File.Exists("_osume.exe"))
                {
                    File.Delete("osume.exe");
                    File.Move("_osume.exe", "osume.exe");
                    GeneralHelper.ProcessStart("osume.exe");
                }
                else
#if !DEBUG
                { try
                  {
#endif
                    game.Run(); }
                if (Restart)
                {
                    GeneralHelper.ProcessStart("osu!.exe");
                }
#if !DEBUG
            }