Explorar el Código

Reimporting script code as editor-only now works properly

BearishSun hace 10 años
padre
commit
b649ca1fae

+ 1 - 1
BansheeEditor/Include/BsProjectLibrary.h

@@ -328,7 +328,7 @@ namespace BansheeEngine
 		/**
 		/**
 		 * @brief	Checks is the resource a native engine resource that doesn't require importing.
 		 * @brief	Checks is the resource a native engine resource that doesn't require importing.
 		 */
 		 */
-		bool isNative(ResourceEntry* resource) const;
+		bool isNative(const Path& path) const;
 
 
 		/**
 		/**
 		 * @brief	Returns a path to a .meta file based on the resource path.
 		 * @brief	Returns a path to a .meta file based on the resource path.

+ 3 - 3
BansheeEditor/Source/BsProjectLibrary.cpp

@@ -379,7 +379,7 @@ namespace BansheeEngine
 		{
 		{
 			// Note: If resource is native we just copy it to the internal folder. We could avoid the copy and 
 			// Note: If resource is native we just copy it to the internal folder. We could avoid the copy and 
 			// load the resource directly from the Resources folder but that requires complicating library code.
 			// load the resource directly from the Resources folder but that requires complicating library code.
-			bool isNativeResource = isNative(resource);
+			bool isNativeResource = isNative(resource->path);
 
 
 			ImportOptionsPtr curImportOptions = nullptr;
 			ImportOptionsPtr curImportOptions = nullptr;
 			if (importOptions == nullptr && !isNativeResource)
 			if (importOptions == nullptr && !isNativeResource)
@@ -1046,9 +1046,9 @@ namespace BansheeEngine
 		return fullPath.getWExtension() == L".meta";
 		return fullPath.getWExtension() == L".meta";
 	}
 	}
 
 
-	bool ProjectLibrary::isNative(ResourceEntry* resource) const
+	bool ProjectLibrary::isNative(const Path& path) const
 	{
 	{
-		WString extension = resource->path.getWExtension();
+		WString extension = path.getWExtension();
 
 
 		return extension == L".asset" || extension == L".prefab";
 		return extension == L".asset" || extension == L".prefab";
 	}
 	}

+ 15 - 0
MBansheeEditor/EditorApplication.cs

@@ -473,6 +473,9 @@ namespace BansheeEditor
             foreach (var resourceUUID in dirtyResources)
             foreach (var resourceUUID in dirtyResources)
             {
             {
                 string path = ProjectLibrary.GetPath(resourceUUID);
                 string path = ProjectLibrary.GetPath(resourceUUID);
+                if (!IsNative(path))
+                    continue; // Native resources can't be changed
+
                 Resource resource = ProjectLibrary.Load<Resource>(path);
                 Resource resource = ProjectLibrary.Load<Resource>(path);
 
 
                 if(resource != null)
                 if(resource != null)
@@ -578,6 +581,18 @@ namespace BansheeEditor
             return dirtyResources.Contains(resource.UUID);
             return dirtyResources.Contains(resource.UUID);
         }
         }
 
 
+        /// <summary>
+        /// Checks does the path represent a native resource.
+        /// </summary>
+        /// <param name="path">Filename or path to check.</param>
+        /// <returns>True if the path represents a native resource.</returns>
+        public static bool IsNative(string path)
+        {
+            string extension = Path.GetExtension(path);
+
+            return extension == ".asset" || extension == ".prefab";
+        }
+
         /// <summary>
         /// <summary>
         /// Unloads the currently loaded project. Offers the user a chance to save the current scene if it is modified.
         /// Unloads the currently loaded project. Offers the user a chance to save the current scene if it is modified.
         /// Automatically saves all project data before unloading.
         /// Automatically saves all project data before unloading.

+ 118 - 68
MBansheeEditor/Inspectors/ScriptCodeInspector.cs

@@ -1,69 +1,119 @@
-using System.Collections.Generic;
-using BansheeEngine;
-
-namespace BansheeEditor
-{
-    /// <summary>
-    /// Renders an inspector for the <see cref="ScriptCode"/> resource.
-    /// </summary>
-    [CustomInspector(typeof (ScriptCode))]
-    internal class ScriptCodeInspector : Inspector
-    {
-        private const int MAX_SHOWN_CHARACTERS = 3000;
-
-        private GUILabel textLabel = new GUILabel("", EditorStyles.MultiLineLabel, GUIOption.FixedHeight(500));
-        private GUITexture textBg = new GUITexture(null, EditorStyles.ScrollAreaBg);
-        private GUIToggleField isEditorField = new GUIToggleField(new LocEdString("Is editor script"));
-
-        private string shownText = "";
-
-        /// <inheritdoc/>
-        protected internal override void Initialize()
-        {
-            ScriptCode scriptCode = InspectedObject as ScriptCode;
-            if (scriptCode == null)
-                return;
-
-            isEditorField.OnChanged += x =>
-            {
-                scriptCode.EditorScript = x;
-                EditorApplication.SetDirty(scriptCode);
-            };
-
-            GUIPanel textPanel = Layout.AddPanel();
-            GUILayout textLayoutY = textPanel.AddLayoutY();
-            textLayoutY.AddSpace(5);
-            GUILayout textLayoutX = textLayoutY.AddLayoutX();
-            textLayoutX.AddSpace(5);
-            textLayoutX.AddElement(textLabel);
-            textLayoutX.AddSpace(5);
-            textLayoutY.AddSpace(5);
-
-            GUIPanel textBgPanel = textPanel.AddPanel(1);
-            textBgPanel.AddElement(textBg);
-
-            Layout.AddElement(isEditorField);
-        }
-
-        /// <inheritdoc/>
-        protected internal override InspectableState Refresh()
-        {
-            ScriptCode scriptCode = InspectedObject as ScriptCode;
-            if (scriptCode == null)
-                return InspectableState.NotModified;
-
-            isEditorField.Value = scriptCode.EditorScript;
-
-            string newText = scriptCode.Text;
-            string newShownText = scriptCode.Text.Substring(0, MathEx.Min(newText.Length, MAX_SHOWN_CHARACTERS));
-
-            if (newShownText != shownText)
-            {
-                textLabel.SetContent(newShownText);
-                shownText = newShownText;
-            }
-
-            return InspectableState.NotModified;
-        }
-    }
+using System.Collections.Generic;
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    /// <summary>
+    /// Renders an inspector for the <see cref="ScriptCode"/> resource.
+    /// </summary>
+    [CustomInspector(typeof (ScriptCode))]
+    internal class ScriptCodeInspector : Inspector
+    {
+        private const int MAX_SHOWN_CHARACTERS = 3000;
+
+        private GUILabel textLabel = new GUILabel("", EditorStyles.MultiLineLabel, GUIOption.FixedHeight(500));
+        private GUITexture textBg = new GUITexture(null, EditorStyles.ScrollAreaBg);
+        private GUIToggleField isEditorField = new GUIToggleField(new LocEdString("Is editor script"));
+
+        private string shownText = "";
+        private ScriptCodeImportOptions importOptions;
+
+        /// <inheritdoc/>
+        protected internal override void Initialize()
+        {
+            ScriptCode scriptCode = InspectedObject as ScriptCode;
+            if (scriptCode == null)
+                return;
+
+            importOptions = GetImportOptions();
+
+            isEditorField.OnChanged += x =>
+            {
+                importOptions.EditorScript = x;
+            };
+
+            GUIPanel textPanel = Layout.AddPanel();
+            GUILayout textLayoutY = textPanel.AddLayoutY();
+            textLayoutY.AddSpace(5);
+            GUILayout textLayoutX = textLayoutY.AddLayoutX();
+            textLayoutX.AddSpace(5);
+            textLayoutX.AddElement(textLabel);
+            textLayoutX.AddSpace(5);
+            textLayoutY.AddSpace(5);
+
+            GUIPanel textBgPanel = textPanel.AddPanel(1);
+            textBgPanel.AddElement(textBg);
+
+            Layout.AddElement(isEditorField);
+
+            GUIButton reimportButton = new GUIButton(new LocEdString("Reimport"));
+            reimportButton.OnClick += TriggerReimport;
+
+            GUILayout reimportButtonLayout = Layout.AddLayoutX();
+            reimportButtonLayout.AddFlexibleSpace();
+            reimportButtonLayout.AddElement(reimportButton);
+        }
+
+        /// <inheritdoc/>
+        protected internal override InspectableState Refresh()
+        {
+            ScriptCode scriptCode = InspectedObject as ScriptCode;
+            if (scriptCode == null)
+                return InspectableState.NotModified;
+
+            isEditorField.Value = importOptions.EditorScript;
+
+            string newText = scriptCode.Text;
+            string newShownText = scriptCode.Text.Substring(0, MathEx.Min(newText.Length, MAX_SHOWN_CHARACTERS));
+
+            if (newShownText != shownText)
+            {
+                textLabel.SetContent(newShownText);
+                shownText = newShownText;
+            }
+
+            return InspectableState.NotModified;
+        }
+
+        /// <summary>
+        /// Retrieves import options for the resource we're currently inspecting.
+        /// </summary>
+        /// <returns>Script code import options object.</returns>
+        private ScriptCodeImportOptions GetImportOptions()
+        {
+            ScriptCode scriptCode = InspectedObject as ScriptCode;
+            ScriptCodeImportOptions output = null;
+
+            if (scriptCode != null)
+            {
+                LibraryEntry libEntry = ProjectLibrary.GetEntry(ProjectLibrary.GetPath(scriptCode));
+                if (libEntry != null && libEntry.Type == LibraryEntryType.File)
+                {
+                    FileEntry fileEntry = (FileEntry)libEntry;
+                    output = fileEntry.Options as ScriptCodeImportOptions;
+                }
+            }
+
+            if (output == null)
+            {
+                if (importOptions == null)
+                    output = new ScriptCodeImportOptions();
+                else
+                    output = importOptions;
+            }
+
+            return output;
+        }
+
+        /// <summary>
+        /// Reimports the script code resource according to the currently set import options.
+        /// </summary>
+        private void TriggerReimport()
+        {
+            ScriptCode scriptCode = (ScriptCode)InspectedObject;
+            string resourcePath = ProjectLibrary.GetPath(scriptCode);
+
+            ProjectLibrary.Reimport(resourcePath, importOptions, true);
+        }
+    }
 }
 }