RGSS 中读取注册表RGSS 中读取注册表

有了这段脚本,便可以在 RGSS 中使用一种较为简单的语法来获得注册表中的字符串值,如:Registry::HKLM["SoftwareEnterbrainRGSSRTP"]["Standard"]
因为 Azogi 中用到了这个,于是先发出来最简单的部分好了。
这段脚本依赖于 String#s2u

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#==============================================================================
# ■ Registry
#------------------------------------------------------------------------------
#  极其简单的一个只能读取字符串值的注册表读取模块。
#------------------------------------------------------------------------------
=begin
来源:https://orzfly.com/html/rgss-registry.html
依赖:https://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

Version History版本历史

  • 0143959 I18N changes: Add language field to posts
  • 4a8e516 Rename categories: stuff -> data, coding -> tech
  • a0c097c Fix some formatting & broken links; remove yourls_shorturl, categories and tags
  • c1bc9db Fix some formatting & broken links; remove yourls_shorturl, categories and tags
  • 5b93ba5 Remove author & delete some broken posts
  • 1ee024d Import posts from my WordPress blog