RGSS – orzFly https://oldblog.orzfly.com His Memories from Here to There, with Fuzzy Ginkgo Sat, 11 Feb 2017 13:57:34 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.5 RGSS 中读取注册表 https://oldblog.orzfly.com/html/rgss-registry.html Sun, 26 Aug 2012 04:45:23 +0000 http://orzfly.com/?p=219 有了这段脚本,便可以在 RGSS 中使用一种较为简单的语法来获得注册表中的字符串值,如:Registry::HKLM["SoftwareEnterbrainRGSSRTP"]["Standard"]
因为 Azoki 中用到了这个,于是先发出来最简单的部分好了。
这段脚本依赖于 String#s2u。

#==============================================================================
# ■ Registry
#------------------------------------------------------------------------------
#  极其简单的一个只能读取字符串值的注册表读取模块。
#------------------------------------------------------------------------------
=begin
  来源:http://orzfly.com/html/rgss-registry.html
  依赖:http://orzfly.com/html/rgss-string-encoding.html
  使用举例: 
    Registry::HKLM["SoftwareEnterbrainRGSSRTP"]["Standard"]
    读取 HKEY_LOCAL_MACHINESoftwareEnterbrainRGSSRTP 下的 Standard 的值
=end
#==============================================================================
module Registry
  #--------------------------------------------------------------------------
  # ● API 定义
  #--------------------------------------------------------------------------
  module NativeMethods
    RegOpenKey = Win32API.new('advapi32', 'RegOpenKeyExA', 'LPLLP', 'L')
    RegQueryValueEx = Win32API.new('advapi32', 'RegQueryValueExA', 'LPLPPP', 'L')
    RegCloseKey = Win32API.new('advapi32', 'RegCloseKey', 'L', 'L')
  end
  
  #--------------------------------------------------------------------------
  # ● 根代理
  #--------------------------------------------------------------------------
  class RegistryRoot
    def initialize root
      @root = root
    end
    
    def [] key
      RegistryKey.new @root, key
    end
  end
  
  #--------------------------------------------------------------------------
  # ● 键代理
  #--------------------------------------------------------------------------
  class RegistryKey
    include NativeMethods
    
    def initialize root, key
      @root, @key = root, key
    end
    
    def [] name
      begin
        key = 0.chr * 4

        RegOpenKey.call(@root, @key, 0, 0x20019, key)
        key = key.unpack('l')[0]
        type = 0.chr * 4
        size = 0.chr * 4

        RegQueryValueEx.call(key, name, 0, type, 0, size)
        data = ' ' * size.unpack('l')[0]

        RegQueryValueEx.call(key, name, 0, type, data, size)
        value = data.respond_to?(:force_encoding) ? 
          data.force_encoding("ascii-8bit").chop.s2u :
          data.chop.s2u
        RegCloseKey.call(key)
        
        return value
      rescue
        nil
      end
    end
  end
  
  #--------------------------------------------------------------------------
  # ● 常量定义
  #--------------------------------------------------------------------------
  HKEY_CLASSES_ROOT  = HKCR = RegistryRoot.new(0x80000000)
  HKEY_CURRENT_USER  = HKCU = RegistryRoot.new(0x80000001)
  HKEY_LOCAL_MACHINE = HKLM = RegistryRoot.new(0x80000002)
end

]]>
RGSS 中转换字符串的编码 https://oldblog.orzfly.com/html/rgss-string-encoding.html Sat, 25 Aug 2012 18:44:37 +0000 http://orzfly.com/?p=200 事实上 RGSS 是个 Ruby 的子集,Ruby 的许多重要特性被 Enterbrain 限制了。与此同时写脚本的时候或多或少会用到编码转换。总算有闲心把这个平时一直用到的代码包装了一下,希望对您有用。事实上实现这个功能的代码很多人都写过了,我的这个版本不过是重构了一下而已。

追加的 String 实例方法

  • iconv(src_encoding, dst_encoding) → str
    将自身的副本从 src_encoding 转换为 dst_encoding 并返回。两个 encoding 参数均接受 Windows 代码页数字或脚本中定义的六个 Symbol。
  • u2s → str
    iconv(:UTF8, :System) 的快捷方式。
  • s2u → str
    iconv(:System, :UTF8) 的快捷方式。

脚本

#==============================================================================
# ■ String(补充定义:编码转换)
#------------------------------------------------------------------------------
#  为字符串追加编码转换的机能。
#==============================================================================
# 来源:http://orzfly.com/html/rgss-string-encoding.html
#==============================================================================
# 参考:
#http://msdn.microsoft.com/en-us/library/windows/desktop/dd319072(v=vs.85).aspx
#http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130(v=vs.85).aspx
#http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
#==============================================================================
class String
  #--------------------------------------------------------------------------
  # ● 常量定义
  #--------------------------------------------------------------------------
  MultiByteToWideChar = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  WideCharToMultiByte = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
  Codepages = {
    :System => 0,     :UTF7   => 65000, :UTF8   => 65001,
    :S_JIS  => 932,   :GB2312 => 936,   :BIG5   => 950, 
  }
  #--------------------------------------------------------------------------
  # ● 伪 iconv 编码转换
  #--------------------------------------------------------------------------
  #     s : 原始编码,可使用 Codepages 中的符号或者直接使用代码页值。
  #     d : 目标编码,同上。
  #--------------------------------------------------------------------------
  def iconv s, d
    src  = s.is_a?(Symbol)? Codepages[s] : s
    dest = d.is_a?(Symbol)? Codepages[d] : d

    len = MultiByteToWideChar.call src, 0, self, -1, nil, 0
    buf = "" * (len * 2)
    MultiByteToWideChar.call src, 0, self, -1, buf, buf.size / 2

    len = WideCharToMultiByte.call dest, 0, buf, -1, nil, 0, nil, nil
    ret = "" * len
    WideCharToMultiByte.call dest, 0, buf, -1, ret, ret.size, nil, nil

    self.respond_to?(:force_encoding) ?
    ret.force_encoding("ASCII-8BIT").delete("x00") :
    ret.delete("x00")
  end
  #--------------------------------------------------------------------------
  # ● 快捷方式:从系统代码页转为 UTF-8 编码
  #--------------------------------------------------------------------------
  def s2u
    self.respond_to?(:force_encoding) ?
    iconv(:System, :UTF8).force_encoding("utf-8") :
    iconv(:System, :UTF8)
  end
  #--------------------------------------------------------------------------
  # ● 快捷方式:从 UTF-8 编码转为系统代码页
  #--------------------------------------------------------------------------
  def u2s
    iconv(:UTF8, :System)
  end
end

]]>