示例#1
0
    public bool CanRemoveResources(RemoveResources remove)
    {
        int food  = remove.food;
        int wood  = remove.wood;
        int gold  = remove.gold;
        int stone = remove.stone;

        return(CanRemoveResources(food, wood, gold, stone));
    }
示例#2
0
        public override bool Execute()
        {
            LogLine(CopyrightNotice);

            if (string.IsNullOrEmpty(OutputFileName))
            {
                OutputFileName = InputFileName;
            }

            if (!Directory.Exists(Path.GetDirectoryName(OutputFileName)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(OutputFileName));
            }


            LogLine("Opening assembly {0}", InputFileName);

            var readWriteSymbols = File.Exists(Path.ChangeExtension(InputFileName, ".pdb"));

            var assembly = AssemblyDefinition.ReadAssembly(InputFileName, new ReaderParameters {
                ReadSymbols = readWriteSymbols
            });
            var mainModule   = assembly.MainModule;
            var allTypes     = mainModule.Types;
            var allResources = mainModule.Resources;


            _typeIdsFound.Add("<Module>");

            if (assembly.EntryPoint != null)
            {
                AddScanType(assembly.EntryPoint.DeclaringType);
            }

            foreach (TypeDefinition typeDef in allTypes.Where(t => t.IsPublic || InheritsFrom(t, SysAttributeName)))
            {
                AddScanType(typeDef);
            }

            if (!string.IsNullOrEmpty(KeepTypes))
            {
                foreach (var typeRegEx in KeepTypes.Split(',', ';'))
                {
                    var regEx = new Regex(typeRegEx, RegexOptions.Compiled);
                    foreach (var typeDef in allTypes.Where(r => regEx.IsMatch(r.FullName)))
                    {
                        AddScanType(typeDef);
                    }
                }
            }

            LogLine("Found {0} accessible types.", _typeIdsFound.Count);


            var removeTypeCount = 0;

            foreach (var typeDef in allTypes.Where(t => !_typeIdsFound.Contains(t.FullName)).ToList())
            {
                if (Verbose >= 2)
                {
                    LogLine("Removing: {0}", typeDef);
                }
                allTypes.Remove(typeDef);
                removeTypeCount++;
            }

            LogLine("Removed {0} inaccessible types.", removeTypeCount);


            IEnumerable <Resource> removeResources = null;

            if (!string.IsNullOrEmpty(KeepResources))
            {
                var keep = new List <Resource>();
                foreach (var resRegEx in KeepResources.Split(',', ';'))
                {
                    var regEx = new Regex(resRegEx, RegexOptions.Compiled);
                    keep = keep.Concat(mainModule.Resources.Where(r => regEx.IsMatch(r.Name))).ToList();
                }
                removeResources = allResources.Where(r => !keep.Contains(r));
            }

            else if (!string.IsNullOrEmpty(RemoveResources))
            {
                foreach (var resRegEx in RemoveResources.Split(',', ';'))
                {
                    var regEx = new Regex(resRegEx, RegexOptions.Compiled);
                    removeResources = (removeResources ?? new List <Resource>()).Concat(allResources.Where(r => regEx.IsMatch(r.Name)));
                }
            }

            if (removeResources != null)
            {
                var removeResCount = 0;

                foreach (var removeRes in removeResources.ToList())
                {
                    if (Verbose >= 2)
                    {
                        LogLine("Removing: {0}", removeRes.Name);
                    }
                    allResources.Remove(removeRes);
                    removeResCount++;
                }

                LogLine("Removed {0} resources.", removeResCount);
            }


            if (!string.IsNullOrEmpty(RenameResources))
            {
                var rename = new List <Resource>();
                foreach (var resRegex in RenameResources.Split(',', ';'))
                {
                    var regex = new Regex(resRegex, RegexOptions.Compiled);
                    rename.AddRange(mainModule.Resources.Where(x => regex.IsMatch(x.Name)));
                }

                var newAssemblyName = Path.GetFileNameWithoutExtension(OutputFileName);
                foreach (var res in rename)
                {
                    res.Name = newAssemblyName + res.Name.Substring(res.Name.IndexOf('.'));
                }

                LogLine("Renamed {0} resources.", rename.Count);
            }

            LogLine("Saving assembly to {0}", OutputFileName);
            assembly.Write(OutputFileName, new WriterParameters {
                WriteSymbols = readWriteSymbols
            });

            LogLine("Done.");

            return(true);
        }