Skip to content

Commit

Permalink
clean up LocalPlayer casts
Browse files Browse the repository at this point in the history
  • Loading branch information
hubastard committed Sep 2, 2021
1 parent 48a7bae commit 9a362da
Show file tree
Hide file tree
Showing 54 changed files with 174 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public override void ProcessPacket(SyncComplete packet, NebulaConnection conn)
{
if (!string.IsNullOrEmpty(s.overrideName))
{
player.SendPacket(new NameInputPacket(s.overrideName, s.id, NebulaModAPI.PLANET_NONE, ((LocalPlayer)Multiplayer.Session.LocalPlayer).Id));
player.SendPacket(new NameInputPacket(s.overrideName, s.id, NebulaModAPI.PLANET_NONE, Multiplayer.Session.LocalPlayer.Id));
}

foreach (PlanetData p in s.planets)
{
if (!string.IsNullOrEmpty(p.overrideName))
{
player.SendPacket(new NameInputPacket(p.overrideName, NebulaModAPI.STAR_NONE, p.id, ((LocalPlayer)Multiplayer.Session.LocalPlayer).Id));
player.SendPacket(new NameInputPacket(p.overrideName, NebulaModAPI.STAR_NONE, p.id, Multiplayer.Session.LocalPlayer.Id));
}
}
}
Expand All @@ -76,7 +76,7 @@ public override void ProcessPacket(SyncComplete packet, NebulaConnection conn)
// Everyone is now connected, we can safely spawn the player model of all the other players that are currently connected
foreach (var playerData in packet.AllPlayers)
{
if (playerData.PlayerId != ((LocalPlayer)Multiplayer.Session.LocalPlayer).Id)
if (playerData.PlayerId != Multiplayer.Session.LocalPlayer.Id)
{
Multiplayer.Session.World.SpawnRemotePlayerModel(playerData);
}
Expand Down
2 changes: 1 addition & 1 deletion NebulaNetwork/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void SaveServerData(string saveName)

//Add host's data
netDataWriter.Put(CryptoUtils.GetCurrentUserPublicKeyHash());
((LocalPlayer)Multiplayer.Session.LocalPlayer).Data.Serialize(netDataWriter);
Multiplayer.Session.LocalPlayer.Data.Serialize(netDataWriter);

File.WriteAllBytes(path, netDataWriter.Data);

Expand Down
2 changes: 1 addition & 1 deletion NebulaPatcher/Patches/Dynamic/ArriveLeavePlanet_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static bool ArrivePlanet_Prefix(GameData __instance, PlanetData planet)
// due to that we have a time window between the vanilla ArrivePlanet() setting the localPlanet and planetId values and
// our code loading the factory data.
// this results in weird planet jumps, so we need to delay this until the factory data is loaded into the game.
if (!Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
{
// if we are the server continue with vanilla logic
return true;
Expand Down
6 changes: 3 additions & 3 deletions NebulaPatcher/Patches/Dynamic/BuildTool_Common_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public static bool CreatePrebuilds_Prefix(BuildTool __instance)
if (Multiplayer.Session.LocalPlayer.IsHost)
{
int planetId = Multiplayer.Session.Factories.EventFactory?.planetId ?? GameMain.localPlanet?.id ?? -1;
Multiplayer.Session.Network.SendPacketToStar(new CreatePrebuildsRequest(planetId, previews, Multiplayer.Session.Factories.PacketAuthor == NebulaModAPI.AUTHOR_NONE ? ((LocalPlayer)Multiplayer.Session.LocalPlayer).Id : Multiplayer.Session.Factories.PacketAuthor, __instance.GetType().ToString()), GameMain.galaxy.PlanetById(planetId).star.id);
Multiplayer.Session.Network.SendPacketToStar(new CreatePrebuildsRequest(planetId, previews, Multiplayer.Session.Factories.PacketAuthor == NebulaModAPI.AUTHOR_NONE ? Multiplayer.Session.LocalPlayer.Id : Multiplayer.Session.Factories.PacketAuthor, __instance.GetType().ToString()), GameMain.galaxy.PlanetById(planetId).star.id);
}

//If client builds, he need to first send request to the host and wait for reply
if (!((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost && !Multiplayer.Session.Factories.IsIncomingRequest.Value)
if (!Multiplayer.Session.LocalPlayer.IsHost && !Multiplayer.Session.Factories.IsIncomingRequest.Value)
{
Multiplayer.Session.Network.SendPacket(new CreatePrebuildsRequest(GameMain.localPlanet?.id ?? -1, previews, Multiplayer.Session.Factories.PacketAuthor == NebulaModAPI.AUTHOR_NONE ? ((LocalPlayer)Multiplayer.Session.LocalPlayer).Id : Multiplayer.Session.Factories.PacketAuthor, __instance.GetType().ToString()));
Multiplayer.Session.Network.SendPacket(new CreatePrebuildsRequest(GameMain.localPlanet?.id ?? -1, previews, Multiplayer.Session.Factories.PacketAuthor == NebulaModAPI.AUTHOR_NONE ? Multiplayer.Session.LocalPlayer.Id : Multiplayer.Session.Factories.PacketAuthor, __instance.GetType().ToString()));
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion NebulaPatcher/Patches/Dynamic/DysonSwarm_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static bool RemoveOrbit_Prefix(DysonSwarm __instance, int orbitId)
public static void AddBullet_Postfix(DysonSwarm __instance, SailBullet bullet, int orbitId)
{
//Host is sending correction / authorization packet to correct constants of the generated bullet
if (Multiplayer.IsActive && ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost)
{
Multiplayer.Session.Network.SendPacket(new DysonSphereBulletCorrectionPacket(__instance.starData.index, bullet.id, bullet.uEndVel, bullet.uEnd));
}
Expand Down
2 changes: 1 addition & 1 deletion NebulaPatcher/Patches/Dynamic/EntitySignRenderer_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EntitySignRenderer_Patch
[HarmonyPatch(nameof(EntitySignRenderer.Init))]
public static void Init_Postfix()
{
if (!Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FactoryProductionStat_Patch
public static bool GameTick_Prefix(FactoryProductionStat __instance)
{
//Do not run in single player for host
if (!Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
{
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions NebulaPatcher/Patches/Dynamic/GalacticTransport_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GalacticTransport_Patch
[HarmonyPatch(nameof(GalacticTransport.SetForNewGame))]
public static void SetForNewGame_Postfix()
{
if (Multiplayer.IsActive && !((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (Multiplayer.IsActive && !Multiplayer.Session.LocalPlayer.IsHost)
{
Multiplayer.Session.Network.SendPacket(new ILSRequestgStationPoolSync());
}
Expand All @@ -21,7 +21,7 @@ public static void SetForNewGame_Postfix()
[HarmonyPatch(nameof(GalacticTransport.RemoveStationComponent))]
public static bool RemoveStationComponent_Prefix(GalacticTransport __instance, int gid)
{
return !Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost || Multiplayer.Session.Ships.PatchLockILS;
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.Ships.PatchLockILS;
}
}
}
6 changes: 3 additions & 3 deletions NebulaPatcher/Patches/Dynamic/GameData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@ public static void SetForNewGame_Postfix(GameData __instance)
//Set starting star and planet to request from the server
if (Multiplayer.IsActive && !Multiplayer.Session.LocalPlayer.IsHost)
{
if (((LocalPlayer)Multiplayer.Session.LocalPlayer).Data.LocalPlanetId != -1)
if (Multiplayer.Session.LocalPlayer.Data.LocalPlanetId != -1)
{
PlanetData planet = __instance.galaxy.PlanetById(((LocalPlayer)Multiplayer.Session.LocalPlayer).Data.LocalPlanetId);
PlanetData planet = __instance.galaxy.PlanetById(Multiplayer.Session.LocalPlayer.Data.LocalPlanetId);
__instance.ArrivePlanet(planet);
}
else
{
StarData nearestStar = null;
PlanetData nearestPlanet = null;
//Update player's position before searching for closest star
__instance.mainPlayer.uPosition = new VectorLF3(((LocalPlayer)Multiplayer.Session.LocalPlayer).Data.UPosition.x, ((LocalPlayer)Multiplayer.Session.LocalPlayer).Data.UPosition.y, ((LocalPlayer)Multiplayer.Session.LocalPlayer).Data.UPosition.z);
__instance.mainPlayer.uPosition = new VectorLF3(Multiplayer.Session.LocalPlayer.Data.UPosition.x, Multiplayer.Session.LocalPlayer.Data.UPosition.y, Multiplayer.Session.LocalPlayer.Data.UPosition.z);
GameMain.data.GetNearestStarPlanet(ref nearestStar, ref nearestPlanet);

if (nearestStar == null)
Expand Down
16 changes: 8 additions & 8 deletions NebulaPatcher/Patches/Dynamic/GameHistoryData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GameHistoryData_Patch
public static void SetForNewGame_Postfix()
{
// Do not run if it is not multiplayer and the player is not a client
if (!Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
{
return;
}
Expand Down Expand Up @@ -99,31 +99,31 @@ public static void ResumeTechQueue_Postfix()
public static bool UnlockRecipe_Prefix()
{
//Wait for the authoritative packet for unlocking recipes in multiplayer for clients
return !Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost || Multiplayer.Session.History.IsIncomingRequest;
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.History.IsIncomingRequest;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(GameHistoryData.UnlockTechFunction))]
public static bool UnlockTechFunction_Prefix()
{
//Wait for the authoritative packet for unlocking tech features in multiplayer for clients
return !Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost || Multiplayer.Session.History.IsIncomingRequest;
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.History.IsIncomingRequest;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(GameHistoryData.GainTechAwards))]
public static bool GainTechAwards_Prefix()
{
//Wait for the authoritative packet for gaining tech awards in multiplayer for clients
return !Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost || Multiplayer.Session.History.IsIncomingRequest;
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.History.IsIncomingRequest;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(GameHistoryData.AddTechHash))]
public static bool AddTechHash_Prefix(GameHistoryData __instance, long addcnt)
{
//Host in multiplayer can do normal research in the mecha
if (!Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
{
return true;
}
Expand All @@ -138,23 +138,23 @@ public static bool AddTechHash_Prefix(GameHistoryData __instance, long addcnt)
public static bool DequeueTech_Prefix()
{
///Wait for the authoritative packet for dequeing tech in multiplayer for clients
return !Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost || Multiplayer.Session.History.IsIncomingRequest;
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.History.IsIncomingRequest;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(GameHistoryData.UnlockTech))]
public static bool UnlockTech_Prefix()
{
//Wait for the authoritative packet for unlocking tech features in multiplayer for clients
return !Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost || Multiplayer.Session.History.IsIncomingRequest;
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost || Multiplayer.Session.History.IsIncomingRequest;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(GameHistoryData.RemoveTechInQueue))]
public static void RemoveTechInQueue_Prefix(int index, out int __state)
{
__state = GameMain.history.techQueue[index];
if (Multiplayer.IsActive && ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost)
{
//we need to know which itemtypes are currently needed for refunds, so trigger refund before cancelling own research
Multiplayer.Session.Network.PlayerManager.SendTechRefundPackagesToClients(__state);
Expand Down
6 changes: 3 additions & 3 deletions NebulaPatcher/Patches/Dynamic/GameSave_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ class GameSave_Patch
[HarmonyPatch(nameof(GameSave.SaveCurrentGame))]
public static bool SaveCurrentGame_Prefix(string saveName)
{
if (Multiplayer.IsActive && ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost)
{
SaveManager.SaveServerData(saveName);
}

// Only save if in single player or if you are the host
return (!Multiplayer.IsActive && !Multiplayer.IsLeavingGame) || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost;
return (!Multiplayer.IsActive && !Multiplayer.IsLeavingGame) || Multiplayer.Session.LocalPlayer.IsHost;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(GameSave.AutoSave))]
public static bool AutoSave_Prefix()
{
// Only save if in single player or if you are the host
return !Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost;
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost;
}

[HarmonyPrefix]
Expand Down
2 changes: 1 addition & 1 deletion NebulaPatcher/Patches/Dynamic/GameScenarioLogic_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void NotifyOnUnlockTech_Postfix(int techId)
{
//Synchronize unlocking techs
// Do not run if it is not multiplayer and if the player is not a client
if (!Multiplayer.IsActive || !((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (!Multiplayer.IsActive || !Multiplayer.Session.LocalPlayer.IsHost)
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion NebulaPatcher/Patches/Dynamic/GameStatData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GameStatData_Patch
[HarmonyPatch(nameof(GameStatData.AfterTick))]
public static void AfterTick_Postfix()
{
if (Multiplayer.IsActive && ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost)
{
Multiplayer.Session.Statistics.CaptureStatisticalSnapshot();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GuideMissionStandardMode_Patch
public static bool Skip_Prefix()
{
//This prevents spawning landing capsule and preparing spawn area for the clients in multiplayer.
return !Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost;
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost;
}
}
}
4 changes: 2 additions & 2 deletions NebulaPatcher/Patches/Dynamic/Mecha_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static bool Mecha_GenerateEnergy_Prefix(Mecha __instance)
// some players managed to break the fuel chamber on clients.
// the game thought there is still fuel burning while not adding energy to the mecha and preventing new fuel from beeing added.
// this checks for this corner case and resets the reactor energy to 0 (empty fuel chamber as displayed to the player)
if (!((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost && __instance.reactorEnergy > 0 && __instance.reactorItemId == 0)
if (!Multiplayer.Session.LocalPlayer.IsHost && __instance.reactorEnergy > 0 && __instance.reactorItemId == 0)
{
__instance.reactorEnergy = 0;
}
Expand All @@ -38,7 +38,7 @@ public static bool Mecha_GenerateEnergy_Prefix(Mecha __instance)
[HarmonyPatch(typeof(Mecha), nameof(Mecha.AddProductionStat))]
public static bool AddStat_Common_Prefix()
{
if (!Multiplayer.IsActive || ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
return true;

// TODO: Send packet to host to add stat?
Expand Down
4 changes: 2 additions & 2 deletions NebulaPatcher/Patches/Dynamic/PlanetData_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PlanetData_Patch
public static bool UnloadMeshes_Prefix(PlanetData __instance)
{
//Host should not unload planet meshes, since he need to permorm all terrain operations
if (Multiplayer.IsActive && ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost)
{
//Do not unload meshes, just hide them so it is not visible
UnloadVisuals(__instance);
Expand All @@ -26,7 +26,7 @@ public static bool UnloadMeshes_Prefix(PlanetData __instance)
public static bool UnloadData_Prefix()
{
//Host should not unload planet data, since he need to permorm all operations from users
if (Multiplayer.IsActive && ((LocalPlayer)Multiplayer.Session.LocalPlayer).IsHost)
if (Multiplayer.IsActive && Multiplayer.Session.LocalPlayer.IsHost)
{
return false;
}
Expand Down
Loading

0 comments on commit 9a362da

Please sign in to comment.