Core Function DotNet

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Remarks)
(Example)
 
(10 intermediate revisions by one user not shown)
Line 32: Line 32:
  
 
The .NET objects you create support their operator overloads such as += *= etc so this makes using .NET classes much easier almost seamless.
 
The .NET objects you create support their operator overloads such as += *= etc so this makes using .NET classes much easier almost seamless.
 +
 +
If you place a .NET DLL you compiled in C#/VB etc in the Sputnik folder and set the proper information in the Use() function you can then create/use the classes and methods etc in that DLL just like you can with all the examples below.
 +
 +
Alternatively you can use Sputnik to compile (see the function Compile()) some C# code into a DLL and use that instead.
  
 
Note - When you use strings it uses Sputnik escapes and not C#/.NET ones (Which is fine since Sputnik has them all anyway and a lot more).
 
Note - When you use strings it uses Sputnik escapes and not C#/.NET ones (Which is fine since Sputnik has them all anyway and a lot more).
 +
 +
=== Capabilities & Limitations ===
 +
 +
This will always contain the current limitations of this stuff for Sputnik it may change over time.
 +
 +
* Sputnik can create .NET DLLs from given C# source code and use these DLLs to provide more functionality to Sputnik.
 +
 +
* Sputnik can compile C# source code in RAM to instant gain access to new classes and functions seamlessly.
 +
 +
* Sputnik does not support compiling of VisualBasic etc code only C#.
 +
 +
* Sputnik will load the required DLL when searching for a TYPE to create using the ->new().
 +
 +
* Sputnik can call static methods in any given .NET class.
 +
 +
* Sputnik can spawn a .NET object (like a class) as an instance and access its instance (non-static) functions and variables etc.
 +
 +
* Sputnik can get/set fields/properties using the following data types: Any .NET created object or Null, Bool, Char, SByte, Byte, Int16, Int32, Int64, Float, Double, IntPtr, UIntPtr, String, Null[], Bool[], Char[], SByte[], Byte[], Int16[], Int32[], Int64[], Float[], Double[], IntPtr[], UIntPtr[], String[], List<Null>, List<Bool>, List<Char>, List<SByte>, List<Byte>, List<Int16>, List<Int32>, List<Int64>, List<Float>, List<Double>, List<IntPtr>, List<UIntPtr>, List<String>
 +
 +
* Sputnik can get from a return value or send as a parameter to a function the following data types: Any .NET created object or Null, Bool, Char, SByte, Byte, Int16, Int32, Int64, Float, Double, IntPtr, UIntPtr, String, Null[], Bool[], Char[], SByte[], Byte[], Int16[], Int32[], Int64[], Float[], Double[], IntPtr[], UIntPtr[], String[], List<Null>, List<Bool>, List<Char>, List<SByte>, List<Byte>, List<Int16>, List<Int32>, List<Int64>, List<Float>, List<Double>, List<IntPtr>, List<UIntPtr>, List<String>
 +
 +
* Sputnik does not currently recognize optional parameters so full parameters are required.
 +
 +
* Sputnik does handle the following operator overloads when used on .NET classes: + - * / % & ^ | += -= *= /= %= &= ^= |=
 +
 +
* Sputnik currently does not support == != < > <= >= || && operator overloads on .NET classes.
 +
 +
* Sputnik does not currently handling of the .NET cast overloads (However .NET objects are converted to/from Sputnik SV as needed).
 +
 +
* Sputnik only supports the converting to/from SV for (up to one array bracket to use more you will need to create that .NET object (%String[][]->new()) instead of trying to use a Sputnik AV) any of the following data types: Any .NET created object or Null, Bool, Char, SByte, Byte, Int16, Int32, Int64, Float, Double, IntPtr, UIntPtr, String, Null[], Bool[], Char[], SByte[], Byte[], Int16[], Int32[], Int64[], Float[], Double[], IntPtr[], UIntPtr[], String[], List<Null>, List<Bool>, List<Char>, List<SByte>, List<Byte>, List<Int16>, List<Int32>, List<Int64>, List<Float>, List<Double>, List<IntPtr>, List<UIntPtr>, List<String>
 +
 +
* Sputnik does not currently support handling of the .NET objects ability to switch its type to a given type (All type changes are currently handled by converting the .NET type to the equal Sputnik SV type.
 +
 +
* Sputnik does not currently support the use of the With() statement for .NET objects it is still only for Sputnik classes.
  
 
=== Example ===
 
=== Example ===
Line 124: Line 162:
 
// String value is ABC
 
// String value is ABC
 
// StringLen value is 3
 
// StringLen value is 3
</syntaxhighlight>
 
 
Example of using Sputniks built in .NET Vector3 class
 
 
<syntaxhighlight lang="sputnik">
 
// Make note of operator overloading of the .NET object
 
// is available here as if it was a Sputnik class
 
$v1 = %Vector3->new(10, 20, 30);
 
$v2 = %Vector3->new(2, 4, 6);
 
say("V1: " . $v1);
 
$v1 += $v2;
 
say("V1: " . $v1);
 
say("V1 Length: ". $v1->$Length);
 
$v1->CrossProduct($v2);
 
say("V1: " . $v1);
 
// Prints:
 
// V1: (10, 20, 30)
 
// V1: (12, 24, 36)
 
// V1 Length: 26.83282
 
// V1: (12, 24, 36)
 
</syntaxhighlight>
 
 
 
Example of using .NETs StringBuilder class and wrapping it inside a Sputnik class
 
 
<syntaxhighlight lang="sputnik">
 
// You will see wrapping the .NET object to a Sputnik one is trivial
 
// However in this case you probably wouldn't really need to
 
// since the object (StringBuilder) is already a class and provides
 
// most of these functions anyhow
 
use("System");
 
use("System.Text");
 
 
Class DotNetStringBuilder
 
{
 
my $sb;
 
my $NewLine = @N;
 
Function __Construct($InitSize = 0)
 
{
 
$this->$sb = %StringBuilder->new((int)$InitSize);
 
}
 
Function __Destruct()
 
{
 
unset($this->$sb);
 
}
 
Function Append()
 
{
 
foreach( @args as my $arg )
 
$this->$sb->Append((string)$arg);
 
}
 
Function AppendLine()
 
{
 
foreach( @args as my $arg )
 
$this->$sb->Append((string)$arg . $this->$NewLine);
 
}
 
Function AppendRear()
 
{
 
my $ID = 0;
 
foreach( @args as my $arg)
 
{
 
my $oldLen = (int)$this->$sb->$Length;
 
$this->$sb->Insert((int)$ID, (string)$arg);
 
$ID += (int)$this->$sb->$Length - $oldLen;
 
}
 
}
 
Function AppendRearLine()
 
{
 
my $ID = 0;
 
foreach( @args as my $arg )
 
{
 
my $oldLen = (int)$this->$sb->$Length;
 
$this->$sb->Insert((int)$ID, (string)$arg . $this->$NewLine);
 
$ID += (int)$this->$sb->$Length - $oldLen;
 
}
 
}
 
Function Insert($ID = 0)
 
{
 
If ( $ID < 0 || $ID > $this->$sb->$Length)
 
return 0;
 
my $args = @args;
 
Next($args);
 
foreach( $args as my $arg )
 
@{
 
$this->$sb->Insert((int)$ID, (string)$arg);
 
$ID++;
 
}
 
}
 
Function InsertLine($ID = 0)
 
{
 
If ( $ID < 0 || $ID > $this->$sb->$Length)
 
return 0;
 
my $args = @args;
 
Next($args);
 
foreach( $args as my $arg )
 
@{
 
$this->$sb->Insert((int)$ID, (string)$arg . $NewLine);
 
$ID++;
 
}
 
}
 
Function Remove($StartID, $Length)
 
{
 
if($StartID < 0 || $StartID > $this->$sb->$Length)
 
return;
 
if($Length > $StartID + $this->$sb->$Length)
 
return;
 
$this->$sb->Remove((int)$StartID, (int)$Length);
 
}
 
Function Clear()
 
{
 
$this->$sb->$Length = 0;
 
$this->$sb->$Capacity = 0;
 
}
 
Function Length()
 
{
 
return $sb->$Length;
 
}
 
Function toString()
 
{
 
return (string)$sb;
 
}
 
Operator "string"
 
{
 
return (string)$sb;
 
}
 
Operator "=" ($x)
 
{
 
Clear();
 
$this->$sb->Append((string)$x);
 
}
 
Operator ".=" ($x)
 
{
 
$this->$sb->Append((string)$x);
 
}
 
Operator "..=" ($x)
 
{
 
$this->$sb->Insert(0, (string)$x);
 
}
 
};
 
 
$a = new DotNetStringBuilder();
 
$a->Append("Cat");
 
$a->Append("FoX!");
 
$a->Append("hehehe");
 
$a->AppendRear("Meow");
 
$a .= "Test";
 
$a ..= "Hmm";
 
say("Length: " . $a->Length());
 
say("Text: $a");
 
// Prints
 
// Length: 15
 
// Text: HmmMeowCatFoX!heheheTest
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 14:53, 21 June 2015

%Name

Contents

Description

Create/use .NET objects and call instance/static method and get/set properties.

Sputnik is made in C# and C# is a .NET language.

Sputnik keeps all the .NET stuff hidden away and out of view and provides its own functions and features for you to use.

However if you wish to use something from .NET Sputnik has a special operator that lets you do that.

You can use the % operator with an Identifier to specify the creation/use of a .NET class see the examples below.

This .NET object support is by no means complete it is never meant to be 100% since if you wanted 100% you wouldn't be using Sputnik however it is good enough to provide a layer of support should you think Sputnik is missing something that can be provided by something within the .NET library.

Of course you can look into DLLImport() (To call functions within DLLs and WinAPIs etc) to extend Sputniks feature set should you think something is missing.

As for .NET DLLs you will have to specify them with Use() and create the objects using the % operator.

Remarks

This is to be considered experimental it may cause crashes/slowness or may give weird errors with no real information on what the hell just happened or even where it happened.

It will get better over time but for now it is useable as shown in the examples.

To use .NET stuff you must first define where the object in question can be found in .NET you do that using the use() function.

Then you can create the object using the % opeator.

The .NET objects you create support their operator overloads such as += *= etc so this makes using .NET classes much easier almost seamless.

If you place a .NET DLL you compiled in C#/VB etc in the Sputnik folder and set the proper information in the Use() function you can then create/use the classes and methods etc in that DLL just like you can with all the examples below.

Alternatively you can use Sputnik to compile (see the function Compile()) some C# code into a DLL and use that instead.

Note - When you use strings it uses Sputnik escapes and not C#/.NET ones (Which is fine since Sputnik has them all anyway and a lot more).

Capabilities & Limitations

This will always contain the current limitations of this stuff for Sputnik it may change over time.

Example

Example of using .NETs Console.WriteLine() function

use("System");
%Console->WriteLine("Hello World!");
// Prints: Hello World!

Example of using .NETs StringBuilder class

use("System");
use("System.Text");
//$sb = %StringBuilder->new(100); // We could pre-allocate
$sb = %StringBuilder->new();
$sb->Append("Cat");
$sb->Append("Dog");
 
%Console->WriteLine("StringBuilder Text: " . $sb);
%Console->WriteLine("StringBuilder Length: " . $sb->$Length);
// Prints:
// StringBuilder Text: CatDog
// StringBuilder Length: 6
 
// Now lets edit it
$sb[0] = 'P';
say("Character 0 = " . $sb[0]);
say("Character 3 = " . $sb[3]);
%Console->WriteLine("StringBuilder Text: " . $sb);
%Console->WriteLine("StringBuilder Length: " . $sb->$Length);
// Prints
// Character 0 = P
// Character 3 = D
// StringBuilder Text: PatDog
// StringBuilder Length: 6

Example of using .NETs Static Environment class

use("System");
$CurDir = %Environment->$CurrentDirectory;
echo "CurDir is: $CurDir\n";

Example of using .NETs Point Class

use("System.Drawing");
 
$a = %Point->new(2, 4);
say($a->ToString());
$a->$X = 77;
$a->$Y = 20;
say( "Value is: " . $a->$X );
say( "Value is: " . $a->$Y );
// Prints:
// {X=2,Y=4}
// Value is: 77
// Value is: 20

Example of using .NETs Char[]

use("System");
 
$chars = %Char[]->new(3);
$chars[0] = 'A';
$chars[1] = 'B';
$chars[2] = 'C';
printf("Char is: %s\n", $chars[0]);
printf("Char is: %s\n", $chars[1]);
printf("Char is: %s\n", $chars[2]);
$str = %String->new($chars);
printf("String: %s\n", $str);
// Finally make a .NET String from the Char[]
$a = %String->new($chars);
printf("String value is %s\n", $a);
printf("StringLen value is %d\n", $a->$Length);
// Prints
// Char is: A
// Char is: B
// Char is: C
// String: ABC
// String value is ABC
// StringLen value is 3
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox