UnixStreamHelper.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. public delegate int isattyDelegate(int fd);
  28. [DynDllImport("libc")]
  29. public static isattyDelegate isatty;
  30. static UnixStreamHelper()
  31. {
  32. var libcMapping = new Dictionary<string, List<DynDllMapping>>
  33. {
  34. ["libc"] = new List<DynDllMapping>
  35. {
  36. "libc",
  37. "libc.so.6", // Fuck you Ubuntu!!!!!!!!!!
  38. }
  39. };
  40. typeof(UnixStreamHelper).ResolveDynDllImports(libcMapping);
  41. }
  42. public static Stream CreateDuplicateStream(int fileDescriptor)
  43. {
  44. int newFd = dup(fileDescriptor);
  45. return new UnixStream(newFd, FileAccess.Write);
  46. }
  47. }
  48. }