设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 64|回复: 0

[制作教程] Automatically running a Temporary Font With RMXP using PowerShell.

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    前天 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    4471

    主题

    864

    回帖

    2万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    22949
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    28314

    灌水之王

    发表于 7 天前 | 显示全部楼层 |阅读模式
    This tutorial explains how I managed to add a non-standard temporary font to an RPG Maker XP game at runtime without using the aging Win32API library approach.

    This was achieved by using a PowerShell script to load the Font directly into the currently open windows session.
    Meaning it temporarily installs a font that only lasts while the RPG Maker XP game session is running and is then discarded in a similar way to how the newer versions of RPG Maker work. This method uses a simple 'Custom Font Loaded' Module added to the Script Editor, which is then called at the beginning of the 'Main' Class script when the XP game runs.
    I have had success using this method but provide no guarantees and it is supplied as is.

    Steps Taken:
    1) Make two subdirectories directly under your XP game directory.
        One called: Font
        One called: PowerShell
    2) Copy your required .ttf font file into the Font directory.
    3) Make a text file called LoadFont.ps1 in the PowerShell directory.
        Then paste the contents of the PowerShell script below into that file and save it.
                    Code:        
    param(     [Parameter(Mandatory=$true)]     [string]$FontPath )  # --- LOAD FONT  --- function Load-TemporaryFont {       # Check the file exists     if (-not (Test-Path -Path $FontPath -PathType Leaf)) {         Write-Error "ERROR: Font file not found at: $FontPath"         exit 1 # Exit with error code 1     }      try {         $AbsoluteFontPath = (Resolve-Path $FontPath).Path          # Setup the Windows function...         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);                   public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);                 public static readonly int WM_FONTCHANGE = 0x001d;             } "@          # Load the font into the session         $resourceHandle = [FontLoader]::AddFontResource($AbsoluteFontPath)          if ($resourceHandle -ne 0) {             # Broadcast the change to all windows so the game can detect it             [FontLoader]::SendMessage([FontLoader]::HWND_BROADCAST, [FontLoader]::WM_FONTCHANGE, 0, 0)             Write-Output "SUCCESS: Font successfully loaded for session: $AbsoluteFontPath"             exit 0 # Exit successfully         } else {             Write-Error "ERROR: AddFontResource failed for unknown reason (File might be corrupted or in use)."             exit 2 # Exit with error code 2         }      } catch {         # Catch any errors         Write-Error "FATAL ERROR during font loading: $($_.Exception.Message)"         exit 3 # Exit with error code 3     } }  # Run the function Load-TemporaryFont

    4) Open your RPG Maker XP game in the Game Maker editor application and go to the Tools --> Script Editor option (Or press the F11 key)
           Once in the script editor scroll down to the end of the list where you see the 'Main' script.
    5) Next we need to add the 'Custom Font Loader' module.
           Make a new script just above the existing 'Main' script.
            You can right-click on a script name and select insert to make a new script.
            This will have a blank name field, use the 'Name' text box underneath the scripts list to rename this blank entry to 'Custom Font Loader'
        Next paste the contents of the module into the newly created script.
                    Ruby:        
    #============================================================================== # ** Scene_FontLoaderPS #------------------------------------------------------------------------------ #  This module executes the Font Load PowerShell script. #==============================================================================  module FontLoaderPS   def self.load_font(font_filename)     font_path = File.expand_path("Font/#{font_filename}")      ps_script = "start /b powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File \"PowerShell\\LoadFont.ps1\" -FontPath \"#{font_path}\""     #This needs to be called asynchronously or RMXP gets confused when the script closes.     system(ps_script)   end end


    6) Finally you need to edit the 'Main' script and add the commands to call the PowerShell script loader and setup the custom font file at the beginning of this script.
        Add these lines directly under the 'begin' keyword, E.g.

                    Ruby:        
    #============================================================================== # ** Main #------------------------------------------------------------------------------ #  After defining each class, actual processing begins here. #==============================================================================  begin     #Set the Font   FontLoaderPS.load_font("{filename}.ttf")   #wait to mitigate the race condition... This may need to be changed for your game,   sleep(0.3)   $defaultfonttype = $fontface = $fontname = Font.default_name = "{Font Name}"   $defaultfontsize = $fontsize = Font.default_size = 18


        The {filename}.ttf needs to be the name of the Windows Font file as it appears in the Fonts directory.
        The {Font Name} needs to be the actual proper name of the font, which can be different from the file name.
        So remember these placeholders need to be replaced by the real names.
        E.g.
                 FontLoaderPS.load_font("Mordred.ttf")
                 $defaultfonttype = $fontface = $fontname = Font.default_name = "Mordred"

    If setup properly this should load the font into the current session when the game first runs.
    If it works for you this approach is much more straightforward, simple and modular than the old Ruby scripting method.
    And since the font was only loaded temporarily using PowerShell's AddFontResource, it will automatically be neatly removed when the RMXP game session is closed with no user involvement in setting up fonts.



    P.s. I kept my Try Catch testing in the PowerShell script so you can see the testing I did. Technically you don't need that but it keeps it safe if the script can't run for whatever windows related reason there may be!


    本贴来自国际rpgmaker官方论坛作者:Garryg处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/automatically-running-a-temporary-font-with-rmxp-using-powershell.181324/
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-7-14 18:01 , Processed in 0.067153 second(s), 55 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表