Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading ruby gems #118

Open
enumag opened this issue Nov 2, 2023 · 8 comments
Open

Loading ruby gems #118

enumag opened this issue Nov 2, 2023 · 8 comments

Comments

@enumag
Copy link

enumag commented Nov 2, 2023

I wanted to use rubyzip in my game so I was looking for how to load gems.

The results written here are a result of brainstorming with Nathan and Splendide Imaginarius in Maple Shrine Discord - big thanks to both!

Third party gems

As long as it's a pure ruby gem (written in just ruby without C) they can be loaded by adding this into mkxp.json:

    "rubyLoadpath": [
        "gems/"
    ],

And copying the gem's ruby files (typically the content's of lib directory) into this gems/ directory which should be next to the mkxp.json.

Standard library

This however didn't work on it's own for rubyzip because it requires some standard ruby libraries to work. Fortunately mkxp ships with stdlib directory. According to current documentation it can be loaded like this:

$:.push(File.join(Dir.pwd, "stdlib")) unless System.is_mac?

For some reason it's apparently built in on mac so this step is unnecessary. (Disclaimer: I didn't test things on mac yet so I can't confirm it for now.)

However from testing this is essentially equivalent to just adding the stdlib directory to rubyLoadPath:

    "rubyLoadpath": [
        "stdlib/",
        "gems/"
    ],

There is one thing worth noting: the stdlib directory is intentionally different for windows and linux meaning you can't simply use the same one for all platforms. Ideally use the correct one for windows and linux and omit it altogether on mac.

rbconfig

This however still wasn't enough for rubyzip because it uses rbconfig which is platform dependent. We fixed it by changing mkxp.json like this:

    "rubyLoadpath": [
        "stdlib/",
        "stdlib/x64-mingw32",
        "gems/"
    ],

On linux it should instead be like this:

    "rubyLoadpath": [
        "stdlib/",
        "stdlib/x64_64-linux",
        "gems/"
    ],

Yes, this means that mkxp.json needs to be different for each platform as well.

With that rubyzip finally worked for me. I'll post further info about linux and mac later.

One caveat I should note that when I made a typo in mkxp.json the gems simply weren't loaded but there was no error about mkxp.json being invalid (#116).

@enumag
Copy link
Author

enumag commented Nov 7, 2023

Mac has the same issue regarding require 'rbconfig' and again some workaround is needed - adding the load path somehow, moving the rbconfig file to a moveable location or something like that. This should be fixed. Where do the stdlib files come from that the rbconfig gem is broken like this?

@enumag
Copy link
Author

enumag commented Nov 8, 2023

In the end I ended up using this to fix the loading issues:

  $:.push('stdlib') unless System.platform[/macOS/]
  $:.push('stdlib/x64-mingw32') if System.platform[/Windows/]
  $:.push('stdlib/x86_64-linux') if System.platform[/Linux/]
  $:.push('../Resources/Ruby/3.1.0/x86_64-darwin') if System.platform[/macOS/]
  $:.push('gems')

@enumag
Copy link
Author

enumag commented Nov 14, 2023

There is also an issue with using https with net/http but it's not difficult to fix.

On macOS there is no issue and it already works with current setup.

For Linux you just need to remove openssl from --with-out-ext in linux/Makefile and it will work as well.

Windows is the most complicated. Again you need to remove openssl from --with-out-ext in windows/Makefile but this time it isn't enough because you'll be missing libcrypto and libssl dlls. To build them you need to remove the no-shared \ line from openssl configuration in windows/Makefile. With this the dlls will appear in the bin directory during the build process and can be copied over to the game directory. I'm building everything on GitHub Actions so it was just a question of adding

          cp ../../windows/build-mingw64/bin/libcrypto-3-x64.dll .
          cp ../../windows/build-mingw64/bin/libssl-3-x64.dll .

right after

          cp ../../windows/build-mingw64/bin/x64-msvcrt-ruby310.dll .

Commit: enumag@54c7704

On all platforms there is the additional issue that you need either disable verification (not recommended) or provide a own ca_file to use for the verification. You can get it for instance from https://curl.se/docs/caextract.html.

Code example:

      require 'net/http'
      uri = URI('https://...')
      Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :ca_file => 'cacert.pem') do |http|
        http.request(Net::HTTP::Get.new(uri)) do |response|
          # ...
        end
      end

@Nathan-MV
Copy link

Nathan-MV commented Nov 15, 2023

I was having some problems on Linux because it needs rbconfig.rb which is inside x64-mingw32, so this worked better for me

$LOAD_PATH.unshift('lib/stdlib', 'lib/stdlib/x64-mingw32') if System.is_windows? || System.is_linux?
$LOAD_PATH << '../Resources/Ruby/3.2.0/x86_64-darwin' if System.is_mac?
$LOAD_PATH << 'lib/gems'

@enumag
Copy link
Author

enumag commented Nov 15, 2023

@Nathan-MV That doesn't make sense, why would you have rbconfig in x64-mingw32 on linux? On linux it should be in x86_64-linux.

@Nathan-MV
Copy link

Nathan-MV commented Nov 15, 2023

@Nathan-MV That doesn't make sense, why would you have rbconfig in x64-mingw32 on linux? On linux it should be in x86_64-linux.

I'm using Ruby 3.2.2, there's no such a thing as x86_64-linux in 3.2.2 stdlib
Nathan-MV@bec94fb

@enumag
Copy link
Author

enumag commented Nov 15, 2023

I'm using Ruby 3.2.2, there's no such a thing as x86_64-linux in 3.2.2 stdlib Nathan-MV@bec94fb

That sounds very suspicious. Mingw is a windows tool so a directory called x64-mingw32 should not exist in linux stdlib. If it does it's most likely a bug and the contents of rbconfig.rb will likely be incorrect. Do you have a build with ruby 3.2 on your github actions?

@Nathan-MV
Copy link

Wait for @Splendide-Imaginarius to start the actions of this PR #131

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants