UnixStreamHelper.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using MonoMod.Utils.Dummy;
  5. namespace BepInEx.Unix
  6. {
  7. internal static class UnixStreamHelper
  8. {
  9. public delegate int dupDelegate(int fd);
  10. [DynDllImport("libc")]
  11. public static dupDelegate dup;
  12. public delegate IntPtr fdopenDelegate(int fd, string mode);
  13. [DynDllImport("libc")]
  14. public static fdopenDelegate fdopen;
  15. public delegate IntPtr freadDelegate(IntPtr ptr, IntPtr size, IntPtr nmemb, IntPtr stream);
  16. [DynDllImport("libc")]
  17. public static freadDelegate fread;
  18. public delegate int fwriteDelegate(IntPtr ptr, IntPtr size, IntPtr nmemb, IntPtr stream);
  19. [DynDllImport("libc")]
  20. public static fwriteDelegate fwrite;
  21. public delegate int fcloseDelegate(IntPtr stream);
  22. [DynDllImport("libc")]
  23. public static fcloseDelegate fclose;
  24. public delegate int fflushDelegate(IntPtr stream);
  25. [DynDllImport("libc")]
  26. public static fflushDelegate fflush;
  27. static UnixStreamHelper()
  28. {
  29. var libcMapping = new Dictionary<string, List<DynDllMapping>>
  30. {
  31. ["libc"] = new List<DynDllMapping>
  32. {
  33. "libc",
  34. "libc.so.6", // Fuck you Ubuntu!!!!!!!!!!
  35. }
  36. };
  37. typeof(UnixStreamHelper).ResolveDynDllImports(libcMapping);
  38. }
  39. public static Stream CreateDuplicateStream(int fileDescriptor)
  40. {
  41. int newFd = dup(fileDescriptor);
  42. return new UnixStream(newFd, FileAccess.Write);
  43. }
  44. }
  45. }