Skip to content
Open
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: 0 additions & 1 deletion Multibonk/Game/EventHandlerExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Multibonk.Game.Handlers;
using UnityEngine.UIElements;

namespace Multibonk.Game
{
Expand Down
1 change: 0 additions & 1 deletion Multibonk/Game/GameFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,3 @@ public void Rotate(Vector3 rotation)
}
}
}

9 changes: 8 additions & 1 deletion Multibonk/Game/GamePatchFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ public static class GamePatchFlags

public static int Seed { get; set; } = _rng.Next(int.MinValue, int.MaxValue);

public static bool AllowStartMapCall { get; set; } = false;
public static bool AllowStartMapCall { get; set; } = false;

public static Vector3 LastPlayerPosition { get; set; }
public static Quaternion LastPlayerRotation { get; set; }

public static GameplayRulesSnapshot GameplayRules { get; private set; } = GameplayRulesSnapshot.FromPreferences();

public static void SetGameplayRules(GameplayRulesSnapshot snapshot)
{
GameplayRules = snapshot;
}
}
}
53 changes: 53 additions & 0 deletions Multibonk/Game/GameplayRulesSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;

namespace Multibonk.Game
{
public readonly struct GameplayRulesSnapshot : IEquatable<GameplayRulesSnapshot>
{
public bool PvpEnabled { get; }
public bool ReviveEnabled { get; }
public float ReviveDelaySeconds { get; }
public Preferences.LootDistributionMode XpSharingMode { get; }
public Preferences.LootDistributionMode GoldSharingMode { get; }
public Preferences.LootDistributionMode ChestSharingMode { get; }

public GameplayRulesSnapshot(
bool pvpEnabled,
bool reviveEnabled,
float reviveDelaySeconds,
Preferences.LootDistributionMode xpSharingMode,
Preferences.LootDistributionMode goldSharingMode,
Preferences.LootDistributionMode chestSharingMode)
{
PvpEnabled = pvpEnabled;
ReviveEnabled = reviveEnabled;
ReviveDelaySeconds = reviveDelaySeconds;
XpSharingMode = xpSharingMode;
GoldSharingMode = goldSharingMode;
ChestSharingMode = chestSharingMode;
}

public static GameplayRulesSnapshot FromPreferences() => new GameplayRulesSnapshot(
Preferences.PvpEnabled.Value,
Preferences.ReviveEnabled.Value,
Preferences.ReviveTimeSeconds.Value,
Preferences.GetXpSharingMode(),
Preferences.GetGoldSharingMode(),
Preferences.GetChestSharingMode());

public bool Equals(GameplayRulesSnapshot other) =>
PvpEnabled == other.PvpEnabled &&
ReviveEnabled == other.ReviveEnabled &&
Math.Abs(ReviveDelaySeconds - other.ReviveDelaySeconds) < 0.001f &&
XpSharingMode == other.XpSharingMode &&
GoldSharingMode == other.GoldSharingMode &&
ChestSharingMode == other.ChestSharingMode;

public override bool Equals(object? obj) => obj is GameplayRulesSnapshot other && Equals(other);

public override int GetHashCode() => HashCode.Combine(PvpEnabled, ReviveEnabled, ReviveDelaySeconds, XpSharingMode, GoldSharingMode, ChestSharingMode);

public override string ToString() =>
$"PvP={(PvpEnabled ? "Enabled" : "Disabled")}, Revive={(ReviveEnabled ? "Enabled" : "Disabled")} ({ReviveDelaySeconds:0.##}s), XP={XpSharingMode}, Gold={GoldSharingMode}, Chest={ChestSharingMode}";
}
}
4 changes: 2 additions & 2 deletions Multibonk/Game/Handlers/GameDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Concurrent;
using MelonLoader;
using MelonLoader;
using System.Collections.Concurrent;

namespace Multibonk.Game.Handlers
{
Expand Down
48 changes: 48 additions & 0 deletions Multibonk/Game/Handlers/Logic/GameplayRuleSynchronizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using MelonLoader;
using UnityEngine;

namespace Multibonk.Game.Handlers.Logic
{
public class GameplayRuleSynchronizer : GameEventHandler
{
private GameplayRulesSnapshot lastSnapshot;
private float lastLogTime;

public GameplayRuleSynchronizer()
{
lastSnapshot = GameplayRulesSnapshot.FromPreferences();
GamePatchFlags.SetGameplayRules(lastSnapshot);
GameEvents.GameLoadedEvent += ApplyRulesToWorld;
GameEvents.ConfirmMapEvent += ApplyRulesToWorld;
}

public override void Update()
{
if (Time.frameCount % 60 != 0)
{
return;
}

var current = GameplayRulesSnapshot.FromPreferences();
if (current.Equals(lastSnapshot))
{
return;
}

lastSnapshot = current;
GamePatchFlags.SetGameplayRules(current);
ApplyRulesToWorld();
}

private void ApplyRulesToWorld()
{
GamePatchFlags.SetGameplayRules(lastSnapshot);

if (Time.time - lastLogTime > 5f)
{
MelonLogger.Msg($"Applied gameplay rules: {lastSnapshot}.");
lastLogTime = Time.time;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Multibonk.Game.Handlers.Logic
{
public class UpdateNetworkPlayerAnimationsEventHandler : GameEventHandler
{
public UpdateNetworkPlayerAnimationsEventHandler()
{
public UpdateNetworkPlayerAnimationsEventHandler()
{

}


public override void FixedUpdate()
{
foreach(var player in GamePatchFlags.PlayersCache.Values)
foreach (var player in GamePatchFlags.PlayersCache.Values)
{
if (player.PlayerObject.IsNullOrDestroyed())
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using Il2Cpp;
using Il2CppAssets.Scripts.Actors.Player;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using MelonLoader;
using Multibonk.Networking.Comms.Base.Packet;
using Multibonk.Networking.Lobby;
using UnityEngine;
using static Il2Cpp.AnimatedMeshScriptableObject;
using static MelonLoader.MelonLaunchOptions;

namespace Multibonk.Game.Handlers.NetworkNotify
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Multibonk.Networking.Comms.Base.Packet.Multibonk.Networking.Comms.Base.Packet;
using Multibonk.Networking.Comms.Base.Packet;
using Multibonk.Networking.Lobby;
using Multibonk.Networking.Comms.Base.Packet;
using Multibonk.Networking.Comms.Base.Packet.Multibonk.Networking.Comms.Base.Packet;
using Multibonk.Networking.Comms.Multibonk.Networking.Comms;
using Multibonk.Networking.Lobby;

namespace Multibonk.Game.Handlers.NetworkNotify
{
Expand Down
5 changes: 1 addition & 4 deletions Multibonk/Game/Patches/MainMenuPatches.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@


using System.Runtime.CompilerServices;
using HarmonyLib;
using HarmonyLib;
using Il2Cpp;
using Il2CppAssets.Scripts.Actors.Player;
using Il2CppRewired.Utils;
Expand Down
27 changes: 17 additions & 10 deletions Multibonk/Multibonk.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using MelonLoader;
using Multibonk.UserInterface.Window;
using Microsoft.Extensions.DependencyInjection;
using Multibonk.Networking.Lobby;
using Multibonk.Networking.Comms.Server.Protocols;
using Multibonk.Game;
using Multibonk.Game.Handlers;
using Multibonk.Game.Handlers.Logic;
using Multibonk.Game.Handlers.NetworkNotify;
using Multibonk.Networking.Comms.Base;
using Multibonk.Networking.Comms.Client.Handlers;
using Multibonk.Networking.Comms.Client.Protocols;
using Multibonk.Networking.Comms.Multibonk.Networking.Comms;
using Multibonk.Networking.Comms.Server.Handlers;
using Multibonk.Networking.Comms.Client.Handlers;
using Multibonk.Game.Handlers;
using Multibonk.Game;
using Multibonk.Networking.Comms.Base;
using Multibonk.Game.Handlers.NetworkNotify;
using Multibonk.Game.Handlers.Logic;
using Multibonk.Networking.Comms.Server.Protocols;
using Multibonk.Networking.Lobby;
using Multibonk.Networking.Steam;
using Multibonk.UserInterface.Window;

namespace Multibonk
{
Expand All @@ -22,7 +23,7 @@ public class MultibonkMod : MelonMod

public override void OnGUI()
{
if(manager != null)
if (manager != null)
manager.OnGUI();

}
Expand Down Expand Up @@ -52,6 +53,7 @@ public override void OnInitializeMelon()
services.AddSingleton<IGameEventHandler, PlayerMovementEventHandler>();
services.AddSingleton<IGameEventHandler, StartGameEventHandler>();
services.AddSingleton<IGameEventHandler, UpdateNetworkPlayerAnimationsEventHandler>();
services.AddSingleton<IGameEventHandler, GameplayRuleSynchronizer>();
services.AddSingleton<IGameEventHandler, GameDispatcher>();

services.AddSingleton<EventHandlerExecutor>();
Expand All @@ -76,11 +78,14 @@ public override void OnInitializeMelon()

// Packet Handlers cannot call services. Otherwise, it will cause circular dependency
services.AddSingleton<NetworkService>();
services.AddSingleton<SteamTunnelService>();
services.AddSingleton<SteamTunnelCallbackBinder>();
services.AddSingleton<LobbyService>();

services.AddSingleton<ClientLobbyWindow>();
services.AddSingleton<ConnectionWindow>();
services.AddSingleton<HostLobbyWindow>();
services.AddSingleton<OptionsWindow>();

services.AddSingleton<UIManager>();

Expand All @@ -91,6 +96,8 @@ public override void OnInitializeMelon()

var _lobbyContext = serviceProvider.GetService<LobbyContext>();

serviceProvider.GetService<SteamTunnelCallbackBinder>();

base.OnInitializeMelon();
}
}
Expand Down
Loading