Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix C# examples in documentation for 4.0 #72462

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions doc/classes/AESContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,38 @@
[/gdscript]
[csharp]
using Godot;
using System;
using System.Diagnostics;

public class Example : Node
public partial class MyNode : Node
{
public AESContext Aes = new AESContext();
private AesContext _aes = new AesContext();

public override void _Ready()
{
string key = "My secret key!!!"; // Key must be either 16 or 32 bytes.
string data = "My secret text!!"; // Data size must be multiple of 16 bytes, apply padding if needed.
// Encrypt ECB
Aes.Start(AESContext.Mode.EcbEncrypt, key.ToUTF8());
byte[] encrypted = Aes.Update(data.ToUTF8());
Aes.Finish();
_aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8());
byte[] encrypted = _aes.Update(data.ToUtf8());
_aes.Finish();
// Decrypt ECB
Aes.Start(AESContext.Mode.EcbDecrypt, key.ToUTF8());
byte[] decrypted = Aes.Update(encrypted);
Aes.Finish();
_aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8());
byte[] decrypted = _aes.Update(encrypted);
_aes.Finish();
// Check ECB
Debug.Assert(decrypted == data.ToUTF8());
Debug.Assert(decrypted == data.ToUtf8());

string iv = "My secret iv!!!!"; // IV must be of exactly 16 bytes.
// Encrypt CBC
Aes.Start(AESContext.Mode.EcbEncrypt, key.ToUTF8(), iv.ToUTF8());
encrypted = Aes.Update(data.ToUTF8());
Aes.Finish();
_aes.Start(AesContext.Mode.EcbEncrypt, key.ToUtf8(), iv.ToUtf8());
encrypted = _aes.Update(data.ToUtf8());
_aes.Finish();
// Decrypt CBC
Aes.Start(AESContext.Mode.EcbDecrypt, key.ToUTF8(), iv.ToUTF8());
decrypted = Aes.Update(encrypted);
Aes.Finish();
_aes.Start(AesContext.Mode.EcbDecrypt, key.ToUtf8(), iv.ToUtf8());
decrypted = _aes.Update(encrypted);
_aes.Finish();
// Check CBC
Debug.Assert(decrypted == data.ToUTF8());
Debug.Assert(decrypted == data.ToUtf8());
}
}
[/csharp]
Expand Down
11 changes: 6 additions & 5 deletions doc/classes/AStar3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
return min(0, abs(u - v) - 1)
[/gdscript]
[csharp]
public class MyAStar : AStar3D
public partial class MyAStar : AStar3D
{
public override float _ComputeCost(int u, int v)
public override float _ComputeCost(long fromId, long toId)
{
return Mathf.Abs(u - v);
return Mathf.Abs((int)(fromId - toId));
}
public override float _EstimateCost(int u, int v)

public override float _EstimateCost(long fromId, long toId)
{
return Mathf.Min(0, Mathf.Abs(u - v) - 1);
return Mathf.Min(0, Mathf.Abs((int)(fromId - toId)) - 1);
}
}
[/csharp]
Expand Down
13 changes: 6 additions & 7 deletions doc/classes/Color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@
<description>
Returns a new color from [param rgba], an HTML hexadecimal color string. [param rgba] is not case-sensitive, and may be prefixed by a hash sign ([code]#[/code]).
[param rgba] must be a valid three-digit or six-digit hexadecimal color string, and may contain an alpha channel value. If [param rgba] does not contain an alpha channel value, an alpha channel value of 1.0 is applied. If [param rgba] is invalid, returns an empty color.
[b]Note:[/b] In C#, this method is not implemented. The same functionality is provided by the Color constructor.
[codeblocks]
[gdscript]
var blue = Color.html("#0000ff") # blue is Color(0.0, 0.0, 1.0, 1.0)
Expand Down Expand Up @@ -264,13 +263,13 @@
Color.html_is_valid("#55aaFF5") # Returns false
[/gdscript]
[csharp]
Color.IsHtmlValid("#55AAFF"); // Returns true
Color.IsHtmlValid("#55AAFF20"); // Returns true
Color.IsHtmlValid("55AAFF"); // Returns true
Color.IsHtmlValid("#F2C"); // Returns true
Color.HtmlIsValid("#55AAFF"); // Returns true
Color.HtmlIsValid("#55AAFF20"); // Returns true
Color.HtmlIsValid("55AAFF"); // Returns true
Color.HtmlIsValid("#F2C"); // Returns true

Color.IsHtmlValid("#AABBC"); // Returns false
Color.IsHtmlValid("#55aaFF5"); // Returns false
Color.HtmlIsValid("#AABBC"); // Returns false
Color.HtmlIsValid("#55aaFF5"); // Returns false
[/csharp]
[/codeblocks]
</description>
Expand Down
53 changes: 27 additions & 26 deletions doc/classes/Control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
return typeof(data) == TYPE_DICTIONARY and data.has("expected")
[/gdscript]
[csharp]
public override bool CanDropData(Vector2 position, object data)
public override bool _CanDropData(Vector2 atPosition, Variant data)
{
// Check position if it is relevant to you
// Otherwise, just check data
return data is Godot.Collections.Dictionary &amp;&amp; (data as Godot.Collections.Dictionary).Contains("expected");
return data.VariantType == Variant.Type.Dictionary &amp;&amp; data.AsGodotDictionary().Contains("expected");
}
[/csharp]
[/codeblocks]
Expand All @@ -57,17 +57,19 @@
[gdscript]
func _can_drop_data(position, data):
return typeof(data) == TYPE_DICTIONARY and data.has("color")

func _drop_data(position, data):
var color = data["color"]
[/gdscript]
[csharp]
public override bool CanDropData(Vector2 position, object data)
public override bool _CanDropData(Vector2 atPosition, Variant data)
{
return data is Godot.Collections.Dictionary &amp;&amp; (data as Godot.Collections.Dictionary).Contains("color");
return data.VariantType == Variant.Type.Dictionary &amp;&amp; dict.AsGodotDictionary().Contains("color");
}
public override void DropData(Vector2 position, object data)

public override void _DropData(Vector2 atPosition, Variant data)
{
Color color = (Color)(data as Godot.Collections.Dictionary)["color"];
Color color = data.AsGodotDictionary()["color"].AsColor();
}
[/csharp]
[/codeblocks]
Expand All @@ -87,11 +89,11 @@
return mydata
[/gdscript]
[csharp]
public override object GetDragData(Vector2 position)
public override Variant _GetDragData(Vector2 atPosition)
{
object mydata = MakeData(); // This is your custom method generating the drag data.
SetDragPreview(MakePreview(mydata)); // This is your custom method generating the preview of the drag data.
return mydata;
var myData = MakeData(); // This is your custom method generating the drag data.
SetDragPreview(MakePreview(myData)); // This is your custom method generating the preview of the drag data.
return myData;
}
[/csharp]
[/codeblocks]
Expand Down Expand Up @@ -121,10 +123,9 @@
[csharp]
public override void _GuiInput(InputEvent @event)
{
if (@event is InputEventMouseButton)
if (@event is InputEventMouseButton mb)
{
var mb = @event as InputEventMouseButton;
if (mb.ButtonIndex == (int)ButtonList.Left &amp;&amp; mb.Pressed)
if (mb.ButtonIndex == MouseButton.Left &amp;&amp; mb.Pressed)
{
GD.Print("I've been clicked D:");
}
Expand Down Expand Up @@ -168,7 +169,7 @@
return label
[/gdscript]
[csharp]
public override Godot.Control _MakeCustomTooltip(String forText)
public override Control _MakeCustomTooltip(string forText)
{
var label = new Label();
label.Text = forText;
Expand All @@ -185,7 +186,7 @@
return tooltip
[/gdscript]
[csharp]
public override Godot.Control _MakeCustomTooltip(String forText)
public override Control _MakeCustomTooltip(string forText)
{
Node tooltip = ResourceLoader.Load&lt;PackedScene&gt;("res://some_tooltip_scene.tscn").Instantiate();
tooltip.GetNode&lt;Label&gt;("Label").Text = forText;
Expand Down Expand Up @@ -229,11 +230,11 @@
[/gdscript]
[csharp]
// Given the child Label node "MyLabel", override its font color with a custom value.
GetNode&lt;Label&gt;("MyLabel").AddThemeColorOverride("font_color", new Color(1, 0.5f, 0))
GetNode&lt;Label&gt;("MyLabel").AddThemeColorOverride("font_color", new Color(1, 0.5f, 0));
// Reset the font color of the child label.
GetNode&lt;Label&gt;("MyLabel").RemoveThemeColorOverride("font_color")
GetNode&lt;Label&gt;("MyLabel").RemoveThemeColorOverride("font_color");
// Alternatively it can be overridden with the default value from the Label type.
GetNode&lt;Label&gt;("MyLabel").AddThemeColorOverride("font_color", GetThemeColor("font_color", "Label"))
GetNode&lt;Label&gt;("MyLabel").AddThemeColorOverride("font_color", GetThemeColor("font_color", "Label"));
[/csharp]
[/codeblocks]
</description>
Expand Down Expand Up @@ -542,12 +543,12 @@
[codeblocks]
[gdscript]
func _process(delta):
grab_click_focus() #when clicking another Control node, this node will be clicked instead
grab_click_focus() # When clicking another Control node, this node will be clicked instead.
[/gdscript]
[csharp]
public override void _Process(float delta)
public override void _Process(double delta)
{
GrabClickFocus(); //when clicking another Control node, this node will be clicked instead
GrabClickFocus(); // When clicking another Control node, this node will be clicked instead.
}
[/csharp]
[/codeblocks]
Expand Down Expand Up @@ -812,16 +813,16 @@
[/gdscript]
[csharp]
[Export]
public Color Color = new Color(1, 0, 0, 1);
private Color _color = new Color(1, 0, 0, 1);

public override object GetDragData(Vector2 position)
public override Variant _GetDragData(Vector2 atPosition)
{
// Use a control that is not in the tree
var cpb = new ColorPickerButton();
cpb.Color = Color;
cpb.RectSize = new Vector2(50, 50);
cpb.Color = _color;
cpb.Size = new Vector2(50, 50);
SetDragPreview(cpb);
return Color;
return _color;
}
[/csharp]
[/codeblocks]
Expand Down
30 changes: 16 additions & 14 deletions doc/classes/Crypto.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
[codeblocks]
[gdscript]
extends Node

var crypto = Crypto.new()
var key = CryptoKey.new()
var cert = X509Certificate.new()

func _ready():
# Generate new RSA key.
key = crypto.generate_rsa(4096)
Expand All @@ -35,35 +37,35 @@
[/gdscript]
[csharp]
using Godot;
using System;
using System.Diagnostics;

public class CryptoNode : Node
public partial class MyNode : Node
{
public Crypto Crypto = new Crypto();
public CryptoKey Key = new CryptoKey();
public X509Certificate Cert = new X509Certificate();
private Crypto _crypto = new Crypto();
private CryptoKey _key = new CryptoKey();
private X509Certificate _cert = new X509Certificate();

public override void _Ready()
{
// Generate new RSA key.
Key = Crypto.GenerateRsa(4096);
_key = _crypto.GenerateRsa(4096);
// Generate new self-signed certificate with the given key.
Cert = Crypto.GenerateSelfSignedCertificate(Key, "CN=mydomain.com,O=My Game Company,C=IT");
_cert = _crypto.GenerateSelfSignedCertificate(_key, "CN=mydomain.com,O=My Game Company,C=IT");
// Save key and certificate in the user folder.
Key.Save("user://generated.key");
Cert.Save("user://generated.crt");
_key.Save("user://generated.key");
_cert.Save("user://generated.crt");
// Encryption
string data = "Some data";
byte[] encrypted = Crypto.Encrypt(Key, data.ToUTF8());
byte[] encrypted = _crypto.Encrypt(_key, data.ToUtf8());
// Decryption
byte[] decrypted = Crypto.Decrypt(Key, encrypted);
byte[] decrypted = _crypto.Decrypt(_key, encrypted);
// Signing
byte[] signature = Crypto.Sign(HashingContext.HashType.Sha256, Data.SHA256Buffer(), Key);
byte[] signature = _crypto.Sign(HashingContext.HashType.Sha256, Data.Sha256Buffer(), _key);
// Verifying
bool verified = Crypto.Verify(HashingContext.HashType.Sha256, Data.SHA256Buffer(), signature, Key);
bool verified = _crypto.Verify(HashingContext.HashType.Sha256, Data.Sha256Buffer(), signature, _key);
// Checks
Debug.Assert(verified);
Debug.Assert(data.ToUTF8() == decrypted);
Debug.Assert(data.ToUtf8() == decrypted);
}
}
[/csharp]
Expand Down
Loading