diff --git a/core/logic/smn_adt_array.cpp b/core/logic/smn_adt_array.cpp index 3b367241..954b58cb 100644 --- a/core/logic/smn_adt_array.cpp +++ b/core/logic/smn_adt_array.cpp @@ -582,5 +582,27 @@ REGISTER_NATIVES(cellArrayNatives) {"CloneArray", CloneArray}, {"FindStringInArray", FindStringInArray}, {"FindValueInArray", FindValueInArray}, + + // Transitional syntax support. + {"ArrayList.ArrayList", CreateArray}, + {"ArrayList.Clear", ClearArray}, + {"ArrayList.Length.get", GetArraySize}, + {"ArrayList.Resize", ResizeArray}, + {"ArrayList.Get", GetArrayCell}, + {"ArrayList.GetString", GetArrayString}, + {"ArrayList.GetArray", GetArrayArray}, + {"ArrayList.Push", PushArrayCell}, + {"ArrayList.PushString", PushArrayString}, + {"ArrayList.PushArray", PushArrayArray}, + {"ArrayList.Set", SetArrayCell}, + {"ArrayList.SetString", SetArrayString}, + {"ArrayList.SetArray", SetArrayArray}, + {"ArrayList.Erease", RemoveFromArray}, + {"ArrayList.ShiftUp", ShiftArrayUp}, + {"ArrayList.SwapAt", SwapArrayItems}, + {"ArrayList.Clone", CloneArray}, + {"ArrayList.FindString", FindStringInArray}, + {"ArrayList.FindValue", FindValueInArray}, + {NULL, NULL}, }; diff --git a/plugins/include/adt_array.inc b/plugins/include/adt_array.inc index f13a1e1a..c16934e0 100644 --- a/plugins/include/adt_array.inc +++ b/plugins/include/adt_array.inc @@ -1,7 +1,7 @@ /** - * vim: set ts=4 : + * vim: set ts=4 sw=4 tw=99 noet : * ============================================================================= - * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved. + * SourceMod (C)2004-2014 AlliedModders LLC. All rights reserved. * ============================================================================= * * This file is part of the SourceMod/SourcePawn SDK. @@ -45,13 +45,172 @@ stock ByteCountToCells(size) { if (!size) - { return 1; - } - return (size + 3) / 4; } +methodmap ArrayList < Handle { + // Creates a dynamic global cell array. While slower than a normal array, + // it can be used globally AND dynamically, which is otherwise impossible. + // + // The contents of the array are uniform; i.e. storing a string at index X + // and then retrieving it as an integer is NOT the same as StringToInt()! + // The "blocksize" determines how many cells each array slot has; it cannot + // be changed after creation. + // + // @param blocksize The number of cells each member of the array can + // hold. For example, 32 cells is equivalent to: + // new Array[X][32] + // @param startsize Initial size of the array. Note that data will + // NOT be auto-intialized. + // @return New Handle to the array object. + public native ArrayList(int blocksize=1, int startsize=0); + + // Clears an array of all entries. This is the same as Resize(0). + public native void ClearArray(); + + // Clones an array, returning a new handle with the same size and data. + // This should NOT be confused with CloneHandle. This is a completely new + // handle with the same data but no relation to the original. It should + // closed when no longer needed. + // + // @return New handle to the cloned array object + public native ArrayList Clone(); + + // Resizes an array. If the size is smaller than the current size, the + // array is truncated. + // + // @param newsize New size. + public native void Resize(int newsize); + + // Pushes a value onto the end of an array, adding a new index. + // + // This may safely be used even if the array has a blocksize greater + // than 1. + // + // @param value Value to push. + // @return Index of the new entry. + // @error Invalid Handle or out of memory. + // + public native int Push(any value); + + // Pushes a string onto the end of an array, truncating it if it is too big. + // + // @param value String to push. + // @return Index of the new entry. + public native int PushString(const char[] value); + + // Pushes an array of cells onto the end of an array. The cells + // are pushed as a block (i.e. the entire array sits at the index), + // rather than pushing each cell individually. + // + // @param values Block of values to copy. + // @param size If not set, the number of elements copied from the array + // will be equal to the blocksize. If set higher than the + // blocksize, the operation will be truncated. + // @return Index of the new entry. + public native int PushArray(const any[] values, int size=-1); + + // Retrieves a cell value from an array. + // + // @param index Index in the array. + // @param block Optionally specify which block to read from + // (useful if the blocksize > 0). + // @param asChar Optionally read as a byte instead of a cell. + // @return Value read. + // @error Invalid index. + public native any Get(int index, int block=0, bool asChar=false); + + // Retrieves a string value from an array. + // + // @param index Index in the array. + // @param buffer Buffer to copy to. + // @param maxlength Maximum size of the buffer. + // @return Number of characters copied. + // @error Invalid index. + public native int GetString(int index, char[] buffer, maxlength); + + // Retrieves an array of cells from an array. + // + // @param index Index in the array. + // @param buffer Buffer to store the array in. + // @param size If not set, assumes the buffer size is equal to the + // blocksize. Otherwise, the size passed is used. + // @return Number of cells copied. + // @error Invalid index. + public native int GetArray(int index, any[] buffer, int size=-1); + + // Sets a cell value in an array. + // + // @param index Index in the array. + // @param value Cell value to set. + // @param block Optionally specify which block to write to + // (useful if the blocksize > 0). + // @param asChar Optionally set as a byte instead of a cell. + // @error Invalid index, or invalid block. + public native void Set(int index, any value, int block=0, bool asChar=false); + + // Sets a string value in an array. + // + // @param index Index in the array. + // @param value String value to set. + // @return Number of characters copied. + // @error Invalid index. + public native void SetString(int index, const char[] value); + + // Sets an array of cells in an array. + // + // @param index Index in the array. + // @param values Array to copy. + // @param size If not set, assumes the buffer size is equal to the + // blocksize. Otherwise, the size passed is used. + // @return Number of cells copied. + // @error Invalid index. + public native void SetArray(int index, const any[] values, int size=-1); + + // Shifts an array up. All array contents after and including the given + // index are shifted up by one, and the given index is then "free." + // After shifting, the contents of the given index is undefined. + // + // @param index Index in the array to shift up from. + // @error Invalid index. + public native void ShiftUp(int index); + + // Removes an array index, shifting the entire array down from that position + // on. For example, if item 8 of 10 is removed, the last 3 items will then be + // (6,7,8) instead of (7,8,9), and all indexes before 8 will remain unchanged. + // + // @param index Index in the array to remove at. + // @error Invalid index. + public native void Erase(int index); + + // Swaps two items in the array. + // + // @param index1 First index. + // @param index2 Second index. + // @error Invalid index. + public native void SwapAt(int index1, int index2); + + // Returns the index for the first occurance of the provided string. If + // the string cannot be located, -1 will be returned. + // + // @param item String to search for + // @return Array index, or -1 on failure + public native int FindString(const char[] item); + + // Returns the index for the first occurance of the provided value. If the + // value cannot be located, -1 will be returned. + // + // @param item Value to search for + // @return Array index, or -1 on failure + public native int FindValue(any item); + + // Retrieve the size of the array. + property int Length { + public native get(); + } +}; + /** * Creates a dynamic global cell array. While slower than a normal array, * it can be used globally AND dynamically, which is otherwise impossible. @@ -68,16 +227,15 @@ stock ByteCountToCells(size) * NOT be auto-intialized. * @return New Handle to the array object. */ -native ArrayList:CreateArray(blocksize=1, startsize=0); +native ArrayList CreateArray(int blocksize=1, int startsize=0); /** * Clears an array of all entries. This is the same as ResizeArray(0). * * @param array Array Handle. - * @noreturn * @error Invalid Handle. */ -native ClearArray(Handle:array); +native void ClearArray(Handle array); /** * Clones an array, returning a new handle with the same size and data. This should NOT @@ -88,7 +246,7 @@ native ClearArray(Handle:array); * @return New handle to the cloned array object * @error Invalid Handle */ -native Handle:CloneArray(Handle:array); +native Handle CloneArray(Handle array); /** * Resizes an array. If the size is smaller than the current size, @@ -99,7 +257,7 @@ native Handle:CloneArray(Handle:array); * @noreturn * @error Invalid Handle or out of memory. */ -native bool:ResizeArray(Handle:array, newsize); +native bool ResizeArray(Handle array, int newsize); /** * Returns the array size. @@ -108,7 +266,7 @@ native bool:ResizeArray(Handle:array, newsize); * @return Number of elements in the array. * @error Invalid Handle. */ -native GetArraySize(Handle:array); +native int GetArraySize(Handle array); /** * Pushes a value onto the end of an array, adding a new index. @@ -121,7 +279,7 @@ native GetArraySize(Handle:array); * @return Index of the new entry. * @error Invalid Handle or out of memory. */ -native PushArrayCell(Handle:array, any:value); +native int PushArrayCell(Handle array, any value); /** * Pushes a string onto the end of an array, truncating it @@ -132,7 +290,7 @@ native PushArrayCell(Handle:array, any:value); * @return Index of the new entry. * @error Invalid Handle or out of memory. */ -native PushArrayString(Handle:array, const String:value[]); +native int PushArrayString(Handle array, const char[] value); /** * Pushes an array of cells onto the end of an array. The cells @@ -147,7 +305,7 @@ native PushArrayString(Handle:array, const String:value[]); * @return Index of the new entry. * @error Invalid Handle or out of memory. */ -native PushArrayArray(Handle:array, const any:values[], size=-1); +native int PushArrayArray(Handle array, const any[] values, int size=-1); /** * Retrieves a cell value from an array. @@ -160,7 +318,7 @@ native PushArrayArray(Handle:array, const any:values[], size=-1); * @return Value read. * @error Invalid Handle, invalid index, or invalid block. */ -native any:GetArrayCell(Handle:array, index, block=0, bool:asChar=false); +native any GetArrayCell(Handle array, int index, int block=0, bool asChar=false); /** * Retrieves a string value from an array. @@ -172,7 +330,7 @@ native any:GetArrayCell(Handle:array, index, block=0, bool:asChar=false); * @return Number of characters copied. * @error Invalid Handle or invalid index. */ -native GetArrayString(Handle:array, index, String:buffer[], maxlength); +native int GetArrayString(Handle array, int index, char[] buffer, maxlength); /** * Retrieves an array of cells from an array. @@ -185,7 +343,7 @@ native GetArrayString(Handle:array, index, String:buffer[], maxlength); * @return Number of cells copied. * @error Invalid Handle or invalid index. */ -native GetArrayArray(Handle:array, index, any:buffer[], size=-1); +native int GetArrayArray(Handle array, int index, any[] buffer, int size=-1); /** * Sets a cell value in an array. @@ -196,10 +354,9 @@ native GetArrayArray(Handle:array, index, any:buffer[], size=-1); * @param block Optionally specify which block to write to * (useful if the blocksize > 0). * @param asChar Optionally set as a byte instead of a cell. - * @noreturn * @error Invalid Handle, invalid index, or invalid block. */ -native SetArrayCell(Handle:array, index, any:value, block=0, bool:asChar=false); +native void SetArrayCell(Handle array, int index, any value, int block=0, bool asChar=false); /** * Sets a string value in an array. @@ -210,7 +367,7 @@ native SetArrayCell(Handle:array, index, any:value, block=0, bool:asChar=false); * @return Number of characters copied. * @error Invalid Handle or invalid index. */ -native SetArrayString(Handle:array, index, const String:value[]); +native int SetArrayString(Handle array, int index, const char[] value); /** * Sets an array of cells in an array. @@ -223,7 +380,7 @@ native SetArrayString(Handle:array, index, const String:value[]); * @return Number of cells copied. * @error Invalid Handle or invalid index. */ -native SetArrayArray(Handle:array, index, const any:values[], size=-1); +native int SetArrayArray(Handle array, int index, const any[] values, int size=-1); /** * Shifts an array up. All array contents after and including the given @@ -232,10 +389,9 @@ native SetArrayArray(Handle:array, index, const any:values[], size=-1); * * @param array Array Handle. * @param index Index in the array to shift up from. - * @noreturn * @error Invalid Handle or invalid index. */ -native ShiftArrayUp(Handle:array, index); +native void ShiftArrayUp(Handle array, int index); /** * Removes an array index, shifting the entire array down from that position @@ -244,10 +400,9 @@ native ShiftArrayUp(Handle:array, index); * * @param array Array Handle. * @param index Index in the array to remove at. - * @noreturn * @error Invalid Handle or invalid index. */ -native RemoveFromArray(Handle:array, index); +native void RemoveFromArray(Handle array, int index); /** * Swaps two items in the array. @@ -255,10 +410,9 @@ native RemoveFromArray(Handle:array, index); * @param array Array Handle. * @param index1 First index. * @param index2 Second index. - * @noreturn * @error Invalid Handle or invalid index. */ -native SwapArrayItems(Handle:array, index1, index2); +native void SwapArrayItems(Handle array, int index1, int index2); /** * Returns the index for the first occurance of the provided string. If the string @@ -269,7 +423,7 @@ native SwapArrayItems(Handle:array, index1, index2); * @return Array index, or -1 on failure * @error Invalid Handle */ -native FindStringInArray(Handle:array, const String:item[]); +native int FindStringInArray(Handle array, const char[] item); /** * Returns the index for the first occurance of the provided value. If the value @@ -280,28 +434,4 @@ native FindStringInArray(Handle:array, const String:item[]); * @return Array index, or -1 on failure * @error Invalid Handle */ -native FindValueInArray(Handle:array, any:item); - -methodmap ArrayList < Handle { - public ArrayList() = CreateArray; - public Clear() = ClearArray; - public Clone() = CloneArray; - public Resize() = ResizeArray; - public Push() = PushArrayCell; - public PushString() = PushArrayString; - public PushArray() = PushArrayArray; - public Get() = GetArrayCell; - public GetString() = GetArrayString; - public GetArray() = GetArrayArray; - public Set() = SetArrayCell; - public SetString() = SetArrayString; - public SetArray() = SetArrayArray; - public ShiftUp() = ShiftArrayUp; - public Erase() = RemoveFromArray; - public SwapAt() = SwapArrayItems; - public FindString() = FindStringInArray; - public FindValue() = FindValueInArray; - property int Length { - public get() = GetArraySize; - } -}; +native int FindValueInArray(Handle array, any item); diff --git a/plugins/include/handles.inc b/plugins/include/handles.inc index 7312e2e7..24e46130 100644 --- a/plugins/include/handles.inc +++ b/plugins/include/handles.inc @@ -80,7 +80,7 @@ native Handle:CloneHandle(Handle:hndl, Handle:plugin=INVALID_HANDLE); */ methodmap Handle __nullable__ { - public Clone() = CloneHandle; + public Close() = CloseHandle; public ~Handle() = CloseHandle; }