Skip to content

Commit

Permalink
📦 2.1.5 Release
Browse files Browse the repository at this point in the history
- Updated the initialization & reload checks in the back end of the asset.
- [Community Reported]: Fixed a legacy code line in the settings provider class that was missed out of an earlier refactor.
  • Loading branch information
JonathanMCarter committed Apr 17, 2024
1 parent a43264f commit e87368d
Show file tree
Hide file tree
Showing 38 changed files with 520 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ private void InitializeObject()
serializedObject.Update();

// Adds to save data if it doesn't exist.
if (UtilEditor.Settings.SaveData.Data.Contains((SaveObject)target)) return;
if (UtilEditor.AssetGlobalRuntimeSettings.SaveData.Data.Contains((SaveObject)target)) return;

UtilEditor.Settings.SaveData.Data.Add((SaveObject)target);
UtilEditor.AssetGlobalRuntimeSettings.SaveData.Data.Add((SaveObject)target);

propertiesLookup = new Dictionary<string, SerializedProperty>()
{
{ "SaveKey", serializedObject.Fp("saveKey") },
};

EditorUtility.SetDirty(UtilEditor.Settings.SaveData);
EditorUtility.SetDirty(UtilEditor.AssetGlobalRuntimeSettings.SaveData);

AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace CarterGames.Assets.SaveManager.Editor
/// <summary>
/// A custom inspector for the settings asset scriptable object.
/// </summary>
[CustomEditor(typeof(SettingsAssetRuntime))]
[CustomEditor(typeof(AssetGlobalRuntimeSettings))]
public sealed class SettingsAssetRuntimeEditor : UnityEditor.Editor
{
/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -89,7 +89,7 @@ private static void DrawCogIcon()
/// </summary>
private void DrawDataSettings()
{
UtilEditor.DrawSoScriptSection(target as SettingsAssetRuntime);
UtilEditor.DrawSoScriptSection(target as AssetGlobalRuntimeSettings);

GUILayout.Space(12.5f);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static void LoadSaveProfile(TextAsset data)
}
catch (Exception e)
{
SmLog.Error($"Failed to read to {UtilEditor.Settings.SavePath} with the exception: {e}");
SmLog.Error($"Failed to read to {UtilEditor.AssetGlobalRuntimeSettings.SavePath} with the exception: {e}");
return;
}

Expand All @@ -225,7 +225,7 @@ private static void LoadSaveProfile(TextAsset data)
}

// Updates the data in the editor & in the actual save.
EditorUtility.SetDirty(UtilEditor.Settings);
EditorUtility.SetDirty(UtilEditor.AssetGlobalRuntimeSettings);
AssetDatabase.SaveAssets();
SaveManager.Save();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class SaveManagerMenuItems
[MenuItem("Tools/Carter Games/Save Manager/Load Save Data", priority = 30)]
public static void ManualLoadGame()
{
SaveManager.Load(new StandardSaveHandler().LoadFromFile(UtilEditor.Settings.SavePath));
SaveManager.Load(new StandardSaveHandler().LoadFromFile(UtilEditor.AssetGlobalRuntimeSettings.SavePath));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ private static SerializedObject SettingsAssetObject
{
if (settingsAssetObject != null) return settingsAssetObject;

if (!UtilEditor.AssetIndex.Lookup.ContainsKey(typeof(SettingsAssetRuntime).ToString()))
if (!UtilEditor.AssetIndex.Lookup.ContainsKey(typeof(AssetGlobalRuntimeSettings).ToString()))
{
UtilEditor.Initialize();
}

settingsAssetObject = new SerializedObject(UtilEditor.AssetIndex.Lookup[typeof(SettingsAssetRuntime).ToString()][0]);
settingsAssetObject = new SerializedObject(UtilEditor.AssetIndex.Lookup[typeof(AssetGlobalRuntimeSettings).ToString()][0]);
return settingsAssetObject;
}
}
Expand Down Expand Up @@ -195,7 +195,7 @@ private static void DrawGeneralOptions()
EditorUtility.RevealInFinder(UtilEditor.Settings.SavePath.Replace("save.sf", string.Empty));
}

GUI.backgroundColor = UtilEditor.SettingsAssetEditor.BackgroundColor;
GUI.backgroundColor = Color.white;

EditorGUILayout.EndHorizontal();
#else
Expand All @@ -205,12 +205,12 @@ private static void DrawGeneralOptions()

if (GUILayout.Button(ExplorerButtonLabel))
{
if (!Directory.Exists(UtilEditor.Settings.SavePath))
if (!Directory.Exists(UtilEditor.AssetGlobalRuntimeSettings.SavePath))
{
FileEditorUtil.CreateToDirectory(UtilEditor.Settings.SavePath);
FileEditorUtil.CreateToDirectory(UtilEditor.AssetGlobalRuntimeSettings.SavePath);
}

EditorUtility.RevealInFinder(UtilEditor.Settings.SavePath.Replace("save.sf", string.Empty));
EditorUtility.RevealInFinder(UtilEditor.AssetGlobalRuntimeSettings.SavePath.Replace("save.sf", string.Empty));
}

GUI.backgroundColor = Color.white;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,31 @@ namespace CarterGames.Assets.SaveManager.Editor
/// <summary>
/// Handles the setup of the asset index for runtime references to scriptable objects used for the asset.
/// </summary>
public sealed class AssetIndexHandler : IPreprocessBuildWithReport
public sealed class AssetIndexHandler : IPreprocessBuildWithReport, IAssetEditorInitialize
{
/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Fields
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

private const string AssetFilter = "t:savemanagerasset";
private static readonly string AssetFilter = $"t:{nameof(SaveManagerAsset)}";

/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| IAssetEditorInitialize Implementation
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Defines the order that this initializer run at.
/// </summary>
public int InitializeOrder => 1;


/// <summary>
/// Runs when the asset initialize flow is used.
/// </summary>
public void OnEditorInitialized()
{
UpdateIndex();
}

/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| IPreprocessBuildWithReport Implementation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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 NON-INFRINGEMENT. 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.Linq;
using System.Threading.Tasks;
using CarterGames.Common;
using UnityEditor;

namespace CarterGames.Assets.SaveManager.Editor
{
/// <summary>
/// Handles any initial listeners in the project for the asset.
/// </summary>
public static class AssetInitializer
{
/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Fields
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

// The key for if the asset has been initialized.
private static readonly string AssetInitializeKey = $"{FileEditorUtil.AssetName}_Session_EditorInitialize";

/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Properties
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Gets if the asset is initialized or not.
/// </summary>
public static bool IsInitialized
{
get => SessionState.GetBool(AssetInitializeKey, false);
private set => SessionState.SetBool(AssetInitializeKey, value);
}

/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Events
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Is raised when the asset is initialized.
/// </summary>
public static readonly Evt Initialized = new Evt();

/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Methods
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Initializes the editor logic for the asset when called.
/// </summary>
[InitializeOnLoadMethod]
private static void TryInitialize()
{
if (IsInitialized) return;
InitializeEditorClasses();
}


/// <summary>
/// Runs through all interfaces for initializing the editor asset logic and runs each in the defined order.
/// </summary>
private static async void InitializeEditorClasses()
{
var initClasses = InterfaceHelper.GetAllInterfacesInstancesOfType<IAssetEditorInitialize>();

if (initClasses.Length > 0)
{
foreach (var init in initClasses.OrderBy(t => t.InitializeOrder))
{
init.OnEditorInitialized();
await Task.Yield();
}
}

OnAllClassesInitialized();
}


/// <summary>
/// Runs any post initialize logic to complete the process.
/// </summary>
private static void OnAllClassesInitialized()
{
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

IsInitialized = true;
Initialized.Raise();
}
}
}

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 @@ -21,77 +21,66 @@
* THE SOFTWARE.
*/

using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using CarterGames.Common;
using UnityEditor;
using UnityEditor.Callbacks;

namespace CarterGames.Assets.SaveManager.Editor
{
/// <summary>
/// Handles the auto setup of the save manager when the assets are changed in the project.
/// Handles any reload listeners in the project for the asset.
/// </summary>
public class AssetInitializer : AssetPostprocessor
public static class AssetReloadHandler
{
/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Methods
| Events
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Runs after assets have imported / script reload etc at a safe time to edit assets.
/// </summary>
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets,
string[] movedFromAssetPaths)
{
TryInitialize();
}


/// <summary>
/// Initializes the save data on project load
/// </summary>
[InitializeOnLoadMethod]
private static void TryInitSave()
{
if (SessionState.GetBool("HasLoaded", false)) return;
DelayUpdate();
}


/// <summary>
/// Listen for a delayed update to ensure the editor is all loaded before loading data.
/// Raises when the reload has occured.
/// </summary>
private static void DelayUpdate()
{
EditorApplication.delayCall -= LoadOnProjectOpen;
EditorApplication.delayCall += LoadOnProjectOpen;
}


public static readonly Evt Reloaded = new Evt();

/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Methods
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Loads the latest data into the save system for use.
/// Add subscription to the delay call when scripts reload.
/// </summary>
private static void LoadOnProjectOpen()
[DidReloadScripts]
private static void FireReloadCalls()
{
EditorApplication.delayCall -= LoadOnProjectOpen;

// Initializes the Save Manager and loads the data for use.
typeof(SaveManager).GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Static)?.Invoke(null, null);
if (EditorApplication.isCompiling || EditorApplication.isUpdating)
{
EditorApplication.delayCall -= CallListeners;
EditorApplication.delayCall += CallListeners;
return;
}

SessionState.SetBool("HasLoaded", true);
EditorApplication.delayCall -= CallListeners;
EditorApplication.delayCall += CallListeners;
}


/// <summary>
/// Creates the scriptable objects for the asset if they don't exist yet.
/// Updates all the listeners when called.
/// </summary>
private static void TryInitialize()
private static async void CallListeners()
{
LegacyIndexRemovalTool.TryRemoveOldIndex();
var reloadClasses = InterfaceHelper.GetAllInterfacesInstancesOfType<IAssetEditorReload>();

if (reloadClasses.Length > 0)
{
foreach (var init in reloadClasses)
{
init.OnEditorReloaded();
await Task.Yield();
}
}

if (UtilEditor.HasInitialized) return;
UtilEditor.Initialize();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Reloaded.Raise();
}
}
}
}

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

Loading

0 comments on commit e87368d

Please sign in to comment.