| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.CompilerServices;
- using bs;
- namespace bs.Editor
- {
- /** @addtogroup Library
- * @{
- */
- /// <summary>
- /// The primary location for interacting with all the resources in the current project. A complete hierarchy of
- /// resources is provided which can be interacted with by importing new ones, deleting them, moving, renaming and similar.
- /// </summary>
- public sealed class ProjectLibrary : ScriptObject
- {
- /// <summary>
- /// Root entry of the project library, referencing the top level resources folder.
- /// </summary>
- public static DirectoryEntry Root { get { return Internal_GetRoot(); } }
- /// <summary>
- /// Absolute path to the current project's project library resource folder.
- /// </summary>
- public static string ResourceFolder { get { return Internal_GetResourceFolder(); } }
- /// <summary>
- /// Triggered when a new entry is added to the project library. Provided path relative to the project library
- /// resources folder.
- /// </summary>
- public static event Action<string> OnEntryAdded;
- /// <summary>
- /// Triggered when an entry is removed from the project library. Provided path relative to the project library
- /// resources folder.
- /// </summary>
- public static event Action<string> OnEntryRemoved;
- /// <summary>
- /// Triggered when an entry is (re)imported in the project library. Provided path relative to the project library
- /// resources folder.
- /// </summary>
- public static event Action<string> OnEntryImported;
- /// <summary>
- /// Checks wheher an asset import is currently in progress.
- /// </summary>
- internal static bool ImportInProgress { get { return InProgressImportCount > 0; } }
- /// <summary>
- /// Returns the number of resources currently being imported.
- /// </summary>
- internal static int InProgressImportCount { get { return Internal_GetInProgressImportCount(); } }
- /// <summary>
- /// Gets the import progress of all files currently being imported. Returns 0 if no import in progress.
- /// </summary>
- internal static float ImportProgressPercent { get; private set; }
- private static int totalFilesToImport;
- /// <summary>
- /// Checks the project library folder for any modifications and reimports the required resources.
- /// </summary>
- /// <param name="synchronous">If true this method will block until the project library has done refreshing,
- /// otherwise the refresh will happen over the course of this and next frames.</param>
- public static void Refresh(bool synchronous = false)
- {
- totalFilesToImport += Internal_Refresh(ResourceFolder, synchronous);
- if (synchronous)
- totalFilesToImport = 0;
- }
- /// <summary>
- /// Checks the specified folder for any modifications and reimports the required resources.
- /// </summary>
- /// <param name="path">Path to a file or folder to refresh. Relative to the project library resources folder or
- /// absolute.</param>
- public static void Refresh(string path)
- {
- totalFilesToImport += Internal_Refresh(path, false);
- }
- /// <summary>
- /// Registers a new resource in the library.
- /// </summary>
- /// <param name="resource">Resource instance to add to the library. A copy of the resource will be saved at the
- /// provided path.</param>
- /// <param name="path">Path where where to store the resource. Absolute or relative to the resources folder.</param>
- public static void Create(Resource resource, string path)
- {
- Internal_Create(resource, path);
- }
- /// <summary>
- /// Updates a resource that is already in the library.
- /// </summary>
- /// <param name="resource">Resource to save.</param>
- public static void Save(Resource resource)
- {
- Internal_Save(resource);
- }
- /// <summary>
- /// Loads a resource from the project library.
- /// </summary>
- /// <typeparam name="T">Type of the resource to load.</typeparam>
- /// <param name="path">Path of the resource to load. Absolute or relative to the resources folder. If a
- /// sub-resource within a file is needed, append the name of the subresource to the path (
- /// for example mymesh.fbx/my_animation).</param>
- /// <returns>Instance of the loaded resource, or null if not found.</returns>
- public static T Load<T>(string path) where T : Resource
- {
- return (T)Internal_Load(path);
- }
- /// <summary>
- /// Triggers a reimport of a resource using the provided import options, if needed.
- /// </summary>
- /// <param name="path">Path to the resource to reimport, absolute or relative to resources folder.</param>
- /// <param name="options">ptional import options to use when importing the resource. Caller must ensure the import
- /// options are of the correct type for the resource in question. If null is provided default
- /// import options are used.</param>
- /// <param name="force">Should the resource be reimported even if no changes are detected.</param>
- /// <param name="synchronous">If true the import will happen synchronously on the calling thread. If false the
- /// import operation will be queued for execution on a worker thread.</param>
- public static void Reimport(string path, ImportOptions options = null, bool force = false, bool synchronous = false)
- {
- Internal_Reimport(path, options, force, synchronous);
- }
- /// <summary>
- /// Checks how far along is the import for the specified file.
- /// </summary>
- /// <param name="path">Path to the file to check, absolute or relative to the resources folder.</param>
- /// <returns> Reports 1 if the file is fully imported. Reports 0 if the import has not started or the file isn't
- /// even queued for import. Reports >= 0 if the file is in process of being imported. Note that not all importers
- /// support fine grained progress reporting, in which case the import progress will be reported as a binary 0 or 1.
- /// </returns>
- public static float GetImportProgress(string path)
- {
- return Internal_GetImportProgress(path);
- }
- /// <summary>
- /// Cancels import for all resources currently queued for import. This method returns immediately but it may take
- /// some time until imports are fully canceled.
- /// </summary>
- public static void CancelImport()
- {
- Internal_CancelImport();
- }
- /// <summary>
- /// Checks does the project library contain a file or folder at the specified path.
- /// </summary>
- /// <param name="path">Path to the file/folder to check, absolute or relative to resources folder.</param>
- /// <returns>True if the element exists, false otherwise.</returns>
- public static bool Exists(string path)
- {
- return GetEntry(path) != null;
- }
- /// <summary>
- /// Attempts to locate a library entry that describes a file or a folder in the project library.
- /// </summary>
- /// <param name="path">Path to the entry to retrieve, absolute or relative to resources folder.</param>
- /// <returns>Library entry if found, null otherwise. This object can become invalid on the next library refresh
- /// and you are not meant to hold a permanent reference to it.</returns>
- public static LibraryEntry GetEntry(string path)
- {
- return Internal_GetEntry(path);
- }
- /// <summary>
- /// Checks whether the provided path points to a sub-resource. Sub-resource is any resource that is not the
- /// primary resource in the file.
- /// </summary>
- /// <param name="path">Path to the entry, absolute or relative to resources folder.</param>
- /// <returns>True if the path is a sub-resource, false otherwise.</returns>
- public static bool IsSubresource(string path)
- {
- return Internal_IsSubresource(path);
- }
- /// <summary>
- /// Attempts to locate meta-data for a resource at the specified path.
- /// </summary>
- /// <param name="path">Path to the entry to retrieve, absolute or relative to resources folder. If a sub-resource
- /// within a file is needed, append the name of the subresource to the path (for example
- /// mymesh.fbx/my_animation).</param>
- /// <returns>Resource meta-data if the resource was found, null otherwise.</returns>
- public static ResourceMeta GetMeta(string path)
- {
- return Internal_GetMeta(path);
- }
- /// <summary>
- /// Searches the library for a pattern and returns all entries matching it.
- /// </summary>
- /// <param name="pattern">Pattern to search for. Use wildcard * to match any character(s).</param>
- /// <param name="types">Type of resources to search for. If null all entries will be searched.</param>
- /// <returns>A set of entries matching the pattern. These objects can become invalid on the next library refresh
- /// and you are not meant to hold a permanent reference to them.</returns>
- public static LibraryEntry[] Search(string pattern, ResourceType[] types = null)
- {
- return Internal_Search(pattern, types);
- }
- /// <summary>
- /// Returns a path to a resource stored in the project library.
- /// </summary>
- /// <param name="resource">Resource to find the path for.</param>
- /// <returns>Path to relative to the project library resources folder if resource was found, null otherwise.
- /// </returns>
- public static string GetPath(Resource resource)
- {
- return Internal_GetPath(resource);
- }
- /// <summary>
- /// Returns a path to a resource with the specified UUID stored in the project library.
- /// </summary>
- /// <param name="uuid">Unique identifier of the resources to retrieve the path of.</param>
- /// <returns>Path to relative to the project library resources folder if resource was found, null otherwise.
- /// </returns>
- public static string GetPath(UUID uuid)
- {
- return Internal_GetPathFromUUID(ref uuid);
- }
- /// <summary>
- /// Deletes a resource in the project library.
- /// </summary>
- /// <param name="path">Path to the entry to delete, absolute or relative to resources folder.</param>
- public static void Delete(string path)
- {
- Internal_Delete(path);
- }
- /// <summary>
- /// Creates a new folder in the library.
- /// </summary>
- /// <param name="path">Path of the folder to create. Absolute or relative to the resources folder.</param>
- public static void CreateFolder(string path)
- {
- Internal_CreateFolder(path);
- }
- /// <summary>
- /// Renames an entry in the project library.
- /// </summary>
- /// <param name="path">Path of the entry to rename, absolute or relative to resources folder.</param>
- /// <param name="name">New name of the entry with an extension.</param>
- /// <param name="overwrite">Determines should the entry be deleted if one with the provided name already exists. If
- /// this is false and an entry already exists, no rename operation will be performed.</param>
- public static void Rename(string path, string name, bool overwrite = false)
- {
- Internal_Rename(path, name, false);
- }
- /// <summary>
- /// Moves an entry in the project library from one path to another.
- /// </summary>
- /// <param name="oldPath">Source path of the entry, absolute or relative to resources folder.</param>
- /// <param name="newPath">Destination path of the entry, absolute or relative to resources folder.</param>
- /// <param name="overwrite">Determines should the entry be deleted if one at the destination path already exists. If
- /// this is false and an entry already exists, no move operation will be performed.</param>
- public static void Move(string oldPath, string newPath, bool overwrite = false)
- {
- Internal_Move(oldPath, newPath, overwrite);
- }
- /// <summary>
- /// Copies an entry in the project library from one path to another.
- /// </summary>
- /// <param name="source">Source path of the entry, absolute or relative to resources folder.</param>
- /// <param name="destination">Destination path of the entry, absolute or relative to resources folder.</param>
- /// <param name="overwrite">Determines should the entry be deleted if one at the destination path already exists. If
- /// this is false and an entry already exists, no copy operation will be performed.</param>
- public static void Copy(string source, string destination, bool overwrite = false)
- {
- Internal_Copy(source, destination, overwrite);
- }
- /// <summary>
- /// Controls should a resource be included an a build. All dependant resources will also be included.
- /// </summary>
- /// <param name="path">Path of the resource to include, absolute or relative to resources folder.</param>
- /// <param name="include">True if it should be included, false otherwise.</param>
- public static void SetIncludeInBuild(string path, bool include)
- {
- Internal_SetIncludeInBuild(path, include);
- }
- /// <summary>
- /// Assigns a non-specific editor data object to the resource at the specified path.
- /// </summary>
- /// <param name="path">Path to the resource to modify, absolute or relative to resources folder.</param>
- /// <param name="userData">Data to assign to the resource, which can later be retrieved from the resource's
- /// meta-data as needed. </param>
- public static void SetEditorData(string path, object userData)
- {
- Internal_SetEditorData(path, userData);
- }
- /// <summary>
- /// Triggers reimport for queued resource. Should be called once per frame.
- /// </summary>
- internal static void Update()
- {
- Internal_FinalizeImports();
- int inProgressImports = InProgressImportCount;
- if (inProgressImports > 0)
- {
- int numRemaining = totalFilesToImport - inProgressImports;
- float pct = 1.0f;
- if (totalFilesToImport > 0)
- pct = numRemaining / (float)totalFilesToImport;
- ImportProgressPercent = pct;
- EditorApplication.SetStatusImporting(true, pct);
- }
- else
- {
- totalFilesToImport = 0;
- ImportProgressPercent = 0.0f;
- EditorApplication.SetStatusImporting(false, 0.0f);
- }
- }
- /// <summary>
- /// Triggered internally by the runtime when a new entry is added to the project library.
- /// </summary>
- /// <param name="path">Path relative to the project library resources folder.</param>
- private static void Internal_DoOnEntryAdded(string path)
- {
- if (OnEntryAdded != null)
- OnEntryAdded(path);
- }
- /// <summary>
- /// Triggered internally by the runtime when an entry is removed from the project library.
- /// </summary>
- /// <param name="path">Path relative to the project library resources folder.</param>
- private static void Internal_DoOnEntryRemoved(string path)
- {
- if (OnEntryRemoved != null)
- OnEntryRemoved(path);
- }
- /// <summary>
- /// Triggered internally by the runtime when an entry is (re)imported in the project library.
- /// </summary>
- /// <param name="path">Path relative to the project library resources folder.</param>
- private static void Internal_DoOnEntryImported(string path)
- {
- if (OnEntryImported != null)
- OnEntryImported(path);
- }
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern int Internal_Refresh(string path, bool synchronous);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_FinalizeImports();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern int Internal_GetInProgressImportCount();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_Create(Resource resource, string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern Resource Internal_Load(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_Save(Resource resource);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern DirectoryEntry Internal_GetRoot();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern bool Internal_IsSubresource(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_Reimport(string path, ImportOptions options, bool force, bool synchronous);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern float Internal_GetImportProgress(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_CancelImport();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern LibraryEntry Internal_GetEntry(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern ResourceMeta Internal_GetMeta(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern LibraryEntry[] Internal_Search(string path, ResourceType[] types);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetPath(Resource resource);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetPathFromUUID(ref UUID uuid);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_Delete(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_CreateFolder(string path);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_Rename(string path, string name, bool overwrite);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_Move(string oldPath, string newPath, bool overwrite);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_Copy(string source, string destination, bool overwrite);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetResourceFolder();
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_SetIncludeInBuild(string path, bool force);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_SetEditorData(string path, object userData);
- }
- /// <summary>
- /// Type of project library entries.
- /// </summary>
- public enum LibraryEntryType // Note: Must match the C++ enum ProjectLibrary::LibraryEntryType
- {
- File, Directory
- }
- /// <summary>
- /// Type of resources supported by the project library.
- /// </summary>
- public enum ResourceType // Note: Must match the C++ enum ScriptResourceType
- {
- Texture, SpriteTexture, Mesh, Font, Shader, ShaderInclude, Material, Prefab, PlainText, ScriptCode, StringTable,
- GUISkin, PhysicsMaterial, PhysicsMesh, AudioClip, AnimationClip, VectorField, Undefined
- }
- /// <summary>
- /// A generic project library entry that may be a file or a folder.
- /// </summary>
- public class LibraryEntry : ScriptObject
- {
- /// <summary>
- /// Path of the library entry, relative to the project library resources folder.
- /// </summary>
- public string Path { get { return Internal_GetPath(mCachedPtr); } }
- /// <summary>
- /// Name of the library entry.
- /// </summary>
- public string Name { get { return Internal_GetName(mCachedPtr); } }
- /// <summary>
- /// Type of the library entry.
- /// </summary>
- public LibraryEntryType Type { get { return Internal_GetType(mCachedPtr); } }
- /// <summary>
- /// Directory entry that contains this entry. This may be null for the root entry.
- /// </summary>
- public DirectoryEntry Parent { get { return Internal_GetParent(mCachedPtr); } }
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetPath(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetName(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern LibraryEntryType Internal_GetType(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern DirectoryEntry Internal_GetParent(IntPtr thisPtr);
- }
- /// <summary>
- /// A project library entry representing a directory that contains other entries.
- /// </summary>
- public class DirectoryEntry : LibraryEntry
- {
- /// <summary>
- /// A set of entries contained in this entry.
- /// </summary>
- public LibraryEntry[] Children { get { return Internal_GetChildren(mCachedPtr); } }
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern LibraryEntry[] Internal_GetChildren(IntPtr thisPtr);
- }
- /// <summary>
- /// A library entry representing a file.
- /// </summary>
- public class FileEntry : LibraryEntry
- {
- /// <summary>
- /// Import options used for importing the resources in the file.
- /// </summary>
- public ImportOptions Options { get { return Internal_GetImportOptions(mCachedPtr); } }
- /// <summary>
- /// Returns meta-data for all resources part of the file represented by this object.
- /// </summary>
- public ResourceMeta[] ResourceMetas { get { return Internal_GetResourceMetas(mCachedPtr); } }
- /// <summary>
- /// Determines will the resources in the file be included in the project build.
- /// </summary>
- public bool IncludeInBuild { get { return Internal_GetIncludeInBuild(mCachedPtr); } }
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern ImportOptions Internal_GetImportOptions(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern ResourceMeta[] Internal_GetResourceMetas(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern bool Internal_GetIncludeInBuild(IntPtr thisPtr);
- }
- /// <summary>
- /// Contains meta-data for a resource in the ProjectLibrary.
- /// </summary>
- public class ResourceMeta : ScriptObject
- {
- /// <summary>
- /// Unique identifier of the resource.
- /// </summary>
- public UUID UUID
- {
- get
- {
- UUID uuid;
- Internal_GetUUID(mCachedPtr, out uuid);
- return uuid;
- }
- }
- /// <summary>
- /// Returns a name of the subresources. Each resource within a file has a unique name.
- /// </summary>
- public string SubresourceName { get { return Internal_GetSubresourceName(mCachedPtr); } }
- /// <summary>
- /// Custom icons for the resource to display in the editor, if the resource has them.
- /// </summary>
- public ProjectResourceIcons Icons
- {
- get
- {
- ProjectResourceIcons output;
- Internal_GetPreviewIcons(mCachedPtr, out output);
- return output;
- }
- }
- /// <summary>
- /// Type of the resource referenced by this entry.
- /// </summary>
- public ResourceType ResType { get { return Internal_GetResourceType(mCachedPtr); } }
- /// <summary>
- /// Type information of the resource referenced by this entry.
- /// </summary>
- public Type Type { get { return Internal_GetType(mCachedPtr); } }
- /// <summary>
- /// Non-specific data assigned to the resource, available in editor only.
- /// </summary>
- public object EditorData { get { return Internal_GetEditorData(mCachedPtr); } }
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_GetUUID(IntPtr thisPtr, out UUID uuid);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern string Internal_GetSubresourceName(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void Internal_GetPreviewIcons(IntPtr thisPtr, out ProjectResourceIcons icons);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern ResourceType Internal_GetResourceType(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern Type Internal_GetType(IntPtr thisPtr);
- [MethodImpl(MethodImplOptions.InternalCall)]
- private static extern object Internal_GetEditorData(IntPtr thisPtr);
- }
- /** @} */
- }
|