From 66ea3b07951291aa218a568e3bb49b325aa1687a Mon Sep 17 00:00:00 2001 From: Nickolas Gupton Date: Sun, 26 Nov 2023 04:28:57 -0600 Subject: [PATCH] Split one more part of MemberReferenceScanner into its own method --- .../Scanners/MemberReferenceScanner.cs | 72 ++++++++++--------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/UnitystationLauncher/ContentScanning/Scanners/MemberReferenceScanner.cs b/UnitystationLauncher/ContentScanning/Scanners/MemberReferenceScanner.cs index 6ae9a86..fda3e62 100644 --- a/UnitystationLauncher/ContentScanning/Scanners/MemberReferenceScanner.cs +++ b/UnitystationLauncher/ContentScanning/Scanners/MemberReferenceScanner.cs @@ -55,47 +55,53 @@ when typeCfg.FieldsParsed.Any(field => field.Name == mMemberRefField.Name errors.Add(new($"Access to field not allowed: {mMemberRefField}")); return; case MMemberRefMethod mMemberRefMethod: + bool safe = IsMMemberRefMethodSafe(mMemberRefMethod, typeCfg); + if (!safe) { - foreach (WhitelistMethodDefine parsed in typeCfg.MethodsParsed) - { - bool paramMismatch = false; - - if (parsed.Name != mMemberRefMethod.Name || - !mMemberRefMethod.ReturnType.WhitelistEquals(parsed.ReturnType) || - mMemberRefMethod.ParameterTypes.Length != parsed.ParameterTypes.Count || - mMemberRefMethod.GenericParameterCount != parsed.GenericParameterCount) - { - continue; - } - - for (int i = 0; i < mMemberRefMethod.ParameterTypes.Length; i++) - { - MType a = mMemberRefMethod.ParameterTypes[i]; - MType b = parsed.ParameterTypes[i]; - - if (a.WhitelistEquals(b)) - { - continue; - } - - paramMismatch = true; - break; - } - - if (!paramMismatch) - { - return; // Found - } - } - errors.Add(new($"Access to method not allowed: {mMemberRefMethod}")); - return; } + + return; default: throw new ArgumentException($"Invalid memberRef type: {memberRef.GetType()}", nameof(memberRef)); } } + private static bool IsMMemberRefMethodSafe(MMemberRefMethod mMemberRefMethod, TypeConfig typeCfg) + { + foreach (WhitelistMethodDefine parsed in typeCfg.MethodsParsed) + { + bool paramMismatch = false; + if (parsed.Name != mMemberRefMethod.Name || + !mMemberRefMethod.ReturnType.WhitelistEquals(parsed.ReturnType) || + mMemberRefMethod.ParameterTypes.Length != parsed.ParameterTypes.Count || + mMemberRefMethod.GenericParameterCount != parsed.GenericParameterCount) + { + continue; + } + + for (int i = 0; i < mMemberRefMethod.ParameterTypes.Length; i++) + { + MType a = mMemberRefMethod.ParameterTypes[i]; + MType b = parsed.ParameterTypes[i]; + + if (a.WhitelistEquals(b)) + { + continue; + } + + paramMismatch = true; + break; + } + if (!paramMismatch) + { + return true; // Found + } + } + + return false; + } + private static MTypeReferenced? GetBaseMTypeReferenced(MMemberRef memberRef) {