Class: Processing::LibraryLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-processing/library_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLibraryLoader

Returns a new instance of LibraryLoader.



5
6
7
8
# File 'lib/ruby-processing/library_loader.rb', line 5

def initialize
  @sketchbook_library_path = File.join(find_sketchbook_path || "", "libraries")
  @loaded_libraries = Hash.new(false)
end

Instance Attribute Details

#sketchbook_library_pathObject (readonly)

Returns the value of attribute sketchbook_library_path.



3
4
5
# File 'lib/ruby-processing/library_loader.rb', line 3

def sketchbook_library_path
  @sketchbook_library_path
end

Instance Method Details

#get_platform_specific_library_paths(basename) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby-processing/library_loader.rb', line 85

def get_platform_specific_library_paths(basename)
  bits = "32"
  if java.lang.System.getProperty("sun.arch.data.model") == "64" || 
     java.lang.System.getProperty("java.vm.name").index("64")
    bits = "64"
  end

  match_string, platform = {"Mac" => "macosx", "Linux" => "linux", "Windows" => "windows" }.detect do |string, platform_|
    java.lang.System.getProperty("os.name").index(string)
  end
  platform ||= "other"
  [ platform, platform+bits ].collect { |p| File.join(basename, p) }
end

#library_loaded?(library_name) ⇒ Boolean

Detect if a library has been loaded (for conditional loading)

Returns:

  • (Boolean)


11
12
13
# File 'lib/ruby-processing/library_loader.rb', line 11

def library_loaded?(library_name)
  @loaded_libraries[library_name.to_sym]
end

#load_java_library(library_name) ⇒ Object

For pure java libraries, such as the ones that are available on this page: processing.org/reference/libraries/index.html

P.S. – Loading libraries which include native code needs to hack the Java ClassLoader, so that you don’t have to futz with your PATH. But it’s probably bad juju.



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
# File 'lib/ruby-processing/library_loader.rb', line 54

def load_java_library(library_name)
  library_name = library_name.to_sym
  return true if @loaded_libraries[library_name]
  if Processing.online?
    return @loaded_libraries[library_name] = !!(JRUBY_APPLET.get_parameter("archive").match(%r(#{library_name}))) 
  end
  path = get_library_path(library_name, "jar")
  jars = Dir["#{path}/*.jar"]
  return false if jars.empty?
  jars.each {|jar| require jar }

  platform_specific_library_paths = get_platform_specific_library_paths(path)
  platform_specific_library_paths = platform_specific_library_paths.select do |path|
    test(?d, path) && !Dir.glob(File.join(path, "*.{so,dll,jnilib}")).empty?
  end

  if !platform_specific_library_paths.empty?
    platform_specific_library_paths << java.lang.System.getProperty("java.library.path")
    new_library_path = platform_specific_library_paths.join(java.io.File.pathSeparator)

    java.lang.System.setProperty("java.library.path", new_library_path)

    field = java.lang.Class.for_name("java.lang.ClassLoader").get_declared_field("sys_paths")
    if field
      field.accessible = true
      field.set(java.lang.Class.for_name("java.lang.System").get_class_loader, nil)
    end
  end
  return @loaded_libraries[library_name] = true
end

#load_libraries(*args) ⇒ Object Also known as: load_library

Load a list of Ruby or Java libraries (in that order) Usage: load_libraries :opengl, :boids

If a library is put into a ‘library’ folder next to the sketch it will be used instead of the library that ships with Ruby-Processing.



20
21
22
23
24
25
# File 'lib/ruby-processing/library_loader.rb', line 20

def load_libraries(*args)
  args.each do |lib|
    loaded = load_ruby_library(lib) || load_java_library(lib)
    raise LoadError.new "no such file to load -- #{lib}" if !loaded
  end
end

#load_ruby_library(library_name) ⇒ Object

For pure ruby libraries. The library should have an initialization ruby file of the same name as the library folder.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby-processing/library_loader.rb', line 31

def load_ruby_library(library_name)
  library_name = library_name.to_sym
  return true if @loaded_libraries[library_name]
  if Processing.online?
    begin
      return @loaded_libraries[library_name] = (require "library/#{library_name}/#{library_name}")
    rescue LoadError => e
      return false
    end
  else
    path = get_library_path(library_name, "rb")
    return false unless path
    return @loaded_libraries[library_name] = (require "#{path}/#{library_name}")
  end
end