Пример #1
0
    static void Main()
    {
        string[] args = Environment.GetCommandLineArgs();

        // not obsolete for the migration to the new settings..
#pragma warning disable 618
        SettingsOld.FromOldSettings(Settings);
#pragma warning restore 618

        Settings.Load(Settings.SettingFileName);

        // wait for the possible install process to finnish..
        VPKSoft.WaitForProcessUtil.WaitForProcess.WaitForProcessArguments(args, 30);

        foreach (var arg in args)
        {
            if (arg.StartsWith("--restoreBackup"))
            {
                var restoreParameters = arg.Split('=');
                if (restoreParameters.Length == 2)
                {
                    if (File.Exists(restoreParameters[1]))
                    {
                        while (AppRunning.CheckIfRunningNoAdd("VPKSoft.amp.sharp#"))
                        {
                            Thread.Sleep(100);
                        }

                        try
                        {
                            using (ZipFile zip = ZipFile.Read(restoreParameters[1]))
                            {
                                zip.ExtractAll(Paths.GetAppSettingsFolder(Misc.AppType.Winforms),
                                               ExtractExistingFileAction.OverwriteSilently);
                            }
                        }
                        catch (Exception ex)
                        {
                            // log the exception..
                            ExceptionLogger.LogError(ex);
                        }
                    }
                }
            }
        }


        Process localizeProcess = Utils.CreateDBLocalizeProcess(Paths.AppInstallDir);
        // localizeProcess..

        if (localizeProcess != null)
        {
            localizeProcess.Start();
            return;
        }

        if (!Debugger.IsAttached)
        {
            ExceptionLogger.Bind(); // bind before any visual objects are created
        }

        ExceptionLogger.ApplicationCrashData += ExceptionLogger_ApplicationCrashData;


        // Save languages
        if (Utils.ShouldLocalize() != null)
        {
            // ReSharper disable once ObjectCreationAsStatement
            new FormMain();
            // ReSharper disable once ObjectCreationAsStatement
            new FormPsycho();
            // ReSharper disable once ObjectCreationAsStatement
            new FormProgressBackground();
            // ReSharper disable once ObjectCreationAsStatement
            new FormAddAlbum();
            // ReSharper disable once ObjectCreationAsStatement
            new FormRename();
            // ReSharper disable once ObjectCreationAsStatement
            new FormQueueSnapshotName();        // 16.10.17
            // ReSharper disable once ObjectCreationAsStatement
            new FormSavedQueues();              // 18.10.17
            // ReSharper disable once ObjectCreationAsStatement
            new FormModifySavedQueue();         // 19.10.17
            // ReSharper disable once ObjectCreationAsStatement
            new FormSettings();                 // 21.10.17
            // ReSharper disable once ObjectCreationAsStatement
            new FormTagInfo();                  // 11.02.18
            // ReSharper disable once ObjectCreationAsStatement
            new FormAlbumNaming();              // 27.10.18
            // ReSharper disable once ObjectCreationAsStatement
            new FormDatabaseUpdatingProgress(); // 27.10.18
            // ReSharper disable once ObjectCreationAsStatement
            new FormHelp();                     // 28.10.18
            // ReSharper disable once ObjectCreationAsStatement
            new FormRandomizePriority();        // 30.10.18
            // ReSharper disable once ObjectCreationAsStatement
            new FormDatabaseMigrate();          // 01.09.19
            // ReSharper disable once ObjectCreationAsStatement
            new FormThemeSettings();

            ExceptionLogger.ApplicationCrashData -= ExceptionLogger_ApplicationCrashData;
            ExceptionLogger.UnBind(); // unbind so the truncate thread is stopped successfully
            return;
        }
        // End save languages

#pragma warning disable 618
        // required for history reasons..
        if (Settings.DbUpdateRequiredLevel < 1)
#pragma warning restore 618
        {
            if (AppRunning.CheckIfRunningNoAdd("VPKSoft.amp.DBUpdate.sharp#"))
            {
                ExceptionLogger.ApplicationCrashData -= ExceptionLogger_ApplicationCrashData;
                ExceptionLogger.UnBind(); // unbind so the truncate thread is stopped successfully
                return;
            }
            DBLangEngine.UseCulture = Settings.Culture; // set the localization value..
            Application.Run(new FormDatabaseUpdatingProgress());
            ExceptionLogger.ApplicationCrashData -= ExceptionLogger_ApplicationCrashData;
            ExceptionLogger.UnBind(); // unbind so the truncate thread is stopped successfully
#pragma warning disable 618
            // required for history reasons..
            Settings.DbUpdateRequiredLevel = 1;
#pragma warning restore 618
            Process.Start(new ProcessStartInfo(Path.Combine(Paths.AppInstallDir, "amp.exe"))); // the database is updated..
            return;
        }

        if (AppRunning.CheckIfRunningNoAdd("VPKSoft.amp.sharp#"))
        {
            ExceptionLogger.LogMessage($"Application is running. Checking for open file requests. The current directory is: '{Environment.CurrentDirectory}'.");
            try
            {
                RpcSelfClient <string> ipcClient = new RpcSelfClient <string>(50671);

                // only send the existing files to the running instance, don't send the executable
                // file name thus the start from 1..
                for (int i = 1; i < args.Length; i++)
                {
                    string file = args[i];

                    ExceptionLogger.LogMessage($"Request file open: '{file}'.");
                    bool exists;

                    if ((exists = File.Exists(file)) ||
                        file == ArgumentNext ||
                        file == ArgumentPrevious ||
                        file == ArgumentPlayPause)
                    {
                        if (exists)
                        {
                            ExceptionLogger.LogMessage($"File exists: '{file}'. Send open request.");
                        }
                        else
                        {
                            file = file.TrimStart('-');
                            file = file[0].ToString().ToUpperInvariant() + file.Substring(1);
                            ExceptionLogger.LogMessage($"Send playback request: '{file}'.");
                        }

                        ipcClient.SendData(file);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogError(ex);
                // just in case something fails with the IPC communication..
            }
            ExceptionLogger.ApplicationCrashData -= ExceptionLogger_ApplicationCrashData;
            ExceptionLogger.UnBind(); // unbind so the truncate thread is stopped successfully..
            return;
        }

        AppRunning.CheckIfRunning("VPKSoft.amp.sharp#");

        // create an IPC server at localhost, the port was randomized in the development phase..
        IpcServer = new RpcSelfHost <string>(50671);

        // subscribe to the IPC event if the application receives a message from another instance of this application..
        IpcServer.MessageReceived += MessageReceived;

        PositionCore.Bind(ApplicationType.WinForms); // attach the PosLib to the application
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        DBLangEngine.UseCulture = Settings.Culture;    // set the localization value..
        Application.Run(new FormMain());
        FormHelp.DisposeSingleton();                   // release the help form if any..
        PositionCore.UnBind(ApplicationType.WinForms); // release the event handlers used by the PosLib and save the default data

        // unsubscribe the IpcClientServer MessageReceived event handler..
        IpcServer.MessageReceived -= MessageReceived;
        IpcServer.Dispose();

        ExceptionLogger.ApplicationCrashData -= ExceptionLogger_ApplicationCrashData;
        ExceptionLogger.UnBind(); // unbind so the truncate thread is stopped successfully
        AppRunning.DisposeMutexByName("VPKSoft.amp.sharp#");

        // run a possible software (as of the time of the writing the software is this one.)..
        if (File.Exists(RunProgramOnExit))
        {
            Process.Start(RunProgramOnExit, RunProgramOnExitArguments);
        }
        else if (FormMain.RestartRequired)
        {
            Process.Start(Application.ExecutablePath);
        }
    }