Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/ContractConfigurator/ContractConfigurator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<Compile Include="Behaviour\UnlockPart.cs" />
<Compile Include="Behaviour\UnlockTech.cs" />
<Compile Include="HarmonyPatcher.cs" />
<Compile Include="Harmony\Contract.cs" />
<Compile Include="Harmony\Kerbal.cs" />
<Compile Include="Harmony\ContractSystem.cs" />
<Compile Include="ScenarioModules\ScienceReporter.cs" />
Expand Down
33 changes: 33 additions & 0 deletions source/ContractConfigurator/Harmony/Contract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Contracts;
using HarmonyLib;
using System;
using System.Collections;
using System.Threading.Tasks;

namespace ContractConfigurator.Harmony
{
[HarmonyPatch(typeof(Contract))]
internal class PatchContract
{
/// <summary>
/// KSP's ContractSystem calls Contract.Generate() with State.Generated when filling the offered contracts.
/// ConfiguredContract.Generate() always returns false for these calls (contractType is unset on fresh instances) so each attempt is a wasted allocation.
/// Short-circuit here before Activator.CreateInstance is reached. The pre-loader uses State.Withdrawn, which is allowed through.
/// </summary>
/// <param name="__result"></param>
/// <param name="contractType"></param>
/// <param name="state"></param>
/// <returns></returns>
[HarmonyPrefix]
[HarmonyPatch("Generate", new Type[] { typeof(Type), typeof(Contract.ContractPrestige), typeof(int), typeof(Contract.State) })]
internal static bool Prefix_Generate(ref Contract __result, Type contractType, Contract.State state)
{
if (contractType == typeof(ConfiguredContract) && state == Contract.State.Generated)
{
__result = null;
return false;
}
return true;
}
}
}
33 changes: 33 additions & 0 deletions source/ContractConfigurator/Harmony/ContractSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,39 @@ internal static void PostfixAction(ConfigNode gameNode)
}
}

/// <summary>
/// Make KSP count pre-loaded CC contracts toward the offered-contract quota
/// </summary>
/// <param name="__result"></param>
/// <param name="difficulty"></param>
[HarmonyPostfix]
[HarmonyPatch("CountContracts")]
internal static void Postfix_CountContracts(ref int __result, Contract.ContractPrestige difficulty)
{
if (ContractPreLoader.Instance == null) return;
foreach (ConfiguredContract c in ContractPreLoader.Instance.PendingContracts())
{
if (c.Prestige == difficulty)
__result++;
}
}

/// <summary>
/// Skip weighted random selection when ConfiguredContract is the only registered contract type.
/// </summary>
[HarmonyPrefix]
[HarmonyPatch("WeightedContractChoice")]
internal static bool Prefix_WeightedContractChoice(ref Type __result)
{
if (ContractSystem.ContractTypes?.Count == 1 &&
ContractSystem.ContractTypes[0] == typeof(ConfiguredContract))
{
__result = typeof(ConfiguredContract);
return false;
}
return true;
}

private class PostfixEnumerator : IEnumerable
{
public IEnumerator enumerator;
Expand Down
14 changes: 10 additions & 4 deletions source/ContractConfigurator/ScenarioModules/ContractPreLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ContractPreLoader : ScenarioModule
private static System.Random rand = new System.Random();
private static int nextContractGroup = rand.Next();

private List<ConfiguredContract> contracts = new List<ConfiguredContract>();
private readonly List<ConfiguredContract> contracts = new List<ConfiguredContract>();

private string lastKey = null;
private double lastGenerationFailure;
Expand Down Expand Up @@ -376,7 +376,7 @@ public override void OnSave(ConfigNode node)
{
try
{
foreach (ConfiguredContract contract in contracts.Where(c => c.ContractState == Contract.State.Offered))
foreach (ConfiguredContract contract in contracts)
{
ConfigNode child = new ConfigNode("CONTRACT");
node.AddNode(child);
Expand Down Expand Up @@ -436,9 +436,15 @@ public override void OnLoad(ConfigNode node)
}
}

public IEnumerable<ConfiguredContract> PendingContracts()
/// <summary>
/// Contracts that have been generated but not yet accepted. Not all of may be presented to the player.
/// Do not edit the returned collection directly.
/// </summary>
/// <returns></returns>
public List<ConfiguredContract> PendingContracts()
{
return contracts.Where(c => c.ContractState == Contract.State.Offered);
// contracts only ever holds State.Offered contracts
return contracts;
}
}
}
Loading