Has anybody tried to get fonts to automatically load using PowerShell for RMXP?

I know about the old scripting way but I’ve been trying to get a neater solution recently using PowerShell.

I wrote a module and placed it before the main class in the scripts list:
Ruby:
module FontLoaderPS
  def self.load_font(font_filename)
    font_path = File.expand_path("Font/#{font_filename}")
    ps_script = "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File \"PowerShell\\LoadFont.ps1\" -FontPath \"#{font_path}\""
   system_output = `#{ps_script}`
  end
end

Then I called this at the top of the Main Class:
Ruby:
begin
 
  #First set the Font
  FontLoaderPS.load_font("Mordred.ttf")
  $defaultfonttype = $fontface = $fontname = Font.default_name = "Mordred"
  $defaultfontsize = $fontsize = Font.default_size = 18

This part seemed to work fine calling the PowerShell script:
Code:
#PowerShell
param(
    [Parameter(Mandatory=$true)]
    [string]$FontPath
)

# --- FONT LOADING LOGIC ---
function Load-TemporaryFont {
    $AbsoluteFontPath = (Resolve-Path $FontPath).Path

    Add-Type -TypeDefinition @"
        using System;
        using System.Runtime.InteropServices;
        public class FontLoader {
            [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr AddFontResource(string lpszFilename);
         
            [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
            public static extern int RemoveFontResource(string lpszFilename);

            [DllImport("user32.dll")]
            public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
         
            // Constants for broadcasting the font change to the system
            public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
            public static readonly int WM_FONTCHANGE = 0x001d;
        }
"@

    [FontLoader]::AddFontResource($AbsoluteFontPath)
    [FontLoader]::SendMessage([FontLoader]::HWND_BROADCAST, [FontLoader]::WM_FONTCHANGE, 0, 0)
    Write-Output "Font successfully loaded for session: $AbsoluteFontPath"
}

# Load the font immediately when the script runs
Load-TemporaryFont

Now the PowerShell script appears to be running fine but the game just stopes after this script finishes. If I put a read-host at the end it shows this getting through the script without error.
But the game itself just seems to close then. I'm not really sure why as the script is running outwith the game.

I know it isn't the:
Ruby:
$defaultfonttype = $fontface = $fontname = Font.default_name = "Mordred"
$defaultfontsize = $fontsize = Font.default_size = 18 commands.
lines after I call the script code as these were already tested before trying to add the script.

The idea is that if I can load a font into Windows temporarily in the current session from within the game it should be usable by the game then get dropped when the game ends.

Has anybody else tried this, can it work and I'm just doing something wrong?
I recommend just using mkxp-z and working with what it provides.
I recommend just using mkxp-z and working with what it provides.
Thanks, but I'm trying to make a better way of automatically packaging and loading custom fonts on the original RMXP not using something else completely. I'm just not seeing why this isn't working.

I think it may just be a resource conflict so maybe a combination of a wait command and running it in parallel will work.

The programming is the thing here i'm not really doing this to fix any particular game, I'm just trying to replace an older convoluted thing with a newer option! But thanks for your reply.
Just wanted to say I got this running. I've submitted a tutorial to these boards on how I did it if anybody is interested. Hopefully it should be approved soon.
I basically had to run it asyncronousley, so RMXP didn't get confused when the PS script finished, and added a wait to stop conflicts.

But if anybody is interested or has any questions about what I did just feel free to ask.
Or let me know if you have any ideas on how to impruve it!
You can copy the fonts to the user folder via command line if you are using Windows 10 or Windows 11, which means you don't need administrator privileges:
%userprofile%\AppData\Local\Microsoft\Windows\Fonts

Alternatively, include the font in the game folder via the current user's registry, but only the user who starts the game will have access to the font through other software (as Notepad):
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Fonts


If the user is using Windows 7 or Windows 8, it is possible to load the font using the Win32API in the current login session after running the game; that is, upon logging out, the loading is cleared (Working in Windows 10/11).

But at the moment I have no idea how to use RGSS to call external libraries. I used it to change the fonts in Rm2k/2k3 without having to close the game or have administrative privileges:
If what Dev_without_coffee tells us is true, and I hope so, you don't need external bash script to accomplish it. Ruby already has the ENV hash variable to access locations such as ENV["USERPROFILE"]. Then add FileUtils module script to XP through VX ACE, and voila, you can now copy all fonts you want to the Fonts directory.
Mine is just another way to do it without using the old method then! The PowerShell way is a non-complicated way to automatically load the font in the background from within the game code while the game is running and disposed of it when it is done. So is a bit cleaner and more self-contained than the method described here. Is all. I have VX but not sure if I have VX ACE. You don't need any Admin privilege for this to work ether. So this is all invisible to the user who dosn't need to do anything with the font file for it to work..
Sorry, I hadn't analyzed that code; it's exactly the same as the link I sent, but with a different library:
This part seemed to work fine calling the PowerShell script:
Code:
#PowerShell
param(
    [Parameter(Mandatory=$true)]
    [string]$FontPath
)

# --- FONT LOADING LOGIC ---
function Load-TemporaryFont {
    $AbsoluteFontPath = (Resolve-Path $FontPath).Path

    Add-Type -TypeDefinition @"
        using System;
        using System.Runtime.InteropServices;
        public class FontLoader {
            [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr AddFontResource(string lpszFilename);
      
            [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
            public static extern int RemoveFontResource(string lpszFilename);

            [DllImport("user32.dll")]
            public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
      
            // Constants for broadcasting the font change to the system
            public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
            public static readonly int WM_FONTCHANGE = 0x001d;
        }
"@

    [FontLoader]::AddFontResource($AbsoluteFontPath)
    [FontLoader]::SendMessage([FontLoader]::HWND_BROADCAST, [FontLoader]::WM_FONTCHANGE, 0, 0)
    Write-Output "Font successfully loaded for session: $AbsoluteFontPath"
}

# Load the font immediately when the script runs
Load-TemporaryFont

Regarding the folder issue, it's as simple as loading it into memory, as you wouldn't need to repeat it or use PowerShell, since you could check for the folder's existence when starting the game.

EDIT:
I found a script that loads FONT file into memory without executing external command lines:
Thanks I'll have a look at that!

Original thread: https://forums.rpgmakerweb.com/threads/has-anybody-tried-to-get-fonts-to-automatically-load-using-powershell-for-rmxp.181199/