Removed static field support
This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
using PashaBibko.Pacore.Attributes;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System;
|
||||
|
||||
namespace PashaBibko.Pacore.Editor.Caches
|
||||
{
|
||||
public static class StaticInspectorFieldCache
|
||||
{
|
||||
public struct FieldData
|
||||
{
|
||||
public FieldInfo Info;
|
||||
public object Value;
|
||||
}
|
||||
|
||||
private const BindingFlags BINDING_FLAGS =
|
||||
BindingFlags.Static |
|
||||
BindingFlags.NonPublic |
|
||||
BindingFlags.Public;
|
||||
|
||||
private static Dictionary<Type, FieldData[]> Cache { get; } = new();
|
||||
|
||||
public static FieldData[] GetAllFieldsOfType(Type type)
|
||||
{
|
||||
/* Checks the cache for the type */
|
||||
if (Cache.TryGetValue(type, out FieldData[] cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
/* Else finds all the fields with the attribute */
|
||||
FieldInfo[] fields = type.GetFields(BINDING_FLAGS);
|
||||
List<FieldData> instances = new();
|
||||
|
||||
foreach (FieldInfo field in fields)
|
||||
{
|
||||
StaticInspectorFieldAttribute attribute
|
||||
= field.GetCustomAttribute<StaticInspectorFieldAttribute>();
|
||||
|
||||
if (attribute != null)
|
||||
{
|
||||
FieldData data = new()
|
||||
{
|
||||
Info = field
|
||||
};
|
||||
|
||||
instances.Add(data);
|
||||
}
|
||||
}
|
||||
|
||||
/* Adds the fields to the cache before returning */
|
||||
FieldData[] array = instances.ToArray();
|
||||
Cache.Add(type, array);
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c0d83f0696f4a8d92f0ad89514645f4
|
||||
timeCreated: 1769350289
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 023429caa3904eb99618d2b079e3a508
|
||||
timeCreated: 1769386974
|
||||
@@ -1,25 +0,0 @@
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace PashaBibko.Pacore.Editor.Data
|
||||
{
|
||||
public static class ScriptableObjectGenerator
|
||||
{
|
||||
public static T Create<T>(string path, string name) where T : ScriptableObject
|
||||
{
|
||||
/* Creates the asset instance */
|
||||
T asset = ScriptableObject.CreateInstance<T>();
|
||||
|
||||
/* Creates the file on disk */
|
||||
string fullpath = Path.Join(path, name);
|
||||
AssetDatabase.CreateAsset(asset, fullpath);
|
||||
|
||||
/* Refreshes the asset database before returning */
|
||||
AssetDatabase.SaveAssets();
|
||||
AssetDatabase.Refresh();
|
||||
|
||||
return asset;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 670c37fd285242438eabfb6c328a7e53
|
||||
timeCreated: 1769363293
|
||||
@@ -1,42 +0,0 @@
|
||||
using PashaBibko.Pacore.Data;
|
||||
using UnityEditor;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace PashaBibko.Pacore.Editor.Data
|
||||
{
|
||||
[InitializeOnLoad] public static class PacoreDataCreator
|
||||
{
|
||||
private static StaticFieldValues ValuesAsset;
|
||||
|
||||
static PacoreDataCreator() // Runs on editor startup or recompile
|
||||
{
|
||||
/* Creates the folder for the data files */
|
||||
if (!Directory.Exists(PacoreDataLoader.FullResourcesPath))
|
||||
{
|
||||
Directory.CreateDirectory(PacoreDataLoader.FullResourcesPath);
|
||||
}
|
||||
|
||||
/* Creates the ScriptableObject asset file for StaticFieldValues */
|
||||
const string VALUES_NAME = "StaticFieldValues.asset"; // .asset is REQUIRED by unity API
|
||||
string valuesPath = Path.Combine(PacoreDataLoader.ResourcesPath, VALUES_NAME);
|
||||
if (!File.Exists(valuesPath))
|
||||
{
|
||||
ValuesAsset = ScriptableObjectGenerator.Create<StaticFieldValues>
|
||||
(
|
||||
path: PacoreDataLoader.ResourcesPath,
|
||||
name: VALUES_NAME
|
||||
);
|
||||
}
|
||||
|
||||
/* If one already exists loads it */
|
||||
else
|
||||
{
|
||||
ValuesAsset = AssetDatabase.LoadAssetAtPath<StaticFieldValues>(valuesPath);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetStaticFieldValue(FieldInfo field, object value)
|
||||
=> ValuesAsset.AddValue(field, value);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da662fed35ba45aa890b57c00f94a987
|
||||
timeCreated: 1769375660
|
||||
@@ -1,97 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using PashaBibko.Pacore.Data;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace PashaBibko.Pacore.Editor.Data
|
||||
{
|
||||
public static class StaticFieldValuesExtensions
|
||||
{
|
||||
public static void AddValue(this StaticFieldValues asset, FieldInfo info, object value)
|
||||
{
|
||||
/* Checks field has a declaring type */
|
||||
Type declaringType = info.DeclaringType;
|
||||
if (declaringType == null)
|
||||
{
|
||||
Debug.LogError($"[StaticFieldValues] Cannot add field {info.Name} because it has no declaring type");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Checks if the type is already contained */
|
||||
string typename = declaringType.FullName;
|
||||
int tIdx = -1;
|
||||
|
||||
if (asset.StoredValues != null)
|
||||
{
|
||||
for (int i = 0; i < asset.StoredValues.Length; i++)
|
||||
{
|
||||
if (asset.StoredValues[i].Typename == typename)
|
||||
{
|
||||
tIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Adds the type if it is not contained */
|
||||
if (tIdx == -1)
|
||||
{
|
||||
StaticFieldValues.TypeFieldValues newType = new()
|
||||
{
|
||||
Typename = typename,
|
||||
Fields = Array.Empty<StaticFieldValues.FieldValue>()
|
||||
};
|
||||
|
||||
List<StaticFieldValues.TypeFieldValues> types = asset.StoredValues?.ToList() ??
|
||||
new List<StaticFieldValues.TypeFieldValues>();
|
||||
types.Add(newType);
|
||||
|
||||
asset.StoredValues = types.ToArray();
|
||||
tIdx = types.Count - 1; // -1 for 0 based indexing
|
||||
}
|
||||
|
||||
/* Checks if the field is already contained in the type */
|
||||
string fieldName = info.Name;
|
||||
int sIdx = -1;
|
||||
|
||||
Debug.Assert(asset.StoredValues != null, nameof(asset.StoredValues) + " != null");
|
||||
for (int i = 0; i < asset.StoredValues[tIdx].Fields.Length; i++)
|
||||
{
|
||||
if (asset.StoredValues[tIdx].Fields[i].Name == fieldName)
|
||||
{
|
||||
sIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Adds the field if it is not contained */
|
||||
if (sIdx == -1)
|
||||
{
|
||||
StaticFieldValues.FieldValue newValue = new()
|
||||
{
|
||||
SerializedValue = value.ToString(),
|
||||
Name = fieldName
|
||||
};
|
||||
|
||||
List<StaticFieldValues.FieldValue> fields = asset.StoredValues[tIdx].Fields?.ToList() ??
|
||||
new List<StaticFieldValues.FieldValue>();
|
||||
fields.Add(newValue);
|
||||
|
||||
asset.StoredValues[tIdx].Fields = fields.ToArray();
|
||||
}
|
||||
|
||||
/* Else updates the value */
|
||||
else
|
||||
{
|
||||
asset.StoredValues[tIdx].Fields[sIdx].SerializedValue = value.ToString();
|
||||
}
|
||||
|
||||
/* Tells Unity to save the value */
|
||||
EditorUtility.SetDirty(asset);
|
||||
AssetDatabase.SaveAssets();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4ba9a121a4524876b105945f75e1e79d
|
||||
timeCreated: 1769387061
|
||||
@@ -2,9 +2,6 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using PashaBibko.Pacore.Attributes;
|
||||
using PashaBibko.Pacore.Editor.Data;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace PashaBibko.Pacore.Editor.Drawers
|
||||
@@ -16,7 +13,6 @@ namespace PashaBibko.Pacore.Editor.Drawers
|
||||
{
|
||||
DrawDefaultInspector();
|
||||
DrawFunctionButtons(target);
|
||||
DrawStaticFields(target);
|
||||
}
|
||||
|
||||
public static void DrawFunctionButtons(Object target)
|
||||
@@ -42,53 +38,5 @@ namespace PashaBibko.Pacore.Editor.Drawers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawStaticFields(Object target)
|
||||
{
|
||||
Type type = target.GetType();
|
||||
StaticInspectorFieldCache.FieldData[] fields
|
||||
= StaticInspectorFieldCache.GetAllFieldsOfType(type);
|
||||
|
||||
if (fields.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EditorGUILayout.Space();
|
||||
EditorGUILayout.LabelField("Static Fields (only applied at runtime)", EditorStyles.boldLabel);
|
||||
|
||||
for (int i = 0; i < fields.Length; i++)
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
fields[i].Value = DrawStaticField(fields[i].Info, fields[i].Value);
|
||||
bool changed = EditorGUI.EndChangeCheck();
|
||||
|
||||
if (changed)
|
||||
{
|
||||
PacoreDataCreator.SetStaticFieldValue(fields[i].Info, fields[i].Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static object DrawStaticField(FieldInfo field, object current)
|
||||
{
|
||||
Type type = field.FieldType;
|
||||
string label = field.Name;
|
||||
|
||||
if (type == typeof(int))
|
||||
return EditorGUILayout.IntField(label, current as int? ?? 0);
|
||||
|
||||
if (type == typeof(float))
|
||||
return EditorGUILayout.FloatField(label, current as float? ?? 0f);
|
||||
|
||||
if (type == typeof(string))
|
||||
return EditorGUILayout.TextField(label, current as string ?? "");
|
||||
|
||||
if (type == typeof(bool))
|
||||
return EditorGUILayout.Toggle(label, current as bool? ?? false);
|
||||
|
||||
EditorGUILayout.LabelField(label, $"Unsupported type: {type.FullName}");
|
||||
return current;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using PashaBibko.Pacore.Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace PashaBibko.Pacore
|
||||
@@ -53,10 +52,6 @@ namespace PashaBibko.Pacore
|
||||
AttributeCache[type].Add(current);
|
||||
}
|
||||
}
|
||||
|
||||
/* Initializes PacoreDataLoader as it requires this to have been loaded first */
|
||||
// TODO: Add a way for this to trigger an event that can be run by other scripts dynamically
|
||||
PacoreDataLoader.LoadAndApplyStaticValues();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d323da97ac614fdb8e6d952111ba0b7d
|
||||
timeCreated: 1769362533
|
||||
@@ -1,106 +0,0 @@
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace PashaBibko.Pacore.Data
|
||||
{
|
||||
public static class PacoreDataLoader
|
||||
{
|
||||
private const string PATH_FROM_RESOURCES = "PacoreData";
|
||||
private const string BASE_PATH = "Pacore/Resources/PacoreData";
|
||||
public static string FullResourcesPath => Path.Join(Application.dataPath, BASE_PATH);
|
||||
public static string ResourcesPath => Path.Join("Assets", BASE_PATH);
|
||||
|
||||
public static void LoadAndApplyStaticValues()
|
||||
{
|
||||
/* Loads the static values from the serialized file */
|
||||
string filename = Path.Join(PATH_FROM_RESOURCES, "StaticFieldValues");
|
||||
StaticFieldValues asset = Resources.Load<StaticFieldValues>(filename);
|
||||
if (asset is null)
|
||||
{
|
||||
Debug.LogWarning("Could not find StaticFieldValues asset to load");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Applies the values to the static fields */
|
||||
foreach (StaticFieldValues.TypeFieldValues values in asset.StoredValues)
|
||||
{
|
||||
/* Makes sure the type is valid before trying to apply values */
|
||||
Type type = Type.GetType($"{values.Typename}, Assembly-CSharp");
|
||||
if (type is null)
|
||||
{
|
||||
Debug.LogWarning($"Could not find type [{values.Typename}]");
|
||||
continue;
|
||||
}
|
||||
|
||||
ApplyStaticValuesToType(type, values.Fields);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyStaticValuesToType(Type type, StaticFieldValues.FieldValue[] fields)
|
||||
{
|
||||
const BindingFlags FLAGS = BindingFlags.NonPublic |
|
||||
BindingFlags.Public |
|
||||
BindingFlags.Static;
|
||||
|
||||
/* Converts the fields to a dictionary for easier access */
|
||||
Dictionary<string, string> values = fields.ToDictionary
|
||||
(
|
||||
f => f.Name,
|
||||
f => f.SerializedValue
|
||||
);
|
||||
|
||||
/* Fetches the static field values of the current type and applies the values */
|
||||
FieldInfo[] typeFields = type.GetFields(FLAGS);
|
||||
foreach (FieldInfo field in typeFields)
|
||||
{
|
||||
string name = field.Name;
|
||||
if (values.TryGetValue(name, out string serialized))
|
||||
{
|
||||
object value = ConvertFromString(field.FieldType, serialized);
|
||||
field.SetValue(null, value);
|
||||
|
||||
Debug.Log($"Set [{field.Name}] to [{value}]");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"Could not find field [{name}]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static object ConvertFromString(Type type, string serialized)
|
||||
{
|
||||
if (string.IsNullOrEmpty(serialized))
|
||||
return type.IsValueType ? Activator.CreateInstance(type) : null;
|
||||
|
||||
if (type.IsEnum)
|
||||
return Enum.Parse(type, serialized, ignoreCase: true);
|
||||
|
||||
if (type == typeof(Vector2))
|
||||
return JsonUtility.FromJson<Vector2>(serialized);
|
||||
|
||||
if (type == typeof(Vector3))
|
||||
return JsonUtility.FromJson<Vector3>(serialized);
|
||||
|
||||
if (type == typeof(Vector4))
|
||||
return JsonUtility.FromJson<Vector4>(serialized);
|
||||
|
||||
if (type == typeof(Color))
|
||||
return JsonUtility.FromJson<Color>(serialized);
|
||||
|
||||
if (type == typeof(string))
|
||||
return serialized;
|
||||
|
||||
if (type.IsPrimitive || type == typeof(decimal))
|
||||
return Convert.ChangeType(serialized, type);
|
||||
|
||||
return type.IsSerializable
|
||||
? JsonUtility.FromJson(serialized, type)
|
||||
: throw new NotSupportedException($"Cannot convert {type} to type {type.FullName}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 79da011308294074b6edb339ac2f271b
|
||||
timeCreated: 1769372269
|
||||
@@ -1,22 +0,0 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace PashaBibko.Pacore.Data
|
||||
{
|
||||
public class StaticFieldValues : ScriptableObject
|
||||
{
|
||||
[Serializable] public struct FieldValue
|
||||
{
|
||||
public string SerializedValue;
|
||||
public string Name;
|
||||
}
|
||||
|
||||
[Serializable] public struct TypeFieldValues
|
||||
{
|
||||
public string Typename;
|
||||
public FieldValue[] Fields;
|
||||
}
|
||||
|
||||
public TypeFieldValues[] StoredValues;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e0846e954a8489cbc43e81d8f1dcc01
|
||||
timeCreated: 1769362550
|
||||
@@ -13,7 +13,8 @@ namespace PashaBibko.Pacore.Threading
|
||||
{
|
||||
public IncorrectThreadException(string message)
|
||||
: base(message)
|
||||
{ }
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
using PashaBibko.Pacore.Attributes;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateInstanceOnStart] public class TestMonoBehaviour : MonoBehaviour
|
||||
{
|
||||
public int TestValue;
|
||||
|
||||
[StaticInspectorField] private static string StaticText;
|
||||
[StaticInspectorField] private static string OtherStaticText;
|
||||
[StaticInspectorField] private static int StaticInt = 0;
|
||||
[StaticInspectorField] private static int OtherStaticInt = 0;
|
||||
|
||||
[InspectorCallable(nameof(PrintStaticFields))]
|
||||
public void PrintStaticFields()
|
||||
{
|
||||
Debug.Log
|
||||
(
|
||||
$"{nameof(StaticInt)}: [{StaticInt}] " +
|
||||
$"{nameof(OtherStaticInt)}: [{OtherStaticInt}] " +
|
||||
$"{nameof(StaticText)}: [{StaticText}] " +
|
||||
$"{nameof(OtherStaticText)}: [{OtherStaticText}]"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a411a69d284bfd04bafc210a81b43e88
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user