这个模块是为了在脚本中操作 RGSS 的两个选项:平滑模式和抑制画面抖动(垂直同步)。除了 RGSS100J 无法使用之外,其他任何 RGSS 1~3 版本理论上都能使用。
脚本内容已放在后文,不过脚本中并没有直接设置。使用时,不仅应当引入脚本,还应该调用它的方法。
RGSSRuntime.smooth_mode
读取/设置 RGSS 的平滑模式选项。true 为开启,false 为关闭。在 RGSS 版本不支持的时候,会返回 nil,对其设置将不会产生任何效果。
RGSSRuntime.wait_for_vsync
读取/设置 RGSS 的抑制画面抖动(垂直同步)选项。true 为开启,false 为关闭。在 RGSS 版本不支持的时候,会返回 nil,对其设置将不会产生任何效果。
脚本正文
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | =begin RGSSRuntime Module 1.0 for RPG Maker XP/VX/VX Ace http://orzFly.com/html/rgssruntime.html Copyright 2013-2014 Yeechan Lu a.k.a. orzFly <i@orzFly.com> Partial Copyright 2013-2014 Seiran A. [http://seiran.mist.so/] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =end unless defined? RGSSRuntime module RGSSRuntime; end class << RGSSRuntime ASCIICHAR = [0].pack('C') GetModuleHandleA = Win32API.new("kernel32", "GetModuleHandleA", "p", "l") LoadAccelerators = Win32API.new("user32", "LoadAccelerators", "ll", "l") VirtualQuery = Win32API.new("kernel32", "VirtualQuery", "lpl", "l") RtlMoveMemory_pl = Win32API.new("kernel32", "RtlMoveMemory", "pli", "v") RtlMoveMemory_lp = Win32API.new("kernel32", "RtlMoveMemory", "lpi", "v") GetWindowTextA = Win32API.new("user32", "GetWindowTextA", "lpi", "i") GetWindowTextW = Win32API.new("user32", "GetWindowTextW", "lpi", "i") GetPrivateProfileStringA = Win32API.new('kernel32', 'GetPrivateProfileStringA','ppppip', 'v') GetWindowThreadProcessId = Win32API.new("user32", "GetWindowThreadProcessId", "LP", "L") GetWindow = Win32API.new("user32", "GetWindow", "LL", "L") GetClassName = Win32API.new("user32", "GetClassName", "LPL", "L") GetCurrentThreadId = Win32API.new("kernel32", "GetCurrentThreadId", "V", "L") GetForegroundWindow = Win32API.new("user32", "GetForegroundWindow", "V", "L") GetCurrentProcess = Win32API.new('kernel32', 'GetCurrentProcess', '', 'L') EnumProcessModules = Win32API.new('psapi', 'EnumProcessModules', 'lplp', 'L') GetModuleFileNameA = Win32API.new("kernel32", "GetModuleFileNameA", "lpi", "l") GetProcAddress = Win32API.new("kernel32", "GetProcAddress", "lp", "l") AnsiStringLength = Win32API.new("kernel32", "lstrlenA", "l", "l") class CPtr attr_accessor :address def initialize(address) @address = address end def [](ptr, size) return nil if address.nil? return nil if ptr.nil? buf = "\0" * size RtlMoveMemory_pl.call(buf, @address + ptr, size) buf end def []=(ptr, size, data) return nil if address.nil? return nil if ptr.nil? raise ArgumentError if data.length != size RtlMoveMemory_lp.call(@address + ptr, data, size) data end NIL = CPtr.new(nil) end def initialize_runtime @initialized ||= lambda { @rgss_module_path, @rgss_instance = search_for_rgss_module @rgss_module_identify = search_for_rgss_module_identify @rgss_version = { # "rgss100j" => [1, 0, :japanese], # no properly Win32API support "rgss102j" => [1, 2, :japanese], "rgss102e" => [1, 2, :english], "rgss103j" => [1, 3, :japanese], "rgss104j" => [1, 4, :japanese], "rgss104e" => [1, 4, :english], "rgss200e" => [2, 0, :english], "rgss200j" => [2, 0, :japanese], "rgss202e" => [2, 2, :english], "rgss202j" => [2, 2, :japanese], "rgss300" => [3, 0, nil], "rgss301" => [3, 1, nil], }[File.basename(@rgss_module_identify.downcase, ".dll")] @runtime_handle = search_for_runtime_handle true }.call end def rgss_version_major @rgss_version[0] rescue nil end def rgss_version_minor @rgss_version[1] rescue nil end def rgss_language @rgss_version[2] rescue nil end def search_for_rgss_module_identify dll = CPtr.new(rgss_instance) signature = dll[60, 4].unpack('L').first return if dll[signature, 2] != "PE" newhdr = dll[newhdrofs = signature+4, 20].unpack('SSLLLSS') optheader = dll[opthdrofs = newhdrofs+20, newhdr[5]].unpack('SCCLLLLLLLLLSSSSSSLLLLSSL*') exp = dll[*dll[opthdrofs+96, 8].unpack('LL')] export = exp.unpack('LLSSLLLLLLL') name = lambda{ |ptr| dll[ptr, AnsiStringLength.call(dll.address + ptr)] }.call(export[4]) end def search_for_rgss_module process = GetCurrentProcess.call num = ASCIICHAR * 4 EnumProcessModules.call process, nil, 0, num num = num.unpack('L')[0] x = ASCIICHAR * (num * 8) lnum = ASCIICHAR * 4 EnumProcessModules.call process, x, num, lnum x = x.unpack('L*') x.each do |xx| buf = ASCIICHAR * 1024 GetModuleFileNameA.call xx, buf, 1024 buf = buf.gsub(/\0.+$/){} return [buf, xx] if GetProcAddress.call(xx, "RGSSEval") != 0 end ['', 0] end def search_for_runtime_handle pattern = [] pattern[0] = LoadAccelerators.call(rgss_instance, rgss_version_major == 3 ? 0x65 : 0x66) pattern[1] = hwnd = player_window_handle pat = pattern.pack("LL") if rgss_version_major == 1 title = ASCIICHAR * 256 GetWindowTextA.call(hwnd, title, 256) else title = ASCIICHAR * 512 GetWindowTextW.call(hwnd, title, 512) end paddr = 0 mi = ASCIICHAR * 28 milen = mi.length buffer = ASCIICHAR * 4096 this = nil catch(:this) do while VirtualQuery.call(paddr, mi, milen) == milen i = Hash[*[:BaseAddress, :AllocationBase, :AllocationProtect, :RegionSize, :State, :Protect, :Type].zip(mi.unpack("LLLLLLL")).flatten] if i[:State] == 0x1000 && i[:Type] == 0x20000 && i[:Protect] == 0x04 cur = i[:BaseAddress] last = i[:BaseAddress] + i[:RegionSize] while cur < last size = [last - cur, 4096].min RtlMoveMemory_pl.call(buffer, cur, size) if (index = buffer.index(pat)) && index < size if (buffer.index(title) == index + 8) this = index + cur - 4 throw :this end end cur += buffer.length end end paddr = i[:BaseAddress] + i[:RegionSize] end end this end def player_window_handle @player_window_handle ||= lambda { threadID = GetCurrentThreadId.call hWnd = GetWindow.call(GetForegroundWindow.call, 0) while hWnd != 0 if threadID == GetWindowThreadProcessId.call(hWnd, 0) className = " " * 11 GetClassName.call(hWnd, className, 12) break if className == "RGSS Player" end hWnd = GetWindow.call(hWnd, 2) end hWnd }.call end def player_module_name @player_module_name ||= File.basename(player_module_path) end def player_module_path @player_module_path ||= lambda { buf = ASCIICHAR * 1024 GetModuleFileNameA.call 0, buf, 1024 buf = buf.gsub(/\0.+$/){} }.call end def rgss_module_name @rgss_module_name ||= File.basename(rgss_module_path) end def rgss_module_path @rgss_module_path end def rgss_instance @rgss_instance end def memory @memory ||= CPtr.new(0) end def smooth_mode_ptr @smooth_mode_ptr ||= lambda { return CPtr::NIL if @runtime_handle.nil? return CPtr::NIL if rgss_version_major != 1 CPtr.new(@runtime_handle + 348) }.call end def wait_for_vsync_ptr @wait_for_vsync_ptr ||= lambda { return CPtr::NIL if @runtime_handle.nil? if rgss_version_major == 1 return CPtr.new(memory[@runtime_handle + 308, 4].unpack("L").first + { 2 => 236, 3 => 236, 4 => 244 }[rgss_version_minor]) rescue CPtr::NIL elsif rgss_version_major == 2 || rgss_version_major == 3 return CPtr.new(@runtime_handle + 528) rescue CPtr::NIL end CPtr::NIL }.call end def smooth_mode smooth_mode_ptr[0, 4].unpack("L").first == 1 end def smooth_mode=(value) smooth_mode_ptr[0, 4] = [value ? 1 : 0].pack("L") end def wait_for_vsync wait_for_vsync_ptr[0, 4].unpack("L").first == 1 end def wait_for_vsync=(value) wait_for_vsync_ptr[0, 4] = [value ? 1 : 0].pack("L") end end RGSSRuntime.initialize_runtime end |