Created
April 24, 2020 00:54
-
-
Save theWill/af9b37758060fd9fa8f5249793a18d0a to your computer and use it in GitHub Desktop.
MatchCleanup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BeardedManStudios.Forge.Networking; | |
using BeardedManStudios.Forge.Networking.Generated; | |
using BeardedManStudios.Forge.Networking.Unity; | |
using CreativeSpore.SuperTilemapEditor; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public static class MatchCleanup | |
{ | |
public static List<System.Type> networkBehaviors = new List<System.Type>() { | |
typeof(PlantBehavior), typeof(GardenSpotBehavior), | |
typeof(MAPlayerBehavior), typeof(MatchAnnounceBehavior), | |
typeof(ReadyUpBehavior), typeof(ScoreBoxBehavior), | |
typeof(PlayerBehavior), typeof(PlayerStatsBehavior) | |
}; | |
public static List<NetworkObject> toDelete = new List<NetworkObject>(); | |
private static void cleanupNetworkObjects(NetWorker owner, NetworkingPlayer player = null, List<System.Type> specificBehaviors = null) | |
{ | |
if (NetworkManager.Instance == null) | |
return; | |
toDelete.Clear(); | |
MainThreadManager.Run(() => | |
{ | |
foreach (var no in owner.NetworkObjectList) | |
{ | |
if (no == null) | |
continue; | |
if (player != null && no.Owner != player) | |
continue; | |
//delete specific stuff | |
if(specificBehaviors != null) | |
{ | |
INetworkBehavior behav = no.AttachedBehavior; | |
System.Type tempType = behav.GetType(); | |
if (specificBehaviors.Contains(behav.GetType())) | |
toDelete.Add(no); | |
} else { | |
toDelete.Add(no); | |
} | |
} | |
if (toDelete.Count > 0) | |
{ | |
for (int i = toDelete.Count - 1; i >= 0; i--) | |
{ | |
owner.NetworkObjectList.Remove(toDelete[i]); | |
toDelete[i].Destroy(); | |
} | |
} | |
}); | |
} | |
public static void cleanupAfterMatchNetworkObjects() | |
{ | |
try | |
{ | |
cleanupNetworkObjects(NetworkManager.Instance.Networker, NetworkManager.Instance.Networker.Me, networkBehaviors); | |
} | |
catch (System.Exception e) { } | |
} | |
public static void cleanupOnRestartNetworkObjects() | |
{ | |
try | |
{ | |
cleanupNetworkObjects(NetworkManager.Instance.Networker); | |
} | |
catch (System.Exception e) { } | |
} | |
public static void cleanupOnPlayerDisconnected(NetworkingPlayer player, NetWorker sender) | |
{ | |
try | |
{ | |
cleanupNetworkObjects(sender, player); | |
} | |
catch (System.Exception e) { } | |
} | |
public static void deactivateTilemaps() | |
{ | |
TilemapGroup mapObj = GameObject.FindObjectOfType<TilemapGroup>(); | |
if (mapObj != null) | |
mapObj.gameObject.SetActive(false); | |
} | |
public static void destroyObjectsNotUsuallyDestroyed() | |
{ | |
MusicPlayer mp = GameObject.FindObjectOfType<MusicPlayer>(); | |
if (mp != null) GameObject.DestroyImmediate(mp.gameObject); | |
UserInterfaceBossController uib = GameObject.FindObjectOfType<UserInterfaceBossController>(); | |
if (uib != null) GameObject.DestroyImmediate(uib.gameObject); | |
PlayerInputManager pim = GameObject.FindObjectOfType<PlayerInputManager>(); | |
if (pim != null) GameObject.DestroyImmediate(pim.gameObject); | |
} | |
public static void cleanupRewired() | |
{ | |
GameObject go = GameObject.Find("Rewired Input Manager"); | |
if (go != null) | |
GameObject.DestroyImmediate(go); | |
} | |
public static void shutdownNetworking() | |
{ | |
DisconnectCanvasController dcc = GameObject.FindObjectOfType<DisconnectCanvasController>(); | |
if (dcc != null) | |
dcc.rebooting = true; | |
if (NetworkManager.Instance != null) | |
NetworkManager.Instance.Disconnect(); | |
} | |
public static void kickPlayer(NetworkingPlayer playerToKick, NetWorker sender) | |
{ | |
disconnectPlayer(playerToKick); | |
cleanupOnPlayerDisconnected(playerToKick, sender); | |
} | |
private static void disconnectPlayer(NetworkingPlayer playerToKick) | |
{ | |
// We are going to force the disconnect of the client | |
bool forcefully = false; | |
// Consider the player you want to disconnect is the targetPlayer object | |
// Also consider that serverNetworker is the server's networker (this can be NetworkManager.Instance.Networker) | |
Debug.Log("Kick player " + playerToKick); | |
((IServer)NetworkManager.Instance.Networker).Disconnect(playerToKick, forcefully); | |
// This needs to be called whether "forced" is true or false in Disconnect() | |
((IServer)NetworkManager.Instance.Networker).CommitDisconnects(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment