Core Function Pointer Read/Write Bracket

From Sputnik Wiki
Jump to: navigation, search
$ptr[<index>]:<type> = <value>
$ptr[<index>]::<type> = <value>

Contents

Description

Read and Write to memory pointers similar to PTRRead() and PTRWrite() but using [] to simulate like it's an array

$ptr

The variable that will be treated as an contains an IntPtr/UIntPtr.

index

The index of the value to get.

If you use only one : then the index will act like an array of that object size so if you are reading Int64 values index 0 will be 0 but index 1 will really be index 8 since it will push up the indexes by the index * object size.

However if you do two : like :: then the index is a literal index so index 0 is 0 and 1 is 1 it will take the object size into account.

type

The type of value to read possible types are (case-sensitive):

c = Char (Non-Unicode) [1 byte in size]
C = Char (Unicode) [2 bytes in size]
b = byte (signed) [1 byte in size]
B = byte (unsigned) [1 byte in size]
s = short (signed) [2 bytes in size]
i = int (signed) [4 bytes in size]
l = long (signed) [8 bytes in size]
S = ushort (unsigned) [2 bytes in size]
I = uint (unsigned) [4 bytes in size]
L = ulong (unsigned) [8 bytes in size]
f = float [4 bytes in size]
d = Double [8 bytes in size]
t = Pointer (signed) [(depends if running 32bit or 64bit Sputnik)]
T = UPointer (unsigned) [(depends if running 32bit or 64bit Sputnik)]

Return Value

If you are only using like $a = $ptr[<index>]::<type>; then it will return the value at that location or NULL if it can't obtain one.

Remarks

This can be used to effortlessly read from pointers that you allocate using Alloc() or obtain from calling some DLL etc.

Example

Basic example read/write floats

// Allocate memory
$ptr = Alloc(10, 0x00);
// Write floats using ARRAY index
// this causes index 1 to be 4 bytes in)
$ptr[0]:f = 133.77;
$ptr[1]:f = 777.42;
// Print whats currently in the array
say $ptr[0]:f; // 133.77
say $ptr[1]:f; // 777.42
// Free up ram when we are done
free($ptr);

Using += (works for all operators like that

// Allocate memory
$ptr = Alloc(3, 0x00);
// Write floats using ARRAY index
// this causes index 1 to be 4 bytes in)
$ptr[0]:f = 133.77;
$ptr[0]:f += 100;
// Print whats currently in the array
say $ptr[0]:f; // 233.77
// Free up ram when we are done
free($ptr);

You get a wildly different result if you write to the address directly instead of stacking the object size in bytes

// Allocate memory
$ptr = Alloc(10, 0x00);
// Write floats using direct index
// this causes index 1 to be 1 byte in)
$ptr[0]::f = 133.77;
$ptr[1]::f = 777.42;
// Print whats currently in the array
say $ptr[0]:f; // 54.71984
say $ptr[1]:f; // 9.52883E-44
// Free up ram when we are done
free($ptr);

Example of writing multiple variable types

// Allocate memory
$ptr = Alloc(10, 0x00);
// Write using direct index
// this causes index 1 to be 1 byte in)
$ptr[0]::f = 133.77;
$ptr[4]::i = 101;
$ptr[8]::f = 942.77;
// Print whats currently in the array
say $ptr[0]::f; // 133.77
say $ptr[4]::i; // 101
say $ptr[8]::f; // 942.77
// Free up ram when we are done
free($ptr);

Large Example

Class ByteWriter
{
	my $_data;
	my $_capacity;
	my $Length = 0;
	my $Capacity
	{
		get
		{
			return $this->$_capacity;
		}
		set
		{
			if($value <= 0)
			{
				if(IsVarIntPtr($this->$_data))
					free($this->$_data);
				$this->$_data = null;
				$this->$_capacity = 0;
			}
			else
			{
				if(IsVarIntPtr($this->$_data))
				{
					my $OldPtr = $this->$_data;
					$this->$_data = Alloc($value, 0x00);
					for(my $i = 0; $i < $this->$_capacity; $i++)
						$this->$_data[$i]::B = $OldPtr[$i]::B;
					$this->$_capacity = $value;
					free($OldPtr);
				}
				else
				{
					$this->$_data = Alloc($value, 0x00);
					$this->$_capacity = $value;
				}
			}
		}
	};
	Function __Construct( $size = 0 )
	{
		if($size <= 0)
			$size = 10;
		$this->$Capacity = $size;
		$Length = 0;
	}
	Function WriteByte( $value )
	{
		if ($Length == $Capacity)
			$this->$Capacity = 2 * $this->$Capacity;
		$this->$_data[$Length]::B = $value;
		$Length++;
	}
	Function WriteBytes( $value )
	{
		foreach((Binary)$value as my $byte)
			WriteByte($byte);
	}
	Function WritePack( $format, $value )
	{
		my $bin = Pack($format, $value);
		foreach($bin as my $byte)
			WriteByte($byte);
	}
	Function DebugPrint( )
	{
		say "Capacity: " . $this->$Capacity;
		say "Length: " . $Length;
		for(my $i = 0; $i < $Length; $i++)
		{
			my $byte = $_data[$i]::B;
			printf("Index '%02d' Byte '%02d' Hex '%X' Char '%c'\n", $i, $byte, $byte, $byte);
		}
	}
};
my $s = new ByteWriter();
$s->WriteByte(20);
$s->WriteByte(30);
$s->WriteByte(40);
$s->WriteByte(50);
$s->WriteByte(60);
$s->WriteBytes(bin(1,2,3,4,5,6,7));
$s->WritePack("z0", "Hello");
 
$s->DebugPrint();
 
// Prints
// Capacity: 20
// Length: 17
// Index '00' Byte '20' Hex '14' Char '¶'
// Index '01' Byte '30' Hex '1E' Char '▲'
// Index '02' Byte '40' Hex '28' Char '('
// Index '03' Byte '50' Hex '32' Char '2'
// Index '04' Byte '60' Hex '3C' Char '<'
// Index '05' Byte '01' Hex '1' Char '☺'
// Index '06' Byte '02' Hex '2' Char '☻'
// Index '07' Byte '03' Hex '3' Char '♥'
// Index '08' Byte '04' Hex '4' Char '♦'
// Index '09' Byte '05' Hex '5' Char '♣'
// Index '10' Byte '06' Hex '6' Char '♠'
// Index '11' Byte '07' Hex '7' Char ''
// Index '12' Byte '72' Hex '48' Char 'H'
// Index '13' Byte '101' Hex '65' Char 'e'
// Index '14' Byte '108' Hex '6C' Char 'l'
// Index '15' Byte '108' Hex '6C' Char 'l'
// Index '16' Byte '111' Hex '6F' Char 'o'
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox