// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System.Text; namespace FlaxEditor.Content { /// /// Content item that contains script file with source code. /// /// public abstract class ScriptItem : ContentItem { /// /// Gets the name of the script (deducted from the asset name). /// public string ScriptName => FilterScriptName(ShortName); /// /// Checks if the script item references the valid use script type that can be used in a gameplay. /// public bool IsValid => ScriptsBuilder.FindScript(ScriptName) != null; /// /// Initializes a new instance of the class. /// /// The path to the item. protected ScriptItem(string path) : base(path) { ShowFileExtension = true; } internal static string FilterScriptName(string input) { var length = input.Length; var sb = new StringBuilder(length); // Skip leading '0-9' characters for (int i = 0; i < length; i++) { var c = input[i]; if (char.IsLetterOrDigit(c) && !char.IsDigit(c)) break; } // Remove all characters that are not '_' or 'a-z' or 'A-Z' or '0-9' for (int i = 0; i < length; i++) { var c = input[i]; if (c == '_' || char.IsLetterOrDigit(c)) sb.Append(c); } return sb.ToString(); } /// /// Creates the name of the script for the given file. /// /// The path. /// Script name public static string CreateScriptName(string path) { return FilterScriptName(System.IO.Path.GetFileNameWithoutExtension(path)); } /// public override ContentItemType ItemType => ContentItemType.Script; /// public override ContentItemSearchFilter SearchFilter => ContentItemSearchFilter.Script; /// public override ScriptItem FindScriptWitScriptName(string scriptName) { return scriptName == ScriptName ? this : null; } /// public override void OnPathChanged() { ScriptsBuilder.MarkWorkspaceDirty(); base.OnPathChanged(); } /// public override void OnDelete() { ScriptsBuilder.MarkWorkspaceDirty(); base.OnDelete(); } } }