Made modified static fields serialize to disk
This commit is contained in:
3
Assets/Pacore/.gitignore
vendored
Normal file
3
Assets/Pacore/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Files are generated/modified at runtime and do not need backing up #
|
||||||
|
Resources.meta
|
||||||
|
Resources
|
||||||
3
Assets/Pacore/Editor/Data.meta
Normal file
3
Assets/Pacore/Editor/Data.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 023429caa3904eb99618d2b079e3a508
|
||||||
|
timeCreated: 1769386974
|
||||||
25
Assets/Pacore/Editor/Data/CreateScriptableObject.cs
Normal file
25
Assets/Pacore/Editor/Data/CreateScriptableObject.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Assets/Pacore/Editor/Data/CreateScriptableObject.cs.meta
Normal file
3
Assets/Pacore/Editor/Data/CreateScriptableObject.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 670c37fd285242438eabfb6c328a7e53
|
||||||
|
timeCreated: 1769363293
|
||||||
42
Assets/Pacore/Editor/Data/PacoreDataCreator.cs
Normal file
42
Assets/Pacore/Editor/Data/PacoreDataCreator.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Assets/Pacore/Editor/Data/PacoreDataCreator.cs.meta
Normal file
3
Assets/Pacore/Editor/Data/PacoreDataCreator.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: da662fed35ba45aa890b57c00f94a987
|
||||||
|
timeCreated: 1769375660
|
||||||
97
Assets/Pacore/Editor/Data/StaticFieldValuesExtensions.cs
Normal file
97
Assets/Pacore/Editor/Data/StaticFieldValuesExtensions.cs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 4ba9a121a4524876b105945f75e1e79d
|
||||||
|
timeCreated: 1769387061
|
||||||
@@ -3,6 +3,7 @@ using UnityEditor;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System;
|
using System;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using PashaBibko.Pacore.Editor.Data;
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
|
|
||||||
namespace PashaBibko.Pacore.Editor.Drawers
|
namespace PashaBibko.Pacore.Editor.Drawers
|
||||||
@@ -57,7 +58,14 @@ namespace PashaBibko.Pacore.Editor.Drawers
|
|||||||
|
|
||||||
for (int i = 0; i < fields.Length; i++)
|
for (int i = 0; i < fields.Length; i++)
|
||||||
{
|
{
|
||||||
|
EditorGUI.BeginChangeCheck();
|
||||||
fields[i].Value = DrawStaticField(fields[i].Info, fields[i].Value);
|
fields[i].Value = DrawStaticField(fields[i].Info, fields[i].Value);
|
||||||
|
bool changed = EditorGUI.EndChangeCheck();
|
||||||
|
|
||||||
|
if (changed)
|
||||||
|
{
|
||||||
|
PacoreDataCreator.SetStaticFieldValue(fields[i].Info, fields[i].Value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
3
Assets/Pacore/Runtime/Data.meta
Normal file
3
Assets/Pacore/Runtime/Data.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d323da97ac614fdb8e6d952111ba0b7d
|
||||||
|
timeCreated: 1769362533
|
||||||
12
Assets/Pacore/Runtime/Data/DataLoader.cs
Normal file
12
Assets/Pacore/Runtime/Data/DataLoader.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System.IO;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace PashaBibko.Pacore.Data
|
||||||
|
{
|
||||||
|
public static class PacoreDataLoader
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Assets/Pacore/Runtime/Data/DataLoader.cs.meta
Normal file
3
Assets/Pacore/Runtime/Data/DataLoader.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 79da011308294074b6edb339ac2f271b
|
||||||
|
timeCreated: 1769372269
|
||||||
22
Assets/Pacore/Runtime/Data/StaticFieldValues.cs
Normal file
22
Assets/Pacore/Runtime/Data/StaticFieldValues.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Assets/Pacore/Runtime/Data/StaticFieldValues.cs.meta
Normal file
3
Assets/Pacore/Runtime/Data/StaticFieldValues.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7e0846e954a8489cbc43e81d8f1dcc01
|
||||||
|
timeCreated: 1769362550
|
||||||
Reference in New Issue
Block a user