|
| 1 | +using Steamworks; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | +using UnityEngine; |
| 6 | +using ConnectionState = BeatSaberOnline.Data.Steam.SteamAPI.ConnectionState; |
| 7 | + |
| 8 | +namespace BeatSaberOnline.Data.Steam |
| 9 | +{ |
| 10 | + public class LobbyPacket |
| 11 | + { |
| 12 | + public enum SCREEN_TYPE : byte |
| 13 | + { |
| 14 | + NONE, |
| 15 | + WAITING, |
| 16 | + MENU, |
| 17 | + LOBBY, |
| 18 | + DOWNLOADING, |
| 19 | + PLAY_SONG, |
| 20 | + IN_GAME, |
| 21 | + } |
| 22 | + |
| 23 | + public string HostName { get; set; } = ""; |
| 24 | + public CSteamID LobbyID { get; set; } = new CSteamID(0); |
| 25 | + |
| 26 | + public string Status { get; set; } = ""; |
| 27 | + public bool Joinable { get; set; } = true; |
| 28 | + |
| 29 | + public int UsedSlots { get; set; } = 1; |
| 30 | + public int TotalSlots { get; set; } = 5; |
| 31 | + public int MaxSlots { get; private set; } = 10; |
| 32 | + |
| 33 | + public string CurrentSongId { get; set; } = ""; |
| 34 | + public string CurrentSongName { get; set; } = ""; |
| 35 | + public byte CurrentSongDifficulty { get; set; } = 0; |
| 36 | + public float CurrentSongOffset { get; set; } = 0f; |
| 37 | + |
| 38 | + public SCREEN_TYPE Screen { get; set; } = SCREEN_TYPE.NONE; |
| 39 | + private string _gameplayModifiers = ""; |
| 40 | + |
| 41 | + public GameplayModifiers GameplayModifiers |
| 42 | + { |
| 43 | + get => new GameplayModifiers(JsonUtility.FromJson<GameplayModifiers>(_gameplayModifiers)); |
| 44 | + set => _gameplayModifiers = JsonUtility.ToJson(value); |
| 45 | + } |
| 46 | + public LobbyPacket() { |
| 47 | + GameplayModifiers = new GameplayModifiers(); |
| 48 | + } |
| 49 | + public LobbyPacket(string data) |
| 50 | + { |
| 51 | + FromBytes(DeSerialize(data)); |
| 52 | + } |
| 53 | + private void FromBytes(byte[] data) |
| 54 | + { |
| 55 | + int currentStringPadding = BitConverter.ToInt32(data, 0); |
| 56 | + HostName = Encoding.UTF8.GetString(data, 4, currentStringPadding); |
| 57 | + LobbyID = new CSteamID(BitConverter.ToUInt64(data, 4 + currentStringPadding)); |
| 58 | + |
| 59 | + int statusLength = BitConverter.ToInt32(data, 12 + currentStringPadding); |
| 60 | + Status = Encoding.UTF8.GetString(data, 16 + currentStringPadding, statusLength); |
| 61 | + currentStringPadding += statusLength; |
| 62 | + |
| 63 | + Joinable = BitConverter.ToBoolean(data, 16 + currentStringPadding); |
| 64 | + UsedSlots = BitConverter.ToInt32(data, 17 + currentStringPadding); |
| 65 | + TotalSlots = BitConverter.ToInt32(data, 21 + currentStringPadding); |
| 66 | + MaxSlots = BitConverter.ToInt32(data, 25 + currentStringPadding); |
| 67 | + |
| 68 | + statusLength = BitConverter.ToInt32(data, 29 + currentStringPadding); |
| 69 | + CurrentSongId = Encoding.UTF8.GetString(data, 33 + currentStringPadding, statusLength); |
| 70 | + currentStringPadding += statusLength; |
| 71 | + |
| 72 | + statusLength = BitConverter.ToInt32(data, 33 + currentStringPadding); |
| 73 | + CurrentSongName = Encoding.UTF8.GetString(data, 37 + currentStringPadding, statusLength); |
| 74 | + currentStringPadding += statusLength; |
| 75 | + |
| 76 | + CurrentSongDifficulty = data[37 + currentStringPadding]; |
| 77 | + CurrentSongOffset = BitConverter.ToSingle(data, 38 + currentStringPadding); |
| 78 | + |
| 79 | + Screen = (SCREEN_TYPE) data[42 + currentStringPadding]; |
| 80 | + |
| 81 | + statusLength = BitConverter.ToInt32(data, 43 + currentStringPadding); |
| 82 | + _gameplayModifiers = Encoding.UTF8.GetString(data, 47 + currentStringPadding, statusLength); |
| 83 | + currentStringPadding += statusLength; |
| 84 | + } |
| 85 | + |
| 86 | + private byte[] ToBytes(bool includeSize = true) |
| 87 | + { |
| 88 | + List<byte> buffer = new List<byte>(); |
| 89 | + byte[] nameBuffer = Encoding.UTF8.GetBytes(HostName); |
| 90 | + buffer.AddRange(BitConverter.GetBytes(nameBuffer.Length)); |
| 91 | + buffer.AddRange(nameBuffer); |
| 92 | + |
| 93 | + buffer.AddRange(BitConverter.GetBytes(LobbyID.m_SteamID)); |
| 94 | + |
| 95 | + nameBuffer = Encoding.UTF8.GetBytes(Status); |
| 96 | + buffer.AddRange(BitConverter.GetBytes(nameBuffer.Length)); |
| 97 | + buffer.AddRange(nameBuffer); |
| 98 | + |
| 99 | + buffer.AddRange(BitConverter.GetBytes(Joinable)); |
| 100 | + buffer.AddRange(BitConverter.GetBytes(UsedSlots)); |
| 101 | + buffer.AddRange(BitConverter.GetBytes(TotalSlots)); |
| 102 | + buffer.AddRange(BitConverter.GetBytes(MaxSlots)); |
| 103 | + |
| 104 | + nameBuffer = Encoding.UTF8.GetBytes(CurrentSongId); |
| 105 | + buffer.AddRange(BitConverter.GetBytes(nameBuffer.Length)); |
| 106 | + buffer.AddRange(nameBuffer); |
| 107 | + |
| 108 | + nameBuffer = Encoding.UTF8.GetBytes(CurrentSongName); |
| 109 | + buffer.AddRange(BitConverter.GetBytes(nameBuffer.Length)); |
| 110 | + buffer.AddRange(nameBuffer); |
| 111 | + buffer.Add(CurrentSongDifficulty); |
| 112 | + buffer.AddRange(BitConverter.GetBytes(CurrentSongOffset)); |
| 113 | + |
| 114 | + buffer.Add((byte) Screen); |
| 115 | + |
| 116 | + nameBuffer = Encoding.UTF8.GetBytes(_gameplayModifiers); |
| 117 | + buffer.AddRange(BitConverter.GetBytes(nameBuffer.Length)); |
| 118 | + buffer.AddRange(nameBuffer); |
| 119 | + |
| 120 | + if (includeSize) |
| 121 | + buffer.InsertRange(0, BitConverter.GetBytes(buffer.Count)); |
| 122 | + |
| 123 | + return buffer.ToArray(); |
| 124 | + } |
| 125 | + |
| 126 | + public override bool Equals(object obj) |
| 127 | + { |
| 128 | + if (obj is PlayerPacket) |
| 129 | + { |
| 130 | + return (LobbyID == (obj as LobbyPacket).LobbyID); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + public override int GetHashCode() |
| 138 | + { |
| 139 | + return unchecked(this.LobbyID.m_SteamID.GetHashCode() * 17 + this.HostName.GetHashCode()); |
| 140 | + } |
| 141 | + public override string ToString() |
| 142 | + { |
| 143 | + return $"lobbyId={LobbyID},hostname={HostName},status={Status},joinable={Joinable},UsedSlots={UsedSlots},TotalSlots={TotalSlots},MaxSlots={MaxSlots},CurrentSongId={CurrentSongId},CurrentSongDifficulty={CurrentSongDifficulty},CurretnSongName={CurrentSongName},CurrentSongOffset={CurrentSongOffset},Screen={Screen},gameplayModifiers={_gameplayModifiers}"; |
| 144 | + } |
| 145 | + |
| 146 | + public string Serialize() |
| 147 | + { |
| 148 | + return Convert.ToBase64String(ToBytes(false)); |
| 149 | + } |
| 150 | + |
| 151 | + public byte[] DeSerialize(string body) |
| 152 | + { |
| 153 | + return Convert.FromBase64String(body); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments