143 lines
5.3 KiB
C#
143 lines
5.3 KiB
C#
//============= Copyright (c) Ludic GmbH, All rights reserved. ==============
|
|
//
|
|
// Purpose: Part of the My Behaviour Tree Code
|
|
//
|
|
//=============================================================================
|
|
|
|
using System;
|
|
using System.Reflection;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Globalization;
|
|
|
|
namespace MyBT {
|
|
[System.Serializable]
|
|
public class TaskParameterGroup : ScriptableObject {
|
|
[SerializeField]
|
|
List<TaskParameter> taskParameters;
|
|
|
|
public TaskParameterGroup() {
|
|
taskParameters = new List<TaskParameter>();
|
|
}
|
|
|
|
public void Init (MemberInfo memberInfo) {
|
|
MethodInfo methodInfo = (MethodInfo)memberInfo;
|
|
if (methodInfo != null) {
|
|
taskParameters = new List<TaskParameter>();
|
|
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
|
|
foreach (ParameterInfo parameterInfo in parameterInfos) {
|
|
// TaskParameter taskParm = new TaskParameter() { valType = Type.GetTypeCode(parameterInfo.ParameterType) };
|
|
TaskParameter taskParm = ScriptableObject.CreateInstance<TaskParameter>();
|
|
taskParm.valType = Type.GetTypeCode(parameterInfo.ParameterType);
|
|
taskParameters.Add(taskParm);
|
|
}
|
|
}
|
|
else {
|
|
Debug.LogError("TaskParameter.TaskParameterGroup: methodInfo is null");
|
|
}
|
|
}
|
|
|
|
public TaskParameterGroup Duplicate () {
|
|
TaskParameterGroup tpg = ScriptableObject.CreateInstance<TaskParameterGroup>();
|
|
foreach (TaskParameter tp in taskParameters) {
|
|
TaskParameter tpCopy = tp.Duplicate();
|
|
tpg.taskParameters.Add(tpCopy);
|
|
}
|
|
// tpg.Init(memberInfo);
|
|
return tpg;
|
|
}
|
|
|
|
public void Add(TaskParameter taskParameter) {
|
|
taskParameters.Add(taskParameter);
|
|
}
|
|
|
|
public List<TaskParameter> GetAll() {
|
|
return taskParameters;
|
|
}
|
|
|
|
public TaskParameter this[int index] {
|
|
get {
|
|
if ((0 <= index) && (index < taskParameters.Count)) {
|
|
return taskParameters[index];
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public int Count {
|
|
get {
|
|
return taskParameters.Count;
|
|
}
|
|
}
|
|
|
|
/// mostly for viewing in unity editor
|
|
public string cachedString = "";
|
|
public override string ToString() {
|
|
List<string> taskParmStrs = new List<string>();
|
|
foreach (TaskParameter taskParm in taskParameters) {
|
|
taskParmStrs.Add($"{taskParm.valValue} ({taskParm.valType.ToString()})");
|
|
}
|
|
cachedString = string.Join(",", taskParmStrs);
|
|
//Debug.Log("TaskParameterGroup.ToString: " + cachedString + " (" + taskParameters.Count + ")");
|
|
return cachedString;
|
|
}
|
|
|
|
public bool CheckCompatiblity(TaskParameterGroup taskParameterGroup) {
|
|
// only allow same count of parameters
|
|
if (this.Count != taskParameterGroup.Count)
|
|
return false;
|
|
//TaskParameterGroup parms = parameters;
|
|
bool canConvert = true;
|
|
// check wether the taskParameters can be converted to the parameters
|
|
for (int i = 0; i < taskParameterGroup.Count; i++) {
|
|
TaskParameter simpleValue = taskParameterGroup[i];
|
|
TaskParameter complexValue = this[i];
|
|
|
|
switch (simpleValue.valType) {
|
|
case TypeCode.DBNull:
|
|
// FAIL, invalid value
|
|
break;
|
|
case TypeCode.Boolean:
|
|
if ((new List<TypeCode> { TypeCode.Boolean, TypeCode.Int32, TypeCode.Single }).Contains(complexValue.valType)) {
|
|
// pass
|
|
}
|
|
else {
|
|
canConvert = false;
|
|
}
|
|
break;
|
|
case TypeCode.Int32:
|
|
// can be integer, or an enum
|
|
if ((new List<TypeCode> { TypeCode.Int32, TypeCode.Single }).Contains(complexValue.valType)) {
|
|
// pass
|
|
}
|
|
else {
|
|
canConvert = false;
|
|
}
|
|
break;
|
|
case TypeCode.Single:
|
|
if ((new List<TypeCode> { TypeCode.Single }).Contains(complexValue.valType)) {
|
|
// pass
|
|
}
|
|
else {
|
|
canConvert = false;
|
|
}
|
|
break;
|
|
case TypeCode.Empty:
|
|
|
|
break;
|
|
case TypeCode.String:
|
|
if ((new List<TypeCode> { TypeCode.String }).Contains(complexValue.valType)) {
|
|
// pass
|
|
}
|
|
else {
|
|
canConvert = false;
|
|
}
|
|
break;
|
|
}
|
|
|
|
}
|
|
return canConvert;
|
|
}
|
|
}
|
|
} |