@@ -0,0 +1,103 @@
+---
+title: RGSS 中读取注册表
+author: orzFly
+layout: post
+permalink: /html/rgss-registry.html
+yourls_shorturl:
+ - http://miv.im/hj5h
+categories:
+ - Coding
+tags:
+ - Registry
+ - RGSS
+ - RPG Maker
+ - Ruby
+---
+有了这段脚本,便可以在 RGSS 中使用一种较为简单的语法来获得注册表中的字符串值,如:`Registry::HKLM["SoftwareEnterbrainRGSSRTP"]["Standard"]`
+因为 [Azoki][1] 中用到了这个,于是先发出来最简单的部分好了。
+这段脚本依赖于 [String#s2u][2]。<!--more-->
+
+
+`
+<pre>#==============================================================================
+# ■ 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</pre>
+<p>`
+
+ [1]: http://orzfly.com/html/azoki.html
+ [2]: http://orzfly.com/html/rgss-string-encoding.html
\ No newline at end of file