Module: U3d::Utils

Defined in:
lib/u3d/utils.rb

Overview

Several different utility methods

Constant Summary collapse

CSIDL_LOCAL_APPDATA =

Regex to capture each part of a version string (0.0.0x0)

0x001c
UNITY_VERSION_REGEX =
/(\d+)(?:\.(\d+)(?:\.(\d+))?)?(?:(\w)(?:(\d+))?)?/

Class Method Summary collapse

Class Method Details

.ensure_dir(dir) ⇒ Object



71
72
73
# File 'lib/u3d/utils.rb', line 71

def ensure_dir(dir)
  FileUtils.mkpath(dir) unless File.directory?(dir)
end

.get_ssl(url, redirect_limit: 10) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/u3d/utils.rb', line 36

def get_ssl(url, redirect_limit: 10)
  raise 'Too many redirections' if redirect_limit.zero?
  response = nil
  request = nil
  uri = URI(url)
  begin
    Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
      request = Net::HTTP::Get.new uri
      response = http.request request
    end
  rescue OpenSSL::OpenSSLError => ssl_error
    UI.error 'SSL has faced an error, you may want to check our README to fix it'
    raise ssl_error
  end

  case response
  when Net::HTTPSuccess then
    response.body
  when Net::HTTPRedirection then
    UI.verbose "Redirected to #{response['location']}"
    get_ssl(response['location'], redirect_limit: redirect_limit - 1)
  else raise "Request failed with status #{response.code}"
  end
end

.hashfile(file_path, blocksize: 65_536) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
# File 'lib/u3d/utils.rb', line 61

def hashfile(file_path, blocksize: 65_536)
  require 'digest'
  raise ArgumentError, 'Not a file' unless File.file?(file_path)
  md5 = Digest::MD5.new
  File.open(file_path, 'r') do |f|
    md5 << f.read(blocksize) until f.eof?
  end
  md5.hexdigest
end

.parse_unity_version(version) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/u3d/utils.rb', line 92

def parse_unity_version(version)
  ver = UNITY_VERSION_REGEX.match(version)
  if ver.nil?
    raise ArgumentError, "Version (#{version}) does not match the Unity "\
    'version format 0.0.0x0'
  end
  [ver[1], ver[2], ver[3], ver[4], ver[5]]
end

.pretty_filesize(filesize) ⇒ Object



116
117
118
# File 'lib/u3d/utils.rb', line 116

def pretty_filesize(filesize)
  Filesize.from(filesize.round.to_s + ' B').pretty
end


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/u3d/utils.rb', line 75

def print_progress(current, total, started_at)
  ratio = [current.to_f / total, 1.0].min
  percent = (ratio * 100.0).round(1)
  arrow = (ratio * 20.0).floor
  time_spent = Time.now.to_i - started_at
  print("\r[")
  print('=' * [arrow - 1, 0].max)
  print('>')
  print('.' * (20 - arrow))
  print("] #{pretty_filesize(current)}/#{pretty_filesize(total)} (#{percent}% at #{pretty_filesize(current.to_f / time_spent)}/s)     ")
end


87
88
89
90
# File 'lib/u3d/utils.rb', line 87

def print_progress_nosize(current, started_at)
  time_spent = Time.now.to_i - started_at
  print("\r>#{pretty_filesize(current)} downloaded at #{pretty_filesize(current.to_f / time_spent)}/s)    ")
end

.windows_local_appdataObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/u3d/utils.rb', line 101

def windows_local_appdata
  require 'win32api'

  windir = ' ' * 261

  getdir = Win32API.new('shell32', 'SHGetFolderPath', 'LLLLP', 'L')
  result = getdir.call(0, CSIDL_LOCAL_APPDATA, 0, 0, windir)
  raise "Unable to get Local Appdata directory, returned with value #{result}" unless result.zero?
  windir.rstrip!
  windir = File.expand_path(windir.rstrip)

  return windir if Dir.exist? windir
  raise "Local Appdata retrieved (#{windir}) is not correct"
end