kaitai-io / kaitai_struct_visualizer

Kaitai Struct: visualizer and hex viewer tool
https://rubygems.org/gems/kaitai-struct-visualizer
GNU General Public License v3.0
280 stars 25 forks source link

Use Fiddle in console-windows.rb instead of the deprecated Win32API #49

Closed humdogm closed 2 years ago

humdogm commented 2 years ago

Win32API, which was originally part of the Ruby standard library, was slated for removal in version 2.0, according to the developer of win32-api. win32-api is a drop-in replacement for the Win32API library. The only major differences are putting the dll name as the last argument, and it requires capital letters for prototype and return types.

The developer states that:

As of win32-api 1.4.8 a binary gem is shipped that contains binaries for both Ruby 1.8, Ruby 1.9, and 2.x.
For Ruby 2.x, both 32 and 64 bit binaries are included as of release 1.5.0.
The file under lib/win32 dynamically requires the correct binary based on your version of Ruby.

So, there is not a problem supporting multiple versions of Ruby with this change.

I have tested this on Ruby 3.0.2 on Windows 10.0.19043 in Command Prompt and Powershell v5.1.19041.1237

humdogm commented 2 years ago

Fixes #48

generalmimon commented 2 years ago

Doesn't this require adding win32-api as a dependency in .gemspec, like this:

https://github.com/kaitai-io/kaitai_struct_visualizer/blob/5b3ec3dbaa6f3cbc9c34c0e51d92d443c3861e71/kaitai-struct-visualizer.gemspec#L36

...?

humdogm commented 2 years ago

Doesn't this require adding win32-api as a dependency in .gemspec

Yes sorry, here is the commit for that as well.

humdogm commented 2 years ago

While fixing things for Ruby 3.0 looks like a good idea, we can't afford to break things for other OSes. Can we somehow scope win32-api to Windows only?

From doing some reading on gemspecs, it looks like You'll want to create platform-specific gems for your gem that has different dependencies according to this rubygems github issue and uses this part of the gem specification. I'm not very familiar with ruby, would that entail creating a new repository or could that be done just at build time?

generalmimon commented 2 years ago

I'd be all for releasing a new version of kaitai-struct-visualizer, but I really wanted to resolve the Ruby 3 incompatibility on Windows. I have been eyeing this PR, but it uses https://github.com/cosmo0920/win32-api whose author strongly encourages to migrate away from and "use FFI" instead. There is also win32api from Ruby lang developers, which is deprecated as well, but at least it gives a hint what to do (https://github.com/ruby/win32api/blob/f020cca/lib/Win32API.rb#L5):

warn "Win32API is deprecated after Ruby 1.9.1; use fiddle directly instead", uplevel: 2

So https://github.com/ruby/fiddle is apparently the way to go. This Gist compares how to call a Win32 function using both Fiddle and Win32API (which we're using right now), so we can see how to migrate: https://gist.github.com/Iristyle/db78afd90bf59c28d3cc1741241bde56

I don't even think we'll still need to roll out platform-specific gems with Fiddle, because it's even supposedly in the standard library since 1.9 (source). Also: https://ruby-doc.org/stdlib-3.1.2/libdoc/fiddle/rdoc/Fiddle.html

humdogm commented 2 years ago

Is that the kind of thing you are looking for? If I had know about Fiddle at the time I made my PR I would have used that instead.

generalmimon commented 2 years ago

@humdogm Thanks again! 🥇 🚀 🎉