tgstation-server 6.19.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
RightsTypeInterceptor.cs
Go to the documentation of this file.
1using System;
3using System.Linq;
5
12
14{
19 {
23 const string IsPrefix = "is";
24
28 const string NoneFieldName = $"{IsPrefix}None";
29
34
39 {
40 var rightTypes = Enum.GetValues<RightsType>();
42
43 foreach (var rightType in rightTypes)
44 {
45 var rightName = rightType.ToString();
46 inputNames.Add($"{rightName}RightsFlagsInput");
47 }
48 }
49
55 {
57
58 foreach (var field in fields)
59 {
60 var fieldName = field.Name;
62 {
64 continue;
65 }
66
67 if (!fieldName.StartsWith(IsPrefix))
68 throw new InvalidOperationException("Expected flags enum type field to start with \"is\"!");
69
70 field.Name = $"can{fieldName[IsPrefix.Length..]}";
71 field.Type = TypeReference.Parse($"{ScalarNames.Boolean}!");
72 }
73
74 if (noneField == null)
75 throw new InvalidOperationException($"Expected flags enum type field to contain \"{NoneFieldName}\"!");
76
77 fields.Remove(noneField);
78 }
79
85 {
86 // now we're hacking privates, but there's a dictionary with bad keys here that needs adjusting
88 .GetType()
89 .GetField("_flags", BindingFlags.Instance | BindingFlags.NonPublic)
90 ?.GetValue(inputValueFormatter)
91 ?? throw new InvalidOperationException("Could not locate private enum mapping dictionary field!"));
92
93 foreach (var key in dictionary.Keys.ToList())
94 {
95 if (key == NoneFieldName)
96 {
97 dictionary.Remove(key);
98 continue;
99 }
100
102 var newKey = $"can{key.Substring(IsPrefix.Length)}";
103 dictionary.Remove(key);
104 dictionary.Add(newKey, value);
105 }
106 }
107
110 {
112
114 {
115 const string PermissionSetInputName = $"{nameof(PermissionSet)}Input";
116 const string InstancePermissionSetInputName = $"{nameof(InstancePermissionSet)}Input";
117
118 var name = inputTypeDef.Name;
119 if (inputNames.Contains(name))
120 FixFields(inputTypeDef.Fields);
122 foreach (var field in inputTypeDef.Fields)
123 FixFormatter(field.Formatters.Single());
124 }
125 }
126 }
127}
Fixes the names used for the default flags types in API rights.
static void FixFormatter(IInputValueFormatter inputValueFormatter)
Fix the inputValueFormatter for a tweaked field.
const string NoneFieldName
Name given to default None fields.
RightsTypeInterceptor()
Initializes a new instance of the RightsTypeInterceptor class.
const string IsPrefix
Prefix normally used by hot chocolate for flag enums.
readonly HashSet< string > inputNames
Names of rights GraphQL input types.
static void FixFields(IBindableList< InputFieldDefinition > fields)
Fix the "is" prefix on a given set of fields .
override void OnAfterRegisterDependencies(ITypeDiscoveryContext discoveryContext, DefinitionBase definition)
@ List
User may list files if the Models.Instance allows it.
RightsType
The type of rights a model uses.
Definition RightsType.cs:7