You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.AccessControl;
  6. using System.Security.Principal;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace IPA.Injector
  10. {
  11. internal static class PermissionFix
  12. {
  13. public static Task FixPermissions(DirectoryInfo root)
  14. {
  15. if (!root.Exists) return Task.CompletedTask;
  16. return Task.Run(() =>
  17. {
  18. try
  19. {
  20. var acl = root.GetAccessControl();
  21. var rules = acl.GetAccessRules(true, true, typeof(SecurityIdentifier));
  22. var requestedRights = FileSystemRights.Modify;
  23. var requestedInheritance = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
  24. var requestedPropagation = PropagationFlags.InheritOnly;
  25. bool hasRule = false;
  26. for (var i = 0; i < rules.Count; i++)
  27. {
  28. var rule = rules[i];
  29. if (rule is FileSystemAccessRule fsrule
  30. && fsrule.AccessControlType == AccessControlType.Allow
  31. && fsrule.InheritanceFlags.HasFlag(requestedInheritance)
  32. && fsrule.PropagationFlags == requestedPropagation
  33. && fsrule.FileSystemRights.HasFlag(requestedRights))
  34. { hasRule = true; break; }
  35. }
  36. if (!hasRule)
  37. { // this is *sooo* fucking slow on first run
  38. acl.AddAccessRule(
  39. new FileSystemAccessRule(
  40. new SecurityIdentifier(WellKnownSidType.WorldSid, null),
  41. requestedRights,
  42. requestedInheritance,
  43. requestedPropagation,
  44. AccessControlType.Allow
  45. )
  46. );
  47. root.SetAccessControl(acl);
  48. }
  49. }
  50. catch (Exception e)
  51. {
  52. Logging.Logger.log.Warn("Error configuring permissions in the game install dir");
  53. Logging.Logger.log.Warn(e);
  54. }
  55. });
  56. }
  57. }
  58. }