Ejemplo n.º 1
0
        public MainForm(string[] args)
        {
            bool canSetCurrentDir = true;

            this.StartPosition = FormStartPosition.WindowsDefaultLocation;

            bool          splash    = false;
            List <string> fileNames = new List <string>();

            // Parse command line arguments
            foreach (string argument in args)
            {
                if (0 == string.Compare(argument, "/dontForceGC"))
                {
                    Utility.AllowGCFullCollect = false;
                }
                else if (0 == string.Compare(argument, "/splash", true))
                {
                    splash = true;
                }
                else if (0 == string.Compare(argument, "/test", true))
                {
                    // This lets us use an alternate update manifest on the web server so that
                    // we can test manifests on a small scale before "deploying" them to everybody
                    PdnInfo.IsTestMode = true;
                }
                else if (0 == string.Compare(argument, "/profileStartupTimed", true))
                {
                    // profileStartupTimed and profileStartupWorkingSet compete, which
                    // ever is last in the args list wins.
                    PdnInfo.StartupTest = StartupTestType.Timed;
                }
                else if (0 == string.Compare(argument, "/profileStartupWorkingSet", true))
                {
                    // profileStartupTimed and profileStartupWorkingSet compete, which
                    // ever is last in the args list wins.
                    PdnInfo.StartupTest = StartupTestType.WorkingSet;
                }
                else if (argument.Length > 0 && argument[0] != '/')
                {
                    try
                    {
                        string fullPath = Path.GetFullPath(argument);
                        fileNames.Add(fullPath);
                    }

                    catch (Exception)
                    {
                        fileNames.Add(argument);
                        canSetCurrentDir = false;
                    }

                    splash = true;
                }
            }

            if (canSetCurrentDir)
            {
                try
                {
                    Environment.CurrentDirectory = PdnInfo.GetApplicationDir();
                }

                catch (Exception ex)
                {
                    Tracing.Ping("Exception while trying to set Environment.CurrentDirectory: " + ex.ToString());
                }
            }

            // make splash, if warranted
            if (splash)
            {
                this.splashForm         = new SplashForm();
                this.splashForm.TopMost = true;
                this.splashForm.Show();
                this.splashForm.Update();
            }

            InitializeComponent();

            this.Icon = PdnInfo.AppIcon;

            // Does not load window location/state
            LoadSettings();

            foreach (string fileName in fileNames)
            {
                this.queuedInstanceMessages.Add(fileName);
            }

            // no file specified? create a blank image
            if (fileNames.Count == 0)
            {
                MeasurementUnit units   = Document.DefaultDpuUnit;
                double          dpu     = Document.GetDefaultDpu(units);
                Size            newSize = this.appWorkspace.GetNewDocumentSize();
                this.appWorkspace.CreateBlankDocumentInNewWorkspace(newSize, units, dpu, true);
                this.appWorkspace.ActiveDocumentWorkspace.IncrementJustPaintWhite();
                this.appWorkspace.ActiveDocumentWorkspace.Document.Dirty = false;
            }

            LoadWindowState();

            deferredInitializationTimer.Enabled = true;

            Application.Idle += new EventHandler(Application_Idle);
        }
Ejemplo n.º 2
0
        public static void MainImpl(string[] args)
        {
            //System.Windows.Forms.MessageBox.Show(Environment.CommandLine);

            // Syntax:
            //     SetupNgen </cleanUpStaging | </install | /delete> DESKTOPSHORTCUT=<0|1> PDNUPDATING=<0|1> SKIPCLEANUP=<0|1> PROGRAMSGROUP=relativeName QUEUENGEN=<0|1>>

            if (args.Length >= 2 && args[0] == "/cleanUpStaging")
            {
                // SetupNgen.exe is overloaded for cleaning up the "Staging" directory
                string stagingPath = args[1];

                // Sanity check: staging directory must ALWAYS have the word Staging in it (capitalized that way too)
                // It must exist, too.
                if (Directory.Exists(stagingPath) &&
                    -1 != stagingPath.IndexOf("Staging"))
                {
                    foreach (string filePath in Directory.GetFiles(stagingPath, "*.msi"))
                    {
                        try
                        {
                            Console.WriteLine("delete: " + filePath);
                            System.IO.File.Delete(filePath);
                        }

                        catch
                        {
                        }
                    }

                    try
                    {
                        Console.WriteLine("rmdir: " + stagingPath);
                        Directory.Delete(stagingPath);
                    }

                    catch
                    {
                    }
                }
            }
            else
            {
                bool delete = false;

                if (args.Length < 5)
                {
                    return;
                }

                if (args[0] == "/delete")
                {
                    delete = true;
                }

                // otherwise we assume args[0] == "/install"

                bool queueNgen;

                if (args[5] == "QUEUENGEN=1")
                {
                    queueNgen = true;
                }
                else
                {
                    queueNgen = false;
                }

                // Pre-JIT via ngen. These are in alphabetical order.
                string[] names =
                    new string[]
                {
                    "ICSharpCode.SharpZipLib.dll",
                    "PaintDotNet.Data.dll",
                    "PaintDotNet.Effects.dll",
                    "PaintDotNet.exe",
                    "PaintDotNet.Resources.dll",
                    "PaintDotNet.StylusReader.dll",
                    "PaintDotNet.SystemLayer.dll",
                    "PdnLib.dll"
                };

                string[] names32 =
                    new string[]
                {
                    "Interop.WIA.dll",
                    "WiaProxy32.exe"
                };

                foreach (string name in names)
                {
                    try
                    {
                        InstallAssembly(name, delete, queueNgen, false);
                    }

                    // We don't raise a stink if ngen fails.
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                foreach (string name in names32)
                {
                    try
                    {
                        InstallAssembly(name, delete, queueNgen, true);
                    }

                    // We don't raise a stink if ngen fails.
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                // Create desktop shortcut
                bool createDesktopShortcut = false;
                bool updating    = false;
                bool skipCleanup = false;

                if (args[1] == "DESKTOPSHORTCUT=1")
                {
                    createDesktopShortcut = true;
                }

                if (args[2] == "PDNUPDATING=1")
                {
                    updating = true;
                }

                if (args[3] == "SKIPCLEANUP=1")
                {
                    skipCleanup = true;
                }

                string       programsShortcutGroup = string.Empty;
                const string programsGroup         = "PROGRAMSGROUP";
                if (args[4].StartsWith(programsGroup + "=")) // starts with "PROGRAMSGROUP="
                {
                    programsShortcutGroup = args[4].Substring(1 + programsGroup.Length);
                }

                // Create shortcuts

                // Set up out strings
                string desktopDir  = GetSpecialFolderPath(CSIDL_COMMON_DESKTOPDIRECTORY);
                string programsDir = GetSpecialFolderPath(CSIDL_COMMON_PROGRAMS);

                string linkName            = PaintDotNet.PdnResources.GetString("Setup.DesktopShortcut.LinkName");
                string description         = PaintDotNet.PdnResources.GetString("Setup.DesktopShortcut.Description");
                string desktopLinkPath     = Path.Combine(desktopDir, linkName) + ".lnk"; // if we just use ChangeExtension it will overwrite the .NET part of Mono Paint :)
                string programsShortcutDir = Path.Combine(programsDir, programsShortcutGroup);
                string programsLinkPath    = Path.Combine(programsShortcutDir, linkName) + ".lnk";
                string workingDirectory    = PdnInfo.GetApplicationDir();
                string targetPath          = Path.Combine(workingDirectory, "PaintDotNet.exe");

                // Desktop shortcut
                if ((delete && !skipCleanup) || (!createDesktopShortcut && skipCleanup))
                {
                    if (System.IO.File.Exists(desktopLinkPath))
                    {
                        Console.WriteLine("delete: " + desktopLinkPath);

                        try
                        {
                            System.IO.File.Delete(desktopLinkPath);
                        }

                        catch
                        {
                        }
                    }
                }
                else if (createDesktopShortcut && !delete && !updating)
                {
                    CreateShortcut(desktopLinkPath, targetPath, workingDirectory, description);
                }

                // Programs shortcut
                const string programsShortcutPathKey = "ProgramsShortcutPath";
                const string pdnKey = @"SOFTWARE\Mono Paint";

                if (delete && !skipCleanup)
                {
                    string path = programsLinkPath;

                    // For the purposes of deleting the shortcut, we actually store the file's location in the registry
                    using (RegistryKey hklmPDN = Registry.LocalMachine.OpenSubKey(pdnKey, false))
                    {
                        if (hklmPDN != null)
                        {
                            object pathObj = hklmPDN.GetValue(programsShortcutPathKey, programsLinkPath);

                            if (pathObj is string)
                            {
                                path = (string)pathObj;
                            }
                        }
                    }

                    // Do some quick checks to make sure we don't delete something we don't want to
                    bool allowDelete = true;

                    // Verify that it is a .lnk file
                    allowDelete &= (Path.GetExtension(path) == ".lnk");

                    // Verify that it is in a subdirectory of Programs
                    allowDelete &= IsPathInDirectory(path, programsDir);

                    // Delete it
                    if (allowDelete && System.IO.File.Exists(path))
                    {
                        Console.WriteLine("delete: " + path);
                        System.IO.File.Delete(path);

                        // If we're the last shortcut, then delete that directory.
                        string dir = Path.GetDirectoryName(path);

                        try
                        {
                            System.IO.Directory.Delete(dir, false);
                        }

                        catch
                        {
                        }
                    }
                }
                else if (!delete && !updating)
                {
                    // Create it
                    if (!Directory.Exists(programsShortcutDir))
                    {
                        Directory.CreateDirectory(programsShortcutDir);
                    }

                    CreateShortcut(programsLinkPath, targetPath, workingDirectory, description);

                    // Save the location to the registry
                    using (RegistryKey hklmPDN = Registry.LocalMachine.CreateSubKey(pdnKey))
                    {
                        if (hklmPDN != null)
                        {
                            hklmPDN.SetValue(programsShortcutPathKey, programsLinkPath);
                        }
                    }
                }

                // Register shell extension
                const string shellExtensionName_x86 = "ShellExtension_x86.dll";
                const string shellExtensionName_x64 = "ShellExtension_x64.dll";
                const string shellExtensionGuid     = "{D292F82A-50BE-4351-96CC-E86F3F8049DA}";
                const string regKeyName             = @"CLSID\" + shellExtensionGuid;
                const string regKeyWow64Name        = @"Wow6432Node\" + regKeyName;
                const string shellExtension_regName = "Mono Paint Shell Extension";
                const string inProcServer32         = "InProcServer32";
                const string threadingModel         = "ThreadingModel";
                const string apartment = "Apartment";

                string[] shellExtensionFileNames;
                string[] regKeyNames;

                if (UIntPtr.Size == 4)
                {
                    shellExtensionFileNames = new string[1] {
                        shellExtensionName_x86
                    };
                    regKeyNames = new string[1] {
                        regKeyName
                    };
                }
                else
                {
                    Platform platform     = GetPlatform();
                    string   dll64bitName = shellExtensionName_x64;

                    shellExtensionFileNames = new string[2] {
                        dll64bitName, shellExtensionName_x86
                    };
                    regKeyNames = new string[2] {
                        regKeyName, regKeyWow64Name
                    };
                }

                string[] shellExtensionPaths = new string[shellExtensionFileNames.Length];

                for (int i = 0; i < shellExtensionFileNames.Length; ++i)
                {
                    shellExtensionPaths[i] = Path.Combine(ourPath, shellExtensionFileNames[i]);
                }

                if (!delete)
                {
                    // Register the shell extension
                    try
                    {
                        for (int i = 0; i < shellExtensionPaths.Length; ++i)
                        {
                            RegistryKey clsidKey = Registry.ClassesRoot.CreateSubKey(regKeyNames[i], RegistryKeyPermissionCheck.ReadWriteSubTree);
                            RegistryKey ips32key = clsidKey.CreateSubKey(inProcServer32);

                            clsidKey.SetValue(null, shellExtension_regName);
                            ips32key.SetValue(threadingModel, apartment);
                            ips32key.SetValue(null, shellExtensionPaths[i]);
                        }
                    }

                    catch
                    {
                    }
                }
                else
                {
                    // Unregister the shell extension
                    try
                    {
                        for (int i = 0; i < regKeyNames.Length; ++i)
                        {
                            Registry.ClassesRoot.DeleteSubKeyTree(regKeyNames[i]);
                        }
                    }

                    catch
                    {
                    }
                }
            }
        }