Skip to content

Instantly share code, notes, and snippets.

@hyrious hyrious/s_api.rb
Last active Mar 1, 2019

Embed
What would you like to do?
smaller api.rb
def api(f)
a=($api||={})[f];return a if a;lambda{|*a|%w[kernel32 user32 msvcrt].each{|d|break($api[f]=Win32API.new(d,f,a.map{'p'},'L')).call(*a)rescue next}}
end
def buf(t=())
t ?$buf=[].pack("x#{($buf_t=t).scan(/(\w)(\d+)?/).inject(0){|s,(c,i)|s+('axZ'.include?(c)?1:2**('CSLQ'.index c.upcase))*(i||1).to_i}}"):$buf.unpack($buf_t)
end
# api('GetCursorPos').call(buf('L2'))
# msgbox_p buf
# 'A256' => 'hello, world!'
# 'a256' => "hello, world\0\0\0..."
# 'Z256' => 'hello, world!\0'
# 'x256' => "\0\0\0..."
api('GetModuleFileName').call(0, buf('A256'), 256)
RGSS_PATH = buf[0]
RGSS_INI = "./#{File.basename RGSS_PATH, '.exe'}.ini"
api('GetPrivateProfileString').call('Game', 'Library', 0, buf('A64'), 64, RGSS_INI)
RGSS_DLL = buf[0]
RGSS_VERSION = RGSS_DLL[/\d+/].scan(/./).join('.') unless defined? RGSS_VERSION
api('AllocConsole').call
api('SetConsoleTitle').call('RGSS Console')
def enable_ansi_color
hout = api('GetStdHandle').call(-12)
api('GetConsoleMode').call(hout, buf('L'))
api('SetConsoleMode').call(hout, buf[0] | 4)
end
enable_ansi_color
def print(*a)
a.each do |s|
m2w = api('MultiByteToWideChar')
l = m2w.call(65001, 0, s = "#{s}", -1, 0, 0)
m2w.call(65001, 0, s, -1, buf("a#{l * 2}"), l)
w2m = api('WideCharToMultiByte')
l = w2m.call(0, 0, s = buf[0], -1, 0, 0, 0, 0)
w2m.call(0, 0, s, -1, buf("A#{l}"), l, 0, 0)
l = api('lstrlen').call(s = buf[0])
api('WriteConsole').call(api('GetStdHandle').call(-12), s, l, 0, 0)
end
end
def puts(*a)
print *a.map { |e| (s = "#{e}")[-1] == "\n"[0] ? s : "#{s}\n" }
end
def p(*a)
puts *a.map { |e| e.inspect }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.