Proper extension folder structure with scrambled files


#1

Hi,

my plugin folder looks like this:

/SP/core.rb
/SP/foo.rb
/SP/...
SP_ext.rb

In SP_ext.rb I register my extension:

plugin = SketchupExtension.new('SP', File.expand_path('../SP/core.rb', __FILE__))
Sketchup.register_extension(plugin, true)

In SP/core.rb I load my other files and create everything my extension needs:

Sketchup.require("SP/foo")	
...

Now with Sketchup.require('file') it will load the correct file, but in the initializer of SketchupExtension I need to register a specific file. Do I need to make the distinction myself? Is the registration itself in the proper place or should this go into another file?

Is there supposed to be another way now that SU2016 encrypts some file itself?


#2

Quick question: why do you have the ../ prefix there? That makes it point to the parent directory.

SketchupExtension will internally call Sketchup.require on the file you provide it. So you’d have something like this:

plugin = SketchupExtension.new('SP', 'SP/core')
Sketchup.register_extension(plugin, true)

By the way, the correct naming convention would be for the support folder to have the same name as the registration file:

.../Example/core.rb
.../Example/foo.rb
.../Example/...
.../Example.rb

SketchupExtension itself doesn’t require this - but Extension Warehouse do. So does the new extension signing. It’s a structure required such that SketchUp can treat the files as a bundle.

Btw, beware of __FILE__ as under Windows there is a bug in Ruby that will label the incorrect encoding to the string returned. This easily cause errors with file paths with unicode characters. (Some users will have unicode usernames and hence in their user folder where SketchUp’s Plugins folder is located.)


#3

Quick question: why do you have the …/ prefix there? That makes it point to the parent directory.

File.expand_path('../Gemfile', __FILE__)
is a somewhat ugly Ruby idiom for getting the absolute path to a file when you know the path relative to the current file. Another way of writing it is this:

File.expand_path('Gemfile', File.dirname(__FILE__))

SketchupExtension will internally call Sketchup.require on the file you provide it. So you’d have something like this:

Cool, didn’t know this existed. Thanks

By the way, the correct naming convention would be for the support folder to have the same name as the registration file:

Ok, renamed it correctly.

Btw, beware of FILE as under Windows there is a bug in Ruby that will label the incorrect encoding to the string returned. This easily cause errors with file paths with unicode characters. (Some users will have unicode usernames and hence in their user folder where SketchUp’s Plugins folder is located.)

λ ruby C:\#123+1äü23`\test.rb
C:/#123+1"?23`/test.rb

Nice, didn’t know that…


#4

Let’s assume the file/folder structure is this…

/SP/...
/SP/core.rb
/SP/foo.rb
SP.rb

In SP.rb - the extension:

plugin = SketchupExtension.new(File.basename(__FILE__, ".*"), File,join(File.basename(__FILE__, ".*"), "core.rb")
# i.e. "SP/core.rb" ### you could add the whole path too...
Sketchup.register_extension(plugin, true) # 'SP'

In SP/core.rb you then “load” the other file[s] to create everything the extension needs:

Sketchup.require(File.join(File.dirname, "foo"))
# or better "foo.rb" to 'freeze' the loader and avoid hijacking with hacker's added .rbs earlier loading even after 'signing' in v2016.