Module: Wine

Defined in:
lib/wine.rb

Overview

Note:

This module requires a valid Wine binary and prefix to function correctly.

Module for handling Wine registry operations

Constant Summary collapse

BIN =

The path to the Wine binary

$wine_bin
PREFIX =

The path to the Wine prefix

$wine_prefix

Class Method Summary collapse

Class Method Details

.registry_gets(key) ⇒ String?

Retrieves a value from the Wine registry.

Examples:

value = Wine.registry_gets('HKEY_CURRENT_USER\\Software\\MyApp\\Setting')

Parameters:

  • key (String)

    The registry key to retrieve the value from.

Returns:

  • (String, nil)

    The value associated with the key, or nil if not found.

Raises:

  • (StandardError)

    If there is an issue reading the registry file.



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
# File 'lib/wine.rb', line 44

def Wine.registry_gets(key)
  hkey, subkey, thingie = /(HKEY_LOCAL_MACHINE|HKEY_CURRENT_USER)\\(.+)\\([^\\]*)/.match(key).captures # fixme: stupid highlights ]/
  if File.exist?(PREFIX + '/system.reg')
    if hkey == 'HKEY_LOCAL_MACHINE'
      subkey = "[#{subkey.gsub('\\', '\\\\\\')}]"
      if thingie.nil? or thingie.empty?
        thingie = '@'
      else
        thingie = "\"#{thingie}\""
      end
      lookin = result = false
      File.open(PREFIX + '/system.reg') { |f| f.readlines }.each { |line|
        if line[0...subkey.length] == subkey
          lookin = true
        elsif line =~ /^\[/
          lookin = false
        elsif lookin and line =~ /^#{thingie}="(.*)"$/i
          result = $1.split('\\"').join('"').split('\\\\').join('\\').sub(/\\0$/, '')
          break
        end
      }
      return result
    else
      return false
    end
  else
    return false
  end
end

.registry_puts(key, value) ⇒ Boolean

Writes a value to the Wine registry.

Examples:

success = Wine.registry_puts('HKEY_CURRENT_USER\\Software\\MyApp\\Setting', 'MyValue')

Parameters:

  • key (String)

    The registry key to write the value to.

  • value (String)

    The value to be written to the registry.

Returns:

  • (Boolean)

    True if the operation was successful, false otherwise.

Raises:

  • (StandardError)

    If there is an issue writing to the registry.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wine.rb', line 82

def Wine.registry_puts(key, value)
  hkey, subkey, thingie = /(HKEY_LOCAL_MACHINE|HKEY_CURRENT_USER)\\(.+)\\([^\\]*)/.match(key).captures # fixme ]/
  if File.exist?(PREFIX)
    if thingie.nil? or thingie.empty?
      thingie = '@'
    else
      thingie = "\"#{thingie}\""
    end
    # gsub sucks for this..
    value = value.split('\\').join('\\\\')
    value = value.split('"').join('\"')
    begin
      regedit_data = "REGEDIT4\n\n[#{hkey}\\#{subkey}]\n#{thingie}=\"#{value}\"\n\n"
      filename = "#{TEMP_DIR}/wine-#{Time.now.to_i}.reg"
      File.open(filename, 'w') { |f| f.write(regedit_data) }
      system("#{BIN} regedit #{filename}")
      sleep 0.2
      File.delete(filename)
    rescue
      return false
    end
    return true
  end
end