// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved.
// This code can only be used under the standard Unity Asset Store End User License Agreement
// A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using UnityEngine;
using UnityEngine.Assertions;
namespace Doozy.Runtime.Common
{
/// Base class used to find the filepath inside the project of the given derived class
///
[SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse")]
[SuppressMessage("ReSharper", "StaticMemberInGenericType")]
public class BasePathFinder : ScriptableObject where T : ScriptableObject
{
private static bool debugMode => false;
private static string s_foundPath = string.Empty;
/// Get the filepath for the class
public static string path
{
get
{
#if UNITY_EDITOR
if (!string.IsNullOrEmpty(s_foundPath))
{
if (debugMode) Debug.Log($"Saved Path: {s_foundPath}");
return s_foundPath;
}
T obj = CreateInstance();
var s = UnityEditor.MonoScript.FromScriptableObject(obj);
string assetPath = UnityEditor.AssetDatabase.GetAssetPath(s);
DestroyImmediate(obj);
var fileInfo = new FileInfo(assetPath);
DirectoryInfo baseDir = fileInfo.Directory;
Debug.Assert(baseDir != null, nameof(baseDir) + " != null");
string baseDirPath = CleanPath(baseDir.ToString());
int index = baseDirPath.IndexOf("Assets/", StringComparison.Ordinal);
Assert.IsTrue(index >= 0);
baseDirPath = baseDirPath.Substring(index);
s_foundPath = baseDirPath;
if (debugMode) Debug.Log($"Found Path: {s_foundPath}");
return baseDirPath;
#else
return "Path cannot be returned outside the Unity Editor";
#endif
}
}
/// Replace any back slashes '\' with forward slashes '/' and returns the path string
private static string CleanPath(string rawPath) =>
rawPath.Replace('\\', '/');
}
}