Skip to content

Commit

Permalink
🔩 Merge pull request #5 from CarterGames/pre-release
Browse files Browse the repository at this point in the history
📦 0.3.0 Bits
  • Loading branch information
JonathanMCarter authored Oct 28, 2024
2 parents d7826d1 + db06501 commit 0a4d95e
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 78 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public override void OnInspectorGUI()

EditorGUILayout.Space(1.5f);
GeneralUtilEditor.DrawHorizontalGUILine();
base.OnInspectorGUI();

DrawPropertiesExcluding(serializedObject, "sortProperties");
}


Expand All @@ -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) ||
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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<SortPropertiesWindow>())
{
FocusWindowIfItsOpen<SortPropertiesWindow>();
}
else
{
GetWindow<SortPropertiesWindow>(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"));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}


/// <summary>
/// Validates the prefix of the key used.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>If the prefix is valid.</returns>
private static bool PrefixValid(string key)
{
return key.Contains(SecretAPIKeyPrefix) || key.Contains(NtnKeyPrefix);
}


/// <summary>
/// Validates the lenght of the key to max of 50 characters.
/// </summary>
/// <param name="key">The key to check.</param>
/// <returns>If the lenght is valid.</returns>
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class AssetInfo
/// <summary>
/// The version number of the asset.
/// </summary>
public static string VersionNumber => "0.2.3";
public static string VersionNumber => "0.3.0";


/// <summary>
Expand All @@ -40,6 +40,6 @@ public static class AssetInfo
/// <remarks>
/// Format is Y/M/D.
/// </remarks>
public static string ReleaseDate => "2024/09/11";
public static string ReleaseDate => "2024/10/28";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotionSortProperty> sortProperties;
[SerializeField] private List<NotionSortProperty> sortProperties;
#pragma warning restore

[SerializeField] private List<T> data;
Expand All @@ -56,6 +56,12 @@ namespace CarterGames.Standalone.NotionData
/// </summary>
public List<T> Data => data;


/// <summary>
/// Defines the parser used to apply the data to the asset from Notion.
/// </summary>
protected virtual INotionDatabaseProcessor<T> DatabaseProcessor => new NotionDatabaseProcessorStandard<T>();

/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Methods
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */
Expand All @@ -66,40 +72,7 @@ namespace CarterGames.Standalone.NotionData
/// <param name="result">The resulting data downloaded to try and apply.</param>
private void Apply(NotionDatabaseQueryResult result)
{
var list = new List<T>();


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
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Implement to alter the method at which data is parser to an asset from the data downloaded from Notion.
/// </summary>
/// <typeparam name="T">The type to parse to, normally the NotionDataAsset genetic passed argument type.</typeparam>
public interface INotionDatabaseProcessor<T>
{
List<T> Process(NotionDatabaseQueryResult result);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The standard data parser from Notion to a NotionDataAsset. Matches the property name for parses the data to each entry.
/// </summary>
/// <typeparam name="T">The type to parse to.</typeparam>
public sealed class NotionDatabaseProcessorStandard<T> : INotionDatabaseProcessor<T> where T : new()
{
public List<T> Process(NotionDatabaseQueryResult result)
{
var list = new List<T>();

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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace CarterGames.Standalone.NotionData
/// A class to define a sort property.
/// </summary>
[Serializable]
public sealed class NotionSortProperty
public class NotionSortProperty
{
/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Fields
Expand Down
Loading

0 comments on commit 0a4d95e

Please sign in to comment.