Skip to content

Commit

Permalink
# [1.7.0] Added
Browse files Browse the repository at this point in the history
New `RunTime` structure
## Changed
Changes in `RunTimeInitialization`.
  • Loading branch information
BelicusBr committed Sep 29, 2024
1 parent e3670da commit eb658c2
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 37 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# [1.7.0] (29/09/2024)
## Added
New `RunTime` structure
### Details
The `RunTime` structure has been added to provide runtime values ​​and functions.

## Changed
Changes in `RunTimeInitialization`.
### Details
Form changes to the `RunTimeInitialization` class such as placing the `PriorityList` structure outside the `RunTimeInitialization` class, allowing `RunTimeInitialization` to accept negative values ​​as priority. \
The lower the priority value, the higher its execution level will be.

# [1.6.0] (26/09/2024)
## Added
New structures and methods.
Expand Down
9 changes: 6 additions & 3 deletions GodotEngine.Utility/Runtime/Priority.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace Cobilas.GodotEngine.Utility.Runtime;

public enum Priority {
StartBefore,
StartLater
/// <summary>Indicates the boot priority.</summary>
public enum Priority : byte {
/// <summary>Starts before everyone else.</summary>
StartBefore = 0,
/// <summary>Starts after everyone else.</summary>
StartLater = 1
}
58 changes: 58 additions & 0 deletions GodotEngine.Utility/Runtime/PriorityList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Godot;
using System;
using Cobilas.Collections;
using System.Collections.Generic;

namespace Cobilas.GodotEngine.Utility.Runtime;
/// <summary>Represents a list of <seealso cref="RunTimeInitialization"/> priorities.</summary>
public struct PriorityList : IDisposable {
private KeyValuePair<int, Node>[] nodes;

/// <summary>Adds items to the priority list.</summary>
/// <param name="priority">Object execution priority.</param>
/// <param name="node">The object to be added to the list.</param>
/// <returns>The method will return a <seealso cref="PriorityList"/> object with its modified priority list.</returns>
public PriorityList Add(int priority, Node node) {
nodes ??= Array.Empty<KeyValuePair<int, Node>>();
ArrayManipulation.Add(new KeyValuePair<int, Node>(priority, node), ref nodes);
return this;
}

/// <summary>Execute your priority list.</summary>
/// <param name="root">The parent node where nodes will be added to start their priority execution.</param>
public readonly void Run(Node root) {
if (!ArrayManipulation.EmpytArray(nodes))
foreach (KeyValuePair<int, Node> item in nodes)
if (item.Value != null)
root.AddChild(item.Value);
}

/// <summary>Sort the priority list according to the priority of the list items.</summary>
public void ReorderList() {
KeyValuePair<int, Node>[] temp = ReorderList(nodes);
ArrayManipulation.ClearArray(ref nodes);
nodes = temp;
}

/// <inheritdoc/>
public void Dispose() => ArrayManipulation.ClearArraySafe(ref nodes);

private static KeyValuePair<int, Node>[] ReorderList(KeyValuePair<int, Node>[] list) {
KeyValuePair<int, Node>[] result = Array.Empty<KeyValuePair<int, Node>>();
for (long A = 0; A < list.Length; A++) {
if (result.LongLength == 0) {
ArrayManipulation.Add(list[A], ref result);
continue;
}
bool addInResult = true;
for (long B = 0; B < result.LongLength; B++)
if (list[A].Key < result[B].Key) {
ArrayManipulation.Insert(list[A], B, ref result);
addInResult = false;
break;
}
if (addInResult) ArrayManipulation.Add(list[A], ref result);
}
return result;
}
}
12 changes: 12 additions & 0 deletions GodotEngine.Utility/Runtime/RunTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.InteropServices;

namespace Cobilas.GodotEngine.Utility.Runtime;

/// <summary>Provides RunTime values ​​and functions.</summary>
[StructLayout(LayoutKind.Sequential, Size = 1)]
public readonly struct RunTime {
/// <summary>The interval in seconds from the last frame to the current one (Read Only).</summary>
public const float DeltaTime = .33333333f;
/// <summary>The interval in seconds of in-game time at which physics and other fixed frame rate updates are performed.</summary>
public const float FixedDeltaTime = .02f;
}
59 changes: 33 additions & 26 deletions GodotEngine.Utility/Runtime/RunTimeInitialization.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
using Godot;
using System;
using Cobilas.Collections;
using System.Collections.Generic;

namespace Cobilas.GodotEngine.Utility.Runtime;

/// <summary>Responsible for initializing other classes marked with the <seealso cref="RunTimeInitializationClassAttribute"/> attribute.</summary>
/// <example>
/// The <c>RunTimeInitialization</c> class allows you to automate the <kbd>Project&gt;Project Settings&gt;AutoLoad</kbd> option.
/// To use the <c>RunTimeInitialization</c> class, you must create a class and make it inherit <c>RunTimeInitialization</c>.///
/// <code>
/// using Cobilas.GodotEngine.Utility.Runtime;
/// // The name of the class is up to you.
/// public class RunTimeProcess : RunTimeInitialization {}
/// </code>
/// And remember to add the class that inherits <c>RunTimeInitialization</c> in <kbd>Project&gt;Project Settings&gt;AutoLoad</kbd>.
/// Remembering that the <c>RunTimeInitialization</c> class uses the virtual method <c>_Ready()</c> to perform the initialization of other classes.
/// And to initialize other classes along with the <c>RunTimeInitialization</c> class, the class must inherit the <c>Godot.Node</c> class or some class that inherits <c>Godot.Node</c> and use the <c>RunTimeInitializationClassAttribute</c> attribute.
/// <code>
/// using Godot;
/// using Cobilas.GodotEngine.Utility.Runtime;
/// [RunTimeInitializationClass]
/// public class ClassTest : Node {}
/// </code>
/// </example>
public class RunTimeInitialization : Node {

public const float DeltaTime = .33333333f;
public const float FixedDeltaTime = .02f;
/// <summary>The interval in seconds from the last frame to the current one (Read Only)</summary>
[Obsolete("Use RunTime.DeltaTime")]
public const float DeltaTime = RunTime.DeltaTime;
/// <summary>The interval in seconds of in-game time at which physics and other fixed frame rate updates are performed.</summary>
[Obsolete("Use RunTime.FixedDeltaTime")]
public const float FixedDeltaTime = RunTime.FixedDeltaTime;

/// <inheritdoc/>
public override void _Ready() {
Type[] components = TypeUtilitarian.GetTypes();
Dictionary<Priority, PriorityList> pairs = new() {
Expand All @@ -21,34 +44,18 @@ public override void _Ready() {
if (attri != null) {
Node node = item.Activator<Node>();
if (!string.IsNullOrEmpty(attri.ClassName))
node.Name = attri.ClassName;
node.Name = string.IsNullOrEmpty(attri.ClassName) ? item.Name : attri.ClassName;
pairs[attri.BootPriority] = pairs[attri.BootPriority].Add(attri.SubPriority, node);
}
}
pairs[Priority.StartBefore].Run(this);
pairs[Priority.StartLater].Run(this);
}

public struct PriorityList : IDisposable {
private Node[] nodes;

public PriorityList Add(int index, Node node) {
nodes ??= Array.Empty<Node>();
if (index > ArrayManipulation.ArrayLength(nodes))
ArrayManipulation.Resize(ref nodes, index);
ArrayManipulation.Insert(node, index, ref nodes);
return this;
using (PriorityList list = pairs[Priority.StartBefore]) {
list.ReorderList();
list.Run(this);
}

public readonly void Run(Node root) {
if (!ArrayManipulation.EmpytArray(nodes))
foreach (var item in nodes)
if (item != null)
root.AddChild(item);
}

public void Dispose() {
ArrayManipulation.ClearArraySafe(ref nodes);
using (PriorityList list = pairs[Priority.StartLater]) {
list.ReorderList();
list.Run(this);
}
}
}
45 changes: 45 additions & 0 deletions GodotEngine.Utility/Runtime/RunTimeInitializationClassAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,70 @@

namespace Cobilas.GodotEngine.Utility.Runtime;

/// <summary>This attribute marks which classes will be called by <seealso cref="RunTimeInitialization"/>.</summary>
/// <example>
/// Simple example of class demarcation to be called by <seealso cref="RunTimeInitialization"/>.
/// <code>
/// using Godot;
/// using Cobilas.GodotEngine.Utility.Runtime;
/// [RunTimeInitializationClass]
/// public class ClassTest : Node {}
/// </code>
/// </example>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class RunTimeInitializationClassAttribute : Attribute {
/// <summary>The execution priority.</summary>
/// <value>Returns the priority execution level.</value>
public int SubPriority { get; private set; }
/// <summary>The name of the priority.</summary>
/// <value>Returns the name of the priority that will be executed.</value>
public string ClassName { get; private set; }
/// <summary>The type of priority.</summary>
/// <value>Returns the type of priority that will be executed.</value>
public Priority BootPriority { get; private set; }

/// <summary>Instance the RunTimeInitializationClassAttribute attribute.</summary>
/// <param name="bootPriority">The type of priority.</param>
/// <param name="name">The name of the priority.</param>
/// <param name="subPriority">The execution priority.</param>
public RunTimeInitializationClassAttribute(Priority bootPriority, string? name, int subPriority) {
SubPriority = subPriority;
ClassName = name!;
BootPriority = bootPriority;
}

/// <summary>Instance the RunTimeInitializationClassAttribute attribute.
/// <para>By default the execution priority is 0.</para>
/// </summary>
/// <param name="bootPriority">The type of priority.</param>
/// <param name="name">The name of the priority.</param>
public RunTimeInitializationClassAttribute(Priority bootPriority, string? name) : this(bootPriority, name, 0) {}

/// <summary>Instance the RunTimeInitializationClassAttribute attribute.
/// <para>By default the execution priority is 0.</para>
/// <para>By default the priority name and the class name that the attribute is associated with.</para>
/// </summary>
/// <param name="bootPriority">The type of priority.</param>
public RunTimeInitializationClassAttribute(Priority bootPriority) : this(bootPriority, null) {}

/// <summary>Instance the RunTimeInitializationClassAttribute attribute.
/// <para>By default the priority type is <seealso cref="Priority.StartBefore"/>.</para>
/// </summary>
/// <param name="name">The name of the priority.</param>
/// <param name="subPriority">The execution priority.</param>
public RunTimeInitializationClassAttribute(string name, int subPriority) : this(Priority.StartBefore, name, subPriority) {}

/// <summary>Instance the RunTimeInitializationClassAttribute attribute.
/// <para>By default the priority type is <seealso cref="Priority.StartBefore"/>.</para>
/// <para>By default the execution priority is 0.</para>
/// </summary>
/// <param name="name">The name of the priority.</param>
public RunTimeInitializationClassAttribute(string name) : this(Priority.StartBefore, name) {}

/// <summary>Instance the RunTimeInitializationClassAttribute attribute.
/// <para>By default the priority type is <seealso cref="Priority.StartBefore"/>.</para>
/// <para>By default the execution priority is 0.</para>
/// <para>By default the priority name and the class name that the attribute is associated with.</para>
/// </summary>
public RunTimeInitializationClassAttribute() : this(Priority.StartBefore) {}
}
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
### Descripition
The package contains utility classes in csharp for godot engine(Godot3.5)
## RunTimeInitialization
(namespace: Cobilas.GodotEngine.Utility.Runtime)<br>
The `RunTimeInitialization` class allows you to automate the <kbd>Project&gt;Project Settings&gt;AutoLoad</kbd> option.<br>
(namespace: Cobilas.GodotEngine.Utility.Runtime) \
The `RunTimeInitialization` class allows you to automate the <kbd>Project&gt;Project Settings&gt;AutoLoad</kbd> option. \
To use the `RunTimeInitialization` class, you must create a class and make it inherit `RunTimeInitialization`.
```c#
using Cobilas.GodotEngine.Utility.Runtime;
//The name of the class is up to you.
public class RunTimeProcess : RunTimeInitialization {}
```
And remember to add the class that inherits `RunTimeInitialization` in <kbd>Project&gt;Project Settings&gt;AutoLoad</kbd>.<br>
Remembering that the `RunTimeInitialization` class uses the virtual method `_Ready()` to perform the initialization of other classes.<br>
And remember to add the class that inherits `RunTimeInitialization` in <kbd>Project&gt;Project Settings&gt;AutoLoad</kbd>. \
Remembering that the `RunTimeInitialization` class uses the virtual method `_Ready()` to perform the initialization of other classes. \
And to initialize other classes along with the `RunTimeInitialization` class, the class must inherit the `Godot.Node` class or some class that inherits `Godot.Node` and use the `RunTimeInitializationClassAttribute` attribute.
```c#
using Godot;
Expand All @@ -38,7 +38,7 @@ subPriority: And the execution priority order.
[RunTimeInitializationClass()]
```
## CoroutineManager
The `CoroutineManager` class is responsible for creating and managing coroutines for godot.<br>
The `CoroutineManager` class is responsible for creating and managing coroutines for godot. \
How to create a coroutine?
```c#
using Godot;
Expand Down Expand Up @@ -93,10 +93,10 @@ public static void StopAllCoroutines();
To include the package, open the `.csproj` file and add it.
```xml
<ItemGroup>
<PackageReference Include="Cobilas.Godot.Utility" Version="1.5.3" />
<PackageReference Include="Cobilas.Godot.Utility" Version="1.7.0" />
</ItemGroup>
```
Or use command line.
```
dotnet add package Cobilas.Godot.Utility --version 1.5.3
dotnet add package Cobilas.Godot.Utility --version 1.7.0
```
2 changes: 1 addition & 1 deletion com.cobilas.godot.utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Authors>BélicusBr</Authors>
<Title>Cobilas Godot Utility</Title>
<PackageId>Cobilas.Godot.Utility</PackageId>
<PackageVersion>1.6.0</PackageVersion>
<PackageVersion>1.7.0</PackageVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageOutputPath>C:\local.nuget</PackageOutputPath>
<RepositoryType>git</RepositoryType>
Expand Down

0 comments on commit eb658c2

Please sign in to comment.