public ItemStack(Item item, int number) { XDebug.Assert(item != null); XDebug.Assert(number > 0); this.item = item; this.number = number; }
public T[] ArrayEmpty <T>() { T[] array = Enumerable.Empty <T>() as T[]; XDebug.Assert(array != null); // Defensively guard against a version of Linq where Enumerable.Empty<T> doesn't // return an array, but throw in debug versions so a better strategy can be // used if that ever happens. return(array ?? new T[0]); }
public override void Init(object[] args) { var result = StageName.TryParse((string)args[0], out _name); XDebug.Assert(result); if (args.Length > 1) { _overrideSpawn = args[1].ToString(); } }
public static void Detach(object obj) { XDebug.Assert(_subscriberMethodList.ContainsKey(obj), obj + " is not attached!"); var listenMethods = GetListenMethods(obj); var cacheList = _subscriberMethodList[obj]; for (int i = 0; i < listenMethods.Length; ++i) { var t = listenMethods[i].method.GetParameters()[0].ParameterType; UnlistenInternal(t, cacheList[i]); } _subscriberMethodList.Remove(obj); }
// Returns size of hashtable to grow to. public int ExpandPrime(int oldSize) { var newSize = 2 * oldSize; // Allow the hashtables to grow to maximum possible size (~2G elements) before encoutering capacity overflow. // Note that this check works even when _items.Length overflowed thanks to the (uint) cast if ((uint)newSize > MaxPrimeArrayLength && MaxPrimeArrayLength > oldSize) { XDebug.Assert(MaxPrimeArrayLength == GetPrime(MaxPrimeArrayLength), "Invalid MaxPrimeArrayLength"); return(MaxPrimeArrayLength); } return(GetPrime(newSize)); }
public object Evaluate(ExpressionContext ctx) { XDebug.Assert(entries.Length > 0, "Must have function name"); var firstEntry = entries[0]; XDebug.Assert(firstEntry is ID, "Expression call first element must be ID"); var functionNameHash = new StringHash(((ID)firstEntry).id); if (_functionInstance == null) { _functionInstance = FunctionRegistry.GetFunction(functionNameHash); _functionInstance.Init(entries.Skip(1).ToArray()); } return(_functionInstance.Invoke(ctx)); }
// Attach and detach subscribers // Subscribers are objects whose class contain methods with [SubscribeEvent] attribute. // All methods must have exactly 1 parameter specifying the listening event type. // Attach and Detach adds/removes all the subscribed methods declared in class. public static void Attach(object obj) { XDebug.Assert(!_subscriberMethodList.ContainsKey(obj), "Can't attach " + obj + " twice!"); var listenMethods = GetListenMethods(obj); var cacheList = new List <Delegate>(); foreach (var m in listenMethods) { XDebug.Assert(m.method.GetParameters().Length == 1, "Event listener method must have 1 parameter"); var t = m.method.GetParameters()[0].ParameterType; var del = Delegate.CreateDelegate(Expression.GetActionType(t), obj, m.method.Name); ListenInternal(t, del, m.priority); cacheList.Add(del); } _subscriberMethodList.Add(obj, cacheList); }
public void DrainHunger(float value) { XDebug.Assert(value >= 0); if (overflowHunger > 0) { if (overflowHunger > value) { overflowHunger -= value; value = 0f; } else { overflowHunger = 0f; value -= overflowHunger; } } SetHunger(currentHunger - value); }
private void OnOverrideTextureSignal(Component sender, object data) { Texture texture = data as Texture; if (!texture || !m_MaterialWithTexture || !m_Renderer) { XDebug.Assert(texture, this, "Signal data doesn't contain a texture."); XDebug.Assert(m_MaterialWithTexture, this, "Material reference missing."); XDebug.Assert(m_Renderer, this, "Renderer missing."); return; } Material mat = m_Renderer.materials.FirstOrDefault(MatchingMaterial); if (!mat) { XDebug.LogWarning(this, "No matching texture: " + m_MaterialWithTexture.name); return; } mat.mainTexture = texture; }
private int CvtIndex(int i, int j, out bool isInverse) { XDebug.Assert(i < dimension && j < dimension); if (i > j) { int tmp = i; i = j; j = tmp; isInverse = true; } else { isInverse = false; } int baseIdx = (i * (2 * dimension - 1 - i)) / 2; // dim=5 i=1: 1 * (10 - 2) / 2 = 4 // dim=5 i=2: 2 * (10 - 3) / 2 = 7 return(baseIdx + (j - i - 1)); }
public static void ComponentMoveToBottomMenu(MenuCommand command) { Component component = command.context as Component; if (component != null) { Component[] components = component.gameObject.GetComponents <Component>(); int index = components.IndexOf(component); XDebug.Assert(index >= 0, component, "component not found in components... huh??"); int numSpacesToMove = components.Length - 1 - index; if (numSpacesToMove > 0) { Undo.RegisterCompleteObjectUndo(component.gameObject, string.Format("Move {0} to bottom", component.GetType().Name)); for (int i = 0; i < numSpacesToMove; i++) { ComponentUtility.MoveComponentDown(component); } } } }
public static void ComponentMoveToTopMenu(MenuCommand command) { Component component = command.context as Component; if (component != null) { Component[] components = component.gameObject.GetComponents <Component>(); int index = components.IndexOf(component); XDebug.Assert(index >= 0, component, "component not found in components... huh??"); int numSpacesToMove = index - 1; // -1 because we want Transform to still be at the top! if (numSpacesToMove > 0) { Undo.RegisterCompleteObjectUndo(component.gameObject, string.Format("Move {0} to top", component.GetType().Name)); for (int i = 0; i < numSpacesToMove; i++) { ComponentUtility.MoveComponentUp(component); } } } }
private void Resize <TKey, TValue>(Root.Code.Models.E01D.Core.Collections.Generic.Dictionary <TKey, TValue> dictionary, int newSize, bool forceNewHashCodes) { XDebug.Assert(newSize >= dictionary.Entries.Length); var newBuckets = new int[newSize]; for (var i = 0; i < newBuckets.Length; i++) { newBuckets[i] = -1; } var newEntries = new DictionaryEntry <TKey, TValue> [newSize]; Array.Copy(dictionary.Entries, 0, newEntries, 0, dictionary.Count); if (forceNewHashCodes) { for (var i = 0; i < dictionary.Count; i++) { if (newEntries[i].HashCode != -1) { newEntries[i].HashCode = (dictionary.Comparer.GetHashCode(newEntries[i].Key) & 0x7FFFFFFF); } } } for (var i = 0; i < dictionary.Count; i++) { if (newEntries[i].HashCode >= 0) { var bucket = newEntries[i].HashCode % newSize; newEntries[i].Next = newBuckets[bucket]; newBuckets[bucket] = i; } } dictionary.Buckets = newBuckets; dictionary.Entries = newEntries; }
public void AddEnumerable <T>(Root.Code.Models.E01D.Core.Collections.Generic.List <T> list, IEnumerable <T> enumerable) { XDebug.Assert(enumerable != null); XDebug.Assert(!(enumerable is Collection_I <T>), "We should have optimized for this beforehand."); using (var en = enumerable.GetEnumerator()) { list.Version++; // Even if the enumerable has no items, we can update list.Version. while (en.MoveNext()) { // Capture Current before doing anything else. If this throws // an exception, we want to make a clean break. var current = en.Current; if (list.Count == list.Items.Length) { EnsureCapacity(list, list.Count + 1); } list.Items[list.Count++] = current; } } }
public Task <T> FromCanceled <T>(CancellationToken cancellationToken) { XDebug.Assert(cancellationToken.IsCancellationRequested); return(new Task <T>(() => default(T), cancellationToken)); }
public static Task <int> ReadAsync(this TextReader reader, char[] buffer, int index, int count, CancellationToken cancellationToken) { XDebug.Assert(reader != null); return(cancellationToken.IsCancellationRequested ? XAsync.Api.CancellationTokens.FromCanceled <int>(cancellationToken) : reader.ReadAsync(buffer, index, count)); }
public void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, ArrayPoolApi_I <char> bufferPool, ref char[] writeBuffer) { // leading delimiter if (appendDelimiters) { writer.Write(delimiter); } if (!string.IsNullOrEmpty(s)) { int lastWritePosition = FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling); if (lastWritePosition == -1) { writer.Write(s); } else { if (lastWritePosition != 0) { if (writeBuffer == null || writeBuffer.Length < lastWritePosition) { writeBuffer = XMemory.EnsureBufferSize(bufferPool, lastWritePosition, writeBuffer); } // write unchanged chars at start of text. s.CopyTo(0, writeBuffer, 0, lastWritePosition); writer.Write(writeBuffer, 0, lastWritePosition); } int length; for (int i = lastWritePosition; i < s.Length; i++) { char c = s[i]; if (c < charEscapeFlags.Length && !charEscapeFlags[c]) { continue; } string escapedValue; switch (c) { case '\t': escapedValue = @"\t"; break; case '\n': escapedValue = @"\n"; break; case '\r': escapedValue = @"\r"; break; case '\f': escapedValue = @"\f"; break; case '\b': escapedValue = @"\b"; break; case '\\': escapedValue = @"\\"; break; case '\u0085': // Next Line escapedValue = @"\u0085"; break; case '\u2028': // Line Separator escapedValue = @"\u2028"; break; case '\u2029': // Paragraph Separator escapedValue = @"\u2029"; break; default: if (c < charEscapeFlags.Length || stringEscapeHandling == StringEscapeHandling.EscapeNonAscii) { if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml) { escapedValue = @"\'"; } else if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml) { escapedValue = @"\"""; } else { if (writeBuffer == null || writeBuffer.Length < UnicodeTextLength) { writeBuffer = XMemory.EnsureBufferSize(bufferPool, UnicodeTextLength, writeBuffer); } XStrings.ToCharAsUnicode(c, writeBuffer); // slightly hacky but it saves multiple conditions in if test escapedValue = EscapedUnicodeText; } } else { escapedValue = null; } break; } if (escapedValue == null) { continue; } bool isEscapedUnicodeText = string.Equals(escapedValue, EscapedUnicodeText); if (i > lastWritePosition) { length = i - lastWritePosition + ((isEscapedUnicodeText) ? UnicodeTextLength : 0); int start = (isEscapedUnicodeText) ? UnicodeTextLength : 0; if (writeBuffer == null || writeBuffer.Length < length) { char[] newBuffer = XMemory.RentBuffer(bufferPool, length); // the unicode text is already in the buffer // copy it over when creating new buffer if (isEscapedUnicodeText) { XDebug.Assert(writeBuffer != null, "Write buffer should never be null because it is set when the escaped unicode text is encountered."); Array.Copy(writeBuffer, newBuffer, UnicodeTextLength); } XMemory.ReturnBuffer(bufferPool, writeBuffer); writeBuffer = newBuffer; } s.CopyTo(lastWritePosition, writeBuffer, start, length - start); // write unchanged chars before writing escaped text writer.Write(writeBuffer, start, length - start); } lastWritePosition = i + 1; if (!isEscapedUnicodeText) { writer.Write(escapedValue); } else { writer.Write(writeBuffer, 0, UnicodeTextLength); } } XDebug.Assert(lastWritePosition != 0); length = s.Length - lastWritePosition; if (length > 0) { if (writeBuffer == null || writeBuffer.Length < length) { writeBuffer = XMemory.EnsureBufferSize(bufferPool, length, writeBuffer); } s.CopyTo(lastWritePosition, writeBuffer, 0, length); // write remaining text writer.Write(writeBuffer, 0, length); } } } // trailing delimiter if (appendDelimiters) { writer.Write(delimiter); } }
// From 4.6 on we could use Task.FromCanceled(), but we need an equivalent for // previous frameworks. public Task FromCanceled(CancellationToken cancellationToken) { XDebug.Assert(cancellationToken.IsCancellationRequested); return(new Task(() => { }, cancellationToken)); }
public static Task WriteAsync(this TextWriter writer, char value, CancellationToken cancellationToken) { XDebug.Assert(writer != null); return(cancellationToken.IsCancellationRequested ? XAsync.Api.CancellationTokens.FromCanceled(cancellationToken) : writer.WriteAsync(value)); }
protected void Start() { SignalManager.Register(this, m_Signal, OnOverrideTextureSignal); XDebug.AssertRequiresComponent(m_Renderer, this); XDebug.Assert(m_MaterialWithTexture, this, "Material reference missing."); }