Skip to content

Commit

Permalink
Added UI and preset support for the extra passions from Vanilla Skills
Browse files Browse the repository at this point in the history
Expanded
  • Loading branch information
edbmods committed May 22, 2024
1 parent 156e22b commit da9358b
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 29 deletions.
1 change: 1 addition & 0 deletions EdBPrepareCarefully.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<Compile Include="Source\OptionsHealth.cs" />
<Compile Include="Source\HarmonyPatches.cs" />
<Compile Include="Source\PawnGenerationRequestWrapper.cs" />
<Compile Include="Source\ProviderPassions.cs" />
<Compile Include="Source\ProviderPawnKinds.cs" />
<Compile Include="Source\PawnLayer.cs" />
<Compile Include="Source\PawnLayerAlienAddon.cs" />
Expand Down
1 change: 1 addition & 0 deletions Resources/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Version 1.5.12
_____________________________________________________________________________

- Added support for the additional passions in Vanilla Skills Expanded
- Bug fixes:
- Fixed problems with temporary pawns in the Relationships tab

Expand Down
12 changes: 5 additions & 7 deletions Source/ControllerTabViewPawns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ControllerTabViewPawns {
public PawnCustomizer Customizer { get; set; }
public ManagerPawns PawnManager { get; set; }
public ManagerRelationships RelationshipManager { get; set; }
public ProviderPassions ProviderPassions { get; set; }

protected Dictionary<Type, PawnLayerOptionUpdatedHandler> PawnLayerOptionUpdateHandlers { get; set; } = new Dictionary<Type, PawnLayerOptionUpdatedHandler>();
protected Dictionary<Type, PawnLayerColorUpdatedHandler> PawnLayerColorUpdateHandlers { get; set; } = new Dictionary<Type, PawnLayerColorUpdatedHandler>();
Expand Down Expand Up @@ -251,14 +252,11 @@ public void AdjustSkillPassion(SkillDef skill, int direction) {
}
Passion currentPassion = record.passion;
Passion nextPassion = currentPassion;
if (currentPassion == Passion.None) {
nextPassion = direction > 0 ? Passion.Minor : Passion.Major;
if (direction > 0) {
nextPassion = ProviderPassions.NextPassionValue(currentPassion);
}
else if (currentPassion == Passion.Minor) {
nextPassion = direction > 0 ? Passion.Major : Passion.None;
}
else if (currentPassion == Passion.Major) {
nextPassion = direction > 0 ? Passion.None : Passion.Minor;
else {
nextPassion = ProviderPassions.PreviousPassionValue(currentPassion);
}
PawnManager.UpdatePawnSkillPassion(customizedPawn, skill, nextPassion);
}
Expand Down
12 changes: 9 additions & 3 deletions Source/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public void Start(Page_ConfigureStartingPawns configureStartingPawnsPage) {
};
providerEquipmentTypes.PostConstruction();
var providerPawnKinds = new ProviderPawnKinds();
var providerPassions = new ProviderPassions();
providerPassions.PostConstruct();

var ageModifier = new AgeModifier();

Expand All @@ -94,7 +96,8 @@ public void Start(Page_ConfigureStartingPawns configureStartingPawnsPage) {
ProviderHealthOptions = providerHealthOptions,
};
var pawnLoaderV5 = new PawnLoaderV5() {
ProviderHealthOptions = providerHealthOptions
ProviderHealthOptions = providerHealthOptions,
ProviderPassions = providerPassions,
};
var pawnLoader = new PawnLoader() {
PawnLoaderV3 = pawnLoaderV3,
Expand All @@ -121,7 +124,8 @@ public void Start(Page_ConfigureStartingPawns configureStartingPawnsPage) {
PresetLoaderV5 = presetLoaderV5,
};
var pawnSaver = new PawnSaver() {
ProviderHealthOptions = providerHealthOptions
ProviderHealthOptions = providerHealthOptions,
ProviderPassions = providerPassions,
};
var presetSaver = new PresetSaver() {
PawnSaver = pawnSaver
Expand Down Expand Up @@ -164,6 +168,7 @@ public void Start(Page_ConfigureStartingPawns configureStartingPawnsPage) {
Customizer = pawnCustomizer,
PawnManager = pawnManager,
RelationshipManager = relationshipManager,
ProviderPassions = providerPassions,
};
var controllerRelationships = new ControllerTabViewRelationships() {
State = state,
Expand Down Expand Up @@ -277,7 +282,8 @@ public void Start(Page_ConfigureStartingPawns configureStartingPawnsPage) {
};
var skillsPanel = new PanelSkills() {
State = state,
ViewState = viewState
ViewState = viewState,
ProviderPassions = providerPassions,
};
var incapableOfPanel = new PanelIncapableOf() {
State = state,
Expand Down
15 changes: 2 additions & 13 deletions Source/PanelSkills.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class PanelSkills : PanelBase {
protected WidgetScrollViewVertical scrollView = new WidgetScrollViewVertical();
public ModState State { get; set; }
public ViewState ViewState { get; set; }
public ProviderPassions ProviderPassions { get; set; }

public PanelSkills() {
}
Expand Down Expand Up @@ -219,19 +220,7 @@ public Texture2D GetPassionTextureForSkill(SkillRecord skillRecord) {
if (skillRecord == null) {
return null;
}
Passion passion = skillRecord.passion;
if (passion == Passion.Minor) {
return Textures.TexturePassionMinor;
}
else if (passion == Passion.Major) {
return Textures.TexturePassionMajor;
}
else if (passion == Passion.None) {
return Textures.TexturePassionNone;
}
else {
return null;
}
return ProviderPassions.TextureForPassion(skillRecord.passion);
}

public static void FillableBar(Rect rect, float fillPercent, Texture2D fillTex) {
Expand Down
6 changes: 4 additions & 2 deletions Source/PawnLoaderV5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace EdB.PrepareCarefully {
public class PawnLoaderV5 {
public ProviderHealthOptions ProviderHealthOptions { get; set; }
public ProviderPassions ProviderPassions { get; set; }

// Maintain lists of definitions that were replaced in newer versions of the game.
public Dictionary<string, string> thingDefReplacements = new Dictionary<string, string>();
Expand Down Expand Up @@ -534,12 +535,13 @@ public PawnLoaderResult ConvertSaveRecordToCustomizedPawn(SaveRecordPawnV5 recor
result.AddWarning("Could not load skill definition \"" + skill.name + "\" from saved preset");
continue;
}
Passion passion = ProviderPassions.MapFromString(skill.passion);
customizations.Skills.Add(new CustomizationsSkill() {
SkillDef = def,
Level = skill.value,
OriginalLevel = skill.value,
Passion = skill.passion,
OriginalPassion = skill.passion,
Passion = passion,
OriginalPassion = passion,
}
);
}
Expand Down
3 changes: 2 additions & 1 deletion Source/PawnSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace EdB.PrepareCarefully {
public class PawnSaver {
public ProviderHealthOptions ProviderHealthOptions { get; set; }
public ProviderPassions ProviderPassions { get; set; }

public void SaveToFile(CustomizedPawn customizedPawn, string colonistName) {
if (customizedPawn?.Customizations == null) {
Expand Down Expand Up @@ -233,7 +234,7 @@ public void ConvertSkills(SaveRecordPawnV5 result, CustomizationsPawn customizat
result.skills.Add(new SaveRecordSkillV4() {
name = skill.SkillDef?.defName,
value = skill.Level,
passion = skill.Passion
passion = ProviderPassions.MapToString(skill.Passion),
});
}
}
Expand Down
166 changes: 166 additions & 0 deletions Source/ProviderPassions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RimWorld;
using UnityEngine;
using Verse;

namespace EdB.PrepareCarefully {
public class ProviderPassions {
public Type PassionManagerType { get; set; }
public List<Texture2D> Icons { get; set; } = new List<Texture2D>();
public List<Def> PassionDefs { get; set; } = new List<Def>();
public List<int> NextValues { get; set; } = new List<int>() { 1, 2, 0 };
public List<int> PreviousValues { get; set; } = new List<int>() { 2, 0, 1 };
public int PassionCount { get; set; } = 3;
public void PostConstruct() {
InitializeExtendedPassions();
}
public bool UsingExtendedPassions {
get {
return PassionManagerType != null;
}
}
public void InitializeExtendedPassions() {
//Logger.Debug("InitializeExtendedPassions()");
PassionManagerType = ReflectionUtil.TypeByName("VSE.Passions.PassionManager");
if (PassionManagerType == null) {
//Logger.Debug(" Didn't find VSE.Passions.PassionManager class");
return;
}
//Logger.Debug(" Found VFE PassionManager class");
Array passionDefArray = ReflectionUtil.GetStaticFieldValue<Array>(PassionManagerType, "Passions");
if (passionDefArray == null) {
Logger.Debug(" Didn't find PassionManager.Passions array");
return;
}
//Logger.Debug(" Found VFE PassionManager.Passions array with " + passionDefArray.Length + " items");
PassionCount = passionDefArray.Length;
for (int i = 0; i < PassionCount; i++) {
Texture2D icon = Textures.TexturePassionNone;
Def passionDef = passionDefArray.GetValue(i) as Def;
if (passionDef != null) {
if (passionDef.defName == "None") {
icon = Textures.TexturePassionNone;
}
else {
icon = ReflectionUtil.GetPropertyValue<Texture2D>(passionDef, "Icon");
}
if (icon != null) {
//Logger.Debug(" Found icon for passion " + i + ", " + passionDef.defName + ": " + (icon?.name ?? "no name"));
}
else {
//Logger.Debug(" Did not find icon for passion " + i + ", " + passionDef.defName);
}
}
else {
//Logger.Debug(" Did not find passion def for index " + i);
}
PassionDefs.Add(passionDef);
Icons.Add(icon);
}

if (PassionCount == 6
&& PassionDefs[0]?.defName == "None"
&& PassionDefs[1]?.defName == "Minor"
&& PassionDefs[2]?.defName == "Major"
&& PassionDefs[3]?.defName == "VSE_Apathy"
&& PassionDefs[4]?.defName == "VSE_Natural"
&& PassionDefs[5]?.defName == "VSE_Critical") {
NextValues = new List<int> {
1, 2, 5, 0, 3, 4
};
PreviousValues = new List<int> {
3, 0, 1, 4, 5, 2
};
}
else {
NextValues.Clear();
for (int i=0; i<PassionDefs.Count - 1; i++) {
NextValues.Add(i+1);
}
NextValues.Add(0);
PreviousValues.Clear();
PreviousValues.Add(PassionDefs.Count - 1);
for (int i = 1; i < PassionDefs.Count; i++) {
PreviousValues.Add(i-1);
}
}

}
public Texture2D TextureForPassion(Passion passion) {
if (PassionManagerType == null) {
if (passion == Passion.Minor) {
return Textures.TexturePassionMinor;
}
else if (passion == Passion.Major) {
return Textures.TexturePassionMajor;
}
else {
return Textures.TexturePassionNone;
}
}
else {
int value = (int)passion;
if (value >= 0 && value < Icons.Count) {
return Icons[value];
}
}
return Textures.TexturePassionNone;
}

public Passion NextPassionValue(Passion passion) {
int value = (int)passion;
if (value < 0 || value >= NextValues.Count) {
return Passion.None;
}
return (Passion)NextValues[value];
}

public Passion PreviousPassionValue(Passion passion) {
int value = (int)passion;
if (value < 0 || value >= PreviousValues.Count) {
return Passion.None;
}
return (Passion)PreviousValues[value];
}

public Passion MapFromString(string value) {
if (UsingExtendedPassions && PassionDefs.CountAllowNull() > 0) {
int index = PassionDefs.FindIndex(d => d.defName == value);
if (index != -1) {
return (Passion)index;
}
else {
return Passion.None;
}
}
else {
Passion? passion = null;
try {
passion = (Passion)Enum.Parse(typeof(Passion), value);
}
catch (Exception) { }
if (passion.HasValue) {
return passion.Value;
}
if (value == "VSE_Critical" || value == "VSE_Natural") {
return Passion.Major;
}
return Passion.None;
}
}

public string MapToString(Passion passion) {
int index = (int)passion;
if (UsingExtendedPassions && PassionDefs.CountAllowNull() > index) {
return PassionDefs[index].defName;
}
else {
return passion.ToString();
}
}
}
}
23 changes: 23 additions & 0 deletions Source/ReflectionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ public static T GetFieldValue<T>(object target, string name) {
return default(T);
}

public static T GetStaticFieldValue<T>(Type target, string name) {
if (target == null) {
Logger.Warning("Could not get value from static field {" + name + "} using reflection because the target type was null");
return default(T);
}
FieldInfo field = Field(target, name);
if (field == null) {
Logger.Warning("Could not get value from static field {" + name + "} using reflection because we could not find the field on the target type");
return default(T);
}
object o = field.GetValue(null);
if (o == null) {
return default(T);
}
if (typeof(T).IsAssignableFrom(o.GetType())) {
return (T)field.GetValue(target);
}
else {
Logger.Warning("Could not cast the value from static field {" + name + "} whose type is {" + o.GetType().FullName + "} to the specified type {" + typeof(T).FullName + "}");
}
return default(T);
}

public static T GetPropertyValue<T>(object target, string name) {
if (target == null) {
Logger.Warning("Could not get value from property {" + name + "} using reflection because the target was null");
Expand Down
6 changes: 3 additions & 3 deletions Source/Version4/SaveRecordSkillV4.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using RimWorld;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,12 +9,12 @@ namespace EdB.PrepareCarefully {
public class SaveRecordSkillV4 : IExposable {
public string name;
public int value;
public Passion passion;
public string passion;

public void ExposeData() {
Scribe_Values.Look<string>(ref this.name, "name", null, true);
Scribe_Values.Look<int>(ref this.value, "value", 0, true);
Scribe_Values.Look<Passion>(ref this.passion, "passion", Passion.None, true);
Scribe_Values.Look<string>(ref this.passion, "passion", "None", true);
}
}
}

0 comments on commit da9358b

Please sign in to comment.