<commit_before>/*
 * Copyright (c) 2010 <NAME>
 * 
 * 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 NONINFRINGEMENT. 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 UnityEngine;
using UnityEditor;
using org.critterai.nav;
using org.critterai.nav.u3d;

/// <summary>
/// Custom inspector for <see cref="BakedNavmesh"/>.
/// </summary>
[CustomEditor(typeof(BakedNavmesh))]
public class BakedNavmeshEditor
    : Editor
{
    private bool mForceDirty = false;

    /// <summary>
    /// Controls behavior of the inspector.
    /// </summary>
    public override void OnInspectorGUI()
    {
        BakedNavmesh targ = (BakedNavmesh)target;

        EditorGUIUtility.LookLikeControls(125);

        EditorGUILayout.Separator();

        const string label = "State";
        string state;

        if (targ.HasNavmesh())
            state = "Baked";
        else if (targ.sourceData == null)
            state = "No tile source.";
        else if (targ.sourceData.HasTileBuildData)
            state = "Ready to bake.";
        else
            state = "No tile data.";

        EditorGUILayout.LabelField(label, state);

        EditorGUILayout.Separator();

        bool bo = targ.DisplayMesh;
        bool bn = EditorGUILayout.Toggle("Display Mesh", bo);
        if (bn != bo)
            targ.DisplayMesh = bn;

        EditorGUILayout.Separator();
        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.PrefixLabel("Tile Data Source");
        targ.sourceData = (DSTileData)EditorGUILayout.ObjectField(
            targ.sourceData, typeof(DSTileData));
         
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Separator();
        EditorGUILayout.BeginHorizontal();

        if (!IsReadyToBuild(targ))
            GUI.enabled = false;
        if (GUILayout.Button("Bake") && BuildMesh(targ))
            mForceDirty = true;
        GUI.enabled = true;

        if (!targ.HasNavmesh())
            GUI.enabled = false;
        if (GUILayout.Button("Clear Mesh") && targ.HasNavmesh())
        {
            targ.ClearMesh();
            mForceDirty = true;
        }
        GUI.enabled = true;

        EditorGUILayout.EndHorizontal();

        if (GUI.changed || mForceDirty)
        {
            EditorUtility.SetDirty(target);
            mForceDirty = false;
        }
    }

    private static bool BuildMesh(BakedNavmesh targ)
    {
        string pre = targ.name + ": NavmeshBuilder: Build aborted: ";

        NavmeshTileBuildData buildData = 
            targ.sourceData.GetTileBuildData(false);

        if (buildData == null || buildData.IsDisposed)
        {
            Debug.LogError(pre 
                + " Data source provided null or disposed tile data.");
            return false;
        }

        Navmesh mesh;
        NavStatus status = Navmesh.Build(buildData, out mesh);

        // Debug.Log(NavmeshDebug.ToString(mesh.GetTile(0).GetHeader()));

        if (NavUtil.Failed(status))
        {
            Debug.LogError(pre + "Status flags: " + status.ToString());
            return false;
        }

        if (!targ.Bake(mesh))
        {
            Debug.LogError(pre + "Could not load mesh into builder."
                + " Internal error?");
            return false;
        }

        return true;
    }

    private static bool IsReadyToBuild(BakedNavmesh targ)
    {
        return (targ.sourceData != null && targ.sourceData.HasTileBuildData);
    }
}<commit_msg>cai-nmgen-u3d-editor: Add warning when a source without any meshes is added to a MeshFilterSource. (Fixes issue 11)<commit_after>/*
 * Copyright (c) 2010 <NAME>
 * 
 * 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 NONINFRINGEMENT. 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 UnityEngine;
using UnityEditor;
using org.critterai.nav;
using org.critterai.nav.u3d;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

/// <summary>
/// Custom inspector for <see cref="BakedNavmesh"/>.
/// </summary>
[CustomEditor(typeof(BakedNavmesh))]
public class BakedNavmeshEditor
    : Editor
{
    /// <summary>
    /// Controls behavior of the inspector.
    /// </summary>
    public override void OnInspectorGUI()
    {
        bool mForceDirty = false;
        BakedNavmesh targ = (BakedNavmesh)target;

        EditorGUIUtility.LookLikeControls(125);

        EditorGUILayout.Separator();

        const string label = "State";
        string state;

        if (targ.HasNavmesh())
            state = "Baked";
        else if (targ.sourceData == null)
            state = "No tile source.";
        else if (targ.sourceData.HasTileBuildData)
            state = "Ready to bake.";
        else
            state = "No tile data.";

        EditorGUILayout.LabelField(label, state);

        EditorGUILayout.Separator();

        bool bo = targ.DisplayMesh;
        bool bn = EditorGUILayout.Toggle("Display Mesh", bo);
        if (bn != bo)
            targ.DisplayMesh = bn;

        EditorGUILayout.Separator();
        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.PrefixLabel("Tile Data Source");
        targ.sourceData = (DSTileData)EditorGUILayout.ObjectField(
            targ.sourceData, typeof(DSTileData));
         
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Separator();

        EditorGUILayout.BeginHorizontal();

        if (!IsReadyToBuild(targ))
            GUI.enabled = false;
        if (GUILayout.Button("Bake") && BuildMesh(targ))
            mForceDirty = true;
        GUI.enabled = true;

        GUI.enabled = targ.HasNavmesh();
        if (GUILayout.Button("Clear Mesh"))
        {
            targ.ClearMesh();
            mForceDirty = true;
        }
        GUI.enabled = true;

        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Separator();

        EditorGUILayout.BeginHorizontal();

        GUI.enabled = targ.HasNavmesh();
        if (GUILayout.Button("Save"))
        {
            string filePath = EditorUtility.SaveFilePanel(
                "Save Navigation Mesh"
                , ""
                , targ.name
                , "navmesh");
            SaveMesh(targ, filePath);
        }
        GUI.enabled = true;

        if (GUILayout.Button("Load"))
        {
            string filePath = EditorUtility.OpenFilePanel(
                "Select Serialized Navmesh"
                , ""
                , "navmesh");
            if (LoadMesh(targ, filePath))
                mForceDirty = true;
        }

        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();

        GUI.enabled = targ.HasNavmesh();
        if (GUILayout.Button("Save Bytes"))
        {
            string filePath = EditorUtility.SaveFilePanel(
                "Save Navigation Mesh As Bytes"
                , ""
                , targ.name
                , "navmeshb");
            SaveMeshBytes(targ, filePath);
        }
        GUI.enabled = true;

        if (GUILayout.Button("Load Bytes"))
        {
            string filePath = EditorUtility.OpenFilePanel(
                "Select Byte Serialized Navigation Mesh"
                , ""
                , "navmeshb");
            if (LoadMeshBytes(targ, filePath))
                mForceDirty = true;
        }

        EditorGUILayout.EndHorizontal();

        if (GUI.changed || mForceDirty)
        {
            EditorUtility.SetDirty(target);
            mForceDirty = false;
        }
    }

    private static bool LoadMeshBytes(BakedNavmesh targ, string filePath)
    {
        string msg = null;

        if (filePath.Length == 0)
            return false;

        FileStream fs = null;
        BinaryFormatter formatter = new BinaryFormatter();

        try
        {
            fs = new FileStream(filePath, FileMode.Open);
            System.Object obj = formatter.Deserialize(fs);

            // Roundabout load method is used for error detection.

            Navmesh nm;
            NavStatus status = Navmesh.Build((byte[])obj, out nm);
            if (NavUtil.Failed(status))
                msg = status.ToString();
            else if (!targ.Bake(nm))
                msg = "Could not load mesh. Internal error?";
        }
        catch (System.Exception ex)
        {
            msg = ex.Message;
        }
        finally
        {
            if (fs != null)
                fs.Close();
        }

        if (msg != null)
        {
            Debug.LogError(targ.name + ": BakedNavmesh: Load bytes failed: " 
                + msg);
            return false;
        }

        return true;
    }

    private static bool LoadMesh(BakedNavmesh targ, string filePath)
    {
        string msg = null;

        if (filePath.Length == 0)
            return false;

        FileStream fs = null;
        BinaryFormatter formatter = new BinaryFormatter();

        try
        {
            fs = new FileStream(filePath, FileMode.Open);

            if (!targ.Bake((Navmesh)formatter.Deserialize(fs)))
                msg = "Could not load mesh. Internal error?";
        }
        catch (System.Exception ex)
        {
            msg = ex.Message;
        }
        finally
        {
            if (fs != null)
                fs.Close();
        }

        if (msg != null)
        {
            Debug.LogError(targ.name + ": BakedNavmesh: Load failed: " + msg);
            return false;
        }

        return true;
    }

    private static void SaveMesh(BakedNavmesh targ, string filePath)
    {
        if (filePath.Length == 0 || !targ.HasNavmesh())
            return;

        FileStream fs = null;
        BinaryFormatter formatter = new BinaryFormatter();

        try
        {
            fs = new FileStream(filePath, FileMode.Create);
            formatter.Serialize(fs, targ.GetNavmesh());
        }
        catch (System.Exception ex)
        {
            Debug.LogError(targ.name + ": BakedNavmesh: Save failed: "
                + ex.Message);
        }
        finally
        {
            if (fs != null)
                fs.Close();
        }
    }

    private static void SaveMeshBytes(BakedNavmesh targ, string filePath)
    {
        if (filePath.Length == 0 || !targ.HasNavmesh())
            return;

        FileStream fs = null;
        BinaryFormatter formatter = new BinaryFormatter();

        try
        {
            fs = new FileStream(filePath, FileMode.Create);
            formatter.Serialize(fs, targ.GetNavmesh().GetSerializedMesh());
        }
        catch (System.Exception ex)
        {
            Debug.LogError(targ.name + ": BakedNavmesh: Save bytes failed: "
                + ex.Message);
        }
        finally
        {
            if (fs != null)
                fs.Close();
        }
    }

    private static bool BuildMesh(BakedNavmesh targ)
    {
        string pre = targ.name + ": BakedNavmesh: Bake aborted: ";

        NavmeshTileBuildData buildData = 
            targ.sourceData.GetTileBuildData(false);

        if (buildData == null || buildData.IsDisposed)
        {
            Debug.LogError(pre 
                + " Data source provided null or disposed tile data.");
            return false;
        }

        Navmesh mesh;
        NavStatus status = Navmesh.Build(buildData, out mesh);

        if (NavUtil.Failed(status))
        {
            Debug.LogError(pre + "Status flags: " + status.ToString());
            return false;
        }

        if (!targ.Bake(mesh))
        {
            Debug.LogError(pre + "Could not load mesh."
                + " Internal error?");
            return false;
        }

        return true;
    }

    private static bool IsReadyToBuild(BakedNavmesh targ)
    {
        return (targ.sourceData != null && targ.sourceData.HasTileBuildData);
    }
}
{'fraction_non_alphanumeric': 0.09679073856975381, 'fraction_numerical': 0.0017584994138335288, 'mean_word_length': 1.9108551930049051, 'pattern_counts': {'":': 6, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4553526', 'n_tokens_mistral': 3826, 'n_tokens_neox': 3640, 'n_words': 1033}