Jump to content

Detecting Portable mode when using 3.5.11 DLLs


null54

Recommended Posts

This is a hack that is probably not relevant to 99% of plugins, but it may be useful for plugins that allow the user to select folders containing brushes

or other data files outside of the Paint.NET User Files.

 

 I wanted to have a way to disable the selection of external plugin folders in PSFilterPdn when Paint.NET is running in portable mode.

To maintain compatibility with 3.5.11 the following code reads the value of the IAppInfoService InstallMode property via Reflection.

 

Spoiler

internal static class PdnInstallModeHelper
{
    private static PropertyInfo installTypeProperty;
    private static bool initializedInstallTypeProperty;
    private static bool isPortableMode;

    public static void Initialize(IServiceProvider serviceProvider)
    {
        isPortableMode = false;
        if (serviceProvider != null)
        {
            IAppInfoService appInfo = serviceProvider.GetService<IAppInfoService>();

            if (appInfo != null)
            {
                try
                {
                    if (!initializedInstallTypeProperty)
                    {
                        initializedInstallTypeProperty = true;

                        installTypeProperty = appInfo.GetType().GetProperty("InstallType");
                    }

                    if (installTypeProperty != null)
                    {
                        object appInstallMode = installTypeProperty.GetValue(appInfo, null);

                        if (appInstallMode != null)
                        {
                            isPortableMode = string.Equals(appInstallMode.ToString(), "Portable", StringComparison.Ordinal);
                        }
                    }
                }
                catch (Exception)
                {
                    // Ignore any errors, good faith effort only.
                }
            }
        }
    }

    public static bool IsPortableMode
    {
        get
        {
            return isPortableMode;
        }
    }
}

 

 

There probably is a better way to detect portable mode when referencing the  3.5.11 DLLs, but if there is I have not found it.

PdnSig.png

Plugin Pack | PSFilterPdn | Content Aware Fill | G'MICPaint Shop Pro Filetype | RAW Filetype | WebP Filetype

The small increase in performance you get coding in C++ over C# is hardly enough to offset the headache of coding in the C++ language. ~BoltBait

 

Link to comment
Share on other sites

Why would you need this information in the first place though? edit: you stated why above :)

 

Also, you have no error handling code. Your code could easily crash if, for example, the property doesn't exist.

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

20 hours ago, Rick Brewster said:

Also, you have no error handling code. Your code could easily crash if, for example, the property doesn't exist.

 

Added.

 

20 hours ago, Rick Brewster said:

Why would you need this information in the first place though? edit: you stated why above :)

 

After thinking about it some more, I am not convinced that arbitrarily disabling plugin functionality based on the type of Paint.NET install is a good idea.

 

My original reasoning was that allowing the user to add search directories would not make sense as there is no guarantee that two systems would have the same folders, but the current "portable" installs already have that issue and if a folder does not exist or gets deleted when the effect is executing my code would still have to handle that case

(to quote Raymond Chen "You just have to accept that the file system can change").

 

Another reason was to reduce the number of writes to disk if the Paint.NET User Files is stored on a flash drive.

 

20 hours ago, Rick Brewster said:

I'd still prefer you just compile against the latest binaries. 3.5 is dead. Please, don't encourage its use.

 

The reason I compile against 3.5.x is that I do not want to prevent users on older OSes from using my plugins, and supporting multiple versions is too much work.

The DX10 DDS file type only supports 4.0.17 and later.

PdnSig.png

Plugin Pack | PSFilterPdn | Content Aware Fill | G'MICPaint Shop Pro Filetype | RAW Filetype | WebP Filetype

The small increase in performance you get coding in C++ over C# is hardly enough to offset the headache of coding in the C++ language. ~BoltBait

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...