static bool OnToggle(UnityModManager.ModEntry modEntry, bool value) { if (value == enabled) { return(true); } enabled = value; if (enabled) { harmonyInstance = HarmonyInstance.Create(modEntry.Info.Id); harmonyInstance.PatchAll(Assembly.GetExecutingAssembly()); menu = new GameObject().AddComponent <MultiplayerMenu>(); statusMenu = new GameObject().AddComponent <MultiplayerStatusMenu>(); UnityEngine.Object.DontDestroyOnLoad(menu.gameObject); UnityEngine.Object.DontDestroyOnLoad(statusMenu.gameObject); MultiplayerUtils.StartMapLoading(); } else { harmonyInstance.UnpatchAll(harmonyInstance.Id); menu.EndMultiplayer(); UnityEngine.Object.Destroy(menu.gameObject); UnityEngine.Object.Destroy(statusMenu.gameObject); MultiplayerUtils.StopMapLoading(); } return(true); }
// Open replay editor on start to prevent null references to replay editor instance public void Start() { MultiplayerController.Instance = this; if (ReplayEditorController.Instance == null) { GameManagement.GameStateMachine.Instance.ReplayObject.SetActive(true); StartCoroutine(TurnOffReplay()); } var clipDict = Traverse.Create(SoundManager.Instance).Field("clipForName").GetValue<Dictionary<string, AudioClip>>(); MultiplayerUtils.InitializeClipToArrayByteDict(clipDict); MultiplayerUtils.audioPlayerNames.Clear(); foreach (ReplayAudioEventPlayer audioPlayer in ReplayEditorController.Instance.playbackController.AudioEventPlayers) { AudioSource newSource = Traverse.Create(audioPlayer).Property("audioSource").GetValue<AudioSource>(); if (newSource != null) MultiplayerUtils.audioPlayerNames.Add(newSource.name); } NewMultiplayerMenu.Instance.SendChatMessage = SendChatMessage; }
static bool OnToggle(UnityModManager.ModEntry modEntry, bool value) { if (value == enabled) { return(true); } enabled = value; if (enabled) { //Patch the replay editor harmonyInstance = HarmonyInstance.Create(modEntry.Info.Id); harmonyInstance.PatchAll(Assembly.GetExecutingAssembly()); menu = ModMenu.Instance.gameObject.AddComponent <MultiplayerMenu>(); utilityMenu = ModMenu.Instance.gameObject.AddComponent <MultiplayerUtilityMenu>(); uiBox = ModMenu.Instance.RegisterModMaker("Silentbaws", "Silentbaws", 0); uiBox.AddCustom("Patreon", DisplayPatreon, () => enabled); MultiplayerUtils.StartMapLoading(); } else { //Unpatch the replay editor harmonyInstance.UnpatchAll(harmonyInstance.Id); MultiplayerUtils.StopMapLoading(); if (multiplayerController != null) { multiplayerController.DisconnectFromServer(); } menu.CloseMultiplayerMenu(); UnityEngine.Object.Destroy(menu); UnityEngine.Object.Destroy(utilityMenu); } return(true); }
public void SaveTexture(int connectionId, byte[] buffer) { this.debugWriter.WriteLine("Saving texture in queue"); if (isCustom && buffer != null) { if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\Mods\\XLMultiplayer\\Temp\\Clothing")) { Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Mods\\XLMultiplayer\\Temp\\Clothing"); } this.path = Directory.GetCurrentDirectory() + "\\Mods\\XLMultiplayer\\Temp\\Clothing\\" + textureType.ToString() + MultiplayerUtils.CalculateMD5Bytes(buffer) + connectionId.ToString() + ".jpg"; try { File.WriteAllBytes(this.path, buffer); } catch (Exception e) { this.path = ""; this.debugWriter.WriteLine(e.ToString()); } } this.saved = true; this.debugWriter.WriteLine("Saved texture in queue"); }
public byte[] PackSounds() { List <List <AudioOneShotEvent> > newOneShotEvents = new List <List <AudioOneShotEvent> >(); List <List <AudioClipEvent> > newClipEvents = new List <List <AudioClipEvent> >(); List <List <AudioVolumeEvent> > newVolumeEvents = new List <List <AudioVolumeEvent> >(); List <List <AudioPitchEvent> > newPitchEvents = new List <List <AudioPitchEvent> >(); List <List <AudioCutoffEvent> > newCutoffEvents = new List <List <AudioCutoffEvent> >(); for (int i = 0; i < MultiplayerUtils.audioPlayerNames.Count; i++) { newOneShotEvents.Add(new List <AudioOneShotEvent>()); newClipEvents.Add(new List <AudioClipEvent>()); newVolumeEvents.Add(new List <AudioVolumeEvent>()); newPitchEvents.Add(new List <AudioPitchEvent>()); newCutoffEvents.Add(new List <AudioCutoffEvent>()); if (lastSentOneShots.Count < i + 1) { lastSentOneShots.Add(null); } if (lastSentClipEvents.Count < i + 1) { lastSentClipEvents.Add(null); } if (lastSentVolumeEvents.Count < i + 1) { lastSentVolumeEvents.Add(null); } if (lastSentPitchEvents.Count < i + 1) { lastSentPitchEvents.Add(null); } if (lastSentCutoffEvents.Count < i + 1) { lastSentCutoffEvents.Add(null); } // Out of range exception int previousOneShotIndex = lastSentOneShots[i] == null || !ReplayRecorder.Instance.oneShotEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i]) ? -1 : ReplayRecorder.Instance.oneShotEvents[MultiplayerUtils.audioPlayerNames[i]].IndexOf(lastSentOneShots[i]); int previousClipIndex = lastSentClipEvents[i] == null || !ReplayRecorder.Instance.clipEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i]) ? -1 : ReplayRecorder.Instance.clipEvents[MultiplayerUtils.audioPlayerNames[i]].IndexOf(lastSentClipEvents[i]); int previousVolumeIndex = lastSentVolumeEvents[i] == null || !ReplayRecorder.Instance.volumeEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i]) ? -1 : ReplayRecorder.Instance.volumeEvents[MultiplayerUtils.audioPlayerNames[i]].IndexOf(lastSentVolumeEvents[i]); int previousPitchIndex = lastSentPitchEvents[i] == null || !ReplayRecorder.Instance.pitchEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i]) ? -1 : ReplayRecorder.Instance.pitchEvents[MultiplayerUtils.audioPlayerNames[i]].IndexOf(lastSentPitchEvents[i]); int previousCutoffIndex = lastSentCutoffEvents[i] == null || !ReplayRecorder.Instance.cutoffEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i]) ? -1 : ReplayRecorder.Instance.cutoffEvents[MultiplayerUtils.audioPlayerNames[i]].IndexOf(lastSentCutoffEvents[i]); if (ReplayRecorder.Instance.oneShotEvents != null && ReplayRecorder.Instance.oneShotEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i])) { for (int j = previousOneShotIndex + 1; j < ReplayRecorder.Instance.oneShotEvents[MultiplayerUtils.audioPlayerNames[i]].Count; j++) { newOneShotEvents[i].Add(ReplayRecorder.Instance.oneShotEvents[MultiplayerUtils.audioPlayerNames[i]][j]); } } if (ReplayRecorder.Instance.clipEvents != null && ReplayRecorder.Instance.clipEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i])) { for (int j = previousClipIndex + 1; j < ReplayRecorder.Instance.clipEvents[MultiplayerUtils.audioPlayerNames[i]].Count; j++) { newClipEvents[i].Add(ReplayRecorder.Instance.clipEvents[MultiplayerUtils.audioPlayerNames[i]][j]); } } if (ReplayRecorder.Instance.volumeEvents != null && ReplayRecorder.Instance.volumeEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i])) { for (int j = previousVolumeIndex + 1; j < ReplayRecorder.Instance.volumeEvents[MultiplayerUtils.audioPlayerNames[i]].Count; j++) { newVolumeEvents[i].Add(ReplayRecorder.Instance.volumeEvents[MultiplayerUtils.audioPlayerNames[i]][j]); } } if (ReplayRecorder.Instance.pitchEvents != null && ReplayRecorder.Instance.pitchEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i])) { for (int j = previousPitchIndex + 1; j < ReplayRecorder.Instance.pitchEvents[MultiplayerUtils.audioPlayerNames[i]].Count; j++) { newPitchEvents[i].Add(ReplayRecorder.Instance.pitchEvents[MultiplayerUtils.audioPlayerNames[i]][j]); } } if (ReplayRecorder.Instance.cutoffEvents != null && ReplayRecorder.Instance.cutoffEvents.ContainsKey(MultiplayerUtils.audioPlayerNames[i])) { for (int j = previousCutoffIndex + 1; j < ReplayRecorder.Instance.cutoffEvents[MultiplayerUtils.audioPlayerNames[i]].Count; j++) { newCutoffEvents[i].Add(ReplayRecorder.Instance.cutoffEvents[MultiplayerUtils.audioPlayerNames[i]][j]); } } } List <List <byte> > newOneShotBytes = new List <List <byte> >(); List <List <byte> > newClipBytes = new List <List <byte> >(); List <List <byte> > newVolumeBytes = new List <List <byte> >(); List <List <byte> > newPitchBytes = new List <List <byte> >(); List <List <byte> > newCutoffBytes = new List <List <byte> >(); List <byte> newAudioBytes = new List <byte>(); for (int i = 0; i < MultiplayerUtils.audioPlayerNames.Count; i++) { newOneShotBytes.Add(new List <byte>()); newClipBytes.Add(new List <byte>()); newVolumeBytes.Add(new List <byte>()); newPitchBytes.Add(new List <byte>()); newCutoffBytes.Add(new List <byte>()); lastSentOneShots[i] = newOneShotEvents[i].Count > 0 ? newOneShotEvents[i][newOneShotEvents[i].Count - 1] : lastSentOneShots[i]; lastSentClipEvents[i] = newClipEvents[i].Count > 0 ? newClipEvents[i][newClipEvents[i].Count - 1] : lastSentClipEvents[i]; lastSentVolumeEvents[i] = newVolumeEvents[i].Count > 0 ? newVolumeEvents[i][newVolumeEvents[i].Count - 1] : lastSentVolumeEvents[i]; lastSentPitchEvents[i] = newPitchEvents[i].Count > 0 ? newPitchEvents[i][newPitchEvents[i].Count - 1] : lastSentPitchEvents[i]; lastSentCutoffEvents[i] = newCutoffEvents[i].Count > 0 ? newCutoffEvents[i][newCutoffEvents[i].Count - 1] : lastSentCutoffEvents[i]; // 21 audio event players // 2 floats, 1 string // 1 float, 1 string, 1 bool // 3x 2 floats for (int j = 0; j < newOneShotEvents[i].Count; j++) { byte clipNameIndex = MultiplayerUtils.GetArrayByteFromClipName(newOneShotEvents[i][j].clipName); float time = newOneShotEvents[i][j].time; float volume = newOneShotEvents[i][j].volumeScale; if (clipNameIndex != 255) { newOneShotBytes[i].Add(clipNameIndex); newOneShotBytes[i].AddRange(BitConverter.GetBytes(time)); newOneShotBytes[i].AddRange(BitConverter.GetBytes(volume)); } else { this.debugWriter.WriteLine($"Attempted encoding invalid one shot clip {newOneShotEvents[i][j].clipName}"); } } for (int j = 0; j < newClipEvents[i].Count; j++) { byte clipNameIndex = MultiplayerUtils.GetArrayByteFromClipName(newClipEvents[i][j].clipName); float time = newClipEvents[i][j].time; byte playing = newClipEvents[i][j].isPlaying ? (byte)1 : (byte)0; if (clipNameIndex != 255) { newClipBytes[i].Add(clipNameIndex); newClipBytes[i].AddRange(BitConverter.GetBytes(time)); newClipBytes[i].Add(playing); } else { this.debugWriter.WriteLine($"Attempted encoding invalid clip event clip {newClipEvents[i][j].clipName}"); } } for (int j = 0; j < newVolumeEvents[i].Count; j++) { float time = newVolumeEvents[i][j].time; float volume = newVolumeEvents[i][j].volume; newVolumeBytes[i].AddRange(BitConverter.GetBytes(time)); newVolumeBytes[i].AddRange(BitConverter.GetBytes(volume)); } for (int j = 0; j < newPitchEvents[i].Count; j++) { float time = newPitchEvents[i][j].time; float pitch = newPitchEvents[i][j].pitch; newPitchBytes[i].AddRange(BitConverter.GetBytes(time)); newPitchBytes[i].AddRange(BitConverter.GetBytes(pitch)); } for (int j = 0; j < newCutoffEvents[i].Count; j++) { float time = newCutoffEvents[i][j].time; float cutoff = newCutoffEvents[i][j].cutoff; newCutoffBytes[i].AddRange(BitConverter.GetBytes(time)); newCutoffBytes[i].AddRange(BitConverter.GetBytes(cutoff)); } newAudioBytes.AddRange(BitConverter.GetBytes(newOneShotEvents[i].Count)); newAudioBytes.AddRange(newOneShotBytes[i]); newAudioBytes.AddRange(BitConverter.GetBytes(newClipEvents[i].Count)); newAudioBytes.AddRange(newClipBytes[i]); newAudioBytes.AddRange(BitConverter.GetBytes(newVolumeEvents[i].Count)); newAudioBytes.AddRange(newVolumeBytes[i]); newAudioBytes.AddRange(BitConverter.GetBytes(newPitchEvents[i].Count)); newAudioBytes.AddRange(newPitchBytes[i]); newAudioBytes.AddRange(BitConverter.GetBytes(newCutoffEvents[i].Count)); newAudioBytes.AddRange(newCutoffBytes[i]); } byte[] compressedAudio = MultiplayerController.Compress(newAudioBytes.ToArray()); byte[] sendPacket = new byte[compressedAudio.Length + 1]; sendPacket[0] = (byte)OpCode.Sound; Array.Copy(compressedAudio, 0, sendPacket, 1, compressedAudio.Length); return(sendPacket); }
public void UnpackSounds(byte[] soundBytes) { int readBytes = 0; int numberOfAudioPlayers = MultiplayerUtils.audioPlayerNames.Count; List <AudioOneShotEvent>[] newOneShots = new List <AudioOneShotEvent> [numberOfAudioPlayers]; List <AudioClipEvent>[] newClipEvents = new List <AudioClipEvent> [numberOfAudioPlayers]; List <AudioVolumeEvent>[] newVolumeEvents = new List <AudioVolumeEvent> [numberOfAudioPlayers]; List <AudioPitchEvent>[] newPitchEvents = new List <AudioPitchEvent> [numberOfAudioPlayers]; List <AudioCutoffEvent>[] newCutoffEvents = new List <AudioCutoffEvent> [numberOfAudioPlayers]; float earliestSoundTime = float.MaxValue; for (int i = 0; i < numberOfAudioPlayers; i++) { newOneShots[i] = new List <AudioOneShotEvent>(); newClipEvents[i] = new List <AudioClipEvent>(); newVolumeEvents[i] = new List <AudioVolumeEvent>(); newPitchEvents[i] = new List <AudioPitchEvent>(); newCutoffEvents[i] = new List <AudioCutoffEvent>(); int oneShots = BitConverter.ToInt32(soundBytes, readBytes); readBytes += 4; for (int j = 0; j < oneShots; j++) { newOneShots[i].Add(new AudioOneShotEvent()); newOneShots[i][j].clipName = MultiplayerUtils.ClipNameFromArrayByte(soundBytes[readBytes]); newOneShots[i][j].time = BitConverter.ToSingle(soundBytes, readBytes + 1); newOneShots[i][j].volumeScale = BitConverter.ToSingle(soundBytes, readBytes + 5) * Main.settings.volumeMultiplier; earliestSoundTime = Mathf.Min(newOneShots[i][j].time, earliestSoundTime); readBytes += 9; } int clipEvents = BitConverter.ToInt32(soundBytes, readBytes); readBytes += 4; for (int j = 0; j < clipEvents; j++) { newClipEvents[i].Add(new AudioClipEvent()); newClipEvents[i][j].clipName = MultiplayerUtils.ClipNameFromArrayByte(soundBytes[readBytes]); newClipEvents[i][j].time = BitConverter.ToSingle(soundBytes, readBytes + 1); newClipEvents[i][j].isPlaying = soundBytes[readBytes + 5] == 1 ? true : false; earliestSoundTime = Mathf.Min(newClipEvents[i][j].time, earliestSoundTime); readBytes += 6; } int volumeEvents = BitConverter.ToInt32(soundBytes, readBytes); readBytes += 4; for (int j = 0; j < volumeEvents; j++) { newVolumeEvents[i].Add(new AudioVolumeEvent()); newVolumeEvents[i][j].time = BitConverter.ToSingle(soundBytes, readBytes); newVolumeEvents[i][j].volume = BitConverter.ToSingle(soundBytes, readBytes + 4) * Main.settings.volumeMultiplier; earliestSoundTime = Mathf.Min(newVolumeEvents[i][j].time, earliestSoundTime); readBytes += 8; } int pitchEvents = BitConverter.ToInt32(soundBytes, readBytes); readBytes += 4; for (int j = 0; j < pitchEvents; j++) { newPitchEvents[i].Add(new AudioPitchEvent()); newPitchEvents[i][j].time = BitConverter.ToSingle(soundBytes, readBytes); newPitchEvents[i][j].pitch = BitConverter.ToSingle(soundBytes, readBytes + 4); earliestSoundTime = Mathf.Min(newPitchEvents[i][j].time, earliestSoundTime); readBytes += 8; } int cutoffEvents = BitConverter.ToInt32(soundBytes, readBytes); readBytes += 4; for (int j = 0; j < cutoffEvents; j++) { newCutoffEvents[i].Add(new AudioCutoffEvent()); newCutoffEvents[i][j].time = BitConverter.ToSingle(soundBytes, readBytes); newCutoffEvents[i][j].cutoff = BitConverter.ToSingle(soundBytes, readBytes + 4); earliestSoundTime = Mathf.Min(newCutoffEvents[i][j].time, earliestSoundTime); readBytes += 8; } } MultiplayerSoundBufferObject newSoundBufferObject = new MultiplayerSoundBufferObject(numberOfAudioPlayers); soundQueue.Add(newSoundBufferObject); for (int i = 0; i < MultiplayerUtils.audioPlayerNames.Count; i++) { newSoundBufferObject.audioClipEvents[i] = newClipEvents[i]; newSoundBufferObject.audioOneShots[i] = newOneShots[i]; newSoundBufferObject.audioCutoffEvents[i] = newCutoffEvents[i]; newSoundBufferObject.audioPitchEvents[i] = newPitchEvents[i]; newSoundBufferObject.audioVolumeEvents[i] = newVolumeEvents[i]; } try { MultiplayerFrameBufferObject firstRealTime = this.replayAnimationFrames.FirstOrDefault(f => f.realFrameTime != -1f); if (firstRealTime != null) { foreach (ReplayAudioEventPlayer audioPlayer in replayController.AudioEventPlayers) { if (!ReferenceEquals(audioPlayer, null)) { if (audioPlayer.clipEvents != null) { MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.clipEvents, firstRealTime.realFrameTime, 0); } if (audioPlayer.cutoffEvents != null) { MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.cutoffEvents, firstRealTime.realFrameTime, 0); } if (audioPlayer.oneShotEvents != null) { MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.oneShotEvents, firstRealTime.realFrameTime, 0); } if (audioPlayer.pitchEvents != null) { MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.pitchEvents, firstRealTime.realFrameTime, 0); } if (audioPlayer.volumeEvents != null) { MultiplayerUtils.RemoveAudioEventsOlderThanExcept(audioPlayer.volumeEvents, firstRealTime.realFrameTime, 0); } } } } } catch (Exception) { } }
public void ReceiveCallbackTCP(IAsyncResult ar) { try { StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { state.readBytes += bytesRead; if (state.readBytes < 4) { handler.BeginReceive(state.buffer, state.readBytes, state.buffer.Length - state.readBytes, SocketFlags.None, ReceiveCallbackTCP, state); } else { lastAlive = elapsedTime.ElapsedMilliseconds > lastAlive ? elapsedTime.ElapsedMilliseconds : lastAlive; if (state.readBytes == 4) { state.buffer = new byte[BitConverter.ToInt32(state.buffer, 0)]; } if (state.readBytes - 4 == state.buffer.Length) { if (state.buffer[0] == (byte)OpCode.Texture) { controller.debugWriter.WriteLine("Filled texture buffer"); controller.textureQueue.Add(new MultiplayerSkinBuffer(state.buffer, (int)state.buffer[1], (MPTextureType)state.buffer[2])); } else if (state.buffer[0] == (byte)OpCode.Connect) { bufferObjects.Add(new BufferObject(state.buffer, state.buffer.Length)); } else if (state.buffer[0] == (byte)OpCode.Chat) { bufferObjects.Add(new BufferObject(state.buffer, state.buffer.Length)); } else if (state.buffer[0] == (byte)OpCode.Settings) { debugWriter.WriteLine("Adding player settings to queue"); bufferObjects.Add(new BufferObject(state.buffer, state.buffer.Length)); } else if (state.buffer[0] == (byte)OpCode.MapList) { MultiplayerUtils.LoadServerMaps(state.buffer); } else if (state.buffer[0] == (byte)OpCode.MapHash) { string returnMap = MultiplayerUtils.ChangeMap(state.buffer); if (returnMap != "") { Main.statusMenu.DisplayNoMap(returnMap); Main.menu.multiplayerManager.KillConnection(); } } else if (state.buffer[0] == (byte)OpCode.MapVote) { if (state.buffer[state.buffer.Length - 1] == 255) { Main.statusMenu.StartVotePopup(); } } else if (state.buffer[0] == (byte)OpCode.Disconnect) { Main.menu.multiplayerManager.ProcessMessage(state.buffer, state.buffer.Length); } BeginReceivingTCP(); } else { handler.BeginReceive(state.buffer, state.readBytes - 4, state.buffer.Length - state.readBytes + 4, SocketFlags.None, ReceiveCallbackTCP, state); } } } else { CloseConnection(); } } catch (Exception e) { if (tcpConnection.Connected) { BeginReceivingTCP(); } else { debugWriter.WriteLine(e.ToString()); CloseConnection(); } } }