Skip to content

Commit

Permalink
Merge pull request #117 from Zingabopp/dev
Browse files Browse the repository at this point in the history
1.16.1
  • Loading branch information
Goobwabber authored May 29, 2021
2 parents 57240e5 + 618f195 commit 66e6e42
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 37 deletions.
2 changes: 1 addition & 1 deletion MultiplayerExtensions/Beatmaps/PreviewBeatmapPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static async public Task<PreviewBeatmapPacket> FromPreview(PreviewBeatmapStub pr
packet.beatsPerMinute = preview.beatsPerMinute;
packet.songDuration = preview.songDuration;

packet.coverImage = await preview.GetRawCoverAsync(CancellationToken.None);
//packet.coverImage = await preview.GetRawCoverAsync(CancellationToken.None);

packet.characteristic = characteristic;
packet.difficulty = difficulty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void HandleLobbyEnvironmentLoaded(object sender, System.EventArgs e)
_stageManager.transform.localScale = new Vector3(centerScreenScale, centerScreenScale, centerScreenScale);

SetAllPlayerPlaceColors(Color.black);
SetPlayerPlaceColor(_sessionManager.localPlayer, _playerManager.localColor);
SetPlayerPlaceColor(_sessionManager.localPlayer, ExtendedPlayerManager.localColor);
foreach (ExtendedPlayer player in _playerManager.players.Values)
SetPlayerPlaceColor(player, player.playerColor);
}
Expand Down
29 changes: 17 additions & 12 deletions MultiplayerExtensions/HarmonyPatches/InstallerPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,23 @@ internal static void Prefix(ref GameplayCoreInstaller __instance, ref IConnected
newModifiers = exPlayer.lastModifiers;
else
newModifiers = hostPlayer?.lastModifiers;

if (newModifiers != null)
____sceneSetupData = new GameplayCoreSceneSetupData(
____sceneSetupData.difficultyBeatmap,
____sceneSetupData.previewBeatmapLevel,
newModifiers,
____sceneSetupData.playerSpecificSettings,
____sceneSetupData.practiceSettings,
____sceneSetupData.useTestNoteCutSoundEffects,
____sceneSetupData.environmentInfo,
____sceneSetupData.colorScheme
);

if (Plugin.Config.LagReducer && newModifiers != null)
newModifiers = newModifiers.CopyWith(zenMode: true);

if (newModifiers == null)
newModifiers = ____sceneSetupData.gameplayModifiers;

____sceneSetupData = new GameplayCoreSceneSetupData(
____sceneSetupData.difficultyBeatmap,
____sceneSetupData.previewBeatmapLevel,
newModifiers.CopyWith(zenMode: true),
____sceneSetupData.playerSpecificSettings,
____sceneSetupData.practiceSettings,
____sceneSetupData.useTestNoteCutSoundEffects,
____sceneSetupData.environmentInfo,
____sceneSetupData.colorScheme
);
}
}
}
7 changes: 7 additions & 0 deletions MultiplayerExtensions/MPEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public static class MPEvents
/// </summary>
public static event EventHandler<bool>? FreeModChanged;

/// <summary>
/// Raised when the host toggles host pick.
/// </summary>
public static event EventHandler<bool>? HostPickChanged;

internal static void RaiseMasterServerChanged(object sender, MasterServerInfo info)
=> MasterServerChanged?.RaiseEventSafe(sender, info, nameof(MasterServerChanged));
internal static void RaiseRoomCodeChanged(object sender, string code)
Expand All @@ -55,6 +60,8 @@ internal static void RaiseCustomSongsChanged(object sender, bool state)
=> CustomSongsChanged.RaiseEventSafe(sender, state, nameof(CustomSongsChanged));
internal static void RaiseFreeModChanged(object sender, bool state)
=> FreeModChanged.RaiseEventSafe(sender, state, nameof(FreeModChanged));
internal static void RaiseHostPickChanged(object sender, bool state)
=> HostPickChanged.RaiseEventSafe(sender, state, nameof(HostPickChanged));
}

public class SelectedBeatmapEventArgs : EventArgs
Expand Down
16 changes: 16 additions & 0 deletions MultiplayerExtensions/MPState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ internal set
}
}

private static bool _hostPickEnabled;
/// <summary>
/// Whether host pick is enabled in the current lobby.
/// </summary>
public static bool HostPickEnabled
{
get => _hostPickEnabled;
internal set
{
if (_hostPickEnabled == value)
return;
_hostPickEnabled = value;
Plugin.Log?.Debug($"Updated host pick to '{value}'");
}
}

private static bool _easterEggsEnabled = true;
/// <summary>
/// Whether easter eggs in multiplayer are enabled.
Expand Down
2 changes: 1 addition & 1 deletion MultiplayerExtensions/MultiplayerExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<AssemblyName>MultiplayerExtensions</AssemblyName>
<AssemblyVersion>0.4.8</AssemblyVersion>
<AssemblyVersion>0.4.9</AssemblyVersion>
<TargetFramework>net472</TargetFramework>
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
Expand Down
26 changes: 26 additions & 0 deletions MultiplayerExtensions/OverrideClasses/GameStateControllerStub.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using IPA.Utilities;
using MultiplayerExtensions.Packets;
using MultiplayerExtensions.Sessions;
using MultiplayerExtensions.Utilities;
using Polyglot;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MultiplayerExtensions.OverrideClasses
Expand All @@ -25,6 +28,8 @@ internal GameStateControllerStub(PacketManager packetManager, ExtendedPlayerMana
_lobbyGameState.gameStateDidChangeEvent -= base.HandleGameStateDidChange;
_lobbyGameState.gameStateDidChangeEvent += HandleGameStateDidChange;
base.Activate();

(this as ILobbyGameStateController).levelFinishedEvent += handleLevelFinished;
}

public new void Deactivate()
Expand All @@ -35,6 +40,8 @@ internal GameStateControllerStub(PacketManager packetManager, ExtendedPlayerMana
_lobbyGameState.gameStateDidChangeEvent -= HandleGameStateDidChange;
_lobbyGameState.gameStateDidChangeEvent += base.HandleGameStateDidChange;
base.Deactivate();

(this as ILobbyGameStateController).levelFinishedEvent -= handleLevelFinished;
}

public new void StartListeningToGameStart()
Expand Down Expand Up @@ -86,6 +93,18 @@ private void OnPlayerStateChanged(IConnectedPlayer player)
{
_multiplayerSessionManager.SetLocalPlayerState("start_primed", false);
starting = true;

if (!Plugin.Config.HostPick)
{
ILobbyPlayerDataModel localPlayerDataModel = _lobbyPlayersDataModel.GetLobbyPlayerDataModel(_lobbyPlayersDataModel.localUserId);
IEnumerable<ILobbyPlayerDataModel> validDataModels = _lobbyPlayersDataModel.playersData.Values.Where(data => data.beatmapLevel != null);
ILobbyPlayerDataModel chosenPlayerDataModel = validDataModels.ElementAt(new Random().Next(0, validDataModels.Count()));
localPlayerDataModel.beatmapLevel = chosenPlayerDataModel.beatmapLevel;
localPlayerDataModel.beatmapCharacteristic = chosenPlayerDataModel.beatmapCharacteristic;
localPlayerDataModel.beatmapDifficulty = chosenPlayerDataModel.beatmapDifficulty;
localPlayerDataModel.gameplayModifiers = chosenPlayerDataModel.gameplayModifiers;
}

base.StartGame();
_multiplayerLevelLoader.countdownFinishedEvent -= base.HandleMultiplayerLevelLoaderCountdownFinished;
_multiplayerLevelLoader.countdownFinishedEvent += HandleCountdown;
Expand Down Expand Up @@ -162,6 +181,13 @@ private void StartLevel()
base.HandleMultiplayerLevelLoaderCountdownFinished(previewBeatmapLevel, beatmapDifficulty, beatmapCharacteristic, difficultyBeatmap, gameplayModifiers);
}

private void handleLevelFinished(MultiplayerLevelScenesTransitionSetupDataSO sceneSetupData, MultiplayerResultsData resultsData)
{
string? hash = Utilities.Utils.LevelIdToHash(sceneSetupData.previewBeatmapLevel.levelID);
if (hash != null)
_ = Statistics.PlayMap(hash, sceneSetupData.beatmapDifficulty.ToString(), sceneSetupData.beatmapCharacteristic.serializedName, (int)Math.Floor(resultsData.localPlayerResultData.levelCompletionResults.endSongTime), (int)ExtendedPlayerManager.localPlatform, MPState.CurrentMasterServer.hostname);
}

private bool starting;

private IPreviewBeatmapLevel? previewBeatmapLevel;
Expand Down
36 changes: 26 additions & 10 deletions MultiplayerExtensions/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,44 @@
using Zenject;
using MultiplayerExtensions.UI;
using BeatSaberMarkupLanguage.Settings;
using System.Net.Http;
using MultiplayerExtensions.Sessions;

namespace MultiplayerExtensions
{
[Plugin(RuntimeOptions.SingleStartInit)]
public class Plugin
{
public static readonly string HarmonyId = "com.github.Zingabopp.MultiplayerExtensions";

internal static Plugin Instance { get; private set; } = null!;
internal static PluginMetadata PluginMetadata = null!;
internal static Harmony? _harmony;
internal static IPALogger Log { get; private set; } = null!;
internal static PluginConfig Config = null!;

internal static HttpClient HttpClient { get; private set; } = null!;
internal static BeatSaver BeatSaver = null!;
internal static Harmony? _harmony;
internal static Harmony Harmony
{
get
{
return _harmony ??= new Harmony(HarmonyId);
}
}
/// <summary>
/// Use to send log messages through BSIPA.
/// </summary>
internal static IPALogger Log { get; private set; } = null!;
internal static PluginConfig Config = null!;

public static string UserAgent
{
get
{
var modVersion = PluginMetadata.Version.ToString();
var bsVersion = IPA.Utilities.UnityGame.GameVersion.ToString();
return $"MultiplayerExtensions/{modVersion} (BeatSaber/{bsVersion})";
}
}

private const int MaxPlayers = 100;
private const int MinPlayers = 10;

[Init]
public Plugin(IPALogger logger, Config conf, Zenjector zenjector, PluginMetadata pluginMetadata)
Expand All @@ -47,11 +62,15 @@ public Plugin(IPALogger logger, Config conf, Zenjector zenjector, PluginMetadata
PluginMetadata = pluginMetadata;
Log = logger;
Config = conf.Generated<PluginConfig>();

zenjector.OnApp<MPCoreInstaller>();
zenjector.OnMenu<MPMenuInstaller>();
zenjector.OnGame<MPGameInstaller>();

HttpOptions options = new HttpOptions("MultiplayerExtensions", new Version(pluginMetadata.Version.ToString()));
BeatSaver = new BeatSaver(options);
HttpClient = new HttpClient();
HttpClient.DefaultRequestHeaders.Add("User-Agent", Plugin.UserAgent);
}

[OnStart]
Expand All @@ -60,10 +79,7 @@ public void OnApplicationStart()
Plugin.Log?.Info($"MultiplayerExtensions: '{VersionInfo.Description}'");
BSMLSettings.instance.AddSettingsMenu("Multiplayer", "MultiplayerExtensions.UI.settings.bsml", MPSettings.instance);

if (Plugin.Config.MaxPlayers > 100)
Plugin.Config.MaxPlayers = 100;
if (Plugin.Config.MaxPlayers < 10)
Plugin.Config.MaxPlayers = 10;
Plugin.Config.MaxPlayers = Math.Max(Math.Min(Config.MaxPlayers, MaxPlayers), MinPlayers);
MPState.FreeModEnabled = false;

HarmonyManager.ApplyDefaultPatches();
Expand Down
8 changes: 5 additions & 3 deletions MultiplayerExtensions/Sessions/ExtendedPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class ExtendedPlayerManager : IInitializable, IDisposable
protected readonly IPlatformUserModel _platformUserModel;

private Dictionary<string, ExtendedPlayer> _players = new Dictionary<string, ExtendedPlayer>();
internal string? localPlatformID;
internal Platform localPlatform;
internal Color localColor;
internal static string? localPlatformID;
internal static Platform localPlatform;
internal static Color localColor;

public Dictionary<string, ExtendedPlayer> players { get => _players; }
public event Action<ExtendedPlayer>? extendedPlayerConnectedEvent;
Expand Down Expand Up @@ -44,6 +44,8 @@ public void Initialize()
{
localPlatformID = r.Result.platformUserId;
localPlatform = r.Result.platform.ToPlatform();

_ = Statistics.AddUser(localPlatformID, (int)localPlatform);
});
}

Expand Down
5 changes: 4 additions & 1 deletion MultiplayerExtensions/Sessions/SessionManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MultiplayerExtensions.Utilities;
using System;
using Zenject;

namespace MultiplayerExtensions.Sessions
Expand All @@ -18,6 +19,7 @@ public void Initialize()

MPState.CustomSongsEnabled = Plugin.Config.CustomSongs;
MPState.FreeModEnabled = Plugin.Config.FreeMod;
MPState.HostPickEnabled = Plugin.Config.HostPick;

_sessionManager.SetLocalPlayerState("modded", true);
_sessionManager.SetLocalPlayerState("customsongs", Plugin.Config.CustomSongs);
Expand All @@ -34,6 +36,7 @@ public void Dispose()
private void HandleConnected()
{
MPState.LocalPlayerIsHost = _sessionManager.localPlayer.isConnectionOwner;
_ = Statistics.UseMaster(MPState.CurrentMasterServer.hostname, (int)ExtendedPlayerManager.localPlatform, ExtendedPlayerManager.localPlatformID, MPState.LocalPlayerIsHost);
}

private void HandlePlayerStateChanged(IConnectedPlayer player)
Expand Down
6 changes: 4 additions & 2 deletions MultiplayerExtensions/UI/ClientLobbySetupPanel.bsml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<vertical>
<horizontal pad-top='10'>
<horizontal pad-top='12'>
<vertical min-width='45' horizontal-fit='MinSize'>
<toggle-setting id='CustomSongsToggle' value='CustomSongs' on-change='SetCustomSongs' text='Custom Songs' interactable='false' hover-hint='Whether the host has custom songs enabled'></toggle-setting>
<toggle-setting id='FreeModToggle' value='FreeMod' on-change='SetFreeMod' text='Free Mod' interactable='false' hover-hint='Whether players can pick their own modifiers.'></toggle-setting>
<toggle-setting id='FreeModToggle' value='FreeMod' on-change='SetFreeMod' text='Free Mod' interactable='false' hover-hint='Whether players can pick their own modifiers.'></toggle-setting>
<toggle-setting id='HostPickToggle' value='HostPick' on-change='SetHostPick' text='Host Picks' interactable='false' hover-hint='Host picks the song to be played.'></toggle-setting>
</vertical>
<vertical min-width='45' horizontal-fit='MinSize'>
<toggle-setting id="DefaultHUDToggle" value='DefaultHUD' on-change='SetDefaultHUD' text='Singleplayer HUD' hover-hint='Rearranges the HUD to look like it does in singleplayer'></toggle-setting>
<toggle-setting id="VerticalHUDToggle" value='VerticalHUD' on-change='SetVerticalHUD' text='Vertical HUD' hover-hint='Moves the HUD so that it is vertical'></toggle-setting>
<toggle-setting id="HologramToggle" value='Hologram' on-change='SetHologram' text='Hologram' hover-hint='Toggles the hologram showing the player in first place'></toggle-setting>
<toggle-setting id="LagReducerToggle" value='LagReducer' on-change='SetLagReducer' text='Lag Reducer' hover-hint='Makes multiplayer easier for computers to handle'></toggle-setting>
</vertical>
</horizontal>
<text id='DownloadProgressText' value='DownloadProgress' text='' align='Center'></text>
Expand Down
Loading

0 comments on commit 66e6e42

Please sign in to comment.