The BepInEx console will not appear when launching like it does for other games on Thunderstore (you can turn it back on in your BepInEx.cfg file). If your PEAK crashes on startup, add -dx12 to your launch parameters.
Decompiled source of Twitch Bing Bong v1.1.7
TwitchBingBong.dll
Decompiled a month agousing System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Text.RegularExpressions; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using Microsoft.CodeAnalysis; using Microsoft.Extensions.Logging; using Photon.Pun; using Photon.Realtime; using TwitchLib.Client; using TwitchLib.Client.Enums; using TwitchLib.Client.Events; using TwitchLib.Client.Models; using TwitchLib.Communication.Interfaces; using UnityEngine; using UnityEngine.Networking; using Zorro.Core; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)] [assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")] [assembly: AssemblyCompany("TwitchBingBong")] [assembly: AssemblyConfiguration("Debug")] [assembly: AssemblyFileVersion("1.1.3.0")] [assembly: AssemblyInformationalVersion("1.1.3")] [assembly: AssemblyProduct("TwitchBingBong")] [assembly: AssemblyTitle("TwitchBingBong")] [assembly: AssemblyVersion("1.1.3.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } public class BingBongTTS : MonoBehaviourPunCallbacks { [CompilerGenerated] private sealed class <>c__DisplayClass52_0 { public bool ok; internal void <ClientPrepareClip>b__0(bool r) { ok = r; } } [CompilerGenerated] private sealed class <ClientPrepareClip>d__52 : IEnumerator<object>, IDisposable, IEnumerator { private int <>1__state; private object <>2__current; public string text; public string utteranceId; public BingBongTTS <>4__this; private <>c__DisplayClass52_0 <>8__1; private string <temp>5__2; private UnityWebRequest <uwr>5__3; object IEnumerator<object>.Current { [DebuggerHidden] get { return <>2__current; } } object IEnumerator.Current { [DebuggerHidden] get { return <>2__current; } } [DebuggerHidden] public <ClientPrepareClip>d__52(int <>1__state) { this.<>1__state = <>1__state; } [DebuggerHidden] void IDisposable.Dispose() { int num = <>1__state; if (num == -3 || num == 2) { try { } finally { <>m__Finally1(); } } <>8__1 = null; <temp>5__2 = null; <uwr>5__3 = null; <>1__state = -2; } private bool MoveNext() { //IL_015e: Unknown result type (might be due to invalid IL or missing references) //IL_0164: Invalid comparison between Unknown and I4 try { switch (<>1__state) { default: return false; case 0: <>1__state = -1; <>8__1 = new <>c__DisplayClass52_0(); <>4__this._preparedClip = null; <temp>5__2 = Path.Combine(Application.temporaryCachePath, $"tts_{utteranceId}_{DateTime.Now.Ticks}.wav"); <>8__1.ok = false; <>2__current = <>4__this.RunPiperToFile(text, <temp>5__2, delegate(bool r) { <>8__1.ok = r; }); <>1__state = 1; return true; case 1: <>1__state = -1; if (!<>8__1.ok || !File.Exists(<temp>5__2)) { <>4__this.TryAckReady(utteranceId, ok: false); return false; } <uwr>5__3 = UnityWebRequestMultimedia.GetAudioClip("file://" + <temp>5__2, (AudioType)20); <>1__state = -3; <>2__current = <uwr>5__3.SendWebRequest(); <>1__state = 2; return true; case 2: <>1__state = -3; if ((int)<uwr>5__3.result == 1) { <>4__this._preparedClip = DownloadHandlerAudioClip.GetContent(<uwr>5__3); } <>m__Finally1(); <uwr>5__3 = null; try { File.Delete(<temp>5__2); } catch { } if ((Object)(object)<>4__this._preparedClip != (Object)null) { <>4__this._clips[utteranceId] = <>4__this._preparedClip; } <>4__this.TryAckReady(utteranceId, (Object)(object)<>4__this._preparedClip != (Object)null); <>4__this.TryStart(utteranceId); return false; } } catch { //try-fault ((IDisposable)this).Dispose(); throw; } } bool IEnumerator.MoveNext() { //ILSpy generated this explicit interface implementation from .override directive in MoveNext return this.MoveNext(); } private void <>m__Finally1() { <>1__state = -1; if (<uwr>5__3 != null) { ((IDisposable)<uwr>5__3).Dispose(); } } [DebuggerHidden] void IEnumerator.Reset() { throw new NotSupportedException(); } } [CompilerGenerated] private sealed class <HostPrepareBarrierThenStart>d__50 : IEnumerator<object>, IDisposable, IEnumerator { private int <>1__state; private object <>2__current; public string text; public BingBongTTS <>4__this; private float <started>5__1; private double <goAtNetworkTime>5__2; private bool <hostPrepared>5__3; private bool <everyoneAnswered>5__4; private bool <someoneAnswered>5__5; object IEnumerator<object>.Current { [DebuggerHidden] get { return <>2__current; } } object IEnumerator.Current { [DebuggerHidden] get { return <>2__current; } } [DebuggerHidden] public <HostPrepareBarrierThenStart>d__50(int <>1__state) { this.<>1__state = <>1__state; } [DebuggerHidden] void IDisposable.Dispose() { <>1__state = -2; } private bool MoveNext() { //IL_0379: Unknown result type (might be due to invalid IL or missing references) //IL_0383: Expected O, but got Unknown //IL_0357: Unknown result type (might be due to invalid IL or missing references) //IL_0361: Expected O, but got Unknown switch (<>1__state) { default: return false; case 0: <>1__state = -1; if (!<>4__this._piperReady) { Log.LogWarning((object)"[TTS] Piper not initialized; skipping."); return false; } <>4__this._currentUtteranceId = Guid.NewGuid().ToString(); <>4__this._readyOK.Clear(); <>4__this._readyAll.Clear(); <>4__this._preparedClip = null; <>4__this._pendingGo.Clear(); <>4__this._clips.Clear(); Log.LogInfo((object)("[TTS] PREPARE “" + text + "” (" + <>4__this._currentUtteranceId + ")")); <>4__this._pv.RPC("RPC_PrepareTTS", (RpcTarget)0, new object[2] { text, <>4__this._currentUtteranceId }); <>4__this.RebuildExpectedSnapshot(); <started>5__1 = Time.time; goto IL_0254; case 1: <>1__state = -1; goto IL_0254; case 2: <>1__state = -1; break; case 3: { <>1__state = -1; break; } IL_0254: <hostPrepared>5__3 = (Object)(object)<>4__this._preparedClip != (Object)null; <everyoneAnswered>5__4 = <>4__this._readyAll.IsSupersetOf(<>4__this._expected); <someoneAnswered>5__5 = <>4__this._readyAll.Overlaps(<>4__this._expected) || <>4__this._readyAll.Count > 0; if (!(<hostPrepared>5__3 & <everyoneAnswered>5__4)) { if (!(Time.time - <started>5__1 >= 15f)) { <>2__current = null; <>1__state = 1; return true; } if (!(<hostPrepared>5__3 & <someoneAnswered>5__5)) { Log.LogWarning((object)"[TTS] Timeout: nobody answered; skipping line."); return false; } Log.LogWarning((object)"[TTS] Timeout: proceeding with answered clients only."); } <goAtNetworkTime>5__2 = PhotonNetwork.Time + 0.95; Log.LogInfo((object)$"[TTS] GO {<>4__this._currentUtteranceId} at net={<goAtNetworkTime>5__2:F3} (answered {<>4__this._readyAll.Count}/{<>4__this._expected.Count}, ok {<>4__this._readyOK.Count})"); <>4__this._pv.RPC("RPC_StartPreparedTTS", (RpcTarget)0, new object[2] { <>4__this._currentUtteranceId, <goAtNetworkTime>5__2 }); if ((Object)(object)<>4__this._preparedClip != (Object)null) { <>2__current = (object)new WaitForSeconds(<>4__this._preparedClip.length + 0.25f); <>1__state = 2; return true; } <>2__current = (object)new WaitForSeconds(0.2f); <>1__state = 3; return true; } return false; } bool IEnumerator.MoveNext() { //ILSpy generated this explicit interface implementation from .override directive in MoveNext return this.MoveNext(); } [DebuggerHidden] void IEnumerator.Reset() { throw new NotSupportedException(); } } [CompilerGenerated] private sealed class <ProcessQueue>d__49 : IEnumerator<object>, IDisposable, IEnumerator { private int <>1__state; private object <>2__current; public BingBongTTS <>4__this; private string <next>5__1; object IEnumerator<object>.Current { [DebuggerHidden] get { return <>2__current; } } object IEnumerator.Current { [DebuggerHidden] get { return <>2__current; } } [DebuggerHidden] public <ProcessQueue>d__49(int <>1__state) { this.<>1__state = <>1__state; } [DebuggerHidden] void IDisposable.Dispose() { <next>5__1 = null; <>1__state = -2; } private bool MoveNext() { switch (<>1__state) { default: return false; case 0: <>1__state = -1; <>4__this._isSpeaking = true; goto IL_00a5; case 1: <>1__state = -1; goto IL_0086; case 2: { <>1__state = -1; <next>5__1 = null; goto IL_00a5; } IL_00a5: if (<>4__this._queue.Count > 0) { <next>5__1 = <>4__this._queue.Dequeue(); if (<>4__this.ActAsHost()) { <>2__current = <>4__this.HostPrepareBarrierThenStart(<next>5__1); <>1__state = 1; return true; } goto IL_0086; } <>4__this._isSpeaking = false; return false; IL_0086: <>2__current = null; <>1__state = 2; return true; } } bool IEnumerator.MoveNext() { //ILSpy generated this explicit interface implementation from .override directive in MoveNext return this.MoveNext(); } [DebuggerHidden] void IEnumerator.Reset() { throw new NotSupportedException(); } } [CompilerGenerated] private sealed class <RegisterWhenInRoom>d__45 : IEnumerator<object>, IDisposable, IEnumerator { private int <>1__state; private object <>2__current; public BingBongTTS <>4__this; object IEnumerator<object>.Current { [DebuggerHidden] get { return <>2__current; } } object IEnumerator.Current { [DebuggerHidden] get { return <>2__current; } } [DebuggerHidden] public <RegisterWhenInRoom>d__45(int <>1__state) { this.<>1__state = <>1__state; } [DebuggerHidden] void IDisposable.Dispose() { <>1__state = -2; } private bool MoveNext() { switch (<>1__state) { default: return false; case 0: <>1__state = -1; break; case 1: <>1__state = -1; break; } if (!PhotonNetwork.InRoom) { <>2__current = null; <>1__state = 1; return true; } if (PhotonNetwork.IsMasterClient) { <>4__this._modClients.Add(<>4__this._userId); } if (PhotonNetwork.IsMasterClient) { <>4__this._actorToUserId[PhotonNetwork.LocalPlayer.ActorNumber] = <>4__this._userId; } <>4__this._pv.RPC("RPC_RegisterMod", (RpcTarget)2, new object[1] { <>4__this._userId }); return false; } bool IEnumerator.MoveNext() { //ILSpy generated this explicit interface implementation from .override directive in MoveNext return this.MoveNext(); } [DebuggerHidden] void IEnumerator.Reset() { throw new NotSupportedException(); } } [CompilerGenerated] private sealed class <RunPiperToFile>d__61 : IEnumerator<object>, IDisposable, IEnumerator { private int <>1__state; private object <>2__current; public string text; public string outPath; public Action<bool> done; public BingBongTTS <>4__this; private string <len>5__1; private string <args>5__2; private ProcessStartInfo <psi>5__3; private Process <proc>5__4; private Exception <ex>5__5; private FileInfo <fi>5__6; object IEnumerator<object>.Current { [DebuggerHidden] get { return <>2__current; } } object IEnumerator.Current { [DebuggerHidden] get { return <>2__current; } } [DebuggerHidden] public <RunPiperToFile>d__61(int <>1__state) { this.<>1__state = <>1__state; } [DebuggerHidden] void IDisposable.Dispose() { <len>5__1 = null; <args>5__2 = null; <psi>5__3 = null; <proc>5__4 = null; <ex>5__5 = null; <fi>5__6 = null; <>1__state = -2; } private bool MoveNext() { switch (<>1__state) { default: return false; case 0: <>1__state = -1; done(obj: false); if (!<>4__this._piperReady) { Log.LogError((object)"[TTS] Piper files missing."); return false; } try { Directory.CreateDirectory(Path.GetDirectoryName(outPath) ?? Application.temporaryCachePath); } catch { } <len>5__1 = <>4__this.PiperSpeed.ToString(CultureInfo.InvariantCulture); <args>5__2 = "-m \"" + <>4__this._piperModelPath + "\" -c \"" + <>4__this._piperConfigPath + "\" -f \"" + outPath + "\" -q --length_scale " + <len>5__1; if (<>4__this.PiperSentencePauseMs > 0f) { <args>5__2 += $" --sentence_silence {Mathf.RoundToInt(<>4__this.PiperSentencePauseMs)}"; } <psi>5__3 = new ProcessStartInfo { FileName = <>4__this._piperExePath, Arguments = <args>5__2, UseShellExecute = false, RedirectStandardInput = true, RedirectStandardError = true, CreateNoWindow = true }; <proc>5__4 = null; try { <proc>5__4 = Process.Start(<psi>5__3); } catch (Exception ex) { <ex>5__5 = ex; Log.LogError((object)("[TTS] Failed to start piper: " + <ex>5__5.Message)); return false; } try { <proc>5__4.StandardInput.WriteLine(text); <proc>5__4.StandardInput.Close(); } catch { } break; case 1: <>1__state = -1; break; } if (!<proc>5__4.HasExited) { <>2__current = null; <>1__state = 1; return true; } if (<proc>5__4.ExitCode != 0) { try { Log.LogError((object)("[TTS] Piper stderr: " + <proc>5__4.StandardError.ReadToEnd())); } catch { } return false; } if (!File.Exists(outPath)) { return false; } try { <fi>5__6 = new FileInfo(outPath); if (<fi>5__6.Length < 128) { return false; } <fi>5__6 = null; } catch { } done(obj: true); return false; } bool IEnumerator.MoveNext() { //ILSpy generated this explicit interface implementation from .override directive in MoveNext return this.MoveNext(); } [DebuggerHidden] void IEnumerator.Reset() { throw new NotSupportedException(); } } private static readonly ManualLogSource Log = Logger.CreateLogSource("BingBongTTS"); private static BingBongTTS _instance; private string _piperExePath = ""; private string _piperModelPath = ""; private string _piperConfigPath = ""; private bool _piperReady = false; public float PiperSpeed = 1f; public float PiperSentencePauseMs = 0f; public string VoiceName = "Ryan-High"; private AudioSource _src; [Range(0f, 1f)] public float BaseVolume = 1f; private Transform _target; private float _nextTargetSearchAt; private const float TargetSearchPeriod = 0.5f; private float _mouthOpen; public float MouthUpdateHz = 13f; public float MouthSampleWindowMs = 64f; public float MouthAttackHz = 25f; public float MouthReleaseHz = 15f; public float MouthGain = 25f; public float MouthFloor = 0.021f; public float MouthKnee = 0.07f; public float MouthMaxDeltaPerSec = 6f; private float[] _outSamples; private float _mouthNextAt; private float _mouthPeak = 0.05f; private const int PV_ID = 71539131; private PhotonView _pv; private readonly string _userId = Guid.NewGuid().ToString(); private readonly HashSet<string> _modClients = new HashSet<string>(); private readonly HashSet<string> _readyOK = new HashSet<string>(); private readonly HashSet<string> _readyAll = new HashSet<string>(); private readonly HashSet<string> _expected = new HashSet<string>(); private readonly Dictionary<int, string> _actorToUserId = new Dictionary<int, string>(); private string _currentUtteranceId = ""; private Coroutine _prepareCo; private AudioClip _preparedClip; private readonly Dictionary<string, double> _pendingGo = new Dictionary<string, double>(); private readonly Dictionary<string, AudioClip> _clips = new Dictionary<string, AudioClip>(); private readonly Queue<string> _queue = new Queue<string>(); private bool _isSpeaking; private Animator _anim; private int _animMouthHash = -1; private void Awake() { if ((Object)(object)_instance != (Object)null) { Object.Destroy((Object)(object)((Component)this).gameObject); return; } _instance = this; Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject); _src = ((Component)this).gameObject.AddComponent<AudioSource>(); _src.spatialBlend = 1f; _src.rolloffMode = (AudioRolloffMode)0; _src.minDistance = 3f; _src.maxDistance = 40f; _src.priority = 128; _src.loop = false; _pv = ((Component)this).gameObject.AddComponent<PhotonView>(); _pv.ViewID = 71539131; TryInitPiper(); ((MonoBehaviour)this).StartCoroutine(RegisterWhenInRoom()); } private void TryBindAnimator() { if ((Object)(object)_anim != (Object)null) { return; } Transform val = (Object.op_Implicit((Object)(object)_target) ? _target.root : null); if (Object.op_Implicit((Object)(object)val)) { _anim = ((Component)val).GetComponentInChildren<Animator>(true); if ((Object)(object)_anim != (Object)null) { _animMouthHash = Animator.StringToHash("Mouth Blend"); Log.LogInfo((object)("[Mouth] Driving Animator float 'Mouth Blend' on " + ((Object)((Component)_anim).gameObject).name)); } } } [IteratorStateMachine(typeof(<RegisterWhenInRoom>d__45))] private IEnumerator RegisterWhenInRoom() { //yield-return decompiler failed: Unexpected instruction in Iterator.Dispose() return new <RegisterWhenInRoom>d__45(0) { <>4__this = this }; } public bool TryInitPiper() { string path = Path.GetDirectoryName(((object)this).GetType().Assembly.Location) ?? Environment.CurrentDirectory; _piperExePath = Path.Combine(path, "piper.exe"); _piperModelPath = Path.Combine(path, "en_US-ryan-high.onnx"); _piperConfigPath = _piperModelPath + ".json"; _piperReady = File.Exists(_piperExePath) && File.Exists(_piperModelPath) && File.Exists(_piperConfigPath); if (_piperReady) { Log.LogInfo((object)$"[TTS] Piper ready. exe={_piperExePath}, model={_piperModelPath}, length_scale={PiperSpeed.ToString(CultureInfo.InvariantCulture)}, sentence_silence={PiperSentencePauseMs}ms"); } else { if (!File.Exists(_piperExePath)) { Log.LogError((object)("[TTS] Missing piper.exe at: " + _piperExePath)); } if (!File.Exists(_piperModelPath)) { Log.LogError((object)("[TTS] Missing voice model at: " + _piperModelPath)); } if (!File.Exists(_piperConfigPath)) { Log.LogError((object)("[TTS] Missing model config at: " + _piperConfigPath)); } } return _piperReady; } private bool ActAsHost() { return PhotonNetwork.IsMasterClient; } public void Enqueue(string text) { if (!string.IsNullOrWhiteSpace(text)) { text = new string((from c in text.Trim() where !char.IsControl(c) || c == ' ' select c).ToArray()); _queue.Enqueue(text); if (!_isSpeaking) { ((MonoBehaviour)this).StartCoroutine(ProcessQueue()); } } } [IteratorStateMachine(typeof(<ProcessQueue>d__49))] private IEnumerator ProcessQueue() { //yield-return decompiler failed: Unexpected instruction in Iterator.Dispose() return new <ProcessQueue>d__49(0) { <>4__this = this }; } [IteratorStateMachine(typeof(<HostPrepareBarrierThenStart>d__50))] private IEnumerator HostPrepareBarrierThenStart(string text) { //yield-return decompiler failed: Unexpected instruction in Iterator.Dispose() return new <HostPrepareBarrierThenStart>d__50(0) { <>4__this = this, text = text }; } [PunRPC] private void RPC_PrepareTTS(string text, string utteranceId) { _currentUtteranceId = utteranceId; if (_prepareCo != null) { ((MonoBehaviour)this).StopCoroutine(_prepareCo); _prepareCo = null; } _pendingGo.Clear(); _clips.Clear(); _prepareCo = ((MonoBehaviour)this).StartCoroutine(ClientPrepareClip(text, utteranceId)); } [IteratorStateMachine(typeof(<ClientPrepareClip>d__52))] private IEnumerator ClientPrepareClip(string text, string utteranceId) { //yield-return decompiler failed: Unexpected instruction in Iterator.Dispose() return new <ClientPrepareClip>d__52(0) { <>4__this = this, text = text, utteranceId = utteranceId }; } private void TryAckReady(string utteranceId, bool ok) { try { _pv.RPC("RPC_ReadyAck", (RpcTarget)2, new object[3] { utteranceId, _userId, ok }); } catch { } } [PunRPC] private void RPC_ReadyAck(string utteranceId, string userId, bool ok) { if (PhotonNetwork.IsMasterClient && !(utteranceId != _currentUtteranceId)) { _modClients.Add(userId); _readyAll.Add(userId); if (ok) { _readyOK.Add(userId); } Log.LogInfo((object)$"[TTS] READY {userId} ok={ok} ({_readyOK.Count}/{_expected.Count})"); } } [PunRPC] private void RPC_StartPreparedTTS(string utteranceId, double goAtNetworkTime) { _currentUtteranceId = utteranceId; _pendingGo[utteranceId] = goAtNetworkTime; TryStart(utteranceId); } private void TryStart(string utteranceId) { if (_pendingGo.TryGetValue(utteranceId, out var value) && _clips.TryGetValue(utteranceId, out var value2) && !((Object)(object)value2 == (Object)null)) { if (_src.isPlaying) { _src.Stop(); } _src.clip = null; _src.clip = value2; double time = PhotonNetwork.Time; double num = value - time; if (num > 0.02) { _src.PlayScheduled(AudioSettings.dspTime + num); Log.LogInfo((object)$"[TTS] schedule in {num:F3}s (net now={time:F3})"); } else { _src.Play(); Log.LogInfo((object)$"[TTS] late by {0.0 - num:F3}s -> playing immediately"); } _pendingGo.Remove(utteranceId); _clips.Remove(utteranceId); } } [PunRPC] private void RPC_RegisterMod(string userId, PhotonMessageInfo info) { //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) if (PhotonNetwork.IsMasterClient) { int num = ((info.Sender != null) ? info.Sender.ActorNumber : PhotonNetwork.LocalPlayer.ActorNumber); _actorToUserId[num] = userId; _modClients.Add(userId); Log.LogInfo((object)$"[TTS] Registered mod client: {userId} (actor {num}) (total {_modClients.Count})"); } } public static void NotifyRoomRosterPossiblyChanged() { _instance?._PruneGhostsAndRebuildExpected(); } private void _PruneGhostsAndRebuildExpected() { if (!PhotonNetwork.IsMasterClient) { return; } HashSet<int> presentActors = new HashSet<int>(PhotonNetwork.PlayerList.Select((Player p) => p.ActorNumber)); List<int> list = _actorToUserId.Keys.Where((int a) => !presentActors.Contains(a)).ToList(); foreach (int item in list) { string text = _actorToUserId[item]; _actorToUserId.Remove(item); _modClients.Remove(text); _readyAll.Remove(text); _readyOK.Remove(text); _expected.Remove(text); Log.LogInfo((object)$"[TTS] Pruned ghost: {text} (actor {item})"); } RebuildExpectedSnapshot(); } private void RebuildExpectedSnapshot() { _expected.Clear(); Player[] playerList = PhotonNetwork.PlayerList; foreach (Player val in playerList) { if (_actorToUserId.TryGetValue(val.ActorNumber, out var value) && _modClients.Contains(value)) { _expected.Add(value); } } if (PhotonNetwork.IsMasterClient && _actorToUserId.TryGetValue(PhotonNetwork.LocalPlayer.ActorNumber, out var value2)) { _expected.Add(value2); } Log.LogInfo((object)$"[TTS] Expected snapshot = {_expected.Count} clients."); } [IteratorStateMachine(typeof(<RunPiperToFile>d__61))] private IEnumerator RunPiperToFile(string text, string outPath, Action<bool> done) { //yield-return decompiler failed: Unexpected instruction in Iterator.Dispose() return new <RunPiperToFile>d__61(0) { <>4__this = this, text = text, outPath = outPath, done = done }; } private void LateUpdate() { //IL_0084: Unknown result type (might be due to invalid IL or missing references) if (!Object.op_Implicit((Object)(object)_target) && Time.time >= _nextTargetSearchAt) { _nextTargetSearchAt = Time.time + 0.5f; GameObject val = GameObject.Find("Bing Bong Plush") ?? GameObject.Find("BingBong(Clone)"); if (Object.op_Implicit((Object)(object)val)) { _target = val.transform; } } if (Object.op_Implicit((Object)(object)_target)) { ((Component)this).transform.position = _target.position; } bool flag = Singleton<PeakHandler>.Instance?.isPlayingCinematic ?? false; _src.spatialBlend = (flag ? 0f : 1f); _src.volume = (((Object)(object)_target != (Object)null) ? BaseVolume : 0f); if ((Object)(object)_src != (Object)null && _src.isPlaying && Time.time >= _mouthNextAt) { _mouthNextAt = Time.time + 1f / Mathf.Max(10f, MouthUpdateHz); int outputSampleRate = AudioSettings.outputSampleRate; int num = Mathf.Clamp(Mathf.RoundToInt((float)outputSampleRate * (MouthSampleWindowMs / 1000f)), 256, 4096); if (_outSamples == null || _outSamples.Length != num) { _outSamples = new float[num]; } _src.GetOutputData(_outSamples, 0); float num2 = 0f; for (int i = 0; i < num; i++) { float num3 = _outSamples[i]; num2 += num3 * num3; } float num4 = Mathf.Sqrt(num2 / (float)Mathf.Max(1, num)); if (num4 < MouthFloor) { num4 = 0f; } float num5 = 1f / Mathf.Max(10f, MouthUpdateHz); float num6 = Mathf.Exp((0f - num5) / 1.8f); if (num4 > _mouthPeak) { _mouthPeak = num4; } else { _mouthPeak *= num6; } float num7 = ((_mouthPeak > 1E-06f) ? (num4 / _mouthPeak) : 0f); float num8 = Mathf.InverseLerp(MouthKnee, 1f, num7); float num9 = Mathf.Pow(num8, 0.75f); float num10 = Mathf.Clamp01(num9 * MouthGain); float deltaTime = Time.deltaTime; float num11 = ((num10 > _mouthOpen) ? MouthAttackHz : MouthReleaseHz); float num12 = 1f - Mathf.Exp((0f - num11) * deltaTime); float num13 = _mouthOpen + (num10 - _mouthOpen) * num12; if (MouthMaxDeltaPerSec > 0f) { num13 = Mathf.MoveTowards(_mouthOpen, num13, MouthMaxDeltaPerSec * deltaTime); } _mouthOpen = num13; float num14 = Mathf.Clamp01(Mathf.Pow(_mouthOpen, 0.7f) * 2.2f); TryBindAnimator(); if ((Object)(object)_anim != (Object)null && _animMouthHash != -1) { _anim.SetFloat(_animMouthHash, num14); } } else { float deltaTime2 = Time.deltaTime; float num15 = 1f - Mathf.Exp((0f - MouthReleaseHz) * deltaTime2); _mouthOpen = Mathf.Lerp(_mouthOpen, 0f, num15); float num16 = Mathf.Clamp01(Mathf.Pow(_mouthOpen, 0.7f) * 2.2f); TryBindAnimator(); if ((Object)(object)_anim != (Object)null && _animMouthHash != -1) { _anim.SetFloat(_animMouthHash, num16); } } } private void OnDestroy() { ((MonoBehaviour)this).StopAllCoroutines(); if (Object.op_Implicit((Object)(object)_src)) { _src.Stop(); } try { if (Object.op_Implicit((Object)(object)_pv)) { PhotonNetwork.RemoveRPCs(_pv); } } catch { } if (Object.op_Implicit((Object)(object)_pv)) { _pv.ViewID = 0; } _pendingGo.Clear(); _clips.Clear(); } } [BepInPlugin("com.TeddyBRB.twitchbingbongtts", "Twitch→BingBong TTS", "1.1.7")] public class YourPlugin : BaseUnityPlugin { private ConfigEntry<string> _channelsCsv; private ConfigEntry<int> _minBits; private ConfigEntry<bool> _readEveryChat; private ConfigEntry<float> _volume; private ConfigEntry<bool> _sayBingBongPrefix; private string[] _channels = Array.Empty<string>(); private TwitchConnector _chat; private BingBongTTS _tts; private void Awake() { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Expected O, but got Unknown Harmony val = new Harmony("com.TeddyBRB.twitchbingbongtts"); val.PatchAll(); List<MethodBase> list = val.GetPatchedMethods().ToList(); ((BaseUnityPlugin)this).Logger.LogInfo((object)$"[Harmony] Patched {list.Count} method(s) for {val.Id}:"); foreach (MethodBase item in list) { ((BaseUnityPlugin)this).Logger.LogInfo((object)("[Harmony] • " + item.DeclaringType?.FullName + "." + item.Name)); Patches patchInfo = Harmony.GetPatchInfo(item); if (patchInfo != null) { ((BaseUnityPlugin)this).Logger.LogInfo((object)$"[Harmony] prefixes={patchInfo.Prefixes.Count}, postfixes={patchInfo.Postfixes.Count}, transpilers={patchInfo.Transpilers.Count}"); } } _channelsCsv = ((BaseUnityPlugin)this).Config.Bind<string>("Twitch", "Channels", "", "Comma/space-separated list of channels (Example:TeddyBRB, Greppian)."); _minBits = ((BaseUnityPlugin)this).Config.Bind<int>("Twitch", "MinimumBits", 100, "Cheer threshold (ignored when ReadEveryChat is true)."); _readEveryChat = ((BaseUnityPlugin)this).Config.Bind<bool>("Twitch", "ReadEveryChat", false, "If true, speak every chat message; otherwise only cheers with ≥ MinimumBits."); _volume = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "Volume", 0.7f, "TTS playback volume (0.0 min – 1.0 max)."); _sayBingBongPrefix = ((BaseUnityPlugin)this).Config.Bind<bool>("Twitch", "SayBingBongPrefix", true, "If true, prepend \"Bing bong\" before spoken Twitch messages."); _channels = (from s in _channelsCsv.Value.Split(new char[3] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries) select s.Trim().TrimStart(new char[1] { '#' }).ToLowerInvariant()).Distinct<string>(StringComparer.OrdinalIgnoreCase).ToArray(); _tts = ((Component)this).gameObject.AddComponent<BingBongTTS>(); _tts.BaseVolume = Mathf.Clamp01(_volume.Value); _volume.SettingChanged += delegate { if (Object.op_Implicit((Object)(object)_tts)) { _tts.BaseVolume = Mathf.Clamp01(_volume.Value); } }; EvaluateTwitchListener(); ((BaseUnityPlugin)this).Logger.LogInfo((object)("[TTS] Temp path: " + Application.temporaryCachePath)); } private void OnDisable() { StopTwitchListener(); } private void EvaluateTwitchListener() { if (_channels.Length == 0) { StopTwitchListener(); ((BaseUnityPlugin)this).Logger.LogInfo((object)"[Twitch] No channels configured; not listening."); } else { MaybeStartTwitchListener(); } } private void MaybeStartTwitchListener() { if (_chat == null) { _chat = new TwitchConnector(_channels, _minBits.Value, _readEveryChat.Value, _sayBingBongPrefix.Value); _chat.OnBitsCheerReceived += delegate(string msg) { _tts.Enqueue(msg); }; _chat.Connect(); ((BaseUnityPlugin)this).Logger.LogInfo((object)("[Twitch] Listening (" + (_readEveryChat.Value ? "reading all chat" : $"cheers ≥ {_minBits.Value}") + ").")); } } private void StopTwitchListener() { if (_chat != null) { try { _chat.Dispose(); } catch { } _chat = null; ((BaseUnityPlugin)this).Logger.LogInfo((object)"[Twitch] Stopped listening."); } } } [HarmonyPatch(typeof(PlayerConnectionLog), "OnPlayerLeftRoom")] internal static class TTS_OnPlayerLeftRoomPatch { [HarmonyPostfix] private static void Postfix() { BingBongTTS.NotifyRoomRosterPossiblyChanged(); } } [HarmonyPatch(typeof(PlayerConnectionLog), "OnPlayerEnteredRoom")] internal static class TTS_OnPlayerEnteredRoomPatch { [HarmonyPostfix] private static void Postfix() { BingBongTTS.NotifyRoomRosterPossiblyChanged(); } } public class TwitchConnector : IDisposable { private readonly TwitchClient _client = new TwitchClient((IClient)null, (ClientProtocol)1, (ILogger<TwitchClient>)null); private readonly string[] _channels; private readonly int _minBits; private readonly bool _readEveryChat; private static readonly ManualLogSource Log = Logger.CreateLogSource("TwitchConnector"); private readonly bool _addPrefix; public event Action<string> OnBitsCheerReceived; public TwitchConnector(string[] channels, int minBits, bool readEveryChat, bool addPrefix) { //IL_0004: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Expected O, but got Unknown //IL_00db: Unknown result type (might be due to invalid IL or missing references) //IL_00e1: Expected O, but got Unknown _readEveryChat = readEveryChat; _addPrefix = addPrefix; if (channels == null || channels.Length == 0) { throw new ArgumentException("At least one channel is required.", "channels"); } _channels = (from c in channels where !string.IsNullOrWhiteSpace(c) select c.Trim().TrimStart(new char[1] { '#' }).ToLowerInvariant()).Distinct<string>(StringComparer.OrdinalIgnoreCase).ToArray(); _minBits = Math.Max(1, minBits); string text = $"justinfan{Random.Range(10000, 99999)}"; ConnectionCredentials val = new ConnectionCredentials(text, "SCHMUCK", "wss://irc-ws.chat.twitch.tv:443", false, (Capabilities)null); _client.Initialize(val, _channels[0], '!', '!', true); _client.OnConnected += delegate { for (int i = 1; i < _channels.Length; i++) { _client.JoinChannel(_channels[i], false); } }; _client.OnLog += delegate(object _, OnLogArgs e) { Log.LogDebug((object)("[TwitchLib] " + e.Data)); }; _client.OnConnectionError += delegate(object _, OnConnectionErrorArgs e) { Log.LogError((object)$"[Twitch] Conn error: {e.Error}"); }; _client.OnJoinedChannel += delegate(object _, OnJoinedChannelArgs e) { Log.LogInfo((object)("[Twitch] Joined #" + e.Channel)); }; _client.OnMessageReceived += HandleMessage; } public void Connect() { _client.Connect(); } public void Dispose() { try { _client.OnMessageReceived -= HandleMessage; if (_client.IsConnected) { _client.Disconnect(); } } catch { } } private void Emit(string text) { string obj = (_addPrefix ? ("Bing bong, " + text) : text); this.OnBitsCheerReceived?.Invoke(obj); } private void HandleMessage(object _, OnMessageReceivedArgs e) { string text = e.ChatMessage.Message ?? string.Empty; if (_readEveryChat) { string text2 = text.Trim(); if (!string.IsNullOrWhiteSpace(text2)) { Emit(text2); } } else if (e.ChatMessage.Bits >= _minBits && !Regex.IsMatch(text, "^(?:/cheer\\s*)?\\d+\\s*(?:bits?)?$", RegexOptions.IgnoreCase)) { string text3 = Regex.Replace(text, "(?:(?:/cheer|cheer)\\d+\\b|\\b\\d+\\s*bits?\\b)", "", RegexOptions.IgnoreCase).Trim(); if (!string.IsNullOrWhiteSpace(text3)) { Emit(text3); } } } } namespace TwitchBingBong { public static class MyPluginInfo { public const string PLUGIN_GUID = "TwitchBingBong"; public const string PLUGIN_NAME = "TwitchBingBong"; public const string PLUGIN_VERSION = "1.1.3"; } }
TwitchLib.Client.dll
Decompiled a month ago
The result has been truncated due to the large size, download it to view full contents!
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Text; using System.Text.RegularExpressions; using System.Timers; using Microsoft.Extensions.Logging; using TwitchLib.Client.Enums; using TwitchLib.Client.Enums.Internal; using TwitchLib.Client.Events; using TwitchLib.Client.Exceptions; using TwitchLib.Client.Interfaces; using TwitchLib.Client.Internal; using TwitchLib.Client.Internal.Parsing; using TwitchLib.Client.Manager; using TwitchLib.Client.Models; using TwitchLib.Client.Models.Internal; using TwitchLib.Communication.Clients; using TwitchLib.Communication.Events; using TwitchLib.Communication.Interfaces; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")] [assembly: AssemblyCompany("swiftyspiffy, Prom3theu5, Syzuna, LuckyNoS7evin")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright 2022")] [assembly: AssemblyDescription("Client component of TwitchLib. This component allows you to access Twitch chat and whispers, as well as the events that are sent over this connection.")] [assembly: AssemblyFileVersion("3.3.1")] [assembly: AssemblyInformationalVersion("3.3.1")] [assembly: AssemblyProduct("TwitchLib.Client")] [assembly: AssemblyTitle("TwitchLib.Client")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/TwitchLib/TwitchLib.Client")] [assembly: NeutralResourcesLanguage("en-US")] [assembly: AssemblyVersion("3.3.1.0")] namespace TwitchLib.Client { public class TwitchClient : ITwitchClient { private IClient _client; private MessageEmoteCollection _channelEmotes = new MessageEmoteCollection(); private readonly ICollection<char> _chatCommandIdentifiers = new HashSet<char>(); private readonly ICollection<char> _whisperCommandIdentifiers = new HashSet<char>(); private readonly Queue<JoinedChannel> _joinChannelQueue = new Queue<JoinedChannel>(); private readonly ILogger<TwitchClient> _logger; private readonly ClientProtocol _protocol; private bool _currentlyJoiningChannels; private Timer _joinTimer; private List<KeyValuePair<string, DateTime>> _awaitingJoins; private readonly IrcParser _ircParser; private readonly JoinedChannelManager _joinedChannelManager; private readonly List<string> _hasSeenJoinedChannels = new List<string>(); private string _lastMessageSent; public Version Version => Assembly.GetEntryAssembly().GetName().Version; public bool IsInitialized => _client != null; public IReadOnlyList<JoinedChannel> JoinedChannels => _joinedChannelManager.GetJoinedChannels(); public string TwitchUsername { get; private set; } public WhisperMessage PreviousWhisper { get; private set; } public bool IsConnected { get { if (!IsInitialized || _client == null) { return false; } return _client.IsConnected; } } public MessageEmoteCollection ChannelEmotes => _channelEmotes; public bool DisableAutoPong { get; set; } public bool WillReplaceEmotes { get; set; } public ConnectionCredentials ConnectionCredentials { get; private set; } public bool AutoReListenOnException { get; set; } public event EventHandler<OnAnnouncementArgs> OnAnnouncement; public event EventHandler<OnVIPsReceivedArgs> OnVIPsReceived; public event EventHandler<OnLogArgs> OnLog; public event EventHandler<OnConnectedArgs> OnConnected; public event EventHandler<OnJoinedChannelArgs> OnJoinedChannel; public event EventHandler<OnIncorrectLoginArgs> OnIncorrectLogin; public event EventHandler<OnChannelStateChangedArgs> OnChannelStateChanged; public event EventHandler<OnUserStateChangedArgs> OnUserStateChanged; public event EventHandler<OnMessageReceivedArgs> OnMessageReceived; public event EventHandler<OnWhisperReceivedArgs> OnWhisperReceived; public event EventHandler<OnMessageSentArgs> OnMessageSent; public event EventHandler<OnWhisperSentArgs> OnWhisperSent; public event EventHandler<OnChatCommandReceivedArgs> OnChatCommandReceived; public event EventHandler<OnWhisperCommandReceivedArgs> OnWhisperCommandReceived; public event EventHandler<OnUserJoinedArgs> OnUserJoined; public event EventHandler<OnModeratorJoinedArgs> OnModeratorJoined; public event EventHandler<OnModeratorLeftArgs> OnModeratorLeft; public event EventHandler<OnMessageClearedArgs> OnMessageCleared; public event EventHandler<OnNewSubscriberArgs> OnNewSubscriber; public event EventHandler<OnReSubscriberArgs> OnReSubscriber; public event EventHandler<OnPrimePaidSubscriberArgs> OnPrimePaidSubscriber; public event EventHandler<OnExistingUsersDetectedArgs> OnExistingUsersDetected; public event EventHandler<OnUserLeftArgs> OnUserLeft; public event EventHandler<OnDisconnectedEventArgs> OnDisconnected; public event EventHandler<OnConnectionErrorArgs> OnConnectionError; public event EventHandler<OnChatClearedArgs> OnChatCleared; public event EventHandler<OnUserTimedoutArgs> OnUserTimedout; public event EventHandler<OnLeftChannelArgs> OnLeftChannel; public event EventHandler<OnUserBannedArgs> OnUserBanned; public event EventHandler<OnModeratorsReceivedArgs> OnModeratorsReceived; public event EventHandler<OnChatColorChangedArgs> OnChatColorChanged; public event EventHandler<OnSendReceiveDataArgs> OnSendReceiveData; public event EventHandler<OnRaidNotificationArgs> OnRaidNotification; public event EventHandler<OnGiftedSubscriptionArgs> OnGiftedSubscription; public event EventHandler<OnCommunitySubscriptionArgs> OnCommunitySubscription; public event EventHandler<OnContinuedGiftedSubscriptionArgs> OnContinuedGiftedSubscription; public event EventHandler<OnMessageThrottledEventArgs> OnMessageThrottled; public event EventHandler<OnWhisperThrottledEventArgs> OnWhisperThrottled; public event EventHandler<OnErrorEventArgs> OnError; public event EventHandler<OnReconnectedEventArgs> OnReconnected; public event EventHandler<OnRequiresVerifiedEmailArgs> OnRequiresVerifiedEmail; public event EventHandler<OnRequiresVerifiedPhoneNumberArgs> OnRequiresVerifiedPhoneNumber; public event EventHandler<OnRateLimitArgs> OnRateLimit; public event EventHandler<OnDuplicateArgs> OnDuplicate; public event EventHandler<OnBannedEmailAliasArgs> OnBannedEmailAlias; public event EventHandler OnSelfRaidError; public event EventHandler OnNoPermissionError; public event EventHandler OnRaidedChannelIsMatureAudience; public event EventHandler<OnFailureToReceiveJoinConfirmationArgs> OnFailureToReceiveJoinConfirmation; public event EventHandler<OnFollowersOnlyArgs> OnFollowersOnly; public event EventHandler<OnSubsOnlyArgs> OnSubsOnly; public event EventHandler<OnEmoteOnlyArgs> OnEmoteOnly; public event EventHandler<OnSuspendedArgs> OnSuspended; public event EventHandler<OnBannedArgs> OnBanned; public event EventHandler<OnSlowModeArgs> OnSlowMode; public event EventHandler<OnR9kModeArgs> OnR9kMode; public event EventHandler<OnUserIntroArgs> OnUserIntro; public event EventHandler<OnUnaccountedForArgs> OnUnaccountedFor; public TwitchClient(IClient client = null, ClientProtocol protocol = 1, ILogger<TwitchClient> logger = null) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Expected O, but got Unknown //IL_004c: Unknown result type (might be due to invalid IL or missing references) //IL_004d: Unknown result type (might be due to invalid IL or missing references) _logger = logger; _client = client; _protocol = protocol; _joinedChannelManager = new JoinedChannelManager(); _ircParser = new IrcParser(); } public void Initialize(ConnectionCredentials credentials, string channel = null, char chatCommandIdentifier = '!', char whisperCommandIdentifier = '!', bool autoReListenOnExceptions = true) { if (channel != null && channel[0] == '#') { channel = channel.Substring(1); } initializeHelper(credentials, new List<string> { channel }, chatCommandIdentifier, whisperCommandIdentifier, autoReListenOnExceptions); } public void Initialize(ConnectionCredentials credentials, List<string> channels, char chatCommandIdentifier = '!', char whisperCommandIdentifier = '!', bool autoReListenOnExceptions = true) { channels = channels.Select((string x) => (x[0] != '#') ? x : x.Substring(1)).ToList(); initializeHelper(credentials, channels, chatCommandIdentifier, whisperCommandIdentifier, autoReListenOnExceptions); } private void initializeHelper(ConnectionCredentials credentials, List<string> channels, char chatCommandIdentifier = '!', char whisperCommandIdentifier = '!', bool autoReListenOnExceptions = true) { //IL_00ef: Unknown result type (might be due to invalid IL or missing references) //IL_00f9: Expected O, but got Unknown Log($"TwitchLib-TwitchClient initialized, assembly version: {Assembly.GetExecutingAssembly().GetName().Version}"); ConnectionCredentials = credentials; TwitchUsername = ConnectionCredentials.TwitchUsername; if (chatCommandIdentifier != 0) { _chatCommandIdentifiers.Add(chatCommandIdentifier); } if (whisperCommandIdentifier != 0) { _whisperCommandIdentifiers.Add(whisperCommandIdentifier); } AutoReListenOnException = autoReListenOnExceptions; if (channels != null && channels.Count > 0) { int i; for (i = 0; i < channels.Count; i++) { if (!string.IsNullOrEmpty(channels[i])) { if (((IEnumerable<JoinedChannel>)JoinedChannels).FirstOrDefault((Func<JoinedChannel, bool>)((JoinedChannel x) => x.Channel.ToLower() == channels[i])) != null) { return; } _joinChannelQueue.Enqueue(new JoinedChannel(channels[i])); } } } InitializeClient(); } private void InitializeClient() { //IL_0009: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_001a: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Expected O, but got Unknown //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0014: Invalid comparison between Unknown and I4 //IL_0028: Unknown result type (might be due to invalid IL or missing references) //IL_0032: Expected O, but got Unknown if (_client == null) { ClientProtocol protocol = _protocol; if ((int)protocol != 0) { if ((int)protocol == 1) { _client = (IClient)new WebSocketClient((IClientOptions)null); } } else { _client = (IClient)new TcpClient((IClientOptions)null); } } _client.OnConnected += _client_OnConnected; _client.OnMessage += _client_OnMessage; _client.OnDisconnected += _client_OnDisconnected; _client.OnFatality += _client_OnFatality; _client.OnMessageThrottled += _client_OnMessageThrottled; _client.OnWhisperThrottled += _client_OnWhisperThrottled; _client.OnReconnected += _client_OnReconnected; } internal void RaiseEvent(string eventName, object args = null) { Delegate[] invocationList = (GetType().GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this) as MulticastDelegate).GetInvocationList(); foreach (Delegate @delegate in invocationList) { @delegate.Method.Invoke(@delegate.Target, (args == null) ? new object[2] { this, new EventArgs() } : new object[2] { this, args }); } } public void SendRaw(string message) { //IL_0040: Unknown result type (might be due to invalid IL or missing references) if (!IsInitialized) { HandleNotInitialized(); } Log("Writing: " + message); _client.Send(message); this.OnSendReceiveData?.Invoke(this, new OnSendReceiveDataArgs { Direction = (SendReceiveDirection)0, Data = message }); } private void SendTwitchMessage(JoinedChannel channel, string message, string replyToId = null, bool dryRun = false) { //IL_0038: Unknown result type (might be due to invalid IL or missing references) //IL_003d: Unknown result type (might be due to invalid IL or missing references) //IL_0049: Unknown result type (might be due to invalid IL or missing references) //IL_005a: Unknown result type (might be due to invalid IL or missing references) //IL_0062: Expected O, but got Unknown if (!IsInitialized) { HandleNotInitialized(); } if (channel == null || message == null || dryRun) { return; } if (message.Length > 500) { LogError("Message length has exceeded the maximum character count. (500)"); return; } OutboundChatMessage val = new OutboundChatMessage { Channel = channel.Channel, Username = ConnectionCredentials.TwitchUsername, Message = message }; if (replyToId != null) { val.ReplyToId = replyToId; } _lastMessageSent = message; _client.Send(((object)val).ToString()); } public void SendMessage(JoinedChannel channel, string message, bool dryRun = false) { SendTwitchMessage(channel, message, null, dryRun); } public void SendMessage(string channel, string message, bool dryRun = false) { SendMessage(GetJoinedChannel(channel), message, dryRun); } public void SendReply(JoinedChannel channel, string replyToId, string message, bool dryRun = false) { SendTwitchMessage(channel, message, replyToId, dryRun); } public void SendReply(string channel, string replyToId, string message, bool dryRun = false) { SendReply(GetJoinedChannel(channel), replyToId, message, dryRun); } public void SendWhisper(string receiver, string message, bool dryRun = false) { //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_0036: Expected O, but got Unknown if (!IsInitialized) { HandleNotInitialized(); } if (!dryRun) { OutboundWhisperMessage val = new OutboundWhisperMessage { Receiver = receiver, Username = ConnectionCredentials.TwitchUsername, Message = message }; _client.SendWhisper(((object)val).ToString()); this.OnWhisperSent?.Invoke(this, new OnWhisperSentArgs { Receiver = receiver, Message = message }); } } public bool Connect() { if (!IsInitialized) { HandleNotInitialized(); } Log("Connecting to: " + ConnectionCredentials.TwitchWebsocketURI); _joinedChannelManager.Clear(); if (_client.Open()) { Log("Should be connected!"); return true; } return false; } public void Disconnect() { Log("Disconnect Twitch Chat Client..."); if (!IsInitialized) { HandleNotInitialized(); } _client.Close(true); _joinedChannelManager.Clear(); PreviousWhisper = null; } public void Reconnect() { if (!IsInitialized) { HandleNotInitialized(); } Log("Reconnecting to Twitch"); _client.Reconnect(); } public void AddChatCommandIdentifier(char identifier) { if (!IsInitialized) { HandleNotInitialized(); } _chatCommandIdentifiers.Add(identifier); } public void RemoveChatCommandIdentifier(char identifier) { if (!IsInitialized) { HandleNotInitialized(); } _chatCommandIdentifiers.Remove(identifier); } public void AddWhisperCommandIdentifier(char identifier) { if (!IsInitialized) { HandleNotInitialized(); } _whisperCommandIdentifiers.Add(identifier); } public void RemoveWhisperCommandIdentifier(char identifier) { if (!IsInitialized) { HandleNotInitialized(); } _whisperCommandIdentifiers.Remove(identifier); } public void SetConnectionCredentials(ConnectionCredentials credentials) { if (!IsInitialized) { HandleNotInitialized(); } if (IsConnected) { throw new IllegalAssignmentException("While the client is connected, you are unable to change the connection credentials. Please disconnect first and then change them."); } ConnectionCredentials = credentials; } public void JoinChannel(string channel, bool overrideCheck = false) { //IL_0076: Unknown result type (might be due to invalid IL or missing references) //IL_0080: Expected O, but got Unknown if (!IsInitialized) { HandleNotInitialized(); } if (!IsConnected) { HandleNotConnected(); } if (((IEnumerable<JoinedChannel>)JoinedChannels).FirstOrDefault((Func<JoinedChannel, bool>)((JoinedChannel x) => x.Channel.ToLower() == channel && !overrideCheck)) == null) { if (channel[0] == '#') { channel = channel.Substring(1); } _joinChannelQueue.Enqueue(new JoinedChannel(channel)); if (!_currentlyJoiningChannels) { QueueingJoinCheck(); } } } public JoinedChannel GetJoinedChannel(string channel) { if (!IsInitialized) { HandleNotInitialized(); } if (JoinedChannels.Count == 0) { throw new BadStateException("Must be connected to at least one channel."); } if (channel[0] == '#') { channel = channel.Substring(1); } return _joinedChannelManager.GetJoinedChannel(channel); } public void LeaveChannel(string channel) { if (!IsInitialized) { HandleNotInitialized(); } channel = channel.ToLower(); if (channel[0] == '#') { channel = channel.Substring(1); } Log("Leaving channel: " + channel); if (_joinedChannelManager.GetJoinedChannel(channel) != null) { _client.Send(Rfc2812.Part("#" + channel)); } } public void LeaveChannel(JoinedChannel channel) { if (!IsInitialized) { HandleNotInitialized(); } LeaveChannel(channel.Channel); } public void OnReadLineTest(string rawIrc) { if (!IsInitialized) { HandleNotInitialized(); } HandleIrcMessage(_ircParser.ParseIrcMessage(rawIrc)); } private void _client_OnWhisperThrottled(object sender, OnWhisperThrottledEventArgs e) { this.OnWhisperThrottled?.Invoke(sender, e); } private void _client_OnMessageThrottled(object sender, OnMessageThrottledEventArgs e) { this.OnMessageThrottled?.Invoke(sender, e); } private void _client_OnFatality(object sender, OnFatalErrorEventArgs e) { //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Expected O, but got Unknown this.OnConnectionError?.Invoke(this, new OnConnectionErrorArgs { BotUsername = TwitchUsername, Error = new ErrorEvent { Message = e.Reason } }); } private void _client_OnDisconnected(object sender, OnDisconnectedEventArgs e) { this.OnDisconnected?.Invoke(sender, e); } private void _client_OnReconnected(object sender, OnReconnectedEventArgs e) { foreach (JoinedChannel joinedChannel in _joinedChannelManager.GetJoinedChannels()) { if (!string.Equals(joinedChannel.Channel, TwitchUsername, StringComparison.CurrentCultureIgnoreCase)) { _joinChannelQueue.Enqueue(joinedChannel); } } _joinedChannelManager.Clear(); this.OnReconnected?.Invoke(sender, e); } private void _client_OnMessage(object sender, OnMessageEventArgs e) { //IL_0055: Unknown result type (might be due to invalid IL or missing references) string[] separator = new string[1] { "\r\n" }; string[] array = e.Message.Split(separator, StringSplitOptions.None); foreach (string text in array) { if (text.Length > 1) { Log("Received: " + text); this.OnSendReceiveData?.Invoke(this, new OnSendReceiveDataArgs { Direction = (SendReceiveDirection)1, Data = text }); HandleIrcMessage(_ircParser.ParseIrcMessage(text)); } } } private void _client_OnConnected(object sender, object e) { _client.Send(Rfc2812.Pass(ConnectionCredentials.TwitchOAuth)); _client.Send(Rfc2812.Nick(ConnectionCredentials.TwitchUsername)); _client.Send(Rfc2812.User(ConnectionCredentials.TwitchUsername, 0, ConnectionCredentials.TwitchUsername)); if (ConnectionCredentials.Capabilities.Membership) { _client.Send("CAP REQ twitch.tv/membership"); } if (ConnectionCredentials.Capabilities.Commands) { _client.Send("CAP REQ twitch.tv/commands"); } if (ConnectionCredentials.Capabilities.Tags) { _client.Send("CAP REQ twitch.tv/tags"); } if (_joinChannelQueue != null && _joinChannelQueue.Count > 0) { QueueingJoinCheck(); } } private void QueueingJoinCheck() { //IL_006b: Unknown result type (might be due to invalid IL or missing references) //IL_0075: Expected O, but got Unknown if (_joinChannelQueue.Count > 0) { _currentlyJoiningChannels = true; JoinedChannel val = _joinChannelQueue.Dequeue(); Log("Joining channel: " + val.Channel); _client.Send(Rfc2812.Join("#" + val.Channel.ToLower())); _joinedChannelManager.AddJoinedChannel(new JoinedChannel(val.Channel)); StartJoinedChannelTimer(val.Channel); } else { Log("Finished channel joining queue."); } } private void StartJoinedChannelTimer(string channel) { if (_joinTimer == null) { _joinTimer = new Timer(1000.0); _joinTimer.Elapsed += JoinChannelTimeout; _awaitingJoins = new List<KeyValuePair<string, DateTime>>(); } _awaitingJoins.Add(new KeyValuePair<string, DateTime>(channel.ToLower(), DateTime.Now)); if (!_joinTimer.Enabled) { _joinTimer.Start(); } } private void JoinChannelTimeout(object sender, ElapsedEventArgs e) { if (_awaitingJoins.Any()) { List<KeyValuePair<string, DateTime>> list = _awaitingJoins.Where((KeyValuePair<string, DateTime> x) => (DateTime.Now - x.Value).TotalSeconds > 5.0).ToList(); if (!list.Any()) { return; } _awaitingJoins.RemoveAll((KeyValuePair<string, DateTime> x) => (DateTime.Now - x.Value).TotalSeconds > 5.0); { foreach (KeyValuePair<string, DateTime> item in list) { _joinedChannelManager.RemoveJoinedChannel(item.Key.ToLowerInvariant()); this.OnFailureToReceiveJoinConfirmation?.Invoke(this, new OnFailureToReceiveJoinConfirmationArgs { Exception = new FailureToReceiveJoinConfirmationException(item.Key) }); } return; } } _joinTimer.Stop(); _currentlyJoiningChannels = false; QueueingJoinCheck(); } private void HandleIrcMessage(IrcMessage ircMessage) { //IL_0041: Unknown result type (might be due to invalid IL or missing references) //IL_0046: Unknown result type (might be due to invalid IL or missing references) //IL_0047: Unknown result type (might be due to invalid IL or missing references) //IL_00c1: Expected I4, but got Unknown if (ircMessage.Message.Contains("Login authentication failed")) { this.OnIncorrectLogin?.Invoke(this, new OnIncorrectLoginArgs { Exception = new ErrorLoggingInException(ircMessage.ToString(), TwitchUsername) }); return; } IrcCommand command = ircMessage.Command; switch ((int)command) { case 1: HandlePrivMsg(ircMessage); break; case 2: HandleNotice(ircMessage); break; case 3: if (!DisableAutoPong) { SendRaw("PONG"); } break; case 4: break; case 5: HandleJoin(ircMessage); break; case 6: HandlePart(ircMessage); break; case 7: HandleClearChat(ircMessage); break; case 8: HandleClearMsg(ircMessage); break; case 9: HandleUserState(ircMessage); break; case 17: Handle004(); break; case 18: Handle353(ircMessage); break; case 19: Handle366(); break; case 23: HandleWhisper(ircMessage); break; case 24: HandleRoomState(ircMessage); break; case 25: Reconnect(); break; case 27: HandleUserNotice(ircMessage); break; case 28: HandleMode(ircMessage); break; case 13: HandleCap(ircMessage); break; default: this.OnUnaccountedFor?.Invoke(this, new OnUnaccountedForArgs { BotUsername = TwitchUsername, Channel = null, Location = "HandleIrcMessage", RawIRC = ircMessage.ToString() }); UnaccountedFor(ircMessage.ToString()); break; case 10: case 14: case 15: case 16: case 20: case 21: case 22: break; } } private void HandlePrivMsg(IrcMessage ircMessage) { //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Expected O, but got Unknown //IL_011d: Unknown result type (might be due to invalid IL or missing references) //IL_0124: Expected O, but got Unknown ChatMessage val = new ChatMessage(TwitchUsername, ircMessage, ref _channelEmotes, WillReplaceEmotes); foreach (JoinedChannel item in JoinedChannels.Where((JoinedChannel x) => string.Equals(x.Channel, ircMessage.Channel, StringComparison.InvariantCultureIgnoreCase))) { item.HandleMessage(val); } this.OnMessageReceived?.Invoke(this, new OnMessageReceivedArgs { ChatMessage = val }); if (ircMessage.Tags.TryGetValue("msg-id", out var value) && value == "user-intro") { this.OnUserIntro?.Invoke(this, new OnUserIntroArgs { ChatMessage = val }); } if (_chatCommandIdentifiers != null && _chatCommandIdentifiers.Count != 0 && !string.IsNullOrEmpty(val.Message) && _chatCommandIdentifiers.Contains(val.Message[0])) { ChatCommand command = new ChatCommand(val); this.OnChatCommandReceived?.Invoke(this, new OnChatCommandReceivedArgs { Command = command }); } } private void HandleNotice(IrcMessage ircMessage) { if (ircMessage.Message.Contains("Improperly formatted auth")) { this.OnIncorrectLogin?.Invoke(this, new OnIncorrectLoginArgs { Exception = new ErrorLoggingInException(ircMessage.ToString(), TwitchUsername) }); return; } if (!ircMessage.Tags.TryGetValue("msg-id", out var value)) { this.OnUnaccountedFor?.Invoke(this, new OnUnaccountedForArgs { BotUsername = TwitchUsername, Channel = ircMessage.Channel, Location = "NoticeHandling", RawIRC = ircMessage.ToString() }); UnaccountedFor(ircMessage.ToString()); } switch (value) { case "color_changed": this.OnChatColorChanged?.Invoke(this, new OnChatColorChangedArgs { Channel = ircMessage.Channel }); break; case "room_mods": this.OnModeratorsReceived?.Invoke(this, new OnModeratorsReceivedArgs { Channel = ircMessage.Channel, Moderators = ircMessage.Message.Replace(" ", "").Split(new char[1] { ':' })[1].Split(new char[1] { ',' }).ToList() }); break; case "no_mods": this.OnModeratorsReceived?.Invoke(this, new OnModeratorsReceivedArgs { Channel = ircMessage.Channel, Moderators = new List<string>() }); break; case "no_permission": this.OnNoPermissionError?.Invoke(this, null); break; case "raid_error_self": this.OnSelfRaidError?.Invoke(this, null); break; case "raid_notice_mature": this.OnRaidedChannelIsMatureAudience?.Invoke(this, null); break; case "msg_banned_email_alias": this.OnBannedEmailAlias?.Invoke(this, new OnBannedEmailAliasArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_channel_suspended": _awaitingJoins.RemoveAll((KeyValuePair<string, DateTime> x) => x.Key.ToLower() == ircMessage.Channel); _joinedChannelManager.RemoveJoinedChannel(ircMessage.Channel); QueueingJoinCheck(); this.OnFailureToReceiveJoinConfirmation?.Invoke(this, new OnFailureToReceiveJoinConfirmationArgs { Exception = new FailureToReceiveJoinConfirmationException(ircMessage.Channel, ircMessage.Message) }); break; case "msg_requires_verified_phone_number": this.OnRequiresVerifiedPhoneNumber?.Invoke(this, new OnRequiresVerifiedPhoneNumberArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_verified_email": this.OnRequiresVerifiedEmail?.Invoke(this, new OnRequiresVerifiedEmailArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "no_vips": this.OnVIPsReceived?.Invoke(this, new OnVIPsReceivedArgs { Channel = ircMessage.Channel, VIPs = new List<string>() }); break; case "vips_success": this.OnVIPsReceived?.Invoke(this, new OnVIPsReceivedArgs { Channel = ircMessage.Channel, VIPs = ircMessage.Message.Replace(" ", "").Replace(".", "").Split(new char[1] { ':' })[1].Split(new char[1] { ',' }).ToList() }); break; case "msg_ratelimit": this.OnRateLimit?.Invoke(this, new OnRateLimitArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_duplicate": this.OnDuplicate?.Invoke(this, new OnDuplicateArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_followersonly": this.OnFollowersOnly?.Invoke(this, new OnFollowersOnlyArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_subsonly": this.OnSubsOnly?.Invoke(this, new OnSubsOnlyArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_emoteonly": this.OnEmoteOnly?.Invoke(this, new OnEmoteOnlyArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_suspended": this.OnSuspended?.Invoke(this, new OnSuspendedArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_banned": this.OnBanned?.Invoke(this, new OnBannedArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_slowmode": this.OnSlowMode?.Invoke(this, new OnSlowModeArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; case "msg_r9k": this.OnR9kMode?.Invoke(this, new OnR9kModeArgs { Channel = ircMessage.Channel, Message = ircMessage.Message }); break; default: this.OnUnaccountedFor?.Invoke(this, new OnUnaccountedForArgs { BotUsername = TwitchUsername, Channel = ircMessage.Channel, Location = "NoticeHandling", RawIRC = ircMessage.ToString() }); UnaccountedFor(ircMessage.ToString()); break; } } private void HandleJoin(IrcMessage ircMessage) { this.OnUserJoined?.Invoke(this, new OnUserJoinedArgs { Channel = ircMessage.Channel, Username = ircMessage.User }); } private void HandlePart(IrcMessage ircMessage) { if (string.Equals(TwitchUsername, ircMessage.User, StringComparison.InvariantCultureIgnoreCase)) { _joinedChannelManager.RemoveJoinedChannel(ircMessage.Channel); _hasSeenJoinedChannels.Remove(ircMessage.Channel); this.OnLeftChannel?.Invoke(this, new OnLeftChannelArgs { BotUsername = TwitchUsername, Channel = ircMessage.Channel }); } else { this.OnUserLeft?.Invoke(this, new OnUserLeftArgs { Channel = ircMessage.Channel, Username = ircMessage.User }); } } private void HandleClearChat(IrcMessage ircMessage) { //IL_006a: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Expected O, but got Unknown //IL_0045: Unknown result type (might be due to invalid IL or missing references) //IL_004b: Expected O, but got Unknown string value; if (string.IsNullOrWhiteSpace(ircMessage.Message)) { this.OnChatCleared?.Invoke(this, new OnChatClearedArgs { Channel = ircMessage.Channel }); } else if (ircMessage.Tags.TryGetValue("ban-duration", out value)) { UserTimeout userTimeout = new UserTimeout(ircMessage); this.OnUserTimedout?.Invoke(this, new OnUserTimedoutArgs { UserTimeout = userTimeout }); } else { UserBan userBan = new UserBan(ircMessage); this.OnUserBanned?.Invoke(this, new OnUserBannedArgs { UserBan = userBan }); } } private void HandleClearMsg(IrcMessage ircMessage) { this.OnMessageCleared?.Invoke(this, new OnMessageClearedArgs { Channel = ircMessage.Channel, Message = ircMessage.Message, TargetMessageId = ircMessage.ToString().Split(new char[1] { '=' })[3].Split(new char[1] { ';' })[0], TmiSentTs = ircMessage.ToString().Split(new char[1] { '=' })[4].Split(new char[1] { ' ' })[0] }); } private void HandleUserState(IrcMessage ircMessage) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Expected O, but got Unknown //IL_006c: Unknown result type (might be due to invalid IL or missing references) //IL_0076: Expected O, but got Unknown UserState val = new UserState(ircMessage); if (!_hasSeenJoinedChannels.Contains(val.Channel.ToLowerInvariant())) { _hasSeenJoinedChannels.Add(val.Channel.ToLowerInvariant()); this.OnUserStateChanged?.Invoke(this, new OnUserStateChangedArgs { UserState = val }); } else { this.OnMessageSent?.Invoke(this, new OnMessageSentArgs { SentMessage = new SentMessage(val, _lastMessageSent) }); } } private void Handle004() { this.OnConnected?.Invoke(this, new OnConnectedArgs { BotUsername = TwitchUsername }); } private void Handle353(IrcMessage ircMessage) { this.OnExistingUsersDetected?.Invoke(this, new OnExistingUsersDetectedArgs { Channel = ircMessage.Channel, Users = ircMessage.Message.Split(new char[1] { ' ' }).ToList() }); } private void Handle366() { _currentlyJoiningChannels = false; QueueingJoinCheck(); } private void HandleWhisper(IrcMessage ircMessage) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Expected O, but got Unknown //IL_006e: Unknown result type (might be due to invalid IL or missing references) //IL_0074: Expected O, but got Unknown WhisperMessage val2 = (PreviousWhisper = new WhisperMessage(ircMessage, TwitchUsername)); this.OnWhisperReceived?.Invoke(this, new OnWhisperReceivedArgs { WhisperMessage = val2 }); if (_whisperCommandIdentifiers != null && _whisperCommandIdentifiers.Count != 0 && !string.IsNullOrEmpty(val2.Message) && _whisperCommandIdentifiers.Contains(val2.Message[0])) { WhisperCommand command = new WhisperCommand(val2); this.OnWhisperCommandReceived?.Invoke(this, new OnWhisperCommandReceivedArgs { Command = command }); return; } this.OnUnaccountedFor?.Invoke(this, new OnUnaccountedForArgs { BotUsername = TwitchUsername, Channel = ircMessage.Channel, Location = "WhispergHandling", RawIRC = ircMessage.ToString() }); UnaccountedFor(ircMessage.ToString()); } private void HandleRoomState(IrcMessage ircMessage) { //IL_0091: Unknown result type (might be due to invalid IL or missing references) //IL_009b: Expected O, but got Unknown if (ircMessage.Tags.Count > 2) { KeyValuePair<string, DateTime> item = _awaitingJoins.FirstOrDefault((KeyValuePair<string, DateTime> x) => x.Key == ircMessage.Channel); _awaitingJoins.Remove(item); this.OnJoinedChannel?.Invoke(this, new OnJoinedChannelArgs { BotUsername = TwitchUsername, Channel = ircMessage.Channel }); } this.OnChannelStateChanged?.Invoke(this, new OnChannelStateChangedArgs { ChannelState = new ChannelState(ircMessage), Channel = ircMessage.Channel }); } private void HandleUserNotice(IrcMessage ircMessage) { //IL_02c8: Unknown result type (might be due to invalid IL or missing references) //IL_02cf: Expected O, but got Unknown //IL_0262: Unknown result type (might be due to invalid IL or missing references) //IL_0269: Expected O, but got Unknown //IL_02fb: Unknown result type (might be due to invalid IL or missing references) //IL_0302: Expected O, but got Unknown //IL_022f: Unknown result type (might be due to invalid IL or missing references) //IL_0236: Expected O, but got Unknown //IL_019c: Unknown result type (might be due to invalid IL or missing references) //IL_01a2: Expected O, but got Unknown //IL_0295: Unknown result type (might be due to invalid IL or missing references) //IL_029c: Expected O, but got Unknown //IL_01cd: Unknown result type (might be due to invalid IL or missing references) //IL_01d3: Expected O, but got Unknown //IL_01fe: Unknown result type (might be due to invalid IL or missing references) //IL_0204: Expected O, but got Unknown if (!ircMessage.Tags.TryGetValue("msg-id", out var value)) { this.OnUnaccountedFor?.Invoke(this, new OnUnaccountedForArgs { BotUsername = TwitchUsername, Channel = ircMessage.Channel, Location = "UserNoticeHandling", RawIRC = ircMessage.ToString() }); UnaccountedFor(ircMessage.ToString()); return; } switch (value) { case "announcement": { Announcement announcement = new Announcement(ircMessage); this.OnAnnouncement?.Invoke(this, new OnAnnouncementArgs { Announcement = announcement, Channel = ircMessage.Channel }); break; } case "raid": { RaidNotification raidNotification = new RaidNotification(ircMessage); this.OnRaidNotification?.Invoke(this, new OnRaidNotificationArgs { Channel = ircMessage.Channel, RaidNotification = raidNotification }); break; } case "resub": { ReSubscriber reSubscriber = new ReSubscriber(ircMessage); this.OnReSubscriber?.Invoke(this, new OnReSubscriberArgs { ReSubscriber = reSubscriber, Channel = ircMessage.Channel }); break; } case "subgift": { GiftedSubscription giftedSubscription2 = new GiftedSubscription(ircMessage); this.OnGiftedSubscription?.Invoke(this, new OnGiftedSubscriptionArgs { GiftedSubscription = giftedSubscription2, Channel = ircMessage.Channel }); break; } case "submysterygift": { CommunitySubscription giftedSubscription = new CommunitySubscription(ircMessage); this.OnCommunitySubscription?.Invoke(this, new OnCommunitySubscriptionArgs { GiftedSubscription = giftedSubscription, Channel = ircMessage.Channel }); break; } case "giftpaidupgrade": { ContinuedGiftedSubscription continuedGiftedSubscription = new ContinuedGiftedSubscription(ircMessage); this.OnContinuedGiftedSubscription?.Invoke(this, new OnContinuedGiftedSubscriptionArgs { ContinuedGiftedSubscription = continuedGiftedSubscription, Channel = ircMessage.Channel }); break; } case "sub": { Subscriber subscriber = new Subscriber(ircMessage); this.OnNewSubscriber?.Invoke(this, new OnNewSubscriberArgs { Subscriber = subscriber, Channel = ircMessage.Channel }); break; } case "primepaidupgrade": { PrimePaidSubscriber primePaidSubscriber = new PrimePaidSubscriber(ircMessage); this.OnPrimePaidSubscriber?.Invoke(this, new OnPrimePaidSubscriberArgs { PrimePaidSubscriber = primePaidSubscriber, Channel = ircMessage.Channel }); break; } default: this.OnUnaccountedFor?.Invoke(this, new OnUnaccountedForArgs { BotUsername = TwitchUsername, Channel = ircMessage.Channel, Location = "UserNoticeHandling", RawIRC = ircMessage.ToString() }); UnaccountedFor(ircMessage.ToString()); break; } } private void HandleMode(IrcMessage ircMessage) { if (ircMessage.Message.StartsWith("+o")) { this.OnModeratorJoined?.Invoke(this, new OnModeratorJoinedArgs { Channel = ircMessage.Channel, Username = ircMessage.Message.Split(new char[1] { ' ' })[1] }); } else if (ircMessage.Message.StartsWith("-o")) { this.OnModeratorLeft?.Invoke(this, new OnModeratorLeftArgs { Channel = ircMessage.Channel, Username = ircMessage.Message.Split(new char[1] { ' ' })[1] }); } } private void HandleCap(IrcMessage ircMessage) { } private void UnaccountedFor(string ircString) { Log("Unaccounted for: " + ircString + " (please create a TwitchLib GitHub issue :P)"); } private void Log(string message, bool includeDate = false, bool includeTime = false) { string arg = ((includeDate && includeTime) ? $"{DateTime.UtcNow}" : ((!includeDate) ? (DateTime.UtcNow.ToShortTimeString() ?? "") : (DateTime.UtcNow.ToShortDateString() ?? ""))); if (includeDate || includeTime) { _logger?.LogInformation($"[TwitchLib, {Assembly.GetExecutingAssembly().GetName().Version} - {arg}] {message}"); } else { _logger?.LogInformation($"[TwitchLib, {Assembly.GetExecutingAssembly().GetName().Version}] {message}"); } EventHandler<OnLogArgs> onLog = this.OnLog; if (onLog != null) { OnLogArgs onLogArgs = new OnLogArgs(); ConnectionCredentials connectionCredentials = ConnectionCredentials; onLogArgs.BotUsername = ((connectionCredentials != null) ? connectionCredentials.TwitchUsername : null); onLogArgs.Data = message; onLogArgs.DateTime = DateTime.UtcNow; onLog(this, onLogArgs); } } private void LogError(string message, bool includeDate = false, bool includeTime = false) { string arg = ((includeDate && includeTime) ? $"{DateTime.UtcNow}" : ((!includeDate) ? (DateTime.UtcNow.ToShortTimeString() ?? "") : (DateTime.UtcNow.ToShortDateString() ?? ""))); if (includeDate || includeTime) { _logger?.LogError($"[TwitchLib, {Assembly.GetExecutingAssembly().GetName().Version} - {arg}] {message}"); } else { _logger?.LogError($"[TwitchLib, {Assembly.GetExecutingAssembly().GetName().Version}] {message}"); } EventHandler<OnLogArgs> onLog = this.OnLog; if (onLog != null) { OnLogArgs onLogArgs = new OnLogArgs(); ConnectionCredentials connectionCredentials = ConnectionCredentials; onLogArgs.BotUsername = ((connectionCredentials != null) ? connectionCredentials.TwitchUsername : null); onLogArgs.Data = message; onLogArgs.DateTime = DateTime.UtcNow; onLog(this, onLogArgs); } } public void SendQueuedItem(string message) { if (!IsInitialized) { HandleNotInitialized(); } _client.Send(message); } protected static void HandleNotInitialized() { throw new ClientNotInitializedException("The twitch client has not been initialized and cannot be used. Please call Initialize();"); } protected static void HandleNotConnected() { throw new ClientNotConnectedException("In order to perform this action, the client must be connected to Twitch. To confirm connection, try performing this action in or after the OnConnected event has been fired."); } } } namespace TwitchLib.Client.Manager { internal class JoinedChannelManager { private readonly ConcurrentDictionary<string, JoinedChannel> _joinedChannels; public JoinedChannelManager() { _joinedChannels = new ConcurrentDictionary<string, JoinedChannel>(StringComparer.OrdinalIgnoreCase); } public void AddJoinedChannel(JoinedChannel joinedChannel) { _joinedChannels.TryAdd(joinedChannel.Channel, joinedChannel); } public JoinedChannel GetJoinedChannel(string channel) { _joinedChannels.TryGetValue(channel, out var value); return value; } public IReadOnlyList<JoinedChannel> GetJoinedChannels() { return _joinedChannels.Values.ToList().AsReadOnly(); } public void RemoveJoinedChannel(string channel) { _joinedChannels.TryRemove(channel, out var _); } public void Clear() { _joinedChannels.Clear(); } } } namespace TwitchLib.Client.Internal { public sealed class Rfc2812 { private static readonly Regex NicknameRegex = new Regex("^[A-Za-z\\[\\]\\\\`_^{|}][A-Za-z0-9\\[\\]\\\\`_\\-^{|}]+$", RegexOptions.Compiled); private Rfc2812() { } public static bool IsValidNickname(string nickname) { if (!string.IsNullOrEmpty(nickname)) { return NicknameRegex.Match(nickname).Success; } return false; } public static string Pass(string password) { return "PASS " + password; } public static string Nick(string nickname) { return "NICK " + nickname; } public static string User(string username, int usermode, string realname) { return $"USER {username} {usermode} * :{realname}"; } public static string Oper(string name, string password) { return "OPER " + name + " " + password; } public static string Privmsg(string destination, string message) { return "PRIVMSG " + destination + " :" + message; } public static string Notice(string destination, string message) { return "NOTICE " + destination + " :" + message; } public static string Join(string channel) { return "JOIN " + channel; } public static string Join(string[] channels) { return "JOIN " + string.Join(",", channels); } public static string Join(string channel, string key) { return "JOIN " + channel + " " + key; } public static string Join(string[] channels, string[] keys) { return "JOIN " + string.Join(",", channels) + " " + string.Join(",", keys); } public static string Part(string channel) { return "PART " + channel; } public static string Part(string[] channels) { return "PART " + string.Join(",", channels); } public static string Part(string channel, string partmessage) { return "PART " + channel + " :" + partmessage; } public static string Part(string[] channels, string partmessage) { return "PART " + string.Join(",", channels) + " :" + partmessage; } public static string Kick(string channel, string nickname) { return "KICK " + channel + " " + nickname; } public static string Kick(string channel, string nickname, string comment) { return "KICK " + channel + " " + nickname + " :" + comment; } public static string Kick(string[] channels, string nickname) { return "KICK " + string.Join(",", channels) + " " + nickname; } public static string Kick(string[] channels, string nickname, string comment) { return "KICK " + string.Join(",", channels) + " " + nickname + " :" + comment; } public static string Kick(string channel, string[] nicknames) { return "KICK " + channel + " " + string.Join(",", nicknames); } public static string Kick(string channel, string[] nicknames, string comment) { return "KICK " + channel + " " + string.Join(",", nicknames) + " :" + comment; } public static string Kick(string[] channels, string[] nicknames) { return "KICK " + string.Join(",", channels) + " " + string.Join(",", nicknames); } public static string Kick(string[] channels, string[] nicknames, string comment) { return "KICK " + string.Join(",", channels) + " " + string.Join(",", nicknames) + " :" + comment; } public static string Motd() { return "MOTD"; } public static string Motd(string target) { return "MOTD " + target; } public static string Lusers() { return "LUSERS"; } public static string Lusers(string mask) { return "LUSER " + mask; } public static string Lusers(string mask, string target) { return "LUSER " + mask + " " + target; } public static string Version() { return "VERSION"; } public static string Version(string target) { return "VERSION " + target; } public static string Stats() { return "STATS"; } public static string Stats(string query) { return "STATS " + query; } public static string Stats(string query, string target) { return "STATS " + query + " " + target; } public static string Links() { return "LINKS"; } public static string Links(string servermask) { return "LINKS " + servermask; } public static string Links(string remoteserver, string servermask) { return "LINKS " + remoteserver + " " + servermask; } public static string Time() { return "TIME"; } public static string Time(string target) { return "TIME " + target; } public static string Connect(string targetserver, string port) { return "CONNECT " + targetserver + " " + port; } public static string Connect(string targetserver, string port, string remoteserver) { return "CONNECT " + targetserver + " " + port + " " + remoteserver; } public static string Trace() { return "TRACE"; } public static string Trace(string target) { return "TRACE " + target; } public static string Admin() { return "ADMIN"; } public static string Admin(string target) { return "ADMIN " + target; } public static string Info() { return "INFO"; } public static string Info(string target) { return "INFO " + target; } public static string Servlist() { return "SERVLIST"; } public static string Servlist(string mask) { return "SERVLIST " + mask; } public static string Servlist(string mask, string type) { return "SERVLIST " + mask + " " + type; } public static string Squery(string servicename, string servicetext) { return "SQUERY " + servicename + " :" + servicename; } public static string List() { return "LIST"; } public static string List(string channel) { return "LIST " + channel; } public static string List(string[] channels) { return "LIST " + string.Join(",", channels); } public static string List(string channel, string target) { return "LIST " + channel + " " + target; } public static string List(string[] channels, string target) { return "LIST " + string.Join(",", channels) + " " + target; } public static string Names() { return "NAMES"; } public static string Names(string channel) { return "NAMES " + channel; } public static string Names(string[] channels) { return "NAMES " + string.Join(",", channels); } public static string Names(string channel, string target) { return "NAMES " + channel + " " + target; } public static string Names(string[] channels, string target) { return "NAMES " + string.Join(",", channels) + " " + target; } public static string Topic(string channel) { return "TOPIC " + channel; } public static string Topic(string channel, string newtopic) { return "TOPIC " + channel + " :" + newtopic; } public static string Mode(string target) { return "MODE " + target; } public static string Mode(string target, string newmode) { return "MODE " + target + " " + newmode + target + " " + newmode; } public static string Mode(string target, string[] newModes, string[] newModeParameters) { if (newModes == null) { throw new ArgumentNullException("newModes"); } if (newModeParameters == null) { throw new ArgumentNullException("newModeParameters"); } if (newModes.Length != newModeParameters.Length) { throw new ArgumentException("newModes and newModeParameters must have the same size."); } StringBuilder stringBuilder = new StringBuilder(newModes.Length); StringBuilder stringBuilder2 = new StringBuilder(); if (newModes.Length > 3) { throw new ArgumentOutOfRangeException("Length", newModes.Length, $"Mode change list is too large (> {3})."); } for (int i = 0; i <= newModes.Length; i += 3) { for (int j = 0; j < 3 && i + j < newModes.Length; j++) { stringBuilder.Append(newModes[i + j]); } for (int k = 0; k < 3 && i + k < newModeParameters.Length; k++) { stringBuilder2.Append(newModeParameters[i + k]); stringBuilder2.Append(" "); } } if (stringBuilder2.Length <= 0) { return Mode(target, stringBuilder.ToString()); } stringBuilder2.Length--; stringBuilder.Append(" "); stringBuilder.Append((object?)stringBuilder2); return Mode(target, stringBuilder.ToString()); } public static string Service(string nickname, string distribution, string info) { return "SERVICE " + nickname + " * " + distribution + " * * :" + info; } public static string Invite(string nickname, string channel) { return "INVITE " + nickname + " " + channel; } public static string Who() { return "WHO"; } public static string Who(string mask) { return "WHO " + mask; } public static string Who(string mask, bool ircop) { if (!ircop) { return "WHO " + mask; } return "WHO " + mask + " o"; } public static string Whois(string mask) { return "WHOIS " + mask; } public static string Whois(string[] masks) { return "WHOIS " + string.Join(",", masks); } public static string Whois(string target, string mask) { return "WHOIS " + target + " " + mask; } public static string Whois(string target, string[] masks) { return "WHOIS " + target + " " + string.Join(",", masks); } public static string Whowas(string nickname) { return "WHOWAS " + nickname; } public static string Whowas(string[] nicknames) { return "WHOWAS " + string.Join(",", nicknames); } public static string Whowas(string nickname, string count) { return "WHOWAS " + nickname + " " + count + " "; } public static string Whowas(string[] nicknames, string count) { return "WHOWAS " + string.Join(",", nicknames) + " " + count + " "; } public static string Whowas(string nickname, string count, string target) { return "WHOWAS " + nickname + " " + count + " " + target; } public static string Whowas(string[] nicknames, string count, string target) { return "WHOWAS " + string.Join(",", nicknames) + " " + count + " " + target; } public static string Kill(string nickname, string comment) { return "KILL " + nickname + " :" + comment; } public static string Ping(string server) { return "PING " + server; } public static string Ping(string server, string server2) { return "PING " + server + " " + server2; } public static string Pong(string server) { return "PONG " + server; } public static string Pong(string server, string server2) { return "PONG " + server + " " + server2; } public static string Error(string errormessage) { return "ERROR :" + errormessage; } public static string Away() { return "AWAY"; } public static string Away(string awaytext) { return "AWAY :" + awaytext; } public static string Rehash() { return "REHASH"; } public static string Die() { return "DIE"; } public static string Restart() { return "RESTART"; } public static string Summon(string user) { return "SUMMON " + user; } public static string Summon(string user, string target) { return "SUMMON " + user + " " + target + user + " " + target; } public static string Summon(string user, string target, string channel) { return "SUMMON " + user + " " + target + " " + channel; } public static string Users() { return "USERS"; } public static string Users(string target) { return "USERS " + target; } public static string Wallops(string wallopstext) { return "WALLOPS :" + wallopstext; } public static string Userhost(string nickname) { return "USERHOST " + nickname; } public static string Userhost(string[] nicknames) { return "USERHOST " + string.Join(" ", nicknames); } public static string Ison(string nickname) { return "ISON " + nickname; } public static string Ison(string[] nicknames) { return "ISON " + string.Join(" ", nicknames); } public static string Quit() { return "QUIT"; } public static string Quit(string quitmessage) { return "QUIT :" + quitmessage; } public static string Squit(string server, string comment) { return "SQUIT " + server + " :" + comment; } } } namespace TwitchLib.Client.Internal.Parsing { internal class IrcParser { private enum ParserState { STATE_NONE, STATE_V3, STATE_PREFIX, STATE_COMMAND, STATE_PARAM, STATE_TRAILING } public IrcMessage ParseIrcMessage(string raw) { //IL_01ec: Unknown result type (might be due to invalid IL or missing references) //IL_068c: Unknown result type (might be due to invalid IL or missing references) //IL_064f: Unknown result type (might be due to invalid IL or missing references) //IL_0691: Unknown result type (might be due to invalid IL or missing references) //IL_065f: Unknown result type (might be due to invalid IL or missing references) //IL_06f0: Unknown result type (might be due to invalid IL or missing references) //IL_06b4: Unknown result type (might be due to invalid IL or missing references) //IL_0728: Unknown result type (might be due to invalid IL or missing references) //IL_073d: Unknown result type (might be due to invalid IL or missing references) //IL_0743: Expected O, but got Unknown //IL_06ba: Unknown result type (might be due to invalid IL or missing references) //IL_06cc: Unknown result type (might be due to invalid IL or missing references) //IL_06d8: Unknown result type (might be due to invalid IL or missing references) //IL_06e4: Unknown result type (might be due to invalid IL or missing references) //IL_0680: Unknown result type (might be due to invalid IL or missing references) //IL_06f6: Unknown result type (might be due to invalid IL or missing references) //IL_06de: Unknown result type (might be due to invalid IL or missing references) //IL_06c0: Unknown result type (might be due to invalid IL or missing references) //IL_0696: Unknown result type (might be due to invalid IL or missing references) //IL_0667: Unknown result type (might be due to invalid IL or missing references) //IL_0657: Unknown result type (might be due to invalid IL or missing references) //IL_06ae: Unknown result type (might be due to invalid IL or missing references) //IL_06a8: Unknown result type (might be due to invalid IL or missing references) //IL_06fc: Unknown result type (might be due to invalid IL or missing references) //IL_06d2: Unknown result type (might be due to invalid IL or missing references) //IL_06c6: Unknown result type (might be due to invalid IL or missing references) //IL_06ea: Unknown result type (might be due to invalid IL or missing references) //IL_0677: Unknown result type (might be due to invalid IL or missing references) //IL_066f: Unknown result type (might be due to invalid IL or missing references) //IL_06a2: Unknown result type (might be due to invalid IL or missing references) //IL_0686: Unknown result type (might be due to invalid IL or missing references) //IL_069c: Unknown result type (might be due to invalid IL or missing references) Dictionary<string, string> dictionary = new Dictionary<string, string>(); ParserState parserState = ParserState.STATE_NONE; int[] array = new int[6]; int[] array2 = new int[6]; for (int i = 0; i < raw.Length; i++) { array2[(int)parserState] = i - array[(int)parserState] - 1; if (parserState == ParserState.STATE_NONE && raw[i] == '@') { parserState = ParserState.STATE_V3; i = (array[(int)parserState] = i + 1); int num = i; string text = null; for (; i < raw.Length; i++) { if (raw[i] == '=') { text = raw.Substring(num, i - num); num = i + 1; } else if (raw[i] == ';') { if (text == null) { dictionary[raw.Substring(num, i - num)] = "1"; } else { dictionary[text] = raw.Substring(num, i - num); } num = i + 1; } else if (raw[i] == ' ') { if (text == null) { dictionary[raw.Substring(num, i - num)] = "1"; } else { dictionary[text] = raw.Substring(num, i - num); } break; } } } else if (parserState < ParserState.STATE_PREFIX && raw[i] == ':') { parserState = ParserState.STATE_PREFIX; i = (array[(int)parserState] = i + 1); } else if (parserState < ParserState.STATE_COMMAND) { parserState = ParserState.STATE_COMMAND; array[(int)parserState] = i; } else { if (parserState < ParserState.STATE_TRAILING && raw[i] == ':') { parserState = ParserState.STATE_TRAILING; i = (array[(int)parserState] = i + 1); break; } if ((parserState < ParserState.STATE_TRAILING && raw[i] == '+') || (parserState < ParserState.STATE_TRAILING && raw[i] == '-')) { parserState = ParserState.STATE_TRAILING; array[(int)parserState] = i; break; } if (parserState == ParserState.STATE_COMMAND) { parserState = ParserState.STATE_PARAM; array[(int)parserState] = i; } } for (; i < raw.Length && raw[i] != ' '; i++) { } } array2[(int)parserState] = raw.Length - array[(int)parserState]; string text2 = raw.Substring(array[3], array2[3]); IrcCommand val = (IrcCommand)0; switch (text2) { case "PRIVMSG": val = (IrcCommand)1; break; case "NOTICE": val = (IrcCommand)2; break; case "PING": val = (IrcCommand)3; break; case "PONG": val = (IrcCommand)4; break; case "CLEARCHAT": val = (IrcCommand)7; break; case "CLEARMSG": val = (IrcCommand)8; break; case "USERSTATE": val = (IrcCommand)9; break; case "GLOBALUSERSTATE": val = (IrcCommand)10; break; case "NICK": val = (IrcCommand)11; break; case "JOIN": val = (IrcCommand)5; break; case "PART": val = (IrcCommand)6; break; case "PASS": val = (IrcCommand)12; break; case "CAP": val = (IrcCommand)13; break; case "001": val = (IrcCommand)14; break; case "002": val = (IrcCommand)15; break; case "003": val = (IrcCommand)16; break; case "004": val = (IrcCommand)17; break; case "353": val = (IrcCommand)18; break; case "366": val = (IrcCommand)19; break; case "372": val = (IrcCommand)20; break; case "375": val = (IrcCommand)21; break; case "376": val = (IrcCommand)22; break; case "WHISPER": val = (IrcCommand)23; break; case "SERVERCHANGE": val = (IrcCommand)26; break; case "RECONNECT": val = (IrcCommand)25; break; case "ROOMSTATE": val = (IrcCommand)24; break; case "USERNOTICE": val = (IrcCommand)27; break; case "MODE": val = (IrcCommand)28; break; } string text3 = raw.Substring(array[4], array2[4]); string text4 = raw.Substring(array[5], array2[5]); string text5 = raw.Substring(array[2], array2[2]); return new IrcMessage(val, new string[2] { text3, text4 }, text5, dictionary); } } } namespace TwitchLib.Client.Interfaces { public interface ITwitchClient { bool AutoReListenOnException { get; set; } MessageEmoteCollection ChannelEmotes { get; } ConnectionCredentials ConnectionCredentials { get; } bool DisableAutoPong { get; set; } bool IsConnected { get; } bool IsInitialized { get; } IReadOnlyList<JoinedChannel> JoinedChannels { get; } WhisperMessage PreviousWhisper { get; } string TwitchUsername { get; } bool WillReplaceEmotes { get; set; } event EventHandler<OnChannelStateChangedArgs> OnChannelStateChanged; event EventHandler<OnChatClearedArgs> OnChatCleared; event EventHandler<OnChatColorChangedArgs> OnChatColorChanged; event EventHandler<OnChatCommandReceivedArgs> OnChatCommandReceived; event EventHandler<OnConnectedArgs> OnConnected; event EventHandler<OnConnectionErrorArgs> OnConnectionError; event EventHandler<OnDisconnectedEventArgs> OnDisconnected; event EventHandler<OnExistingUsersDetectedArgs> OnExistingUsersDetected; event EventHandler<OnGiftedSubscriptionArgs> OnGiftedSubscription; event EventHandler<OnIncorrectLoginArgs> OnIncorrectLogin; event EventHandler<OnJoinedChannelArgs> OnJoinedChannel; event EventHandler<OnLeftChannelArgs> OnLeftChannel; event EventHandler<OnLogArgs> OnLog; event EventHandler<OnMessageReceivedArgs> OnMessageReceived; event EventHandler<OnMessageSentArgs> OnMessageSent; event EventHandler<OnModeratorJoinedArgs> OnModeratorJoined; event EventHandler<OnModeratorLeftArgs> OnModeratorLeft; event EventHandler<OnModeratorsReceivedArgs> OnModeratorsReceived; event EventHandler<OnNewSubscriberArgs> OnNewSubscriber; event EventHandler<OnRaidNotificationArgs> OnRaidNotification; event EventHandler<OnReSubscriberArgs> OnReSubscriber; event EventHandler<OnSendReceiveDataArgs> OnSendReceiveData; event EventHandler<OnUserBannedArgs> OnUserBanned; event EventHandler<OnUserJoinedArgs> OnUserJoined; event EventHandler<OnUserLeftArgs> OnUserLeft; event EventHandler<OnUserStateChangedArgs> OnUserStateChanged; event EventHandler<OnUserTimedoutArgs> OnUserTimedout; event EventHandler<OnWhisperCommandReceivedArgs> OnWhisperCommandReceived; event EventHandler<OnWhisperReceivedArgs> OnWhisperReceived; event EventHandler<OnWhisperSentArgs> OnWhisperSent; event EventHandler<OnMessageThrottledEventArgs> OnMessageThrottled; event EventHandler<OnWhisperThrottledEventArgs> OnWhisperThrottled; event EventHandler<OnErrorEventArgs> OnError; event EventHandler<OnReconnectedEventArgs> OnReconnected; event EventHandler<OnVIPsReceivedArgs> OnVIPsReceived; event EventHandler<OnCommunitySubscriptionArgs> OnCommunitySubscription; event EventHandler<OnMessageClearedArgs> OnMessageCleared; event EventHandler<OnRequiresVerifiedEmailArgs> OnRequiresVerifiedEmail; event EventHandler<OnRequiresVerifiedPhoneNumberArgs> OnRequiresVerifiedPhoneNumber; event EventHandler<OnBannedEmailAliasArgs> OnBannedEmailAlias; event EventHandler<OnUserIntroArgs> OnUserIntro; event EventHandler<OnAnnouncementArgs> OnAnnouncement; void Initialize(ConnectionCredentials credentials, string channel = null, char chatCommandIdentifier = '!', char whisperCommandIdentifier = '!', bool autoReListenOnExceptions = true); void Initialize(ConnectionCredentials credentials, List<string> channels, char chatCommandIdentifier = '!', char whisperCommandIdentifier = '!', bool autoReListenOnExceptions = true); void SetConnectionCredentials(ConnectionCredentials credentials); void AddChatCommandIdentifier(char identifier); void AddWhisperCommandIdentifier(char identifier); void RemoveChatCommandIdentifier(char identifier); void RemoveWhisperCommandIdentifier(char identifier); bool Connect(); void Disconnect(); void Reconnect(); JoinedChannel GetJoinedChannel(string channel); void JoinChannel(string channel, bool overrideCheck = false); void LeaveChannel(JoinedChannel channel); void LeaveChannel(string channel); void OnReadLineTest(string rawIrc); void SendMessage(JoinedChannel channel, string message, bool dryRun = false); void SendMessage(string channel, string message, bool dryRun = false); void SendReply(JoinedChannel channel, string replyToId, string message, bool dryRun = false); void SendReply(string channel, string replyToId, string message, bool dryRun = false); void SendQueuedItem(string message); void SendRaw(string message); void SendWhisper(string receiver, string message, bool dryRun = false); } } namespace TwitchLib.Client.Extensions { public static class AnnoucementExt { public static void Announce(this ITwitchClient client, JoinedChannel channel, string message) { client.SendMessage(channel, ".announce " + message); } public static void Announce(this ITwitchClient client, string channel, string message) { client.SendMessage(channel, ".announce " + message); } } public static class BanUserExt { public static void BanUser(this ITwitchClient client, JoinedChannel channel, string viewer, string message = "", bool dryRun = false) { client.SendMessage(channel, ".ban " + viewer + " " + message); } public static void BanUser(this ITwitchClient client, string channel, string viewer, string message = "", bool dryRun = false) { JoinedChannel joinedChannel = client.GetJoinedChannel(channel); if (joinedChannel != null) { client.BanUser(joinedChannel, viewer, message, dryRun); } } } public static class ChangeChatColorExt { public static void ChangeChatColor(this ITwitchClient client, JoinedChannel channel, ChatColorPresets color) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) client.SendMessage(channel, $".color {color}"); } public static void ChangeChatColor(this ITwitchClient client, string channel, ChatColorPresets color) { //IL_0007: Unknown result type (might be due to invalid IL or missing references) client.SendMessage(channel, $".color {color}"); } } public static class ClearChatExt { public static void ClearChat(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".clear"); } public static void ClearChat(this ITwitchClient client, string channel) { client.SendMessage(channel, ".clear"); } } public static class CommercialExt { public static void StartCommercial(this ITwitchClient client, JoinedChannel channel, CommercialLength length) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0003: Invalid comparison between Unknown and I4 //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Invalid comparison between Unknown and I4 //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Invalid comparison between Unknown and I4 //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Invalid comparison between Unknown and I4 //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Invalid comparison between Unknown and I4 //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0029: Invalid comparison between Unknown and I4 //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Invalid comparison between Unknown and I4 //IL_0086: Unknown result type (might be due to invalid IL or missing references) if ((int)length <= 90) { if ((int)length == 30) { client.SendMessage(channel, ".commercial 30"); return; } if ((int)length == 60) { client.SendMessage(channel, ".commercial 60"); return; } if ((int)length == 90) { client.SendMessage(channel, ".commercial 90"); return; } } else { if ((int)length == 120) { client.SendMessage(channel, ".commercial 120"); return; } if ((int)length == 150) { client.SendMessage(channel, ".commercial 150"); return; } if ((int)length == 180) { client.SendMessage(channel, ".commercial 180"); return; } } throw new ArgumentOutOfRangeException("length", length, null); } public static void StartCommercial(this ITwitchClient client, string channel, CommercialLength length) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0003: Invalid comparison between Unknown and I4 //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Invalid comparison between Unknown and I4 //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Invalid comparison between Unknown and I4 //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Invalid comparison between Unknown and I4 //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Invalid comparison between Unknown and I4 //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0029: Invalid comparison between Unknown and I4 //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Invalid comparison between Unknown and I4 //IL_0086: Unknown result type (might be due to invalid IL or missing references) if ((int)length <= 90) { if ((int)length == 30) { client.SendMessage(channel, ".commercial 30"); return; } if ((int)length == 60) { client.SendMessage(channel, ".commercial 60"); return; } if ((int)length == 90) { client.SendMessage(channel, ".commercial 90"); return; } } else { if ((int)length == 120) { client.SendMessage(channel, ".commercial 120"); return; } if ((int)length == 150) { client.SendMessage(channel, ".commercial 150"); return; } if ((int)length == 180) { client.SendMessage(channel, ".commercial 180"); return; } } throw new ArgumentOutOfRangeException("length", length, null); } } public static class DeleteMessageExt { public static void DeleteMessage(this ITwitchClient client, JoinedChannel channel, string messageId) { client.SendMessage(channel, ".delete " + messageId); } public static void DeleteMessage(this ITwitchClient client, string channel, string messageId) { client.SendMessage(channel, ".delete " + messageId); } public static void DeleteMessage(this ITwitchClient client, JoinedChannel channel, ChatMessage msg) { client.SendMessage(channel, ".delete " + msg.Id); } public static void DeleteMessage(this ITwitchClient client, string channel, ChatMessage msg) { client.SendMessage(channel, ".delete " + msg.Id); } } public static class EmoteOnlyExt { public static void EmoteOnlyOn(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".emoteonly"); } public static void EmoteOnlyOn(this ITwitchClient client, string channel) { client.SendMessage(channel, ".emoteonly"); } public static void EmoteOnlyOff(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".emoteonlyoff"); } public static void EmoteOnlyOff(this ITwitchClient client, string channel) { client.SendMessage(channel, ".emoteonlyoff"); } } public static class EventInvocationExt { public static void InvokeChannelStateChanged(this TwitchClient client, string channel, bool r9k, bool rituals, bool subOnly, int slowMode, bool emoteOnly, string broadcasterLanguage, TimeSpan followersOnly, bool mercury, string roomId) { //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Expected O, but got Unknown ChannelState channelState = new ChannelState(r9k, rituals, subOnly, slowMode, emoteOnly, broadcasterLanguage, channel, followersOnly, mercury, roomId); OnChannelStateChangedArgs args = new OnChannelStateChangedArgs { Channel = channel, ChannelState = channelState }; client.RaiseEvent("OnChannelStateChanged", args); } public static void InvokeChatCleared(this TwitchClient client, string channel) { OnChatClearedArgs args = new OnChatClearedArgs { Channel = channel }; client.RaiseEvent("OnChatCleared", args); } public static void InvokeChatCommandsReceived(this TwitchClient client, string botUsername, string userId, string userName, string displayName, string colorHex, Color color, EmoteSet emoteSet, string message, UserType userType, string channel, string id, bool isSubscriber, int subscribedMonthCount, string roomId, bool isTurbo, bool isModerator, bool isMe, bool isBroadcaster, bool isVip, bool isPartner, bool isStaff, Noisy noisy, string rawIrcMessage, string emoteReplacedMessage, List<KeyValuePair<string, string>> badges, CheerBadge cheerBadge, int bits, double bitsInDollars, string commandText, string argumentsAsString, List<string> argumentsAsList, char commandIdentifier) { //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_0027: Unknown result type (might be due to invalid IL or missing references) //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Expected O, but got Unknown //IL_004a: Unknown result type (might be due to invalid IL or missing references) //IL_0054: Expected O, but got Unknown ChatMessage val = new ChatMessage(botUsername, userId, userName, displayName, colorHex, color, emoteSet, message, userType, channel, id, isSubscriber, subscribedMonthCount, roomId, isTurbo, isModerator, isMe, isBroadcaster, isVip, isPartner, isStaff, noisy, rawIrcMessage, emoteReplacedMessage, badges, cheerBadge, bits, bitsInDollars); OnChatCommandReceivedArgs args = new OnChatCommandReceivedArgs { Command = new ChatCommand(val, commandText, argumentsAsString, argumentsAsList, commandIdentifier) }; client.RaiseEvent("OnChatCommandReceived", args); } public static void InvokeConnected(this TwitchClient client, string autoJoinChannel, string botUsername) { OnConnectedArgs args = new OnConnectedArgs { AutoJoinChannel = autoJoinChannel, BotUsername = botUsername }; client.RaiseEvent("OnConnected", args); } public static void InvokeConnectionError(this TwitchClient client, string botUsername, ErrorEvent errorEvent) { OnConnectionErrorArgs args = new OnConnectionErrorArgs { BotUsername = botUsername, Error = errorEvent }; client.RaiseEvent("OnConnectionError", args); } public static void InvokeDisconnected(this TwitchClient client, string botUsername) { OnDisconnectedArgs args = new OnDisconnectedArgs { BotUsername = botUsername }; client.RaiseEvent("OnDisconnected", args); } public static void InvokeExistingUsersDetected(this TwitchClient client, string channel, List<string> users) { OnExistingUsersDetectedArgs args = new OnExistingUsersDetectedArgs { Channel = channel, Users = users }; client.RaiseEvent("OnExistingUsersDetected", args); } public static void InvokeGiftedSubscription(this TwitchClient client, List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string color, string displayName, string emotes, string id, string login, bool isModerator, string msgId, string msgParamMonths, string msgParamRecipientDisplayName, string msgParamRecipientId, string msgParamRecipientUserName, string msgParamSubPlanName, string msgMultiMonthGiftDuration, SubscriptionPlan msgParamSubPlan, string roomId, bool isSubscriber, string systemMsg, string systemMsgParsed, string tmiSentTs, bool isTurbo, UserType userType, string userId) { //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_003d: Expected O, but got Unknown OnGiftedSubscriptionArgs args = new OnGiftedSubscriptionArgs { GiftedSubscription = new GiftedSubscription(badges, badgeInfo, color, displayName, emotes, id, login, isModerator, msgId, msgParamMonths, msgParamRecipientDisplayName, msgParamRecipientId, msgParamRecipientUserName, msgParamSubPlanName, msgMultiMonthGiftDuration, msgParamSubPlan, roomId, isSubscriber, systemMsg, systemMsgParsed, tmiSentTs, isTurbo, userType, userId) }; client.RaiseEvent("OnGiftedSubscription", args); } public static void InvokeIncorrectLogin(this TwitchClient client, ErrorLoggingInException ex) { OnIncorrectLoginArgs args = new OnIncorrectLoginArgs { Exception = ex }; client.RaiseEvent("OnIncorrectLogin", args); } public static void InvokeJoinedChannel(this TwitchClient client, string botUsername, string channel) { OnJoinedChannelArgs args = new OnJoinedChannelArgs { BotUsername = botUsername, Channel = channel }; client.RaiseEvent("OnJoinedChannel", args); } public static void InvokeLeftChannel(this TwitchClient client, string botUsername, string channel) { OnLeftChannelArgs args = new OnLeftChannelArgs { BotUsername = botUsername, Channel = channel }; client.RaiseEvent("OnLeftChannel", args); } public static void InvokeLog(this TwitchClient client, string botUsername, string data, DateTime dateTime) { OnLogArgs args = new OnLogArgs { BotUsername = botUsername, Data = data, DateTime = dateTime }; client.RaiseEvent("OnLog", args); } public static void InvokeMessageReceived(this TwitchClient client, string botUsername, string userId, string userName, string displayName, string colorHex, Color color, EmoteSet emoteSet, string message, UserType userType, string channel, string id, bool isSubscriber, int subscribedMonthCount, string roomId, bool isTurbo, bool isModerator, bool isMe, bool isBroadcaster, bool isVip, bool isPartner, bool isStaff, Noisy noisy, string rawIrcMessage, string emoteReplacedMessage, List<KeyValuePair<string, string>> badges, CheerBadge cheerBadge, int bits, double bitsInDollars) { //IL_0013: Unknown result type (might be due to invalid IL or missing references) //IL_002d: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Unknown result type (might be due to invalid IL or missing references) //IL_0045: Expected O, but got Unknown OnMessageReceivedArgs args = new OnMessageReceivedArgs { ChatMessage = new ChatMessage(botUsername, userId, userName, displayName, colorHex, color, emoteSet, message, userType, channel, id, isSubscriber, subscribedMonthCount, roomId, isTurbo, isModerator, isMe, isBroadcaster, isVip, isPartner, isStaff, noisy, rawIrcMessage, emoteReplacedMessage, badges, cheerBadge, bits, bitsInDollars) }; client.RaiseEvent("OnMessageReceived", args); } public static void InvokeMessageSent(this TwitchClient client, List<KeyValuePair<string, string>> badges, string channel, string colorHex, string displayName, string emoteSet, bool isModerator, bool isSubscriber, UserType userType, string message) { //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_0015: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Expected O, but got Unknown OnMessageSentArgs args = new OnMessageSentArgs { SentMessage = new SentMessage(badges, channel, colorHex, displayName, emoteSet, isModerator, isSubscriber, userType, message) }; client.RaiseEvent("OnMessageSent", args); } public static void InvokeModeratorJoined(this TwitchClient client, string channel, string username) { OnModeratorJoinedArgs args = new OnModeratorJoinedArgs { Channel = channel, Username = username }; client.RaiseEvent("OnModeratorJoined", args); } public static void InvokeModeratorLeft(this TwitchClient client, string channel, string username) { OnModeratorLeftArgs args = new OnModeratorLeftArgs { Channel = channel, Username = username }; client.RaiseEvent("OnModeratorLeft", args); } public static void InvokeModeratorsReceived(this TwitchClient client, string channel, List<string> moderators) { OnModeratorsReceivedArgs args = new OnModeratorsReceivedArgs { Channel = channel, Moderators = moderators }; client.RaiseEvent("OnModeratorsReceived", args); } public static void InvokeNewSubscriber(this TwitchClient client, List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string colorHex, Color color, string displayName, string emoteSet, string id, string login, string systemMessage, string msgId, string msgParamCumulativeMonths, string msgParamStreakMonths, bool msgParamShouldShareStreak, string systemMessageParsed, string resubMessage, SubscriptionPlan subscriptionPlan, string subscriptionPlanName, string roomId, string userId, bool isModerator, bool isTurbo, bool isSubscriber, bool isPartner, string tmiSentTs, UserType userType, string rawIrc, string channel) { //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_0039: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Expected O, but got Unknown OnNewSubscriberArgs args = new OnNewSubscriberArgs { Subscriber = new Subscriber(badges, badgeInfo, colorHex, color, displayName, emoteSet, id, login, systemMessage, msgId, msgParamCumulativeMonths, msgParamStreakMonths, msgParamShouldShareStreak, systemMessageParsed, resubMessage, subscriptionPlan, subscriptionPlanName, roomId, userId, isModerator, isTurbo, isSubscriber, isPartner, tmiSentTs, userType, rawIrc, channel) }; client.RaiseEvent("OnNewSubscriber", args); } public static void InvokeRaidNotification(this TwitchClient client, string channel, List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string color, string displayName, string emotes, string id, string login, bool moderator, string msgId, string msgParamDisplayName, string msgParamLogin, string msgParamViewerCount, string roomId, bool subscriber, string systemMsg, string systemMsgParsed, string tmiSentTs, bool turbo, UserType userType, string userId) { //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_003d: Expected O, but got Unknown OnRaidNotificationArgs args = new OnRaidNotificationArgs { Channel = channel, RaidNotification = new RaidNotification(badges, badgeInfo, color, displayName, emotes, id, login, moderator, msgId, msgParamDisplayName, msgParamLogin, msgParamViewerCount, roomId, subscriber, systemMsg, systemMsgParsed, tmiSentTs, turbo, userType, userId) }; client.RaiseEvent("OnRaidNotification", args); } public static void InvokeReSubscriber(this TwitchClient client, List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string colorHex, Color color, string displayName, string emoteSet, string id, string login, string systemMessage, string msgId, string msgParamCumulativeMonths, string msgParamStreakMonths, bool msgParamShouldShareStreak, string systemMessageParsed, string resubMessage, SubscriptionPlan subscriptionPlan, string subscriptionPlanName, string roomId, string userId, bool isModerator, bool isTurbo, bool isSubscriber, bool isPartner, string tmiSentTs, UserType userType, string rawIrc, string channel) { //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_0044: Expected O, but got Unknown OnReSubscriberArgs args = new OnReSubscriberArgs { ReSubscriber = new ReSubscriber(badges, badgeInfo, colorHex, color, displayName, emoteSet, id, login, systemMessage, msgId, msgParamCumulativeMonths, msgParamStreakMonths, msgParamShouldShareStreak, systemMessageParsed, resubMessage, subscriptionPlan, subscriptionPlanName, roomId, userId, isModerator, isTurbo, isSubscriber, isPartner, tmiSentTs, userType, rawIrc, channel, 0) }; client.RaiseEvent("OnReSubscriber", args); } public static void InvokeSendReceiveData(this TwitchClient client, string data, SendReceiveDirection direction) { //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) OnSendReceiveDataArgs args = new OnSendReceiveDataArgs { Data = data, Direction = direction }; client.RaiseEvent("OnSendReceiveData", args); } public static void InvokeUserBanned(this TwitchClient client, string channel, string username, string banReason, string roomId, string targetUserId) { //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Expected O, but got Unknown OnUserBannedArgs args = new OnUserBannedArgs { UserBan = new UserBan(channel, username, banReason, roomId, targetUserId) }; client.RaiseEvent("OnUserBanned", args); } public static void InvokeUserJoined(this TwitchClient client, string channel, string username) { OnUserJoinedArgs args = new OnUserJoinedArgs { Channel = channel, Username = username }; client.RaiseEvent("OnUserJoined", args); } public static void InvokeUserLeft(this TwitchClient client, string channel, string username) { OnUserLeftArgs args = new OnUserLeftArgs { Channel = channel, Username = username }; client.RaiseEvent("OnUserLeft", args); } public static void InvokeUserStateChanged(this TwitchClient client, List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string colorHex, string displayName, string emoteSet, string channel, string id, bool isSubscriber, bool isModerator, UserType userType) { //IL_0015: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Expected O, but got Unknown OnUserStateChangedArgs args = new OnUserStateChangedArgs { UserState = new UserState(badges, badgeInfo, colorHex, displayName, emoteSet, channel, id, isSubscriber, isModerator, userType) }; client.RaiseEvent("OnUserStateChanged", args); } public static void InvokeUserTimedout(this TwitchClient client, string channel, string username, string targetUserId, int timeoutDuration, string timeoutReason) { //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Expected O, but got Unknown OnUserTimedoutArgs args = new OnUserTimedoutArgs { UserTimeout = new UserTimeout(channel, username, targetUserId, timeoutDuration, timeoutReason) }; client.RaiseEvent("OnUserTimedout", args); } public static void InvokeWhisperCommandReceived(this TwitchClient client, List<KeyValuePair<string, string>> badges, string colorHex, Color color, string username, string displayName, EmoteSet emoteSet, string threadId, string messageId, string userId, bool isTurbo, string botUsername, string message, UserType userType, string commandText, string argumentsAsString, List<string> argumentsAsList, char commandIdentifier) { //IL_0015: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Expected O, but got Unknown //IL_002c: Unknown result type (might be due to invalid IL or missing references) //IL_0036: Expected O, but got Unknown WhisperMessage val = new WhisperMessage(badges, colorHex, color, username, displayName, emoteSet, threadId, messageId, userId, isTurbo, botUsername, message, userType); OnWhisperCommandReceivedArgs args = new OnWhisperCommandReceivedArgs { Command = new WhisperCommand(val, commandText, argumentsAsString, argumentsAsList, commandIdentifier) }; client.RaiseEvent("OnWhisperCommandReceived", args); } public static void InvokeWhisperReceived(this TwitchClient client, List<KeyValuePair<string, string>> badges, string colorHex, Color color, string username, string displayName, EmoteSet emoteSet, string threadId, string messageId, string userId, bool isTurbo, string botUsername, string message, UserType userType) { //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_0027: Expected O, but got Unknown OnWhisperReceivedArgs args = new OnWhisperReceivedArgs { WhisperMessage = new WhisperMessage(badges, colorHex, color, username, displayName, emoteSet, threadId, messageId, userId, isTurbo, botUsername, message, userType) }; client.RaiseEvent("OnWhisperReceived", args); } public static void InvokeWhisperSent(this TwitchClient client, string username, string receiver, string message) { OnWhisperSentArgs args = new OnWhisperSentArgs { Message = message, Receiver = receiver, Username = username }; client.RaiseEvent("OnWhisperSent", args); } } public static class FollowersOnlyExt { private const int MaximumDurationAllowedDays = 90; public static void FollowersOnlyOn(this ITwitchClient client, JoinedChannel channel, TimeSpan requiredFollowTime) { if (requiredFollowTime > TimeSpan.FromDays(90.0)) { throw new InvalidParameterException("The amount of time required to chat exceeded the maximum allowed by Twitch, which is 3 months.", client.TwitchUsername); } string text = $"{requiredFollowTime.Days}d {requiredFollowTime.Hours}h {requiredFollowTime.Minutes}m"; client.SendMessage(channel, ".followers " + text); } public static void FollowersOnlyOn(this ITwitchClient client, string channel, TimeSpan requiredFollowTime) { if (requiredFollowTime > TimeSpan.FromDays(90.0)) { throw new InvalidParameterException("The amount of time required to chat exceeded the maximum allowed by Twitch, which is 3 months.", client.TwitchUsername); } string text = $"{requiredFollowTime.Days}d {requiredFollowTime.Hours}h {requiredFollowTime.Minutes}m"; client.SendMessage(channel, ".followers " + text); } public static void FollowersOnlyOff(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".followersoff"); } public static void FollowersOnlyOff(this TwitchClient client, string channel) { client.SendMessage(channel, ".followersoff"); } } public static class GetChannelModeratorsExt { public static void GetChannelModerators(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".mods"); } public static void GetChannelModerators(this ITwitchClient client, string channel) { client.SendMessage(channel, ".mods"); } } public static class MarkerExt { public static void Marker(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".marker"); } public static void Marker(this ITwitchClient client, string channel) { client.SendMessage(channel, ".marker"); } } public static class ModExt { public static void Mod(this ITwitchClient client, JoinedChannel channel, string viewerToMod) { client.SendMessage(channel, ".mod " + viewerToMod); } public static void Mod(this ITwitchClient client, string channel, string viewerToMod) { client.SendMessage(channel, ".mod " + viewerToMod); } public static void Unmod(this ITwitchClient client, JoinedChannel channel, string viewerToUnmod) { client.SendMessage(channel, ".unmod " + viewerToUnmod); } public static void Unmod(this ITwitchClient client, string channel, string viewerToUnmod) { client.SendMessage(channel, ".unmod " + viewerToUnmod); } } public static class RaidExt { public static void Raid(this ITwitchClient client, JoinedChannel channel, string channelToRaid) { client.SendMessage(channel, ".raid " + channelToRaid); } public static void Raid(this ITwitchClient client, string channel, string channelToRaid) { client.SendMessage(channel, ".raid " + channelToRaid); } } public static class ReplyWhisperExt { public static void ReplyToLastWhisper(this ITwitchClient client, string message = "", bool dryRun = false) { if (client.PreviousWhisper != null) { client.SendWhisper(((TwitchLibMessage)client.PreviousWhisper).Username, message, dryRun); } } } public static class SlowModeExt { public static void SlowModeOn(this ITwitchClient client, JoinedChannel channel, TimeSpan messageCooldown) { if (messageCooldown > TimeSpan.FromDays(1.0)) { throw new InvalidParameterException("The message cooldown time supplied exceeded the maximum allowed by Twitch, which is 1 day.", client.TwitchUsername); } client.SendMessage(channel, $".slow {messageCooldown.TotalSeconds}"); } public static void SlowModeOn(this ITwitchClient client, string channel, TimeSpan messageCooldown) { if (messageCooldown > TimeSpan.FromDays(1.0)) { throw new InvalidParameterException("The message cooldown time supplied exceeded the maximum allowed by Twitch, which is 1 day.", client.TwitchUsername); } client.SendMessage(channel, $".slow {messageCooldown.TotalSeconds}"); } public static void SlowModeOff(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".slowoff"); } public static void SlowModeOff(this ITwitchClient client, string channel) { client.SendMessage(channel, ".slowoff"); } } public static class SubscribersOnly { public static void SubscribersOnlyOn(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".subscribers"); } public static void SubscribersOnlyOn(this ITwitchClient client, string channel) { client.SendMessage(channel, ".subscribers"); } public static void SubscribersOnlyOff(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".subscribersoff"); } public static void SubscribersOnlyOff(this ITwitchClient client, string channel) { client.SendMessage(channel, ".subscribersoff"); } } public static class TimeoutUserExt { public static void TimeoutUser(this ITwitchClient client, JoinedChannel channel, string viewer, TimeSpan duration, string message = "", bool dryRun = false) { client.SendMessage(channel, $".timeout {viewer} {duration.TotalSeconds} {message}", dryRun); } public static void TimeoutUser(this ITwitchClient client, string channel, string viewer, TimeSpan duration, string message = "", bool dryRun = false) { JoinedChannel joinedChannel = client.GetJoinedChannel(channel); if (joinedChannel != null) { client.TimeoutUser(joinedChannel, viewer, duration, message, dryRun); } } } public static class UnbanUserExt { public static void UnbanUser(this ITwitchClient client, JoinedChannel channel, string viewer, bool dryRun = false) { client.SendMessage(channel, ".unban " + viewer, dryRun); } public static void UnbanUser(this ITwitchClient client, string channel, string viewer, bool dryRun = false) { JoinedChannel joinedChannel = client.GetJoinedChannel(channel); if (joinedChannel != null) { client.UnbanUser(joinedChannel, viewer, dryRun); } } } public static class VIPExt { public static void VIP(this ITwitchClient client, JoinedChannel channel, string viewerToVIP) { client.SendMessage(channel, ".vip " + viewerToVIP); } public static void VIP(this ITwitchClient client, string channel, string viewerToVIP) { client.SendMessage(channel, ".vip " + viewerToVIP); } public static void UnVIP(this ITwitchClient client, JoinedChannel channel, string viewerToUnVIP) { client.SendMessage(channel, ".unvip " + viewerToUnVIP); } public static void UnVIP(this ITwitchClient client, string channel, string viewerToUnVIP) { client.SendMessage(channel, ".unvip " + viewerToUnVIP); } public static void GetVIPs(this ITwitchClient client, JoinedChannel channel) { client.SendMessage(channel, ".vips"); } public static void GetVIPs(this ITwitchClient client, string channel) { client.SendMessage(channel, ".vips"); } } } namespace TwitchLib.Client.Exceptions { public class BadListenException : Exception { public BadListenException(string eventName, string additionalDetails = "") : base(string.IsNullOrEmpty(additionalDetails) ? ("You are listening to event '" + eventName + "', which is not currently allowed. See details: " + additionalDetails) : ("You are listening to event '" + eventName + "', which is not currently allowed.")) { } } public class BadStateException : Exception { public BadStateException(string details) : base(details) { } } public class ClientNotConnectedException : Exception { public ClientNotConnectedException(string description) : base(description) { } } public class ClientNotInitializedException : Exception { public ClientNotInitializedException(string description) : base(description) { } } public class ErrorLoggingInException : Exception { public string Username { get; protected set; } public ErrorLoggingInException(string ircData, string twitchUsername) : base(ircData) { Username = twitchUsername; } } public class EventNotHandled : Exception { public EventNotHandled(string eventName, string additionalDetails = "") : base(string.IsNullOrEmpty(additionalDetails) ? ("To use this call, you must handle/subscribe to event: " + eventName) : ("To use this call, you must handle/subscribe to event: " + eventName + ", additional details: " + additionalDetails)) { } } public class FailureToReceiveJoinConfirmationException { public string Channel { get; protected set; } public string Details { get; protected set; }
TwitchLib.Client.Enums.dll
Decompiled a month agousing System.Diagnostics; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.Versioning; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")] [assembly: AssemblyCompany("swiftyspiffy, Prom3theu5, Syzuna, LuckyNoS7evin")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright 2021")] [assembly: AssemblyDescription("Project containing all of the enums used in TwitchLib.Client.")] [assembly: AssemblyFileVersion("3.3.1")] [assembly: AssemblyInformationalVersion("3.3.1")] [assembly: AssemblyProduct("TwitchLib.Client.Enums")] [assembly: AssemblyTitle("TwitchLib.Client.Enums")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/TwitchLib/TwitchLib.Client")] [assembly: NeutralResourcesLanguage("en-US")] [assembly: AssemblyVersion("3.3.1.0")] namespace TwitchLib.Client.Enums { public enum BadgeColor { Red = 10000, Blue = 5000, Green = 1000, Purple = 100, Gray = 1 } public enum ChatColorPresets { Blue, Coral, DodgerBlue, SpringGreen, YellowGreen, Green, OrangeRed, Red, GoldenRod, HotPink, CadetBlue, SeaGreen, Chocolate, BlueViolet, Firebrick } public enum ClientProtocol { TCP, WebSocket } public enum CommercialLength { Seconds30 = 30, Seconds60 = 60, Seconds90 = 90, Seconds120 = 120, Seconds150 = 150, Seconds180 = 180 } public enum Noisy { NotSet, True, False } public enum SendReceiveDirection { Sent, Received } public abstract class StringEnum { public string Value { get; } protected StringEnum(string value) { Value = value; } public override string ToString() { return Value; } } public enum SubscriptionPlan { NotSet, Prime, Tier1, Tier2, Tier3 } public enum ThrottleType { MessageTooShort, MessageTooLong } public enum UserType : byte { Viewer, Moderator, GlobalModerator, Broadcaster, Admin, Staff } } namespace TwitchLib.Client.Enums.Internal { public enum IrcCommand { Unknown, PrivMsg, Notice, Ping, Pong, Join, Part, ClearChat, ClearMsg, UserState, GlobalUserState, Nick, Pass, Cap, RPL_001, RPL_002, RPL_003, RPL_004, RPL_353, RPL_366, RPL_372, RPL_375, RPL_376, Whisper, RoomState, Reconnect, ServerChange, UserNotice, Mode } }
TwitchLib.Client.Models.dll
Decompiled a month ago
The result has been truncated due to the large size, download it to view full contents!
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Drawing; using System.Globalization; using System.Linq; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Text; using System.Text.RegularExpressions; using TwitchLib.Client.Enums; using TwitchLib.Client.Enums.Internal; using TwitchLib.Client.Models.Builders; using TwitchLib.Client.Models.Common; using TwitchLib.Client.Models.Extensions.NetCore; using TwitchLib.Client.Models.Extractors; using TwitchLib.Client.Models.Internal; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")] [assembly: AssemblyCompany("swiftyspiffy, Prom3theu5, Syzuna, LuckyNoS7evin")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright 2021")] [assembly: AssemblyDescription("Project contains all of the models used in TwitchLib.Client.")] [assembly: AssemblyFileVersion("3.3.1")] [assembly: AssemblyInformationalVersion("3.3.1")] [assembly: AssemblyProduct("TwitchLib.Client.Models")] [assembly: AssemblyTitle("TwitchLib.Client.Models")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/TwitchLib/TwitchLib.Client")] [assembly: NeutralResourcesLanguage("en-US")] [assembly: AssemblyVersion("3.3.1.0")] namespace TwitchLib.Client.Models { public class Announcement { public string Id { get; } public List<KeyValuePair<string, string>> Badges { get; } public List<KeyValuePair<string, string>> BadgeInfo { get; } public string SystemMessage { get; } public string SystemMessageParsed { get; } public bool IsBroadcaster { get; } public bool IsModerator { get; } public bool IsPartner { get; } public bool IsSubscriber { get; } public bool IsStaff { get; } public bool IsTurbo { get; } public string Login { get; } public string UserId { get; } public string RoomId { get; } public UserType UserType { get; } public string TmiSentTs { get; } public string EmoteSet { get; } public string RawIrc { get; } public string MsgId { get; } public string MsgParamColor { get; } public string ColorHex { get; } public Color Color { get; } public string Message { get; } public Announcement(IrcMessage ircMessage) { //IL_047c: Unknown result type (might be due to invalid IL or missing references) //IL_0485: Unknown result type (might be due to invalid IL or missing references) //IL_048e: Unknown result type (might be due to invalid IL or missing references) //IL_0497: Unknown result type (might be due to invalid IL or missing references) //IL_04a0: Unknown result type (might be due to invalid IL or missing references) RawIrc = ircMessage.ToString(); Message = ircMessage.Message; foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "badges": { Badges = Helpers.ParseBadges(text); using (List<KeyValuePair<string, string>>.Enumerator enumerator2 = Badges.GetEnumerator()) { while (enumerator2.MoveNext()) { switch (enumerator2.Current.Key) { case "broadcaster": IsBroadcaster = true; break; case "turbo": IsTurbo = true; break; case "moderator": IsModerator = true; break; case "subscriber": IsSubscriber = true; break; case "admin": IsStaff = true; break; case "staff": IsStaff = true; break; case "partner": IsPartner = true; break; } } } break; } case "badge-info": BadgeInfo = Helpers.ParseBadges(text); break; case "color": ColorHex = text; if (!string.IsNullOrEmpty(ColorHex)) { Color = TwitchLib.Client.Models.Extensions.NetCore.ColorTranslator.FromHtml(ColorHex); } break; case "msg-param-color": MsgParamColor = text; break; case "emotes": EmoteSet = text; break; case "id": Id = text; break; case "login": Login = text; break; case "msg-id": MsgId = text; break; case "room-id": RoomId = text; break; case "system-msg": SystemMessage = text; SystemMessageParsed = text.Replace("\\s", " "); break; case "tmi-sent-ts": TmiSentTs = text; break; case "user-id": UserId = text; break; case "user-type": switch (text) { case "mod": UserType = (UserType)1; break; case "global_mod": UserType = (UserType)2; break; case "admin": UserType = (UserType)4; break; case "staff": UserType = (UserType)5; break; default: UserType = (UserType)0; break; } break; } } } } public class ChannelState { public string BroadcasterLanguage { get; } public string Channel { get; } public bool? EmoteOnly { get; } public TimeSpan? FollowersOnly { get; } public bool Mercury { get; } public bool? R9K { get; } public bool? Rituals { get; } public string RoomId { get; } public int? SlowMode { get; } public bool? SubOnly { get; } public ChannelState(IrcMessage ircMessage) { foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "broadcaster-lang": BroadcasterLanguage = text; break; case "emote-only": EmoteOnly = Helpers.ConvertToBool(text); break; case "r9k": R9K = Helpers.ConvertToBool(text); break; case "rituals": Rituals = Helpers.ConvertToBool(text); break; case "slow": { int result2; bool flag = int.TryParse(text, out result2); SlowMode = (flag ? new int?(result2) : null); break; } case "subs-only": SubOnly = Helpers.ConvertToBool(text); break; case "followers-only": { if (int.TryParse(text, out var result) && result > -1) { FollowersOnly = TimeSpan.FromMinutes(result); } break; } case "room-id": RoomId = text; break; case "mercury": Mercury = Helpers.ConvertToBool(text); break; default: Console.WriteLine("[TwitchLib][ChannelState] Unaccounted for: " + key); break; } } Channel = ircMessage.Channel; } public ChannelState(bool r9k, bool rituals, bool subonly, int slowMode, bool emoteOnly, string broadcasterLanguage, string channel, TimeSpan followersOnly, bool mercury, string roomId) { R9K = r9k; Rituals = rituals; SubOnly = subonly; SlowMode = slowMode; EmoteOnly = emoteOnly; BroadcasterLanguage = broadcasterLanguage; Channel = channel; FollowersOnly = followersOnly; Mercury = mercury; RoomId = roomId; } } public class ChatCommand { public List<string> ArgumentsAsList { get; } public string ArgumentsAsString { get; } public ChatMessage ChatMessage { get; } public char CommandIdentifier { get; } public string CommandText { get; } public ChatCommand(ChatMessage chatMessage) { ChatCommand chatCommand = this; ChatMessage = chatMessage; string[] array = chatMessage.Message.Split(new char[1] { ' ' }); CommandText = ((array != null) ? array[0].Substring(1, chatMessage.Message.Split(new char[1] { ' ' })[0].Length - 1) : null) ?? chatMessage.Message.Substring(1, chatMessage.Message.Length - 1); object obj; if (!chatMessage.Message.Contains(" ")) { obj = ""; } else { string message = chatMessage.Message; string[] array2 = chatMessage.Message.Split(new char[1] { ' ' }); obj = message.Replace(((array2 != null) ? array2[0] : null) + " ", ""); } ArgumentsAsString = (string)obj; if (!chatMessage.Message.Contains("\"") || chatMessage.Message.Count((char x) => x == '"') % 2 == 1) { ArgumentsAsList = chatMessage.Message.Split(new char[1] { ' ' })?.Where((string arg) => arg != chatMessage.Message[0] + chatCommand.CommandText).ToList() ?? new List<string>(); } else { ArgumentsAsList = Helpers.ParseQuotesAndNonQuotes(ArgumentsAsString); } CommandIdentifier = chatMessage.Message[0]; } public ChatCommand(ChatMessage chatMessage, string commandText, string argumentsAsString, List<string> argumentsAsList, char commandIdentifier) { ChatMessage = chatMessage; CommandText = commandText; ArgumentsAsString = argumentsAsString; ArgumentsAsList = argumentsAsList; CommandIdentifier = commandIdentifier; } } public class ChatMessage : TwitchLibMessage { protected readonly MessageEmoteCollection _emoteCollection; public List<KeyValuePair<string, string>> BadgeInfo { get; } public int Bits { get; } public double BitsInDollars { get; } public string Channel { get; } public CheerBadge CheerBadge { get; } public string CustomRewardId { get; } public string EmoteReplacedMessage { get; } public string Id { get; } public bool IsBroadcaster { get; } public bool IsFirstMessage { get; } public bool IsHighlighted { get; internal set; } public bool IsMe { get; } public bool IsModerator { get; } public bool IsSkippingSubMode { get; internal set; } public bool IsSubscriber { get; } public bool IsVip { get; } public bool IsStaff { get; } public bool IsPartner { get; } public string Message { get; } public Noisy Noisy { get; } public string RoomId { get; } public int SubscribedMonthCount { get; } public string TmiSentTs { get; } public ChatReply ChatReply { get; } public ChatMessage(string botUsername, IrcMessage ircMessage, ref MessageEmoteCollection emoteCollection, bool replaceEmotes = false) { //IL_06f9: Unknown result type (might be due to invalid IL or missing references) base.BotUsername = botUsername; base.RawIrcMessage = ircMessage.ToString(); Message = ircMessage.Message; if (Message.Length > 0 && (byte)Message[0] == 1 && (byte)Message[Message.Length - 1] == 1 && Message.StartsWith("\u0001ACTION ") && Message.EndsWith("\u0001")) { Message = Message.Trim(new char[1] { '\u0001' }).Substring(7); IsMe = true; } _emoteCollection = emoteCollection; base.Username = ircMessage.User; Channel = ircMessage.Channel; foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "badges": base.Badges = Helpers.ParseBadges(text); foreach (KeyValuePair<string, string> badge in base.Badges) { switch (badge.Key) { case "bits": CheerBadge = new CheerBadge(int.Parse(badge.Value)); break; case "subscriber": if (SubscribedMonthCount == 0) { SubscribedMonthCount = int.Parse(badge.Value); } break; case "vip": IsVip = true; break; case "admin": IsStaff = true; break; case "staff": IsStaff = true; break; case "partner": IsPartner = true; break; } } break; case "badge-info": { BadgeInfo = Helpers.ParseBadges(text); KeyValuePair<string, string> keyValuePair = BadgeInfo.Find((KeyValuePair<string, string> b) => b.Key == "founder"); if (!keyValuePair.Equals(default(KeyValuePair<string, string>))) { IsSubscriber = true; SubscribedMonthCount = int.Parse(keyValuePair.Value); break; } KeyValuePair<string, string> keyValuePair2 = BadgeInfo.Find((KeyValuePair<string, string> b) => b.Key == "subscriber"); if (!keyValuePair2.Equals(default(KeyValuePair<string, string>))) { SubscribedMonthCount = int.Parse(keyValuePair2.Value); } break; } case "bits": Bits = int.Parse(text); BitsInDollars = ConvertBitsToUsd(Bits); break; case "color": base.ColorHex = text; if (!string.IsNullOrWhiteSpace(base.ColorHex)) { base.Color = TwitchLib.Client.Models.Extensions.NetCore.ColorTranslator.FromHtml(base.ColorHex); } break; case "custom-reward-id": CustomRewardId = text; break; case "display-name": base.DisplayName = text; break; case "emotes": base.EmoteSet = new EmoteSet(text, Message); break; case "first-msg": IsFirstMessage = text == "1"; break; case "id": Id = text; break; case "msg-id": handleMsgId(text); break; case "mod": IsModerator = Helpers.ConvertToBool(text); break; case "noisy": Noisy = (Noisy)(Helpers.ConvertToBool(text) ? 1 : 2); break; case "reply-parent-display-name": if (ChatReply == null) { ChatReply = new ChatReply(); } ChatReply.ParentDisplayName = text; break; case "reply-parent-msg-body": if (ChatReply == null) { ChatReply = new ChatReply(); } ChatReply.ParentMsgBody = text; break; case "reply-parent-msg-id": if (ChatReply == null) { ChatReply = new ChatReply(); } ChatReply.ParentMsgId = text; break; case "reply-parent-user-id": if (ChatReply == null) { ChatReply = new ChatReply(); } ChatReply.ParentUserId = text; break; case "reply-parent-user-login": if (ChatReply == null) { ChatReply = new ChatReply(); } ChatReply.ParentUserLogin = text; break; case "room-id": RoomId = text; break; case "subscriber": IsSubscriber = IsSubscriber || Helpers.ConvertToBool(text); break; case "tmi-sent-ts": TmiSentTs = text; break; case "turbo": base.IsTurbo = Helpers.ConvertToBool(text); break; case "user-id": base.UserId = text; break; case "user-type": switch (text) { case "mod": base.UserType = (UserType)1; break; case "global_mod": base.UserType = (UserType)2; break; case "admin": base.UserType = (UserType)4; IsStaff = true; break; case "staff": base.UserType = (UserType)5; IsStaff = true; break; default: base.UserType = (UserType)0; break; } break; } } if (base.EmoteSet != null && Message != null && base.EmoteSet.Emotes.Count > 0) { string[] array = base.EmoteSet.RawEmoteSetString.Split(new char[1] { '/' }); foreach (string text2 in array) { int num = text2.IndexOf(':'); int num2 = text2.IndexOf(','); if (num2 == -1) { num2 = text2.Length; } int num3 = text2.IndexOf('-'); if (num > 0 && num3 > num && num2 > num3 && int.TryParse(text2.Substring(num + 1, num3 - num - 1), out var result) && int.TryParse(text2.Substring(num3 + 1, num2 - num3 - 1), out var result2) && result >= 0 && result < result2 && result2 < Message.Length) { string id = text2.Substring(0, num); string text3 = Message.Substring(result, result2 - result + 1); _emoteCollection.Add(new MessageEmote(id, text3)); } } if (replaceEmotes) { EmoteReplacedMessage = _emoteCollection.ReplaceEmotes(Message); } } if (base.EmoteSet == null) { base.EmoteSet = new EmoteSet((string)null, Message); } if (string.IsNullOrEmpty(base.DisplayName)) { base.DisplayName = base.Username; } if (string.Equals(Channel, base.Username, StringComparison.InvariantCultureIgnoreCase)) { base.UserType = (UserType)3; IsBroadcaster = true; } if (Channel.Split(new char[1] { ':' }).Length == 3 && string.Equals(Channel.Split(new char[1] { ':' })[1], base.UserId, StringComparison.InvariantCultureIgnoreCase)) { base.UserType = (UserType)3; IsBroadcaster = true; } } public ChatMessage(string botUsername, string userId, string userName, string displayName, string colorHex, Color color, EmoteSet emoteSet, string message, UserType userType, string channel, string id, bool isSubscriber, int subscribedMonthCount, string roomId, bool isTurbo, bool isModerator, bool isMe, bool isBroadcaster, bool isVip, bool isPartner, bool isStaff, Noisy noisy, string rawIrcMessage, string emoteReplacedMessage, List<KeyValuePair<string, string>> badges, CheerBadge cheerBadge, int bits, double bitsInDollars) { //IL_003d: Unknown result type (might be due to invalid IL or missing references) //IL_00a5: Unknown result type (might be due to invalid IL or missing references) //IL_00a7: Unknown result type (might be due to invalid IL or missing references) base.BotUsername = botUsername; base.UserId = userId; base.DisplayName = displayName; base.ColorHex = colorHex; base.Color = color; base.EmoteSet = emoteSet; Message = message; base.UserType = userType; Channel = channel; Id = id; IsSubscriber = isSubscriber; SubscribedMonthCount = subscribedMonthCount; RoomId = roomId; base.IsTurbo = isTurbo; IsModerator = isModerator; IsMe = isMe; IsBroadcaster = isBroadcaster; IsVip = isVip; IsPartner = isPartner; IsStaff = isStaff; Noisy = noisy; base.RawIrcMessage = rawIrcMessage; EmoteReplacedMessage = emoteReplacedMessage; base.Badges = badges; CheerBadge = cheerBadge; Bits = bits; BitsInDollars = bitsInDollars; base.Username = userName; } private void handleMsgId(string val) { if (!(val == "highlighted-message")) { if (val == "skip-subs-mode-message") { IsSkippingSubMode = true; } } else { IsHighlighted = true; } } private static double ConvertBitsToUsd(int bits) { if (bits < 1500) { return (double)bits / 100.0 * 1.4; } if (bits < 5000) { return (double)bits / 1500.0 * 19.95; } if (bits < 10000) { return (double)bits / 5000.0 * 64.4; } if (bits < 25000) { return (double)bits / 10000.0 * 126.0; } return (double)bits / 25000.0 * 308.0; } } public class ChatReply { public string ParentDisplayName { get; internal set; } public string ParentMsgBody { get; internal set; } public string ParentMsgId { get; internal set; } public string ParentUserId { get; internal set; } public string ParentUserLogin { get; internal set; } } public class CheerBadge { public int CheerAmount { get; } public BadgeColor Color { get; } public CheerBadge(int cheerAmount) { //IL_0010: Unknown result type (might be due to invalid IL or missing references) //IL_0015: Unknown result type (might be due to invalid IL or missing references) CheerAmount = cheerAmount; Color = GetColor(cheerAmount); } private BadgeColor GetColor(int cheerAmount) { if (cheerAmount < 10000) { if (cheerAmount < 5000) { if (cheerAmount < 1000) { if (cheerAmount >= 100) { return (BadgeColor)100; } return (BadgeColor)1; } return (BadgeColor)1000; } return (BadgeColor)5000; } return (BadgeColor)10000; } } public class CommunitySubscription { private const string AnonymousGifterUserId = "274598607"; public List<KeyValuePair<string, string>> Badges; public List<KeyValuePair<string, string>> BadgeInfo; public string Color; public string DisplayName; public string Emotes; public string Id; public string Login; public bool IsModerator; public bool IsAnonymous; public string MsgId; public int MsgParamMassGiftCount; public int MsgParamSenderCount; public SubscriptionPlan MsgParamSubPlan; public string RoomId; public bool IsSubscriber; public string SystemMsg; public string SystemMsgParsed; public string TmiSentTs; public bool IsTurbo; public string UserId; public UserType UserType; public string MsgParamMultiMonthGiftDuration; public CommunitySubscription(IrcMessage ircMessage) { //IL_03cb: Unknown result type (might be due to invalid IL or missing references) //IL_03d7: Unknown result type (might be due to invalid IL or missing references) //IL_04eb: Unknown result type (might be due to invalid IL or missing references) //IL_03e3: Unknown result type (might be due to invalid IL or missing references) //IL_04f4: Unknown result type (might be due to invalid IL or missing references) //IL_03ef: Unknown result type (might be due to invalid IL or missing references) //IL_04fd: Unknown result type (might be due to invalid IL or missing references) //IL_0506: Unknown result type (might be due to invalid IL or missing references) //IL_050f: Unknown result type (might be due to invalid IL or missing references) foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "badges": Badges = Helpers.ParseBadges(text); break; case "badge-info": BadgeInfo = Helpers.ParseBadges(text); break; case "color": Color = text; break; case "display-name": DisplayName = text; break; case "emotes": Emotes = text; break; case "id": Id = text; break; case "login": Login = text; break; case "mod": IsModerator = Helpers.ConvertToBool(text); break; case "msg-id": MsgId = text; break; case "msg-param-sub-plan": switch (text) { case "prime": MsgParamSubPlan = (SubscriptionPlan)1; break; case "1000": MsgParamSubPlan = (SubscriptionPlan)2; break; case "2000": MsgParamSubPlan = (SubscriptionPlan)3; break; case "3000": MsgParamSubPlan = (SubscriptionPlan)4; break; default: throw new ArgumentOutOfRangeException("ToLower"); } break; case "msg-param-mass-gift-count": MsgParamMassGiftCount = int.Parse(text); break; case "msg-param-sender-count": MsgParamSenderCount = int.Parse(text); break; case "room-id": RoomId = text; break; case "subscriber": IsSubscriber = Helpers.ConvertToBool(text); break; case "system-msg": SystemMsg = text; SystemMsgParsed = text.Replace("\\s", " ").Replace("\\n", ""); break; case "tmi-sent-ts": TmiSentTs = text; break; case "turbo": IsTurbo = Helpers.ConvertToBool(text); break; case "user-id": UserId = text; if (UserId == "274598607") { IsAnonymous = true; } break; case "user-type": switch (text) { case "mod": UserType = (UserType)1; break; case "global_mod": UserType = (UserType)2; break; case "admin": UserType = (UserType)4; break; case "staff": UserType = (UserType)5; break; default: UserType = (UserType)0; break; } break; case "msg-param-gift-months": MsgParamMultiMonthGiftDuration = text; break; } } } } public class ConnectionCredentials { public const string DefaultWebSocketUri = "wss://irc-ws.chat.twitch.tv:443"; public string TwitchWebsocketURI { get; } public string TwitchOAuth { get; } public string TwitchUsername { get; } public Capabilities Capabilities { get; } public ConnectionCredentials(string twitchUsername, string twitchOAuth, string twitchWebsocketURI = "wss://irc-ws.chat.twitch.tv:443", bool disableUsernameCheck = false, Capabilities capabilities = null) { if (!disableUsernameCheck && !new Regex("^([a-zA-Z0-9][a-zA-Z0-9_]{3,25})$").Match(twitchUsername).Success) { throw new Exception("Twitch username does not appear to be valid. " + twitchUsername); } TwitchUsername = twitchUsername.ToLower(); TwitchOAuth = twitchOAuth; if (!twitchOAuth.Contains(":")) { TwitchOAuth = "oauth:" + twitchOAuth.Replace("oauth", ""); } TwitchWebsocketURI = twitchWebsocketURI; if (capabilities == null) { capabilities = new Capabilities(); } Capabilities = capabilities; } } public class Capabilities { public bool Membership { get; } public bool Tags { get; } public bool Commands { get; } public Capabilities(bool membership = true, bool tags = true, bool commands = true) { Membership = membership; Tags = tags; Commands = commands; } } public class ContinuedGiftedSubscription { public List<KeyValuePair<string, string>> Badges { get; } public List<KeyValuePair<string, string>> BadgeInfo { get; } public string Color { get; } public string DisplayName { get; } public string Emotes { get; } public string Flags { get; } public string Id { get; } public string Login { get; } public bool IsModerator { get; } public string MsgId { get; } public string MsgParamSenderLogin { get; } public string MsgParamSenderName { get; } public string RoomId { get; } public bool IsSubscriber { get; } public string SystemMsg { get; } public string TmiSentTs { get; } public string UserId { get; } public UserType UserType { get; } public ContinuedGiftedSubscription(IrcMessage ircMessage) { //IL_03e7: Unknown result type (might be due to invalid IL or missing references) //IL_03f0: Unknown result type (might be due to invalid IL or missing references) //IL_03f9: Unknown result type (might be due to invalid IL or missing references) //IL_0402: Unknown result type (might be due to invalid IL or missing references) //IL_040b: Unknown result type (might be due to invalid IL or missing references) foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "system-msg": SystemMsg = text; break; case "flags": Flags = text; break; case "msg-param-sender-login": MsgParamSenderLogin = text; break; case "msg-param-sender-name": MsgParamSenderName = text; break; case "badges": Badges = Helpers.ParseBadges(text); break; case "badge-info": BadgeInfo = Helpers.ParseBadges(text); break; case "color": Color = text; break; case "display-name": DisplayName = text; break; case "emotes": Emotes = text; break; case "id": Id = text; break; case "login": Login = text; break; case "mod": IsModerator = Helpers.ConvertToBool(text); break; case "msg-id": MsgId = text; break; case "room-id": RoomId = text; break; case "subscriber": IsSubscriber = Helpers.ConvertToBool(text); break; case "tmi-sent-ts": TmiSentTs = text; break; case "user-id": UserId = text; break; case "user-type": switch (text) { case "mod": UserType = (UserType)1; break; case "global_mod": UserType = (UserType)2; break; case "admin": UserType = (UserType)4; break; case "staff": UserType = (UserType)5; break; default: UserType = (UserType)0; break; } break; } } } } public class Emote { public string Id { get; } public string Name { get; } public int StartIndex { get; } public int EndIndex { get; } public string ImageUrl { get; } public Emote(string emoteId, string name, int emoteStartIndex, int emoteEndIndex) { Id = emoteId; Name = name; StartIndex = emoteStartIndex; EndIndex = emoteEndIndex; ImageUrl = "https://static-cdn.jtvnw.net/emoticons/v1/" + emoteId + "/1.0"; } } public class EmoteSet { public List<Emote> Emotes { get; } public string RawEmoteSetString { get; } public EmoteSet(string rawEmoteSetString, string message) { RawEmoteSetString = rawEmoteSetString; EmoteExtractor emoteExtractor = new EmoteExtractor(); Emotes = emoteExtractor.Extract(rawEmoteSetString, message).ToList(); } public EmoteSet(IEnumerable<Emote> emotes, string emoteSetData) { RawEmoteSetString = emoteSetData; Emotes = emotes.ToList(); } } public class ErrorEvent { public string Message { get; set; } } public class GiftedSubscription { private const string AnonymousGifterUserId = "274598607"; public List<KeyValuePair<string, string>> Badges { get; } public List<KeyValuePair<string, string>> BadgeInfo { get; } public string Color { get; } public string DisplayName { get; } public string Emotes { get; } public string Id { get; } public bool IsModerator { get; } public bool IsSubscriber { get; } public bool IsTurbo { get; } public bool IsAnonymous { get; } public string Login { get; } public string MsgId { get; } public string MsgParamMonths { get; } public string MsgParamRecipientDisplayName { get; } public string MsgParamRecipientId { get; } public string MsgParamRecipientUserName { get; } public string MsgParamSubPlanName { get; } public SubscriptionPlan MsgParamSubPlan { get; } public string RoomId { get; } public string SystemMsg { get; } public string SystemMsgParsed { get; } public string TmiSentTs { get; } public string UserId { get; } public UserType UserType { get; } public string MsgParamMultiMonthGiftDuration { get; } public GiftedSubscription(IrcMessage ircMessage) { //IL_0467: Unknown result type (might be due to invalid IL or missing references) //IL_0473: Unknown result type (might be due to invalid IL or missing references) //IL_047f: Unknown result type (might be due to invalid IL or missing references) //IL_0565: Unknown result type (might be due to invalid IL or missing references) //IL_048b: Unknown result type (might be due to invalid IL or missing references) //IL_056e: Unknown result type (might be due to invalid IL or missing references) //IL_0577: Unknown result type (might be due to invalid IL or missing references) //IL_0580: Unknown result type (might be due to invalid IL or missing references) //IL_0589: Unknown result type (might be due to invalid IL or missing references) foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "badges": Badges = Helpers.ParseBadges(text); break; case "badge-info": BadgeInfo = Helpers.ParseBadges(text); break; case "color": Color = text; break; case "display-name": DisplayName = text; break; case "emotes": Emotes = text; break; case "id": Id = text; break; case "login": Login = text; break; case "mod": IsModerator = Helpers.ConvertToBool(text); break; case "msg-id": MsgId = text; break; case "msg-param-months": MsgParamMonths = text; break; case "msg-param-recipient-display-name": MsgParamRecipientDisplayName = text; break; case "msg-param-recipient-id": MsgParamRecipientId = text; break; case "msg-param-recipient-user-name": MsgParamRecipientUserName = text; break; case "msg-param-sub-plan-name": MsgParamSubPlanName = text; break; case "msg-param-sub-plan": switch (text) { case "prime": MsgParamSubPlan = (SubscriptionPlan)1; break; case "1000": MsgParamSubPlan = (SubscriptionPlan)2; break; case "2000": MsgParamSubPlan = (SubscriptionPlan)3; break; case "3000": MsgParamSubPlan = (SubscriptionPlan)4; break; default: throw new ArgumentOutOfRangeException("ToLower"); } break; case "room-id": RoomId = text; break; case "subscriber": IsSubscriber = Helpers.ConvertToBool(text); break; case "system-msg": SystemMsg = text; SystemMsgParsed = text.Replace("\\s", " ").Replace("\\n", ""); break; case "tmi-sent-ts": TmiSentTs = text; break; case "turbo": IsTurbo = Helpers.ConvertToBool(text); break; case "user-id": UserId = text; if (UserId == "274598607") { IsAnonymous = true; } break; case "user-type": switch (text) { case "mod": UserType = (UserType)1; break; case "global_mod": UserType = (UserType)2; break; case "admin": UserType = (UserType)4; break; case "staff": UserType = (UserType)5; break; default: UserType = (UserType)0; break; } break; case "msg-param-gift-months": MsgParamMultiMonthGiftDuration = text; break; } } } public GiftedSubscription(List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string color, string displayName, string emotes, string id, string login, bool isModerator, string msgId, string msgParamMonths, string msgParamRecipientDisplayName, string msgParamRecipientId, string msgParamRecipientUserName, string msgParamSubPlanName, string msgMultiMonthDuration, SubscriptionPlan msgParamSubPlan, string roomId, bool isSubscriber, string systemMsg, string systemMsgParsed, string tmiSentTs, bool isTurbo, UserType userType, string userId) { //IL_0074: Unknown result type (might be due to invalid IL or missing references) //IL_0076: Unknown result type (might be due to invalid IL or missing references) //IL_00b4: Unknown result type (might be due to invalid IL or missing references) //IL_00b6: Unknown result type (might be due to invalid IL or missing references) Badges = badges; BadgeInfo = badgeInfo; Color = color; DisplayName = displayName; Emotes = emotes; Id = id; Login = login; IsModerator = isModerator; MsgId = msgId; MsgParamMonths = msgParamMonths; MsgParamRecipientDisplayName = msgParamRecipientDisplayName; MsgParamRecipientId = msgParamRecipientId; MsgParamRecipientUserName = msgParamRecipientUserName; MsgParamSubPlanName = msgParamSubPlanName; MsgParamSubPlan = msgParamSubPlan; MsgParamMultiMonthGiftDuration = msgMultiMonthDuration; RoomId = roomId; IsSubscriber = isSubscriber; SystemMsg = systemMsg; SystemMsgParsed = systemMsgParsed; TmiSentTs = tmiSentTs; IsTurbo = isTurbo; UserType = userType; UserId = userId; } } public class JoinedChannel { public string Channel { get; } public ChannelState ChannelState { get; protected set; } public ChatMessage PreviousMessage { get; protected set; } public JoinedChannel(string channel) { Channel = channel; } public void HandleMessage(ChatMessage message) { PreviousMessage = message; } } public class MessageEmote { public delegate string ReplaceEmoteDelegate(MessageEmote caller); public enum EmoteSource { Twitch, FrankerFaceZ, BetterTwitchTv } public enum EmoteSize { Small, Medium, Large } public static readonly ReadOnlyCollection<string> TwitchEmoteUrls = new ReadOnlyCollection<string>(new string[3] { "https://static-cdn.jtvnw.net/emoticons/v1/{0}/1.0", "https://static-cdn.jtvnw.net/emoticons/v1/{0}/2.0", "https://static-cdn.jtvnw.net/emoticons/v1/{0}/3.0" }); public static readonly ReadOnlyCollection<string> FrankerFaceZEmoteUrls = new ReadOnlyCollection<string>(new string[3] { "//cdn.frankerfacez.com/emoticon/{0}/1", "//cdn.frankerfacez.com/emoticon/{0}/2", "//cdn.frankerfacez.com/emoticon/{0}/4" }); public static readonly ReadOnlyCollection<string> BetterTwitchTvEmoteUrls = new ReadOnlyCollection<string>(new string[3] { "//cdn.betterttv.net/emote/{0}/1x", "//cdn.betterttv.net/emote/{0}/2x", "//cdn.betterttv.net/emote/{0}/3x" }); private readonly string _id; private readonly string _text; private readonly string _escapedText; private readonly EmoteSource _source; private readonly EmoteSize _size; public string Id => _id; public string Text => _text; public EmoteSource Source => _source; public EmoteSize Size => _size; public string ReplacementString => ReplacementDelegate(this); public static ReplaceEmoteDelegate ReplacementDelegate { get; set; } = SourceMatchingReplacementText; public string EscapedText => _escapedText; public static string SourceMatchingReplacementText(MessageEmote caller) { int size = (int)caller.Size; return caller.Source switch { EmoteSource.BetterTwitchTv => string.Format(BetterTwitchTvEmoteUrls[size], caller.Id), EmoteSource.FrankerFaceZ => string.Format(FrankerFaceZEmoteUrls[size], caller.Id), EmoteSource.Twitch => string.Format(TwitchEmoteUrls[size], caller.Id), _ => caller.Text, }; } public MessageEmote(string id, string text, EmoteSource source = EmoteSource.Twitch, EmoteSize size = EmoteSize.Small, ReplaceEmoteDelegate replacementDelegate = null) { _id = id; _text = text; _escapedText = Regex.Escape(text); _source = source; _size = size; if (replacementDelegate != null) { ReplacementDelegate = replacementDelegate; } } } public class MessageEmoteCollection { public delegate bool EmoteFilterDelegate(MessageEmote emote); private readonly SortedList<string, MessageEmote> _emoteList; private const string BasePattern = "(\\b{0}\\b)"; private string _currentPattern; private Regex _regex; private readonly EmoteFilterDelegate _preferredFilter; private string CurrentPattern { get { return _currentPattern; } set { if (_currentPattern == null || !_currentPattern.Equals(value)) { _currentPattern = value; PatternChanged = true; } } } private Regex CurrentRegex { get { if (PatternChanged) { if (CurrentPattern != null) { _regex = new Regex(string.Format(CurrentPattern, "")); PatternChanged = false; } else { _regex = null; } } return _regex; } } private bool PatternChanged { get; set; } private EmoteFilterDelegate CurrentEmoteFilter { get; set; } = AllInclusiveEmoteFilter; public MessageEmoteCollection() { _emoteList = new SortedList<string, MessageEmote>(); _preferredFilter = AllInclusiveEmoteFilter; } public MessageEmoteCollection(EmoteFilterDelegate preferredFilter) : this() { _preferredFilter = preferredFilter; } public void Add(MessageEmote emote) { if (!_emoteList.TryGetValue(emote.Text, out var _)) { _emoteList.Add(emote.Text, emote); } if (CurrentPattern == null) { CurrentPattern = $"(\\b{emote.EscapedText}\\b)"; } else { CurrentPattern = CurrentPattern + "|" + $"(\\b{emote.EscapedText}\\b)"; } } public void Merge(IEnumerable<MessageEmote> emotes) { IEnumerator<MessageEmote> enumerator = emotes.GetEnumerator(); while (enumerator.MoveNext()) { Add(enumerator.Current); } enumerator.Dispose(); } public void Remove(MessageEmote emote) { if (_emoteList.ContainsKey(emote.Text)) { _emoteList.Remove(emote.Text); string text = "(^\\(\\\\b" + emote.EscapedText + "\\\\b\\)\\|?)"; string text2 = "(\\|\\(\\\\b" + emote.EscapedText + "\\\\b\\))"; string text3 = Regex.Replace(CurrentPattern, text + "|" + text2, ""); CurrentPattern = (text3.Equals("") ? null : text3); } } public void RemoveAll() { _emoteList.Clear(); CurrentPattern = null; } public string ReplaceEmotes(string originalMessage, EmoteFilterDelegate del = null) { if (CurrentRegex == null) { return originalMessage; } if (del != null && del != CurrentEmoteFilter) { CurrentEmoteFilter = del; } string result = CurrentRegex.Replace(originalMessage, GetReplacementString); CurrentEmoteFilter = _preferredFilter; return result; } public static bool AllInclusiveEmoteFilter(MessageEmote emote) { return true; } public static bool TwitchOnlyEmoteFilter(MessageEmote emote) { return emote.Source == MessageEmote.EmoteSource.Twitch; } private string GetReplacementString(Match m) { if (!_emoteList.ContainsKey(m.Value)) { return m.Value; } MessageEmote messageEmote = _emoteList[m.Value]; if (!CurrentEmoteFilter(messageEmote)) { return m.Value; } return messageEmote.ReplacementString; } } public class OutboundChatMessage { public string Channel { get; set; } public string Message { get; set; } public string Username { get; set; } public string ReplyToId { get; set; } public override string ToString() { string text = Username.ToLower(); string text2 = Channel.ToLower(); if (ReplyToId == null) { return ":" + text + "!" + text + "@" + text + ".tmi.twitch.tv PRIVMSG #" + text2 + " :" + Message; } return "@reply-parent-msg-id=" + ReplyToId + " PRIVMSG #" + text2 + " :" + Message; } } public class OutboundWhisperMessage { public string Username { get; set; } public string Receiver { get; set; } public string Message { get; set; } public override string ToString() { return ":" + Username + "~" + Username + "@" + Username + ".tmi.twitch.tv PRIVMSG #jtv :/w " + Receiver + " " + Message; } } public class OutgoingMessage { public string Channel { get; set; } public string Message { get; set; } public int Nonce { get; set; } public string Sender { get; set; } public MessageState State { get; set; } } public enum MessageState : byte { Normal, Queued, Failed } public class PrimePaidSubscriber : SubscriberBase { public PrimePaidSubscriber(IrcMessage ircMessage) : base(ircMessage) { } public PrimePaidSubscriber(List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string colorHex, Color color, string displayName, string emoteSet, string id, string login, string systemMessage, string msgId, string msgParamCumulativeMonths, string msgParamStreakMonths, bool msgParamShouldShareStreak, string systemMessageParsed, string resubMessage, SubscriptionPlan subscriptionPlan, string subscriptionPlanName, string roomId, string userId, bool isModerator, bool isTurbo, bool isSubscriber, bool isPartner, string tmiSentTs, UserType userType, string rawIrc, string channel, int months = 0) : base(badges, badgeInfo, colorHex, color, displayName, emoteSet, id, login, systemMessage, msgId, msgParamCumulativeMonths, msgParamStreakMonths, msgParamShouldShareStreak, systemMessageParsed, resubMessage, subscriptionPlan, subscriptionPlanName, roomId, userId, isModerator, isTurbo, isSubscriber, isPartner, tmiSentTs, userType, rawIrc, channel, months) { }//IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) } public class RaidNotification { public List<KeyValuePair<string, string>> Badges { get; } public List<KeyValuePair<string, string>> BadgeInfo { get; } public string Color { get; } public string DisplayName { get; } public string Emotes { get; } public string Id { get; } public string Login { get; } public bool Moderator { get; } public string MsgId { get; } public string MsgParamDisplayName { get; } public string MsgParamLogin { get; } public string MsgParamViewerCount { get; } public string RoomId { get; } public bool Subscriber { get; } public string SystemMsg { get; } public string SystemMsgParsed { get; } public string TmiSentTs { get; } public bool Turbo { get; } public string UserId { get; } public UserType UserType { get; } public RaidNotification(IrcMessage ircMessage) { //IL_0411: Unknown result type (might be due to invalid IL or missing references) //IL_041a: Unknown result type (might be due to invalid IL or missing references) //IL_0423: Unknown result type (might be due to invalid IL or missing references) //IL_042c: Unknown result type (might be due to invalid IL or missing references) //IL_0435: Unknown result type (might be due to invalid IL or missing references) foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "badges": Badges = Helpers.ParseBadges(text); break; case "badge-info": BadgeInfo = Helpers.ParseBadges(text); break; case "color": Color = text; break; case "display-name": DisplayName = text; break; case "emotes": Emotes = text; break; case "login": Login = text; break; case "mod": Moderator = Helpers.ConvertToBool(text); break; case "msg-id": MsgId = text; break; case "msg-param-displayName": MsgParamDisplayName = text; break; case "msg-param-login": MsgParamLogin = text; break; case "msg-param-viewerCount": MsgParamViewerCount = text; break; case "room-id": RoomId = text; break; case "subscriber": Subscriber = Helpers.ConvertToBool(text); break; case "system-msg": SystemMsg = text; SystemMsgParsed = text.Replace("\\s", " ").Replace("\\n", ""); break; case "tmi-sent-ts": TmiSentTs = text; break; case "turbo": Turbo = Helpers.ConvertToBool(text); break; case "user-id": UserId = text; break; case "user-type": switch (text) { case "mod": UserType = (UserType)1; break; case "global_mod": UserType = (UserType)2; break; case "admin": UserType = (UserType)4; break; case "staff": UserType = (UserType)5; break; default: UserType = (UserType)0; break; } break; } } } public RaidNotification(List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string color, string displayName, string emotes, string id, string login, bool moderator, string msgId, string msgParamDisplayName, string msgParamLogin, string msgParamViewerCount, string roomId, bool subscriber, string systemMsg, string systemMsgParsed, string tmiSentTs, bool turbo, UserType userType, string userId) { //IL_0094: Unknown result type (might be due to invalid IL or missing references) //IL_0096: Unknown result type (might be due to invalid IL or missing references) Badges = badges; BadgeInfo = badgeInfo; Color = color; DisplayName = displayName; Emotes = emotes; Id = id; Login = login; Moderator = moderator; MsgId = msgId; MsgParamDisplayName = msgParamDisplayName; MsgParamLogin = msgParamLogin; MsgParamViewerCount = msgParamViewerCount; RoomId = roomId; Subscriber = subscriber; SystemMsg = systemMsg; SystemMsgParsed = systemMsgParsed; TmiSentTs = tmiSentTs; Turbo = turbo; UserType = userType; UserId = userId; } } public class ReSubscriber : SubscriberBase { public int Months => monthsInternal; public ReSubscriber(IrcMessage ircMessage) : base(ircMessage) { } public ReSubscriber(List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string colorHex, Color color, string displayName, string emoteSet, string id, string login, string systemMessage, string msgId, string msgParamCumulativeMonths, string msgParamStreakMonths, bool msgParamShouldShareStreak, string systemMessageParsed, string resubMessage, SubscriptionPlan subscriptionPlan, string subscriptionPlanName, string roomId, string userId, bool isModerator, bool isTurbo, bool isSubscriber, bool isPartner, string tmiSentTs, UserType userType, string rawIrc, string channel, int months = 0) : base(badges, badgeInfo, colorHex, color, displayName, emoteSet, id, login, systemMessage, msgId, msgParamCumulativeMonths, msgParamStreakMonths, msgParamShouldShareStreak, systemMessageParsed, resubMessage, subscriptionPlan, subscriptionPlanName, roomId, userId, isModerator, isTurbo, isSubscriber, isPartner, tmiSentTs, userType, rawIrc, channel, months) { }//IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) } public class RitualNewChatter { public List<KeyValuePair<string, string>> Badges { get; } public List<KeyValuePair<string, string>> BadgeInfo { get; } public string Color { get; } public string DisplayName { get; } public string Emotes { get; } public string Id { get; } public bool IsModerator { get; } public bool IsSubscriber { get; } public bool IsTurbo { get; } public string Login { get; } public string Message { get; } public string MsgId { get; } public string MsgParamRitualName { get; } public string RoomId { get; } public string SystemMsgParsed { get; } public string SystemMsg { get; } public string TmiSentTs { get; } public string UserId { get; } public UserType UserType { get; } public RitualNewChatter(IrcMessage ircMessage) { //IL_03f1: Unknown result type (might be due to invalid IL or missing references) //IL_03fa: Unknown result type (might be due to invalid IL or missing references) //IL_0403: Unknown result type (might be due to invalid IL or missing references) //IL_040c: Unknown result type (might be due to invalid IL or missing references) //IL_0415: Unknown result type (might be due to invalid IL or missing references) Message = ircMessage.Message; foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "badges": Badges = Helpers.ParseBadges(text); break; case "badge-info": BadgeInfo = Helpers.ParseBadges(text); break; case "color": Color = text; break; case "display-name": DisplayName = text; break; case "emotes": Emotes = text; break; case "id": Id = text; break; case "login": Login = text; break; case "mod": IsModerator = Helpers.ConvertToBool(text); break; case "msg-id": MsgId = text; break; case "msg-param-ritual-name": MsgParamRitualName = text; break; case "room-id": RoomId = text; break; case "subscriber": IsSubscriber = Helpers.ConvertToBool(text); break; case "system-msg": SystemMsg = text; SystemMsgParsed = text.Replace("\\s", " ").Replace("\\n", ""); break; case "tmi-sent-ts": TmiSentTs = text; break; case "turbo": IsTurbo = Helpers.ConvertToBool(text); break; case "user-id": UserId = text; break; case "user-type": switch (text) { case "mod": UserType = (UserType)1; break; case "global_mod": UserType = (UserType)2; break; case "admin": UserType = (UserType)4; break; case "staff": UserType = (UserType)5; break; default: UserType = (UserType)0; break; } break; } } } } public class SentMessage { public List<KeyValuePair<string, string>> Badges { get; } public string Channel { get; } public string ColorHex { get; } public string DisplayName { get; } public string EmoteSet { get; } public bool IsModerator { get; } public bool IsSubscriber { get; } public string Message { get; } public UserType UserType { get; } public SentMessage(UserState state, string message) { //IL_005c: Unknown result type (might be due to invalid IL or missing references) //IL_0061: Unknown result type (might be due to invalid IL or missing references) Badges = state.Badges; Channel = state.Channel; ColorHex = state.ColorHex; DisplayName = state.DisplayName; EmoteSet = state.EmoteSet; IsModerator = state.IsModerator; IsSubscriber = state.IsSubscriber; UserType = state.UserType; Message = message; } public SentMessage(List<KeyValuePair<string, string>> badges, string channel, string colorHex, string displayName, string emoteSet, bool isModerator, bool isSubscriber, UserType userType, string message) { //IL_003c: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) Badges = badges; Channel = channel; ColorHex = colorHex; DisplayName = displayName; EmoteSet = emoteSet; IsModerator = isModerator; IsSubscriber = isSubscriber; UserType = userType; Message = message; } } public class Subscriber : SubscriberBase { public Subscriber(IrcMessage ircMessage) : base(ircMessage) { } public Subscriber(List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string colorHex, Color color, string displayName, string emoteSet, string id, string login, string systemMessage, string msgId, string msgParamCumulativeMonths, string msgParamStreakMonths, bool msgParamShouldShareStreak, string systemMessageParsed, string resubMessage, SubscriptionPlan subscriptionPlan, string subscriptionPlanName, string roomId, string userId, bool isModerator, bool isTurbo, bool isSubscriber, bool isPartner, string tmiSentTs, UserType userType, string rawIrc, string channel) : base(badges, badgeInfo, colorHex, color, displayName, emoteSet, id, login, systemMessage, msgId, msgParamCumulativeMonths, msgParamStreakMonths, msgParamShouldShareStreak, systemMessageParsed, resubMessage, subscriptionPlan, subscriptionPlanName, roomId, userId, isModerator, isTurbo, isSubscriber, isPartner, tmiSentTs, userType, rawIrc, channel, 0) { }//IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) } public class SubscriberBase { protected readonly int monthsInternal; public List<KeyValuePair<string, string>> Badges { get; } public List<KeyValuePair<string, string>> BadgeInfo { get; } public string ColorHex { get; } public Color Color { get; } public string DisplayName { get; } public string EmoteSet { get; } public string Id { get; } public bool IsModerator { get; } public bool IsPartner { get; } public bool IsSubscriber { get; } public bool IsTurbo { get; } public string Login { get; } public string MsgId { get; } public string MsgParamCumulativeMonths { get; } public bool MsgParamShouldShareStreak { get; } public string MsgParamStreakMonths { get; } public string RawIrc { get; } public string ResubMessage { get; } public string RoomId { get; } public SubscriptionPlan SubscriptionPlan { get; } public string SubscriptionPlanName { get; } public string SystemMessage { get; } public string SystemMessageParsed { get; } public string TmiSentTs { get; } public string UserId { get; } public UserType UserType { get; } public string Channel { get; } protected SubscriberBase(IrcMessage ircMessage) { //IL_04a2: Unknown result type (might be due to invalid IL or missing references) //IL_04ae: Unknown result type (might be due to invalid IL or missing references) //IL_058d: Unknown result type (might be due to invalid IL or missing references) //IL_04ba: Unknown result type (might be due to invalid IL or missing references) //IL_0596: Unknown result type (might be due to invalid IL or missing references) //IL_04c6: Unknown result type (might be due to invalid IL or missing references) //IL_059f: Unknown result type (might be due to invalid IL or missing references) //IL_05a8: Unknown result type (might be due to invalid IL or missing references) //IL_05b1: Unknown result type (might be due to invalid IL or missing references) RawIrc = ircMessage.ToString(); ResubMessage = ircMessage.Message; foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "badges": Badges = Helpers.ParseBadges(text); foreach (KeyValuePair<string, string> badge in Badges) { if (badge.Key == "partner") { IsPartner = true; } } break; case "badge-info": BadgeInfo = Helpers.ParseBadges(text); break; case "color": ColorHex = text; if (!string.IsNullOrEmpty(ColorHex)) { Color = TwitchLib.Client.Models.Extensions.NetCore.ColorTranslator.FromHtml(ColorHex); } break; case "display-name": DisplayName = text; break; case "emotes": EmoteSet = text; break; case "id": Id = text; break; case "login": Login = text; break; case "mod": IsModerator = ConvertToBool(text); break; case "msg-id": MsgId = text; break; case "msg-param-cumulative-months": MsgParamCumulativeMonths = text; break; case "msg-param-streak-months": MsgParamStreakMonths = text; break; case "msg-param-should-share-streak": MsgParamShouldShareStreak = Helpers.ConvertToBool(text); break; case "msg-param-sub-plan": switch (text.ToLower()) { case "prime": SubscriptionPlan = (SubscriptionPlan)1; break; case "1000": SubscriptionPlan = (SubscriptionPlan)2; break; case "2000": SubscriptionPlan = (SubscriptionPlan)3; break; case "3000": SubscriptionPlan = (SubscriptionPlan)4; break; default: throw new ArgumentOutOfRangeException("ToLower"); } break; case "msg-param-sub-plan-name": SubscriptionPlanName = text.Replace("\\s", " "); break; case "room-id": RoomId = text; break; case "subscriber": IsSubscriber = ConvertToBool(text); break; case "system-msg": SystemMessage = text; SystemMessageParsed = text.Replace("\\s", " "); break; case "tmi-sent-ts": TmiSentTs = text; break; case "turbo": IsTurbo = ConvertToBool(text); break; case "user-id": UserId = text; break; case "user-type": switch (text) { case "mod": UserType = (UserType)1; break; case "global_mod": UserType = (UserType)2; break; case "admin": UserType = (UserType)4; break; case "staff": UserType = (UserType)5; break; default: UserType = (UserType)0; break; } break; } } } internal SubscriberBase(List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string colorHex, Color color, string displayName, string emoteSet, string id, string login, string systemMessage, string msgId, string msgParamCumulativeMonths, string msgParamStreakMonths, bool msgParamShouldShareStreak, string systemMessageParsed, string resubMessage, SubscriptionPlan subscriptionPlan, string subscriptionPlanName, string roomId, string userId, bool isModerator, bool isTurbo, bool isSubscriber, bool isPartner, string tmiSentTs, UserType userType, string rawIrc, string channel, int months) { //IL_007c: Unknown result type (might be due to invalid IL or missing references) //IL_007e: Unknown result type (might be due to invalid IL or missing references) //IL_00c8: Unknown result type (might be due to invalid IL or missing references) //IL_00ca: Unknown result type (might be due to invalid IL or missing references) Badges = badges; BadgeInfo = badgeInfo; ColorHex = colorHex; Color = color; DisplayName = displayName; EmoteSet = emoteSet; Id = id; Login = login; MsgId = msgId; MsgParamCumulativeMonths = msgParamCumulativeMonths; MsgParamStreakMonths = msgParamStreakMonths; MsgParamShouldShareStreak = msgParamShouldShareStreak; SystemMessage = systemMessage; SystemMessageParsed = systemMessageParsed; ResubMessage = resubMessage; SubscriptionPlan = subscriptionPlan; SubscriptionPlanName = subscriptionPlanName; RoomId = roomId; UserId = UserId; IsModerator = isModerator; IsTurbo = isTurbo; IsSubscriber = isSubscriber; IsPartner = isPartner; TmiSentTs = tmiSentTs; UserType = userType; RawIrc = rawIrc; monthsInternal = months; UserId = userId; Channel = channel; } private static bool ConvertToBool(string data) { return data == "1"; } public override string ToString() { //IL_00dc: Unknown result type (might be due to invalid IL or missing references) return $"Badges: {Badges.Count}, color hex: {ColorHex}, display name: {DisplayName}, emote set: {EmoteSet}, login: {Login}, system message: {SystemMessage}, msgId: {MsgId}, msgParamCumulativeMonths: {MsgParamCumulativeMonths}" + $"msgParamStreakMonths: {MsgParamStreakMonths}, msgParamShouldShareStreak: {MsgParamShouldShareStreak}, resub message: {ResubMessage}, months: {monthsInternal}, room id: {RoomId}, user id: {UserId}, mod: {IsModerator}, turbo: {IsTurbo}, sub: {IsSubscriber}, user type: {UserType}, raw irc: {RawIrc}"; } } public abstract class TwitchLibMessage { public List<KeyValuePair<string, string>> Badges { get; protected set; } public string BotUsername { get; protected set; } public Color Color { get; protected set; } public string ColorHex { get; protected set; } public string DisplayName { get; protected set; } public EmoteSet EmoteSet { get; protected set; } public bool IsTurbo { get; protected set; } public string UserId { get; protected set; } public string Username { get; protected set; } public UserType UserType { get; protected set; } public string RawIrcMessage { get; protected set; } } public class UserBan { public string BanReason; public string Channel; public string Username; public string RoomId; public string TargetUserId; public UserBan(IrcMessage ircMessage) { Channel = ircMessage.Channel; Username = ircMessage.Message; if (ircMessage.Tags.TryGetValue("ban-reason", out var value)) { BanReason = value; } if (ircMessage.Tags.TryGetValue("room-id", out var value2)) { RoomId = value2; } if (ircMessage.Tags.TryGetValue("target-user-id", out var value3)) { TargetUserId = value3; } } public UserBan(string channel, string username, string banReason, string roomId, string targetUserId) { Channel = channel; Username = username; BanReason = banReason; RoomId = roomId; TargetUserId = targetUserId; } } public class UserState { public List<KeyValuePair<string, string>> Badges { get; } = new List<KeyValuePair<string, string>>(); public List<KeyValuePair<string, string>> BadgeInfo { get; } = new List<KeyValuePair<string, string>>(); public string Channel { get; } public string ColorHex { get; } public string DisplayName { get; } public string EmoteSet { get; } public string Id { get; } public bool IsModerator { get; } public bool IsSubscriber { get; } public UserType UserType { get; } public UserState(IrcMessage ircMessage) { //IL_02a7: Unknown result type (might be due to invalid IL or missing references) //IL_023a: Unknown result type (might be due to invalid IL or missing references) //IL_0243: Unknown result type (might be due to invalid IL or missing references) //IL_024c: Unknown result type (might be due to invalid IL or missing references) //IL_0255: Unknown result type (might be due to invalid IL or missing references) //IL_025e: Unknown result type (might be due to invalid IL or missing references) Channel = ircMessage.Channel; foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "badges": Badges = Helpers.ParseBadges(text); break; case "badge-info": BadgeInfo = Helpers.ParseBadges(text); break; case "color": ColorHex = text; break; case "display-name": DisplayName = text; break; case "emote-sets": EmoteSet = text; break; case "id": Id = text; break; case "mod": IsModerator = Helpers.ConvertToBool(text); break; case "subscriber": IsSubscriber = Helpers.ConvertToBool(text); break; case "user-type": switch (text) { case "mod": UserType = (UserType)1; break; case "global_mod": UserType = (UserType)2; break; case "admin": UserType = (UserType)4; break; case "staff": UserType = (UserType)5; break; default: UserType = (UserType)0; break; } break; default: Console.WriteLine("Unaccounted for [UserState]: " + key); break; } } if (string.Equals(ircMessage.User, Channel, StringComparison.InvariantCultureIgnoreCase)) { UserType = (UserType)3; } } public UserState(List<KeyValuePair<string, string>> badges, List<KeyValuePair<string, string>> badgeInfo, string colorHex, string displayName, string emoteSet, string channel, string id, bool isSubscriber, bool isModerator, UserType userType) { //IL_0062: Unknown result type (might be due to invalid IL or missing references) //IL_0064: Unknown result type (might be due to invalid IL or missing references) Badges = badges; BadgeInfo = badgeInfo; ColorHex = colorHex; DisplayName = displayName; EmoteSet = emoteSet; Channel = channel; Id = id; IsSubscriber = isSubscriber; IsModerator = isModerator; UserType = userType; } } public class UserTimeout { public string Channel; public int TimeoutDuration; public string TimeoutReason; public string Username; public string TargetUserId; public UserTimeout(IrcMessage ircMessage) { Channel = ircMessage.Channel; Username = ircMessage.Message; foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "ban-duration": TimeoutDuration = int.Parse(text); break; case "ban-reason": TimeoutReason = text; break; case "target-user-id": TargetUserId = text; break; } } } public UserTimeout(string channel, string username, string targetuserId, int timeoutDuration, string timeoutReason) { Channel = channel; Username = username; TargetUserId = targetuserId; TimeoutDuration = timeoutDuration; TimeoutReason = timeoutReason; } } public class WhisperCommand { public List<string> ArgumentsAsList { get; } public string ArgumentsAsString { get; } public char CommandIdentifier { get; } public string CommandText { get; } public WhisperMessage WhisperMessage { get; } public WhisperCommand(WhisperMessage whisperMessage) { WhisperCommand whisperCommand = this; WhisperMessage = whisperMessage; string[] array = whisperMessage.Message.Split(new char[1] { ' ' }); CommandText = ((array != null) ? array[0].Substring(1, whisperMessage.Message.Split(new char[1] { ' ' })[0].Length - 1) : null) ?? whisperMessage.Message.Substring(1, whisperMessage.Message.Length - 1); string message = whisperMessage.Message; string[] array2 = whisperMessage.Message.Split(new char[1] { ' ' }); ArgumentsAsString = message.Replace(((array2 != null) ? array2[0] : null) + " ", ""); ArgumentsAsList = whisperMessage.Message.Split(new char[1] { ' ' })?.Where((string arg) => arg != whisperMessage.Message[0] + whisperCommand.CommandText).ToList() ?? new List<string>(); CommandIdentifier = whisperMessage.Message[0]; } public WhisperCommand(WhisperMessage whisperMessage, string commandText, string argumentsAsString, List<string> argumentsAsList, char commandIdentifier) { WhisperMessage = whisperMessage; CommandText = commandText; ArgumentsAsString = argumentsAsString; ArgumentsAsList = argumentsAsList; CommandIdentifier = commandIdentifier; } } public class WhisperMessage : TwitchLibMessage { public string MessageId { get; } public string ThreadId { get; } public string Message { get; } public WhisperMessage(List<KeyValuePair<string, string>> badges, string colorHex, Color color, string username, string displayName, EmoteSet emoteSet, string threadId, string messageId, string userId, bool isTurbo, string botUsername, string message, UserType userType) { //IL_0064: Unknown result type (might be due to invalid IL or missing references) base.Badges = badges; base.ColorHex = colorHex; base.Color = color; base.Username = username; base.DisplayName = displayName; base.EmoteSet = emoteSet; ThreadId = threadId; MessageId = messageId; base.UserId = userId; base.IsTurbo = isTurbo; base.BotUsername = botUsername; Message = message; base.UserType = userType; } public WhisperMessage(IrcMessage ircMessage, string botUsername) { base.Username = ircMessage.User; base.BotUsername = botUsername; base.RawIrcMessage = ircMessage.ToString(); Message = ircMessage.Message; foreach (string key in ircMessage.Tags.Keys) { string text = ircMessage.Tags[key]; switch (key) { case "badges": { base.Badges = new List<KeyValuePair<string, string>>(); if (!Enumerable.Contains(text, '/')) { break; } if (!text.Contains(",")) { base.Badges.Add(new KeyValuePair<string, string>(text.Split(new char[1] { '/' })[0], text.Split(new char[1] { '/' })[1])); break; } string[] array = text.Split(new char[1] { ',' }); foreach (string text2 in array) { base.Badges.Add(new KeyValuePair<string, string>(text2.Split(new char[1] { '/' })[0], text2.Split(new char[1] { '/' })[1])); } break; } case "color": base.ColorHex = text; if (!string.IsNullOrEmpty(base.ColorHex)) { base.Color = TwitchLib.Client.Models.Extensions.NetCore.ColorTranslator.FromHtml(base.ColorHex); } break; case "display-name": base.DisplayName = text; break; case "emotes": base.EmoteSet = new EmoteSet(text, Message); break; case "message-id": MessageId = text; break; case "thread-id": ThreadId = text; break; case "turbo": base.IsTurbo = Helpers.ConvertToBool(text); break; case "user-id": base.UserId = text; break; case "user-type": switch (text) { case "global_mod": base.UserType = (UserType)2; break; case "admin": base.UserType = (UserType)4; break; case "staff": base.UserType = (UserType)5; break; default: base.UserType = (UserType)0; break; } break; } } if (base.EmoteSet == null) { base.EmoteSet = new EmoteSet((string)null, Message); } } } } namespace TwitchLib.Client.Models.Internal { public class IrcMessage { private readonly string[] _parameters; public readonly string User; public readonly string Hostmask; public readonly IrcCommand Command; public readonly Dictionary<string, string> Tags; public string Channel { get { if (!Params.StartsWith("#")) { return Params; } return Params.Remove(0, 1); } } public string Params { get { if (_parameters == null || _parameters.Length == 0) { return ""; } return _parameters[0]; } } public string Message => Trailing; public string Trailing { get { if (_parameters == null || _parameters.Length <= 1) { return ""; } return _parameters[_parameters.Length - 1]; } } public IrcMessage(string user) { //IL_001d: Unknown result type (might be due to invalid IL or missing references) _parameters = null; User = user; Hostmask = null; Command = (IrcCommand)0; Tags = null; } public IrcMessage(IrcCommand command, string[] parameters, string hostmask, Dictionary<string, string> tags = null) { //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Unknown result type (might be due to invalid IL or missing references) //IL_0041: Unknown result type (might be due to invalid IL or missing references) //IL_0044: Invalid comparison between Unknown and I4 int num = hostmask.IndexOf('!'); User = ((num != -1) ? hostmask.Substring(0, num) : hostmask); Hostmask = hostmask; _parameters = parameters; Command = command; Tags = tags; if ((int)command == 18 && Params.Length > 0 && Params.Contains("#")) { _parameters[0] = "#" + _parameters[0].Split(new char[1] { '#' })[1]; } } public new string ToString() { StringBuilder stringBuilder = new StringBuilder(32); if (Tags != null) { string[] array = new string[Tags.Count]; int num = 0; foreach (KeyValuePair<string, string> tag in Tags) { array[num] = tag.Key + "=" + tag.Value; num++; } if (array.Length != 0) { stringBuilder.Append("@").Append(string.Join(";", array)).Append(" "); } } if (!string.IsNullOrEmpty(Hostmask)) { stringBuilder.Append(":").Append(Hostmask).Append(" "); } stringBuilder.Append(((object)(IrcCommand)(ref Command)).ToString().ToUpper().Replace("RPL_", "")); if (_parameters.Length == 0) { return stringBuilder.ToString(); } if (_parameters[0] != null && _parameters[0].Length > 0) { stringBuilder.Append(" ").Append(_parameters[0]); } if (_parameters.Length > 1 && _parameters[1] != null && _parameters[1].Length > 0) { stringBuilder.Append(" :").Append(_parameters[1]); } return stringBuilder.ToString(); } } public static class MsgIds { public const string AlreadyBanned = "already_banned"; public const string AlreadyEmotesOff = "already_emotes_off"; public const string AlreadyEmotesOn = "already_emotes_on"; public const string AlreadyR9KOff = "already_r9k_off"; public const string AlreadyR9KOn = "already_r9k_on"; public const string AlreadySubsOff = "already_subs_off"; public const string AlreadySubsOn = "already_subs_on"; public const string Announcement = "announcement"; public const string BadUnbanNoBan = "bad_unban_no_ban"; public const string BanSuccess = "ban_success"; public const string ColorChanged = "color_changed"; public const string EmoteOnlyOff = "emote_only_off"; public const string EmoteOnlyOn = "emote_only_on"; public const string HighlightedMessage = "highlighted-message"; public const string ModeratorsReceived = "room_mods"; public const string NoMods = "no_mods"; public const string NoVIPs = "no_vips"; public const string MsgBannedEmailAlias = "msg_banned_email_alias"; public const string MsgChannelSuspended = "msg_channel_suspended"; public const string MsgRequiresVerifiedPhoneNumber = "msg_requires_verified_phone_number"; public const string MsgVerifiedEmail = "msg_verified_email"; public const string MsgRateLimit = "msg_ratelimit"; public const string MsgDuplicate = "msg_duplicate"; public const string MsgR9k = "msg_r9k"; public const string MsgFollowersOnly = "msg_followersonly"; public const string MsgSubsOnly = "msg_subsonly"; public const string MsgEmoteOnly = "msg_emoteonly"; public const string MsgSuspended = "msg_suspended"; public const string MsgBanned = "msg_banned"; public const string MsgSlowMode = "msg_slowmode"; public const string NoPermission = "no_permission"; public const string PrimePaidUprade = "primepaidupgrade"; public const string Raid = "raid"; public const string RaidErrorSelf = "raid_error_self"; public const string RaidNoticeMature = "raid_notice_mature"; public const string ReSubscription = "resub"; public const string R9KOff = "r9k_off"; public const string R9KOn = "r9k_on"; public const string SubGift = "subgift"; public const string CommunitySubscription = "submysterygift"; public const string ContinuedGiftedSubscription = "giftpaidupgrade"; public const string Subscription = "sub"; public const string SubsOff = "subs_off"; public const string SubsOn = "subs_on"; public const string TimeoutSuccess = "timeout_success"; public const string UnbanSuccess = "unban_success"; public const string UnrecognizedCmd = "unrecognized_cmd"; public const string UserIntro = "user-intro"; public const string VIPsSuccess = "vips_success"; public const string SkipSubsModeMessage = "skip-subs-mode-message"; } public static class Tags { public const string Badges = "badges"; public const string BadgeInfo = "badge-info"; public const string BanDuration = "ban-duration"; public const string BanReason = "ban-reason"; public const string BroadcasterLang = "broadcaster-lang"; public const string Bits = "bits"; public const string Color = "color"; public const string CustomRewardId = "custom-reward-id"; public const string DisplayName = "display-name"; public const string Emotes = "emotes"; public const string EmoteOnly = "emote-only"; public const string EmotesSets = "emote-sets"; public const string FirstMessage = "first-msg"; public const string Flags = "flags"; public const string FollowersOnly = "followers-only"; public const string Id = "id"; public const string Login = "login"; public const string Mercury = "mercury"; public const string MessageId = "message-id"; public const string Mod = "mod"; public const string MsgId = "msg-id"; public const string MsgParamColor = "msg-param-color"; public const string MsgParamDisplayname = "msg-param-displayName"; public const string MsgParamLogin = "msg-param-login"; public const string MsgParamCumulativeMonths = "msg-param-cumulative-months"; public const string MsgParamMonths = "msg-param-months"; public const string MsgParamPromoGiftTotal = "msg-param-promo-gift-total"; public const string MsgParamPromoName = "msg-param-promo-name"; public const string MsgParamShouldShareStreak = "msg-param-should-share-streak"; public const string MsgParamStreakMonths = "msg-param-streak-months"; public const string MsgParamSubPlan = "msg-param-sub-plan"; public const string MsgParamSubPlanName = "msg-param-sub-plan-name"; public const string MsgParamViewerCount = "msg-param-viewerCount"; public const string MsgParamRecipientDisplayname = "msg-param-recipient-display-name"; public const string MsgParamRecipientId = "msg-param-recipient-id"; public const string MsgParamRecipientUsername = "msg-param-recipient-user-name"; public const string MsgParamRitualName = "msg-param-ritual-name"; public const string MsgParamMassGiftCount = "msg-param-mass-gift-count"; public const string MsgParamSenderCount = "msg-param-sender-count"; public const string MsgParamSenderLogin = "msg-param-sender-login"; public const string MsgParamSenderName = "msg-param-sender-name"; public const string MsgParamThreshold = "msg-param-threshold"; public const string Noisy = "noisy"; public const string ReplyParentDisplayName = "reply-parent-display-name"; public const string ReplyParentMsgBody = "reply-parent-msg-body"; public const string ReplyParentMsgId = "reply-parent-msg-id"; public const string ReplyParentUserId = "reply-parent-user-id"; public const string ReplyParentUserLogin = "reply-parent-user-login"; public const string Rituals = "rituals"; public const string RoomId = "room-id"; public const string R9K = "r9k"; public const string Slow = "slow"; public const string Subscriber = "subscriber"; public const string SubsOnly = "subs-only"; public const string SystemMsg = "system-msg"; public const string ThreadId = "thread-id"; public const string TmiSentTs = "tmi-sent-ts"; public const string Turbo = "turbo"; public const string UserId = "user-id"; public const string UserType = "user-type"; public const string MsgParamMultiMonthGiftDuration = "msg-param-gift-months"; public const string TargetUserId = "target-user-id"; } } namespace TwitchLib.Client.Models.Extractors { public class EmoteExtractor { public IEnumerable<Emote> Extract(string rawEmoteSetString, string message) { if (string.IsNullOrEmpty(rawEmoteSetString) || string.IsNullOrEmpty(message)) { yield break; } string emoteId2; if (rawEmoteSetString.Contains("/")) { string[] array = rawEmoteSetString.Split(new char[1] { '/' }); foreach (string text in array) { emoteId2 = text.Split(new char[1] { ':' })[0]; if (text.Contains(",")) { string[] array2 = text.Replace(emoteId2 + ":", "").Split(new char[1] { ',' }); foreach (string emoteData in array2) { yield return GetEmote(emoteData, emoteId2, message); } } else { yield return GetEmote(text, emoteId2, message, single: true); } } yield break; } emoteId2 = rawEmoteSetString.Split(new char[1] { ':' })[0]; if (rawEmoteSetString.Contains(",")) { string[] array = rawEmoteSetString.Replace(emoteId2 + ":", "").Split(new char[1] { ',' }); foreach (string emoteData2 in array) { yield return GetEmote(emoteData2, emoteId2, message); } } else { yield return GetEmote(rawEmoteSetString, emoteId2, message, single: true); } } private Emote GetEmote(string emoteData, string emoteId, string message, bool single = false) { int num = -1; int num2 = -1; if (single) { num = int.Parse(emoteData.Split(new char[1] { ':' })[1].Split(new char[1] { '-' })[0]); num2 = int.Parse(emoteData.Split(new char[1] { ':' })[1].Split(new char[1] { '-' })[1]); } else { num = int.Parse(emoteData.Split(new char[1] { '-' })[0]); num2 = int.Parse(emoteData.Split(new char[1] { '-' })[1]); } string name = message.Substring(num, num2 - num + 1); return EmoteBuilder.Create().WithId(emoteId).WithName(name) .WithStartIndex(num) .WithEndIndex(num2) .Build(); } } public interface IExtractor<TResult> { TResult Extract(IrcMessage ircMessage); } } namespace TwitchLib.Client.Models.Extensions.NetCore { public static class ColorTranslator { public static Color FromHtml(string hexColor) { hexColor += 0; return Color.FromArgb(int.Parse(hexColor.Replace("#", ""), NumberStyles.HexNumber)); } } } namespace TwitchLib.Client.Models.Common { public static class Helpers { public static List<string> ParseQuotesAndNonQuotes(string message) { List<string> list = new List<string>(); if (message == "") { return new List<string>(); } bool flag = message[0] != '"'; string[] array = message.Split(new char[1] { '"' }); foreach (string text in array) { if (string.IsNullOrEmpty(text)) { continue; } if (!flag) { list.Add(text); flag = true; } else { if (!text.Contains(" ")) { continue; } string[] array2 = text.Split(new char[1] { ' ' }); foreach (string text2 in array2) { if (!string.IsNullOrWhiteSpace(text2)) { list.Add(text2); flag = false; } } } } return list; } public static List<KeyValuePair<string, string>> ParseBadges(string badgesStr) { List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(); if (Enumerable.Contains(badgesStr, '/')) { if (!badgesStr.Contains(",")) { list.Add(new KeyValuePair<string, string>(badgesStr.Split(new char[1] { '/' })[0], badgesStr.Split(new char[1] { '/' })[1])); } else { string[] array = badgesStr.Split(new char[1] { ',' }); foreach (string text in array) { list.Add(new KeyValuePair<string, string>(text.Split(new char[1] { '/' })[0], text.Split(new char[1] { '/' })[1])); } } } return list; } public static string ParseToken(string token, string message) { string result = string.Empty; for (int num = message.IndexOf(token, StringComparison.InvariantCultureIgnoreCase); num > -1; num = message.IndexOf(token, num + token.Length, StringComparison.InvariantCultureIgnoreCase)) { result = new string(message.Substring(num).TakeWhile((char x) => x != ';' && x != ' ').ToArray()).Split(new char[1] { '=' }).LastOrDefault(); } return result; } public static bool ConvertToBool(string data) { return data == "1"; } } } namespace TwitchLib.Client.Models.Builders { public sealed class ChannelStateBuilder : IBuilder<ChannelState>, IFromIrcMessageBuilder<ChannelState> { private string _broadcasterLanguage; private string _channel; private bool _emoteOnly; private TimeSpan _followersOnly; private bool _mercury; private bool _r9K; private bool _rituals; private string _roomId; private int _slowMode; private bool _subOnly; private ChannelStateBuilder() { } public ChannelStateBuilder WithBroadcasterLanguage(string broadcasterLanguage) { _broadcasterLanguage = broadcasterLanguage; return this; } public ChannelStateBuilder WithChannel(string channel) { _channel = channel; return this; } public ChannelStateBuilder WithEmoteOnly(bool emoteOnly) { _emoteOnly = emoteOnly; return this; } public ChannelStateBuilder WIthFollowersOnly(TimeSpan followersOnly) { _followersOnly = followersOnly; return this; } public ChannelStateBuilder WithMercury(bool mercury) { _mercury = mercury; return this; } public ChannelStateBuilder WithR9K(bool r9k) { _r9K = r9k; return this; } public ChannelStateBuilder WithRituals(bool rituals) { _rituals = rituals; return this; } public ChannelStateBuilder WithRoomId(string roomId) { _roomId = roomId; return this; } public ChannelStateBuilder WithSlowMode(int slowMode) { _slowMode = slowMode; return this; } public ChannelStateBuilder WithSubOnly(bool subOnly) { _subOnly = subOnly; return this; } public static ChannelStateBuilder Create() { return new ChannelStateBuilder(); } public ChannelState Build() { return new ChannelState(_r9K, _rituals, _subOnly, _slowMode, _emoteOnly, _broadcasterLanguage, _channel, _followersOnly, _mercury, _roomId); } public ChannelState BuildFromIrcMessage(FromIrcMessageBuilderDataObject fromIrcMessageBuilderDataObject) { return new ChannelState(fromIrcMessageBuilderDataObject.Message); } } public sealed class ChatCommandBuilder : IBuilder<ChatCommand> { private readonly List<string> _argumentsAsList = new List<string>(); private string _argumentsAsString; private ChatMessage _chatMessage; private char _commandIdentifier; private string _commandText; private ChatCommandBuilder() { } public ChatCommandBuilder WithArgumentsAsList(params string[] argumentsList) { _argumentsAsList.AddRange(argumentsList); return this; } public ChatCommandBuilder WithArgumentsAsString(string argumentsAsString) { _argumentsAsString = argumentsAsString; return this; } public ChatCommandBuilder WithChatMessage(ChatMessage chatMessage) { _chatMessage = chatMessage; return this; } public ChatCommandBuilder WithCommandIdentifier(char commandIdentifier) { _commandIdentifier = commandIdentifier; return this; } public ChatCommandBuilder WithCommandText(string commandText) { _commandText = commandText; return this; } public static ChatCommandBuilder Create() { return new ChatCommandBuilder(); } public ChatCommand Build() { return new ChatCommand(_chatMessage, _commandText, _argumentsAsString, _argumentsAsList, _commandIdentifier); } public ChatCommand BuildFromChatMessage(ChatMessage chatMessage) { return new ChatCommand(chatMessage); } } public sealed class ChatMessageBuilder : IBuilder<ChatMessage> { private TwitchLibMessage _twitchLibMessage; private readonly List<KeyValuePair<string, string>> BadgeInfo = new List<KeyValuePair<string, string>>(); private int _bits; private double _bitsInDollars; private string _channel; private CheerBadge _cheerBadge; private string _emoteReplacedMessage; private string _id; private bool _isBroadcaster; private bool _isMe; private bool _isModerator; private bool _isSubscriber; private bool _isVip; private bool _isStaff; private bool _isPartner; private string _message; private Noisy _noisy; private string _rawIrcMessage; private string _roomId; private int _subscribedMonthCount; private ChatMessageBuilder() { _twitchLibMessage = TwitchLibMessageBuilder.Create().Build(); } public ChatMessageBuilder WithTwitchLibMessage(TwitchLibMessage twitchLibMessage) { _twitchLibMessage = twitchLibMessage; return this; } public ChatMessageBuilder WithBadgeInfos(params KeyValuePair<string, string>[] badgeInfos) { BadgeInfo.AddRange(badgeInfos); return this; } public ChatMessageBuilder WithBits(int bits) { _bits = bits; return this; } public ChatMessageBuilder WithBitsInDollars(double bitsInDollars) { _bitsInDollars = bitsInDollars; return this; } public ChatMessageBuilder WithChannel(string channel) { _channel = channel; return this; } public ChatMessageBuilder WithCheerBadge(CheerBadge cheerBadge) { _cheerBadge = cheerBadge; return this; } public ChatMessageBuilder WithEmoteReplaceMessage(string emoteReplaceMessage) { _emoteReplacedMessage = emoteReplaceMessage; return this; } public ChatMessageBuilder WithId(string id) { _id = id; return this; } public ChatMessageBuilder WithIsBroadcaster(bool isBroadcaster) { _isBroadcaster = isBroadcaster; return this; } public ChatMessageBuilder WithIsMe(bool isMe) { _isMe = isMe; return this; } public ChatMessageBuilder WithIsModerator(bool isModerator) { _isModerator = isModerator; return this; } public ChatMessageBuilder WithIsSubscriber(bool isSubscriber) { _isSubscriber = isSubscriber; return this; } public ChatMessageBuilder WithIsVip(bool isVip) { _isVip = isVip; return this; } public ChatMessageBuilder WithIsStaff(bool isStaff) { _isStaff = isStaff; return this; } public ChatMessageBuilder WithIsPartner(bool isPartner) { _isPartner = isPartner; return this; } public ChatMessageBuilder WithMessage(string message) { _message = message; return this; } public ChatMessageBuilder WithNoisy(Noisy noisy) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0002: Unknown result type (might be due to invalid IL or missing references) _noisy = noisy; return this; } public ChatMessageBuilder WithRawIrcMessage(string rawIrcMessage) { _rawIrcMessage = rawIrcMessage; return this; } public ChatMessageBuilder WithRoomId(string roomId) { _roomId = roomId; return this; } public ChatMessageBuilder WithSubscribedMonthCount(int subscribedMonthCount) { _subscribedMonthCount = subscribedMonthCount; return this; } public static ChatMessageBuilder Create() { return new ChatMessageBuilder(); } public ChatMessage Build() { //IL_0059: Unknown result type (might be due to invalid IL or missing references) //IL_00ac: Unknown result type (might be due to invalid IL or missing references) return new ChatMessage(_twitchLibMessage.BotUsername, _twitchLibMessage.UserId, _twitchLibMessage.Username, _twitchLibMessage.DisplayName, _twitchLibMessage.ColorHex, _twitchLibMessage.Color, _twitchLibMessage.EmoteSet, _message, _twitchLibMessage.UserType, _channel, _id, _isSubscriber, _subscribedMonthCount, _roomId, _twitchLibMessage.IsTurbo, _isModerator, _isMe, _isBroadcaster, _isVip, _isPartner, _isStaff, _noisy, _rawIrcMessage, _emoteReplacedMessage, _twitchLibMessage.Badges, _cheerBadge, _bits, _bitsInDollars); } } public sealed class CheerBadgeBuilder : IBuilder<CheerBadge> { private int _cheerAmount; public CheerBadgeBuilder WithCheerAmount(int cheerAmount) { _cheerAmount = cheerAmount; return this; } public CheerBadge Build() { return new CheerBadge(_cheerAmount); } } public sealed class CommunitySubscriptionBuilder : IFromIrcMessageBuilder<CommunitySubscription> { private CommunitySubscriptionBuilder() { } public static CommunitySubscriptionBuilder Create() { return new CommunitySubscriptionBuilder(); } public CommunitySubscription BuildFromIrcMessage(FromIrcMessageBuilderDataObject fromIrcMessageBuilderDataObject) { return new CommunitySubscription(fromIrcMessageBuilderDataObject.Message); } } public sealed class ConnectionCredentialsBuilder : IBuilder<ConnectionCredentials> { private string _twitchUsername; private string _twitchOAuth; private string _twitchWebsocketURI = "wss://irc-ws.chat.twitch.tv:443"; private bool _disableUsernameCheck; private ConnectionCredentialsBuilder() { } public ConnectionCredentialsBuilder WithTwitchUsername(string twitchUsername) { _twitchUsername = twitchUsername; return this; } public ConnectionCredentialsBuilder WithTwitchOAuth(string twitchOAuth) { _twitchOAuth = twitchOAuth; return this; } public ConnectionCredentialsBuilder WithTwitchWebSocketUri(string twitchWebSocketUri) { _twitchWebsocketURI = twitchWebSocketUri; return this; } public ConnectionCredentialsBuilder WithDisableUsernameCheck(bool disableUsernameCheck) { _disableUsernameCheck = disableUsernameCheck; return this; } public static ConnectionCredentialsBuilder Create() { return new ConnectionCredentialsBuilder(); } public ConnectionCredentials Build() { return new ConnectionCredentials(_twitchUsername, _twitchOAuth, _twitchWebsocketURI, _disableUsernameCheck); } } public sealed class EmoteBuilder : IBuilder<Emote> { private string _id; private string _name; private int _startIndex; private int _endIndex; private EmoteBuilder() { } public static EmoteBuilder Create() { return new EmoteBuilder(); } public EmoteBuilder WithId(string id) { _id = id; return this; } public EmoteBuilder WithName(string name) { _name = name; return this; } public EmoteBuilder WithStartIndex(int startIndex) { _startIndex = startIndex; return this; } public EmoteBuilder WithEndIndex(int endIndex) { _endIndex = endIndex; return this; } public Emote Build() { return new Emote(_id, _name, _startIndex, _endIndex); } } public sealed class ErrorEventBuilder : IBuilder<ErrorEvent> { private string _message; private ErrorEventBuilder() { } public ErrorEventBuilder WithMessage(string message) { _message = message; return this; } public static ErrorEventBuilder Create() { return new ErrorEventBuilder(); } public ErrorEvent Build() { return new ErrorEvent { Message = _message }; } } public class FromIrcMessageBuilderDataObject { public IrcMessage Message { get; set; } public object AditionalData { get; set; } } public sealed class GiftedSubscriptionBuilder : IBuilder<GiftedSubscription>, IFromIrcMessageBuilder<GiftedSubscription> { private readonly List<KeyValuePair<string, string>> _badges = new List<KeyValuePair<string, string>>(); private readonly List<KeyValuePair<string, string>> _badgeInfo = new List<KeyValuePair<string, string>>(); private string _color; private string _displayName; private string _emotes; private string _id; private bool _isModerator; private bool _isSubscriber; private bool _isTurbo; private string _login; private string _msgId; private string _msgParamMonths; private string _msgParamRecipientDisplayName; private string _msgParamRecipientId; private string _msgParamRecipientUserName; private string _msgParamSubPlanName; private string _msgParamMultiMonthGiftDuration; private SubscriptionPlan _msgParamSubPlan; private string _roomId; private string _systemMsg; private string _systemMsgParsed; private string _tmiSentTs; private string _userId; private UserType _userType; private GiftedSubscriptionBuilder() { } public GiftedSubscriptionBuilder WithBadges(params KeyValuePair<string, string>[] badges) { _badges.AddRange(badges); return this; } public GiftedSubscriptionBuilder WithBadgeInfos(params KeyValuePair<string, string>[] badgeInfos) { _badgeInfo.AddRange(badgeInfos); return this; } public GiftedSubscriptionBuilder WithColor(string color) { _color = color; return this; } public GiftedSubscriptionBuilder WithDisplayName(string displayName) { _displayName = displayName; return this; } public GiftedSubscriptionBuilder WithEmotes(string emotes) { _emotes = emotes; return this; } public GiftedSubscriptionBuilder WithId(string id) { _id = id; return this; } public GiftedSubscriptionBuilder WithIsModerator(bool isModerator) { _isModerator = isModerator; return this; } public GiftedSubscriptionBuilder WithIsSubscriber(bool isSubscriber) { _isSubscriber = isSubscriber; return this; } public GiftedSubscriptionBuilder WithIsTurbo(bool isTurbo) { _isTurbo = isTurbo; return this; } public GiftedSubscriptionBuilder WithLogin(string login) { _login = login; return this; } public GiftedSubscriptionBuilder WithMessageId(string msgId) { _msgId = msgId; return this; } public GiftedSubscriptionBuilder WithMsgParamCumulativeMonths(string msgParamMonths) { _msgParamMonths = msgParamMonths; return this; } public GiftedSubscriptionBuilder WithMsgParamRecipientDisplayName(string msgParamRecipientDisplayName) { _msgParamRecipientDisplayName = msgParamRecipientDisplayName; return this; } public GiftedSubscriptionBuilder WithMsgParamRecipientId(string msgParamRecipientId) { _msgParamRecipientId = msgParamRecipientId; return this; } public GiftedSubscriptionBuilder WithMsgParamRecipientUserName(string msgParamRecipientUserName) { _msgParamRecipientUserName = msgParamRecipientUserName; return this; } public GiftedSubscriptionBuilder WithMsgParamSubPlanName(string msgParamSubPlanName) { _msgParamSubPlanName = msgParamSubPlanName; return this; } public GiftedSubscriptionBuilder WithMsgParamMultiMonthGiftDuration(string msgParamMultiMonthGiftDuration) { _msgParamMultiMonthGiftDuration = msgParamMultiMonthGiftDuration; return this; } public GiftedSubscriptionBuilder WithMsgParamSubPlan(SubscriptionPlan msgParamSubPlan) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0002: Unknown result type (might be due to invalid IL or missing references) _msgParamSubPlan = msgParamSubPlan; return this; } public GiftedSubscriptionBuilder WithRoomId(string roomId) { _roomId = roomId; return this; } public GiftedSubscriptionBuilder WithSystemMsg(string systemMsg) { _systemMsg = systemMsg; return this; } public GiftedSubscriptionBuilder WithSystemMsgParsed(string systemMsgParsed) { _systemMsgParsed = systemMsgParsed; return this; } public GiftedSubscriptionBuilder WithTmiSentTs(string tmiSentTs) { _tmiSentTs = tmiSentTs; return this; } public GiftedSubscriptionBuilder WithUserId(string userId) { _userId = userId; return this; } public GiftedSubscriptionBuilder WithUserType(UserType userType) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0002: Unknown result type (might be due to invalid IL or missing references) _userType = userType; return this; } public static GiftedSubscriptionBuilder Create() { return new GiftedSubscriptionBuilder(); } public GiftedSubscription Build() { //IL_005b: Unknown result type (might be due to invalid IL or missing references) //IL_0085: Unknown result type (might be due to invalid IL or missing references) return new GiftedSubscription(_badges, _badgeInfo, _color, _displayName, _emotes, _id, _login, _isModerator, _msgId, _msgParamMonths, _msgParamRecipientDisplayName, _msgParamRecipientId, _msgParamRecipientUserName, _msgParamSubPlanName, _msgParamMultiMonthGiftDuration, _msgParamSubPlan, _roomId, _isSubscriber, _systemMsg, _systemMsgParsed, _tmiSentTs, _isTurbo, _userType, _userId); } public GiftedSubscription BuildFromIrcMessage(FromIrcMessageBuilderDataObject fromIrcMessageBuilderDataObject) { return new GiftedSubscription(fromIrcMessageBuilderDataObject.Message); } } public interface IBuilder<T> { T Build(); } public interface IFromIrcMessageBuilder<T> { T BuildFromIrcMessage(FromIrcMessageBuilderDataObject fromIrcMessageBuilderDataObject); } public sealed class IrcMessageBuilder : IBuilder<IrcMessage> { private IrcCommand _ircCommand; private readonly List<string> _parameters = new List<string>(); private string _hostmask; private Dictionary<string, string> _tags; public static IrcMessageBuilder Create() { return new IrcMessageBuilder(); } private IrcMessageBuilder() { } public IrcMessageBuilder WithCommand(IrcCommand ircCommand) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0002: Unknown result type (might be due to invalid IL or missing references) _ircCommand = ircCommand; return Builder(); } public IrcMessageBuilder WithParameter(params string[] parameters) { _parameters.AddRange(parameters); return Builder(); } private IrcMessageBuilder Builder() { return this; } public IrcMessage BuildWithUserOnly(string user) { return new IrcMessage(user); } public IrcMessageBuilder WithHostMask(string hostMask) { _hostmask = hostMask; return Builder(); } public IrcMessageBuilder WithTags(Dictionary<string, string> tags) { _tags = tags; return Builder(); } public IrcMessage Build() { //IL_0001: Unknown result type (might be due to invalid IL or missing references) return new IrcMessage(_ircCom
TwitchLib.Communication.dll
Decompiled a month agousing System; using System.Collections.Concurrent; using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Security; using System.Net.Sockets; using System.Net.WebSockets; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Text; using System.Threading; using System.Threading.Tasks; using TwitchLib.Communication.Clients; using TwitchLib.Communication.Enums; using TwitchLib.Communication.Events; using TwitchLib.Communication.Interfaces; using TwitchLib.Communication.Models; using TwitchLib.Communication.Services; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")] [assembly: AssemblyCompany("swiftyspiffy, Prom3theu5, Syzuna, LuckyNoS7evin")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright 2022")] [assembly: AssemblyDescription("Connection library used throughout TwitchLib to replace third party depedencies.")] [assembly: AssemblyFileVersion("1.0.4")] [assembly: AssemblyInformationalVersion("1.0.4")] [assembly: AssemblyProduct("TwitchLib.Communication")] [assembly: AssemblyTitle("TwitchLib.Communication")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/TwitchLib/TwitchLib.Communication")] [assembly: NeutralResourcesLanguage("en-US")] [assembly: AssemblyVersion("1.0.4.0")] namespace TwitchLib.Communication.Services { public class Throttlers { public readonly BlockingCollection<Tuple<DateTime, string>> SendQueue = new BlockingCollection<Tuple<DateTime, string>>(); public readonly BlockingCollection<Tuple<DateTime, string>> WhisperQueue = new BlockingCollection<Tuple<DateTime, string>>(); public bool ResetThrottlerRunning; public bool ResetWhisperThrottlerRunning; public int SentCount; public int WhispersSent; public Task ResetThrottler; public Task ResetWhisperThrottler; private readonly TimeSpan _throttlingPeriod; private readonly TimeSpan _whisperThrottlingPeriod; private readonly IClient _client; public bool Reconnecting { get; set; } public bool ShouldDispose { get; set; } public CancellationTokenSource TokenSource { get; set; } public Throttlers(IClient client, TimeSpan throttlingPeriod, TimeSpan whisperThrottlingPeriod) { _throttlingPeriod = throttlingPeriod; _whisperThrottlingPeriod = whisperThrottlingPeriod; _client = client; } public void StartThrottlingWindowReset() { ResetThrottler = Task.Run(async delegate { ResetThrottlerRunning = true; while (!ShouldDispose && !Reconnecting) { Interlocked.Exchange(ref SentCount, 0); await Task.Delay(_throttlingPeriod, TokenSource.Token); } ResetThrottlerRunning = false; return Task.CompletedTask; }); } public void StartWhisperThrottlingWindowReset() { ResetWhisperThrottler = Task.Run(async delegate { ResetWhisperThrottlerRunning = true; while (!ShouldDispose && !Reconnecting) { Interlocked.Exchange(ref WhispersSent, 0); await Task.Delay(_whisperThrottlingPeriod, TokenSource.Token); } ResetWhisperThrottlerRunning = false; return Task.CompletedTask; }); } public void IncrementSentCount() { Interlocked.Increment(ref SentCount); } public void IncrementWhisperCount() { Interlocked.Increment(ref WhispersSent); } public Task StartSenderTask() { StartThrottlingWindowReset(); return Task.Run(async delegate { _ = 2; try { while (!ShouldDispose) { await Task.Delay(_client.Options.SendDelay); if (SentCount == _client.Options.MessagesAllowedInPeriod) { _client.MessageThrottled(new OnMessageThrottledEventArgs { Message = "Message Throttle Occured. Too Many Messages within the period specified in WebsocketClientOptions.", AllowedInPeriod = _client.Options.MessagesAllowedInPeriod, Period = _client.Options.ThrottlingPeriod, SentMessageCount = Interlocked.CompareExchange(ref SentCount, 0, 0) }); } else if (_client.IsConnected && !ShouldDispose) { Tuple<DateTime, string> msg = SendQueue.Take(TokenSource.Token); if (!(msg.Item1.Add(_client.Options.SendCacheItemTimeout) < DateTime.UtcNow)) { try { IClient client = _client; if (client is WebSocketClient webSocketClient) { await webSocketClient.SendAsync(Encoding.UTF8.GetBytes(msg.Item2)); } else if (client is TwitchLib.Communication.Clients.TcpClient tcpClient) { await tcpClient.SendAsync(msg.Item2); } IncrementSentCount(); } catch (Exception exception) { _client.SendFailed(new OnSendFailedEventArgs { Data = msg.Item2, Exception = exception }); break; } } } } } catch (Exception exception2) { _client.SendFailed(new OnSendFailedEventArgs { Data = "", Exception = exception2 }); _client.Error(new OnErrorEventArgs { Exception = exception2 }); } }); } public Task StartWhisperSenderTask() { StartWhisperThrottlingWindowReset(); return Task.Run(async delegate { _ = 2; try { while (!ShouldDispose) { await Task.Delay(_client.Options.SendDelay); if (WhispersSent == _client.Options.WhispersAllowedInPeriod) { _client.WhisperThrottled(new OnWhisperThrottledEventArgs { Message = "Whisper Throttle Occured. Too Many Whispers within the period specified in ClientOptions.", AllowedInPeriod = _client.Options.WhispersAllowedInPeriod, Period = _client.Options.WhisperThrottlingPeriod, SentWhisperCount = Interlocked.CompareExchange(ref WhispersSent, 0, 0) }); } else if (_client.IsConnected && !ShouldDispose) { Tuple<DateTime, string> msg = WhisperQueue.Take(TokenSource.Token); if (!(msg.Item1.Add(_client.Options.SendCacheItemTimeout) < DateTime.UtcNow)) { try { IClient client = _client; if (client is WebSocketClient webSocketClient) { await webSocketClient.SendAsync(Encoding.UTF8.GetBytes(msg.Item2)); } else if (client is TwitchLib.Communication.Clients.TcpClient tcpClient) { await tcpClient.SendAsync(msg.Item2); } IncrementWhisperCount(); } catch (Exception exception) { _client.SendFailed(new OnSendFailedEventArgs { Data = msg.Item2, Exception = exception }); break; } } } } } catch (Exception exception2) { _client.SendFailed(new OnSendFailedEventArgs { Data = "", Exception = exception2 }); _client.Error(new OnErrorEventArgs { Exception = exception2 }); } }); } } } namespace TwitchLib.Communication.Models { public class ClientOptions : IClientOptions { public int SendQueueCapacity { get; set; } = 10000; public TimeSpan SendCacheItemTimeout { get; set; } = TimeSpan.FromMinutes(30.0); public ushort SendDelay { get; set; } = 50; public ReconnectionPolicy ReconnectionPolicy { get; set; } = new ReconnectionPolicy(3000, (int?)10); public bool UseSsl { get; set; } = true; public int DisconnectWait { get; set; } = 20000; public ClientType ClientType { get; set; } public TimeSpan ThrottlingPeriod { get; set; } = TimeSpan.FromSeconds(30.0); public int MessagesAllowedInPeriod { get; set; } = 100; public TimeSpan WhisperThrottlingPeriod { get; set; } = TimeSpan.FromSeconds(60.0); public int WhispersAllowedInPeriod { get; set; } = 100; public int WhisperQueueCapacity { get; set; } = 10000; } public class ReconnectionPolicy { private readonly int _reconnectStepInterval; private readonly int? _initMaxAttempts; private int _minReconnectInterval; private readonly int _maxReconnectInterval; private int? _maxAttempts; private int _attemptsMade; public ReconnectionPolicy() { _reconnectStepInterval = 3000; _minReconnectInterval = 3000; _maxReconnectInterval = 30000; _maxAttempts = null; _initMaxAttempts = null; _attemptsMade = 0; } public void SetMaxAttempts(int attempts) { _maxAttempts = attempts; } public void Reset() { _attemptsMade = 0; _minReconnectInterval = _reconnectStepInterval; _maxAttempts = _initMaxAttempts; } public void SetAttemptsMade(int count) { _attemptsMade = count; } public ReconnectionPolicy(int minReconnectInterval, int maxReconnectInterval, int? maxAttempts) { _reconnectStepInterval = minReconnectInterval; _minReconnectInterval = ((minReconnectInterval > maxReconnectInterval) ? maxReconnectInterval : minReconnectInterval); _maxReconnectInterval = maxReconnectInterval; _maxAttempts = maxAttempts; _initMaxAttempts = maxAttempts; _attemptsMade = 0; } public ReconnectionPolicy(int minReconnectInterval, int maxReconnectInterval) { _reconnectStepInterval = minReconnectInterval; _minReconnectInterval = ((minReconnectInterval > maxReconnectInterval) ? maxReconnectInterval : minReconnectInterval); _maxReconnectInterval = maxReconnectInterval; _maxAttempts = null; _initMaxAttempts = null; _attemptsMade = 0; } public ReconnectionPolicy(int reconnectInterval) { _reconnectStepInterval = reconnectInterval; _minReconnectInterval = reconnectInterval; _maxReconnectInterval = reconnectInterval; _maxAttempts = null; _initMaxAttempts = null; _attemptsMade = 0; } public ReconnectionPolicy(int reconnectInterval, int? maxAttempts) { _reconnectStepInterval = reconnectInterval; _minReconnectInterval = reconnectInterval; _maxReconnectInterval = reconnectInterval; _maxAttempts = maxAttempts; _initMaxAttempts = maxAttempts; _attemptsMade = 0; } internal void ProcessValues() { _attemptsMade++; if (_minReconnectInterval < _maxReconnectInterval) { _minReconnectInterval += _reconnectStepInterval; } if (_minReconnectInterval > _maxReconnectInterval) { _minReconnectInterval = _maxReconnectInterval; } } public int GetReconnectInterval() { return _minReconnectInterval; } public bool AreAttemptsComplete() { return _attemptsMade == _maxAttempts; } } } namespace TwitchLib.Communication.Interfaces { public interface IClient { TimeSpan DefaultKeepAliveInterval { get; set; } int SendQueueLength { get; } int WhisperQueueLength { get; } bool IsConnected { get; } IClientOptions Options { get; } event EventHandler<OnConnectedEventArgs> OnConnected; event EventHandler<OnDataEventArgs> OnData; event EventHandler<OnDisconnectedEventArgs> OnDisconnected; event EventHandler<OnErrorEventArgs> OnError; event EventHandler<OnFatalErrorEventArgs> OnFatality; event EventHandler<OnMessageEventArgs> OnMessage; event EventHandler<OnMessageThrottledEventArgs> OnMessageThrottled; event EventHandler<OnWhisperThrottledEventArgs> OnWhisperThrottled; event EventHandler<OnSendFailedEventArgs> OnSendFailed; event EventHandler<OnStateChangedEventArgs> OnStateChanged; event EventHandler<OnReconnectedEventArgs> OnReconnected; void Close(bool callDisconnect = true); void Dispose(); bool Open(); bool Send(string message); bool SendWhisper(string message); void Reconnect(); void MessageThrottled(OnMessageThrottledEventArgs eventArgs); void SendFailed(OnSendFailedEventArgs eventArgs); void Error(OnErrorEventArgs eventArgs); void WhisperThrottled(OnWhisperThrottledEventArgs eventArgs); } public interface IClientOptions { ClientType ClientType { get; set; } int DisconnectWait { get; set; } int MessagesAllowedInPeriod { get; set; } ReconnectionPolicy ReconnectionPolicy { get; set; } TimeSpan SendCacheItemTimeout { get; set; } ushort SendDelay { get; set; } int SendQueueCapacity { get; set; } TimeSpan ThrottlingPeriod { get; set; } bool UseSsl { get; set; } TimeSpan WhisperThrottlingPeriod { get; set; } int WhispersAllowedInPeriod { get; set; } int WhisperQueueCapacity { get; set; } } } namespace TwitchLib.Communication.Events { public class OnConnectedEventArgs : EventArgs { } public class OnDataEventArgs : EventArgs { public byte[] Data; } public class OnDisconnectedEventArgs : EventArgs { } public class OnErrorEventArgs : EventArgs { public Exception Exception { get; set; } } public class OnFatalErrorEventArgs : EventArgs { public string Reason; } public class OnMessageEventArgs : EventArgs { public string Message; } public class OnMessageThrottledEventArgs : EventArgs { public string Message { get; set; } public int SentMessageCount { get; set; } public TimeSpan Period { get; set; } public int AllowedInPeriod { get; set; } } public class OnReconnectedEventArgs : EventArgs { } public class OnSendFailedEventArgs : EventArgs { public string Data; public Exception Exception; } public class OnStateChangedEventArgs : EventArgs { public bool IsConnected; public bool WasConnected; } public class OnWhisperThrottledEventArgs : EventArgs { public string Message { get; set; } public int SentWhisperCount { get; set; } public TimeSpan Period { get; set; } public int AllowedInPeriod { get; set; } } } namespace TwitchLib.Communication.Enums { public enum ClientType { Chat, PubSub } } namespace TwitchLib.Communication.Clients { public class TcpClient : IClient { private int NotConnectedCounter; private readonly string _server = "irc.chat.twitch.tv"; private StreamReader _reader; private StreamWriter _writer; private readonly Throttlers _throttlers; private CancellationTokenSource _tokenSource = new CancellationTokenSource(); private bool _stopServices; private bool _networkServicesRunning; private Task[] _networkTasks; private Task _monitorTask; public TimeSpan DefaultKeepAliveInterval { get; set; } public int SendQueueLength => _throttlers.SendQueue.Count; public int WhisperQueueLength => _throttlers.WhisperQueue.Count; public bool IsConnected => Client?.Connected ?? false; public IClientOptions Options { get; } private int Port { get { if (Options == null) { return 0; } if (!Options.UseSsl) { return 80; } return 443; } } public System.Net.Sockets.TcpClient Client { get; private set; } public event EventHandler<OnConnectedEventArgs> OnConnected; public event EventHandler<OnDataEventArgs> OnData; public event EventHandler<OnDisconnectedEventArgs> OnDisconnected; public event EventHandler<OnErrorEventArgs> OnError; public event EventHandler<OnFatalErrorEventArgs> OnFatality; public event EventHandler<OnMessageEventArgs> OnMessage; public event EventHandler<OnMessageThrottledEventArgs> OnMessageThrottled; public event EventHandler<OnWhisperThrottledEventArgs> OnWhisperThrottled; public event EventHandler<OnSendFailedEventArgs> OnSendFailed; public event EventHandler<OnStateChangedEventArgs> OnStateChanged; public event EventHandler<OnReconnectedEventArgs> OnReconnected; public TcpClient(IClientOptions options = null) { Options = options ?? new ClientOptions(); _throttlers = new Throttlers(this, Options.ThrottlingPeriod, Options.WhisperThrottlingPeriod) { TokenSource = _tokenSource }; InitializeClient(); } private void InitializeClient() { Client = new System.Net.Sockets.TcpClient(); if (_monitorTask == null) { _monitorTask = StartMonitorTask(); } else if (_monitorTask.IsCompleted) { _monitorTask = StartMonitorTask(); } } public bool Open() { try { if (IsConnected) { return true; } Task.Run(delegate { InitializeClient(); Client.Connect(_server, Port); if (Options.UseSsl) { SslStream sslStream = new SslStream(Client.GetStream(), leaveInnerStreamOpen: false); sslStream.AuthenticateAsClient(_server); _reader = new StreamReader(sslStream); _writer = new StreamWriter(sslStream); } else { _reader = new StreamReader(Client.GetStream()); _writer = new StreamWriter(Client.GetStream()); } }).Wait(10000); if (!IsConnected) { return Open(); } StartNetworkServices(); return true; } catch (Exception) { InitializeClient(); return false; } } public void Close(bool callDisconnect = true) { _reader?.Dispose(); _writer?.Dispose(); Client?.Close(); _stopServices = callDisconnect; CleanupServices(); InitializeClient(); this.OnDisconnected?.Invoke(this, new OnDisconnectedEventArgs()); } public void Reconnect() { Task.Run(delegate { Task.Delay(20).Wait(); Close(); if (Open()) { this.OnReconnected?.Invoke(this, new OnReconnectedEventArgs()); } }); } public bool Send(string message) { try { if (!IsConnected || SendQueueLength >= Options.SendQueueCapacity) { return false; } _throttlers.SendQueue.Add(new Tuple<DateTime, string>(DateTime.UtcNow, message)); return true; } catch (Exception exception) { this.OnError?.Invoke(this, new OnErrorEventArgs { Exception = exception }); throw; } } public bool SendWhisper(string message) { try { if (!IsConnected || WhisperQueueLength >= Options.WhisperQueueCapacity) { return false; } _throttlers.WhisperQueue.Add(new Tuple<DateTime, string>(DateTime.UtcNow, message)); return true; } catch (Exception exception) { this.OnError?.Invoke(this, new OnErrorEventArgs { Exception = exception }); throw; } } private void StartNetworkServices() { _networkServicesRunning = true; _networkTasks = new Task[3] { StartListenerTask(), _throttlers.StartSenderTask(), _throttlers.StartWhisperSenderTask() }.ToArray(); if (_networkTasks.Any((Task c) => c.IsFaulted)) { _networkServicesRunning = false; CleanupServices(); } } public Task SendAsync(string message) { return Task.Run(async delegate { await _writer.WriteLineAsync(message); await _writer.FlushAsync(); }); } private Task StartListenerTask() { return Task.Run(async delegate { while (IsConnected && _networkServicesRunning) { try { string text = await _reader.ReadLineAsync(); if (text == null && IsConnected) { Send("PING"); Task.Delay(500).Wait(); } this.OnMessage?.Invoke(this, new OnMessageEventArgs { Message = text }); } catch (Exception exception) { this.OnError?.Invoke(this, new OnErrorEventArgs { Exception = exception }); } } }); } private Task StartMonitorTask() { return Task.Run(delegate { bool flag = false; int num = 0; try { bool isConnected = IsConnected; while (!_tokenSource.IsCancellationRequested) { if (isConnected == IsConnected) { Thread.Sleep(200); if (!IsConnected) { NotConnectedCounter++; } else { num++; } if (num >= 300) { Send("PING"); num = 0; } switch (NotConnectedCounter) { case 25: case 75: case 150: case 300: case 600: Reconnect(); break; default: if (NotConnectedCounter >= 1200 && NotConnectedCounter % 600 == 0) { Reconnect(); } break; } if (NotConnectedCounter != 0 && IsConnected) { NotConnectedCounter = 0; } } else { this.OnStateChanged?.Invoke(this, new OnStateChangedEventArgs { IsConnected = IsConnected, WasConnected = isConnected }); if (IsConnected) { this.OnConnected?.Invoke(this, new OnConnectedEventArgs()); } if (!IsConnected && !_stopServices) { if (isConnected && Options.ReconnectionPolicy != null && !Options.ReconnectionPolicy.AreAttemptsComplete()) { flag = true; break; } this.OnDisconnected?.Invoke(this, new OnDisconnectedEventArgs()); } isConnected = IsConnected; } } } catch (Exception exception) { this.OnError?.Invoke(this, new OnErrorEventArgs { Exception = exception }); } if (flag && !_stopServices) { Reconnect(); } }, _tokenSource.Token); } private void CleanupServices() { _tokenSource.Cancel(); _tokenSource = new CancellationTokenSource(); _throttlers.TokenSource = _tokenSource; if (_stopServices) { Task[] networkTasks = _networkTasks; if (networkTasks != null && networkTasks.Length != 0 && !Task.WaitAll(_networkTasks, 15000)) { this.OnFatality?.Invoke(this, new OnFatalErrorEventArgs { Reason = "Fatal network error. Network services fail to shut down." }); _stopServices = false; _throttlers.Reconnecting = false; _networkServicesRunning = false; } } } public void WhisperThrottled(OnWhisperThrottledEventArgs eventArgs) { this.OnWhisperThrottled?.Invoke(this, eventArgs); } public void MessageThrottled(OnMessageThrottledEventArgs eventArgs) { this.OnMessageThrottled?.Invoke(this, eventArgs); } public void SendFailed(OnSendFailedEventArgs eventArgs) { this.OnSendFailed?.Invoke(this, eventArgs); } public void Error(OnErrorEventArgs eventArgs) { this.OnError?.Invoke(this, eventArgs); } public void Dispose() { Close(); _throttlers.ShouldDispose = true; _tokenSource.Cancel(); Thread.Sleep(500); _tokenSource.Dispose(); Client?.Dispose(); GC.Collect(); } } public class WebSocketClient : IClient { private int NotConnectedCounter; private readonly Throttlers _throttlers; private CancellationTokenSource _tokenSource = new CancellationTokenSource(); private bool _stopServices; private bool _networkServicesRunning; private Task[] _networkTasks; private Task _monitorTask; public TimeSpan DefaultKeepAliveInterval { get; set; } public int SendQueueLength => _throttlers.SendQueue.Count; public int WhisperQueueLength => _throttlers.WhisperQueue.Count; public bool IsConnected { get { ClientWebSocket client = Client; if (client == null) { return false; } return client.State == WebSocketState.Open; } } public IClientOptions Options { get; } public ClientWebSocket Client { get; private set; } private string Url { get; } public event EventHandler<OnConnectedEventArgs> OnConnected; public event EventHandler<OnDataEventArgs> OnData; public event EventHandler<OnDisconnectedEventArgs> OnDisconnected; public event EventHandler<OnErrorEventArgs> OnError; public event EventHandler<OnFatalErrorEventArgs> OnFatality; public event EventHandler<OnMessageEventArgs> OnMessage; public event EventHandler<OnMessageThrottledEventArgs> OnMessageThrottled; public event EventHandler<OnWhisperThrottledEventArgs> OnWhisperThrottled; public event EventHandler<OnSendFailedEventArgs> OnSendFailed; public event EventHandler<OnStateChangedEventArgs> OnStateChanged; public event EventHandler<OnReconnectedEventArgs> OnReconnected; public WebSocketClient(IClientOptions options = null) { Options = options ?? new ClientOptions(); switch (Options.ClientType) { case ClientType.Chat: Url = (Options.UseSsl ? "wss://irc-ws.chat.twitch.tv:443" : "ws://irc-ws.chat.twitch.tv:80"); break; case ClientType.PubSub: Url = (Options.UseSsl ? "wss://pubsub-edge.twitch.tv:443" : "ws://pubsub-edge.twitch.tv:80"); break; default: throw new ArgumentOutOfRangeException(); } _throttlers = new Throttlers(this, Options.ThrottlingPeriod, Options.WhisperThrottlingPeriod) { TokenSource = _tokenSource }; } private void InitializeClient() { Client?.Abort(); Client = new ClientWebSocket(); if (_monitorTask == null) { _monitorTask = StartMonitorTask(); } else if (_monitorTask.IsCompleted) { _monitorTask = StartMonitorTask(); } } public bool Open() { try { if (IsConnected) { return true; } InitializeClient(); Client.ConnectAsync(new Uri(Url), _tokenSource.Token).Wait(10000); if (!IsConnected) { return Open(); } StartNetworkServices(); return true; } catch (WebSocketException) { InitializeClient(); return false; } } public void Close(bool callDisconnect = true) { Client?.Abort(); _stopServices = callDisconnect; CleanupServices(); InitializeClient(); this.OnDisconnected?.Invoke(this, new OnDisconnectedEventArgs()); } public void Reconnect() { Task.Run(delegate { Task.Delay(20).Wait(); Close(); if (Open()) { this.OnReconnected?.Invoke(this, new OnReconnectedEventArgs()); } }); } public bool Send(string message) { try { if (!IsConnected || SendQueueLength >= Options.SendQueueCapacity) { return false; } _throttlers.SendQueue.Add(new Tuple<DateTime, string>(DateTime.UtcNow, message)); return true; } catch (Exception exception) { this.OnError?.Invoke(this, new OnErrorEventArgs { Exception = exception }); throw; } } public bool SendWhisper(string message) { try { if (!IsConnected || WhisperQueueLength >= Options.WhisperQueueCapacity) { return false; } _throttlers.WhisperQueue.Add(new Tuple<DateTime, string>(DateTime.UtcNow, message)); return true; } catch (Exception exception) { this.OnError?.Invoke(this, new OnErrorEventArgs { Exception = exception }); throw; } } private void StartNetworkServices() { _networkServicesRunning = true; _networkTasks = new Task[3] { StartListenerTask(), _throttlers.StartSenderTask(), _throttlers.StartWhisperSenderTask() }.ToArray(); if (_networkTasks.Any((Task c) => c.IsFaulted)) { _networkServicesRunning = false; CleanupServices(); } } public Task SendAsync(byte[] message) { return Client.SendAsync(new ArraySegment<byte>(message), WebSocketMessageType.Text, endOfMessage: true, _tokenSource.Token); } private Task StartListenerTask() { return Task.Run(async delegate { string message = ""; while (IsConnected && _networkServicesRunning) { byte[] buffer = new byte[1024]; WebSocketReceiveResult webSocketReceiveResult; try { webSocketReceiveResult = await Client.ReceiveAsync(new ArraySegment<byte>(buffer), _tokenSource.Token); } catch { InitializeClient(); break; } if (webSocketReceiveResult != null) { switch (webSocketReceiveResult.MessageType) { case WebSocketMessageType.Close: Close(); break; case WebSocketMessageType.Text: if (!webSocketReceiveResult.EndOfMessage) { message += Encoding.UTF8.GetString(buffer).TrimEnd(new char[1]); continue; } message += Encoding.UTF8.GetString(buffer).TrimEnd(new char[1]); this.OnMessage?.Invoke(this, new OnMessageEventArgs { Message = message }); break; default: throw new ArgumentOutOfRangeException(); case WebSocketMessageType.Binary: break; } message = ""; } } }); } private Task StartMonitorTask() { return Task.Run(delegate { bool flag = false; int num = 0; try { bool isConnected = IsConnected; while (!_tokenSource.IsCancellationRequested) { if (isConnected == IsConnected) { Thread.Sleep(200); if (!IsConnected) { NotConnectedCounter++; } else { num++; } if (num >= 300) { Send("PING"); num = 0; } switch (NotConnectedCounter) { case 25: case 75: case 150: case 300: case 600: Reconnect(); break; default: if (NotConnectedCounter >= 1200 && NotConnectedCounter % 600 == 0) { Reconnect(); } break; } if (NotConnectedCounter != 0 && IsConnected) { NotConnectedCounter = 0; } } else { this.OnStateChanged?.Invoke(this, new OnStateChangedEventArgs { IsConnected = (Client.State == WebSocketState.Open), WasConnected = isConnected }); if (IsConnected) { this.OnConnected?.Invoke(this, new OnConnectedEventArgs()); } if (!IsConnected && !_stopServices) { if (isConnected && Options.ReconnectionPolicy != null && !Options.ReconnectionPolicy.AreAttemptsComplete()) { flag = true; break; } this.OnDisconnected?.Invoke(this, new OnDisconnectedEventArgs()); if (Client.CloseStatus.HasValue && Client.CloseStatus != WebSocketCloseStatus.NormalClosure) { this.OnError?.Invoke(this, new OnErrorEventArgs { Exception = new Exception(Client.CloseStatus.ToString() + " " + Client.CloseStatusDescription) }); } } isConnected = IsConnected; } } } catch (Exception exception) { this.OnError?.Invoke(this, new OnErrorEventArgs { Exception = exception }); } if (flag && !_stopServices) { Reconnect(); } }, _tokenSource.Token); } private void CleanupServices() { _tokenSource.Cancel(); _tokenSource = new CancellationTokenSource(); _throttlers.TokenSource = _tokenSource; if (_stopServices) { Task[] networkTasks = _networkTasks; if (networkTasks != null && networkTasks.Length != 0 && !Task.WaitAll(_networkTasks, 15000)) { this.OnFatality?.Invoke(this, new OnFatalErrorEventArgs { Reason = "Fatal network error. Network services fail to shut down." }); _stopServices = false; _throttlers.Reconnecting = false; _networkServicesRunning = false; } } } public void WhisperThrottled(OnWhisperThrottledEventArgs eventArgs) { this.OnWhisperThrottled?.Invoke(this, eventArgs); } public void MessageThrottled(OnMessageThrottledEventArgs eventArgs) { this.OnMessageThrottled?.Invoke(this, eventArgs); } public void SendFailed(OnSendFailedEventArgs eventArgs) { this.OnSendFailed?.Invoke(this, eventArgs); } public void Error(OnErrorEventArgs eventArgs) { this.OnError?.Invoke(this, eventArgs); } public void Dispose() { Close(); _throttlers.ShouldDispose = true; _tokenSource.Cancel(); Thread.Sleep(500); _tokenSource.Dispose(); Client?.Dispose(); GC.Collect(); } } }
Microsoft.Bcl.AsyncInterfaces.dll
Decompiled a month agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.6.1", FrameworkDisplayName = ".NET Framework 4.6.1")] [assembly: CLSCompliant(true)] [assembly: DefaultDllImportSearchPaths(DllImportSearchPath.System32 | DllImportSearchPath.AssemblyDirectory)] [assembly: AssemblyDefaultAlias("Microsoft.Bcl.AsyncInterfaces")] [assembly: AssemblyMetadata(".NETFrameworkAssembly", "")] [assembly: AssemblyMetadata("Serviceable", "True")] [assembly: AssemblyMetadata("PreferInbox", "True")] [assembly: AssemblyMetadata("IsTrimmable", "True")] [assembly: AssemblyCompany("Microsoft Corporation")] [assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")] [assembly: AssemblyDescription("Provides the IAsyncEnumerable<T> and IAsyncDisposable interfaces and helper types for .NET Standard 2.0. This package is not required starting with .NET Standard 2.1 and .NET Core 3.0.\r\n\r\nCommonly Used Types:\r\nSystem.IAsyncDisposable\r\nSystem.Collections.Generic.IAsyncEnumerable\r\nSystem.Collections.Generic.IAsyncEnumerator")] [assembly: AssemblyFileVersion("6.0.21.52210")] [assembly: AssemblyInformationalVersion("6.0.0+4822e3c3aa77eb82b2fb33c9321f923cf11ddde6")] [assembly: AssemblyProduct("Microsoft® .NET")] [assembly: AssemblyTitle("Microsoft.Bcl.AsyncInterfaces")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/dotnet/runtime")] [assembly: AssemblyVersion("6.0.0.0")] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class IsReadOnlyAttribute : Attribute { } } namespace System { public interface IAsyncDisposable { ValueTask DisposeAsync(); } } namespace System.Collections.Generic { public interface IAsyncEnumerable<out T> { IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default(CancellationToken)); } public interface IAsyncEnumerator<out T> : IAsyncDisposable { T Current { get; } ValueTask<bool> MoveNextAsync(); } } namespace System.Runtime.CompilerServices { [StructLayout(LayoutKind.Auto)] public struct AsyncIteratorMethodBuilder { private AsyncTaskMethodBuilder _methodBuilder; private object _id; internal object ObjectIdForDebugger => _id ?? Interlocked.CompareExchange(ref _id, new object(), null) ?? _id; public static AsyncIteratorMethodBuilder Create() { AsyncIteratorMethodBuilder result = default(AsyncIteratorMethodBuilder); result._methodBuilder = AsyncTaskMethodBuilder.Create(); return result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void MoveNext<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine { _methodBuilder.Start(ref stateMachine); } public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { _methodBuilder.AwaitOnCompleted(ref awaiter, ref stateMachine); } public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { _methodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine); } public void Complete() { _methodBuilder.SetResult(); } } [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] public sealed class AsyncIteratorStateMachineAttribute : StateMachineAttribute { public AsyncIteratorStateMachineAttribute(Type stateMachineType) : base(stateMachineType) { } } [StructLayout(LayoutKind.Auto)] public readonly struct ConfiguredAsyncDisposable { private readonly IAsyncDisposable _source; private readonly bool _continueOnCapturedContext; internal ConfiguredAsyncDisposable(IAsyncDisposable source, bool continueOnCapturedContext) { _source = source; _continueOnCapturedContext = continueOnCapturedContext; } public ConfiguredValueTaskAwaitable DisposeAsync() { return _source.DisposeAsync().ConfigureAwait(_continueOnCapturedContext); } } [StructLayout(LayoutKind.Auto)] public readonly struct ConfiguredCancelableAsyncEnumerable<T> { [StructLayout(LayoutKind.Auto)] public readonly struct Enumerator { private readonly IAsyncEnumerator<T> _enumerator; private readonly bool _continueOnCapturedContext; public T Current => _enumerator.Current; internal Enumerator(IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext) { _enumerator = enumerator; _continueOnCapturedContext = continueOnCapturedContext; } public ConfiguredValueTaskAwaitable<bool> MoveNextAsync() { return _enumerator.MoveNextAsync().ConfigureAwait(_continueOnCapturedContext); } public ConfiguredValueTaskAwaitable DisposeAsync() { return _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext); } } private readonly IAsyncEnumerable<T> _enumerable; private readonly CancellationToken _cancellationToken; private readonly bool _continueOnCapturedContext; internal ConfiguredCancelableAsyncEnumerable(IAsyncEnumerable<T> enumerable, bool continueOnCapturedContext, CancellationToken cancellationToken) { _enumerable = enumerable; _continueOnCapturedContext = continueOnCapturedContext; _cancellationToken = cancellationToken; } public ConfiguredCancelableAsyncEnumerable<T> ConfigureAwait(bool continueOnCapturedContext) { return new ConfiguredCancelableAsyncEnumerable<T>(_enumerable, continueOnCapturedContext, _cancellationToken); } public ConfiguredCancelableAsyncEnumerable<T> WithCancellation(CancellationToken cancellationToken) { return new ConfiguredCancelableAsyncEnumerable<T>(_enumerable, _continueOnCapturedContext, cancellationToken); } public Enumerator GetAsyncEnumerator() { return new Enumerator(_enumerable.GetAsyncEnumerator(_cancellationToken), _continueOnCapturedContext); } } [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] public sealed class EnumeratorCancellationAttribute : Attribute { } } namespace System.Threading.Tasks { public static class TaskAsyncEnumerableExtensions { public static ConfiguredAsyncDisposable ConfigureAwait(this IAsyncDisposable source, bool continueOnCapturedContext) { return new ConfiguredAsyncDisposable(source, continueOnCapturedContext); } public static ConfiguredCancelableAsyncEnumerable<T> ConfigureAwait<T>(this IAsyncEnumerable<T> source, bool continueOnCapturedContext) { return new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext, default(CancellationToken)); } public static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>(this IAsyncEnumerable<T> source, CancellationToken cancellationToken) { return new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext: true, cancellationToken); } } } namespace System.Threading.Tasks.Sources { [StructLayout(LayoutKind.Auto)] public struct ManualResetValueTaskSourceCore<TResult> { private Action<object> _continuation; private object _continuationState; private ExecutionContext _executionContext; private object _capturedContext; private bool _completed; private TResult _result; private ExceptionDispatchInfo _error; private short _version; public bool RunContinuationsAsynchronously { get; set; } public short Version => _version; public void Reset() { _version++; _completed = false; _result = default(TResult); _error = null; _executionContext = null; _capturedContext = null; _continuation = null; _continuationState = null; } public void SetResult(TResult result) { _result = result; SignalCompletion(); } public void SetException(Exception error) { _error = ExceptionDispatchInfo.Capture(error); SignalCompletion(); } public ValueTaskSourceStatus GetStatus(short token) { ValidateToken(token); if (_continuation != null && _completed) { if (_error != null) { if (!(_error.SourceException is OperationCanceledException)) { return ValueTaskSourceStatus.Faulted; } return ValueTaskSourceStatus.Canceled; } return ValueTaskSourceStatus.Succeeded; } return ValueTaskSourceStatus.Pending; } public TResult GetResult(short token) { ValidateToken(token); if (!_completed) { throw new InvalidOperationException(); } _error?.Throw(); return _result; } public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) { if (continuation == null) { throw new ArgumentNullException("continuation"); } ValidateToken(token); if ((flags & ValueTaskSourceOnCompletedFlags.FlowExecutionContext) != 0) { _executionContext = ExecutionContext.Capture(); } if ((flags & ValueTaskSourceOnCompletedFlags.UseSchedulingContext) != 0) { SynchronizationContext current = SynchronizationContext.Current; if (current != null && current.GetType() != typeof(SynchronizationContext)) { _capturedContext = current; } else { TaskScheduler current2 = TaskScheduler.Current; if (current2 != TaskScheduler.Default) { _capturedContext = current2; } } } object obj = _continuation; if (obj == null) { _continuationState = state; obj = Interlocked.CompareExchange(ref _continuation, continuation, null); } if (obj == null) { return; } if (obj != System.Threading.Tasks.Sources.ManualResetValueTaskSourceCoreShared.s_sentinel) { throw new InvalidOperationException(); } object capturedContext = _capturedContext; if (capturedContext != null) { if (!(capturedContext is SynchronizationContext synchronizationContext)) { if (capturedContext is TaskScheduler scheduler) { Task.Factory.StartNew(continuation, state, CancellationToken.None, TaskCreationOptions.DenyChildAttach, scheduler); } } else { synchronizationContext.Post(delegate(object s) { Tuple<Action<object>, object> tuple = (Tuple<Action<object>, object>)s; tuple.Item1(tuple.Item2); }, Tuple.Create(continuation, state)); } } else { Task.Factory.StartNew(continuation, state, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); } } private void ValidateToken(short token) { if (token != _version) { throw new InvalidOperationException(); } } private void SignalCompletion() { if (_completed) { throw new InvalidOperationException(); } _completed = true; if (_continuation == null && Interlocked.CompareExchange(ref _continuation, System.Threading.Tasks.Sources.ManualResetValueTaskSourceCoreShared.s_sentinel, null) == null) { return; } if (_executionContext != null) { ExecutionContext.Run(_executionContext, delegate(object s) { ((ManualResetValueTaskSourceCore<TResult>)s).InvokeContinuation(); }, this); } else { InvokeContinuation(); } } private void InvokeContinuation() { object capturedContext = _capturedContext; if (capturedContext != null) { if (!(capturedContext is SynchronizationContext synchronizationContext)) { if (capturedContext is TaskScheduler scheduler) { Task.Factory.StartNew(_continuation, _continuationState, CancellationToken.None, TaskCreationOptions.DenyChildAttach, scheduler); } } else { synchronizationContext.Post(delegate(object s) { Tuple<Action<object>, object> tuple = (Tuple<Action<object>, object>)s; tuple.Item1(tuple.Item2); }, Tuple.Create(_continuation, _continuationState)); } } else if (RunContinuationsAsynchronously) { Task.Factory.StartNew(_continuation, _continuationState, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); } else { _continuation(_continuationState); } } } internal static class ManualResetValueTaskSourceCoreShared { internal static readonly Action<object> s_sentinel = CompletionSentinel; private static void CompletionSentinel(object _) { throw new InvalidOperationException(); } } }
Microsoft.Extensions.Logging.Abstractions.dll
Decompiled a month agousing System; using System.Buffers; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using System.Text; using System.Threading; using FxResources.Microsoft.Extensions.Logging.Abstractions; using Microsoft.CodeAnalysis; using Microsoft.Extensions.Internal; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: InternalsVisibleTo("Microsoft.Extensions.Logging.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] [assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")] [assembly: AssemblyMetadata(".NETFrameworkAssembly", "")] [assembly: AssemblyMetadata("Serviceable", "True")] [assembly: AssemblyMetadata("PreferInbox", "True")] [assembly: AssemblyDefaultAlias("Microsoft.Extensions.Logging.Abstractions")] [assembly: NeutralResourcesLanguage("en-US")] [assembly: CLSCompliant(true)] [assembly: AssemblyMetadata("IsTrimmable", "True")] [assembly: DefaultDllImportSearchPaths(DllImportSearchPath.System32 | DllImportSearchPath.AssemblyDirectory)] [assembly: AssemblyCompany("Microsoft Corporation")] [assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")] [assembly: AssemblyDescription("Logging abstractions for Microsoft.Extensions.Logging.\r\n\r\nCommonly Used Types:\r\nMicrosoft.Extensions.Logging.ILogger\r\nMicrosoft.Extensions.Logging.ILoggerFactory\r\nMicrosoft.Extensions.Logging.ILogger<TCategoryName>\r\nMicrosoft.Extensions.Logging.LogLevel\r\nMicrosoft.Extensions.Logging.Logger<T>\r\nMicrosoft.Extensions.Logging.LoggerMessage\r\nMicrosoft.Extensions.Logging.Abstractions.NullLogger")] [assembly: AssemblyFileVersion("7.0.22.51805")] [assembly: AssemblyInformationalVersion("7.0.0+d099f075e45d2aa6007a22b71b45a08758559f80")] [assembly: AssemblyProduct("Microsoft® .NET")] [assembly: AssemblyTitle("Microsoft.Extensions.Logging.Abstractions")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/dotnet/runtime")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("7.0.0.0")] [module: UnverifiableCode] [module: RefSafetyRules(11)] [module: System.Runtime.CompilerServices.NullablePublicOnly(true)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class IsReadOnlyAttribute : Attribute { } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class IsByRefLikeAttribute : Attribute { } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class NullablePublicOnlyAttribute : Attribute { public readonly bool IncludesInternals; public NullablePublicOnlyAttribute(bool P_0) { IncludesInternals = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace FxResources.Microsoft.Extensions.Logging.Abstractions { internal static class SR { } } namespace System { internal static class ThrowHelper { internal static void ThrowIfNull(object? argument, [CallerArgumentExpression("argument")] string? paramName = null) { if (argument == null) { Throw(paramName); } } private static void Throw(string paramName) { throw new ArgumentNullException(paramName); } } internal static class SR { private static readonly bool s_usingResourceKeys = AppContext.TryGetSwitch("System.Resources.UseSystemResourceKeys", out var isEnabled) && isEnabled; private static ResourceManager s_resourceManager; internal static ResourceManager ResourceManager => s_resourceManager ?? (s_resourceManager = new ResourceManager(typeof(SR))); internal static string UnexpectedNumberOfNamedParameters => GetResourceString("UnexpectedNumberOfNamedParameters"); private static bool UsingResourceKeys() { return s_usingResourceKeys; } internal static string GetResourceString(string resourceKey) { if (UsingResourceKeys()) { return resourceKey; } string result = null; try { result = ResourceManager.GetString(resourceKey); } catch (MissingManifestResourceException) { } return result; } internal static string GetResourceString(string resourceKey, string defaultString) { string resourceString = GetResourceString(resourceKey); if (!(resourceKey == resourceString) && resourceString != null) { return resourceString; } return defaultString; } internal static string Format(string resourceFormat, object? p1) { if (UsingResourceKeys()) { return string.Join(", ", resourceFormat, p1); } return string.Format(resourceFormat, p1); } internal static string Format(string resourceFormat, object? p1, object? p2) { if (UsingResourceKeys()) { return string.Join(", ", resourceFormat, p1, p2); } return string.Format(resourceFormat, p1, p2); } internal static string Format(string resourceFormat, object? p1, object? p2, object? p3) { if (UsingResourceKeys()) { return string.Join(", ", resourceFormat, p1, p2, p3); } return string.Format(resourceFormat, p1, p2, p3); } internal static string Format(string resourceFormat, params object?[]? args) { if (args != null) { if (UsingResourceKeys()) { return resourceFormat + ", " + string.Join(", ", args); } return string.Format(resourceFormat, args); } return resourceFormat; } internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1) { if (UsingResourceKeys()) { return string.Join(", ", resourceFormat, p1); } return string.Format(provider, resourceFormat, p1); } internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1, object? p2) { if (UsingResourceKeys()) { return string.Join(", ", resourceFormat, p1, p2); } return string.Format(provider, resourceFormat, p1, p2); } internal static string Format(IFormatProvider? provider, string resourceFormat, object? p1, object? p2, object? p3) { if (UsingResourceKeys()) { return string.Join(", ", resourceFormat, p1, p2, p3); } return string.Format(provider, resourceFormat, p1, p2, p3); } internal static string Format(IFormatProvider? provider, string resourceFormat, params object?[]? args) { if (args != null) { if (UsingResourceKeys()) { return resourceFormat + ", " + string.Join(", ", args); } return string.Format(provider, resourceFormat, args); } return resourceFormat; } } } namespace System.Diagnostics.CodeAnalysis { [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, Inherited = false)] internal sealed class AllowNullAttribute : Attribute { } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, Inherited = false)] internal sealed class DisallowNullAttribute : Attribute { } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, Inherited = false)] internal sealed class MaybeNullAttribute : Attribute { } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, Inherited = false)] internal sealed class NotNullAttribute : Attribute { } [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] internal sealed class MaybeNullWhenAttribute : Attribute { public bool ReturnValue { get; } public MaybeNullWhenAttribute(bool returnValue) { ReturnValue = returnValue; } } [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] internal sealed class NotNullWhenAttribute : Attribute { public bool ReturnValue { get; } public NotNullWhenAttribute(bool returnValue) { ReturnValue = returnValue; } } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] internal sealed class NotNullIfNotNullAttribute : Attribute { public string ParameterName { get; } public NotNullIfNotNullAttribute(string parameterName) { ParameterName = parameterName; } } [AttributeUsage(AttributeTargets.Method, Inherited = false)] internal sealed class DoesNotReturnAttribute : Attribute { } [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] internal sealed class DoesNotReturnIfAttribute : Attribute { public bool ParameterValue { get; } public DoesNotReturnIfAttribute(bool parameterValue) { ParameterValue = parameterValue; } } [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] internal sealed class MemberNotNullAttribute : Attribute { public string[] Members { get; } public MemberNotNullAttribute(string member) { Members = new string[1] { member }; } public MemberNotNullAttribute(params string[] members) { Members = members; } } [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] internal sealed class MemberNotNullWhenAttribute : Attribute { public bool ReturnValue { get; } public string[] Members { get; } public MemberNotNullWhenAttribute(bool returnValue, string member) { ReturnValue = returnValue; Members = new string[1] { member }; } public MemberNotNullWhenAttribute(bool returnValue, params string[] members) { ReturnValue = returnValue; Members = members; } } } namespace System.Runtime.InteropServices { [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] internal sealed class LibraryImportAttribute : Attribute { public string LibraryName { get; } public string? EntryPoint { get; set; } public StringMarshalling StringMarshalling { get; set; } public Type? StringMarshallingCustomType { get; set; } public bool SetLastError { get; set; } public LibraryImportAttribute(string libraryName) { LibraryName = libraryName; } } internal enum StringMarshalling { Custom, Utf8, Utf16 } } namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] internal sealed class CallerArgumentExpressionAttribute : Attribute { public string ParameterName { get; } public CallerArgumentExpressionAttribute(string parameterName) { ParameterName = parameterName; } } } namespace System.Text { internal ref struct ValueStringBuilder { private char[] _arrayToReturnToPool; private Span<char> _chars; private int _pos; public int Length { get { return _pos; } set { _pos = value; } } public int Capacity => _chars.Length; public ref char this[int index] => ref _chars[index]; public Span<char> RawChars => _chars; public ValueStringBuilder(Span<char> initialBuffer) { _arrayToReturnToPool = null; _chars = initialBuffer; _pos = 0; } public ValueStringBuilder(int initialCapacity) { _arrayToReturnToPool = ArrayPool<char>.Shared.Rent(initialCapacity); _chars = _arrayToReturnToPool; _pos = 0; } public void EnsureCapacity(int capacity) { if ((uint)capacity > (uint)_chars.Length) { Grow(capacity - _pos); } } public ref char GetPinnableReference() { return ref MemoryMarshal.GetReference(_chars); } public ref char GetPinnableReference(bool terminate) { if (terminate) { EnsureCapacity(Length + 1); _chars[Length] = '\0'; } return ref MemoryMarshal.GetReference(_chars); } public override string ToString() { string result = _chars.Slice(0, _pos).ToString(); Dispose(); return result; } public ReadOnlySpan<char> AsSpan(bool terminate) { if (terminate) { EnsureCapacity(Length + 1); _chars[Length] = '\0'; } return _chars.Slice(0, _pos); } public ReadOnlySpan<char> AsSpan() { return _chars.Slice(0, _pos); } public ReadOnlySpan<char> AsSpan(int start) { return _chars.Slice(start, _pos - start); } public ReadOnlySpan<char> AsSpan(int start, int length) { return _chars.Slice(start, length); } public bool TryCopyTo(Span<char> destination, out int charsWritten) { if (_chars.Slice(0, _pos).TryCopyTo(destination)) { charsWritten = _pos; Dispose(); return true; } charsWritten = 0; Dispose(); return false; } public void Insert(int index, char value, int count) { if (_pos > _chars.Length - count) { Grow(count); } int length = _pos - index; _chars.Slice(index, length).CopyTo(_chars.Slice(index + count)); _chars.Slice(index, count).Fill(value); _pos += count; } public void Insert(int index, string? s) { if (s != null) { int length = s.Length; if (_pos > _chars.Length - length) { Grow(length); } int length2 = _pos - index; _chars.Slice(index, length2).CopyTo(_chars.Slice(index + length)); s.AsSpan().CopyTo(_chars.Slice(index)); _pos += length; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Append(char c) { int pos = _pos; if ((uint)pos < (uint)_chars.Length) { _chars[pos] = c; _pos = pos + 1; } else { GrowAndAppend(c); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Append(string? s) { if (s != null) { int pos = _pos; if (s.Length == 1 && (uint)pos < (uint)_chars.Length) { _chars[pos] = s[0]; _pos = pos + 1; } else { AppendSlow(s); } } } private void AppendSlow(string s) { int pos = _pos; if (pos > _chars.Length - s.Length) { Grow(s.Length); } s.AsSpan().CopyTo(_chars.Slice(pos)); _pos += s.Length; } public void Append(char c, int count) { if (_pos > _chars.Length - count) { Grow(count); } Span<char> span = _chars.Slice(_pos, count); for (int i = 0; i < span.Length; i++) { span[i] = c; } _pos += count; } public unsafe void Append(char* value, int length) { int pos = _pos; if (pos > _chars.Length - length) { Grow(length); } Span<char> span = _chars.Slice(_pos, length); for (int i = 0; i < span.Length; i++) { span[i] = *(value++); } _pos += length; } public void Append(ReadOnlySpan<char> value) { int pos = _pos; if (pos > _chars.Length - value.Length) { Grow(value.Length); } value.CopyTo(_chars.Slice(_pos)); _pos += value.Length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span<char> AppendSpan(int length) { int pos = _pos; if (pos > _chars.Length - length) { Grow(length); } _pos = pos + length; return _chars.Slice(pos, length); } [MethodImpl(MethodImplOptions.NoInlining)] private void GrowAndAppend(char c) { Grow(1); Append(c); } [MethodImpl(MethodImplOptions.NoInlining)] private void Grow(int additionalCapacityBeyondPos) { int minimumLength = (int)Math.Max((uint)(_pos + additionalCapacityBeyondPos), Math.Min((uint)(_chars.Length * 2), 2147483591u)); char[] array = ArrayPool<char>.Shared.Rent(minimumLength); _chars.Slice(0, _pos).CopyTo(array); char[] arrayToReturnToPool = _arrayToReturnToPool; _chars = (_arrayToReturnToPool = array); if (arrayToReturnToPool != null) { ArrayPool<char>.Shared.Return(arrayToReturnToPool); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() { char[] arrayToReturnToPool = _arrayToReturnToPool; this = default(System.Text.ValueStringBuilder); if (arrayToReturnToPool != null) { ArrayPool<char>.Shared.Return(arrayToReturnToPool); } } } } namespace Microsoft.Extensions.Internal { internal static class TypeNameHelper { private readonly struct DisplayNameOptions { public bool FullName { get; } public bool IncludeGenericParameters { get; } public bool IncludeGenericParameterNames { get; } public char NestedTypeDelimiter { get; } public DisplayNameOptions(bool fullName, bool includeGenericParameterNames, bool includeGenericParameters, char nestedTypeDelimiter) { FullName = fullName; IncludeGenericParameters = includeGenericParameters; IncludeGenericParameterNames = includeGenericParameterNames; NestedTypeDelimiter = nestedTypeDelimiter; } } private const char DefaultNestedTypeDelimiter = '+'; private static readonly Dictionary<Type, string> _builtInTypeNames = new Dictionary<Type, string> { { typeof(void), "void" }, { typeof(bool), "bool" }, { typeof(byte), "byte" }, { typeof(char), "char" }, { typeof(decimal), "decimal" }, { typeof(double), "double" }, { typeof(float), "float" }, { typeof(int), "int" }, { typeof(long), "long" }, { typeof(object), "object" }, { typeof(sbyte), "sbyte" }, { typeof(short), "short" }, { typeof(string), "string" }, { typeof(uint), "uint" }, { typeof(ulong), "ulong" }, { typeof(ushort), "ushort" } }; [return: NotNullIfNotNull("item")] public static string? GetTypeDisplayName(object? item, bool fullName = true) { if (item != null) { return GetTypeDisplayName(item.GetType(), fullName); } return null; } public static string GetTypeDisplayName(Type type, bool fullName = true, bool includeGenericParameterNames = false, bool includeGenericParameters = true, char nestedTypeDelimiter = '+') { StringBuilder stringBuilder = new StringBuilder(); DisplayNameOptions options = new DisplayNameOptions(fullName, includeGenericParameterNames, includeGenericParameters, nestedTypeDelimiter); ProcessType(stringBuilder, type, in options); return stringBuilder.ToString(); } private static void ProcessType(StringBuilder builder, Type type, in DisplayNameOptions options) { if (type.IsGenericType) { Type[] genericArguments = type.GetGenericArguments(); ProcessGenericType(builder, type, genericArguments, genericArguments.Length, in options); return; } if (type.IsArray) { ProcessArrayType(builder, type, in options); return; } if (_builtInTypeNames.TryGetValue(type, out var value)) { builder.Append(value); return; } if (type.IsGenericParameter) { if (options.IncludeGenericParameterNames) { builder.Append(type.Name); } return; } string text = (options.FullName ? type.FullName : type.Name); builder.Append(text); if (options.NestedTypeDelimiter != '+') { builder.Replace('+', options.NestedTypeDelimiter, builder.Length - text.Length, text.Length); } } private static void ProcessArrayType(StringBuilder builder, Type type, in DisplayNameOptions options) { Type type2 = type; while (type2.IsArray) { type2 = type2.GetElementType(); } ProcessType(builder, type2, in options); while (type.IsArray) { builder.Append('['); builder.Append(',', type.GetArrayRank() - 1); builder.Append(']'); type = type.GetElementType(); } } private static void ProcessGenericType(StringBuilder builder, Type type, Type[] genericArguments, int length, in DisplayNameOptions options) { int num = 0; if (type.IsNested) { num = type.DeclaringType.GetGenericArguments().Length; } if (options.FullName) { if (type.IsNested) { ProcessGenericType(builder, type.DeclaringType, genericArguments, num, in options); builder.Append(options.NestedTypeDelimiter); } else if (!string.IsNullOrEmpty(type.Namespace)) { builder.Append(type.Namespace); builder.Append('.'); } } int num2 = type.Name.IndexOf('`'); if (num2 <= 0) { builder.Append(type.Name); return; } builder.Append(type.Name, 0, num2); if (!options.IncludeGenericParameters) { return; } builder.Append('<'); for (int i = num; i < length; i++) { ProcessType(builder, genericArguments[i], in options); if (i + 1 != length) { builder.Append(','); if (options.IncludeGenericParameterNames || !genericArguments[i + 1].IsGenericParameter) { builder.Append(' '); } } } builder.Append('>'); } } } namespace Microsoft.Extensions.Logging { public readonly struct EventId : IEquatable<EventId> { public int Id { get; } public string? Name { get; } public static implicit operator EventId(int i) { return new EventId(i); } public static bool operator ==(EventId left, EventId right) { return left.Equals(right); } public static bool operator !=(EventId left, EventId right) { return !left.Equals(right); } public EventId(int id, string? name = null) { Id = id; Name = name; } public override string ToString() { return Name ?? Id.ToString(); } public bool Equals(EventId other) { return Id == other.Id; } public override bool Equals([NotNullWhen(true)] object? obj) { if (obj == null) { return false; } if (obj is EventId other) { return Equals(other); } return false; } public override int GetHashCode() { return Id; } } internal readonly struct FormattedLogValues : IReadOnlyList<KeyValuePair<string, object?>>, IEnumerable<KeyValuePair<string, object?>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object?>> { internal const int MaxCachedFormatters = 1024; private const string NullFormat = "[null]"; private static int _count; private static ConcurrentDictionary<string, LogValuesFormatter> _formatters = new ConcurrentDictionary<string, LogValuesFormatter>(); private readonly LogValuesFormatter _formatter; private readonly object[] _values; private readonly string _originalMessage; internal LogValuesFormatter? Formatter => _formatter; public KeyValuePair<string, object?> this[int index] { get { if (index < 0 || index >= Count) { throw new IndexOutOfRangeException("index"); } if (index == Count - 1) { return new KeyValuePair<string, object>("{OriginalFormat}", _originalMessage); } return _formatter.GetValue(_values, index); } } public int Count { get { if (_formatter == null) { return 1; } return _formatter.ValueNames.Count + 1; } } public FormattedLogValues(string? format, params object?[]? values) { if (values != null && values.Length != 0 && format != null) { if (_count >= 1024) { if (!_formatters.TryGetValue(format, out _formatter)) { _formatter = new LogValuesFormatter(format); } } else { _formatter = _formatters.GetOrAdd(format, delegate(string f) { Interlocked.Increment(ref _count); return new LogValuesFormatter(f); }); } } else { _formatter = null; } _originalMessage = format ?? "[null]"; _values = values; } public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() { int i = 0; while (i < Count) { yield return this[i]; int num = i + 1; i = num; } } public override string ToString() { if (_formatter == null) { return _originalMessage; } return _formatter.Format(_values); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } public interface IExternalScopeProvider { void ForEachScope<TState>(Action<object?, TState> callback, TState state); IDisposable Push(object? state); } public interface ILogger { void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter); bool IsEnabled(LogLevel logLevel); IDisposable? BeginScope<TState>(TState state) where TState : notnull; } public interface ILoggerFactory : IDisposable { ILogger CreateLogger(string categoryName); void AddProvider(ILoggerProvider provider); } public interface ILoggerProvider : IDisposable { ILogger CreateLogger(string categoryName); } public interface ILogger<out TCategoryName> : ILogger { } public interface ISupportExternalScope { void SetScopeProvider(IExternalScopeProvider scopeProvider); } public class LogDefineOptions { public bool SkipEnabledCheck { get; set; } } public static class LoggerExtensions { private static readonly Func<FormattedLogValues, Exception, string> _messageFormatter = MessageFormatter; public static void LogDebug(this ILogger logger, EventId eventId, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Debug, eventId, exception, message, args); } public static void LogDebug(this ILogger logger, EventId eventId, string? message, params object?[] args) { logger.Log(LogLevel.Debug, eventId, message, args); } public static void LogDebug(this ILogger logger, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Debug, exception, message, args); } public static void LogDebug(this ILogger logger, string? message, params object?[] args) { logger.Log(LogLevel.Debug, message, args); } public static void LogTrace(this ILogger logger, EventId eventId, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Trace, eventId, exception, message, args); } public static void LogTrace(this ILogger logger, EventId eventId, string? message, params object?[] args) { logger.Log(LogLevel.Trace, eventId, message, args); } public static void LogTrace(this ILogger logger, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Trace, exception, message, args); } public static void LogTrace(this ILogger logger, string? message, params object?[] args) { logger.Log(LogLevel.Trace, message, args); } public static void LogInformation(this ILogger logger, EventId eventId, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Information, eventId, exception, message, args); } public static void LogInformation(this ILogger logger, EventId eventId, string? message, params object?[] args) { logger.Log(LogLevel.Information, eventId, message, args); } public static void LogInformation(this ILogger logger, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Information, exception, message, args); } public static void LogInformation(this ILogger logger, string? message, params object?[] args) { logger.Log(LogLevel.Information, message, args); } public static void LogWarning(this ILogger logger, EventId eventId, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Warning, eventId, exception, message, args); } public static void LogWarning(this ILogger logger, EventId eventId, string? message, params object?[] args) { logger.Log(LogLevel.Warning, eventId, message, args); } public static void LogWarning(this ILogger logger, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Warning, exception, message, args); } public static void LogWarning(this ILogger logger, string? message, params object?[] args) { logger.Log(LogLevel.Warning, message, args); } public static void LogError(this ILogger logger, EventId eventId, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Error, eventId, exception, message, args); } public static void LogError(this ILogger logger, EventId eventId, string? message, params object?[] args) { logger.Log(LogLevel.Error, eventId, message, args); } public static void LogError(this ILogger logger, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Error, exception, message, args); } public static void LogError(this ILogger logger, string? message, params object?[] args) { logger.Log(LogLevel.Error, message, args); } public static void LogCritical(this ILogger logger, EventId eventId, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Critical, eventId, exception, message, args); } public static void LogCritical(this ILogger logger, EventId eventId, string? message, params object?[] args) { logger.Log(LogLevel.Critical, eventId, message, args); } public static void LogCritical(this ILogger logger, Exception? exception, string? message, params object?[] args) { logger.Log(LogLevel.Critical, exception, message, args); } public static void LogCritical(this ILogger logger, string? message, params object?[] args) { logger.Log(LogLevel.Critical, message, args); } public static void Log(this ILogger logger, LogLevel logLevel, string? message, params object?[] args) { logger.Log(logLevel, 0, null, message, args); } public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, string? message, params object?[] args) { logger.Log(logLevel, eventId, null, message, args); } public static void Log(this ILogger logger, LogLevel logLevel, Exception? exception, string? message, params object?[] args) { logger.Log(logLevel, 0, exception, message, args); } public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, Exception? exception, string? message, params object?[] args) { System.ThrowHelper.ThrowIfNull(logger, "logger"); logger.Log(logLevel, eventId, new FormattedLogValues(message, args), exception, _messageFormatter); } public static IDisposable? BeginScope(this ILogger logger, string messageFormat, params object?[] args) { System.ThrowHelper.ThrowIfNull(logger, "logger"); return logger.BeginScope(new FormattedLogValues(messageFormat, args)); } private static string MessageFormatter(FormattedLogValues state, Exception error) { return state.ToString(); } } public class LoggerExternalScopeProvider : IExternalScopeProvider { private sealed class Scope : IDisposable { private readonly LoggerExternalScopeProvider _provider; private bool _isDisposed; public Scope Parent { get; } public object State { get; } internal Scope(LoggerExternalScopeProvider provider, object state, Scope parent) { _provider = provider; State = state; Parent = parent; } public override string ToString() { return State?.ToString(); } public void Dispose() { if (!_isDisposed) { _provider._currentScope.Value = Parent; _isDisposed = true; } } } private readonly AsyncLocal<Scope> _currentScope = new AsyncLocal<Scope>(); public void ForEachScope<TState>(Action<object?, TState> callback, TState state) { Action<object, TState> callback2 = callback; TState state2 = state; Report(_currentScope.Value); void Report(Scope? current) { if (current != null) { Report(current.Parent); callback2(current.State, state2); } } } public IDisposable Push(object? state) { Scope value = _currentScope.Value; Scope scope = new Scope(this, state, value); _currentScope.Value = scope; return scope; } } public static class LoggerFactoryExtensions { public static ILogger<T> CreateLogger<T>(this ILoggerFactory factory) { System.ThrowHelper.ThrowIfNull(factory, "factory"); return new Logger<T>(factory); } public static ILogger CreateLogger(this ILoggerFactory factory, Type type) { System.ThrowHelper.ThrowIfNull(factory, "factory"); System.ThrowHelper.ThrowIfNull(type, "type"); return factory.CreateLogger(TypeNameHelper.GetTypeDisplayName(type, fullName: true, includeGenericParameterNames: false, includeGenericParameters: false, '.')); } } public static class LoggerMessage { private readonly struct LogValues : IReadOnlyList<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object>> { public static readonly Func<LogValues, Exception, string> Callback = (LogValues state, Exception exception) => state.ToString(); private readonly LogValuesFormatter _formatter; public KeyValuePair<string, object> this[int index] { get { if (index == 0) { return new KeyValuePair<string, object>("{OriginalFormat}", _formatter.OriginalFormat); } throw new IndexOutOfRangeException("index"); } } public int Count => 1; public LogValues(LogValuesFormatter formatter) { _formatter = formatter; } public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { yield return this[0]; } public override string ToString() { return _formatter.Format(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } private readonly struct LogValues<T0> : IReadOnlyList<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object>> { public static readonly Func<LogValues<T0>, Exception, string> Callback = (LogValues<T0> state, Exception exception) => state.ToString(); private readonly LogValuesFormatter _formatter; private readonly T0 _value0; public KeyValuePair<string, object> this[int index] => index switch { 0 => new KeyValuePair<string, object>(_formatter.ValueNames[0], _value0), 1 => new KeyValuePair<string, object>("{OriginalFormat}", _formatter.OriginalFormat), _ => throw new IndexOutOfRangeException("index"), }; public int Count => 2; public LogValues(LogValuesFormatter formatter, T0 value0) { _formatter = formatter; _value0 = value0; } public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { int i = 0; while (i < Count) { yield return this[i]; int num = i + 1; i = num; } } public override string ToString() { return _formatter.Format(_value0); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } private readonly struct LogValues<T0, T1> : IReadOnlyList<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object>> { public static readonly Func<LogValues<T0, T1>, Exception, string> Callback = (LogValues<T0, T1> state, Exception exception) => state.ToString(); private readonly LogValuesFormatter _formatter; private readonly T0 _value0; private readonly T1 _value1; public KeyValuePair<string, object> this[int index] => index switch { 0 => new KeyValuePair<string, object>(_formatter.ValueNames[0], _value0), 1 => new KeyValuePair<string, object>(_formatter.ValueNames[1], _value1), 2 => new KeyValuePair<string, object>("{OriginalFormat}", _formatter.OriginalFormat), _ => throw new IndexOutOfRangeException("index"), }; public int Count => 3; public LogValues(LogValuesFormatter formatter, T0 value0, T1 value1) { _formatter = formatter; _value0 = value0; _value1 = value1; } public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { int i = 0; while (i < Count) { yield return this[i]; int num = i + 1; i = num; } } public override string ToString() { return _formatter.Format(_value0, _value1); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } private readonly struct LogValues<T0, T1, T2> : IReadOnlyList<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object>> { public static readonly Func<LogValues<T0, T1, T2>, Exception, string> Callback = (LogValues<T0, T1, T2> state, Exception exception) => state.ToString(); private readonly LogValuesFormatter _formatter; private readonly T0 _value0; private readonly T1 _value1; private readonly T2 _value2; public int Count => 4; public KeyValuePair<string, object> this[int index] => index switch { 0 => new KeyValuePair<string, object>(_formatter.ValueNames[0], _value0), 1 => new KeyValuePair<string, object>(_formatter.ValueNames[1], _value1), 2 => new KeyValuePair<string, object>(_formatter.ValueNames[2], _value2), 3 => new KeyValuePair<string, object>("{OriginalFormat}", _formatter.OriginalFormat), _ => throw new IndexOutOfRangeException("index"), }; public LogValues(LogValuesFormatter formatter, T0 value0, T1 value1, T2 value2) { _formatter = formatter; _value0 = value0; _value1 = value1; _value2 = value2; } public override string ToString() { return _formatter.Format(_value0, _value1, _value2); } public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { int i = 0; while (i < Count) { yield return this[i]; int num = i + 1; i = num; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } private readonly struct LogValues<T0, T1, T2, T3> : IReadOnlyList<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object>> { public static readonly Func<LogValues<T0, T1, T2, T3>, Exception, string> Callback = (LogValues<T0, T1, T2, T3> state, Exception exception) => state.ToString(); private readonly LogValuesFormatter _formatter; private readonly T0 _value0; private readonly T1 _value1; private readonly T2 _value2; private readonly T3 _value3; public int Count => 5; public KeyValuePair<string, object> this[int index] => index switch { 0 => new KeyValuePair<string, object>(_formatter.ValueNames[0], _value0), 1 => new KeyValuePair<string, object>(_formatter.ValueNames[1], _value1), 2 => new KeyValuePair<string, object>(_formatter.ValueNames[2], _value2), 3 => new KeyValuePair<string, object>(_formatter.ValueNames[3], _value3), 4 => new KeyValuePair<string, object>("{OriginalFormat}", _formatter.OriginalFormat), _ => throw new IndexOutOfRangeException("index"), }; public LogValues(LogValuesFormatter formatter, T0 value0, T1 value1, T2 value2, T3 value3) { _formatter = formatter; _value0 = value0; _value1 = value1; _value2 = value2; _value3 = value3; } private object[] ToArray() { return new object[4] { _value0, _value1, _value2, _value3 }; } public override string ToString() { return _formatter.FormatWithOverwrite(ToArray()); } public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { int i = 0; while (i < Count) { yield return this[i]; int num = i + 1; i = num; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } private readonly struct LogValues<T0, T1, T2, T3, T4> : IReadOnlyList<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object>> { public static readonly Func<LogValues<T0, T1, T2, T3, T4>, Exception, string> Callback = (LogValues<T0, T1, T2, T3, T4> state, Exception exception) => state.ToString(); private readonly LogValuesFormatter _formatter; private readonly T0 _value0; private readonly T1 _value1; private readonly T2 _value2; private readonly T3 _value3; private readonly T4 _value4; public int Count => 6; public KeyValuePair<string, object> this[int index] => index switch { 0 => new KeyValuePair<string, object>(_formatter.ValueNames[0], _value0), 1 => new KeyValuePair<string, object>(_formatter.ValueNames[1], _value1), 2 => new KeyValuePair<string, object>(_formatter.ValueNames[2], _value2), 3 => new KeyValuePair<string, object>(_formatter.ValueNames[3], _value3), 4 => new KeyValuePair<string, object>(_formatter.ValueNames[4], _value4), 5 => new KeyValuePair<string, object>("{OriginalFormat}", _formatter.OriginalFormat), _ => throw new IndexOutOfRangeException("index"), }; public LogValues(LogValuesFormatter formatter, T0 value0, T1 value1, T2 value2, T3 value3, T4 value4) { _formatter = formatter; _value0 = value0; _value1 = value1; _value2 = value2; _value3 = value3; _value4 = value4; } private object[] ToArray() { return new object[5] { _value0, _value1, _value2, _value3, _value4 }; } public override string ToString() { return _formatter.FormatWithOverwrite(ToArray()); } public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { int i = 0; while (i < Count) { yield return this[i]; int num = i + 1; i = num; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } private readonly struct LogValues<T0, T1, T2, T3, T4, T5> : IReadOnlyList<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IReadOnlyCollection<KeyValuePair<string, object>> { public static readonly Func<LogValues<T0, T1, T2, T3, T4, T5>, Exception, string> Callback = (LogValues<T0, T1, T2, T3, T4, T5> state, Exception exception) => state.ToString(); private readonly LogValuesFormatter _formatter; private readonly T0 _value0; private readonly T1 _value1; private readonly T2 _value2; private readonly T3 _value3; private readonly T4 _value4; private readonly T5 _value5; public int Count => 7; public KeyValuePair<string, object> this[int index] => index switch { 0 => new KeyValuePair<string, object>(_formatter.ValueNames[0], _value0), 1 => new KeyValuePair<string, object>(_formatter.ValueNames[1], _value1), 2 => new KeyValuePair<string, object>(_formatter.ValueNames[2], _value2), 3 => new KeyValuePair<string, object>(_formatter.ValueNames[3], _value3), 4 => new KeyValuePair<string, object>(_formatter.ValueNames[4], _value4), 5 => new KeyValuePair<string, object>(_formatter.ValueNames[5], _value5), 6 => new KeyValuePair<string, object>("{OriginalFormat}", _formatter.OriginalFormat), _ => throw new IndexOutOfRangeException("index"), }; public LogValues(LogValuesFormatter formatter, T0 value0, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) { _formatter = formatter; _value0 = value0; _value1 = value1; _value2 = value2; _value3 = value3; _value4 = value4; _value5 = value5; } private object[] ToArray() { return new object[6] { _value0, _value1, _value2, _value3, _value4, _value5 }; } public override string ToString() { return _formatter.FormatWithOverwrite(ToArray()); } public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { int i = 0; while (i < Count) { yield return this[i]; int num = i + 1; i = num; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } public static Func<ILogger, IDisposable?> DefineScope(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 0); LogValues logValues = new LogValues(formatter); return (ILogger logger) => logger.BeginScope(logValues); } public static Func<ILogger, T1, IDisposable?> DefineScope<T1>(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 1); return (ILogger logger, T1 arg1) => logger.BeginScope(new LogValues<T1>(formatter, arg1)); } public static Func<ILogger, T1, T2, IDisposable?> DefineScope<T1, T2>(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 2); return (ILogger logger, T1 arg1, T2 arg2) => logger.BeginScope(new LogValues<T1, T2>(formatter, arg1, arg2)); } public static Func<ILogger, T1, T2, T3, IDisposable?> DefineScope<T1, T2, T3>(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 3); return (ILogger logger, T1 arg1, T2 arg2, T3 arg3) => logger.BeginScope(new LogValues<T1, T2, T3>(formatter, arg1, arg2, arg3)); } public static Func<ILogger, T1, T2, T3, T4, IDisposable?> DefineScope<T1, T2, T3, T4>(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 4); return (ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4) => logger.BeginScope(new LogValues<T1, T2, T3, T4>(formatter, arg1, arg2, arg3, arg4)); } public static Func<ILogger, T1, T2, T3, T4, T5, IDisposable?> DefineScope<T1, T2, T3, T4, T5>(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 5); return (ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) => logger.BeginScope(new LogValues<T1, T2, T3, T4, T5>(formatter, arg1, arg2, arg3, arg4, arg5)); } public static Func<ILogger, T1, T2, T3, T4, T5, T6, IDisposable?> DefineScope<T1, T2, T3, T4, T5, T6>(string formatString) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 6); return (ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) => logger.BeginScope(new LogValues<T1, T2, T3, T4, T5, T6>(formatter, arg1, arg2, arg3, arg4, arg5, arg6)); } public static Action<ILogger, Exception?> Define(LogLevel logLevel, EventId eventId, string formatString) { return Define(logLevel, eventId, formatString, null); } public static Action<ILogger, Exception?> Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 0); if (options != null && options.SkipEnabledCheck) { return Log; } return delegate(ILogger logger, Exception exception) { if (logger.IsEnabled(logLevel)) { Log(logger, exception); } }; void Log(ILogger logger, Exception exception) { logger.Log(logLevel, eventId, new LogValues(formatter), exception, LogValues.Callback); } } public static Action<ILogger, T1, Exception?> Define<T1>(LogLevel logLevel, EventId eventId, string formatString) { return Define<T1>(logLevel, eventId, formatString, null); } public static Action<ILogger, T1, Exception?> Define<T1>(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 1); if (options != null && options.SkipEnabledCheck) { return Log; } return delegate(ILogger logger, T1 arg1, Exception exception) { if (logger.IsEnabled(logLevel)) { Log(logger, arg1, exception); } }; void Log(ILogger logger, T1 arg1, Exception exception) { logger.Log(logLevel, eventId, new LogValues<T1>(formatter, arg1), exception, LogValues<T1>.Callback); } } public static Action<ILogger, T1, T2, Exception?> Define<T1, T2>(LogLevel logLevel, EventId eventId, string formatString) { return Define<T1, T2>(logLevel, eventId, formatString, null); } public static Action<ILogger, T1, T2, Exception?> Define<T1, T2>(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 2); if (options != null && options.SkipEnabledCheck) { return Log; } return delegate(ILogger logger, T1 arg1, T2 arg2, Exception exception) { if (logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, exception); } }; void Log(ILogger logger, T1 arg1, T2 arg2, Exception exception) { logger.Log(logLevel, eventId, new LogValues<T1, T2>(formatter, arg1, arg2), exception, LogValues<T1, T2>.Callback); } } public static Action<ILogger, T1, T2, T3, Exception?> Define<T1, T2, T3>(LogLevel logLevel, EventId eventId, string formatString) { return Define<T1, T2, T3>(logLevel, eventId, formatString, null); } public static Action<ILogger, T1, T2, T3, Exception?> Define<T1, T2, T3>(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 3); if (options != null && options.SkipEnabledCheck) { return Log; } return delegate(ILogger logger, T1 arg1, T2 arg2, T3 arg3, Exception exception) { if (logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, arg3, exception); } }; void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, Exception exception) { logger.Log(logLevel, eventId, new LogValues<T1, T2, T3>(formatter, arg1, arg2, arg3), exception, LogValues<T1, T2, T3>.Callback); } } public static Action<ILogger, T1, T2, T3, T4, Exception?> Define<T1, T2, T3, T4>(LogLevel logLevel, EventId eventId, string formatString) { return Define<T1, T2, T3, T4>(logLevel, eventId, formatString, null); } public static Action<ILogger, T1, T2, T3, T4, Exception?> Define<T1, T2, T3, T4>(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 4); if (options != null && options.SkipEnabledCheck) { return Log; } return delegate(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception exception) { if (logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, arg3, arg4, exception); } }; void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception exception) { logger.Log(logLevel, eventId, new LogValues<T1, T2, T3, T4>(formatter, arg1, arg2, arg3, arg4), exception, LogValues<T1, T2, T3, T4>.Callback); } } public static Action<ILogger, T1, T2, T3, T4, T5, Exception?> Define<T1, T2, T3, T4, T5>(LogLevel logLevel, EventId eventId, string formatString) { return Define<T1, T2, T3, T4, T5>(logLevel, eventId, formatString, null); } public static Action<ILogger, T1, T2, T3, T4, T5, Exception?> Define<T1, T2, T3, T4, T5>(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 5); if (options != null && options.SkipEnabledCheck) { return Log; } return delegate(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Exception exception) { if (logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, arg3, arg4, arg5, exception); } }; void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Exception exception) { logger.Log(logLevel, eventId, new LogValues<T1, T2, T3, T4, T5>(formatter, arg1, arg2, arg3, arg4, arg5), exception, LogValues<T1, T2, T3, T4, T5>.Callback); } } public static Action<ILogger, T1, T2, T3, T4, T5, T6, Exception?> Define<T1, T2, T3, T4, T5, T6>(LogLevel logLevel, EventId eventId, string formatString) { return Define<T1, T2, T3, T4, T5, T6>(logLevel, eventId, formatString, null); } public static Action<ILogger, T1, T2, T3, T4, T5, T6, Exception?> Define<T1, T2, T3, T4, T5, T6>(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, 6); if (options != null && options.SkipEnabledCheck) { return Log; } return delegate(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, Exception exception) { if (logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, arg3, arg4, arg5, arg6, exception); } }; void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, Exception exception) { logger.Log(logLevel, eventId, new LogValues<T1, T2, T3, T4, T5, T6>(formatter, arg1, arg2, arg3, arg4, arg5, arg6), exception, LogValues<T1, T2, T3, T4, T5, T6>.Callback); } } private static LogValuesFormatter CreateLogValuesFormatter(string formatString, int expectedNamedParameterCount) { LogValuesFormatter logValuesFormatter = new LogValuesFormatter(formatString); int count = logValuesFormatter.ValueNames.Count; if (count != expectedNamedParameterCount) { throw new ArgumentException(System.SR.Format(System.SR.UnexpectedNumberOfNamedParameters, formatString, expectedNamedParameterCount, count)); } return logValuesFormatter; } } [AttributeUsage(AttributeTargets.Method)] public sealed class LoggerMessageAttribute : Attribute { public int EventId { get; set; } = -1; public string? EventName { get; set; } public LogLevel Level { get; set; } = LogLevel.None; public string Message { get; set; } = ""; public bool SkipEnabledCheck { get; set; } public LoggerMessageAttribute() { } public LoggerMessageAttribute(int eventId, LogLevel level, string message) { EventId = eventId; Level = level; Message = message; } } public class Logger<T> : ILogger<T>, ILogger { private readonly ILogger _logger; public Logger(ILoggerFactory factory) { System.ThrowHelper.ThrowIfNull(factory, "factory"); _logger = factory.CreateLogger(TypeNameHelper.GetTypeDisplayName(typeof(T), fullName: true, includeGenericParameterNames: false, includeGenericParameters: false, '.')); } IDisposable ILogger.BeginScope<TState>(TState state) { return _logger.BeginScope(state); } bool ILogger.IsEnabled(LogLevel logLevel) { return _logger.IsEnabled(logLevel); } void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) { _logger.Log(logLevel, eventId, state, exception, formatter); } } public enum LogLevel { Trace, Debug, Information, Warning, Error, Critical, None } internal sealed class LogValuesFormatter { private const string NullValue = "(null)"; private static readonly char[] FormatDelimiters = new char[2] { ',', ':' }; private readonly string _format; private readonly List<string> _valueNames = new List<string>(); public string OriginalFormat { get; private set; } public List<string> ValueNames => _valueNames; public LogValuesFormatter(string format) { System.ThrowHelper.ThrowIfNull(format, "format"); OriginalFormat = format; Span<char> initialBuffer = stackalloc char[256]; System.Text.ValueStringBuilder valueStringBuilder = new System.Text.ValueStringBuilder(initialBuffer); int num = 0; int length = format.Length; while (num < length) { int num2 = FindBraceIndex(format, '{', num, length); if (num == 0 && num2 == length) { _format = format; return; } int num3 = FindBraceIndex(format, '}', num2, length); if (num3 == length) { valueStringBuilder.Append(format.AsSpan(num, length - num)); num = length; continue; } int num4 = FindIndexOfAny(format, FormatDelimiters, num2, num3); valueStringBuilder.Append(format.AsSpan(num, num2 - num + 1)); valueStringBuilder.Append(_valueNames.Count.ToString()); _valueNames.Add(format.Substring(num2 + 1, num4 - num2 - 1)); valueStringBuilder.Append(format.AsSpan(num4, num3 - num4 + 1)); num = num3 + 1; } _format = valueStringBuilder.ToString(); } private static int FindBraceIndex(string format, char brace, int startIndex, int endIndex) { int result = endIndex; int i = startIndex; int num = 0; for (; i < endIndex; i++) { if (num > 0 && format[i] != brace) { if (num % 2 != 0) { break; } num = 0; result = endIndex; } else { if (format[i] != brace) { continue; } if (brace == '}') { if (num == 0) { result = i; } } else { result = i; } num++; } } return result; } private static int FindIndexOfAny(string format, char[] chars, int startIndex, int endIndex) { int num = format.IndexOfAny(chars, startIndex, endIndex - startIndex); if (num != -1) { return num; } return endIndex; } public string Format(object?[]? values) { object[] array = values; if (values != null) { for (int i = 0; i < values.Length; i++) { object obj = FormatArgument(values[i]); if (obj != values[i]) { array = new object[values.Length]; Array.Copy(values, array, i); array[i++] = obj; for (; i < values.Length; i++) { array[i] = FormatArgument(values[i]); } break; } } } return string.Format(CultureInfo.InvariantCulture, _format, array ?? Array.Empty<object>()); } internal string FormatWithOverwrite(object?[]? values) { if (values != null) { for (int i = 0; i < values.Length; i++) { values[i] = FormatArgument(values[i]); } } return string.Format(CultureInfo.InvariantCulture, _format, values ?? Array.Empty<object>()); } internal string Format() { return _format; } internal string Format(object? arg0) { return string.Format(CultureInfo.InvariantCulture, _format, FormatArgument(arg0)); } internal string Format(object? arg0, object? arg1) { return string.Format(CultureInfo.InvariantCulture, _format, FormatArgument(arg0), FormatArgument(arg1)); } internal string Format(object? arg0, object? arg1, object? arg2) { return string.Format(CultureInfo.InvariantCulture, _format, FormatArgument(arg0), FormatArgument(arg1), FormatArgument(arg2)); } public KeyValuePair<string, object?> GetValue(object?[] values, int index) { if (index < 0 || index > _valueNames.Count) { throw new IndexOutOfRangeException("index"); } if (_valueNames.Count > index) { return new KeyValuePair<string, object>(_valueNames[index], values[index]); } return new KeyValuePair<string, object>("{OriginalFormat}", OriginalFormat); } public IEnumerable<KeyValuePair<string, object?>> GetValues(object[] values) { KeyValuePair<string, object>[] array = new KeyValuePair<string, object>[values.Length + 1]; for (int i = 0; i != _valueNames.Count; i++) { array[i] = new KeyValuePair<string, object>(_valueNames[i], values[i]); } array[^1] = new KeyValuePair<string, object>("{OriginalFormat}", OriginalFormat); return array; } private object FormatArgument(object value) { if (value == null) { return "(null)"; } if (value is string) { return value; } if (value is IEnumerable enumerable) { Span<char> initialBuffer = stackalloc char[256]; System.Text.ValueStringBuilder valueStringBuilder = new System.Text.ValueStringBuilder(initialBuffer); bool flag = true; foreach (object item in enumerable) { if (!flag) { valueStringBuilder.Append(", "); } valueStringBuilder.Append((item != null) ? item.ToString() : "(null)"); flag = false; } return valueStringBuilder.ToString(); } return value; } } internal sealed class NullExternalScopeProvider : IExternalScopeProvider { public static IExternalScopeProvider Instance { get; } = new NullExternalScopeProvider(); private NullExternalScopeProvider() { } void IExternalScopeProvider.ForEachScope<TState>(Action<object, TState> callback, TState state) { } IDisposable IExternalScopeProvider.Push(object state) { return NullScope.Instance; } } internal sealed class NullScope : IDisposable { public static NullScope Instance { get; } = new NullScope(); private NullScope() { } public void Dispose() { } } } namespace Microsoft.Extensions.Logging.Abstractions { public readonly struct LogEntry<TState> { public LogLevel LogLevel { get; } public string Category { get; } public EventId EventId { get; } public TState State { get; } public Exception? Exception { get; } public Func<TState, Exception?, string> Formatter { get; } public LogEntry(LogLevel logLevel, string category, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) { LogLevel = logLevel; Category = category; EventId = eventId; State = state; Exception = exception; Formatter = formatter; } } public class NullLogger : ILogger { public static NullLogger Instance { get; } = new NullLogger(); private NullLogger() { } public IDisposable BeginScope<TState>(TState state) where TState : notnull { return NullScope.Instance; } public bool IsEnabled(LogLevel logLevel) { return false; } public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) { } } public class NullLoggerFactory : ILoggerFactory, IDisposable { public static readonly NullLoggerFactory Instance = new NullLoggerFactory(); public ILogger CreateLogger(string name) { return NullLogger.Instance; } public void AddProvider(ILoggerProvider provider) { } public void Dispose() { } } public class NullLoggerProvider : ILoggerProvider, IDisposable { public static NullLoggerProvider Instance { get; } = new NullLoggerProvider(); private NullLoggerProvider() { } public ILogger CreateLogger(string categoryName) { return NullLogger.Instance; } public void Dispose() { } } public class NullLogger<T> : ILogger<T>, ILogger { public static readonly NullLogger<T> Instance = new NullLogger<T>(); public IDisposable BeginScope<TState>(TState state) where TState : notnull { return NullScope.Instance; } public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) { } public bool IsEnabled(LogLevel logLevel) { return false; } } }
System.Memory.dll
Decompiled a month ago
The result has been truncated due to the large size, download it to view full contents!
using System; using System.Buffers; using System.Buffers.Binary; using System.Buffers.Text; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Globalization; using System.Numerics; using System.Numerics.Hashing; using System.Reflection; using System.Resources; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security; using System.Security.Permissions; using System.Text; using FxResources.System.Memory; using Microsoft.CodeAnalysis; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: NeutralResourcesLanguage("en-US")] [assembly: AssemblyTitle("System.Memory")] [assembly: AssemblyDescription("System.Memory")] [assembly: AssemblyDefaultAlias("System.Memory")] [assembly: AssemblyCompany("Microsoft Corporation")] [assembly: AssemblyProduct("Microsoft® .NET Framework")] [assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")] [assembly: AssemblyFileVersion("4.6.31308.01")] [assembly: AssemblyInformationalVersion("4.6.31308.01 @BuiltBy: cloudtest-841353dfc000000 @Branch: release/2.1-MSRC @SrcCode: https://github.com/dotnet/corefx/tree/32b491939fbd125f304031c35038b1e14b4e3958")] [assembly: CLSCompliant(true)] [assembly: AssemblyMetadata(".NETFrameworkAssembly", "")] [assembly: AssemblyMetadata("Serviceable", "True")] [assembly: AssemblyMetadata("PreferInbox", "True")] [assembly: DefaultDllImportSearchPaths(DllImportSearchPath.System32 | DllImportSearchPath.AssemblyDirectory)] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("4.0.1.2")] [module: UnverifiableCode] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class IsReadOnlyAttribute : Attribute { } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class IsByRefLikeAttribute : Attribute { } } namespace FxResources.System.Memory { internal static class SR { } } namespace System { public readonly struct SequencePosition : IEquatable<SequencePosition> { private readonly object _object; private readonly int _integer; public SequencePosition(object @object, int integer) { _object = @object; _integer = integer; } [EditorBrowsable(EditorBrowsableState.Never)] public object GetObject() { return _object; } [EditorBrowsable(EditorBrowsableState.Never)] public int GetInteger() { return _integer; } public bool Equals(SequencePosition other) { if (_integer == other._integer) { return object.Equals(_object, other._object); } return false; } [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { if (obj is SequencePosition other) { return Equals(other); } return false; } [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { return HashHelpers.Combine(_object?.GetHashCode() ?? 0, _integer); } } internal static class ThrowHelper { internal static void ThrowArgumentNullException(System.ExceptionArgument argument) { throw CreateArgumentNullException(argument); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentNullException(System.ExceptionArgument argument) { return new ArgumentNullException(argument.ToString()); } internal static void ThrowArrayTypeMismatchException() { throw CreateArrayTypeMismatchException(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArrayTypeMismatchException() { return new ArrayTypeMismatchException(); } internal static void ThrowArgumentException_InvalidTypeWithPointersNotSupported(Type type) { throw CreateArgumentException_InvalidTypeWithPointersNotSupported(type); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentException_InvalidTypeWithPointersNotSupported(Type type) { return new ArgumentException(System.SR.Format(System.SR.Argument_InvalidTypeWithPointersNotSupported, type)); } internal static void ThrowArgumentException_DestinationTooShort() { throw CreateArgumentException_DestinationTooShort(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentException_DestinationTooShort() { return new ArgumentException(System.SR.Argument_DestinationTooShort); } internal static void ThrowIndexOutOfRangeException() { throw CreateIndexOutOfRangeException(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateIndexOutOfRangeException() { return new IndexOutOfRangeException(); } internal static void ThrowArgumentOutOfRangeException() { throw CreateArgumentOutOfRangeException(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentOutOfRangeException() { return new ArgumentOutOfRangeException(); } internal static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument) { throw CreateArgumentOutOfRangeException(argument); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentOutOfRangeException(System.ExceptionArgument argument) { return new ArgumentOutOfRangeException(argument.ToString()); } internal static void ThrowArgumentOutOfRangeException_PrecisionTooLarge() { throw CreateArgumentOutOfRangeException_PrecisionTooLarge(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentOutOfRangeException_PrecisionTooLarge() { return new ArgumentOutOfRangeException("precision", System.SR.Format(System.SR.Argument_PrecisionTooLarge, (byte)99)); } internal static void ThrowArgumentOutOfRangeException_SymbolDoesNotFit() { throw CreateArgumentOutOfRangeException_SymbolDoesNotFit(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentOutOfRangeException_SymbolDoesNotFit() { return new ArgumentOutOfRangeException("symbol", System.SR.Argument_BadFormatSpecifier); } internal static void ThrowInvalidOperationException() { throw CreateInvalidOperationException(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateInvalidOperationException() { return new InvalidOperationException(); } internal static void ThrowInvalidOperationException_OutstandingReferences() { throw CreateInvalidOperationException_OutstandingReferences(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateInvalidOperationException_OutstandingReferences() { return new InvalidOperationException(System.SR.OutstandingReferences); } internal static void ThrowInvalidOperationException_UnexpectedSegmentType() { throw CreateInvalidOperationException_UnexpectedSegmentType(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateInvalidOperationException_UnexpectedSegmentType() { return new InvalidOperationException(System.SR.UnexpectedSegmentType); } internal static void ThrowInvalidOperationException_EndPositionNotReached() { throw CreateInvalidOperationException_EndPositionNotReached(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateInvalidOperationException_EndPositionNotReached() { return new InvalidOperationException(System.SR.EndPositionNotReached); } internal static void ThrowArgumentOutOfRangeException_PositionOutOfRange() { throw CreateArgumentOutOfRangeException_PositionOutOfRange(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentOutOfRangeException_PositionOutOfRange() { return new ArgumentOutOfRangeException("position"); } internal static void ThrowArgumentOutOfRangeException_OffsetOutOfRange() { throw CreateArgumentOutOfRangeException_OffsetOutOfRange(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentOutOfRangeException_OffsetOutOfRange() { return new ArgumentOutOfRangeException("offset"); } internal static void ThrowObjectDisposedException_ArrayMemoryPoolBuffer() { throw CreateObjectDisposedException_ArrayMemoryPoolBuffer(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateObjectDisposedException_ArrayMemoryPoolBuffer() { return new ObjectDisposedException("ArrayMemoryPoolBuffer"); } internal static void ThrowFormatException_BadFormatSpecifier() { throw CreateFormatException_BadFormatSpecifier(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateFormatException_BadFormatSpecifier() { return new FormatException(System.SR.Argument_BadFormatSpecifier); } internal static void ThrowArgumentException_OverlapAlignmentMismatch() { throw CreateArgumentException_OverlapAlignmentMismatch(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateArgumentException_OverlapAlignmentMismatch() { return new ArgumentException(System.SR.Argument_OverlapAlignmentMismatch); } internal static void ThrowNotSupportedException() { throw CreateThrowNotSupportedException(); } [MethodImpl(MethodImplOptions.NoInlining)] private static Exception CreateThrowNotSupportedException() { return new NotSupportedException(); } public static bool TryFormatThrowFormatException(out int bytesWritten) { bytesWritten = 0; ThrowFormatException_BadFormatSpecifier(); return false; } public static bool TryParseThrowFormatException<T>(out T value, out int bytesConsumed) { value = default(T); bytesConsumed = 0; ThrowFormatException_BadFormatSpecifier(); return false; } public static void ThrowArgumentValidationException<T>(ReadOnlySequenceSegment<T> startSegment, int startIndex, ReadOnlySequenceSegment<T> endSegment) { throw CreateArgumentValidationException(startSegment, startIndex, endSegment); } private static Exception CreateArgumentValidationException<T>(ReadOnlySequenceSegment<T> startSegment, int startIndex, ReadOnlySequenceSegment<T> endSegment) { if (startSegment == null) { return CreateArgumentNullException(System.ExceptionArgument.startSegment); } if (endSegment == null) { return CreateArgumentNullException(System.ExceptionArgument.endSegment); } if (startSegment != endSegment && startSegment.RunningIndex > endSegment.RunningIndex) { return CreateArgumentOutOfRangeException(System.ExceptionArgument.endSegment); } if ((uint)startSegment.Memory.Length < (uint)startIndex) { return CreateArgumentOutOfRangeException(System.ExceptionArgument.startIndex); } return CreateArgumentOutOfRangeException(System.ExceptionArgument.endIndex); } public static void ThrowArgumentValidationException(Array array, int start) { throw CreateArgumentValidationException(array, start); } private static Exception CreateArgumentValidationException(Array array, int start) { if (array == null) { return CreateArgumentNullException(System.ExceptionArgument.array); } if ((uint)start > (uint)array.Length) { return CreateArgumentOutOfRangeException(System.ExceptionArgument.start); } return CreateArgumentOutOfRangeException(System.ExceptionArgument.length); } public static void ThrowStartOrEndArgumentValidationException(long start) { throw CreateStartOrEndArgumentValidationException(start); } private static Exception CreateStartOrEndArgumentValidationException(long start) { if (start < 0) { return CreateArgumentOutOfRangeException(System.ExceptionArgument.start); } return CreateArgumentOutOfRangeException(System.ExceptionArgument.length); } } internal enum ExceptionArgument { length, start, minimumBufferSize, elementIndex, comparable, comparer, destination, offset, startSegment, endSegment, startIndex, endIndex, array, culture, manager } internal static class DecimalDecCalc { private static uint D32DivMod1E9(uint hi32, ref uint lo32) { ulong num = ((ulong)hi32 << 32) | lo32; lo32 = (uint)(num / 1000000000); return (uint)(num % 1000000000); } internal static uint DecDivMod1E9(ref MutableDecimal value) { return D32DivMod1E9(D32DivMod1E9(D32DivMod1E9(0u, ref value.High), ref value.Mid), ref value.Low); } internal static void DecAddInt32(ref MutableDecimal value, uint i) { if (D32AddCarry(ref value.Low, i) && D32AddCarry(ref value.Mid, 1u)) { D32AddCarry(ref value.High, 1u); } } private static bool D32AddCarry(ref uint value, uint i) { uint num = value; uint num2 = (value = num + i); if (num2 >= num) { return num2 < i; } return true; } internal static void DecMul10(ref MutableDecimal value) { MutableDecimal d = value; DecShiftLeft(ref value); DecShiftLeft(ref value); DecAdd(ref value, d); DecShiftLeft(ref value); } private static void DecShiftLeft(ref MutableDecimal value) { uint num = (((value.Low & 0x80000000u) != 0) ? 1u : 0u); uint num2 = (((value.Mid & 0x80000000u) != 0) ? 1u : 0u); value.Low <<= 1; value.Mid = (value.Mid << 1) | num; value.High = (value.High << 1) | num2; } private static void DecAdd(ref MutableDecimal value, MutableDecimal d) { if (D32AddCarry(ref value.Low, d.Low) && D32AddCarry(ref value.Mid, 1u)) { D32AddCarry(ref value.High, 1u); } if (D32AddCarry(ref value.Mid, d.Mid)) { D32AddCarry(ref value.High, 1u); } D32AddCarry(ref value.High, d.High); } } internal static class Number { private static class DoubleHelper { public unsafe static uint Exponent(double d) { return (*(uint*)((byte*)(&d) + 4) >> 20) & 0x7FFu; } public unsafe static ulong Mantissa(double d) { return *(uint*)(&d) | ((ulong)(uint)(*(int*)((byte*)(&d) + 4) & 0xFFFFF) << 32); } public unsafe static bool Sign(double d) { return *(uint*)((byte*)(&d) + 4) >> 31 != 0; } } internal const int DECIMAL_PRECISION = 29; private static readonly ulong[] s_rgval64Power10 = new ulong[30] { 11529215046068469760uL, 14411518807585587200uL, 18014398509481984000uL, 11258999068426240000uL, 14073748835532800000uL, 17592186044416000000uL, 10995116277760000000uL, 13743895347200000000uL, 17179869184000000000uL, 10737418240000000000uL, 13421772800000000000uL, 16777216000000000000uL, 10485760000000000000uL, 13107200000000000000uL, 16384000000000000000uL, 14757395258967641293uL, 11805916207174113035uL, 9444732965739290428uL, 15111572745182864686uL, 12089258196146291749uL, 9671406556917033399uL, 15474250491067253438uL, 12379400392853802751uL, 9903520314283042201uL, 15845632502852867522uL, 12676506002282294018uL, 10141204801825835215uL, 16225927682921336344uL, 12980742146337069075uL, 10384593717069655260uL }; private static readonly sbyte[] s_rgexp64Power10 = new sbyte[15] { 4, 7, 10, 14, 17, 20, 24, 27, 30, 34, 37, 40, 44, 47, 50 }; private static readonly ulong[] s_rgval64Power10By16 = new ulong[42] { 10240000000000000000uL, 11368683772161602974uL, 12621774483536188886uL, 14012984643248170708uL, 15557538194652854266uL, 17272337110188889248uL, 9588073174409622172uL, 10644899600020376798uL, 11818212630765741798uL, 13120851772591970216uL, 14567071740625403792uL, 16172698447808779622uL, 17955302187076837696uL, 9967194951097567532uL, 11065809325636130658uL, 12285516299433008778uL, 13639663065038175358uL, 15143067982934716296uL, 16812182738118149112uL, 9332636185032188787uL, 10361307573072618722uL, 16615349947311448416uL, 14965776766268445891uL, 13479973333575319909uL, 12141680576410806707uL, 10936253623915059637uL, 9850501549098619819uL, 17745086042373215136uL, 15983352577617880260uL, 14396524142538228461uL, 12967236152753103031uL, 11679847981112819795uL, 10520271803096747049uL, 9475818434452569218uL, 17070116948172427008uL, 15375394465392026135uL, 13848924157002783096uL, 12474001934591998882uL, 11235582092889474480uL, 10120112665365530972uL, 18230774251475056952uL, 16420821625123739930uL }; private static readonly short[] s_rgexp64Power10By16 = new short[21] { 54, 107, 160, 213, 266, 319, 373, 426, 479, 532, 585, 638, 691, 745, 798, 851, 904, 957, 1010, 1064, 1117 }; public static void RoundNumber(ref NumberBuffer number, int pos) { Span<byte> digits = number.Digits; int i; for (i = 0; i < pos && digits[i] != 0; i++) { } if (i == pos && digits[i] >= 53) { while (i > 0 && digits[i - 1] == 57) { i--; } if (i > 0) { digits[i - 1]++; } else { number.Scale++; digits[0] = 49; i = 1; } } else { while (i > 0 && digits[i - 1] == 48) { i--; } } if (i == 0) { number.Scale = 0; number.IsNegative = false; } digits[i] = 0; } internal static bool NumberBufferToDouble(ref NumberBuffer number, out double value) { double num = NumberToDouble(ref number); uint num2 = DoubleHelper.Exponent(num); ulong num3 = DoubleHelper.Mantissa(num); switch (num2) { case 2047u: value = 0.0; return false; case 0u: if (num3 == 0L) { num = 0.0; } break; } value = num; return true; } public unsafe static bool NumberBufferToDecimal(ref NumberBuffer number, ref decimal value) { MutableDecimal value2 = default(MutableDecimal); byte* ptr = number.UnsafeDigits; int num = number.Scale; if (*ptr == 0) { if (num > 0) { num = 0; } } else { if (num > 29) { return false; } while ((num > 0 || (*ptr != 0 && num > -28)) && (value2.High < 429496729 || (value2.High == 429496729 && (value2.Mid < 2576980377u || (value2.Mid == 2576980377u && (value2.Low < 2576980377u || (value2.Low == 2576980377u && *ptr <= 53))))))) { DecimalDecCalc.DecMul10(ref value2); if (*ptr != 0) { DecimalDecCalc.DecAddInt32(ref value2, (uint)(*(ptr++) - 48)); } num--; } if (*(ptr++) >= 53) { bool flag = true; if (*(ptr - 1) == 53 && *(ptr - 2) % 2 == 0) { int num2 = 20; while (*ptr == 48 && num2 != 0) { ptr++; num2--; } if (*ptr == 0 || num2 == 0) { flag = false; } } if (flag) { DecimalDecCalc.DecAddInt32(ref value2, 1u); if ((value2.High | value2.Mid | value2.Low) == 0) { value2.High = 429496729u; value2.Mid = 2576980377u; value2.Low = 2576980378u; num++; } } } } if (num > 0) { return false; } if (num <= -29) { value2.High = 0u; value2.Low = 0u; value2.Mid = 0u; value2.Scale = 28; } else { value2.Scale = -num; } value2.IsNegative = number.IsNegative; value = System.Runtime.CompilerServices.Unsafe.As<MutableDecimal, decimal>(ref value2); return true; } public static void DecimalToNumber(decimal value, ref NumberBuffer number) { ref MutableDecimal reference = ref System.Runtime.CompilerServices.Unsafe.As<decimal, MutableDecimal>(ref value); Span<byte> digits = number.Digits; number.IsNegative = reference.IsNegative; int num = 29; while ((reference.Mid != 0) | (reference.High != 0)) { uint num2 = DecimalDecCalc.DecDivMod1E9(ref reference); for (int i = 0; i < 9; i++) { digits[--num] = (byte)(num2 % 10 + 48); num2 /= 10; } } for (uint num3 = reference.Low; num3 != 0; num3 /= 10) { digits[--num] = (byte)(num3 % 10 + 48); } int num4 = 29 - num; number.Scale = num4 - reference.Scale; Span<byte> digits2 = number.Digits; int index = 0; while (--num4 >= 0) { digits2[index++] = digits[num++]; } digits2[index] = 0; } private static uint DigitsToInt(ReadOnlySpan<byte> digits, int count) { uint value; int bytesConsumed; bool flag = Utf8Parser.TryParse(digits.Slice(0, count), out value, out bytesConsumed, 'D'); return value; } private static ulong Mul32x32To64(uint a, uint b) { return (ulong)a * (ulong)b; } private static ulong Mul64Lossy(ulong a, ulong b, ref int pexp) { ulong num = Mul32x32To64((uint)(a >> 32), (uint)(b >> 32)) + (Mul32x32To64((uint)(a >> 32), (uint)b) >> 32) + (Mul32x32To64((uint)a, (uint)(b >> 32)) >> 32); if ((num & 0x8000000000000000uL) == 0L) { num <<= 1; pexp--; } return num; } private static int abs(int value) { if (value < 0) { return -value; } return value; } private unsafe static double NumberToDouble(ref NumberBuffer number) { ReadOnlySpan<byte> digits = number.Digits; int i = 0; int numDigits = number.NumDigits; int num = numDigits; for (; digits[i] == 48; i++) { num--; } if (num == 0) { return 0.0; } int num2 = Math.Min(num, 9); num -= num2; ulong num3 = DigitsToInt(digits, num2); if (num > 0) { num2 = Math.Min(num, 9); num -= num2; uint b = (uint)(s_rgval64Power10[num2 - 1] >> 64 - s_rgexp64Power10[num2 - 1]); num3 = Mul32x32To64((uint)num3, b) + DigitsToInt(digits.Slice(9), num2); } int num4 = number.Scale - (numDigits - num); int num5 = abs(num4); if (num5 >= 352) { ulong num6 = ((num4 > 0) ? 9218868437227405312uL : 0); if (number.IsNegative) { num6 |= 0x8000000000000000uL; } return *(double*)(&num6); } int pexp = 64; if ((num3 & 0xFFFFFFFF00000000uL) == 0L) { num3 <<= 32; pexp -= 32; } if ((num3 & 0xFFFF000000000000uL) == 0L) { num3 <<= 16; pexp -= 16; } if ((num3 & 0xFF00000000000000uL) == 0L) { num3 <<= 8; pexp -= 8; } if ((num3 & 0xF000000000000000uL) == 0L) { num3 <<= 4; pexp -= 4; } if ((num3 & 0xC000000000000000uL) == 0L) { num3 <<= 2; pexp -= 2; } if ((num3 & 0x8000000000000000uL) == 0L) { num3 <<= 1; pexp--; } int num7 = num5 & 0xF; if (num7 != 0) { int num8 = s_rgexp64Power10[num7 - 1]; pexp += ((num4 < 0) ? (-num8 + 1) : num8); ulong b2 = s_rgval64Power10[num7 + ((num4 < 0) ? 15 : 0) - 1]; num3 = Mul64Lossy(num3, b2, ref pexp); } num7 = num5 >> 4; if (num7 != 0) { int num9 = s_rgexp64Power10By16[num7 - 1]; pexp += ((num4 < 0) ? (-num9 + 1) : num9); ulong b3 = s_rgval64Power10By16[num7 + ((num4 < 0) ? 21 : 0) - 1]; num3 = Mul64Lossy(num3, b3, ref pexp); } if (((uint)(int)num3 & 0x400u) != 0) { ulong num10 = num3 + 1023 + (ulong)(((int)num3 >> 11) & 1); if (num10 < num3) { num10 = (num10 >> 1) | 0x8000000000000000uL; pexp++; } num3 = num10; } pexp += 1022; num3 = ((pexp <= 0) ? ((pexp == -52 && num3 >= 9223372036854775896uL) ? 1 : ((pexp > -52) ? (num3 >> -pexp + 11 + 1) : 0)) : ((pexp < 2047) ? ((ulong)((long)pexp << 52) + ((num3 >> 11) & 0xFFFFFFFFFFFFFL)) : 9218868437227405312uL)); if (number.IsNegative) { num3 |= 0x8000000000000000uL; } return *(double*)(&num3); } } internal ref struct NumberBuffer { public int Scale; public bool IsNegative; public const int BufferSize = 51; private byte _b0; private byte _b1; private byte _b2; private byte _b3; private byte _b4; private byte _b5; private byte _b6; private byte _b7; private byte _b8; private byte _b9; private byte _b10; private byte _b11; private byte _b12; private byte _b13; private byte _b14; private byte _b15; private byte _b16; private byte _b17; private byte _b18; private byte _b19; private byte _b20; private byte _b21; private byte _b22; private byte _b23; private byte _b24; private byte _b25; private byte _b26; private byte _b27; private byte _b28; private byte _b29; private byte _b30; private byte _b31; private byte _b32; private byte _b33; private byte _b34; private byte _b35; private byte _b36; private byte _b37; private byte _b38; private byte _b39; private byte _b40; private byte _b41; private byte _b42; private byte _b43; private byte _b44; private byte _b45; private byte _b46; private byte _b47; private byte _b48; private byte _b49; private byte _b50; public unsafe Span<byte> Digits => new Span<byte>(System.Runtime.CompilerServices.Unsafe.AsPointer<byte>(ref _b0), 51); public unsafe byte* UnsafeDigits => (byte*)System.Runtime.CompilerServices.Unsafe.AsPointer<byte>(ref _b0); public int NumDigits => Digits.IndexOf<byte>(0); [Conditional("DEBUG")] public void CheckConsistency() { } public override string ToString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append('['); stringBuilder.Append('"'); Span<byte> digits = Digits; for (int i = 0; i < 51; i++) { byte b = digits[i]; if (b == 0) { break; } stringBuilder.Append((char)b); } stringBuilder.Append('"'); stringBuilder.Append(", Scale = " + Scale); stringBuilder.Append(", IsNegative = " + IsNegative); stringBuilder.Append(']'); return stringBuilder.ToString(); } } [DebuggerTypeProxy(typeof(System.MemoryDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public readonly struct Memory<T> { private readonly object _object; private readonly int _index; private readonly int _length; private const int RemoveFlagsBitMask = int.MaxValue; public static Memory<T> Empty => default(Memory<T>); public int Length => _length & 0x7FFFFFFF; public bool IsEmpty => (_length & 0x7FFFFFFF) == 0; public Span<T> Span { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { Span<T> result; if (_index < 0) { result = ((MemoryManager<T>)_object).GetSpan(); return result.Slice(_index & 0x7FFFFFFF, _length); } if (typeof(T) == typeof(char) && _object is string text) { result = new Span<T>(System.Runtime.CompilerServices.Unsafe.As<Pinnable<T>>((object)text), MemoryExtensions.StringAdjustment, text.Length); return result.Slice(_index, _length); } if (_object != null) { return new Span<T>((T[])_object, _index, _length & 0x7FFFFFFF); } result = default(Span<T>); return result; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Memory(T[] array) { if (array == null) { this = default(Memory<T>); return; } if (default(T) == null && array.GetType() != typeof(T[])) { System.ThrowHelper.ThrowArrayTypeMismatchException(); } _object = array; _index = 0; _length = array.Length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Memory(T[] array, int start) { if (array == null) { if (start != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(); } this = default(Memory<T>); return; } if (default(T) == null && array.GetType() != typeof(T[])) { System.ThrowHelper.ThrowArrayTypeMismatchException(); } if ((uint)start > (uint)array.Length) { System.ThrowHelper.ThrowArgumentOutOfRangeException(); } _object = array; _index = start; _length = array.Length - start; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Memory(T[] array, int start, int length) { if (array == null) { if (start != 0 || length != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(); } this = default(Memory<T>); return; } if (default(T) == null && array.GetType() != typeof(T[])) { System.ThrowHelper.ThrowArrayTypeMismatchException(); } if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(); } _object = array; _index = start; _length = length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Memory(MemoryManager<T> manager, int length) { if (length < 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(); } _object = manager; _index = int.MinValue; _length = length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Memory(MemoryManager<T> manager, int start, int length) { if (length < 0 || start < 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(); } _object = manager; _index = start | int.MinValue; _length = length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Memory(object obj, int start, int length) { _object = obj; _index = start; _length = length; } public static implicit operator Memory<T>(T[] array) { return new Memory<T>(array); } public static implicit operator Memory<T>(ArraySegment<T> segment) { return new Memory<T>(segment.Array, segment.Offset, segment.Count); } public static implicit operator ReadOnlyMemory<T>(Memory<T> memory) { return System.Runtime.CompilerServices.Unsafe.As<Memory<T>, ReadOnlyMemory<T>>(ref memory); } public override string ToString() { if (typeof(T) == typeof(char)) { if (!(_object is string text)) { return Span.ToString(); } return text.Substring(_index, _length & 0x7FFFFFFF); } return $"System.Memory<{typeof(T).Name}>[{_length & 0x7FFFFFFF}]"; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Memory<T> Slice(int start) { int length = _length; int num = length & 0x7FFFFFFF; if ((uint)start > (uint)num) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return new Memory<T>(_object, _index + start, length - start); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Memory<T> Slice(int start, int length) { int length2 = _length; int num = length2 & 0x7FFFFFFF; if ((uint)start > (uint)num || (uint)length > (uint)(num - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(); } return new Memory<T>(_object, _index + start, length | (length2 & int.MinValue)); } public void CopyTo(Memory<T> destination) { Span.CopyTo(destination.Span); } public bool TryCopyTo(Memory<T> destination) { return Span.TryCopyTo(destination.Span); } public unsafe MemoryHandle Pin() { if (_index < 0) { return ((MemoryManager<T>)_object).Pin(_index & 0x7FFFFFFF); } if (typeof(T) == typeof(char) && _object is string value) { GCHandle handle = GCHandle.Alloc(value, GCHandleType.Pinned); void* pointer = System.Runtime.CompilerServices.Unsafe.Add<T>((void*)handle.AddrOfPinnedObject(), _index); return new MemoryHandle(pointer, handle); } if (_object is T[] array) { if (_length < 0) { void* pointer2 = System.Runtime.CompilerServices.Unsafe.Add<T>(System.Runtime.CompilerServices.Unsafe.AsPointer<T>(ref MemoryMarshal.GetReference<T>(array)), _index); return new MemoryHandle(pointer2); } GCHandle handle2 = GCHandle.Alloc(array, GCHandleType.Pinned); void* pointer3 = System.Runtime.CompilerServices.Unsafe.Add<T>((void*)handle2.AddrOfPinnedObject(), _index); return new MemoryHandle(pointer3, handle2); } return default(MemoryHandle); } public T[] ToArray() { return Span.ToArray(); } [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { if (obj is ReadOnlyMemory<T> readOnlyMemory) { return readOnlyMemory.Equals(this); } if (obj is Memory<T> other) { return Equals(other); } return false; } public bool Equals(Memory<T> other) { if (_object == other._object && _index == other._index) { return _length == other._length; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { if (_object == null) { return 0; } int hashCode = _object.GetHashCode(); int index = _index; int hashCode2 = index.GetHashCode(); index = _length; return CombineHashCodes(hashCode, hashCode2, index.GetHashCode()); } private static int CombineHashCodes(int left, int right) { return ((left << 5) + left) ^ right; } private static int CombineHashCodes(int h1, int h2, int h3) { return CombineHashCodes(CombineHashCodes(h1, h2), h3); } } internal sealed class MemoryDebugView<T> { private readonly ReadOnlyMemory<T> _memory; [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public T[] Items => _memory.ToArray(); public MemoryDebugView(Memory<T> memory) { _memory = memory; } public MemoryDebugView(ReadOnlyMemory<T> memory) { _memory = memory; } } public static class MemoryExtensions { internal static readonly IntPtr StringAdjustment = MeasureStringAdjustment(); public static ReadOnlySpan<char> Trim(this ReadOnlySpan<char> span) { return span.TrimStart().TrimEnd(); } public static ReadOnlySpan<char> TrimStart(this ReadOnlySpan<char> span) { int i; for (i = 0; i < span.Length && char.IsWhiteSpace(span[i]); i++) { } return span.Slice(i); } public static ReadOnlySpan<char> TrimEnd(this ReadOnlySpan<char> span) { int num = span.Length - 1; while (num >= 0 && char.IsWhiteSpace(span[num])) { num--; } return span.Slice(0, num + 1); } public static ReadOnlySpan<char> Trim(this ReadOnlySpan<char> span, char trimChar) { return span.TrimStart(trimChar).TrimEnd(trimChar); } public static ReadOnlySpan<char> TrimStart(this ReadOnlySpan<char> span, char trimChar) { int i; for (i = 0; i < span.Length && span[i] == trimChar; i++) { } return span.Slice(i); } public static ReadOnlySpan<char> TrimEnd(this ReadOnlySpan<char> span, char trimChar) { int num = span.Length - 1; while (num >= 0 && span[num] == trimChar) { num--; } return span.Slice(0, num + 1); } public static ReadOnlySpan<char> Trim(this ReadOnlySpan<char> span, ReadOnlySpan<char> trimChars) { return span.TrimStart(trimChars).TrimEnd(trimChars); } public static ReadOnlySpan<char> TrimStart(this ReadOnlySpan<char> span, ReadOnlySpan<char> trimChars) { if (trimChars.IsEmpty) { return span.TrimStart(); } int i; for (i = 0; i < span.Length; i++) { int num = 0; while (num < trimChars.Length) { if (span[i] != trimChars[num]) { num++; continue; } goto IL_003c; } break; IL_003c:; } return span.Slice(i); } public static ReadOnlySpan<char> TrimEnd(this ReadOnlySpan<char> span, ReadOnlySpan<char> trimChars) { if (trimChars.IsEmpty) { return span.TrimEnd(); } int num; for (num = span.Length - 1; num >= 0; num--) { int num2 = 0; while (num2 < trimChars.Length) { if (span[num] != trimChars[num2]) { num2++; continue; } goto IL_0044; } break; IL_0044:; } return span.Slice(0, num + 1); } public static bool IsWhiteSpace(this ReadOnlySpan<char> span) { for (int i = 0; i < span.Length; i++) { if (!char.IsWhiteSpace(span[i])) { return false; } } return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOf<T>(this Span<T> span, T value) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value), span.Length); } if (typeof(T) == typeof(char)) { return System.SpanHelpers.IndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, char>(ref value), span.Length); } return System.SpanHelpers.IndexOf(ref MemoryMarshal.GetReference(span), value, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOf<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), value.Length); } return System.SpanHelpers.IndexOf(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(value), value.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOf<T>(this Span<T> span, T value) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value), span.Length); } if (typeof(T) == typeof(char)) { return System.SpanHelpers.LastIndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, char>(ref value), span.Length); } return System.SpanHelpers.LastIndexOf(ref MemoryMarshal.GetReference(span), value, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOf<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), value.Length); } return System.SpanHelpers.LastIndexOf(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(value), value.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool SequenceEqual<T>(this Span<T> span, ReadOnlySpan<T> other) where T : IEquatable<T> { int length = span.Length; if (default(T) != null && IsTypeComparableAsBytes<T>(out var size)) { if (length == other.Length) { return System.SpanHelpers.SequenceEqual(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(other)), (NUInt)length * size); } return false; } if (length == other.Length) { return System.SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(other), length); } return false; } public static int SequenceCompareTo<T>(this Span<T> span, ReadOnlySpan<T> other) where T : IComparable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.SequenceCompareTo(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(other)), other.Length); } if (typeof(T) == typeof(char)) { return System.SpanHelpers.SequenceCompareTo(ref System.Runtime.CompilerServices.Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, char>(ref MemoryMarshal.GetReference(other)), other.Length); } return System.SpanHelpers.SequenceCompareTo(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(other), other.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOf<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value), span.Length); } if (typeof(T) == typeof(char)) { return System.SpanHelpers.IndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, char>(ref value), span.Length); } return System.SpanHelpers.IndexOf(ref MemoryMarshal.GetReference(span), value, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOf<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), value.Length); } return System.SpanHelpers.IndexOf(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(value), value.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOf<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value), span.Length); } if (typeof(T) == typeof(char)) { return System.SpanHelpers.LastIndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, char>(ref value), span.Length); } return System.SpanHelpers.LastIndexOf(ref MemoryMarshal.GetReference(span), value, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOf<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOf(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), value.Length); } return System.SpanHelpers.LastIndexOf(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(value), value.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOfAny<T>(this Span<T> span, T value0, T value1) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value0), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value1), span.Length); } return System.SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOfAny<T>(this Span<T> span, T value0, T value1, T value2) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value0), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value1), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value2), span.Length); } return System.SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, value2, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOfAny<T>(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)), values.Length); } return System.SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOfAny<T>(this ReadOnlySpan<T> span, T value0, T value1) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value0), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value1), span.Length); } return System.SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOfAny<T>(this ReadOnlySpan<T> span, T value0, T value1, T value2) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value0), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value1), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value2), span.Length); } return System.SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, value2, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> values) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.IndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)), values.Length); } return System.SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOfAny<T>(this Span<T> span, T value0, T value1) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value0), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value1), span.Length); } return System.SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOfAny<T>(this Span<T> span, T value0, T value1, T value2) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value0), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value1), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value2), span.Length); } return System.SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, value2, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOfAny<T>(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)), values.Length); } return System.SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOfAny<T>(this ReadOnlySpan<T> span, T value0, T value1) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value0), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value1), span.Length); } return System.SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOfAny<T>(this ReadOnlySpan<T> span, T value0, T value1, T value2) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value0), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value1), System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value2), span.Length); } return System.SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, value2, span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int LastIndexOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> values) where T : IEquatable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.LastIndexOfAny(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)), values.Length); } return System.SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool SequenceEqual<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other) where T : IEquatable<T> { int length = span.Length; if (default(T) != null && IsTypeComparableAsBytes<T>(out var size)) { if (length == other.Length) { return System.SpanHelpers.SequenceEqual(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(other)), (NUInt)length * size); } return false; } if (length == other.Length) { return System.SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(other), length); } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int SequenceCompareTo<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other) where T : IComparable<T> { if (typeof(T) == typeof(byte)) { return System.SpanHelpers.SequenceCompareTo(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(other)), other.Length); } if (typeof(T) == typeof(char)) { return System.SpanHelpers.SequenceCompareTo(ref System.Runtime.CompilerServices.Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), span.Length, ref System.Runtime.CompilerServices.Unsafe.As<T, char>(ref MemoryMarshal.GetReference(other)), other.Length); } return System.SpanHelpers.SequenceCompareTo(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(other), other.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool StartsWith<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { int length = value.Length; if (default(T) != null && IsTypeComparableAsBytes<T>(out var size)) { if (length <= span.Length) { return System.SpanHelpers.SequenceEqual(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), (NUInt)length * size); } return false; } if (length <= span.Length) { return System.SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(value), length); } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool StartsWith<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { int length = value.Length; if (default(T) != null && IsTypeComparableAsBytes<T>(out var size)) { if (length <= span.Length) { return System.SpanHelpers.SequenceEqual(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), (NUInt)length * size); } return false; } if (length <= span.Length) { return System.SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(value), length); } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool EndsWith<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { int length = span.Length; int length2 = value.Length; if (default(T) != null && IsTypeComparableAsBytes<T>(out var size)) { if (length2 <= length) { return System.SpanHelpers.SequenceEqual(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref System.Runtime.CompilerServices.Unsafe.Add<T>(ref MemoryMarshal.GetReference(span), length - length2)), ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), (NUInt)length2 * size); } return false; } if (length2 <= length) { return System.SpanHelpers.SequenceEqual(ref System.Runtime.CompilerServices.Unsafe.Add<T>(ref MemoryMarshal.GetReference(span), length - length2), ref MemoryMarshal.GetReference(value), length2); } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool EndsWith<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { int length = span.Length; int length2 = value.Length; if (default(T) != null && IsTypeComparableAsBytes<T>(out var size)) { if (length2 <= length) { return System.SpanHelpers.SequenceEqual(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref System.Runtime.CompilerServices.Unsafe.Add<T>(ref MemoryMarshal.GetReference(span), length - length2)), ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)), (NUInt)length2 * size); } return false; } if (length2 <= length) { return System.SpanHelpers.SequenceEqual(ref System.Runtime.CompilerServices.Unsafe.Add<T>(ref MemoryMarshal.GetReference(span), length - length2), ref MemoryMarshal.GetReference(value), length2); } return false; } public static void Reverse<T>(this Span<T> span) { ref T reference = ref MemoryMarshal.GetReference(span); int num = 0; int num2 = span.Length - 1; while (num < num2) { T val = System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, num); System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, num) = System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, num2); System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, num2) = val; num++; num2--; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span<T> AsSpan<T>(this T[] array) { return new Span<T>(array); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span<T> AsSpan<T>(this T[] array, int start, int length) { return new Span<T>(array, start, length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span<T> AsSpan<T>(this ArraySegment<T> segment) { return new Span<T>(segment.Array, segment.Offset, segment.Count); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span<T> AsSpan<T>(this ArraySegment<T> segment, int start) { if ((uint)start > segment.Count) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return new Span<T>(segment.Array, segment.Offset + start, segment.Count - start); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span<T> AsSpan<T>(this ArraySegment<T> segment, int start, int length) { if ((uint)start > segment.Count) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } if ((uint)length > segment.Count - start) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.length); } return new Span<T>(segment.Array, segment.Offset + start, length); } public static Memory<T> AsMemory<T>(this T[] array) { return new Memory<T>(array); } public static Memory<T> AsMemory<T>(this T[] array, int start) { return new Memory<T>(array, start); } public static Memory<T> AsMemory<T>(this T[] array, int start, int length) { return new Memory<T>(array, start, length); } public static Memory<T> AsMemory<T>(this ArraySegment<T> segment) { return new Memory<T>(segment.Array, segment.Offset, segment.Count); } public static Memory<T> AsMemory<T>(this ArraySegment<T> segment, int start) { if ((uint)start > segment.Count) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return new Memory<T>(segment.Array, segment.Offset + start, segment.Count - start); } public static Memory<T> AsMemory<T>(this ArraySegment<T> segment, int start, int length) { if ((uint)start > segment.Count) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } if ((uint)length > segment.Count - start) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.length); } return new Memory<T>(segment.Array, segment.Offset + start, length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void CopyTo<T>(this T[] source, Span<T> destination) { new ReadOnlySpan<T>(source).CopyTo(destination); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void CopyTo<T>(this T[] source, Memory<T> destination) { source.CopyTo(destination.Span); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Overlaps<T>(this Span<T> span, ReadOnlySpan<T> other) { return ((ReadOnlySpan<T>)span).Overlaps(other); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Overlaps<T>(this Span<T> span, ReadOnlySpan<T> other, out int elementOffset) { return ((ReadOnlySpan<T>)span).Overlaps(other, out elementOffset); } public static bool Overlaps<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other) { if (span.IsEmpty || other.IsEmpty) { return false; } IntPtr intPtr = System.Runtime.CompilerServices.Unsafe.ByteOffset<T>(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(other)); if (System.Runtime.CompilerServices.Unsafe.SizeOf<IntPtr>() == 4) { if ((uint)(int)intPtr >= (uint)(span.Length * System.Runtime.CompilerServices.Unsafe.SizeOf<T>())) { return (uint)(int)intPtr > (uint)(-(other.Length * System.Runtime.CompilerServices.Unsafe.SizeOf<T>())); } return true; } if ((ulong)(long)intPtr >= (ulong)((long)span.Length * (long)System.Runtime.CompilerServices.Unsafe.SizeOf<T>())) { return (ulong)(long)intPtr > (ulong)(-((long)other.Length * (long)System.Runtime.CompilerServices.Unsafe.SizeOf<T>())); } return true; } public static bool Overlaps<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other, out int elementOffset) { if (span.IsEmpty || other.IsEmpty) { elementOffset = 0; return false; } IntPtr intPtr = System.Runtime.CompilerServices.Unsafe.ByteOffset<T>(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(other)); if (System.Runtime.CompilerServices.Unsafe.SizeOf<IntPtr>() == 4) { if ((uint)(int)intPtr < (uint)(span.Length * System.Runtime.CompilerServices.Unsafe.SizeOf<T>()) || (uint)(int)intPtr > (uint)(-(other.Length * System.Runtime.CompilerServices.Unsafe.SizeOf<T>()))) { if ((int)intPtr % System.Runtime.CompilerServices.Unsafe.SizeOf<T>() != 0) { System.ThrowHelper.ThrowArgumentException_OverlapAlignmentMismatch(); } elementOffset = (int)intPtr / System.Runtime.CompilerServices.Unsafe.SizeOf<T>(); return true; } elementOffset = 0; return false; } if ((ulong)(long)intPtr < (ulong)((long)span.Length * (long)System.Runtime.CompilerServices.Unsafe.SizeOf<T>()) || (ulong)(long)intPtr > (ulong)(-((long)other.Length * (long)System.Runtime.CompilerServices.Unsafe.SizeOf<T>()))) { if ((long)intPtr % System.Runtime.CompilerServices.Unsafe.SizeOf<T>() != 0L) { System.ThrowHelper.ThrowArgumentException_OverlapAlignmentMismatch(); } elementOffset = (int)((long)intPtr / System.Runtime.CompilerServices.Unsafe.SizeOf<T>()); return true; } elementOffset = 0; return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int BinarySearch<T>(this Span<T> span, IComparable<T> comparable) { return span.BinarySearch<T, IComparable<T>>(comparable); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int BinarySearch<T, TComparable>(this Span<T> span, TComparable comparable) where TComparable : IComparable<T> { return BinarySearch((ReadOnlySpan<T>)span, comparable); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int BinarySearch<T, TComparer>(this Span<T> span, T value, TComparer comparer) where TComparer : IComparer<T> { return ((ReadOnlySpan<T>)span).BinarySearch(value, comparer); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int BinarySearch<T>(this ReadOnlySpan<T> span, IComparable<T> comparable) { return MemoryExtensions.BinarySearch<T, IComparable<T>>(span, comparable); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int BinarySearch<T, TComparable>(this ReadOnlySpan<T> span, TComparable comparable) where TComparable : IComparable<T> { return System.SpanHelpers.BinarySearch(span, comparable); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int BinarySearch<T, TComparer>(this ReadOnlySpan<T> span, T value, TComparer comparer) where TComparer : IComparer<T> { if (comparer == null) { System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.comparer); } System.SpanHelpers.ComparerComparable<T, TComparer> comparable = new System.SpanHelpers.ComparerComparable<T, TComparer>(value, comparer); return BinarySearch(span, comparable); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool IsTypeComparableAsBytes<T>(out NUInt size) { if (typeof(T) == typeof(byte) || typeof(T) == typeof(sbyte)) { size = (NUInt)1; return true; } if (typeof(T) == typeof(char) || typeof(T) == typeof(short) || typeof(T) == typeof(ushort)) { size = (NUInt)2; return true; } if (typeof(T) == typeof(int) || typeof(T) == typeof(uint)) { size = (NUInt)4; return true; } if (typeof(T) == typeof(long) || typeof(T) == typeof(ulong)) { size = (NUInt)8; return true; } size = default(NUInt); return false; } public static Span<T> AsSpan<T>(this T[] array, int start) { return Span<T>.Create(array, start); } public static bool Contains(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType) { return span.IndexOf(value, comparisonType) >= 0; } public static bool Equals(this ReadOnlySpan<char> span, ReadOnlySpan<char> other, StringComparison comparisonType) { switch (comparisonType) { case StringComparison.Ordinal: return span.SequenceEqual(other); case StringComparison.OrdinalIgnoreCase: if (span.Length != other.Length) { return false; } return EqualsOrdinalIgnoreCase(span, other); default: return span.ToString().Equals(other.ToString(), comparisonType); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool EqualsOrdinalIgnoreCase(ReadOnlySpan<char> span, ReadOnlySpan<char> other) { if (other.Length == 0) { return true; } return CompareToOrdinalIgnoreCase(span, other) == 0; } public static int CompareTo(this ReadOnlySpan<char> span, ReadOnlySpan<char> other, StringComparison comparisonType) { return comparisonType switch { StringComparison.Ordinal => span.SequenceCompareTo(other), StringComparison.OrdinalIgnoreCase => CompareToOrdinalIgnoreCase(span, other), _ => string.Compare(span.ToString(), other.ToString(), comparisonType), }; } private unsafe static int CompareToOrdinalIgnoreCase(ReadOnlySpan<char> strA, ReadOnlySpan<char> strB) { int num = Math.Min(strA.Length, strB.Length); int num2 = num; fixed (char* ptr = &MemoryMarshal.GetReference(strA)) { fixed (char* ptr3 = &MemoryMarshal.GetReference(strB)) { char* ptr2 = ptr; char* ptr4 = ptr3; while (num != 0 && *ptr2 <= '\u007f' && *ptr4 <= '\u007f') { int num3 = *ptr2; int num4 = *ptr4; if (num3 == num4) { ptr2++; ptr4++; num--; continue; } if ((uint)(num3 - 97) <= 25u) { num3 -= 32; } if ((uint)(num4 - 97) <= 25u) { num4 -= 32; } if (num3 != num4) { return num3 - num4; } ptr2++; ptr4++; num--; } if (num == 0) { return strA.Length - strB.Length; } num2 -= num; return string.Compare(strA.Slice(num2).ToString(), strB.Slice(num2).ToString(), StringComparison.OrdinalIgnoreCase); } } } public static int IndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType) { if (comparisonType == StringComparison.Ordinal) { return span.IndexOf(value); } return span.ToString().IndexOf(value.ToString(), comparisonType); } public static int ToLower(this ReadOnlySpan<char> source, Span<char> destination, CultureInfo culture) { if (culture == null) { System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.culture); } if (destination.Length < source.Length) { return -1; } string text = source.ToString(); string text2 = text.ToLower(culture); AsSpan(text2).CopyTo(destination); return source.Length; } public static int ToLowerInvariant(this ReadOnlySpan<char> source, Span<char> destination) { return source.ToLower(destination, CultureInfo.InvariantCulture); } public static int ToUpper(this ReadOnlySpan<char> source, Span<char> destination, CultureInfo culture) { if (culture == null) { System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.culture); } if (destination.Length < source.Length) { return -1; } string text = source.ToString(); string text2 = text.ToUpper(culture); AsSpan(text2).CopyTo(destination); return source.Length; } public static int ToUpperInvariant(this ReadOnlySpan<char> source, Span<char> destination) { return source.ToUpper(destination, CultureInfo.InvariantCulture); } public static bool EndsWith(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType) { switch (comparisonType) { case StringComparison.Ordinal: return span.EndsWith(value); case StringComparison.OrdinalIgnoreCase: if (value.Length <= span.Length) { return EqualsOrdinalIgnoreCase(span.Slice(span.Length - value.Length), value); } return false; default: { string text = span.ToString(); string value2 = value.ToString(); return text.EndsWith(value2, comparisonType); } } } public static bool StartsWith(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType) { switch (comparisonType) { case StringComparison.Ordinal: return span.StartsWith(value); case StringComparison.OrdinalIgnoreCase: if (value.Length <= span.Length) { return EqualsOrdinalIgnoreCase(span.Slice(0, value.Length), value); } return false; default: { string text = span.ToString(); string value2 = value.ToString(); return text.StartsWith(value2, comparisonType); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ReadOnlySpan<char> AsSpan(this string text) { if (text == null) { return default(ReadOnlySpan<char>); } return new ReadOnlySpan<char>(System.Runtime.CompilerServices.Unsafe.As<Pinnable<char>>((object)text), StringAdjustment, text.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ReadOnlySpan<char> AsSpan(this string text, int start) { if (text == null) { if (start != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return default(ReadOnlySpan<char>); } if ((uint)start > (uint)text.Length) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return new ReadOnlySpan<char>(System.Runtime.CompilerServices.Unsafe.As<Pinnable<char>>((object)text), StringAdjustment + start * 2, text.Length - start); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ReadOnlySpan<char> AsSpan(this string text, int start, int length) { if (text == null) { if (start != 0 || length != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return default(ReadOnlySpan<char>); } if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return new ReadOnlySpan<char>(System.Runtime.CompilerServices.Unsafe.As<Pinnable<char>>((object)text), StringAdjustment + start * 2, length); } public static ReadOnlyMemory<char> AsMemory(this string text) { if (text == null) { return default(ReadOnlyMemory<char>); } return new ReadOnlyMemory<char>(text, 0, text.Length); } public static ReadOnlyMemory<char> AsMemory(this string text, int start) { if (text == null) { if (start != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return default(ReadOnlyMemory<char>); } if ((uint)start > (uint)text.Length) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return new ReadOnlyMemory<char>(text, start, text.Length - start); } public static ReadOnlyMemory<char> AsMemory(this string text, int start, int length) { if (text == null) { if (start != 0 || length != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return default(ReadOnlyMemory<char>); } if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return new ReadOnlyMemory<char>(text, start, length); } private unsafe static IntPtr MeasureStringAdjustment() { string text = "a"; fixed (char* ptr = text) { return System.Runtime.CompilerServices.Unsafe.ByteOffset<char>(ref System.Runtime.CompilerServices.Unsafe.As<Pinnable<char>>((object)text).Data, ref System.Runtime.CompilerServices.Unsafe.AsRef<char>((void*)ptr)); } } } [DebuggerTypeProxy(typeof(System.MemoryDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public readonly struct ReadOnlyMemory<T> { private readonly object _object; private readonly int _index; private readonly int _length; internal const int RemoveFlagsBitMask = int.MaxValue; public static ReadOnlyMemory<T> Empty => default(ReadOnlyMemory<T>); public int Length => _length & 0x7FFFFFFF; public bool IsEmpty => (_length & 0x7FFFFFFF) == 0; public ReadOnlySpan<T> Span { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { if (_index < 0) { return ((MemoryManager<T>)_object).GetSpan().Slice(_index & 0x7FFFFFFF, _length); } ReadOnlySpan<T> result; if (typeof(T) == typeof(char) && _object is string text) { result = new ReadOnlySpan<T>(System.Runtime.CompilerServices.Unsafe.As<Pinnable<T>>((object)text), MemoryExtensions.StringAdjustment, text.Length); return result.Slice(_index, _length); } if (_object != null) { return new ReadOnlySpan<T>((T[])_object, _index, _length & 0x7FFFFFFF); } result = default(ReadOnlySpan<T>); return result; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyMemory(T[] array) { if (array == null) { this = default(ReadOnlyMemory<T>); return; } _object = array; _index = 0; _length = array.Length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyMemory(T[] array, int start, int length) { if (array == null) { if (start != 0 || length != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(); } this = default(ReadOnlyMemory<T>); return; } if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(); } _object = array; _index = start; _length = length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal ReadOnlyMemory(object obj, int start, int length) { _object = obj; _index = start; _length = length; } public static implicit operator ReadOnlyMemory<T>(T[] array) { return new ReadOnlyMemory<T>(array); } public static implicit operator ReadOnlyMemory<T>(ArraySegment<T> segment) { return new ReadOnlyMemory<T>(segment.Array, segment.Offset, segment.Count); } public override string ToString() { if (typeof(T) == typeof(char)) { if (!(_object is string text)) { return Span.ToString(); } return text.Substring(_index, _length & 0x7FFFFFFF); } return $"System.ReadOnlyMemory<{typeof(T).Name}>[{_length & 0x7FFFFFFF}]"; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyMemory<T> Slice(int start) { int length = _length; int num = length & 0x7FFFFFFF; if ((uint)start > (uint)num) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return new ReadOnlyMemory<T>(_object, _index + start, length - start); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyMemory<T> Slice(int start, int length) { int length2 = _length; int num = _length & 0x7FFFFFFF; if ((uint)start > (uint)num || (uint)length > (uint)(num - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return new ReadOnlyMemory<T>(_object, _index + start, length | (length2 & int.MinValue)); } public void CopyTo(Memory<T> destination) { Span.CopyTo(destination.Span); } public bool TryCopyTo(Memory<T> destination) { return Span.TryCopyTo(destination.Span); } public unsafe MemoryHandle Pin() { if (_index < 0) { return ((MemoryManager<T>)_object).Pin(_index & 0x7FFFFFFF); } if (typeof(T) == typeof(char) && _object is string value) { GCHandle handle = GCHandle.Alloc(value, GCHandleType.Pinned); void* pointer = System.Runtime.CompilerServices.Unsafe.Add<T>((void*)handle.AddrOfPinnedObject(), _index); return new MemoryHandle(pointer, handle); } if (_object is T[] array) { if (_length < 0) { void* pointer2 = System.Runtime.CompilerServices.Unsafe.Add<T>(System.Runtime.CompilerServices.Unsafe.AsPointer<T>(ref MemoryMarshal.GetReference<T>(array)), _index); return new MemoryHandle(pointer2); } GCHandle handle2 = GCHandle.Alloc(array, GCHandleType.Pinned); void* pointer3 = System.Runtime.CompilerServices.Unsafe.Add<T>((void*)handle2.AddrOfPinnedObject(), _index); return new MemoryHandle(pointer3, handle2); } return default(MemoryHandle); } public T[] ToArray() { return Span.ToArray(); } [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { if (obj is ReadOnlyMemory<T> other) { return Equals(other); } if (obj is Memory<T> memory) { return Equals(memory); } return false; } public bool Equals(ReadOnlyMemory<T> other) { if (_object == other._object && _index == other._index) { return _length == other._length; } return false; } [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { if (_object == null) { return 0; } int hashCode = _object.GetHashCode(); int index = _index; int hashCode2 = index.GetHashCode(); index = _length; return CombineHashCodes(hashCode, hashCode2, index.GetHashCode()); } private static int CombineHashCodes(int left, int right) { return ((left << 5) + left) ^ right; } private static int CombineHashCodes(int h1, int h2, int h3) { return CombineHashCodes(CombineHashCodes(h1, h2), h3); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal object GetObjectStartLength(out int start, out int length) { start = _index; length = _length; return _object; } } [DebuggerTypeProxy(typeof(System.SpanDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] [DebuggerTypeProxy(typeof(System.SpanDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public readonly ref struct ReadOnlySpan<T> { public ref struct Enumerator { private readonly ReadOnlySpan<T> _span; private int _index; public ref readonly T Current { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return ref _span[_index]; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Enumerator(ReadOnlySpan<T> span) { _span = span; _index = -1; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool MoveNext() { int num = _index + 1; if (num < _span.Length) { _index = num; return true; } return false; } } private readonly Pinnable<T> _pinnable; private readonly IntPtr _byteOffset; private readonly int _length; public int Length => _length; public bool IsEmpty => _length == 0; public static ReadOnlySpan<T> Empty => default(ReadOnlySpan<T>); public unsafe ref readonly T this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { if ((uint)index >= (uint)_length) { System.ThrowHelper.ThrowIndexOutOfRangeException(); } if (_pinnable == null) { IntPtr byteOffset = _byteOffset; return ref System.Runtime.CompilerServices.Unsafe.Add<T>(ref System.Runtime.CompilerServices.Unsafe.AsRef<T>(byteOffset.ToPointer()), index); } return ref System.Runtime.CompilerServices.Unsafe.Add<T>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset<T>(ref _pinnable.Data, _byteOffset), index); } } internal Pinnable<T> Pinnable => _pinnable; internal IntPtr ByteOffset => _byteOffset; public static bool operator !=(ReadOnlySpan<T> left, ReadOnlySpan<T> right) { return !(left == right); } [Obsolete("Equals() on ReadOnlySpan will always throw an exception. Use == instead.")] [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { throw new NotSupportedException(System.SR.NotSupported_CannotCallEqualsOnSpan); } [Obsolete("GetHashCode() on ReadOnlySpan will always throw an exception.")] [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { throw new NotSupportedException(System.SR.NotSupported_CannotCallGetHashCodeOnSpan); } public static implicit operator ReadOnlySpan<T>(T[] array) { return new ReadOnlySpan<T>(array); } public static implicit operator ReadOnlySpan<T>(ArraySegment<T> segment) { return new ReadOnlySpan<T>(segment.Array, segment.Offset, segment.Count); } public Enumerator GetEnumerator() { return new Enumerator(this); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlySpan(T[] array) { if (array == null) { this = default(ReadOnlySpan<T>); return; } _length = array.Length; _pinnable = System.Runtime.CompilerServices.Unsafe.As<Pinnable<T>>((object)array); _byteOffset = System.SpanHelpers.PerTypeValues<T>.ArrayAdjustment; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlySpan(T[] array, int start, int length) { if (array == null) { if (start != 0 || length != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } this = default(ReadOnlySpan<T>); return; } if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } _length = length; _pinnable = System.Runtime.CompilerServices.Unsafe.As<Pinnable<T>>((object)array); _byteOffset = System.SpanHelpers.PerTypeValues<T>.ArrayAdjustment.Add<T>(start); } [MethodImpl(MethodImplOptions.AggressiveInlining)] [CLSCompliant(false)] public unsafe ReadOnlySpan(void* pointer, int length) { if (System.SpanHelpers.IsReferenceOrContainsReferences<T>()) { System.ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } if (length < 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } _length = length; _pinnable = null; _byteOffset = new IntPtr(pointer); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal ReadOnlySpan(Pinnable<T> pinnable, IntPtr byteOffset, int length) { _length = length; _pinnable = pinnable; _byteOffset = byteOffset; } [EditorBrowsable(EditorBrowsableState.Never)] public unsafe ref readonly T GetPinnableReference() { if (_length != 0) { if (_pinnable == null) { IntPtr byteOffset = _byteOffset; return ref System.Runtime.CompilerServices.Unsafe.AsRef<T>(byteOffset.ToPointer()); } return ref System.Runtime.CompilerServices.Unsafe.AddByteOffset<T>(ref _pinnable.Data, _byteOffset); } return ref System.Runtime.CompilerServices.Unsafe.AsRef<T>((void*)null); } public void CopyTo(Span<T> destination) { if (!TryCopyTo(destination)) { System.ThrowHelper.ThrowArgumentException_DestinationTooShort(); } } public bool TryCopyTo(Span<T> destination) { int length = _length; int length2 = destination.Length; if (length == 0) { return true; } if ((uint)length > (uint)length2) { return false; } ref T src = ref DangerousGetPinnableReference(); System.SpanHelpers.CopyTo(ref destination.DangerousGetPinnableReference(), length2, ref src, length); return true; } public static bool operator ==(ReadOnlySpan<T> left, ReadOnlySpan<T> right) { if (left._length == right._length) { return System.Runtime.CompilerServices.Unsafe.AreSame<T>(ref left.DangerousGetPinnableReference(), ref right.DangerousGetPinnableReference()); } return false; } public unsafe override string ToString() { if (typeof(T) == typeof(char)) { if (_byteOffset == MemoryExtensions.StringAdjustment) { object obj = System.Runtime.CompilerServices.Unsafe.As<object>((object)_pinnable); if (obj is string text && _length == text.Length) { return text; } } fixed (char* value = &System.Runtime.CompilerServices.Unsafe.As<T, char>(ref DangerousGetPinnableReference())) { return new string(value, 0, _length); } } return $"System.ReadOnlySpan<{typeof(T).Name}>[{_length}]"; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlySpan<T> Slice(int start) { if ((uint)start > (uint)_length) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } IntPtr byteOffset = _byteOffset.Add<T>(start); int length = _length - start; return new ReadOnlySpan<T>(_pinnable, byteOffset, length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlySpan<T> Slice(int start, int length) { if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } IntPtr byteOffset = _byteOffset.Add<T>(start); return new ReadOnlySpan<T>(_pinnable, byteOffset, length); } public T[] ToArray() { if (_length == 0) { return System.SpanHelpers.PerTypeValues<T>.EmptyArray; } T[] array = new T[_length]; CopyTo(array); return array; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [EditorBrowsable(EditorBrowsableState.Never)] internal unsafe ref T DangerousGetPinnableReference() { if (_pinnable == null) { IntPtr byteOffset = _byteOffset; return ref System.Runtime.CompilerServices.Unsafe.AsRef<T>(byteOffset.ToPointer()); } return ref System.Runtime.CompilerServices.Unsafe.AddByteOffset<T>(ref _pinnable.Data, _byteOffset); } } [DebuggerTypeProxy(typeof(System.SpanDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] [DebuggerTypeProxy(typeof(System.SpanDebugView<>))] [DebuggerDisplay("{ToString(),raw}")] public readonly ref struct Span<T> { public ref struct Enumerator { private readonly Span<T> _span; private int _index; public ref T Current { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return ref _span[_index]; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Enumerator(Span<T> span) { _span = span; _index = -1; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool MoveNext() { int num = _index + 1; if (num < _span.Length) { _index = num; return true; } return false; } } private readonly Pinnable<T> _pinnable; private readonly IntPtr _byteOffset; private readonly int _length; public int Length => _length; public bool IsEmpty => _length == 0; public static Span<T> Empty => default(Span<T>); public unsafe ref T this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { if ((uint)index >= (uint)_length) { System.ThrowHelper.ThrowIndexOutOfRangeException(); } if (_pinnable == null) { IntPtr byteOffset = _byteOffset; return ref System.Runtime.CompilerServices.Unsafe.Add<T>(ref System.Runtime.CompilerServices.Unsafe.AsRef<T>(byteOffset.ToPointer()), index); } return ref System.Runtime.CompilerServices.Unsafe.Add<T>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset<T>(ref _pinnable.Data, _byteOffset), index); } } internal Pinnable<T> Pinnable => _pinnable; internal IntPtr ByteOffset => _byteOffset; public static bool operator !=(Span<T> left, Span<T> right) { return !(left == right); } [Obsolete("Equals() on Span will always throw an exception. Use == instead.")] [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { throw new NotSupportedException(System.SR.NotSupported_CannotCallEqualsOnSpan); } [Obsolete("GetHashCode() on Span will always throw an exception.")] [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { throw new NotSupportedException(System.SR.NotSupported_CannotCallGetHashCodeOnSpan); } public static implicit operator Span<T>(T[] array) { return new Span<T>(array); } public static implicit operator Span<T>(ArraySegment<T> segment) { return new Span<T>(segment.Array, segment.Offset, segment.Count); } public Enumerator GetEnumerator() { return new Enumerator(this); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span(T[] array) { if (array == null) { this = default(Span<T>); return; } if (default(T) == null && array.GetType() != typeof(T[])) { System.ThrowHelper.ThrowArrayTypeMismatchException(); } _length = array.Length; _pinnable = System.Runtime.CompilerServices.Unsafe.As<Pinnable<T>>((object)array); _byteOffset = System.SpanHelpers.PerTypeValues<T>.ArrayAdjustment; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static Span<T> Create(T[] array, int start) { if (array == null) { if (start != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } return default(Span<T>); } if (default(T) == null && array.GetType() != typeof(T[])) { System.ThrowHelper.ThrowArrayTypeMismatchException(); } if ((uint)start > (uint)array.Length) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } IntPtr byteOffset = System.SpanHelpers.PerTypeValues<T>.ArrayAdjustment.Add<T>(start); int length = array.Length - start; return new Span<T>(System.Runtime.CompilerServices.Unsafe.As<Pinnable<T>>((object)array), byteOffset, length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span(T[] array, int start, int length) { if (array == null) { if (start != 0 || length != 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } this = default(Span<T>); return; } if (default(T) == null && array.GetType() != typeof(T[])) { System.ThrowHelper.ThrowArrayTypeMismatchException(); } if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } _length = length; _pinnable = System.Runtime.CompilerServices.Unsafe.As<Pinnable<T>>((object)array); _byteOffset = System.SpanHelpers.PerTypeValues<T>.ArrayAdjustment.Add<T>(start); } [MethodImpl(MethodImplOptions.AggressiveInlining)] [CLSCompliant(false)] public unsafe Span(void* pointer, int length) { if (System.SpanHelpers.IsReferenceOrContainsReferences<T>()) { System.ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } if (length < 0) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } _length = length; _pinnable = null; _byteOffset = new IntPtr(pointer); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal Span(Pinnable<T> pinnable, IntPtr byteOffset, int length) { _length = length; _pinnable = pinnable; _byteOffset = byteOffset; } [EditorBrowsable(EditorBrowsableState.Never)] public unsafe ref T GetPinnableReference() { if (_length != 0) { if (_pinnable == null) { IntPtr byteOffset = _byteOffset; return ref System.Runtime.CompilerServices.Unsafe.AsRef<T>(byteOffset.ToPointer()); } return ref System.Runtime.CompilerServices.Unsafe.AddByteOffset<T>(ref _pinnable.Data, _byteOffset); } return ref System.Runtime.CompilerServices.Unsafe.AsRef<T>((void*)null); } public unsafe void Clear() { int length = _length; if (length == 0) { return; } UIntPtr byteLength = (UIntPtr)(ulong)((uint)length * System.Runtime.CompilerServices.Unsafe.SizeOf<T>()); if ((System.Runtime.CompilerServices.Unsafe.SizeOf<T>() & (sizeof(IntPtr) - 1)) != 0) { if (_pinnable == null) { IntPtr byteOffset = _byteOffset; byte* ptr = (byte*)byteOffset.ToPointer(); System.SpanHelpers.ClearLessThanPointerSized(ptr, byteLength); } else { System.SpanHelpers.ClearLessThanPointerSized(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset<T>(ref _pinnable.Data, _byteOffset)), byteLength); } } else if (System.SpanHelpers.IsReferenceOrContainsReferences<T>()) { UIntPtr pointerSizeLength = (UIntPtr)(ulong)(length * System.Runtime.CompilerServices.Unsafe.SizeOf<T>() / sizeof(IntPtr)); System.SpanHelpers.ClearPointerSizedWithReferences(ref System.Runtime.CompilerServices.Unsafe.As<T, IntPtr>(ref DangerousGetPinnableReference()), pointerSizeLength); } else { System.SpanHelpers.ClearPointerSizedWithoutReferences(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref DangerousGetPinnableReference()), byteLength); } } public unsafe void Fill(T value) { int length = _length; if (length == 0) { return; } if (System.Runtime.CompilerServices.Unsafe.SizeOf<T>() == 1) { byte b = System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref value); if (_pinnable == null) { IntPtr byteOffset = _byteOffset; System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(byteOffset.ToPointer(), b, (uint)length); } else { System.Runtime.CompilerServices.Unsafe.InitBlockUnaligned(ref System.Runtime.CompilerServices.Unsafe.As<T, byte>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset<T>(ref _pinnable.Data, _byteOffset)), b, (uint)length); } return; } ref T reference = ref DangerousGetPinnableReference(); int i; for (i = 0; i < (length & -8); i += 8) { System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 1) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 2) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 3) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 4) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 5) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 6) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 7) = value; } if (i < (length & -4)) { System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 1) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 2) = value; System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i + 3) = value; i += 4; } for (; i < length; i++) { System.Runtime.CompilerServices.Unsafe.Add<T>(ref reference, i) = value; } } public void CopyTo(Span<T> destination) { if (!TryCopyTo(destination)) { System.ThrowHelper.ThrowArgumentException_DestinationTooShort(); } } public bool TryCopyTo(Span<T> destination) { int length = _length; int length2 = destination._length; if (length == 0) { return true; } if ((uint)length > (uint)length2) { return false; } ref T src = ref DangerousGetPinnableReference(); System.SpanHelpers.CopyTo(ref destination.DangerousGetPinnableReference(), length2, ref src, length); return true; } public static bool operator ==(Span<T> left, Span<T> right) { if (left._length == right._length) { return System.Runtime.CompilerServices.Unsafe.AreSame<T>(ref left.DangerousGetPinnableReference(), ref right.DangerousGetPinnableReference()); } return false; } public static implicit operator ReadOnlySpan<T>(Span<T> span) { return new ReadOnlySpan<T>(span._pinnable, span._byteOffset, span._length); } public unsafe override string ToString() { if (typeof(T) == typeof(char)) { fixed (char* value = &System.Runtime.CompilerServices.Unsafe.As<T, char>(ref DangerousGetPinnableReference())) { return new string(value, 0, _length); } } return $"System.Span<{typeof(T).Name}>[{_length}]"; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span<T> Slice(int start) { if ((uint)start > (uint)_length) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } IntPtr byteOffset = _byteOffset.Add<T>(start); int length = _length - start; return new Span<T>(_pinnable, byteOffset, length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span<T> Slice(int start, int length) { if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start)) { System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.start); } IntPtr byteOffset = _byteOffset.Add<T>(start); return new Span<T>(_pinnable, byteOffset, length); } public T[] ToArray() { if (_length == 0) { return System.SpanHelpers.PerTypeValues<T>.EmptyArray; } T[] array = new T[_length]; CopyTo(array); return array; } [MethodImpl(MethodImplOptions.AggressiveInlining)] [EditorBrowsable(EditorBrowsableState.Never)] internal unsafe ref T DangerousGetPinnableReference() { if (_pinnable == null) { IntPtr byteOffset = _byteOffset; return ref System.Runtime.CompilerServices.Unsafe.AsRef<T>(byteOffset.ToPointer()); } return ref System.Runtime.CompilerServices.Unsafe.AddByteOffset<T>(ref _pinnable.Data, _byteOffset); } } internal sealed class SpanDebugView<T> { private readonly T[] _array; [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public T[] Items => _array; public SpanDebugView(Span<T> span) { _array = span.ToArray(); } public SpanDebugView(ReadOnlySpan<T> span) { _array = span.ToArray(); } } internal static class SpanHelpers { internal struct ComparerComparable<T, TComparer> : IComparable<T> where TComparer : IComparer<T> { private readonly T _value; private readonly TComparer _comparer; public ComparerComparable(T value, TComparer comparer) { _value = value; _comparer = comparer; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int CompareTo(T other) { return _comparer.Compare(_value, other); } } [StructLayout(LayoutKind.Sequential, Size = 64)] private struct Reg64 { } [StructLayout(LayoutKind.Sequential, Size = 32)] private struct Reg32 { } [StructLayout(LayoutKind.Sequential, Size = 16)] private struct Reg16 { } public static class PerTypeValues<T> { public static readonly bool IsReferenceOrContainsReferences = IsReferenceOrContainsReferencesCore(typeof(T)); public static readonly T[] EmptyArray = new T[0]; public static readonly IntPtr ArrayAdjustment = MeasureArrayAdjustment(); private static IntPtr MeasureArrayAdjustment() { T[] array = new T[1]; return System.Runtime.CompilerServices.Unsafe.ByteOffset<T>(ref System.Runtime.CompilerServices.Unsafe.As<Pinnable<T>>((object)array).Data, ref array[0]); } } private const ulong XorPowerOfTwoToHighByte = 283686952306184uL; private const ulong XorPowerOfTwoToHighChar = 4295098372uL; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int BinarySearch<T, TComparable>(this ReadOnlySpan<T> span, TComparable comparable) where TComparable : IComparable<T> { if (comparable == null) { System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.comparable); } return BinarySearch(ref MemoryMarshal.GetReference(span), span.Length, comparable); } public static int BinarySearch<T, TComparable>(ref T spanStart, int length, TComparable comparable) where TComparable : IComparable<T> { int num = 0; int num2 = length - 1; while (num <= num2) { int num3 = num2 + num >>> 1; int num4 = comparable.CompareTo(System.Runtime.CompilerServices.Unsafe.Add<T>(ref spanStart, num3)); if (num4 == 0) { return num3; } if (num4 > 0) { num = num3 + 1; } else { num2 = num3 - 1; } } return ~num; } public static int IndexOf(ref byte searchSpace, int searchSpaceLength, ref byte value, int valueLength) { if (valueLength == 0) { return 0; } byte value2 = value; ref byte second = ref System.Runtime.CompilerServices.Unsafe.Add<byte>(ref value, 1); int num = valueLength - 1; int num2 = 0; while (true) { int num3 = searchSpaceLength - num2 - num; if (num3 <= 0) { break; } int num4 = IndexOf(ref System.Runtime.CompilerServices.Unsafe.Add<byte>(ref searchSpace, num2), value2, num3); if (num4 == -1) { break; } num2 += num4; if (SequenceEqual(ref System.Runtime.CompilerServices.Unsafe.Add<byte>(ref searchSpace, num2 + 1), ref second, num)) { return num2; } num2++; } return -1; } public static int IndexOfAny(ref byte searchSpace, int searchSpaceLength, ref byte value, int valueLength) { if (valueLength == 0) { return 0; } int num = -1; for (int i = 0; i < valueLength; i++) { int num2 = IndexOf(ref searchSpace, System.Runtime.CompilerServices.Unsafe.Add<byte>(ref value, i), searchSpaceLength); if ((uint)num2 < (uint)num) { num = num2; searchSpaceLength = num2; if (num == 0) { break;