<commit_before>﻿#if UNITY_EDITOR
using System.Linq;
using Neodroid.Runtime;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Windows;

namespace Neodroid.Editor.Utilities {
  /// <inheritdoc />
  /// <summary>
  ///   Scene preview.
  ///   https://diegogiacomelli.com.br/unity3d-scenepreview-inspector/
  /// </summary>
  [CustomEditor(typeof(SceneAsset))]
  [CanEditMultipleObjects]
  public class ScenePreview : UnityEditor.Editor {
    const float _editor_margin = 50;
    const float _preview_margin = 5;

    /// <summary>
    /// </summary>
    [RuntimeInitializeOnLoadMethod]
    public void CaptureScreenShot() {
      if (NeodroidInfo.GenerateScenePreviews){
        var preview_path = GetPreviewPath(SceneManager.GetActiveScene().name);
        //Debug.LogFormat("Saving scene preview at {0}", preview_path);
        ScreenCapture.CaptureScreenshot(preview_path);
      }
    }

    /// <inheritdoc />
    /// <summary>
    /// </summary>
    public override void OnInspectorGUI()
    {
      if (NeodroidInfo.GenerateScenePreviews){
        //AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
        var scene_names = this.targets.Select(t => ((SceneAsset) t).name).OrderBy(n => n).ToArray();

        var previews_count = scene_names.Length;
        var preview_width = Screen.width;
        var preview_height = (Screen.height - _editor_margin * 2 - _preview_margin * previews_count)
                             / previews_count;

        for (var i = 0; i < scene_names.Length; i++){
          DrawPreview(i, scene_names[i], preview_width, preview_height);
        }
      }
    }

    void DrawPreview(int index, string scene_name, float width, float height) {
      var preview_path = GetPreviewPath(scene_name);
      //var ob = Resources.Load(scene_name);
      //var preview = ob as RenderTexture;
      var preview = LoadPng(preview_path);

      if (preview == null) {
        EditorGUILayout.HelpBox(
            $"There is no image preview for scene {scene_name} at {preview_path}. Please play the scene on editor and image preview will be captured automatically or create the missing path: {_preview_folders}.",
            MessageType.Info);
      } else {
        GUI.DrawTexture(
            new Rect(index, _editor_margin + index * (height + _preview_margin), width, height),
            preview,
            ScaleMode.ScaleToFit);
      }
    }

    string GetPreviewPath(string scene_name) {
      return $"{Application.dataPath}/{NeodroidInfo.ScenePreviewsLocation}{scene_name}.png";
    }

    /// <summary><commit_msg>Refactor<commit_after>﻿#if UNITY_EDITOR
using System.Linq;
using Neodroid.Runtime;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Windows;

namespace Neodroid.Editor.Utilities {
  /// <inheritdoc />
  /// <summary>
  ///   Scene preview.
  ///   https://diegogiacomelli.com.br/unity3d-scenepreview-inspector/
  /// </summary>
  [CustomEditor(typeof(SceneAsset))]
  [CanEditMultipleObjects]
  public class ScenePreview : UnityEditor.Editor {
    const float _editor_margin = 50;
    const float _preview_margin = 5;

    /// <summary>
    /// </summary>
    [RuntimeInitializeOnLoadMethod]
    public void CaptureScreenShot() {
      if (NeodroidInfo.GenerateScenePreviews){
        var preview_path = GetPreviewPath(SceneManager.GetActiveScene().name);
        //Debug.LogFormat("Saving scene preview at {0}", preview_path);
        ScreenCapture.CaptureScreenshot(preview_path);
      }
    }

    /// <inheritdoc />
    /// <summary>
    /// </summary>
    public override void OnInspectorGUI()
    {
      if (NeodroidInfo.GenerateScenePreviews){
        //AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
        var scene_names = this.targets.Select(t => ((SceneAsset) t).name).OrderBy(n => n).ToArray();

        var previews_count = scene_names.Length;
        var preview_width = Screen.width;
        var preview_height = (Screen.height - _editor_margin * 2 - _preview_margin * previews_count)
                             / previews_count;

        for (var i = 0; i < scene_names.Length; i++){
          DrawPreview(i, scene_names[i], preview_width, preview_height);
        }
      }
    }

    void DrawPreview(int index, string scene_name, float width, float height) {
      var preview_path = GetPreviewPath(scene_name);
      //var ob = Resources.Load(scene_name);
      //var preview = ob as RenderTexture;
      var preview = LoadPng(preview_path);

      if (preview == null) {
        EditorGUILayout.HelpBox(
            $"There is no image preview for scene {scene_name} at {preview_path}. Please play the scene on editor and image preview will be captured automatically or create the missing path: {NeodroidInfo.ScenePreviewsLocation}.",
            MessageType.Info);
      } else {
        GUI.DrawTexture(
            new Rect(index, _editor_margin + index * (height + _preview_margin), width, height),
            preview,
            ScaleMode.ScaleToFit);
      }
    }

    string GetPreviewPath(string scene_name) {
      return $"{Application.dataPath}/{NeodroidInfo.ScenePreviewsLocation}{scene_name}.png";
    }

    /// <summary>
{'fraction_non_alphanumeric': 0.11217641418983701, 'fraction_numerical': 0.0026845637583892616, 'mean_word_length': 2.801749271137026, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 2, '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': '1663259', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1498, 'n_words': 339}