/// <summary> /// Sends a packet through the socket. /// </summary> /// <param name="packID">The packet ID.</param> /// <param name="data">The data.</param> public void SendPacket(long packID, byte[] data) { if (RelevantSocket == null) { return; } DataStream outp = new DataStream(); DataWriter dw = new DataWriter(outp); dw.WriteInt(data.Length); dw.WriteVarInt(packID); dw.WriteBytes(data); SentWaiting += data.Length; try { RelevantSocket.BeginSend(data, 0, data.Length, SocketFlags.None, SendComplete, data.Length); } catch (Exception ex) { CommonUtilities.CheckException(ex); RelevantSocket.Close(); RelevantSocket = null; SysConsole.Output(OutputType.INFO, "[Connections:Error] " + ex.ToString()); } }
/// <summary> /// Gets a language document for the specified parameters. /// </summary> /// <param name="id">The document ID.</param> /// <param name="Files">The file system.</param> /// <param name="lang">The language to enforce for this read, if any.</param> /// <param name="confs">The custom configuration set to use, if any.</param> /// <returns></returns> public FDSSection GetLangDoc(string id, FileEngine Files, string lang = null, Dictionary <string, FDSSection> confs = null) { if (lang == null) { lang = CurrentLanguage; } if (confs == null) { confs = LanguageDocuments; } string idlow = id.ToLowerFast(); if (LanguageDocuments.TryGetValue(idlow, out FDSSection doc)) { return(doc); } string path = "info/text/" + idlow + "_" + lang + ".fds"; if (Files.TryReadFileText(path, out string dat)) { try { doc = new FDSSection(dat); LanguageDocuments[idlow] = doc; return(doc); } catch (Exception ex) { CommonUtilities.CheckException(ex); SysConsole.Output("Reading language documents", ex); } } LanguageDocuments[idlow] = null; return(null); }
/// <summary> /// Speaks aloud some text. /// </summary> /// <param name="text">The text.</param> /// <param name="male">Whether to be male (if not, female).</param> /// <param name="rate">The rate at which to speak.</param> public static void Speak(string text, bool male, int rate) { Task.Factory.StartNew(() => { try { #if WINDOWS if (TrySpeech) { SpeechSynthesizer speech = new SpeechSynthesizer(); VoiceInfo vi = null; foreach (InstalledVoice v in speech.GetInstalledVoices()) { if (!v.Enabled) { continue; } if (vi == null) { vi = v.VoiceInfo; } else if ((male && v.VoiceInfo.Gender == VoiceGender.Male) || (!male && v.VoiceInfo.Gender == VoiceGender.Female)) { vi = v.VoiceInfo; break; } } if (vi == null) { TrySpeech = false; } else { speech.SelectVoice(vi.Name); speech.Rate = rate; speech.Speak(text); } } #endif } catch (Exception ex) { CommonUtilities.CheckException(ex); TrySpeech = false; } if (!TrySpeech) { // TODO: Rate! String addme = male ? " -p 40" : " -p 95"; Process p = Process.Start("espeak", "\"" + text.Replace("\"", " quote ") + "\"" + addme); Console.WriteLine(p.MainModule.FileName); } }); }
/// <summary> /// Gets a string output for this stack note. /// </summary> /// <returns>The string output.</returns> public override string ToString() { try { return(Note + " (" + (Related ?? "None") + ")"); } catch (Exception ex) { CommonUtilities.CheckException(ex); return(Note + "(Exception parsing Related: " + ex.ToString() + ")"); } }
/// <summary> /// Run every frame to tick any network updates. /// </summary> public void Tick() { try { int avail = RelevantSocket.Available; if (!IsReady) { while (avail > 0) { int rd = RelevantSocket.Receive(OneByteHolder, 1, SocketFlags.None); if (rd <= 0) { break; } if (OneByteHolder[0] == 0) { string str = StringConversionHelper.UTF8Encoding.GetString(ReadData.ToArray()); if (!str.StartsWith(HEADER)) { throw new Exception("Connection refused: Not a game engine proper connection."); } str = str.Substring(HEADER.Length); int nextLine = str.IndexOf('\n'); string FGEData = str.Substring(0, nextLine); string[] keys = FGEData.Split(':'); int chan = int.Parse(keys[0]); if (!Network.Channels.Contains(chan)) { throw new Exception("Connection refused: Invalid (non-registered) channel."); } Channel = chan; str = str.Substring(nextLine + 1); OpeningInformation = str; if (Network.WantsReady?.Invoke(this) ?? true) { // TODO: Send counter header, all default packets, etc. IsReady = true; } else { throw new Exception("Connection refused: Rejected by game systems."); } ReadData.SetLength(0); return; } ReadData.WriteByte(OneByteHolder[0]); if (ReadData.Length > Network.HeaderLimit) { throw new Exception("Connection refused: Too much header data."); } avail = RelevantSocket.Available; } return; } // If we reached here, IsReady = true. while (avail > 0) { int rd = RelevantSocket.Receive(KiloByteHolder, Math.Min(avail, KiloByteHolder.Length), SocketFlags.None); if (rd <= 0) { return; } ReadData.Write(KiloByteHolder, 0, rd); if (ReadData.Length > Network.MaxPacketWaiting) { throw new Exception("Connection quick-closed: massive packet!"); } if (ReadData.Length > 5) { rd = ReadData.Read(KiloByteHolder, 0, 4); int d = BitConverter.ToInt32(KiloByteHolder, 0); if (ReadData.Length >= d) { byte[] packet = new byte[d]; rd = 0; while (rd < d) { int trd = ReadData.Read(packet, rd, d); if (trd <= 0) { throw new Exception("Connection quick-closed: streaming error."); } rd += trd; } DataStream packStr = new DataStream(packet); DataReader reader = new DataReader(packStr); long pid = reader.ReadVarInt(); ProcessPacket(pid, reader); if (ReadData.Length == 0) { ReadData.Ind = 0; } } else { ReadData.Ind -= rd; } } avail = RelevantSocket.Available; } } catch (Exception ex) { CommonUtilities.CheckException(ex); RelevantSocket.Close(); RelevantSocket = null; SysConsole.Output(OutputType.INFO, "[Connections:Error] " + ex.Message); } }