private void btn_edit_Click(object sender, EventArgs e) { if (readyToPresent && btn_edit.Visible & btn_edit.Enabled) { EvidenceEditor editor = new EvidenceEditor(); editor.evidence = eviList; editor.selected = selectedEvidence; if (editor.ShowDialog() == DialogResult.OK) { int index = 0; foreach (Evidence item in editor.evidence) { if (item.index == selectedEvidence) { //EviData msgToSend = new EviData(eviList[index].name, eviList[index].desc, "Type: Evidence\r\n" + eviList[index].note, eviList[index].index); EviData msgToSend = new EviData(item.name, item.desc, item.note, item.index); var ms = new MemoryStream(); item.icon.Save(ms, item.icon.RawFormat); msgToSend.dataBytes = ms.ToArray(); byte[] msg = msgToSend.ToByte(); clientSocket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(OnSend), null); btn_back_PerformClick(); break; } index++; } } } }
private void OnReceive(IAsyncResult ar) { try { while (byteData[0] == 4 && byteData.Length < incomingSize) { //string test = ""; } clientSocket.EndReceive(ar); if (byteData[0] == 8) { EviData msg = new EviData(byteData); Evidence evi = new Evidence(); evi.name = msg.strName; evi.desc = msg.strDesc; evi.note = msg.strNote; evi.index = msg.index; using (MemoryStream ms = new MemoryStream(msg.dataBytes)) { evi.icon = Image.FromStream(ms,false,true); } eviList.Add(evi); //string dirName = ""; //for (int x = 0; x < msg.strName.Split('/').Length - 1; x++) //{ // dirName = dirName + msg.strName.Split('/')[x]; // if (x < msg.strName.Split('/').Length - 2) // dirName = dirName + '/'; //} //if (!Directory.Exists(dirName)) // Directory.CreateDirectory(dirName); //using (FileStream fs = new FileStream(msg.strName, FileMode.Create)) //{ // using (BinaryWriter w = new BinaryWriter(fs)) // { // if (msg.dataSize > 0) // w.Write(msg.dataBytes.Take(msg.dataSize).ToArray()); // } //} clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); } else { Data msgReceived = new Data(byteData); if (msgReceived.cmdCommand == Command.Disconnect) { MessageBox.Show("You are banned from this server!"); btn_Connect.Image = Image.FromFile("base/misc/btn_connect.png"); //clientSocket.Close(); //Close(); } else if (msgReceived.cmdCommand == Command.PacketSize) { incomingSize = Convert.ToInt32(msgReceived.strMessage); byteData = new byte[incomingSize]; //DialogResult = DialogResult.OK; Data msgToSend = new Data(); msgToSend.cmdCommand = Command.DataInfo; byte[] b = msgToSend.ToByte(); //Send the message to the server clientSocket.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnSend), null); clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); //Close(); } else if (msgReceived.cmdCommand == Command.DataInfo) { if (msgReceived.strMessage != null && msgReceived.strMessage != "") { string[] data = msgReceived.strMessage.Split('|'); int charCount = Convert.ToInt32(data[0]); if (charCount > 0) { for (int x = 1; x < charCount; x++) { charList.Add(data[x]); } } int songCount = Convert.ToInt32(data[charCount + 1]); if (songCount > 0) { for (int x = charCount + 2; x < charCount + 2 + songCount; x++) { musicList.Add(data[x]); } } } byteData = new byte[4194304]; //Do stuff with the evidence/extra binary data here } else clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); if (msgReceived.cmdCommand == Command.DataInfo) { //Program.charList = charList; //Program.musicList = musicList; //Program.connection = clientSocket; DialogResult = DialogResult.OK; Close(); } } } catch (SocketException) { if (MessageBox.Show("You have been disconnected from the server.", "AODXClient", MessageBoxButtons.OK) == DialogResult.OK | MessageBox.Show("You have been kicked from the server.", "AODXClient", MessageBoxButtons.OK) == DialogResult.Cancel) Close(); } catch (ObjectDisposedException) { } catch (Exception ex) { if (Program.debug) MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace.ToString(), "AODXClient", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void OnReceive(IAsyncResult ar) { try { clientSocket.EndReceive(ar); if (byteData[0] == 8) { EviData data = new EviData(byteData); Evidence evi = new Evidence(); evi.name = data.strName; evi.desc = data.strDesc; evi.note = data.strNote; evi.index = data.index; using (MemoryStream ms = new MemoryStream(data.dataBytes)) { evi.icon = Image.FromStream(ms, false, true); } bool found = false; foreach (Evidence item in eviList) { if (item.index == evi.index) { found = true; item.name = evi.name; item.note = evi.note; item.desc = evi.desc; item.icon = evi.icon; break; } } if (found == false) eviList.Add(evi); testimonyPB.Location = new Point(257, 3); testimonyPB.BringToFront(); PictureBox icon = new PictureBox(); icon.Image = evi.icon; icon.Location = new Point(6, 5); icon.Size = new Size(70, 70); icon.BringToFront(); testimonyPB.Invoke((MethodInvoker)delegate { //perform on the UI thread testimonyPB.Controls.Add(icon); }); Label name = new Label(); name.Text = evi.name; name.Location = new Point(91, 8); name.Size = new Size(155, 17); name.TextAlign = ContentAlignment.MiddleCenter; name.ForeColor = Color.DarkOrange; name.BackColor = Color.Transparent; //name.Font = new Font(fonts.Families[0], 12.0f, FontStyle.Bold); name.BringToFront(); testimonyPB.Invoke((MethodInvoker)delegate { //perform on the UI thread testimonyPB.Controls.Add(name); }); Label note = new Label(); note.Text = evi.note; note.Location = new Point(92, 26); note.Size = new Size(153, 44); //note.Font = new Font(fonts.Families[0], 12.0f); note.BackColor = Color.Transparent; note.BringToFront(); testimonyPB.Invoke((MethodInvoker)delegate { //perform on the UI thread testimonyPB.Controls.Add(note); }); Label desc = new Label(); desc.Text = evi.desc; desc.Location = new Point(9, 81); desc.Size = new Size(238, 45); //desc.Font = new Font(fonts.Families[0], 12.0f); desc.BackColor = Color.Transparent; desc.ForeColor = Color.White; desc.BringToFront(); testimonyPB.Invoke((MethodInvoker)delegate { //perform on the UI thread testimonyPB.Controls.Add(desc); }); testimonyPB.Size = new Size(256, 127); testimonyPB.Image = Image.FromFile("base/misc/inventory_update.png"); wr = new WaveFileReader("base/sounds/general/sfx-selectjingle.wav"); sfxPlayer.Initialize(wr); if (!mute) sfxPlayer.Play(); for (int x = 0; x <= 64; x++) { testimonyPB.Location = new Point(256 - (4 * x), 3); //icon.Location = new Point(256 + 6 - (2 * x), 3 + 5); //name.Location = new Point(256 + 91 - (2 * x), 3 + 8); //note.Location = new Point(256 + 92 - (2 * x), 3 + 26); //desc.Location = new Point(256 + 9 - (2 * x), 3 + 81); icon.Refresh(); name.Refresh(); note.Refresh(); desc.Refresh(); } System.Threading.Thread.Sleep(3000); for (int x = 0; x <= 64; x++) { testimonyPB.Location = new Point(0 - (4 * x), 3); //icon.Location = new Point(6 - (2 * x), 3 + 5); //name.Location = new Point(91 - (2 * x), 3 + 8); //note.Location = new Point(92 - (2 * x), 3 + 26); //desc.Location = new Point(9 - (2 * x), 3 + 81); icon.Refresh(); name.Refresh(); note.Refresh(); desc.Refresh(); } testimonyPB.Image = null; name.Dispose(); icon.Dispose(); desc.Dispose(); note.Dispose(); } else { Data msgReceived = new Data(byteData); //Accordingly process the message received switch (msgReceived.cmdCommand) { case Command.Login: break; case Command.Logout: break; case Command.ChangeMusic: if (msgReceived.strMessage != null && msgReceived.strMessage != "" & msgReceived.strName != null) { appendTxtLogSafe("<<<" + msgReceived.strName + " changed the music to " + msgReceived.strMessage + ">>>\r\n"); musicReader = new DmoMp3Decoder("base/sounds/music/" + msgReceived.strMessage); if (musicPlayer.PlaybackState != PlaybackState.Stopped) musicPlayer.Stop(); musicPlayer.Initialize(musicReader); if (!mute) musicPlayer.Play(); } break; case Command.ChangeHealth: if (msgReceived.strName == "def") { if (msgReceived.strMessage == "-1") defHealth--; else if (msgReceived.strMessage == "+1") defHealth++; } else if (msgReceived.strName == "pro") { if (msgReceived.strMessage == "-1") proHealth--; else if (msgReceived.strMessage == "+1") proHealth++; } updateHealth(); break; case Command.Message: case Command.Present: if (latestMsg != null && msgReceived.strName == latestMsg.strName) { newGuy = false; } else { newGuy = true; testimonyPB.Image = null; } latestMsg = msgReceived; objectLayerPB.Image = null; objectLayerPB.Location = new Point(0, 0); objectLayerPB.Size = new Size(256, 192); if (msgReceived.callout <= 3) { sendEnabled = false; curPreAnimTime = 0; curPreAnimTime = 0; curPreAnim = null; soundTime = 0; curSoundTime = 0; if (msgReceived.callout > 0) performCallout(); if (iniParser.GetSoundName(msgReceived.strName, msgReceived.anim) != "1" && iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim) > 0 & File.Exists("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav")) { sfxPlayer.Stop(); wr = new WaveFileReader("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"); sfxPlayer.Initialize(wr); soundTime = iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim); } /* if (iniParser.GetSoundName(msgReceived.strName, msgReceived.anim) != "1" && iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim) > 0 & (File.Exists("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav") | File.Exists("base/characters/" + latestMsg.strName + "/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"))) { sfxPlayer.Stop(); if (File.Exists("base/characters/" + latestMsg.strName + "/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav")) wr = new WaveFileReader("base/characters/" + latestMsg.strName + "/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"); else wr = new WaveFileReader("base/sounds/general/" + iniParser.GetSoundName(latestMsg.strName, latestMsg.anim) + ".wav"); sfxPlayer.Initialize(wr); soundTime = iniParser.GetSoundTime(msgReceived.strName, msgReceived.anim); } */ if (iniParser.GetAnimType(msgReceived.strName, msgReceived.anim) == 5) ChangeSides(true); else ChangeSides(); //If there is no pre-animation if (iniParser.GetAnimType(msgReceived.strName, msgReceived.anim) == 5 | iniParser.GetPreAnim(msgReceived.strName, msgReceived.anim) == null | iniParser.GetPreAnimTime(msgReceived.strName, msgReceived.anim) <= 0) { charLayerPB.Enabled = true; setCharSprite("base/characters/" + msgReceived.strName + "/(b)" + iniParser.GetAnim(msgReceived.strName, msgReceived.anim) + ".gif"); if (msgReceived.cmdCommand == Command.Present) { sfxPlayer.Stop(); wr = new WaveFileReader("base/sounds/general/sfx-shooop.wav"); sfxPlayer.Initialize(wr); if (!mute) sfxPlayer.Play(); switch (iniParser.GetSide(msgReceived.strName)) { case "def": testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceRight.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(173, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; case "pro": testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceLeft.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(13, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; case "hld": testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceLeft.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(13, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; case "hlp": testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceRight.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(173, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; default: testimonyPB.Image = Image.FromFile("base/misc/ani_evidenceRight.gif"); System.Threading.Thread.Sleep(100); testimonyPB.Location = new Point(173, 13); testimonyPB.Size = new Size(70, 70); testimonyPB.Image = eviList[Convert.ToInt32(msgReceived.strMessage.Split('|').Last())].icon; break; } msgReceived.strMessage = msgReceived.strMessage.Split('|')[0]; } prepWriteDispBoxes(msgReceived, msgReceived.textColor); } else //if there is a pre-animation { //charLayerPB.Enabled = false; setCharSprite("base/characters/" + msgReceived.strName + "/" + iniParser.GetPreAnim(msgReceived.strName, msgReceived.anim) + ".gif"); preAnimTime = iniParser.GetPreAnimTime(msgReceived.strName, msgReceived.anim); curPreAnim = iniParser.GetPreAnim(msgReceived.strName, msgReceived.anim); } //dispTextRedraw.Enabled = true; } else { performCallout(); } break; case Command.List: appendTxtLogSafe("<<<" + strName + " has entered the courtroom>>>\r\n"); break; case Command.DataInfo: //Do the stuff with the incoming server data here //The user has logged into the system so we now request the server to send //the names of all users who are in the chat room Data msgToSend = new Data(); msgToSend.cmdCommand = Command.Login; msgToSend.strName = strName; byteData = new byte[1048576]; byteData = msgToSend.ToByte(); clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null); byteData = new byte[1048576]; break; case Command.PacketSize: break; } if (msgReceived.strMessage != null & msgReceived.cmdCommand == Command.Message | msgReceived.cmdCommand == Command.Login | msgReceived.cmdCommand == Command.Logout) { if (msgReceived.callout <= 3) appendTxtLogSafe(msgReceived.strMessage + "\r\n"); } if (msgReceived.cmdCommand != Command.PacketSize) byteData = new byte[1048576]; else byteData = new byte[Convert.ToInt32(msgReceived.strMessage)]; } clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); } catch (SocketException) { if (MessageBox.Show("You have been kicked from the server.", "AODXClient", MessageBoxButtons.OK) == DialogResult.OK) { Close(); } } catch (ObjectDisposedException) { } catch (Exception ex) { if (Program.debug) MessageBox.Show(ex.Message + ".\r\n" + ex.StackTrace.ToString(), "AODXClient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error); } }