/// <summary> /// Processes the assembly - goes through each member and applies a mapping. /// </summary> /// <param name="context">The context.</param> /// <param name="definition">The assembly definition.</param> private static void ProcessAssembly(ICloakContext context, AssemblyDefinition definition) { //Store whether to obfuscate all members bool obfuscateAll = context.Settings.ObfuscateAllModifiers; //Set up the mapping graph AssemblyMapping assemblyMapping = context.MappingGraph.AddAssembly(definition); //Get a reference to the name manager NameManager nameManager = context.NameManager; //Go through each module foreach (ModuleDefinition moduleDefinition in definition.Modules) { //Go through each type foreach (TypeDefinition typeDefinition in moduleDefinition.GetAllTypes()) { //First of all - see if we've declared it already - if so get the existing reference TypeMapping typeMapping = assemblyMapping.GetTypeMapping(typeDefinition); if (typeMapping == null) { //We don't have it - get it if (obfuscateAll) { typeMapping = assemblyMapping.AddType(typeDefinition, nameManager.GenerateName(NamingTable.Type)); } else { typeMapping = assemblyMapping.AddType(typeDefinition, null); } } //Go through each method foreach (MethodDefinition methodDefinition in typeDefinition.Methods) { //First of all - check if we've obfuscated it already - if we have then don't bother if (typeMapping.HasMethodBeenObfuscated(methodDefinition.Name)) { continue; } //We won't do constructors - causes issues if (methodDefinition.IsConstructor) { continue; } //We haven't - let's work out the obfuscated name if (obfuscateAll) { //Take into account whether this is overriden, or an interface implementation if (methodDefinition.IsVirtual) { //We handle this differently - rather than creating a new name each time we need to reuse any already generated names //We do this by firstly finding the root interface or object TypeDefinition baseType = FindBaseTypeDeclaration(typeDefinition, methodDefinition); if (baseType != null) { //Find it in the mappings TypeMapping baseTypeMapping = assemblyMapping.GetTypeMapping(baseType); if (baseTypeMapping != null) { //We found the type mapping - look up the name it uses for this method and use that if (baseTypeMapping.HasMethodMapping(methodDefinition)) { typeMapping.AddMethodMapping(methodDefinition, baseTypeMapping.GetObfuscatedMethodName(methodDefinition)); } else { //That's strange... we shouldn't get into here - but if we ever do then //we'll add the type mapping into both string obfuscatedName = nameManager.GenerateName(NamingTable.Method); typeMapping.AddMethodMapping(methodDefinition, obfuscatedName); baseTypeMapping.AddMethodMapping(methodDefinition, obfuscatedName); } } else { //Otherwise add it into our list manually //at the base level first off baseTypeMapping = assemblyMapping.AddType(baseType, nameManager.GenerateName(NamingTable.Type)); string obfuscatedName = nameManager.GenerateName(NamingTable.Method); baseTypeMapping.AddMethodMapping(methodDefinition, obfuscatedName); //Now at our implemented level typeMapping.AddMethodMapping(methodDefinition, obfuscatedName); } } else { //We must be at the base already - add normally typeMapping.AddMethodMapping(methodDefinition, nameManager.GenerateName(NamingTable.Method)); } } else //Add normally { typeMapping.AddMethodMapping(methodDefinition, nameManager.GenerateName(NamingTable.Method)); } } else if (methodDefinition.IsPrivate) { typeMapping.AddMethodMapping(methodDefinition, nameManager.GenerateName(NamingTable.Method)); } } //Properties foreach (PropertyDefinition propertyDefinition in typeDefinition.Properties) { //First of all - check if we've obfuscated it already - if we have then don't bother if (typeMapping.HasPropertyBeenObfuscated(propertyDefinition.Name)) { continue; } //Go through the old fashioned way if (obfuscateAll) { if ((propertyDefinition.GetMethod != null && propertyDefinition.GetMethod.IsVirtual) || (propertyDefinition.SetMethod != null && propertyDefinition.SetMethod.IsVirtual)) { //We handle this differently - rather than creating a new name each time we need to reuse any already generated names //We do this by firstly finding the root interface or object TypeDefinition baseType = FindBaseTypeDeclaration(typeDefinition, propertyDefinition); if (baseType != null) { //Find it in the mappings TypeMapping baseTypeMapping = assemblyMapping.GetTypeMapping(baseType); if (baseTypeMapping != null) { //We found the type mapping - look up the name it uses for this property and use that if (baseTypeMapping.HasPropertyMapping(propertyDefinition)) { typeMapping.AddPropertyMapping(propertyDefinition, baseTypeMapping.GetObfuscatedPropertyName(propertyDefinition)); } else { //That's strange... we shouldn't get into here - but if we ever do then //we'll add the type mapping into both string obfuscatedName = nameManager.GenerateName(NamingTable.Property); typeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName); baseTypeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName); } } else { //Otherwise add it into our list manually //at the base level first off baseTypeMapping = assemblyMapping.AddType(baseType, nameManager.GenerateName(NamingTable.Type)); string obfuscatedName = nameManager.GenerateName(NamingTable.Property); baseTypeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName); //Now at our implemented level typeMapping.AddPropertyMapping(propertyDefinition, obfuscatedName); } } else { //We must be at the base already - add normally typeMapping.AddPropertyMapping(propertyDefinition, nameManager.GenerateName(NamingTable.Property)); } } else { typeMapping.AddPropertyMapping(propertyDefinition, nameManager.GenerateName(NamingTable.Property)); } } else if (propertyDefinition.GetMethod != null && propertyDefinition.SetMethod != null) { //Both parts need to be private if (propertyDefinition.GetMethod.IsPrivate && propertyDefinition.SetMethod.IsPrivate) { typeMapping.AddPropertyMapping(propertyDefinition, nameManager.GenerateName(NamingTable.Property)); } } else if (propertyDefinition.GetMethod != null) { //Only the get is present - make sure it is private if (propertyDefinition.GetMethod.IsPrivate) { typeMapping.AddPropertyMapping(propertyDefinition, nameManager.GenerateName(NamingTable.Property)); } } else if (propertyDefinition.SetMethod != null) { //Only the set is present - make sure it is private if (propertyDefinition.SetMethod.IsPrivate) { typeMapping.AddPropertyMapping(propertyDefinition, nameManager.GenerateName(NamingTable.Property)); } } } //Fields foreach (FieldDefinition fieldDefinition in typeDefinition.Fields) { //First of all - check if we've obfuscated it already - if we have then don't bother if (typeMapping.HasFieldBeenObfuscated(fieldDefinition.Name)) { continue; } if (obfuscateAll) { typeMapping.AddFieldMapping(fieldDefinition, nameManager.GenerateName(NamingTable.Field)); } else if (fieldDefinition.IsPrivate) { //Rename if private typeMapping.AddFieldMapping(fieldDefinition, nameManager.GenerateName(NamingTable.Field)); } } } } }
//=========================================================================== //Character generation, aka why we're all here today. public void GenerateNewCharacter() { //++++++++++++++++++++ //STEP 1 // //Decide on era // //- If era is set to random, pick era at random. Skip this step if a current year is specified. if (currentYear == 0) { if (era == CharacterClass.Era.Random) { //Pick a random era and pass it to the character var values = System.Enum.GetValues(typeof(CharacterClass.Era)); character.era = (CharacterClass.Era)Random.Range(1, values.Length); } else { //Just pass the selected era to the Character character.era = era; } //Era is now set, now generate a current year. character.currentYear = GetYearFromEra(character.era); } else { //Year is specified, match it with an era and pass it to the character character.currentYear = currentYear; character.era = GetEraFromYear(currentYear); } //++++++++++++++++++++ //STEP 2 // //Species // //Now that we know the current era, we can pick a species from the pool of the ones available, if species is set to random if (species == CharacterClass.Species.Random) { //Load up all the predefined available speciess based on the era we selected above. List <CharacterClass.Species> availableSpeciesinSelectedEra = eraDefinition[character.era].availableSpeciesInEra; character.species = availableSpeciesinSelectedEra[Random.Range(0, availableSpeciesinSelectedEra.Count)]; } else { //Nevermind, it's set. Just pass it on. character.species = species; } //++++++++++++++++++++ //STEP 3 // //Stats // //Knowing the species, we can now use the stats recipes to stat up this character according to selected power level RollStats(character.species); //++++++++++++++++++++ //STEP 4 // //Age // //We know current year and species. Let's chuck up a random age class, generate an age within that and subtract it from current year to get YoB. if (age == 0) //Age isn't specified at all. Is age group specified? { if (ageGroup == CharacterClass.AgeGroup.Random) //Age group is random, pick one of those first { character.ageGroup = (CharacterClass.AgeGroup)Random.Range(1, System.Enum.GetValues(typeof(CharacterClass.AgeGroup)).Length); } else //Age group is set. Just pass it on. { character.ageGroup = ageGroup; } //Now we have our age group, time to slam out an age inside that age group for the chosen species. character.age = GetAgeFromAgeGroup(character.ageGroup, character.species); } else { if (age > speciesDefinition[character.species].ageRange_Ancient.y) { age = (int)speciesDefinition[character.species].ageRange_Ancient.y; } //Quick safeguard //Age is already defined, just assign it and match it with an age class. character.age = age; character.ageGroup = GetAgeGroupFromAge(character.age, character.species); } //Almost done, now get year of birth character.yearOfBirth = character.currentYear - character.age; //++++++++++++++++++++ //STEP 5 // //Name // //Always nice to have a name. This one's easy. if (characterName == "") { character.characterName = nameManager.GenerateName(character.species); } else { character.characterName = characterName; } //++++++++++++++++++++ //STEP 6 // //Gifts // //Same deal as choosing species, compare to era and pick a gift at random. This one however is weighted towards no gift based on powerlevel. if (gift == CharacterClass.Gift.Random) { var availableGifts = eraDefinition[character.era].availableGiftsInEra; int tempPowerLevel; if (powerLevel == CharacterClass.PowerLevel.Random) { tempPowerLevel = Random.Range(1, System.Enum.GetValues(typeof(CharacterClass.PowerLevel)).Length); } else { tempPowerLevel = (int)powerLevel; } int giftChance = Random.Range(0, 100) * tempPowerLevel; if (giftChance < 85) { character.gift = CharacterClass.Gift.None; } else { var avail = eraDefinition[character.era].availableGiftsInEra; character.gift = avail[Random.Range(0, avail.Count)]; } } else { character.gift = gift; } //++++++++++++++++++++ //STEP 6.1 // //Gift details // //If any gifts have been given out, find out more about them. switch (character.gift) { case CharacterClass.Gift.None: character.pantheon = CharacterClass.Pantheon.None; character.esper = CharacterClass.Esper.None; break; case CharacterClass.Gift.Syn: character.pantheon = CharacterClass.Pantheon.None; character.esper = CharacterClass.Esper.None; break; case CharacterClass.Gift.Esper: character.pantheon = CharacterClass.Pantheon.None; if (esper == CharacterClass.Esper.Random || esper == CharacterClass.Esper.None) { character.esper = (CharacterClass.Esper)Random.Range(2, System.Enum.GetValues(typeof(CharacterClass.Esper)).Length); } else { character.esper = esper; } break; case CharacterClass.Gift.Medium: if (pantheon == CharacterClass.Pantheon.Random || pantheon == CharacterClass.Pantheon.None) { character.pantheon = (CharacterClass.Pantheon)Random.Range(2, System.Enum.GetValues(typeof(CharacterClass.Pantheon)).Length); } else { character.pantheon = pantheon; } character.esper = CharacterClass.Esper.None; break; } uIManager.UpdateUIValues(); }
public void GenerateName(NameManager nameManager) { nameManager.GenerateName(); }