Exemplo n.º 1
0
        private static void ExcludeModuleManagers(ref HashSet <UnityType> nativeClasses)
        {
            if (AssemblyStripper.UseUnityLinkerEngineModuleStripping)
            {
                throw new NotSupportedException("Don't want to rely on this method when UnityLinker EMS is used");
            }

            string[] moduleNames = ModuleMetadata.GetModuleNames();

            foreach (string module in moduleNames)
            {
                if (!ModuleMetadata.IsStrippableModule(module)) // Only handle strippable modules
                {
                    continue;
                }

                var moduleClasses = ModuleMetadata.GetModuleTypes(module);

                // Find (non-)manager classes
                HashSet <UnityType> managerClasses    = new HashSet <UnityType>();
                HashSet <UnityType> nonManagerClasses = new HashSet <UnityType>();
                foreach (var klass in moduleClasses)
                {
                    if (klass.IsDerivedFrom(GameManagerTypeInfo))
                    {
                        managerClasses.Add(klass);
                    }
                    else
                    {
                        nonManagerClasses.Add(klass);
                    }
                }

                // At least one non-manager class is required for the removal attempt
                if (nonManagerClasses.Count == 0)
                {
                    continue;
                }

                // Exclude manager classes if no non-manager classes are present
                if (!nativeClasses.Overlaps(nonManagerClasses))
                {
                    foreach (var klass in managerClasses)
                    {
                        nativeClasses.Remove(klass);
                    }
                }
                else
                {
                    foreach (var klass in managerClasses)
                    {
                        nativeClasses.Add(klass);
                    }
                }
            }
        }
Exemplo n.º 2
0
        static void RemoveClassesFromRemovedModules(HashSet <UnityType> nativeClasses, HashSet <string> nativeModules)
        {
            HashSet <UnityType> allModuleClasses = new HashSet <UnityType>();

            foreach (var module in nativeModules)
            {
                foreach (var klass in ModuleMetadata.GetModuleTypes(module))
                {
                    allModuleClasses.Add(klass);
                }
            }
            nativeClasses.RemoveWhere(klass => !allModuleClasses.Contains(klass));
        }
Exemplo n.º 3
0
        private static void ExcludeModuleManagers(ref HashSet <UnityType> nativeClasses)
        {
            string[] moduleNames = ModuleMetadata.GetModuleNames();

            foreach (string module in moduleNames)
            {
                if (!ModuleMetadata.IsStrippableModule(module)) // Only handle strippable modules
                {
                    continue;
                }

                var moduleClasses = ModuleMetadata.GetModuleTypes(module);

                // Find (non-)manager classes
                HashSet <UnityType> managerClasses    = new HashSet <UnityType>();
                HashSet <UnityType> nonManagerClasses = new HashSet <UnityType>();
                foreach (var klass in moduleClasses)
                {
                    if (klass.IsDerivedFrom(GameManagerTypeInfo))
                    {
                        managerClasses.Add(klass);
                    }
                    else
                    {
                        nonManagerClasses.Add(klass);
                    }
                }

                // At least one non-manager class is required for the removal attempt
                if (nonManagerClasses.Count == 0)
                {
                    continue;
                }

                // Exclude manager classes if no non-manager classes are present
                if (!nativeClasses.Overlaps(nonManagerClasses))
                {
                    foreach (var klass in managerClasses)
                    {
                        nativeClasses.Remove(klass);
                    }
                }
                else
                {
                    foreach (var klass in managerClasses)
                    {
                        nativeClasses.Add(klass);
                    }
                }
            }
        }
Exemplo n.º 4
0
 public static void ApplyManualStrippingOverrides(HashSet <UnityType> nativeClasses, HashSet <string> nativeModules, StrippingInfo strippingInfo)
 {
     string[] moduleNames = ModuleMetadata.GetModuleNames();
     for (int i = 0; i < moduleNames.Length; i++)
     {
         string text = moduleNames[i];
         ModuleIncludeSetting moduleIncludeSettingForModule = ModuleMetadata.GetModuleIncludeSettingForModule(text);
         if (moduleIncludeSettingForModule == ModuleIncludeSetting.ForceInclude)
         {
             nativeModules.Add(text);
             UnityType[] moduleTypes = ModuleMetadata.GetModuleTypes(text);
             UnityType[] array       = moduleTypes;
             for (int j = 0; j < array.Length; j++)
             {
                 UnityType unityType = array[j];
                 nativeClasses.Add(unityType);
                 if (strippingInfo != null)
                 {
                     strippingInfo.RegisterDependency(unityType.name, "Force included module");
                     strippingInfo.RegisterDependency(StrippingInfo.ModuleName(text), unityType.name);
                 }
             }
             if (strippingInfo != null)
             {
                 strippingInfo.RegisterDependency(StrippingInfo.ModuleName(text), "Force included module");
             }
         }
         else if (moduleIncludeSettingForModule == ModuleIncludeSetting.ForceExclude)
         {
             if (nativeModules.Contains(text))
             {
                 nativeModules.Remove(text);
                 UnityType[] moduleTypes2 = ModuleMetadata.GetModuleTypes(text);
                 UnityType[] array2       = moduleTypes2;
                 for (int k = 0; k < array2.Length; k++)
                 {
                     UnityType item = array2[k];
                     if (nativeClasses.Contains(item))
                     {
                         nativeClasses.Remove(item);
                     }
                 }
                 if (strippingInfo != null)
                 {
                     strippingInfo.modules.Remove(StrippingInfo.ModuleName(text));
                 }
             }
         }
     }
 }
Exemplo n.º 5
0
        private static HashSet <string> GetRequiredStrippableModules(HashSet <UnityType> nativeClasses, StrippingInfo strippingInfo)
        {
            if (AssemblyStripper.UseUnityLinkerEngineModuleStripping)
            {
                throw new NotSupportedException("Don't want to rely on this method when UnityLinker EMS is used");
            }

            HashSet <UnityType> nativeClassesUsedInModules = new HashSet <UnityType>();
            HashSet <string>    nativeModules = new HashSet <string>();

            foreach (string module in ModuleMetadata.GetModuleNames())
            {
                if (ModuleMetadata.IsStrippableModule(module)) // Only handle strippable modules. Others are listed in RegisterStaticallyLinkedModulesGranular()
                {
                    var moduleClasses = new HashSet <UnityType>(ModuleMetadata.GetModuleTypes(module));
                    if (nativeClasses.Overlaps(moduleClasses)) // Include module if at least one of its classes is present
                    {
                        nativeModules.Add(module);
                        if (strippingInfo != null)
                        {
                            foreach (var klass in moduleClasses)
                            {
                                if (nativeClasses.Contains(klass))
                                {
                                    strippingInfo.RegisterDependency(StrippingInfo.ModuleName(module), klass.name);
                                    // Don't list GlobalGameManagers in a module will automatically be added if the module is added.
                                    nativeClassesUsedInModules.Add(klass);
                                }
                            }
                        }
                    }
                }
            }

            // Classes which have not been registered as belonging to a module belong to the Core module.
            if (strippingInfo != null)
            {
                foreach (var klass in nativeClasses)
                {
                    if (!nativeClassesUsedInModules.Contains(klass))
                    {
                        strippingInfo.RegisterDependency(StrippingInfo.ModuleName("Core"), klass.name);
                    }
                }
            }
            return(nativeModules);
        }
Exemplo n.º 6
0
        public static void ApplyManualStrippingOverrides(HashSet <UnityType> nativeClasses, HashSet <string> nativeModules, StrippingInfo strippingInfo)
        {
            if (AssemblyStripper.UseUnityLinkerEngineModuleStripping)
            {
                throw new NotSupportedException("Don't want to rely on this method when UnityLinker EMS is used");
            }

            // Apply manual stripping overrides
            foreach (var module in ModuleMetadata.GetModuleNames())
            {
                var includeSetting = ModuleMetadata.GetModuleIncludeSettingForModule(module);
                if (includeSetting == ModuleIncludeSetting.ForceInclude)
                {
                    nativeModules.Add(module);
                    if (nativeClasses != null)
                    {
                        var moduleClasses = ModuleMetadata.GetModuleTypes(module);
                        foreach (var klass in moduleClasses)
                        {
                            nativeClasses.Add(klass);
                            if (strippingInfo != null)
                            {
                                strippingInfo.RegisterDependency(klass.name, "Force included module");
                                strippingInfo.RegisterDependency(StrippingInfo.ModuleName(module), klass.name);
                            }
                        }
                    }

                    if (strippingInfo != null)
                    {
                        strippingInfo.RegisterDependency(StrippingInfo.ModuleName(module), "Force included module");
                    }
                }
                else if (includeSetting == ModuleIncludeSetting.ForceExclude)
                {
                    if (nativeModules.Contains(module))
                    {
                        nativeModules.Remove(module);

                        if (strippingInfo != null)
                        {
                            strippingInfo.modules.Remove(StrippingInfo.ModuleName(module));
                        }
                    }
                }
            }
        }
        public static void ApplyManualStrippingOverrides(HashSet <UnityType> nativeClasses, HashSet <string> nativeModules, StrippingInfo strippingInfo)
        {
            // Apply manual stripping overrides
            foreach (var module in ModuleMetadata.GetModuleNames())
            {
                var includeSetting = ModuleMetadata.GetModuleIncludeSettingForModule(module);
                if (includeSetting == ModuleIncludeSetting.ForceInclude)
                {
                    nativeModules.Add(module);
                    var moduleClasses = ModuleMetadata.GetModuleTypes(module);
                    foreach (var klass in moduleClasses)
                    {
                        nativeClasses.Add(klass);
                        if (strippingInfo != null)
                        {
                            strippingInfo.RegisterDependency(klass.name, "Force included module");
                            strippingInfo.RegisterDependency(StrippingInfo.ModuleName(module), klass.name);
                        }
                    }

                    if (strippingInfo != null)
                    {
                        strippingInfo.RegisterDependency(StrippingInfo.ModuleName(module), "Force included module");
                    }
                }
                else if (includeSetting == ModuleIncludeSetting.ForceExclude)
                {
                    if (nativeModules.Contains(module))
                    {
                        nativeModules.Remove(module);
                        var moduleClasses = ModuleMetadata.GetModuleTypes(module);
                        foreach (var klass in moduleClasses)
                        {
                            if (nativeClasses.Contains(klass))
                            {
                                nativeClasses.Remove(klass);
                            }
                        }

                        if (strippingInfo != null)
                        {
                            strippingInfo.modules.Remove(StrippingInfo.ModuleName(module));
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
 private static void ExcludeModuleManagers(ref HashSet <UnityType> nativeClasses)
 {
     string[] moduleNames = ModuleMetadata.GetModuleNames();
     string[] array       = moduleNames;
     for (int i = 0; i < array.Length; i++)
     {
         string moduleName = array[i];
         if (ModuleMetadata.GetModuleStrippable(moduleName))
         {
             UnityType[]         moduleTypes = ModuleMetadata.GetModuleTypes(moduleName);
             HashSet <UnityType> hashSet     = new HashSet <UnityType>();
             HashSet <UnityType> hashSet2    = new HashSet <UnityType>();
             UnityType[]         array2      = moduleTypes;
             for (int j = 0; j < array2.Length; j++)
             {
                 UnityType unityType = array2[j];
                 if (unityType.IsDerivedFrom(CodeStrippingUtils.GameManagerTypeInfo))
                 {
                     hashSet.Add(unityType);
                 }
                 else
                 {
                     hashSet2.Add(unityType);
                 }
             }
             if (hashSet2.Count != 0)
             {
                 if (!nativeClasses.Overlaps(hashSet2))
                 {
                     foreach (UnityType current in hashSet)
                     {
                         nativeClasses.Remove(current);
                     }
                 }
                 else
                 {
                     foreach (UnityType current2 in hashSet)
                     {
                         nativeClasses.Add(current2);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 9
0
        static void RemoveClassesFromRemovedModules(HashSet <UnityType> nativeClasses, HashSet <string> nativeModules)
        {
            if (AssemblyStripper.UseUnityLinkerEngineModuleStripping)
            {
                throw new NotSupportedException("Don't want to rely on this method when UnityLinker EMS is used");
            }

            HashSet <UnityType> allModuleClasses = new HashSet <UnityType>();

            foreach (var module in nativeModules)
            {
                foreach (var klass in ModuleMetadata.GetModuleTypes(module))
                {
                    allModuleClasses.Add(klass);
                }
            }
            nativeClasses.RemoveWhere(klass => !allModuleClasses.Contains(klass));
        }
Exemplo n.º 10
0
        private static HashSet <string> GetRequiredStrippableModules(HashSet <UnityType> nativeClasses, StrippingInfo strippingInfo)
        {
            HashSet <UnityType> hashSet  = new HashSet <UnityType>();
            HashSet <string>    hashSet2 = new HashSet <string>();

            string[] moduleNames = ModuleMetadata.GetModuleNames();
            for (int i = 0; i < moduleNames.Length; i++)
            {
                string text = moduleNames[i];
                if (ModuleMetadata.GetModuleStrippable(text))
                {
                    HashSet <UnityType> hashSet3 = new HashSet <UnityType>(ModuleMetadata.GetModuleTypes(text));
                    if (nativeClasses.Overlaps(hashSet3))
                    {
                        hashSet2.Add(text);
                        if (strippingInfo != null)
                        {
                            foreach (UnityType current in hashSet3)
                            {
                                if (nativeClasses.Contains(current))
                                {
                                    strippingInfo.RegisterDependency(StrippingInfo.ModuleName(text), current.name);
                                    hashSet.Add(current);
                                }
                            }
                        }
                    }
                }
            }
            if (strippingInfo != null)
            {
                foreach (UnityType current2 in nativeClasses)
                {
                    if (!hashSet.Contains(current2))
                    {
                        strippingInfo.RegisterDependency(StrippingInfo.ModuleName("Core"), current2.name);
                    }
                }
            }
            return(hashSet2);
        }
Exemplo n.º 11
0
        public static void GenerateDependencies(string strippedAssemblyDir, string icallsListFile, RuntimeClassRegistry rcr, bool doStripping, out HashSet <UnityType> nativeClasses, out HashSet <string> nativeModules, IIl2CppPlatformProvider platformProvider)
        {
            if (AssemblyStripper.UseUnityLinkerEngineModuleStripping)
            {
                GenerateDependencies2(strippedAssemblyDir, doStripping, out nativeClasses, out nativeModules);
                return;
            }

            var strippingInfo  = platformProvider == null ? null : StrippingInfo.GetBuildReportData(platformProvider.buildReport);
            var userAssemblies = GetUserAssemblies(strippedAssemblyDir);

            // [1] Extract native classes from scene and scripts
            nativeClasses = doStripping ? GenerateNativeClassList(rcr, strippedAssemblyDir, userAssemblies, strippingInfo) : null;

            // Exclude module managers (GlobalGameManager) if no dependent classes are used.
            if (nativeClasses != null)
            {
                ExcludeModuleManagers(ref nativeClasses);
            }

            // [2] Prepare a list of modules to register
            nativeModules = GetNativeModulesToRegister(nativeClasses, strippingInfo);

            if (nativeClasses != null && icallsListFile != null)
            {
                // Get required modules from icall list file
                var icallModules = GetModulesFromICalls(icallsListFile);

                // Add GameManager classes for modules
                foreach (var module in icallModules)
                {
                    if (!nativeModules.Contains(module))
                    {
                        if (strippingInfo != null)
                        {
                            strippingInfo.RegisterDependency(StrippingInfo.ModuleName(module), StrippingInfo.RequiredByScripts);
                        }
                    }

                    var moduleClasses = ModuleMetadata.GetModuleTypes(module);
                    foreach (var klass in moduleClasses)
                    {
                        if (klass.IsDerivedFrom(GameManagerTypeInfo))
                        {
                            nativeClasses.Add(klass);
                        }
                    }
                }

                nativeModules.UnionWith(icallModules);
            }

            ApplyManualStrippingOverrides(nativeClasses, nativeModules, strippingInfo);

            bool didAdd = true;

            if (platformProvider != null)
            {
                while (didAdd)
                {
                    didAdd = false;
                    foreach (var module in nativeModules.ToList())
                    {
                        var dependecies = ModuleMetadata.GetModuleDependencies(module);
                        foreach (var dependentModule in dependecies)
                        {
                            if (!nativeModules.Contains(dependentModule))
                            {
                                nativeModules.Add(dependentModule);
                                didAdd = true;
                            }
                            if (strippingInfo != null)
                            {
                                var moduleName = StrippingInfo.ModuleName(module);
                                strippingInfo.RegisterDependency(StrippingInfo.ModuleName(dependentModule), "Required by " + moduleName);
                                strippingInfo.SetIcon("Required by " + moduleName, $"package/com.unity.modules.{module.ToLower()}");
                            }
                        }
                    }
                }
            }

            if (nativeClasses != null)
            {
                RemoveClassesFromRemovedModules(nativeClasses, nativeModules);
            }

            AssemblyReferenceChecker checker = new AssemblyReferenceChecker();

            checker.CollectReferencesFromRoots(strippedAssemblyDir, userAssemblies, true, 0.0f, true);

            if (strippingInfo != null)
            {
                foreach (var module in nativeModules)
                {
                    strippingInfo.AddModule(module);
                }
                strippingInfo.AddModule("Core");
            }

            if (nativeClasses != null && strippingInfo != null && platformProvider != null)
            {
                InjectCustomDependencies(platformProvider.target, strippingInfo, nativeClasses, nativeModules);
            }
        }
Exemplo n.º 12
0
        public static void GenerateDependencies(string strippedAssemblyDir, string icallsListFile, RuntimeClassRegistry rcr, bool doStripping, out HashSet <UnityType> nativeClasses, out HashSet <string> nativeModules, IIl2CppPlatformProvider platformProvider)
        {
            StrippingInfo strippingInfo = (platformProvider != null) ? StrippingInfo.GetBuildReportData(platformProvider.buildReport) : null;

            string[] userAssemblies = CodeStrippingUtils.GetUserAssemblies(strippedAssemblyDir);
            nativeClasses = ((!doStripping) ? null : CodeStrippingUtils.GenerateNativeClassList(rcr, strippedAssemblyDir, userAssemblies, strippingInfo));
            if (nativeClasses != null)
            {
                CodeStrippingUtils.ExcludeModuleManagers(ref nativeClasses);
            }
            nativeModules = CodeStrippingUtils.GetNativeModulesToRegister(nativeClasses, strippingInfo);
            if (nativeClasses != null && icallsListFile != null)
            {
                HashSet <string> modulesFromICalls = CodeStrippingUtils.GetModulesFromICalls(icallsListFile);
                foreach (string current in modulesFromICalls)
                {
                    if (!nativeModules.Contains(current))
                    {
                        if (strippingInfo != null)
                        {
                            strippingInfo.RegisterDependency(StrippingInfo.ModuleName(current), "Required by Scripts");
                        }
                    }
                    UnityType[] moduleTypes = ModuleMetadata.GetModuleTypes(current);
                    UnityType[] array       = moduleTypes;
                    for (int i = 0; i < array.Length; i++)
                    {
                        UnityType unityType = array[i];
                        if (unityType.IsDerivedFrom(CodeStrippingUtils.GameManagerTypeInfo))
                        {
                            nativeClasses.Add(unityType);
                        }
                    }
                }
                nativeModules.UnionWith(modulesFromICalls);
            }
            bool flag = true;

            if (platformProvider != null)
            {
                while (flag)
                {
                    flag = false;
                    foreach (string current2 in nativeModules.ToList <string>())
                    {
                        string moduleWhitelist = CodeStrippingUtils.GetModuleWhitelist(current2, platformProvider.moduleStrippingInformationFolder);
                        if (File.Exists(moduleWhitelist))
                        {
                            foreach (string current3 in CodeStrippingUtils.GetDependentModules(moduleWhitelist))
                            {
                                if (!nativeModules.Contains(current3))
                                {
                                    nativeModules.Add(current3);
                                    flag = true;
                                }
                                if (strippingInfo != null)
                                {
                                    string text = StrippingInfo.ModuleName(current2);
                                    strippingInfo.RegisterDependency(StrippingInfo.ModuleName(current3), "Required by " + text);
                                    if (strippingInfo.icons.ContainsKey(text))
                                    {
                                        strippingInfo.SetIcon("Required by " + text, strippingInfo.icons[text]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            AssemblyReferenceChecker assemblyReferenceChecker = new AssemblyReferenceChecker();

            assemblyReferenceChecker.CollectReferencesFromRoots(strippedAssemblyDir, userAssemblies, true, 0f, true);
            if (strippingInfo != null)
            {
                foreach (string current4 in nativeModules)
                {
                    strippingInfo.AddModule(StrippingInfo.ModuleName(current4));
                }
                strippingInfo.AddModule(StrippingInfo.ModuleName("Core"));
            }
            if (nativeClasses != null && strippingInfo != null)
            {
                CodeStrippingUtils.InjectCustomDependencies(platformProvider.target, strippingInfo, nativeClasses, nativeModules);
            }
        }