Skip to content

Commit

Permalink
Backstories can be translated
Browse files Browse the repository at this point in the history
  • Loading branch information
erdelf committed Jun 17, 2018
1 parent a224083 commit 19ca658
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
Binary file modified Assemblies/AlienRace.dll
Binary file not shown.
38 changes: 20 additions & 18 deletions Source/AlienRace/AlienRace/BackstoryDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class BackstoryDef : Def
public IntRange bioAgeRange;
public IntRange chronoAgeRange;
public List<string> forcedItems = new List<string>();
public Backstory backstory;

public bool CommonalityApproved(Gender g) => Rand.Range(min: 0, max: 100) < (g == Gender.Female ? this.femaleCommonality : this.maleCommonality);

Expand All @@ -45,14 +46,10 @@ public override void ResolveReferences()
base.ResolveReferences();


if (!this.addToDatabase || BackstoryDatabase.allBackstories.ContainsKey(key: this.defName) || this.title.NullOrEmpty() || this.spawnCategories.NullOrEmpty())
{
return;
}
if (!this.addToDatabase || BackstoryDatabase.allBackstories.ContainsKey(key: this.defName) || this.title.NullOrEmpty() || this.spawnCategories.NullOrEmpty()) return;

Backstory b = new Backstory()
this.backstory = new Backstory
{
baseDesc = this.baseDescription.NullOrEmpty() ? "Empty." : this.baseDescription,
bodyTypeGlobal = this.bodyTypeGlobal,
bodyTypeFemale = this.bodyTypeFemale,
bodyTypeMale = this.bodyTypeMale,
Expand Down Expand Up @@ -82,24 +79,29 @@ public override void ResolveReferences()
})()
};

b.SetTitle(newTitle: this.title);
b.SetTitleShort(newTitleShort: this.titleShort.NullOrEmpty() ? b.Title : this.titleShort);

b.ResolveReferences();
b.PostLoad();
UpdateTranslateableFields(bs: this);

this.backstory.ResolveReferences();
this.backstory.PostLoad();

b.identifier = this.defName;
this.backstory.identifier = this.defName;

IEnumerable<string> errors;
if (!(errors = b.ConfigErrors(ignoreNoSpawnCategories: false)).Any())
{
BackstoryDatabase.AddBackstory(bs: b);
} else
{
if (!(errors = this.backstory.ConfigErrors(ignoreNoSpawnCategories: false)).Any())
BackstoryDatabase.AddBackstory(bs: this.backstory);
else
Log.Error(text: this.defName + " has errors:\n" + string.Join(separator: "\n", value: errors.ToArray()));
}
}

internal static void UpdateTranslateableFields(BackstoryDef bs)
{
bs.backstory.baseDesc = bs.baseDescription.NullOrEmpty() ? "Empty." : bs.baseDescription;
bs.backstory.SetTitle(newTitle: bs.title);
bs.backstory.SetTitleShort(newTitleShort: bs.titleShort.NullOrEmpty() ? bs.backstory.Title : bs.titleShort);
}



public struct BackstoryDefSkillListItem
{
#pragma warning disable CS0649
Expand Down
14 changes: 9 additions & 5 deletions Source/AlienRace/AlienRace/HarmonyPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ static HarmonyPatches()


{
//DateTime time = DateTime.Now;

FieldInfo bodyInfo = AccessTools.Field(type: typeof(RaceProperties), name: nameof(RaceProperties.body));
ILGenerator ilg = new DynamicMethod(name: "ScanMethod", returnType: typeof(int), parameterTypes: Type.EmptyTypes).GetILGenerator();
foreach (MethodInfo mi in LoadedModManager.RunningMods.Where(predicate: mcp => mcp.LoadedAnyAssembly)
Expand All @@ -256,13 +254,19 @@ static HarmonyPatches()
harmony.Patch(original: mi, prefix: null, postfix: null, transpiler: new HarmonyMethod(type: patchType, name: nameof(BodyReferenceTranspiler)));
}
}
Log.Message(
text:

Log.Message(text:
$"Alien race successfully completed {harmony.GetPatchedMethods().Select(selector: mb => harmony.GetPatchInfo(method: mb)).SelectMany(selector: p => p.Prefixes.Concat(second: p.Postfixes).Concat(second: p.Transpilers)).Count(predicate: p => p.owner == harmony.Id)} patches with harmony.");
DefDatabase<HairDef>.GetNamed(defName: "Shaved").hairTags.Add(item: "alienNoHair"); // needed because..... the original idea doesn't work and I spend enough time finding a good solution

foreach (BackstoryDef bd in DefDatabase<BackstoryDef>.AllDefs)
{
BackstoryDef.UpdateTranslateableFields(bs: bd);
}
}

public static IEnumerable<CodeInstruction> BodyReferenceTranspiler(IEnumerable<CodeInstruction> instructions) {
public static IEnumerable<CodeInstruction> BodyReferenceTranspiler(IEnumerable<CodeInstruction> instructions)
{
FieldInfo bodyInfo = AccessTools.Field(type: typeof(RaceProperties), name: nameof(RaceProperties.body));
FieldInfo propsInfo = AccessTools.Field(type: typeof(ThingDef), name: nameof(ThingDef.race));

Expand Down

0 comments on commit 19ca658

Please sign in to comment.