Skip to content

Instantly share code, notes, and snippets.

@shkhln

shkhln/w4

Last active Nov 16, 2020
Embed
What would you like to do?
#!/usr/bin/env ruby
# encoding: UTF-8
# DIY WoW64 Wine convenience script
#
# Usage:
# % sudo pkg install wine winetricks
# % w4 pkg install wine mesa-dri
# % setenv WINEPREFIX ~/.wine-steam # export WINEPREFIX=~/.wine-steam
# % setenv WINEDLLOVERRIDES "gameoverlayrenderer64=" # export WINEDLLOVERRIDES="gameoverlayrenderer64="
# % w4 winetricks vd=1920x1080 steam
# % w4 wine $WINEPREFIX/drive_c/Program\ Files\ \(x86\)/Steam/Steam.exe -no-cef-sandbox
require 'fileutils'
raise 'Don\'t run this script as root!' if `id -u`.chomp == '0'
I386_PKG_ROOT = ENV['W4_i386_PKG_ROOT'] || File.join(ENV['HOME'], '.i386-wine-pkg')
WINE_PKG_PREFIX = ENV['W4_WINE_PKG_PREFIX'] || '/usr/local'
def common_pkg_args
abi = `pkg config ABI`.chomp
raise if not abi =~ /:amd64$/
[
'--option', "ABI=#{abi.gsub('amd64', 'i386')}",
'--option', 'INSTALL_AS_USER=true',
'--option', 'RUN_SCRIPTS=false',
'--rootdir', I386_PKG_ROOT
]
end
def print_and_execute(*args)
STDERR.puts args.map{|arg| arg.include?(' ') ? "'#{arg}'" : arg}.join(' ')
system([args[0]] * 2, *args[1..-1])
end
def move_if_exists(src, dest)
FileUtils.mv(src, dest) if File.exist?(src)
end
def check_wine_versions
wine32_version=`wine --version`.chomp
begin
wine64_version=`wine64 --version`.chomp
rescue Errno::ENOENT
raise "`wine64` is required; try `sudo pkg install wine`"
end
if wine32_version != wine64_version
raise "`wine` [#{wine32_version}] and `wine64` [#{wine64_version}] versions should match each other"
end
end
def exec_with_wine_env(args, opts)
def wine32(path)
File.join(I386_PKG_ROOT, WINE_PKG_PREFIX, path)
end
def wine64(path)
File.join(WINE_PKG_PREFIX, path)
end
raise "`wine` is required; try `#{$PROGRAM_NAME} pkg install wine`" if not File.exist?(wine32('bin/wine'))
ENV['PATH'] = [wine64('bin'), ENV['PATH'], wine32('bin')].compact.join(':')
# just in case
raise if `which wine`.chomp != wine32('bin/wine')
raise if `which wine64`.chomp != wine64('bin/wine64')
move_if_exists(wine32('bin/wineserver'), wine32('bin/wineserver32'))
if opts[:wow64]
check_wine_versions
ENV['WINESERVER'] = wine64('bin/wineserver')
else
ENV['WINESERVER'] = wine32('bin/wineserver32')
end
ENV['LIBGL_DRIVERS_PATH'] = [
ENV['LIBGL_DRIVERS_PATH'],
'/usr/local/lib/dri',
'/usr/local/lib32/dri',
File.join(I386_PKG_ROOT, 'usr/local/lib/dri')
].compact.join(':')
ENV['LD_32_LIBRARY_PATH'] = [
ENV['LD_32_LIBRARY_PATH'],
wine32('lib/wine'),
'/usr/local/lib32',
File.join(I386_PKG_ROOT, 'usr/local/lib')
].compact.join(':')
ENV['LD_32_LIBRARY_PATH_RPATH'] = 'y'
exec([args[0]] * 2, *args[1..-1])
end
action, *args = ARGV
case action
when 'pkg'
FileUtils.mkdir_p(I386_PKG_ROOT)
print_and_execute('pkg', *common_pkg_args, *args)
exit($?.exitstatus)
when /^(wine|w)(32|)$/
exec_with_wine_env(['wine', *args], :wow64 => $2 != '32')
when /^(winecfg|regedit)(32|)$/
exec_with_wine_env(['wine', $1, *args], :wow64 => $2 != '32')
when /^winetricks(32|)$/
exec_with_wine_env(['winetricks', *args], :wow64 => $1 != '32')
when /^x(32|)$/
exec_with_wine_env(args, :wow64 => $1 != '32')
else
puts "#{$PROGRAM_NAME} pkg | wine[32] | w[32] | winecfg[32] | regedit[32] | winetricks[32] | x[32]"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment