Started making static fields be unserialized
This commit is contained in:
@@ -1,12 +1,107 @@
|
|||||||
using System.IO;
|
using UnityEngine;
|
||||||
using UnityEngine;
|
using System.IO;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace PashaBibko.Pacore.Data
|
namespace PashaBibko.Pacore.Data
|
||||||
{
|
{
|
||||||
public static class PacoreDataLoader
|
public static class PacoreDataLoader
|
||||||
{
|
{
|
||||||
|
private const string PATH_FROM_RESOURCES = "PacoreData";
|
||||||
private const string BASE_PATH = "Pacore/Resources/PacoreData";
|
private const string BASE_PATH = "Pacore/Resources/PacoreData";
|
||||||
public static string FullResourcesPath => Path.Join(Application.dataPath, BASE_PATH);
|
public static string FullResourcesPath => Path.Join(Application.dataPath, BASE_PATH);
|
||||||
public static string ResourcesPath => Path.Join("Assets", BASE_PATH);
|
public static string ResourcesPath => Path.Join("Assets", BASE_PATH);
|
||||||
|
|
||||||
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
|
||||||
|
private 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}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,14 @@ using UnityEngine;
|
|||||||
[StaticInspectorField] private static string OtherStaticText;
|
[StaticInspectorField] private static string OtherStaticText;
|
||||||
[StaticInspectorField] private static int StaticInt;
|
[StaticInspectorField] private static int StaticInt;
|
||||||
|
|
||||||
[InspectorCallable(nameof(PrintStaticField))] public void PrintStaticField()
|
[InspectorCallable(nameof(PrintStaticFields))]
|
||||||
|
public void PrintStaticFields()
|
||||||
{
|
{
|
||||||
Debug.Log(StaticText);
|
Debug.Log
|
||||||
|
(
|
||||||
|
$"{nameof(TestValue)}: [{TestValue}] " +
|
||||||
|
$"{nameof(StaticText)}: [{StaticText}] " +
|
||||||
|
$"{nameof(OtherStaticText)}: [{OtherStaticText}]"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,33 +3,45 @@
|
|||||||
--- !u!159 &1
|
--- !u!159 &1
|
||||||
EditorSettings:
|
EditorSettings:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
serializedVersion: 9
|
serializedVersion: 12
|
||||||
m_ExternalVersionControlSupport: Visible Meta Files
|
|
||||||
m_SerializationMode: 2
|
m_SerializationMode: 2
|
||||||
m_LineEndingsForNewScripts: 2
|
m_LineEndingsForNewScripts: 2
|
||||||
m_DefaultBehaviorMode: 0
|
m_DefaultBehaviorMode: 0
|
||||||
m_PrefabRegularEnvironment: {fileID: 0}
|
m_PrefabRegularEnvironment: {fileID: 0}
|
||||||
m_PrefabUIEnvironment: {fileID: 0}
|
m_PrefabUIEnvironment: {fileID: 0}
|
||||||
m_SpritePackerMode: 0
|
m_SpritePackerMode: 0
|
||||||
|
m_SpritePackerCacheSize: 10
|
||||||
m_SpritePackerPaddingPower: 1
|
m_SpritePackerPaddingPower: 1
|
||||||
|
m_Bc7TextureCompressor: 0
|
||||||
m_EtcTextureCompressorBehavior: 1
|
m_EtcTextureCompressorBehavior: 1
|
||||||
m_EtcTextureFastCompressor: 1
|
m_EtcTextureFastCompressor: 1
|
||||||
m_EtcTextureNormalCompressor: 2
|
m_EtcTextureNormalCompressor: 2
|
||||||
m_EtcTextureBestCompressor: 4
|
m_EtcTextureBestCompressor: 4
|
||||||
m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd;asmdef;asmref;rsp
|
m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd;asmdef;asmref;rsp
|
||||||
m_ProjectGenerationRootNamespace:
|
m_ProjectGenerationRootNamespace:
|
||||||
m_CollabEditorSettings:
|
|
||||||
inProgressEnabled: 1
|
|
||||||
m_EnableTextureStreamingInEditMode: 1
|
m_EnableTextureStreamingInEditMode: 1
|
||||||
m_EnableTextureStreamingInPlayMode: 1
|
m_EnableTextureStreamingInPlayMode: 1
|
||||||
|
m_EnableEditorAsyncCPUTextureLoading: 0
|
||||||
m_AsyncShaderCompilation: 1
|
m_AsyncShaderCompilation: 1
|
||||||
m_EnterPlayModeOptionsEnabled: 0
|
m_PrefabModeAllowAutoSave: 1
|
||||||
|
m_EnterPlayModeOptionsEnabled: 1
|
||||||
m_EnterPlayModeOptions: 3
|
m_EnterPlayModeOptions: 3
|
||||||
m_ShowLightmapResolutionOverlay: 1
|
m_GameObjectNamingDigits: 1
|
||||||
|
m_GameObjectNamingScheme: 0
|
||||||
|
m_AssetNamingUsesSpace: 1
|
||||||
|
m_InspectorUseIMGUIDefaultInspector: 0
|
||||||
m_UseLegacyProbeSampleCount: 0
|
m_UseLegacyProbeSampleCount: 0
|
||||||
|
m_SerializeInlineMappingsOnOneLine: 0
|
||||||
|
m_DisableCookiesInLightmapper: 1
|
||||||
m_AssetPipelineMode: 1
|
m_AssetPipelineMode: 1
|
||||||
|
m_RefreshImportMode: 0
|
||||||
m_CacheServerMode: 0
|
m_CacheServerMode: 0
|
||||||
m_CacheServerEndpoint:
|
m_CacheServerEndpoint:
|
||||||
m_CacheServerNamespacePrefix: default
|
m_CacheServerNamespacePrefix: default
|
||||||
m_CacheServerEnableDownload: 1
|
m_CacheServerEnableDownload: 1
|
||||||
m_CacheServerEnableUpload: 1
|
m_CacheServerEnableUpload: 1
|
||||||
|
m_CacheServerEnableAuth: 0
|
||||||
|
m_CacheServerEnableTls: 0
|
||||||
|
m_CacheServerValidationMode: 2
|
||||||
|
m_CacheServerDownloadBatchSize: 128
|
||||||
|
m_EnableEnlightenBakedGI: 0
|
||||||
|
|||||||
Reference in New Issue
Block a user