using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Needle.Engine.Utils;
using Newtonsoft.Json;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
using Task = System.Threading.Tasks.Task;
namespace Needle.Engine.ProjectBundle
{
internal static class ProjectWindowActions
{
private const string BaseMenuPath = "Assets/Create/";
private const int BaseMenuPriority = Engine.Constants.MenuItemOrder;
private class CreateTypescriptAction : EndNameEditAction
{
public string Directory;
public string Template;
///
/// optional codegen dir
///
public string CodeGenDirectory;
///
/// optional path to npmdef
///
public string NpmDefPath;
public override async void Action(int instanceId, string pathName, string resourceFile)
{
var fileName = Path.GetFileNameWithoutExtension(pathName);
SanitizeName(ref fileName);
var targetPath = Directory + "/" + fileName + ".ts";
if (File.Exists(targetPath))
{
Debug.LogWarning("Script already exists at " + targetPath);
// fileName = ObjectNames.GetUniqueName(new[] { fileName }, fileName);
// SantizeName(ref fileName);
// targetPath = Directory + "/" + fileName + ".ts";
}
else
{
var code = Template.Replace("component_name", fileName);
File.WriteAllText(targetPath, code);
if (!string.IsNullOrWhiteSpace(CodeGenDirectory))
System.IO.Directory.CreateDirectory(CodeGenDirectory);
}
// TODO: select the new typescript sub asset
if (!string.IsNullOrEmpty(NpmDefPath) && File.Exists(NpmDefPath))
{
// AssetDatabase.AssetPathToGUID(NpmDefPath)
}
var workspaceDirectory = Directory;
if (!Actions.OpenWorkspace(workspaceDirectory, targetPath))
{
Debug.LogWarning("Failed opening workspace: " + Directory);
}
await Task.Delay(100);
EditorUtility.OpenWithDefaultApp(targetPath);
BundleRegistry.Instance.RunCodeGen(targetPath);
var fp = Path.GetFullPath(Directory);
var bundle = BundleRegistry.Instance.Bundles.FirstOrDefault(b => b.PackageDirectory == fp);
if (bundle != null)
{
BundleImporter.MarkDirty(bundle);
}
}
private static void SanitizeName(ref string str)
{
str = Regex.Replace(str, "[\\W]", "");
}
}
[MenuItem(BaseMenuPath + "Typescript", true, BaseMenuPriority + 1)]
private static bool CreateTypescript_Validate()
{
var obj = Selection.activeObject;
var selectionPath = AssetDatabase.GetAssetPath(obj);
if (selectionPath.EndsWith(Constants.Extension) || selectionPath.EndsWith(".codegen")) return true;
var exportInfo = ExportInfo.Get();
if (exportInfo && exportInfo.Exists()) return true;
return false;
}
[MenuItem(BaseMenuPath + "Typescript", false, BaseMenuPriority + 1)]
private static void CreateTypescript()
{
var obj = Selection.activeObject;
var selectionPath = AssetDatabase.GetAssetPath(obj);
var path = AssetDatabase.GUIDToAssetPath("921c8f326fb84c89b6cb2cee80d99e31");
var template = File.ReadAllText(path);
var creator = ScriptableObject.CreateInstance();
creator.Template = template;
if (selectionPath.EndsWith(Constants.Extension) || selectionPath.EndsWith(".codegen"))
{
var projectPath = selectionPath.Substring(0, selectionPath.LastIndexOf(".", StringComparison.Ordinal)) + "~";
if (Directory.Exists(projectPath))
{
creator.NpmDefPath = selectionPath.Substring(0, selectionPath.LastIndexOf(".", StringComparison.Ordinal)) + Constants.Extension;
creator.Directory = projectPath;
creator.CodeGenDirectory = selectionPath.Substring(0, selectionPath.LastIndexOf(".", StringComparison.Ordinal)) + ".codegen";
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, creator, "New Typescript", null, null);
}
}
else
{
var exportInfo = ExportInfo.Get();
var dir = Path.GetFullPath(exportInfo.GetProjectDirectory() + "/src/scripts");
if (Directory.Exists(dir))
{
creator.Directory = dir;
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, creator, "New Typescript", null, null);
}
}
}
[MenuItem(BaseMenuPath + "NPM Definition", false, BaseMenuPriority + 2)]
private static void CreateBundleContextMenu()
{
var obj = Selection.activeObject;
if (obj)
{
CreateBundle(true);
}
}
[MenuItem(BaseMenuPath + "NPM Definition (Link to external package)", false, BaseMenuPriority + 2)]
private static void CreateLinkedBundleContextMenu()
{
var obj = Selection.activeObject;
if (obj)
{
CreateBundle(false);
}
}
private class CreateAssetAction : EndNameEditAction
{
///
/// Set to false to show dialogue to link to external package
///
public bool Embedded = true;
public override void Action(int instanceId, string pathName, string _)
{
var bundleFilePath = pathName + Constants.Extension;
var fileName = Path.GetFileNameWithoutExtension(bundleFilePath);
var bundle = new Bundle();
bundle.FilePath = bundleFilePath;
var json = JsonConvert.SerializeObject(bundle, Formatting.Indented);
File.WriteAllText(bundleFilePath, json);
AssetDatabase.ImportAsset(bundleFilePath, ImportAssetOptions.ForceSynchronousImport);
var res = AssetDatabase.LoadAssetAtPath