Skip to content

Commit

Permalink
Merge pull request #325 from edbmods/bugfixes-1.14.2
Browse files Browse the repository at this point in the history
Fixed problem with custom xenotypes
  • Loading branch information
edbmods authored Dec 27, 2022
2 parents 9617790 + fd85c5c commit 25f78ca
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
[assembly: AssemblyVersion("1.1.1")]

// Increment for each new release
[assembly: AssemblyFileVersion("1.4.1")]
[assembly: AssemblyFileVersion("1.4.2")]
7 changes: 7 additions & 0 deletions Resources/CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
_____________________________________________________________________________

Version 1.4.2
_____________________________________________________________________________

- Fixed issue with custom xenotypes

_____________________________________________________________________________

Version 1.4.1
Expand Down
2 changes: 1 addition & 1 deletion Source/ColonistFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class ColonistFiles {
public static string SavedColonistsFolderPath {
get {
try {
return Reflection.GenFilePaths.FolderUnderSaveData("PrepareCarefully");
return Reflection.ReflectorGenFilePaths.FolderUnderSaveData("PrepareCarefully");
}
catch (Exception e) {
Logger.Error("Failed to get colonist save directory");
Expand Down
4 changes: 2 additions & 2 deletions Source/ControllerPawns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ public void RandomizeBackstories() {
float adultStoryAge = alienRace == null ? providerAlienRaces.DefaultMinAgeForAdulthood : alienRace.MinAgeForAdulthood;
//Logger.Debug(String.Format("Adulthood age for {0} is {1}", state.CurrentPawn.Pawn.def.defName, adultStoryAge));

List<BackstoryCategoryFilter> backstoryCategoryFiltersFor = Reflection.PawnBioAndNameGenerator.GetBackstoryCategoryFiltersFor(currentPawn.Pawn, factionDef);
List<BackstoryCategoryFilter> backstoryCategoryFiltersFor = Reflection.ReflectorPawnBioAndNameGenerator.GetBackstoryCategoryFiltersFor(currentPawn.Pawn, factionDef);
// Generate a bio from which to get the backstories
if (!Reflection.PawnBioAndNameGenerator.TryGetRandomUnusedSolidBioFor(backstoryCategoryFiltersFor, kindDef, currentPawn.Gender, null, out PawnBio pawnBio)) {
if (!Reflection.ReflectorPawnBioAndNameGenerator.TryGetRandomUnusedSolidBioFor(backstoryCategoryFiltersFor, kindDef, currentPawn.Gender, null, out PawnBio pawnBio)) {
// Other mods are patching the vanilla method in ways that cause it to return false. If that happens,
// we use our duplicate implementation instead.
var providerBackstories = PrepareCarefully.Instance.Providers.Backstories;
Expand Down
29 changes: 26 additions & 3 deletions Source/CustomPawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,17 @@ public void InitializeWithPawn(Pawn pawn) {
InitializeInjuriesAndImplantsFromPawn(pawn);

if (ModsConfig.BiotechActive) {
RandomizeXenotype = pawn.genes.Xenotype;
RandomizeCustomXenotype = pawn.genes.CustomXenotype;
customXenotype = pawn.MatchGenesToCustomXenotype();
if (customXenotype != null) {
RandomizeCustomXenotype = customXenotype;
RandomizeXenotype = null;
xenotype = null;
}
else {
RandomizeCustomXenotype = null;
xenotype = pawn.genes.Xenotype;
RandomizeXenotype = xenotype;
}
RandomizeDevelopmentalStage = pawn.DevelopmentalStage;
}

Expand Down Expand Up @@ -664,6 +673,20 @@ public void SetOriginalSkillLevel(SkillDef def, int value) {
originalSkillLevels[def] = value;
}

private CustomXenotype customXenotype = null;
private XenotypeDef xenotype = null;

public CustomXenotype CustomXenotype {
get {
return customXenotype;
}
}
public XenotypeDef Xenotype {
get {
return xenotype;
}
}

public NameTriple Name {
get {
return pawn.Name as NameTriple;
Expand Down Expand Up @@ -1478,7 +1501,7 @@ public string ResetCachedIncapableOf() {
List<string> incapableList = new List<string>();
WorkTags combinedDisabledWorkTags = pawn.story.DisabledWorkTagsBackstoryAndTraits;
if (combinedDisabledWorkTags != WorkTags.None) {
IEnumerable<WorkTags> list = Reflection.CharacterCardUtility.WorkTagsFrom(combinedDisabledWorkTags);
IEnumerable<WorkTags> list = Reflection.ReflectorCharacterCardUtility.WorkTagsFrom(combinedDisabledWorkTags);
foreach (var tag in list) {
incapableList.Add(WorkTypeDefsUtility.LabelTranslated(tag).CapitalizeFirst());
}
Expand Down
19 changes: 17 additions & 2 deletions Source/ExtensionsPawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public static void ClearCachedDisabledSkillRecords(this Pawn pawn) {
if (pawn.skills != null && pawn.skills.skills != null) {
pawn.skills.Notify_SkillDisablesChanged();
}
Reflection.Pawn.ClearCachedDisabledWorkTypes(pawn);
Reflection.Pawn.ClearCachedDisabledWorkTypesPermanent(pawn);
Reflection.ReflectorPawn.ClearCachedDisabledWorkTypes(pawn);
Reflection.ReflectorPawn.ClearCachedDisabledWorkTypesPermanent(pawn);
}

public static void ClearCachedHealth(this Pawn pawn) {
Expand Down Expand Up @@ -163,5 +163,20 @@ public static void AssignToFaction(this Pawn pawn, Faction faction) {
field.SetValue(pawn, faction);
}

public static CustomXenotype MatchGenesToCustomXenotype(this Pawn pawn) {
if (!ModsConfig.BiotechActive) {
return null;
}
var customXenotypes = Reflection.ReflectorCharacterCardUtility.CustomXenotypes;
if (customXenotypes == null) {
return null;
}
foreach (CustomXenotype c in customXenotypes) {
if (GeneUtility.PawnIsCustomXenotype(pawn, c)) {
return c;
}
}
return null;
}
}
}
2 changes: 1 addition & 1 deletion Source/Injury.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public override void AddToPawn(CustomPawn customPawn, Pawn pawn) {
HediffComp_GetsPermanent getsPermanent = hediff.TryGetComp<HediffComp_GetsPermanent>();
if (getsPermanent != null) {
getsPermanent.IsPermanent = true;
Reflection.HediffComp_GetsPermanent.SetPainCategory(getsPermanent, PainCategoryForFloat(painFactor == null ? 0 : painFactor.Value));
Reflection.ReflectorHediffComp_GetsPermanent.SetPainCategory(getsPermanent, PainCategoryForFloat(painFactor == null ? 0 : painFactor.Value));
}

pawn.health.AddHediff(hediff, BodyPartRecord);
Expand Down
6 changes: 3 additions & 3 deletions Source/PanelRandomize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void DrawForBiotech(State state) {
texture = state.CurrentPawn.RandomizeXenotype.Icon;
}
else if (state.CurrentPawn.RandomizeCustomXenotype != null) {
texture = state.CurrentPawn.RandomizeCustomXenotype.IconDef.Icon;
texture = state.CurrentPawn.RandomizeCustomXenotype.IconDef?.Icon;
}
else if (state.CurrentPawn.RandomizeAnyNonArchite) {
texture = Textures.TextureButtonRandom;
Expand All @@ -112,6 +112,7 @@ public void DrawForBiotech(State state) {
}));
})
};

foreach (XenotypeDef item in DefDatabase<XenotypeDef>.AllDefs.OrderBy((XenotypeDef x) => 0f - x.displayPriority)) {
XenotypeDef xenotype = item;
list.Add(new FloatMenuOption(xenotype.LabelCap, delegate
Expand All @@ -126,14 +127,13 @@ public void DrawForBiotech(State state) {
},
null, 24f, (Rect r) => Widgets.InfoCardButton(r.x, r.y + 3f, xenotype) ? true : false, null, playSelectionSound: true, 0, HorizontalJustification.Left, extraPartRightJustified: true));
}

var customXenotypes = ReflectionUtil.GetStaticPropertyValue<List<CustomXenotype>>(typeof(CharacterCardUtility), "CustomXenotypes");
if (customXenotypes != null) {
foreach (CustomXenotype customXenotype in customXenotypes) {
CustomXenotype customInner = customXenotype;
list.Add(new FloatMenuOption(customInner.name.CapitalizeFirst() + " (" + "Custom".Translate() + ")", delegate
{
Logger.Debug("Customized Xenotype, XenotypeDef: " + state.CurrentPawn.Pawn.genes.Xenotype?.defName);
Logger.Debug("Customized Xenotype: " + state.CurrentPawn.Pawn.genes.CustomXenotype?.name);
state.CurrentPawn.RandomizeCustomXenotype = customInner;
state.CurrentPawn.RandomizeXenotype = null;
state.CurrentPawn.RandomizeAnyNonArchite = false;
Expand Down
12 changes: 6 additions & 6 deletions Source/PanelXenotype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public override float Draw(State state, float y) {
CustomPawn pawn = state.CurrentPawn;
Pawn_GeneTracker geneTracker = pawn.Pawn.genes;
XenotypeDef xenotypeDef = geneTracker?.Xenotype;
CustomXenotype customXenotype = geneTracker?.CustomXenotype;
CustomXenotype customXenotype = pawn.CustomXenotype;

FieldXenotype.Rect = FieldRect.OffsetBy(0, y);
labelTrimmer.Rect = FieldXenotype.Rect.InsetBy(8, 0);

if (customXenotype != null) {
FieldXenotype.Label = labelTrimmer.TrimLabelIfNeeded(customXenotype.name);
FieldXenotype.Label = customXenotype.name != null ? labelTrimmer.TrimLabelIfNeeded(customXenotype.name.CapitalizeFirst()) : "";
}
else if (xenotypeDef != null) {
FieldXenotype.Label = labelTrimmer.TrimLabelIfNeeded(xenotypeDef.LabelCap);
Expand All @@ -54,12 +54,12 @@ public override float Draw(State state, float y) {
Find.WindowStack.Add(new Dialog_ViewGenes(pawn.Pawn));
};
FieldXenotype.DrawIconFunc = (Rect rect) => {
if (xenotypeDef != null) {
GUI.DrawTexture(rect, xenotypeDef.Icon);
}
else if (customXenotype != null) {
if (customXenotype != null) {
GUI.DrawTexture(rect, customXenotype.IconDef?.Icon);
}
else if (xenotypeDef != null) {
GUI.DrawTexture(rect, xenotypeDef.Icon);
}
};
FieldXenotype.IconSizeFunc = () => new Vector2(24, 24);

Expand Down
6 changes: 3 additions & 3 deletions Source/PawnBioGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public static bool TryGetRandomUnusedSolidBioFor(List<BackstoryCategoryFilter> b
});
// Settle for a default category filter if none was chosen.
if (categoryFilter == null) {
categoryFilter = Reflection.PawnBioAndNameGenerator.GetFallbackCategoryGroup();
categoryFilter = Reflection.ReflectorPawnBioAndNameGenerator.GetFallbackCategoryGroup();
}
// Choose a weighted bio.
return (from bio in SolidBioDatabase.allBios.TakeRandom(20)
where Reflection.PawnBioAndNameGenerator.IsBioUseable(bio, categoryFilter, kind, gender, requiredLastName)
select bio).TryRandomElementByWeight(new Func<PawnBio, float>(Reflection.PawnBioAndNameGenerator.BioSelectionWeight), out result);
where Reflection.ReflectorPawnBioAndNameGenerator.IsBioUseable(bio, categoryFilter, kind, gender, requiredLastName)
select bio).TryRandomElementByWeight(new Func<PawnBio, float>(Reflection.ReflectorPawnBioAndNameGenerator.BioSelectionWeight), out result);
}
}
}
2 changes: 1 addition & 1 deletion Source/PrepareCarefully.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private static PawnKindDef RandomPet(ScenPart_StartingAnimal startingAnimal) {
return animalKindDef;
}
if (animalKindDef == null) {
IEnumerable<PawnKindDef> animalKindDefs = Reflection.ScenPart_StartingAnimal.RandomPets(startingAnimal);
IEnumerable<PawnKindDef> animalKindDefs = Reflection.ReflectorScenPart_StartingAnimal.RandomPets(startingAnimal);
if (animalKindDefs != null) {
var enumerator = animalKindDefs.GetEnumerator();
if (enumerator != null) {
Expand Down
2 changes: 1 addition & 1 deletion Source/PresetFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class PresetFiles {
public static string SavedPresetsFolderPath {
get {
try {
return Reflection.GenFilePaths.FolderUnderSaveData("PrepareCarefully");
return Reflection.ReflectorGenFilePaths.FolderUnderSaveData("PrepareCarefully");
}
catch (Exception e) {
Logger.Error("Failed to get preset save directory");
Expand Down
2 changes: 1 addition & 1 deletion Source/PresetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static bool LoadFromFile(PrepareCarefully loadout, string presetName) {
public static void ClearSaveablesAndCrossRefs() {
// I don't fully understand how these cross-references and saveables are resolved, but
// if we don't clear them out, we get null pointer exceptions.
Reflection.PostLoadIniter.ClearSaveablesToPostLoad(Scribe.loader.initer);
Reflection.ReflectorPostLoadIniter.ClearSaveablesToPostLoad(Scribe.loader.initer);
if (Scribe.loader.crossRefs.crossReferencingExposables != null) {
Scribe.loader.crossRefs.crossReferencingExposables.Clear();
}
Expand Down
2 changes: 1 addition & 1 deletion Source/ProviderHealthOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ protected void InitializeInjuryOptions(OptionsHealth options, ThingDef pawnThing
// Get all of the hediffs that can be added via the "forced hediff" scenario part and
// add them to a hash set so that we can quickly look them up.
ScenPart_ForcedHediff scenPart = new ScenPart_ForcedHediff();
IEnumerable<HediffDef> scenPartDefs = Reflection.ScenPart_ForcedHediff.PossibleHediffs(scenPart);
IEnumerable<HediffDef> scenPartDefs = Reflection.ReflectorScenPart_ForcedHediff.PossibleHediffs(scenPart);
HashSet<HediffDef> scenPartDefSet = new HashSet<HediffDef>(scenPartDefs);

// Add injury options.
Expand Down
11 changes: 8 additions & 3 deletions Source/Randomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ public Pawn GeneratePawnAsCloseToAsPossible(Pawn pawn) {
FixedChronologicalAge = pawn.ageTracker.AgeChronologicalYears,
FixedGender = pawn.gender,
FixedIdeology = pawn.Ideo,
DevelopmentalStage = pawn.DevelopmentalStage,
ForcedXenotype = pawn.genes?.Xenotype,
ForcedCustomXenotype = pawn.genes?.CustomXenotype
DevelopmentalStage = pawn.DevelopmentalStage
};
CustomXenotype customXenotype = pawn.MatchGenesToCustomXenotype();
if (customXenotype != null) {
request.ForcedCustomXenotype = customXenotype;
}
else {
request.ForcedXenotype = pawn.genes?.Xenotype;
}
Pawn result = AttemptToGeneratePawn(request.Request);
return result;
}
Expand Down
30 changes: 15 additions & 15 deletions Source/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,41 @@

namespace EdB.PrepareCarefully {
namespace Reflection {
public static class CharacterCardUtility {
public static class ReflectorCharacterCardUtility {
public static IEnumerable<WorkTags> WorkTagsFrom(WorkTags workTags) {
return (IEnumerable<WorkTags>)ReflectionCache.Instance.CharacterCardUtility_WorkTagsFrom.Invoke(null, new object[] { workTags });
}
public static List<CustomXenotype> CustomXenotypes {
get {
return ReflectionUtil.GetStaticPropertyValue<List<CustomXenotype>>(typeof(CharacterCardUtility), "CustomXenotypes");
}
}
}
public static class ScenPart_StartingAnimal {

public static class ReflectorScenPart_StartingAnimal {
public static IEnumerable<PawnKindDef> RandomPets(RimWorld.ScenPart_StartingAnimal scenPart) {
return (IEnumerable<PawnKindDef>)ReflectionCache.Instance.ScenPart_StartingAnimal_RandomPets.Invoke(scenPart, null);
}
}
public static class GenFilePaths {
public static class ReflectorGenFilePaths {
public static string FolderUnderSaveData(string name) {
return (string)ReflectionCache.Instance.GenFilePaths_FolderUnderSaveData.Invoke(null, new object[] { name });
}
}
public static class ScenPart_ForcedHediff {
public static IEnumerable<HediffDef> PossibleHediffs(RimWorld.ScenPart_ForcedHediff scenPart) {
public static class ReflectorScenPart_ForcedHediff {
public static IEnumerable<HediffDef> PossibleHediffs(ScenPart_ForcedHediff scenPart) {
return (IEnumerable<HediffDef>)ReflectionCache.Instance.ScenPart_ForcedHediff_PossibleHediffs.Invoke(scenPart, null);
}
}
//public static class GraphicDatabaseHeadRecords {
// public static void BuildDatabaseIfNecessary() {
// ReflectionCache.Instance.GraphicDatabaseHeadRecords_BuildDatabaseIfNecessary.Invoke(null, null);
// }
//}
public static class Pawn {
public static class ReflectorPawn {
public static void ClearCachedDisabledWorkTypes(Verse.Pawn pawn) {
ReflectionCache.Instance.Pawn_CachedDisabledWorkTypes.SetValue(pawn, null);
}
public static void ClearCachedDisabledWorkTypesPermanent(Verse.Pawn pawn) {
ReflectionCache.Instance.Pawn_CachedDisabledWorkTypesPermanent.SetValue(pawn, null);
}
}
public static class PawnBioAndNameGenerator {
public static class ReflectorPawnBioAndNameGenerator {
public static float BioSelectionWeight(PawnBio b) {
return (float)ReflectionCache.Instance.PawnBioAndNameGenerator_BioSelectionWeight.Invoke(null,
new object[] { b }
Expand Down Expand Up @@ -71,16 +72,15 @@ public static List<string> GetTmpNames() {
return (List<string>)ReflectionCache.Instance.PawnBioAndNameGenerator_tmpNames.GetValue(null);
}
}
public static class PostLoadIniter {
public static class ReflectorPostLoadIniter {
public static void ClearSaveablesToPostLoad(Verse.PostLoadIniter initer) {
HashSet<IExposable> saveables = (HashSet<IExposable>)ReflectionCache.Instance.PostLoadIniter_SaveablesToPostLoad.GetValue(Scribe.loader.initer);
if (saveables != null) {
saveables.Clear();
}
}
}

public static class HediffComp_GetsPermanent {
public static class ReflectorHediffComp_GetsPermanent {
public static void SetPainCategory(Verse.HediffComp_GetsPermanent comp, PainCategory painCategory) {
ReflectionCache.Instance.HediffComp_GetsPermanent_PainCategory.SetValue(comp, painCategory);
}
Expand Down

0 comments on commit 25f78ca

Please sign in to comment.