Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synced/fixed SoS2 allowed torpedo changing #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 35 additions & 1 deletion Source_Referenced/SaveOurShip2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using HarmonyLib;
using Multiplayer.API;
using RimWorld;
Expand Down Expand Up @@ -133,6 +135,9 @@ private static void LatePatch()
MpCompat.harmony.Patch(
MpMethodUtil.GetLambda(typeof(Command_VerbTargetShip), nameof(Command_VerbTargetShip.ProcessInput), lambdaOrdinal: 0),
prefix: new HarmonyMethod(typeof(SaveOurShip2), nameof(PreProcessInput)));

// Sync CompChangeableProjectilePlural so that changing allowed torpedo works
MP.RegisterSyncWorker<CompChangeableProjectilePlural>(SyncChangeableProjectile);
}

// Other buildings
Expand Down Expand Up @@ -367,5 +372,34 @@ private static void PostTryLaunch()
// TODO: Find a way to do it non-instantly that would work in a synced way
SaveShip.SaveShipAndRemoveItemStacks();
}

private static void SyncChangeableProjectile(SyncWorker sync, ref CompChangeableProjectilePlural comp)
{
if (sync.isWriting)
{
if (comp == null)
{
sync.Write<Thing>(null);
return;
}

var compEquippable = comp.parent.TryGetComp<CompEquippable>();

if (compEquippable.AllVerbs.Any())
{
var turretGun = compEquippable.AllVerbs.Select(v => v.caster).OfType<Building_ShipTurret>().FirstOrDefault(x => x.Map != null);
if (turretGun != null)
{
sync.Write(turretGun);
return;
}
}

throw new SerializationException("Couldn't save CompChangeableProjectile for thing " + comp.parent);
}

var parent = sync.Read<Building_ShipTurret>();
comp = (parent.gun as ThingWithComps).TryGetComp<CompChangeableProjectilePlural>();
}
}
}
}