-
Notifications
You must be signed in to change notification settings - Fork 14
Experiment reflection #6
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| using Unity.Entities; | ||
| using Unity.Collections; | ||
| using Unity.Collections.LowLevel.Unsafe; | ||
| using System.Runtime.InteropServices; | ||
| using System; | ||
| namespace ECS_MLAgents_v0.Data | ||
| { | ||
|
|
||
| /* | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| struct char<N> { | ||
| private int size; | ||
| [MarshalAs(UnmanagedType.ByValArray, SizeConst = <N>)] | ||
| private byte[] bytes; | ||
|
|
||
| public char<N>(string s){ | ||
| this.bytes=new byte[<N>]; | ||
| this.size = s.Length; | ||
| if (this.size > <N>){ | ||
| throw new NotSupportedException( | ||
| "Cannot create a char<N> object with more than <N> characters" | ||
| ); | ||
| } | ||
| System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0); | ||
| } | ||
|
|
||
| public string GetString(){ | ||
| return System.Text.Encoding.UTF8.GetString(bytes, 0, size); | ||
| } | ||
| } | ||
|
|
||
| */ | ||
|
|
||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| public struct char64 { | ||
|
|
||
| private int size; // TODO we could use an end of line stopper rather than keeping track of the size | ||
| [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] | ||
| private byte[] bytes; | ||
|
|
||
| public char64(string s){ | ||
| this.bytes=new byte[64]; | ||
| this.size = s.Length; | ||
| if (this.size > 64){ | ||
| throw new NotSupportedException( | ||
| "Cannot create a char64 object with more than 64 characters" | ||
| ); | ||
| } | ||
| System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0); | ||
| } | ||
|
|
||
| public string GetString(){ | ||
| return System.Text.Encoding.UTF8.GetString(bytes, 0, size); | ||
| } | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential)] | ||
| public struct char256 { | ||
|
|
||
| private int size; | ||
| [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] | ||
| private byte[] bytes; | ||
|
|
||
| public char256(string s){ | ||
| this.bytes=new byte[256]; | ||
| this.size = s.Length; | ||
| if (this.size > 256){ | ||
| throw new NotSupportedException( | ||
| "Cannot create a char256 object with more than 256 characters" | ||
| ); | ||
| } | ||
| System.Text.Encoding.ASCII.GetBytes(s, 0, this.size, this.bytes, 0); | ||
| } | ||
|
|
||
| public string GetString(){ | ||
| return System.Text.Encoding.UTF8.GetString(bytes, 0, size); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using UnityEngine; | ||
| using Unity.Collections.LowLevel.Unsafe; | ||
| using Unity.Collections; | ||
| using Unity.Mathematics; | ||
|
|
||
| namespace ECS_MLAgents_v0.Data | ||
| { | ||
| public enum SensorDataType : int { | ||
| FLOAT, | ||
| // INT, | ||
| // BOOL, | ||
| // ENUM, | ||
| // TEXTURE, | ||
|
|
||
| } | ||
|
|
||
| public enum SensorType : int { | ||
| DATA, | ||
| REWARD, | ||
| DONE, | ||
| ID, | ||
|
|
||
| } | ||
|
|
||
| public class SensorAttribute : Attribute{ | ||
| public string Description; | ||
| public SensorType SensorType; | ||
| public SensorAttribute(SensorType Type, string Description = ""){ | ||
| this.Description = Description; | ||
| this.SensorType = Type; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| public class AttributeUtility | ||
| { | ||
| // Note : This is non blittable | ||
| public struct SensorMetadata | ||
| { | ||
| public char64 Name; | ||
| public char256 Description; | ||
| // public int4 Dimension; | ||
| // public int Offset; | ||
| // public SensorDataType DataType; | ||
| public SensorType SensorType; | ||
| } | ||
|
|
||
| public static SensorMetadata[] GetSensorMetaData(Type t) | ||
| { | ||
| var fields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | ||
| // var fields = t.GetFields(); | ||
| var result = new SensorMetadata[fields.Length]; | ||
| for(var i =0; i<fields.Length; i++) | ||
| { | ||
| result[i] = GetMetaDataFromField(fields[i]); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public static SensorMetadata GetMetaDataFromField(FieldInfo field) | ||
| { | ||
| //TODO : Run some checks on the field to make sure the data is right | ||
|
|
||
|
|
||
| var attribute = field.GetCustomAttribute<SensorAttribute>(); | ||
| if (attribute == null){ | ||
| attribute = new SensorAttribute(SensorType.DATA); | ||
| } | ||
|
|
||
| return new SensorMetadata{ | ||
| Name = new char64(field.Name), | ||
| SensorType = attribute.SensorType, | ||
| Description = new char256(attribute.Description), | ||
| }; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using NUnit.Framework; | ||
| using ECS_MLAgents_v0; | ||
| using ECS_MLAgents_v0.Data; | ||
| using System.Runtime.InteropServices; | ||
| using UnityEngine; | ||
| using System; | ||
|
|
||
| namespace ECS_MLAgents_v0.Editor.Tests{ | ||
| public class char64Test{ | ||
|
|
||
| [Test] | ||
| public void TestFromStringChar64(){ | ||
| foreach(string s in new string[]{"", " ", "TestString"}){ | ||
| var tmp = new char64(s); | ||
| Assert.AreEqual(s, tmp.GetString()); | ||
| Assert.AreEqual(4 + 64, Marshal.SizeOf(tmp)); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestLongStringChar64(){ | ||
| Assert.That(() => new char64("0000000001000000000200000000030000000004000000000500000000060000XXXXX"), | ||
| Throws.TypeOf<NotSupportedException>()); | ||
| } | ||
| } | ||
|
|
||
| public class char256Test{ | ||
|
|
||
| [Test] | ||
| public void TestFromStringChar256(){ | ||
| foreach(string s in new string[]{"", " ", "TestString"}){ | ||
| var tmp = new char256(s); | ||
| Assert.AreEqual(s, tmp.GetString()); | ||
| Assert.AreEqual(4 + 256, Marshal.SizeOf(tmp)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the generic approach not work? You commented out the code..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generic does not work because the size must be known at compile time.