Skip to content

Commit

Permalink
Cleanup/refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Jun 8, 2024
1 parent 69a6790 commit 13ad39d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Fika.Core/Coop/BTR/FikaBTRManager_Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void AttachBot(int netId)
weaponPrefab.transform.SetPositionAndRotation(turretView.GunRoot.position, turretView.GunRoot.rotation);
weaponTransform.SetPositionAndRotation(turretView.GunRoot.position, turretView.GunRoot.rotation);

string[] gunModsToDisable = Traverse.Create(turretView).Field("_gunModsToDisable").GetValue<string[]>();
string[] gunModsToDisable = Traverse.Create(turretView).Field<string[]>("_gunModsToDisable").Value;
if (gunModsToDisable != null)
{
foreach (Transform child in weaponTransform)
Expand Down
58 changes: 35 additions & 23 deletions Fika.Core/Coop/Components/CoopHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private async Task ReadFromServerCharactersLoop()
{
while (RunAsyncTasks)
{
CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.Instance;
CoopGame coopGame = LocalGameInstance;
int waitTime = 2500;
if (coopGame.Status == GameStatus.Started)
{
Expand Down Expand Up @@ -285,24 +285,31 @@ private void ReadFromServerCharacters()

private async void SpawnPlayer(SpawnObject spawnObject)
{
if (Singleton<GameWorld>.Instance.RegisteredPlayers.Any(x => x.ProfileId == spawnObject.Profile.ProfileId))
if (spawnObject.Profile == null)
{
Logger.LogError("SpawnPlayer Profile is NULL!");
queuedProfileIds.Remove(spawnObject.Profile.ProfileId);
return;
}

if (Singleton<GameWorld>.Instance.AllAlivePlayersList.Any(x => x.ProfileId == spawnObject.Profile.ProfileId))
foreach (IPlayer player in Singleton<GameWorld>.Instance.RegisteredPlayers)
{
return;
if (player.ProfileId == spawnObject.Profile.ProfileId)
{
return;
}
}

int playerId = Players.Count + Singleton<GameWorld>.Instance.RegisteredPlayers.Count + 1;
if (spawnObject.Profile == null)
foreach (IPlayer player in Singleton<GameWorld>.Instance.AllAlivePlayersList)
{
Logger.LogError("SpawnPlayer Profile is NULL!");
queuedProfileIds.Remove(spawnObject.Profile.ProfileId);
return;
if (player.ProfileId == spawnObject.Profile.ProfileId)
{
return;
}
}

int playerId = Players.Count + Singleton<GameWorld>.Instance.RegisteredPlayers.Count + 1;

IEnumerable<ResourceKey> allPrefabPaths = spawnObject.Profile.GetAllPrefabPaths();
if (allPrefabPaths.Count() == 0)
{
Expand All @@ -311,9 +318,7 @@ private async void SpawnPlayer(SpawnObject spawnObject)
}

await Singleton<PoolManager>.Instance.LoadBundlesAndCreatePools(PoolManager.PoolsCategory.Raid,
PoolManager.AssemblyType.Local,
allPrefabPaths.ToArray(),
JobPriority.General).ContinueWith(x =>
PoolManager.AssemblyType.Local, allPrefabPaths.ToArray(), JobPriority.General).ContinueWith(x =>
{
if (x.IsCompleted)
{
Expand All @@ -340,8 +345,7 @@ await Singleton<PoolManager>.Instance.LoadBundlesAndCreatePools(PoolManager.Pool
{
if (LocalGameInstance != null)
{
CoopGame coopGame = LocalGameInstance;
BotsController botController = coopGame.BotsController;
BotsController botController = LocalGameInstance.BotsController;
if (botController != null)
{
// Start Coroutine as botController might need a while to start sometimes...
Expand Down Expand Up @@ -388,14 +392,20 @@ private IEnumerator ProcessSpawnQueue()

public void QueueProfile(Profile profile, Vector3 position, int netId, bool isAlive = true, bool isAI = false)
{
if (Singleton<GameWorld>.Instance.RegisteredPlayers.Any(x => x.ProfileId == profile.ProfileId))
foreach (IPlayer player in Singleton<GameWorld>.Instance.RegisteredPlayers)
{
return;
if (player.ProfileId == profile.ProfileId)
{
return;
}
}

if (Singleton<GameWorld>.Instance.AllAlivePlayersList.Any(x => x.ProfileId == profile.ProfileId))
foreach (IPlayer player in Singleton<GameWorld>.Instance.AllAlivePlayersList)
{
return;
if (player.ProfileId == profile.ProfileId)
{
return;
}
}

if (queuedProfileIds.Contains(profile.ProfileId))
Expand All @@ -419,10 +429,10 @@ public WorldInteractiveObject GetInteractiveObject(string objectId, out WorldInt

private ObservedCoopPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int playerId, bool isAI, int netId)
{
ObservedCoopPlayer otherPlayer = ObservedCoopPlayer.CreateObservedPlayer(playerId, position, Quaternion.identity,
"Player", isAI == true ? "Bot_" : $"Player_{profile.Nickname}_", EPointOfView.ThirdPerson, profile, isAI,
EUpdateQueue.Update, Player.EUpdateMode.Manual, Player.EUpdateMode.Auto,
GClass548.Config.CharacterController.ObservedPlayerMode,
ObservedCoopPlayer otherPlayer = ObservedCoopPlayer.CreateObservedPlayer(playerId, position,
Quaternion.identity, "Player", isAI == true ? "Bot_" : $"Player_{profile.Nickname}_",
EPointOfView.ThirdPerson, profile, isAI, EUpdateQueue.Update, Player.EUpdateMode.Manual,
Player.EUpdateMode.Auto, GClass548.Config.CharacterController.ObservedPlayerMode,
() => Singleton<SharedGameSettingsClass>.Instance.Control.Settings.MouseSensitivity,
() => Singleton<SharedGameSettingsClass>.Instance.Control.Settings.MouseAimingSensitivity,
GClass1457.Default).Result;
Expand Down Expand Up @@ -474,7 +484,9 @@ private ObservedCoopPlayer SpawnObservedPlayer(Profile profile, Vector3 position
if (profile.Info.Side is EPlayerSide.Bear or EPlayerSide.Usec)
{
Item backpack = profile.Inventory.Equipment.GetSlot(EquipmentSlot.Backpack).ContainedItem;
backpack?.GetAllItems().Where(i => i != backpack).ExecuteForEach(i => i.SpawnedInSession = true);
backpack?.GetAllItems()
.Where(i => i != backpack)
.ExecuteForEach(i => i.SpawnedInSession = true);

// We still want DogTags to be 'FiR'
Item item = otherPlayer.Inventory.Equipment.GetSlot(EquipmentSlot.Dogtag).ContainedItem;
Expand Down
4 changes: 2 additions & 2 deletions Fika.Core/Coop/FreeCamera/FreeCameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,13 @@ private void ClearEffects()

Traverse effectsController = Traverse.Create(cameraClass.EffectsController);

BloodOnScreen bloodOnScreen = effectsController.Field("bloodOnScreen_0").GetValue<BloodOnScreen>();
BloodOnScreen bloodOnScreen = effectsController.Field<BloodOnScreen>("bloodOnScreen_0").Value;
if (bloodOnScreen != null)
{
Destroy(bloodOnScreen);
}

List<EffectsController.Class576> effectsManagerList = effectsController.Field("list_0").GetValue<List<EffectsController.Class576>>();
List<EffectsController.Class576> effectsManagerList = effectsController.Field<List<EffectsController.Class576>>("list_0").Value;
if (effectsManagerList != null)
{
foreach (EffectsController.Class576 effectsManager in effectsManagerList)
Expand Down
12 changes: 6 additions & 6 deletions Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ private async Task SendOrReceiveSpawnPoint()
{
Logger.LogInfo($"Retrieved Spawn Point '{name}' from server");

Dictionary<ISpawnPoint, SpawnPointMarker> allSpawnPoints = Traverse.Create(spawnPoints).Field("dictionary_0").GetValue<Dictionary<ISpawnPoint, SpawnPointMarker>>();
Dictionary<ISpawnPoint, SpawnPointMarker> allSpawnPoints = Traverse.Create(spawnPoints).Field<Dictionary<ISpawnPoint, SpawnPointMarker>>("dictionary_0").Value;
foreach (ISpawnPoint spawnPointObject in allSpawnPoints.Keys)
{
if (spawnPointObject.Id == name)
Expand Down Expand Up @@ -790,7 +790,7 @@ public override async Task<LocalPlayer> vmethod_2(int playerId, Vector3 position
if (MenuUI.Instantiated)
{
MenuUI menuUI = MenuUI.Instance;
DefaultUIButton backButton = Traverse.Create(menuUI.MatchmakerTimeHasCome).Field("_cancelButton").GetValue<DefaultUIButton>();
DefaultUIButton backButton = Traverse.Create(menuUI.MatchmakerTimeHasCome).Field<DefaultUIButton>("_cancelButton").Value;
customButton = Instantiate(backButton.gameObject, backButton.gameObject.transform.parent);
customButton.gameObject.name = "FikaBackButton";
customButton.gameObject.transform.position = new(customButton.transform.position.x, customButton.transform.position.y - 20, customButton.transform.position.z);
Expand Down Expand Up @@ -1445,7 +1445,7 @@ public void UpdateExfilPointFromServer(ExfiltrationPoint point, bool enable)

public void ResetExfilPointsFromServer(ExfiltrationPoint[] points)
{
Dictionary<string, ExitTimerPanel> currentExfils = Traverse.Create(GameUi.TimerPanel).Field("dictionary_0").GetValue<Dictionary<string, ExitTimerPanel>>();
Dictionary<string, ExitTimerPanel> currentExfils = Traverse.Create(GameUi.TimerPanel).Field<Dictionary<string, ExitTimerPanel>>("dictionary_0").Value;
foreach (ExitTimerPanel exitTimerPanel in currentExfils.Values)
{
exitTimerPanel.Close();
Expand Down Expand Up @@ -1598,8 +1598,8 @@ public void ClearHostAI(Player player)
}
}

BotsClass bots = Traverse.Create(botsController_0.BotSpawner).Field("_bots").GetValue<BotsClass>();
HashSet<BotOwner> allBots = Traverse.Create(bots).Field("hashSet_0").GetValue<HashSet<BotOwner>>();
BotsClass bots = Traverse.Create(botsController_0.BotSpawner).Field<BotsClass>("_bots").Value;
HashSet<BotOwner> allBots = Traverse.Create(bots).Field<HashSet<BotOwner>>("hashSet_0").Value;

foreach (BotOwner bot in allBots)
{
Expand Down Expand Up @@ -1994,7 +1994,7 @@ public void HandleExit()

private void FireCallback()
{
Callback<ExitStatus, TimeSpan, MetricsClass> endCallback = Traverse.Create(localGame).Field("callback_0").GetValue<Callback<ExitStatus, TimeSpan, MetricsClass>>();
Callback<ExitStatus, TimeSpan, MetricsClass> endCallback = Traverse.Create(localGame).Field<Callback<ExitStatus, TimeSpan, MetricsClass>>("callback_0").Value;

localGame.SavePlayer(localPlayer, exitStatus, exitName, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void Start()
weaponPrefab = ControllerGameObject.GetComponent<WeaponPrefab>();
if (UnderbarrelWeapon != null)
{
underBarrelManager = Traverse.Create(this).Field("gclass1593_0").GetValue<GClass1593>();
underBarrelManager = Traverse.Create(this).Field<GClass1593>("gclass1593_0").Value;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/Coop/PacketHandlers/ServerPacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private IEnumerator SendTrainTime()
Locomotive locomotive = FindObjectOfType<Locomotive>();
if (locomotive != null)
{
long time = Traverse.Create(locomotive).Field("_depart").GetValue<DateTime>().Ticks;
long time = Traverse.Create(locomotive).Field<DateTime>("_depart").Value.Ticks;

GenericPacket packet = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static void PatchPostfix(RaidSettingsWindow __instance, UiElementBlocker
}

// Remove redundant settings and add our own "Random" to make the setting clear, while also renaming index 0 to "Together"
List<BaseDropDownBox.Struct969> labelList = Traverse.Create(____playersSpawnPlaceDropdown).Field("list_0").GetValue<List<BaseDropDownBox.Struct969>>();
List<BaseDropDownBox.Struct969> labelList = Traverse.Create(____playersSpawnPlaceDropdown).Field<List<BaseDropDownBox.Struct969>>("list_0").Value;
labelList.Clear();
labelList.Add(new()
{
Expand Down
4 changes: 3 additions & 1 deletion Fika.Core/Coop/Players/CoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Fika.Core.Coop.Players
/// </summary>
public class CoopPlayer : LocalPlayer
{
#region Fields and Properties
public PacketReceiver PacketReceiver;
public IPacketSender PacketSender;
private DateTime lastPingTime;
Expand All @@ -46,7 +47,8 @@ public class CoopPlayer : LocalPlayer
public Transform RaycastCameraTransform;
public int NetId;
public bool IsObservedAI = false;
public Dictionary<uint, Callback<EOperationStatus>> OperationCallbacks = [];
public Dictionary<uint, Callback<EOperationStatus>> OperationCallbacks = [];
#endregion

public static async Task<LocalPlayer> Create(int playerId, Vector3 position, Quaternion rotation,
string layerName, string prefix, EPointOfView pointOfView, Profile profile, bool aiControl,
Expand Down
15 changes: 7 additions & 8 deletions Fika.Core/Coop/Players/ObservedCoopPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ public PlayerStatePacket Interpolate(in PlayerStatePacket newState, in PlayerSta
Move(Vector2.Lerp(newState.MovementDirection, lastState.MovementDirection, interpolationRatio));
}

Vector3 a = Vector3.Lerp(MovementContext.TransformPosition, newState.Position, interpolationRatio);
CharacterController.Move(a - MovementContext.TransformPosition, interpolationRatio);
Vector3 newPosition = Vector3.Lerp(MovementContext.TransformPosition, newState.Position, interpolationRatio);
CharacterController.Move(newPosition - MovementContext.TransformPosition, interpolationRatio);

if (!Mathf.Approximately(MovementContext.Tilt, newState.Tilt))
{
Expand Down Expand Up @@ -822,7 +822,7 @@ public override void SetInventory(EquipmentClass equipmentClass)
{
Inventory.Equipment = equipmentClass;

BindableState<Item> itemInHands = (BindableState<Item>)Traverse.Create(this).Field("_itemInHands").GetValue();
BindableState<Item> itemInHands = Traverse.Create(this).Field<BindableState<Item>>("_itemInHands").Value;
if (HandsController != null && HandsController.Item != null)
{
Item item = FindItem(HandsController.Item.Id);
Expand Down Expand Up @@ -920,13 +920,12 @@ public void InitObservedPlayer()
PacketSender.Writer.Reset();
PacketSender.Client.SendData(PacketSender.Writer, ref genericPacket, LiteNetLib.DeliveryMethod.ReliableOrdered);

IVaultingComponent vaultingComponent = playerTraverse.Field("_vaultingComponent").GetValue<IVaultingComponent>();
IVaultingComponent vaultingComponent = playerTraverse.Field<IVaultingComponent>("_vaultingComponent").Value;
if (vaultingComponent != null)
{
UpdateEvent -= vaultingComponent.DoVaultingTick;
}


playerTraverse.Field("_vaultingComponent").SetValue(null);
playerTraverse.Field("_vaultingComponentDebug").SetValue(null);
playerTraverse.Field("_vaultingParameters").SetValue(null);
Expand All @@ -951,7 +950,7 @@ public void InitObservedPlayer()

CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.Instance;

IVaultingComponent vaultingComponent = playerTraverse.Field("_vaultingComponent").GetValue<IVaultingComponent>();
IVaultingComponent vaultingComponent = playerTraverse.Field<IVaultingComponent>("_vaultingComponent").Value;
if (vaultingComponent != null)
{
UpdateEvent -= vaultingComponent.DoVaultingTick;
Expand All @@ -971,7 +970,7 @@ public void InitObservedPlayer()

waitForStartRoutine = StartCoroutine(CreateHealthBar());

RaycastCameraTransform = playerTraverse.Field("_playerLookRaycastTransform").GetValue<Transform>();
RaycastCameraTransform = playerTraverse.Field<Transform>("_playerLookRaycastTransform").Value;
}
}

Expand Down Expand Up @@ -1038,7 +1037,7 @@ public override void LandingAdjustments(float d)

public new void CreateCompass()
{
bool compassInstantiated = Traverse.Create(this).Field("_compassInstantiated").GetValue<bool>();
bool compassInstantiated = Traverse.Create(this).Field<bool>("_compassInstantiated").Value;
if (!compassInstantiated)
{
Transform transform = Singleton<PoolManager>.Instance.CreateFromPool<Transform>(new ResourceKey
Expand Down
4 changes: 2 additions & 2 deletions Fika.Core/UI/FikaUIUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static GClass3108 ShowFikaMessage(this ErrorScreen errorScreen, string ti
errorScreenHandler.context.OnDecline += errorScreen.method_4;
errorScreenHandler.context.OnCloseSilent += errorScreen.method_4;

GClass767 ui = Traverse.Create(errorScreen).Field("UI").GetValue<GClass767>();
GClass767 ui = Traverse.Create(errorScreen).Field<GClass767>("UI").Value;

ui.AddDisposable(new Action(errorScreenHandler.method_0));
string text = buttonType switch
Expand All @@ -115,7 +115,7 @@ public static GClass3108 ShowFikaMessage(this ErrorScreen errorScreen, string ti
string string_1 = message.SubstringIfNecessary(500);
errorScreenTraverse.Field("string_1").SetValue(string_1);

TextMeshProUGUI errorDescription = Traverse.Create(errorScreen).Field("_errorDescription").GetValue<TextMeshProUGUI>();
TextMeshProUGUI errorDescription = Traverse.Create(errorScreen).Field<TextMeshProUGUI>("_errorDescription").Value;
errorDescription.text = string_1;

Coroutine coroutine_0 = errorScreenTraverse.Field("coroutine_0").GetValue<Coroutine>();
Expand Down

0 comments on commit 13ad39d

Please sign in to comment.