diff --git a/Carter Games/Notion Database To Unity/Code/Editor/Editors/Drawer/PropertyDrawerNotionSortProperty.cs.meta b/Carter Games/Notion Database To Unity/Code/Editor/Editors/Drawer/PropertyDrawerNotionSortProperty.cs.meta deleted file mode 100644 index 88cb68b..0000000 --- a/Carter Games/Notion Database To Unity/Code/Editor/Editors/Drawer/PropertyDrawerNotionSortProperty.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: d1afd22c464a401e83335cf1ad3e8e78 -timeCreated: 1724741430 \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Editor/Editors/Inspectors/NotionDataAssetEditor.cs b/Carter Games/Notion Database To Unity/Code/Editor/Editors/Inspectors/NotionDataAssetEditor.cs index 948c810..c29a9f7 100644 --- a/Carter Games/Notion Database To Unity/Code/Editor/Editors/Inspectors/NotionDataAssetEditor.cs +++ b/Carter Games/Notion Database To Unity/Code/Editor/Editors/Inspectors/NotionDataAssetEditor.cs @@ -49,7 +49,8 @@ public override void OnInspectorGUI() EditorGUILayout.Space(1.5f); GeneralUtilEditor.DrawHorizontalGUILine(); - base.OnInspectorGUI(); + + DrawPropertiesExcluding(serializedObject, "sortProperties"); } @@ -64,10 +65,20 @@ private void RenderNotionSettings() EditorGUILayout.PropertyField(serializedObject.Fp("linkToDatabase"), NotionMetaData.DatabaseLink); EditorGUILayout.PropertyField(serializedObject.Fp("databaseApiKey"), NotionMetaData.ApiKey); - EditorGUI.indentLevel++; - EditorGUILayout.PropertyField(serializedObject.Fp("sortProperties")); - EditorGUI.indentLevel--; + EditorGUILayout.BeginHorizontal(); + + // Coming... eventually... + // if (GUILayout.Button("Filters")) + // { + // + // } + + if (GUILayout.Button($"Sorting ({serializedObject.Fp("sortProperties").arraySize})")) + { + SortPropertiesWindow.OpenWindow(serializedObject); + } + EditorGUILayout.EndHorizontal(); EditorGUI.BeginDisabledGroup( !NotionSecretKeyValidator.IsKeyValid(serializedObject.Fp("databaseApiKey").stringValue) || diff --git a/Carter Games/Notion Database To Unity/Code/Editor/Editors/Sort Property.meta b/Carter Games/Notion Database To Unity/Code/Editor/Editors/Sort Property.meta new file mode 100644 index 0000000..55ebf9f --- /dev/null +++ b/Carter Games/Notion Database To Unity/Code/Editor/Editors/Sort Property.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 80afe9bc31f64cd0bfb00924a0d7cfaa +timeCreated: 1726242613 \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Editor/Editors/Drawer/PropertyDrawerNotionSortProperty.cs b/Carter Games/Notion Database To Unity/Code/Editor/Editors/Sort Property/SortPropertiesWindow.cs similarity index 53% rename from Carter Games/Notion Database To Unity/Code/Editor/Editors/Drawer/PropertyDrawerNotionSortProperty.cs rename to Carter Games/Notion Database To Unity/Code/Editor/Editors/Sort Property/SortPropertiesWindow.cs index 9f63614..b045266 100644 --- a/Carter Games/Notion Database To Unity/Code/Editor/Editors/Drawer/PropertyDrawerNotionSortProperty.cs +++ b/Carter Games/Notion Database To Unity/Code/Editor/Editors/Sort Property/SortPropertiesWindow.cs @@ -22,41 +22,40 @@ */ using UnityEditor; -using UnityEngine; namespace CarterGames.Standalone.NotionData.Editor { - [CustomPropertyDrawer(typeof(NotionSortProperty))] - public class PropertyDrawerNotionSortProperty : PropertyDrawer - { - public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) - { - EditorGUI.BeginProperty(position, label, property); + public sealed class SortPropertiesWindow : EditorWindow + { + private static SerializedObject Target { get; set; } + + + public static void OpenWindow(SerializedObject target) + { + Target = target; - EditorGUI.BeginChangeCheck(); + if (HasOpenInstances()) + { + FocusWindowIfItsOpen(); + } + else + { + GetWindow(true, "Edit Sort Properties"); + } + } - var indent = EditorGUI.indentLevel; - EditorGUI.indentLevel = 0; - var left = new Rect(position.x, position.y, position.width - 120f, EditorGUIUtility.singleLineHeight); - var right = new Rect(position.x + position.width - 17.5f, position.y, 120f, EditorGUIUtility.singleLineHeight); - - EditorGUI.PropertyField(left, property.Fpr("propertyName"), GUIContent.none); - EditorGUI.PropertyField(right, property.Fpr("ascending"), GUIContent.none); + private void OnGUI() + { + if (Target == null) return; - if (EditorGUI.EndChangeCheck()) - { - property.serializedObject.ApplyModifiedProperties(); - } + EditorGUILayout.HelpBox("Edit the sort properties for this Notion data asset below", MessageType.Info); - EditorGUI.indentLevel = indent; - EditorGUI.EndProperty(); - } + EditorGUILayout.Space(5f); - - public override float GetPropertyHeight(SerializedProperty property, GUIContent label) - { - return EditorGUIUtility.singleLineHeight; - } - } + var entry = Target.Fp("sortProperties"); + + EditorGUILayout.PropertyField(Target.Fp("sortProperties")); + } + } } \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Editor/Editors/Sort Property/SortPropertiesWindow.cs.meta b/Carter Games/Notion Database To Unity/Code/Editor/Editors/Sort Property/SortPropertiesWindow.cs.meta new file mode 100644 index 0000000..ab6d866 --- /dev/null +++ b/Carter Games/Notion Database To Unity/Code/Editor/Editors/Sort Property/SortPropertiesWindow.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 88bbbd8bf67f4943ac791519f31021c6 +timeCreated: 1726242621 \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Editor/Notion/Notion Api/Secret Key/NotionSecretKeyValidator.cs b/Carter Games/Notion Database To Unity/Code/Editor/Notion/Notion Api/Secret Key/NotionSecretKeyValidator.cs index a26e29e..f401f9e 100644 --- a/Carter Games/Notion Database To Unity/Code/Editor/Notion/Notion Api/Secret Key/NotionSecretKeyValidator.cs +++ b/Carter Games/Notion Database To Unity/Code/Editor/Notion/Notion Api/Secret Key/NotionSecretKeyValidator.cs @@ -32,8 +32,9 @@ public static class NotionSecretKeyValidator | Fields ───────────────────────────────────────────────────────────────────────────────────────────────────────────── */ - private const string APIKeyPrefix = "secret_"; - private const int APIKeySuffixLength = 43; + private const string SecretAPIKeyPrefix = "secret_"; + private const string NtnKeyPrefix = "ntn_"; + private const int MaxKeyLenght = 50; /* ───────────────────────────────────────────────────────────────────────────────────────────────────────────── | Methods @@ -48,8 +49,44 @@ public static bool IsKeyValid(string key) { return !string.IsNullOrEmpty(key) && - key.Contains(APIKeyPrefix) && - key.Substring(0, key.Length - APIKeyPrefix.Length).Length.Equals(APIKeySuffixLength); + PrefixValid(key) && + LenghtValid(key); + } + + + /// + /// Validates the prefix of the key used. + /// + /// The key to check. + /// If the prefix is valid. + private static bool PrefixValid(string key) + { + return key.Contains(SecretAPIKeyPrefix) || key.Contains(NtnKeyPrefix); + } + + + /// + /// Validates the lenght of the key to max of 50 characters. + /// + /// The key to check. + /// If the lenght is valid. + private static bool LenghtValid(string key) + { + if (key.Length != MaxKeyLenght) return false; + + if (key.Contains(SecretAPIKeyPrefix)) + { + return key.Replace(SecretAPIKeyPrefix, string.Empty).Length == + (MaxKeyLenght - SecretAPIKeyPrefix.Length); + } + + if (key.Contains(NtnKeyPrefix)) + { + return key.Replace(NtnKeyPrefix, string.Empty).Length == + (MaxKeyLenght - NtnKeyPrefix.Length); + } + + return false; } } } \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Editor/Supporting Backend/AssetInfo.cs b/Carter Games/Notion Database To Unity/Code/Editor/Supporting Backend/AssetInfo.cs index 48e9104..c3dabad 100644 --- a/Carter Games/Notion Database To Unity/Code/Editor/Supporting Backend/AssetInfo.cs +++ b/Carter Games/Notion Database To Unity/Code/Editor/Supporting Backend/AssetInfo.cs @@ -31,7 +31,7 @@ public static class AssetInfo /// /// The version number of the asset. /// - public static string VersionNumber => "0.2.3"; + public static string VersionNumber => "0.3.0"; /// @@ -40,6 +40,6 @@ public static class AssetInfo /// /// Format is Y/M/D. /// - public static string ReleaseDate => "2024/09/11"; + public static string ReleaseDate => "2024/10/28"; } } \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Data Assets/NotionDataAsset.cs b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Data Assets/NotionDataAsset.cs index 64cd089..b1742ff 100644 --- a/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Data Assets/NotionDataAsset.cs +++ b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Data Assets/NotionDataAsset.cs @@ -42,7 +42,7 @@ namespace CarterGames.Standalone.NotionData #pragma warning disable [SerializeField, HideInInspector] private string linkToDatabase; // Is used in editor space, so ignore the not used warning. [SerializeField, HideInInspector] private string databaseApiKey; // Is used in editor space, so ignore the not used warning. - [SerializeField, HideInInspector] private List sortProperties; + [SerializeField] private List sortProperties; #pragma warning restore [SerializeField] private List data; @@ -56,6 +56,12 @@ namespace CarterGames.Standalone.NotionData /// public List Data => data; + + /// + /// Defines the parser used to apply the data to the asset from Notion. + /// + protected virtual INotionDatabaseProcessor DatabaseProcessor => new NotionDatabaseProcessorStandard(); + /* ───────────────────────────────────────────────────────────────────────────────────────────────────────────── | Methods ───────────────────────────────────────────────────────────────────────────────────────────────────────────── */ @@ -66,40 +72,7 @@ namespace CarterGames.Standalone.NotionData /// The resulting data downloaded to try and apply. private void Apply(NotionDatabaseQueryResult result) { - var list = new List(); - - - foreach (var row in result.Rows) - { - var newEntry = new T(); - var newEntryFields = newEntry.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - - foreach (var field in newEntryFields) - { - if (row.DataLookup.ContainsKey(field.Name.Trim().ToLower())) - { - var valueData = row.DataLookup[field.Name.Trim().ToLower()]; - var fieldType = field.FieldType; - - if (fieldType.BaseType.FullName.Contains(typeof(NotionDataWrapper<>).Namespace + ".NotionDataWrapper")) - { - var instance = valueData.GetValueAs(fieldType); - field.SetValue(newEntry, instance); - - instance.GetType().BaseType.GetMethod("Assign", BindingFlags.NonPublic | BindingFlags.Instance) - ?.Invoke(instance, null); - } - else - { - field.SetValue(newEntry, valueData.GetValueAs(fieldType)); - } - } - } - - list.Add(newEntry); - } - - data = list; + data = DatabaseProcessor.Process(result); PostDataDownloaded(); #if UNITY_EDITOR diff --git a/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing.meta b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing.meta new file mode 100644 index 0000000..a5a6975 --- /dev/null +++ b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fb2f9aba634d42cea7e4fbde5eafbafd +timeCreated: 1729754596 \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/INotionDatabaseProcessor.cs b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/INotionDatabaseProcessor.cs new file mode 100644 index 0000000..5a130c3 --- /dev/null +++ b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/INotionDatabaseProcessor.cs @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024 Carter Games + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +using System.Collections.Generic; + +namespace CarterGames.Standalone.NotionData +{ + /// + /// Implement to alter the method at which data is parser to an asset from the data downloaded from Notion. + /// + /// The type to parse to, normally the NotionDataAsset genetic passed argument type. + public interface INotionDatabaseProcessor + { + List Process(NotionDatabaseQueryResult result); + } +} \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/INotionDatabaseProcessor.cs.meta b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/INotionDatabaseProcessor.cs.meta new file mode 100644 index 0000000..1641a05 --- /dev/null +++ b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/INotionDatabaseProcessor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6dedb5a8222d46f980415ae00b12df82 +timeCreated: 1729754625 \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/NotionDatabaseProcessorStandard.cs b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/NotionDatabaseProcessorStandard.cs new file mode 100644 index 0000000..fcf8979 --- /dev/null +++ b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/NotionDatabaseProcessorStandard.cs @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2024 Carter Games + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +using System.Collections.Generic; +using System.Reflection; + +namespace CarterGames.Standalone.NotionData +{ + /// + /// The standard data parser from Notion to a NotionDataAsset. Matches the property name for parses the data to each entry. + /// + /// The type to parse to. + public sealed class NotionDatabaseProcessorStandard : INotionDatabaseProcessor where T : new() + { + public List Process(NotionDatabaseQueryResult result) + { + var list = new List(); + + foreach (var row in result.Rows) + { + var newEntry = new T(); + var newEntryFields = newEntry.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + + foreach (var field in newEntryFields) + { + if (row.DataLookup.ContainsKey(field.Name.Trim().ToLower())) + { + var valueData = row.DataLookup[field.Name.Trim().ToLower()]; + var fieldType = field.FieldType; + + if (fieldType.BaseType.FullName.Contains(typeof(NotionDataWrapper<>).Namespace + ".NotionDataWrapper")) + { + var instance = valueData.GetValueAs(fieldType); + field.SetValue(newEntry, instance); + + instance.GetType().BaseType.GetMethod("Assign", BindingFlags.NonPublic | BindingFlags.Instance) + ?.Invoke(instance, null); + } + else + { + field.SetValue(newEntry, valueData.GetValueAs(fieldType)); + } + } + } + + list.Add(newEntry); + } + + return list; + } + } +} \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/NotionDatabaseProcessorStandard.cs.meta b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/NotionDatabaseProcessorStandard.cs.meta new file mode 100644 index 0000000..7d1c19a --- /dev/null +++ b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Notion Database Processing/NotionDatabaseProcessorStandard.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c48b9b214a1c41d5b29c4e7dc2925776 +timeCreated: 1729754654 \ No newline at end of file diff --git a/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Sort Properties/NotionSortProperty.cs b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Sort Properties/NotionSortProperty.cs index ae86777..e959060 100644 --- a/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Sort Properties/NotionSortProperty.cs +++ b/Carter Games/Notion Database To Unity/Code/Runtime/Notion/Sort Properties/NotionSortProperty.cs @@ -30,7 +30,7 @@ namespace CarterGames.Standalone.NotionData /// A class to define a sort property. /// [Serializable] - public sealed class NotionSortProperty + public class NotionSortProperty { /* ───────────────────────────────────────────────────────────────────────────────────────────────────────────── | Fields diff --git a/package.json b/package.json index f49fe41..cda4c21 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "games.carter.standalone.notiondata", - "version": "0.2.3", + "version": "0.3.0", "displayName": "Notion Data (Standalone)", "description": "A system to import Notion databases into a Unity scriptable object for use in Unity projects. This is a standalone version of the system in the Cart library of the same name.", "unity": "2020.3",