forrestthewoods / fts_unity_native_plugin_reloader

Single-file helper to allow Unity NativePlugins to be updated without restarting the editor.
Other
106 stars 16 forks source link

Add support for OSX #4

Closed wildwinter closed 1 year ago

wildwinter commented 1 year ago

Hi there Forrest - thanks for your work on this!

I have very quickly put together support for Unity on OSX using this, using dlopen(), dlclose(), dlsym(), dlerr(). It seems to be working fine for me! Have been unable to test my changes to Windows, but it looks reasonably straightforward.

I first tried with [DllImport("libdl.dylib")] for dlsym() etc. but had crashes. Switched to [DllImport("__Internal")] and all seems good.

Main changes are to SystemLibrary. Wrapped LoadLibrary() in LoadLib() and then platform-specific code is inside that. Same for GetProcAddress() (now inside GetAddress()) and FreeLibrary() (now FreeLib). Moved error message handling inside those functions instead of checking externally.

forrestthewoods commented 1 year ago

Hi @wildwinter. Right now the code looks like this:

static class SystemLibrary {
    public static IntPtr LoadLib(string fileName) {
#if WINDOWS
        // Windows stuff
#else
        // mac stuff
  }

  public static Foo Blah(Bar bar) {
#if WINDOWS
        // Windows stuff
#else
        // mac stuff
  }
}

Can you change it to do look like this:

static class SystemLibrary {
#if Windows
    public static IntPtr LoadLib(string fileName) { /* omitted */ }
    public static Foo Blah(Bar bar) { /* omitted */ }

# else if macOS
    public static IntPtr LoadLib(string fileName) { /* omitted */ }
    public static Foo Blah(Bar bar) { /* omitted */ }

#else
    #error unsupported platform
#endif
}

I think this version scales better. Eventually I'd like to support Linux, Android, etc. And it's a lot easier to read if each platform has its own "block" instead of each function being mixed.

Otherwise looks good and this is super helpful!