| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.IO; |
| | 7 | | using GDX.DataTables.DataBinding; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace GDX.DataTables |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A collection of metadata for a <see cref="DataTableBase" />. |
| | 14 | | /// </summary> |
| | 15 | | /// <remarks> |
| | 16 | | /// These objects are created with the <see cref="HideFlags" /> preventing them from entering a build, |
| | 17 | | /// or being seen/editted directly in the editor. |
| | 18 | | /// </remarks> |
| | 19 | | public class DataTableMetaData : ScriptableObject |
| | 20 | | { |
| | 21 | | #pragma warning disable IDE1006 |
| | 22 | | // ReSharper disable InconsistentNaming |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// A `source of truth` binding for the data found in the <see cref="DataTableBase" />. |
| | 26 | | /// </summary> |
| | 27 | | /// <remarks> |
| | 28 | | /// On-disk bindings are relative to the asset folder. |
| | 29 | | /// </remarks> |
| | 30 | | public string BindingUri; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// The timestamp last gotten from the binding. |
| | 34 | | /// </summary> |
| | 35 | | public DateTime BindingTimestamp; |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// The data version number used the last time data was pushed to the binding. |
| | 39 | | /// </summary> |
| | 40 | | public ulong BindingDataVersion; |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// The user-friendly name used throughout the author-time experience for the <see cref="DataTableBase" />. |
| | 44 | | /// </summary> |
| 5 | 45 | | public string DisplayName = "GDX DataTable"; |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Locks out fields on the <see cref="DataTableBase" /> which are not reference based. |
| | 49 | | /// This is useful for when a dataset is being driven by a binding. |
| | 50 | | /// </summary> |
| | 51 | | public bool ReferencesOnlyMode; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// EXPERIMENTAL! Supports undo/redo operations on the <see cref="DataTableBase" />. |
| | 55 | | /// </summary> |
| | 56 | | public bool SupportsUndo; |
| | 57 | |
|
| | 58 | | // ReSharper enable InconsistentNaming |
| | 59 | | #pragma warning restore IDE1006 |
| | 60 | |
|
| | 61 | | #if UNITY_2021_3_OR_NEWER |
| | 62 | |
|
| | 63 | | public void SetBinding(string uri) |
| 0 | 64 | | { |
| 0 | 65 | | if (ValidateBinding(uri) != null) |
| 0 | 66 | | { |
| 0 | 67 | | BindingUri = uri; |
| 0 | 68 | | } |
| | 69 | | else |
| 0 | 70 | | { |
| 0 | 71 | | BindingUri = null; |
| 0 | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public static FormatBase ValidateBinding(string uri) |
| 0 | 76 | | { |
| 0 | 77 | | if (uri == null) |
| 0 | 78 | | { |
| 0 | 79 | | return null; |
| | 80 | | } |
| | 81 | |
|
| 0 | 82 | | string filePath = Path.Combine(Application.dataPath, uri); |
| 0 | 83 | | if (File.Exists(filePath)) |
| 0 | 84 | | { |
| 0 | 85 | | return DataBindingProvider.GetFormatFromFile(filePath); |
| | 86 | | } |
| | 87 | |
|
| 0 | 88 | | return DataBindingProvider.GetFormatFromUri(uri); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | public static string CreateBinding(string uri) |
| 0 | 92 | | { |
| | 93 | | // Convert to relative path, given this is stored in the Unity project we will need it in this form. |
| 0 | 94 | | if (File.Exists(uri)) |
| 0 | 95 | | { |
| 0 | 96 | | return Path.GetRelativePath(Application.dataPath, uri); |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | return uri; |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public bool HasBinding() |
| 28 | 103 | | { |
| 28 | 104 | | return BindingUri != null; |
| 28 | 105 | | } |
| | 106 | |
|
| | 107 | | #endif // UNITY_2021_3_OR_NEWER |
| | 108 | | } |
| | 109 | | } |