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

add support for linux #10

Merged
merged 1 commit into from
Oct 14, 2024
Merged

add support for linux #10

merged 1 commit into from
Oct 14, 2024

Conversation

clouds56
Copy link

@clouds56 clouds56 commented Sep 25, 2024

By using https://github.com/McSimp/linoodle, we could load oo2core_8_win64.dll on linux

@@ -8,11 +8,88 @@ namespace SoulsFormats
public static class Kernel32 {
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr LoadLibraryW([MarshalAs(UnmanagedType.LPWStr)]string lpFileName);

[DllImport("kernel32.dll")]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this still have the ".dll" ending, given that it was removed from the other instances of DllImport?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's no kernel32.dll in linux, this line is just fine.
Actually, we use public static class Libdl in linux instead.

I'm not sure if netstandard could use the latest NativeLibrary.Load and/or LibraryImport, if so, we could simplify some code here.

@ividyon
Copy link

ividyon commented Sep 25, 2024

Looks interesting. We've kept compatibility to netstandard and netcore for the sake of having cross-platform capability, and this seems to move us closer to that goal.

Have you personally tested it? How would you suggest to test it?

@clouds56
Copy link
Author

Yes, I've test on my wsl ubuntu.
But it's tricky for dlopen to find library.
This relies on linoodle library, I suggest use my fork (https://github.com/clouds56-contrib/linoodle/tree/path), or wait McSimp/linoodle#6 to check in. (to summarize, my fork add necessary API that SoulsFormats relies on, could find library in more path, and support both oo2core_6_win64 and oo2core_8_win64)

  1. build linoode with mkdir build && cd build && cmake .. && make
  2. copy output liboodle_26.so and liboodle_28.so to dotnet output folder
    • should be renamed as liboo2core_6_win64.so and liboo2core_8_win64.so since DllImport would find exact this name
    • should copy oo2core_6_win64.dll from game folder to the same folder liboo2core_6_win64.so
    • if you would like not to copy or encountered any error, add the path of liboo2core_6_win64.so and oo2core_6_win64.dll to LD_LIBRARY_PATH
~/workspace/linoodle % ls build
CMakeCache.txt  CMakeFiles  Makefile  cmake_install.cmake  liblinoodle.so  liboodle_26.so  liboodle_28.so  oo2core_6_win64.dll  pe-parse  testlinoodle
# test
~/workspace/linoodle % cp /path/to/game/oo2core_6_win64.dll bin/Debug/net8.0/ && ./build/testlinoodle ./build/liboodle_26.so
Test succeeded!
~/workspace/MyProject % dotnet build
...
~/workspace/MyProject % cp ../linoodle/liboodle_26.so bin/Debug/net8.0/liboo2core_6_win64.so
~/workspace/MyProject % cp /path/to/game/oo2core_6_win64.dll bin/Debug/net8.0/
# if this doesn't work, you might need set LD_LIBRARY_PATH
~/workspace/MyProject % export LD_LIBRARY_PATH="$PWD/bin/Debug/net8.0:$LD_LIBRARY_PATH"

@clouds56
Copy link
Author

Some note here:

On linux we import liboo2core_6_win64.so twice from our SoulsFormats library

  1. first time in public static IOodleCompressor GetOodleCompressor(), we called NativeLibrary.LoadLibrary("oo2core_6_win64"), which finally calls dlopen("liboo2core_6_win64.so") which would find lib in LD_LIBRARY_PATH (behavior of dlopen)
  2. second time on first time calling new Oodle26(), the attribute of [DllImport("oo2core_6_win64", CallingConvention = CallingConvention.StdCall)] would find liboo2core_6_win64.so in LD_LIBRARY_PATH plus "dotnet output folder" (behavior of dotnet, see also Using the LD LIBRARY PATH to load local libraries does not work on Linux dotnet/runtime#34711)

Then when liboo2core_6_win64.so initialize, it would found oo2core_6_win64.dll in LD_LIBRAY_PATH plus where executable_path (just like exe in windows do), plus the location of liboo2core_6_win64.so, (the behavior is written in code of linoodle)

@ividyon ividyon merged commit 207b3b2 into soulsmods:master Oct 14, 2024
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

Successfully merging this pull request may close these issues.

2 participants