public static void ResolveCollisions()
        {
            var seenHashes   = new HashSet <ushort>();
            var defsToRehash = new List <Def>();

            foreach (Type current in GenDefDatabase.AllDefTypesWithDatabases())
            {
                var type              = typeof(DefDatabase <>).MakeGenericType(current);
                var property          = type.GetProperty("AllDefs");
                var getMethod         = property.GetGetMethod();
                var allDefsInDatabase = (IEnumerable)getMethod.Invoke(null, null);
                defsToRehash.Clear();
                foreach (Def def in allDefsInDatabase)
                {
                    if (seenHashes.Contains(def.shortHash))
                    {
                        defsToRehash.Add(def);
                    }
                    else
                    {
                        seenHashes.Add(def.shortHash);
                    }
                }
                defsToRehash.SortBy(d => d.defName);
                for (int i = 0; i < defsToRehash.Count; i++)
                {
                    var def = defsToRehash[i];
                    def.shortHash = 0;
                    InjectedDefHasher.GiveShortHasToDef(def);
                    Log.Message(def.defName + " " + def.shortHash);
                }
                seenHashes.Clear();
            }
        }
        // Will return null if recipe requires no components
        private RecipeDef TryMakeRecipeVariantWithSteel(RecipeDef recipeOriginal)
        {
            var recipeCopy = (RecipeDef)objectCloneMethod.Invoke(recipeOriginal, null);

            recipeCopy.shortHash = 0;
            InjectedDefHasher.GiveShortHasToDef(recipeCopy, typeof(RecipeDef));
            recipeCopy.defName += RemoteExplosivesUtility.InjectedRecipeNameSuffix;

            var newFixedFilter = new ThingFilter();

            foreach (var allowedThingDef in recipeOriginal.fixedIngredientFilter.AllowedThingDefs)
            {
                if (allowedThingDef == ThingDefOf.Component)
                {
                    continue;
                }
                newFixedFilter.SetAllow(allowedThingDef, true);
            }
            newFixedFilter.SetAllow(ThingDefOf.Steel, true);
            recipeCopy.fixedIngredientFilter   = newFixedFilter;
            recipeCopy.defaultIngredientFilter = null;

            float numComponentsRequired = 0;
            var   newIngredientList     = new List <IngredientCount>(recipeOriginal.ingredients);

            foreach (var ingredientCount in newIngredientList)
            {
                if (ingredientCount.filter.Allows(ThingDefOf.Component))
                {
                    numComponentsRequired = ingredientCount.GetBaseCount();
                    newIngredientList.Remove(ingredientCount);
                    break;
                }
            }
            if (numComponentsRequired == 0)
            {
                return(null);
            }

            var steelFilter = new ThingFilter();

            steelFilter.SetAllow(ThingDefOf.Steel, true);
            var steelIngredient = new IngredientCount {
                filter = steelFilter
            };

            steelIngredient.SetBaseCount(ComponentValueInSteel * numComponentsRequired);
            newIngredientList.Add(steelIngredient);
            recipeCopy.ingredients = newIngredientList;
            recipeCopy.ResolveReferences();
            return(recipeCopy);
        }
Beispiel #3
0
        private void InjectDef()
        {
            if (DefDatabase <ThingDef> .GetNamedSilentFail(TokenDefName) != null)
            {
                return;
            }
            var def = new ThingDef {
                defName    = TokenDefName,
                label      = "HugsLib reload detector",
                thingClass = typeof(Thing),
                menuHidden = true
            };

            InjectedDefHasher.GiveShortHasToDef(def);
            DefDatabase <ThingDef> .Add(def);
        }
Beispiel #4
0
        private void TestGiveShortHash()
        {
            var def = new Def {
                defName = "randomDefForTesting"
            };

            InjectedDefHasher.GiveShortHashToDef(def, typeof(Def));
            if (def.shortHash == 0)
            {
                Logger.Error("GiveShortHasToDef has failed");
            }
            else
            {
                Logger.Message("Given short hash: " + def.shortHash);
            }
        }
Beispiel #5
0
 public TickingThingDef()
 {
     defName     = "testTickingThingDef";
     thingClass  = typeof(TickingTestThing);
     label       = defName;
     drawerType  = DrawerType.RealtimeOnly;
     category    = ThingCategory.Item;
     graphicData = new GraphicData()
     {
         graphicClass = typeof(Graphic_Single),
         shaderType   = ShaderType.Cutout,
         texPath      = "Things/Building/Art/Snowman",
         drawSize     = Vector2.one
     };
     selectable = true;
     tickerType = TickerType.Never;
     isSaveable = false;
     InjectedDefHasher.GiveShortHashToDef(this, typeof(ThingDef));
 }
        public static TraderKindDef CopyTraderKindDef(TraderKindDef origTraderKind, string labelBase)
        {
            string newDefName = "FB_" + GenText.ToTitleCaseSmart(labelBase).Replace(" ", "_");

            // Construction
            var newTraderKind = new TraderKindDef {
                defName     = newDefName,
                label       = origTraderKind.label,
                commonality = origTraderKind.commonality,
                orbital     = false,
                requestable = true,

                // Can't change most of these anyway, so we're just going to add them as a new list
                stockGenerators = origTraderKind.stockGenerators.ListFullCopyOrNull(),
            };

            InjectedDefHasher.GiveShortHashToDef(newTraderKind, typeof(TraderKindDef));

            return(newTraderKind);
        }
 private void PrepareReflection()
 {
     InjectedDefHasher.PrepareReflection();
     LogWindowExtensions.PrepareReflection();
     QuickstartController.PrepareReflection();
 }
        /// <summary>
        /// Generates a copy of a given recipe with the provided ingredient replaced with another, at the given ratio.
        ///  Will return null if recipe requires none of the original ingredient.
        /// </summary>
        private RecipeDef TryMakeRecipeVariant(RecipeDef recipeOriginal, RecipeVariantType variant, ThingDef originalIngredient, ThingDef replacementIngredient, float replacementRatio, float workAmountMultiplier)
        {
            // check original recipe for the replaced ingredient, copy other ingredients
            var resourceCountRequired = 0f;
            var newIngredientList     = new List <IngredientCount>(recipeOriginal.ingredients);

            foreach (var ingredientCount in newIngredientList)
            {
                if (ingredientCount.filter.Allows(originalIngredient))
                {
                    resourceCountRequired = ingredientCount.GetBaseCount();
                    newIngredientList.Remove(ingredientCount);
                    break;
                }
            }
            if (resourceCountRequired < float.Epsilon)
            {
                return(null);
            }

            var recipeCopy = CloneObject(recipeOriginal);

            recipeCopy.defName   = $"{recipeOriginal.defName}_{replacementIngredient.defName}";
            recipeCopy.shortHash = 0;
            InjectedDefHasher.GiveShortHashToDef(recipeCopy, typeof(RecipeDef));
            // clone our extension to avoid polluting the original def
            recipeCopy.modExtensions = recipeCopy.modExtensions?.Select(e => e is ICloneable i ? (DefModExtension)i.Clone() : e).ToList();
            if (!recipeOriginal.HasModExtension <MakeRecipeVariants>())
            {
                // mark original as a variant, as well
                recipeOriginal.modExtensions = recipeOriginal.modExtensions ?? new List <DefModExtension>();
                recipeOriginal.modExtensions.Add(new MakeRecipeVariants());
            }

            // mark the copy as variant
            var variantExtension = recipeCopy.GetModExtension <MakeRecipeVariants>();

            if (variantExtension == null)
            {
                variantExtension         = new MakeRecipeVariants();
                recipeCopy.modExtensions = recipeCopy.modExtensions ?? new List <DefModExtension>();
                recipeCopy.modExtensions.Add(variantExtension);
            }
            variantExtension.Variant |= variant;

            // copy non-replaced ingredients over to the new filter
            var newFixedFilter = new ThingFilter();

            foreach (var allowedThingDef in recipeOriginal.fixedIngredientFilter.AllowedThingDefs)
            {
                if (allowedThingDef != originalIngredient)
                {
                    newFixedFilter.SetAllow(allowedThingDef, true);
                }
            }
            newFixedFilter.SetAllow(replacementIngredient, true);
            recipeCopy.fixedIngredientFilter   = newFixedFilter;
            recipeCopy.defaultIngredientFilter = null;

            // add the replacement ingredient
            var replacementIngredientFilter = new ThingFilter();

            replacementIngredientFilter.SetAllow(replacementIngredient, true);
            var replacementCount = new IngredientCount {
                filter = replacementIngredientFilter
            };

            replacementCount.SetBaseCount(Mathf.Round(resourceCountRequired * replacementRatio));
            newIngredientList.Add(replacementCount);
            recipeCopy.ingredients = newIngredientList;

            // multiply work amount
            recipeCopy.workAmount = recipeOriginal.workAmount * workAmountMultiplier;

            recipeCopy.ResolveReferences();
            return(recipeCopy);
        }
 private void PrepareReflection()
 {
     InjectedDefHasher.PrepareReflection();
     LogWindowInjection.PrepareReflection();
 }