Skip to content

Commit

Permalink
major bugfix; zip with design-time reflection and alias for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
deniseAngilica committed Jul 27, 2023
1 parent 2440496 commit 4e5cc21
Show file tree
Hide file tree
Showing 25 changed files with 196 additions and 333 deletions.
1 change: 0 additions & 1 deletion ThinkEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@
<Compile Include="it\unical\mat\objectsMapper\PropertyDetails.cs" />
<Compile Include="it\unical\mat\objectsMapper\PropertyFeatures.cs" />
<Compile Include="it\unical\mat\objectsMapper\SensorsScripts\Sensor.cs" />
<Compile Include="it\unical\mat\objectsMapper\SensorsScripts\MonoBehaviourSensorsManager.cs" />
<Compile Include="it\unical\mat\objectsMapper\SensorsScripts\SensorConfiguration.cs" />
<Compile Include="it\unical\mat\objectsMapper\ObjectTracker.cs" />
<Compile Include="it\unical\mat\objectsMapper\SensorsScripts\ISensors.cs" />
Expand Down
57 changes: 57 additions & 0 deletions ThinkEngine/Assets/Scripts/SensorTemplate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using ThinkEngine.Mappers;
using static ThinkEngine.Mappers.OperationContainer;

namespace ThinkEngine
{
class CLASS_NAME : Sensor
{
private int counter;
private object specificValue;
private Operation operation;
FIELDS
/*
//Singleton
protected static Sensor instance = null;

internal static Sensor Instance
{
get
{
if (instance == null)
{
instance = new CLASS_NAME();
}
return instance;
}
}*/

public override void Initialize(SensorConfiguration sensorConfiguration)
{
// Debug.Log("Initialize method called!");
this.gameObject = sensorConfiguration.gameObject;
ready = true;
INITIALIZATION
}

public override void Destroy()
{
// Debug.Log("Destroy method called!");
//instance = null;
}

public override void Update()
{
// Debug.Log("Update method called!");
UPDATE
}

public override string Map()
{
// Debug.Log("Map method called!");
MAP
}
}
}
Binary file not shown.
Binary file not shown.
Binary file modified ThinkEnginePlugin.zip
Binary file not shown.
1 change: 1 addition & 0 deletions ThinkEngine_Installer.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ xcopy /f "./bin/Debug//Standalone/ThinkEngine.dll" "./ThinkEngine/Assets/ThinkEn
xcopy /f "./bin/Debug/Editor/Antlr4.Runtime.Standard.dll" "./ThinkEngine/Assets/ThinkEngineer/ThinkEngine/Plugins/ThinkEngineDLL/" /Y
xcopy /f "./bin/Debug/dlv2.exe" "./ThinkEngine/Assets/StreamingAssets/ThinkEngineer/ThinkEngine/lib/" /Y
xcopy /f "./bin/Debug/Editor/ThinkEngine.dll.meta" "./ThinkEngine/Assets/ThinkEngineer/ThinkEngine/Plugins/ThinkEngineDLL/" /Y
xcopy /f "./bin/Debug/Editor/SensorTemplate.txt" "./ThinkEngine/Assets/Scripts/" /Y
xcopy /f "./bin/Debug/Standalone/ThinkEngine.dll.meta" "./ThinkEngine/Assets/ThinkEngineer/ThinkEngine/Plugins/" /Y
PowerShell compress-archive -force .\ThinkEngine\* .\ThinkEnginePlugin.zip
12 changes: 7 additions & 5 deletions it/unical/mat/objectsMapper/AbstractConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace ThinkEngine
{
[Serializable, ExecuteInEditMode]
internal abstract class AbstractConfiguration : MonoBehaviour
public abstract class AbstractConfiguration : MonoBehaviour
{
internal ObjectTracker _objectTracker;
internal ObjectTracker ObjectTracker
Expand Down Expand Up @@ -40,7 +40,7 @@ internal List<MyListString> SavedProperties
internal List<MyListString> _toMapProperties;
[SerializeField, HideInInspector]
internal List<PropertyFeatures> _propertyFeatures;
internal List<PropertyFeatures> PropertyFeatures
internal List<PropertyFeatures> PropertyFeaturesList
{
get
{
Expand Down Expand Up @@ -104,7 +104,7 @@ internal virtual void Clear()
_objectTracker = new ObjectTracker(gameObject);
SavedProperties = new List<MyListString>();
ToMapProperties = new List<MyListString>();
PropertyFeatures = new List<PropertyFeatures>();
PropertyFeaturesList = new List<PropertyFeatures>();
}
internal bool IsPropertySelected(MyListString property)
{
Expand All @@ -119,7 +119,7 @@ internal void ToggleProperty(MyListString property, bool isActive)
if (ObjectTracker.IsFinal(property))
{
ToMapProperties.Add(property);
PropertyFeatures.Add(new PropertyFeatures(gameObject, property));
PropertyFeaturesList.Add(new PropertyFeatures(gameObject, property));
}
}
else
Expand All @@ -129,7 +129,9 @@ internal void ToggleProperty(MyListString property, bool isActive)
if (ToMapProperties.Contains(property))
{
ToMapProperties.Remove(property);
PropertyFeatures.RemoveAll(x => x.property.Equals(property));
PropertyFeatures p =PropertyFeaturesList.Find(x => x.property.Equals(property));
p.Remove();
PropertyFeaturesList.Remove(p) ;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace ThinkEngine
{
[RequireComponent(typeof(IndexTracker))]
internal class MonoBehaviourActuator : MonoBehaviour
public class MonoBehaviourActuator : MonoBehaviour
{
internal string configurationName;
private object toPass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ThinkEngine
{
internal interface IActuators
public interface IActuators
{
List<MonoBehaviourActuator> GetActuatorsList();
}
Expand Down
2 changes: 1 addition & 1 deletion it/unical/mat/objectsMapper/BrainsScripts/Brain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ internal virtual void GenerateFile()
continue;
}
seenSensorConfNames.Add(sensorConf.ConfigurationName);
foreach (PropertyFeatures features in sensorConf.PropertyFeatures)
foreach (PropertyFeatures features in sensorConf.PropertyFeaturesList)
{
//Debug.Log(property);
sensorsAsASP = MapperManager.GetASPTemplate(features.PropertyAlias, sensorConf.gameObject, features.property, true);
Expand Down
80 changes: 45 additions & 35 deletions it/unical/mat/objectsMapper/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ internal class CodeGenerator

//Paths
private static readonly string rootPath = string.Format("{0}", Path.Combine(Application.dataPath, "Scripts"));
private static readonly string generatedCodeRelativePath = Path.Combine("Assets","Scripts","GeneratedCode");
private static readonly string generatedCodePath = string.Format(Path.Combine("{0}","GeneratedCode"), rootPath);
private static readonly string templateRelativePath = Path.Combine("Assets","Scripts","SensorTemplate.txt");
private static readonly string generatedCodeRelativePath = Path.Combine("Assets", "Scripts", "GeneratedCode");
private static readonly string generatedCodePath = string.Format(Path.Combine("{0}", "GeneratedCode"), rootPath);
private static readonly string templateRelativePath = Path.Combine("Assets", "Scripts", "SensorTemplate.txt");

//Helping data structures
private static List<string> propertyHierarchyNames;
Expand All @@ -38,12 +38,12 @@ internal class CodeGenerator
private static MyListString currentPropertyHierarchy;
private static PropertyFeatures currentPropertyFeatures;

private CodeGenerator() {}
private CodeGenerator() { }

//Generate the MonobehaviourSensorManager
internal static void GenerateCode(List<MyListString> toMapProperties, object objectValue, SensorConfiguration sensorConfiguration)
{
if(sensorConfiguration.ConfigurationName.Equals(string.Empty))
if (sensorConfiguration.ConfigurationName.Equals(string.Empty))
{
Debug.LogError("SensorConfiguration name can't be empty!");
return;
Expand All @@ -54,7 +54,7 @@ internal static void GenerateCode(List<MyListString> toMapProperties, object obj
foreach (MyListString propertyHierarchy in toMapProperties)
{
currentPropertyHierarchy = propertyHierarchy;
currentPropertyFeatures = currentSensorConfiguration.PropertyFeatures.Find(x => x.property.Equals(currentPropertyHierarchy));
currentPropertyFeatures = currentSensorConfiguration.PropertyFeaturesList.Find(x => x.property.Equals(currentPropertyHierarchy));

propertyHierarchyNames = new List<string>();
propertyHierarchyTypeNames = new List<string>();
Expand All @@ -79,10 +79,10 @@ internal static void GenerateCode(List<MyListString> toMapProperties, object obj

sensorName = GenerateSensorName(sensorConfiguration);

//Debug.Log(string.Join(",", propertyHierarchyNames));
//Debug.Log(string.Join(",", propertyHierarchyTypeNames));
//Debug.Log(string.Join(",", arePropertiesComponent));
//Debug.Log(string.Join(",", arePropertiesPrimitive));
Debug.Log(string.Join(",", propertyHierarchyNames));
Debug.Log(string.Join(",", propertyHierarchyTypeNames));
Debug.Log(string.Join(",", arePropertiesComponent));
Debug.Log(string.Join(",", arePropertiesPrimitive));

TextAsset templateTextFile = AssetDatabase.LoadAssetAtPath(templateRelativePath, typeof(TextAsset)) as TextAsset;
if (templateTextFile == null)
Expand Down Expand Up @@ -155,25 +155,25 @@ private static string ReplaceFields(string text)

if (mapperType.IsSubclassOf(typeof(BasicTypeMapper)))
{
addedText = string.Concat(addedText, "" +
addedText = string.Concat(addedText, "" +
string.Format("" +
"{0}private List<{1}> values = new List<{1}>();{2}", GetTabs(0),TypeNameOrAlias(finalType), Environment.NewLine));
"{0}private List<{1}> values = new List<{1}>();{2}", GetTabs(0), TypeNameOrAlias(finalType), Environment.NewLine));
}
else if(mapperType.IsSubclassOf(typeof(CollectionMapper)))
else if (mapperType.IsSubclassOf(typeof(CollectionMapper)))
{
addedText = string.Concat(addedText, "" +
addedText = string.Concat(addedText, "" +
string.Format("" +
"{0}private List<List<{1}>> values = new List<List<{1}>>();{2}" +
"{0}private List<bool> isIndexActive = new List<bool>();{2}", GetTabs(0), TypeNameOrAlias(((CollectionMapper)mapper).ElementType(finalType)), Environment.NewLine));

//Indexing for the sensor
if(mapperType.Equals(typeof(ASPArrayMapper)) || mapperType.Equals(typeof(ASPListMapper)))
if (mapperType.Equals(typeof(ASPArrayMapper)) || mapperType.Equals(typeof(ASPListMapper)))
{
addedText = string.Concat(addedText, "" +
string.Format("{0}private List<int> indicies = new List<int>();\n", GetTabs(0)));

}
else if(mapperType.Equals(typeof(ASPArray2Mapper)))
else if (mapperType.Equals(typeof(ASPArray2Mapper)))
{
addedText = string.Concat(addedText, "" +
string.Format("{0}private List<(int, int)> indicies = new List<(int, int)>();\n", GetTabs(0)));
Expand Down Expand Up @@ -205,7 +205,7 @@ private static string ReplaceInitialization(string text)
}
else if (mapperType.IsSubclassOf(typeof(CollectionMapper)))
{
addedText = string.Concat(addedText,
addedText = string.Concat(addedText,
string.Format("" +
"{0}mapper = (BasicTypeMapper)MapperManager.GetMapper(typeof({3}));{1}" +
"{0}operation = mapper.OperationList()[{2}];{1}" +
Expand Down Expand Up @@ -296,6 +296,7 @@ private static string ReplaceUpdate(string text)
"{0}}}{4}" +
"{0}if(!invariant || first){4}" +
"{0}{{{4}" +
"{1}first = false;{4}" +
"{2}" +
"{3}" +
"{0}}}", GetTabs(1), GetTabs(2), CheckIfSensorNeedsManaging(), UpdateSensorValues(), Environment.NewLine));
Expand All @@ -306,23 +307,23 @@ private static string ReplaceUpdate(string text)
private static string ReplaceMap(string text)
{
string addedText = string.Empty;
if(mapperType.IsSubclassOf(typeof(BasicTypeMapper)))

if (mapperType.IsSubclassOf(typeof(BasicTypeMapper)))
{
addedText = string.Concat(addedText, "" +
string.Format("" +
"{0}object operationResult = operation(values, specificValue, counter);{1}" +
"{0}return string.Format(mappingTemplate, BasicTypeMapper.GetMapper(operationResult.GetType()).BasicMap(operationResult));", GetTabs(1), Environment.NewLine));
}
else if(mapperType.IsSubclassOf(typeof(CollectionMapper)))
else if (mapperType.IsSubclassOf(typeof(CollectionMapper)))
{
string indicies = string.Empty;

if(mapperType.Equals(typeof(ASPArrayMapper)) || mapperType.Equals(typeof(ASPListMapper)))
if (mapperType.Equals(typeof(ASPArrayMapper)) || mapperType.Equals(typeof(ASPListMapper)))
{
indicies = " indicies[i],";
}
else if(mapperType.Equals(typeof(ASPArray2Mapper)))
else if (mapperType.Equals(typeof(ASPArray2Mapper)))
{
indicies = " indicies[i].Item1, indicies[i].Item2,";
}
Expand Down Expand Up @@ -399,8 +400,17 @@ private static string GetOperationToTargetProperty(int baseOfTabs)
string text = string.Empty;

int i = 0;
text = string.Concat(text, "" +
string.Format("{3}{0} {1}{2} = gameObject.GetComponent<{0}>();\n", propertyHierarchyTypeNames[0], propertyHierarchyNames[0], i, GetTabs(baseOfTabs)));

if (arePropertiesComponent[i])
{
text = string.Concat(text, "" +
string.Format("{3}{0} {1}{2} = gameObject.GetComponent<{0}>();\n", propertyHierarchyTypeNames[0], propertyHierarchyNames[0], i, GetTabs(baseOfTabs)));
}
else
{
text = string.Concat(text, "" +
string.Format("{3}{0} {1}{2} = gameObject.{1};\n", propertyHierarchyTypeNames[0], propertyHierarchyNames[0], i, GetTabs(baseOfTabs)));
}

if (!arePropertiesPrimitive[i])
{
Expand All @@ -413,7 +423,7 @@ private static string GetOperationToTargetProperty(int baseOfTabs)
text = string.Concat(text, "" +
string.Format("{3}{0} {1}{2} = {4}{5}", propertyHierarchyTypeNames[i], propertyHierarchyNames[i], i, GetTabs(baseOfTabs), propertyHierarchyNames[i - 1], i - 1));

if (propertyHierarchyTypeNames[i - 1].Equals("GameObject"))
if (propertyHierarchyTypeNames[i - 1].Equals("GameObject") && arePropertiesComponent[i])
{
text = string.Concat(text, "" +
string.Format(".GetComponent<{0}>();\n", propertyHierarchyTypeNames[i]));
Expand All @@ -424,7 +434,7 @@ private static string GetOperationToTargetProperty(int baseOfTabs)
string.Format(".{0};\n", propertyHierarchyNames[i]));
}


if (arePropertiesPrimitive[i]) continue;

text = string.Concat(text, "" +
Expand All @@ -448,12 +458,12 @@ private static string GetTabs(int count)

return text;
}

private static string GetCollectionElement()
{
string text = string.Empty;

if(mapperType.Equals(typeof(ASPListMapper)))
if (mapperType.Equals(typeof(ASPListMapper)))
{
text = string.Format("{0}{1}[i]", propertyHierarchyNames[propertyHierarchyNames.Count - 1], propertyHierarchyNames.Count - 1);
}
Expand Down Expand Up @@ -504,12 +514,12 @@ private static string GetCollectionSize()

return text;
}

private static string UpdateSensorValues()
{
string text = string.Empty;

int maxValue = currentSensorConfiguration.PropertyFeatures.Find(x => x.property.Equals(currentPropertyHierarchy)).windowWidth;
int maxValue = currentSensorConfiguration.PropertyFeaturesList.Find(x => x.property.Equals(currentPropertyHierarchy)).windowWidth;

//Updating
if (mapperType.IsSubclassOf(typeof(BasicTypeMapper)))
Expand Down Expand Up @@ -555,7 +565,7 @@ private static string UpdateSensorValue()

return text;
}

#endregion

#region REFLECTION
Expand All @@ -576,7 +586,7 @@ private static bool ReachPropertyByReflection(MyListString property, object curr
while (property.Count > 0)
{
currentProperty = property[0];

currentObjectValue = RetrieveProperty(currentObjectValue, currentProperty, currentType, out currentType);
if (mapper == null)
{
Expand All @@ -596,7 +606,7 @@ private static bool ReachPropertyByReflection(MyListString property, object curr

if (currentObjectValue == null)
{
if(!ReachPropertyByReflectionByType(propertyHierarchyNames, propertyHierarchyTypeNames, arePropertiesComponent, arePropertiesPrimitive, property, currentType, out finalType, out mapper))
if (!ReachPropertyByReflectionByType(propertyHierarchyNames, propertyHierarchyTypeNames, arePropertiesComponent, arePropertiesPrimitive, property, currentType, out finalType, out mapper))
{
//Object 's value is null. Is this a problem?
Debug.LogError(string.Format("Couldn't find {0}'s objectValue (null) during reflection!", currentProperty));
Expand Down Expand Up @@ -778,10 +788,10 @@ private static string RecursiveGenericTypeName(Type type)

string aliasName = genericTypeName;
aliasName = string.Concat(aliasName, "<");
for(int i = 0; i < genericArguments.Length; i++)
for (int i = 0; i < genericArguments.Length; i++)
{
aliasName = string.Concat(aliasName, RecursiveGenericTypeName(genericArguments[i]));
if(i < genericArguments.Length - 1)
if (i < genericArguments.Length - 1)
{
string.Concat(aliasName, ", ");
}
Expand Down
Loading

0 comments on commit 4e5cc21

Please sign in to comment.