2025-09-25 19:04:05 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Convention;
|
|
|
|
|
using Unity.VisualScripting;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Demo.Editor
|
|
|
|
|
{
|
|
|
|
|
public static class ProjectCreateHelper
|
|
|
|
|
{
|
|
|
|
|
private static void WritePythonStyleFunction(StreamWriter stream, string name, IEnumerable<string> paramList, string description)
|
|
|
|
|
{
|
|
|
|
|
stream.Write($"def {name}({string.Join(',', paramList)}):\n");
|
|
|
|
|
stream.Write($"'''\n{description}\n'''\n...\n\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void WriteCPPClassBase(StreamWriter stream, Type currentType)
|
|
|
|
|
{
|
|
|
|
|
if (currentType == typeof(ScriptableObject))
|
|
|
|
|
return;
|
|
|
|
|
string typeName = currentType.Name;
|
|
|
|
|
if (typeName.Contains('`'))
|
|
|
|
|
{
|
|
|
|
|
typeName = typeName[..typeName.LastIndexOf('`')];
|
|
|
|
|
}
|
|
|
|
|
stream.Write($"#include \"{currentType.BaseType.Name}.helper.h\"\n\n#define {typeName}\n\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void WriteCPPStyleFunction(StreamWriter stream, string name, IEnumerable<string> paramList, string description)
|
|
|
|
|
{
|
|
|
|
|
if (name == nameof(ScriptableObject.LoadSubScript))
|
|
|
|
|
{
|
2025-10-04 23:09:46 +08:00
|
|
|
// stream.WriteLine("#define __build_in_pragma #");
|
|
|
|
|
// stream.WriteLine("#define __build_in_to_text(x) #x");
|
2025-09-25 19:04:05 +08:00
|
|
|
|
2025-10-04 23:09:46 +08:00
|
|
|
// stream.Write("/*\n" + description + "\n*/\n");
|
|
|
|
|
// stream.Write($"#define {name}({string.Join(',', paramList)}) __build_in_pragma include __build_in_to_text(./##{paramList.First()})\n\n");
|
|
|
|
|
stream.Write($"#define {name}({string.Join(',', paramList)})\n\n");
|
2025-09-25 19:04:05 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
stream.Write("/*\n" + description + "\n*/\n");
|
|
|
|
|
stream.Write($"#define {name}({string.Join(',', paramList)}) \n\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void CreateHelperFiles(string dir, ProjectDefaultFileStyle style = ProjectDefaultFileStyle.CPP)
|
|
|
|
|
{
|
|
|
|
|
var toolDir = new ToolFile(dir);
|
|
|
|
|
if (toolDir.IsDir() == false)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Not a directory");
|
|
|
|
|
}
|
|
|
|
|
toolDir.MustExistsPath();
|
|
|
|
|
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
|
|
|
|
|
{
|
|
|
|
|
foreach (var type in asm.GetTypes())
|
|
|
|
|
{
|
|
|
|
|
if (typeof(ScriptableObject).IsAssignableFrom(type) == false)
|
|
|
|
|
continue;
|
|
|
|
|
var scriptCalls = (from info in type.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
|
|
|
|
|
where info is MethodInfo
|
|
|
|
|
where info.GetCustomAttribute<ScriptableCallAttribute>(false) != null
|
|
|
|
|
select info as MethodInfo).ToList();
|
|
|
|
|
var typeName = type.Name;
|
|
|
|
|
if (typeName.Contains('`'))
|
|
|
|
|
{
|
|
|
|
|
typeName = typeName[..typeName.LastIndexOf('`')];
|
|
|
|
|
}
|
|
|
|
|
string fileHeader = $"{typeName}.helper" + style switch
|
|
|
|
|
{
|
|
|
|
|
ProjectDefaultFileStyle.PY => ",py",
|
|
|
|
|
_ => ".h"
|
|
|
|
|
};
|
|
|
|
|
using var fs = File.AppendText((toolDir | fileHeader).GetFullPath());
|
|
|
|
|
switch (style)
|
|
|
|
|
{
|
|
|
|
|
case ProjectDefaultFileStyle.CPP:
|
|
|
|
|
WriteCPPClassBase(fs, type);
|
|
|
|
|
break;
|
|
|
|
|
case ProjectDefaultFileStyle.PY:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
foreach (var methodInfo in scriptCalls)
|
|
|
|
|
{
|
|
|
|
|
var data = methodInfo.GetCustomAttribute<ScriptableCallAttribute>(false);
|
|
|
|
|
switch (style)
|
|
|
|
|
{
|
|
|
|
|
case ProjectDefaultFileStyle.PY:
|
|
|
|
|
WritePythonStyleFunction(fs, methodInfo.Name, from param in methodInfo.GetParameters() select param.Name, data.Description);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
WriteCPPStyleFunction(fs, methodInfo.Name, from param in methodInfo.GetParameters() select param.Name, data.Description);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|