Seiran API v1.7 beta 0.04 for RGSS
一种新的调用API的包装,因为API不包装也可以调用,其实就是自己的脚本综合练习一类的东西吧:
用法:
鼠标指针:
1 2 3 4 5 6 7 8 | def mouse_pos api{ import 'user32' # 点是两个uint, 传入的int[2].*是两个uint的指针 return GetCursorPos(uint[2].*).out[0].values #传出参数的第0个是这个指针,values取得他的内容(两个点) } end |
自定义结构体的鼠标指针:
1 2 3 4 5 6 7 8 9 10 | def point(x = int(0), y = int(0)) tuple(x, y) end def mouse_pos api{ import 'user32' return GetCursorPos(*point).out[0].values } end |
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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | Win32API rescue require 'win32api' #API 1.7 # http://seiran.mist.so class API class CPtr RMM = Win32API.new("Kernel32", "RtlMoveMemory", "LLL", "L") SLEN = Win32API.new("Kernel32", "lstrlenA", "L", "L") WLEN = Win32API.new("Kernel32", "lstrlenW", "L", "L") def initialize(start, len = 0) @start, @len = start, len end def [](st, le) buf = ""*le RMM.call *[buf, @start+st, le].pack("pLL").unpack("L*") buf end def +(a) CPtr.new(@start+a, @len) end def []=(st, le, str) RMM.call *[@start+st, str, [le,str.size].min].pack("LpL").unpack("L*") end def to_s(size = nil) a = SLEN.call(to_i) size ||= a self[0, [size, a].min] end def to_ws(size = nil) a = WLEN.call(to_i) size ||= a self[0, [size, a].min * 2] end def to_i @start end end module ArrayType def [](size) API::Tuple.new([self] * size) end end module PointerType def *(*a) API::Pointer.new(self) end def to_ary(*a) [API::Pointer.new(self)] end end class Tuple include ArrayType include PointerType def initialize(args) @args = args end def pack @objects = [] @mark = [] @valuestr = "" pos = 0 @args.each{|x| case x when Integer then @valuestr << [x].pack("L"); pos += 4 when String then @valuestr << [x].pack("p"); @mark << x; pos += 4 else @objects << [x, pos]; @valuestr << (u = x.pack); pos += u.size end } @valuestr end def unpack(ptr) out = [] @objects.each{|x| a, b = x; out << a.unpack(ptr + b); } out end end class SingleType include ArrayType include PointerType def initialize(a = 0, packchar = "L", size = 4); @a = a; @p = packchar; @size = size; @buf = [@a].pack(packchar);end def pack; @buf; end def unpack(ptr); ptr[0, @size].unpack(@p).first; end end def uint(a = 0) SingleType.new(a) end def int(a = 0) SingleType.new(a, "l") end alias ulong uint alias long int alias uint32 uint alias int32 int def char(a = 0) SingleType.new(a, "c") end def byte(a = 0) SingleType.new(a, "C") end alias uchar byte alias u8 byte alias uint8 byte alias int8 char def short(a = 0) SingleType.new(a, "s") end def ushort(a = 0) SingleType.new(a, "S") end alias uint16 ushort alias int16 short def longlong(a = 0) SingleType.new(a, "q", 8) end def ulonglong(a = 0) SingleType.new(a, "Q", 8) end alias uint64 ulonglong alias int64 longlong def float(v) SingleType.new(v, "F", 4) end def double(v) SingleType.new(v, "D", 8) end alias real float alias real32 float alias real64 double def packed_char(v) SingleType.new(v, "c", 1) end def packed_short(v) SingleType.new(v, "s", 2) end class PointerToTuple def initialize(tuple, ptr) @tuple, @ptr = tuple, ptr end def value @tuple.unpack(@ptr) end end class Pointer include ArrayType include PointerType def initialize(obj = API::Tuple.new([])); @obj = obj; @obj = API::Tuple.new([@obj]) unless Tuple === @obj;@str = @obj.pack; @ptr = [@str].pack("p"); end def pack; @ptr; end def unpack(ptr); PointerToTuple.new(@obj, API::CPtr.new(ptr[0, 4].unpack("L").first)) end end class BufferAnsiString include ArrayType include PointerType def initialize(size = nil, &b) r = if b then b.call else nil end @size = size || r.size @buf = r || ""*@size end def pack @buf end def unpack(ptr) ptr.to_s(@size) end end class BufferWideString include ArrayType include PointerType def initialize(size = nil, &b) r = if b then b.call else nil end @size = (size ? size * 2 : nil)|| r.size @buf = r || ""*@size end def pack @buf end def unpack(ptr) ptr.to_ws(@size) end end class Buffer include ArrayType include PointerType def initialize(size) @size = size @buf = ""*@size end def pack @buf end def unpack(ptr) ptr[0, @size] end end def pointer(obj = tuple()) Pointer.new(obj) end def buffer(size) Buffer.new(size) end def string(size = nil, &b) BufferAnsiString.new(size, &b) end def wstring(size = nil, &b) BufferWideString.new(size, &b) end def array(size, typesym) tuple(*Array.new(size).map{send typesym}) end attr_accessor :caller class ResultType def initialize(ret, out) @ret, @out = ret, out end def to_int @ret.to_int end def to_i @ret.to_i end def coerce(v) case v when Fixnum; [v, to_int] when String; [v, to_str] end end def method_missing(sym, *args) @ret.send sym, *args end def to_str @ret.to_str end def to_s @ret.to_s end def value @ret end def out @out end def first @out.first end def last @out.last end def [](a) @out[a] end end class Caller attr_accessor :imports def initialize self.imports = [] @api = API.new end def guess(sym, num) params = "L"*num vsyms = ["_#{sym}", "#{sym}", "_#{sym}@#{num}", "#{sym}@#{num}"] self.imports.map{|x| vsyms.map{|y| begin Win32API.new(x, y, params, "L") rescue LoadError # Ruby >= 1.9 nil rescue RuntimeError # RMXP nil end } }.flatten.compact end def call_win32api(sym, *args) t = API::Tuple.new(args) r = t.pack v = guess(sym, r.size / 4) ret = v[0].call *r.unpack("L"*args.size) out = t.unpack(API::CPtr.new([r].pack("p").unpack("L").first)) ResultType.new(ret, out) end def asm_push(a) @api.tuple(@api.packed_char(0x68), a) end def asm_addesp(a) @api.tuple(@api.packed_short(0xc483), a) end def asm_func_begin @api.tuple(@api.packed_short(0x8b55), @api.packed_char(0xec)) end def asm_func_end @api.int(0x0010c2c9) end def asm_call(a) @api.tuple( @api.packed_char(0xb8), @api.int(a), @api.packed_short(0xd0ff) ) end def call_stdcall(addr, *args) t = [asm_func_begin] t.concat args.reverse.map{|x| asm_push(x)} t.concat [asm_call(addr)] t.concat [asm_func_end] api{ import 'user32' return CallWindowProc pointer(tuple(*t)), 0, 0, 0, 0 } end def call_cdecl(addr, *args) t = [asm_func_begin] t.concat args.reverse.map{|x| asm_push(x)} t.concat [asm_call(addr)] t.concat [asm_addesp(@api.char(args.size * 4))] t.concat [asm_func_end] api{ import 'user32' return CallWindowProc pointer(tuple(*t)), 0, 0, 0, 0 } end end def setup self.caller = Caller.new end def import(name) self.caller.imports << name unless self.caller.imports.include?(name) self end def tuple(*args) Tuple.new(args) end def method_missing(sym, *args) self.caller.call_win32api(sym, *args) end def stdcall(addr, *args) self.caller.call_stdcall(addr, *args) end def ccall(addr, *args) self.caller.call_cdecl(addr, *args) end end def api(&b) x = API.new x.setup x.instance_eval(&b) if b x end |