This commit is contained in:
CGFighter
2022-10-20 00:20:00 +03:00
parent 30b37db421
commit 575b4a8440
16 changed files with 491 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
using System.Linq;
using System.Collections.Generic;
using HarmonyLib;
using RimWorld;
using Verse;
using Verse.Sound;
namespace ResearchWhatever.Patches
{
[HarmonyPatch(typeof(ResearchManager), "FinishProject")]
public static class ResearchManager_FinishProject_ResearchWhateverPatch
{
public static void Prefix(ResearchManager __instance, ref bool doCompletionDialog)
{
if (doCompletionDialog)
{
var comp = Current.Game.GetComponent<ResearchWhateverGameComp>();
if (comp.NotifyMode == ResearchWhateverNotifyMode.rwnDefault)
return;
doCompletionDialog = false;
switch (comp.NotifyMode)
{
case ResearchWhateverNotifyMode.rwnLetter:
Find.LetterStack.ReceiveLetter("ResearchFinished".Translate(__instance.currentProj.LabelCap), __instance.currentProj.description, LetterDefOf.PositiveEvent, null, null, null, null, null);
break;
case ResearchWhateverNotifyMode.rwnNotice:
Messages.Message("ResearchFinished".Translate(__instance.currentProj.LabelCap).CapitalizeFirst(), MessageTypeDefOf.SilentInput);
break;
default:
break;
}
}
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.Linq;
using System.Collections.Generic;
using Verse;
using HarmonyLib;
using System.Reflection;
using RimWorld;
namespace ResearchWhatever
{
public static class StaticConstructorOnStartupUtility_Patch
{
//[HarmonyPatch(typeof(StaticConstructorOnStartupUtility), "CallAll")]
[HarmonyPatch]
public static class StaticConstructorOnStartupUtility_CallAll_ResearchWhateverPatch
{
internal static MethodBase TargetMethod()
{
MethodBase LCallAll = AccessTools.Method("BetterLoading.Stage.InitialLoad.StageRunStaticCctors:PreCallAll");
if (LCallAll == null)
{
LCallAll = AccessTools.Method("Verse.StaticConstructorOnStartupUtility:CallAll");
if (LCallAll == null)
throw new Exception("Couldn't find StaticConstructorOnStartupUtility.CallAll()");
}
else
Log.Message("[ResearchWhatever] BetterLoading detected, workaround initiated");
return LCallAll;
}
//
public static void Postfix()
{
List<ThingDef> list = new List<ThingDef>(
from x in DefDatabase<ThingDef>.AllDefsListForReading
where x.thingClass == typeof(Building_ResearchBench) || x.thingClass != null && x.thingClass.IsSubclassOf(typeof(Building_ResearchBench))
select x);
if (list.NullOrEmpty())
return;
else
foreach (var thing in list)
{
thing?.comps?.Add(new CompProperties(typeof(ResearchWhateverComp)));
}
}
}
}
}

View File

@@ -0,0 +1,83 @@
using System.Linq;
using System.Collections.Generic;
using HarmonyLib;
using RimWorld;
using Verse;
using Verse.Sound;
namespace ResearchWhatever.Patches
{
[HarmonyPatch(typeof(WorkGiver_Researcher), "ShouldSkip")]
public static class WorkGiver_Researcher_ShouldSkip_ResearchWhateverPatch
{
public static bool Prefix(ref bool __result)
{
__result = false;
return false;
}
}
[HarmonyPatch(typeof(WorkGiver_Researcher), "PotentialWorkThingRequest", MethodType.Getter)]
public static class WorkGiver_PotentialWorkThingRequest_ResearchWhateverPatch
{
public static bool Prefix(ref ThingRequest __result)
{
__result = ThingRequest.ForGroup(ThingRequestGroup.ResearchBench);
return false;
}
}
[HarmonyPatch(typeof(WorkGiver_Researcher), "HasJobOnThing")]
public static class WorkGiver_Researcher_HasJobOnThing_ResearchWhateverPatch
{
private static bool hasFacilities(this Building_ResearchBench bench, List<ThingDef> requiredFacilities)
{
if (requiredFacilities.NullOrEmpty()) return true;
CompAffectedByFacilities comp = bench.TryGetComp<CompAffectedByFacilities>();
if (comp == null) return false;
foreach (var rf in requiredFacilities)
if (comp.LinkedFacilitiesListForReading.FirstOrDefault(x => x.def == rf && comp.IsFacilityActive(x)) == null)
return false;
return true;
}
public static void Prefix(Pawn pawn, Thing t, bool forced)
{
ResearchProjectDef currentProj = Find.ResearchManager.currentProj;
if (currentProj != null) return;
Building_ResearchBench bench = t as Building_ResearchBench;
if (bench == null) return;
ResearchWhateverComp comp = bench.TryGetComp<ResearchWhateverComp>();
if (comp == null || !comp.Active) return;
List<ResearchProjectDef> projects = new List<ResearchProjectDef>(
from x in DefDatabase<ResearchProjectDef>.AllDefsListForReading
where Find.Storyteller.difficulty.AllowedBy(x.hideWhen)
&& !x.IsFinished
&& x.TechprintRequirementMet
&& x.PrerequisitesCompleted
&& (x.requiredResearchBuilding == null || x.requiredResearchBuilding == bench.def && bench.hasFacilities(x.requiredResearchFacilities))
&& x.GetModExtension<ResearchWhateverExtansion>()?.ignore != true
select x);
if (projects.NullOrEmpty())
{
comp.Active = false;
Messages.Message("ResearchWhateverNothingLeftToResearch".Translate(bench.Label).CapitalizeFirst(), new TargetInfo(bench.Position, bench.Map, false), MessageTypeDefOf.NeutralEvent);
return;
}
projects.SortBy(x => x.GetModExtension<ResearchWhateverExtansion>()?.lowPriority == true ? 100000000f + x.CostApparent : x.CostApparent);
ResearchProjectDef def = projects.First();
projects.TryRandomElementByWeight(x => x.CostApparent == def.CostApparent ? 1f : 0f, out def);
SoundDefOf.ResearchStart.PlayOneShotOnCamera(null);
Find.ResearchManager.currentProj = def;
TutorSystem.Notify_Event("StartResearchProject");
Messages.Message( "ResearchWhateverNewResearch".Translate(pawn.Name.ToStringFull, def.label).CapitalizeFirst(), new TargetInfo(bench.Position, bench.Map, false), MessageTypeDefOf.SilentInput);
}
}
}