<commit_before>﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

using Duality;

namespace Duality.Editor
{
	public interface IFileImporter
	{
		bool CanImportFile(string srcFile);
		void ImportFile(string srcFile, string targetName, string targetDir);
		string[] GetOutputFiles(string srcFile, string targetName, string targetDir);

		bool IsUsingSrcFile(ContentRef<Resource> r, string srcFile);
		void ReimportFile(ContentRef<Resource> r, string srcFile);
	}

	public static class FileImportProvider
	{
		private static List<IFileImporter> importers = new List<IFileImporter>();
		
		internal static void Init()
		{
			foreach (Type genType in DualityEditorApp.GetAvailDualityEditorTypes(typeof(IFileImporter)))
			{
				if (genType.IsAbstract) continue;
				IFileImporter gen = genType.CreateInstanceOf() as IFileImporter;
				if (gen != null) importers.Add(gen);
			}
		}
		internal static void Terminate()
		{
			importers.Clear();
		}

		public static bool IsImportFileExisting(string filePath)
		{
			string srcFilePath, targetName, targetDir;
			PrepareImportFilePaths(filePath, out srcFilePath, out targetName, out targetDir);

			// Does the source file already exist?
			if (File.Exists(srcFilePath)) return true;

			// Find an importer and check if one of its output files already exist
			IFileImporter importer = importers.FirstOrDefault(i => i.CanImportFile(srcFilePath));
			return importer != null && importer.GetOutputFiles(srcFilePath, targetName, targetDir).Any(File.Exists);
		}
		public static bool ImportFile(string filePath)
		{
			// Determine & check paths
			string srcFilePath, targetName, targetDir;
			PrepareImportFilePaths(filePath, out srcFilePath, out targetName, out targetDir);

			// Find an importer to handle the file import
			IFileImporter importer = importers.FirstOrDefault(i => i.CanImportFile(srcFilePath));
			if (importer != null)
			{
				try
				{
					// Assure the directory exists
					Directory.CreateDirectory(Path.GetDirectoryName(srcFilePath));

					// Move file from data directory to source directory
					if (File.Exists(srcFilePath))
					{
						File.Copy(filePath, srcFilePath, true);
						File.Delete(filePath);
					}
					else
						File.Move(filePath, srcFilePath);
				} catch (Exception) { return false; }

				// Import it
				importer.ImportFile(srcFilePath, targetName, targetDir);
				GC.Collect();
				GC.WaitForPendingFinalizers();<commit_msg>Import improvement<commit_after>﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

using Duality;

namespace Duality.Editor
{
	public interface IFileImporter
	{
		bool CanImportFile(string srcFile);
		void ImportFile(string srcFile, string targetName, string targetDir);
		string[] GetOutputFiles(string srcFile, string targetName, string targetDir);

		bool IsUsingSrcFile(ContentRef<Resource> r, string srcFile);
		void ReimportFile(ContentRef<Resource> r, string srcFile);
	}

	public static class FileImportProvider
	{
		private static List<IFileImporter> importers = new List<IFileImporter>();
		
		internal static void Init()
		{
			foreach (Type genType in DualityEditorApp.GetAvailDualityEditorTypes(typeof(IFileImporter)))
			{
				if (genType.IsAbstract) continue;
				IFileImporter gen = genType.CreateInstanceOf() as IFileImporter;
				if (gen != null) importers.Add(gen);
			}
		}
		internal static void Terminate()
		{
			importers.Clear();
		}

		public static bool IsImportFileExisting(string filePath)
		{
			string srcFilePath, targetName, targetDir;
			PrepareImportFilePaths(filePath, out srcFilePath, out targetName, out targetDir);

			// Does the source file already exist?
			if (File.Exists(srcFilePath)) return true;

			// Find an importer and check if one of its output files already exist
			IFileImporter importer = importers.FirstOrDefault(i => i.CanImportFile(srcFilePath));
			return importer != null && importer.GetOutputFiles(srcFilePath, targetName, targetDir).Any(File.Exists);
		}
		public static bool ImportFile(string filePath)
		{
			// Determine & check paths
			string srcFilePath, targetName, targetDir;
			PrepareImportFilePaths(filePath, out srcFilePath, out targetName, out targetDir);

			// Find an importer to handle the file import
			IFileImporter importer = importers.FirstOrDefault(i => i.CanImportFile(srcFilePath));
			if (importer != null)
			{
				try
				{
					// Assure the directory exists
					Directory.CreateDirectory(Path.GetDirectoryName(srcFilePath));

					// Move file from data directory to source directory
					if (File.Exists(srcFilePath))
					{
						File.Copy(filePath, srcFilePath, true);
						File.Delete(filePath);
					}
					else
						File.Move(filePath, srcFilePath);
				} catch (Exception) { return false; }

				// Import it
				try
				{
					importer.ImportFile(srcFilePath, targetName, targetDir);
				}
				catch (Exception ex)
				{
					Log.Editor.WriteError("A {0} occurred while trying to import file {1}.", ex.GetType().Name, srcFilePath);
                    return false;
				}

				GC.Collect();
				GC.WaitForPendingFinalizers();
{'fraction_non_alphanumeric': 0.09353641866822886, 'fraction_numerical': 0.0003905487209529389, 'mean_word_length': 4.0068426197458455, 'pattern_counts': {'":': 0, '<': 11, '<?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': '6956779', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1473, 'n_words': 407}