Skip to content

Commit e9f9e19

Browse files
committed
いろいろ
1 parent 9521480 commit e9f9e19

19 files changed

Lines changed: 800 additions & 52 deletions
Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,77 @@
11
using System.IO;
22
using System.Text;
3-
using CommandForgeGenerator.Generator;
4-
using CommandForgeGenerator.Generator.Semantic;
53
using UnityEditor;
64
using UnityEngine;
75

8-
public class CommandTemplateGenerator : EditorWindow
9-
{
10-
[SerializeField] private string commandsYamlPath = string.Empty;
11-
[SerializeField] private string outputDirectory = string.Empty;
126

13-
[MenuItem("Windows/CommandTemplateGenerator")]
14-
private static void ShowWindow()
7+
namespace CommandForgeGeneratorUtil
8+
{
9+
public class CommandTemplateGenerator : EditorWindow
1510
{
16-
var window = GetWindow<CommandTemplateGenerator>();
17-
window.titleContent = new GUIContent("CommandTemplateGenerator");
18-
window.Show();
19-
}
11+
[SerializeField] private string commandsYamlPath = string.Empty;
12+
[SerializeField] private string outputDirectory = string.Empty;
2013

21-
private void OnGUI()
22-
{
23-
EditorGUILayout.LabelField("Commands.yaml", EditorStyles.boldLabel);
24-
commandsYamlPath = EditorGUILayout.TextField("Path", commandsYamlPath);
14+
[MenuItem("Window/CommandTemplateGenerator")]
15+
private static void ShowWindow()
16+
{
17+
var window = GetWindow<CommandTemplateGenerator>();
18+
window.titleContent = new GUIContent("CommandTemplateGenerator");
19+
window.Show();
20+
}
2521

26-
EditorGUILayout.Space();
22+
private void OnGUI()
23+
{
24+
EditorGUILayout.LabelField("Commands.yaml", EditorStyles.boldLabel);
25+
commandsYamlPath = EditorGUILayout.TextField("Path", commandsYamlPath);
2726

28-
EditorGUILayout.LabelField("Output Directory", EditorStyles.boldLabel);
29-
outputDirectory = EditorGUILayout.TextField("Path", outputDirectory);
27+
EditorGUILayout.Space();
3028

31-
EditorGUILayout.Space();
29+
EditorGUILayout.LabelField("Output Directory", EditorStyles.boldLabel);
30+
outputDirectory = EditorGUILayout.TextField("Path", outputDirectory);
3231

33-
if (GUILayout.Button("Generate"))
34-
{
35-
Generate();
36-
}
37-
}
32+
EditorGUILayout.Space();
3833

39-
private void Generate()
40-
{
41-
if (!File.Exists(commandsYamlPath))
42-
{
43-
Debug.LogError($"commands.yaml not found: {commandsYamlPath}");
44-
return;
34+
if (GUILayout.Button("Generate"))
35+
{
36+
Generate();
37+
}
4538
}
4639

47-
if (!Directory.Exists(outputDirectory))
40+
private void Generate()
4841
{
49-
Directory.CreateDirectory(outputDirectory);
50-
}
42+
if (!File.Exists(commandsYamlPath))
43+
{
44+
Debug.LogError($"commands.yaml not found: {commandsYamlPath}");
45+
return;
46+
}
5147

52-
var yamlText = File.ReadAllText(commandsYamlPath);
53-
var semantics = CommandSemanticsLoader.GetCommandSemantics(yamlText);
48+
if (!Directory.Exists(outputDirectory))
49+
{
50+
Directory.CreateDirectory(outputDirectory);
51+
}
5452

55-
foreach (var command in semantics.Commands)
56-
{
57-
var className = command.ClassName;
58-
var code = $$"""
59-
namespace CommandForgeGenerator.Command
60-
{
61-
public partial class {{className}} : ICommandForgeCommand
62-
{
63-
}
64-
}
65-
""";
53+
var yamlText = File.ReadAllText(commandsYamlPath);
54+
var semantics = CommandSemanticsLoader.GetCommandSemantics(yamlText);
55+
56+
foreach (var command in semantics.Commands)
57+
{
58+
var className = command.ClassName;
59+
var sb = new StringBuilder()
60+
.AppendLine("namespace CommandForgeGenerator.Command")
61+
.AppendLine("{")
62+
.AppendLine($" public partial class {className}")
63+
.AppendLine(" {")
64+
.AppendLine(" }")
65+
.AppendLine("}");
66+
var code = sb.ToString();
6667

67-
var filePath = Path.Combine(outputDirectory, className + ".cs");
68-
File.WriteAllText(filePath, code, Encoding.UTF8);
69-
}
7068

71-
AssetDatabase.Refresh();
72-
Debug.Log("Command templates generated.");
69+
var filePath = Path.Combine(outputDirectory, className + ".cs");
70+
File.WriteAllText(filePath, code, Encoding.UTF8);
71+
}
72+
73+
AssetDatabase.Refresh();
74+
Debug.Log("Command templates generated.");
75+
}
7376
}
7477
}

UnityEditorExtension/CommandForgeGeneratorUtil/Assets/Scripts/Editor/Generator.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Unity.VisualScripting.YamlDotNet.Serialization;
5+
6+
namespace CommandForgeGeneratorUtil
7+
{
8+
9+
10+
public class CommandSemanticsLoader
11+
{
12+
public static CommandsSemantics GetCommandSemantics(string yamlText)
13+
{
14+
var rootNode = GetRootJsonObject();
15+
var semantics = ParseCommandsSchema(rootNode);
16+
var reservedCommands = ReservedCommandSemantics.GetReservedCommand();
17+
18+
semantics.Commands.AddRange(reservedCommands);
19+
20+
return semantics;
21+
22+
23+
#region Internal
24+
25+
JsonObject GetRootJsonObject()
26+
{
27+
try
28+
{
29+
var jsonText = Yaml.ToJson(yamlText);
30+
return JsonParser.Parse(JsonTokenizer.GetTokens(jsonText)) as JsonObject;
31+
}
32+
catch (Exception e)
33+
{
34+
throw new Exception("yamlファイルの形式が正しくありません。" + e.Message + " " + e.StackTrace);
35+
}
36+
}
37+
38+
CommandsSemantics ParseCommandsSchema(JsonObject root)
39+
{
40+
var commandsJson = root["commands"] as JsonArray ?? throw new Exception("commands 配列が見つかりません。");
41+
42+
var commands = new List<CommandSemantics>();
43+
44+
foreach (var commandsJsonNode in commandsJson.Nodes)
45+
{
46+
var commandsJsonObject = commandsJsonNode as JsonObject ?? throw new Exception("command 要素がオブジェクトではありません。");
47+
48+
var idNode = commandsJsonObject["id"] as JsonString ?? throw new Exception("command.id が見つかりません。");
49+
var commandName = idNode.Literal;
50+
51+
// --- properties を走査して CommandProperty のリストを作成 ---
52+
var propsJsonObj = commandsJsonObject["properties"] as JsonObject
53+
?? throw new Exception($"command \"{commandName}\" に properties がありません。");
54+
55+
var properties = new List<CommandProperty>();
56+
57+
foreach (var nodeKeyValue in propsJsonObj.Nodes)
58+
{
59+
var propName = nodeKeyValue.Key;
60+
var propNode = nodeKeyValue.Value;
61+
62+
if (propNode is not JsonObject propObj) throw new Exception($"property \"{propName}\" がオブジェクトではありません。");
63+
64+
// type 文字列を取得
65+
var typeNode = propObj["type"] as JsonString ?? throw new Exception($"property \"{propName}\" に type がありません。");
66+
67+
var typeStr = typeNode.Literal.ToLowerInvariant();
68+
var mappedType = typeStr switch
69+
{
70+
"string" => CommandPropertyType.String,
71+
"integer" => CommandPropertyType.Int,
72+
"number" => CommandPropertyType.Float,
73+
"boolean" => CommandPropertyType.Bool,
74+
"enum" => CommandPropertyType.String,
75+
"command" => CommandPropertyType.CommandId,
76+
"vector2" => CommandPropertyType.Vector2,
77+
"vector3" => CommandPropertyType.Vector3,
78+
"vector4" => CommandPropertyType.Vector4,
79+
"vector2int" => CommandPropertyType.Vector2Int,
80+
"vector3int" => CommandPropertyType.Vector3Int,
81+
_ => throw new Exception($"未知の property type \"{typeStr}\"")
82+
};
83+
84+
properties.Add(new CommandProperty(mappedType, propName));
85+
}
86+
87+
commands.Add(new CommandSemantics(commandName, properties));
88+
}
89+
90+
return new CommandsSemantics(commands);
91+
}
92+
93+
#endregion
94+
}
95+
}
96+
}

UnityEditorExtension/CommandForgeGeneratorUtil/Assets/Scripts/Editor/Generator/CommandSemanticsLoader.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Collections.Generic;
2+
using CommandForgeGeneratorUtil;
3+
4+
namespace CommandForgeGeneratorUtil
5+
{
6+
public class CommandsSemantics
7+
{
8+
public readonly List<CommandSemantics> Commands;
9+
10+
public CommandsSemantics(List<CommandSemantics> commands)
11+
{
12+
Commands = commands;
13+
}
14+
}
15+
16+
public class CommandSemantics
17+
{
18+
public readonly string Name;
19+
public string ClassName => Name.CommandNameToClassName();
20+
public readonly List<CommandProperty> Properties;
21+
22+
public CommandSemantics(string name, List<CommandProperty> properties)
23+
{
24+
Name = name;
25+
Properties = properties;
26+
}
27+
}
28+
29+
public class CommandProperty
30+
{
31+
public readonly string Name;
32+
public readonly CommandPropertyType Type;
33+
34+
public string CodeProperty => Name.ToUpper(0);
35+
36+
public CommandProperty(CommandPropertyType type, string name)
37+
{
38+
Type = type;
39+
Name = name;
40+
}
41+
}
42+
43+
public enum CommandPropertyType
44+
{
45+
String,
46+
Int,
47+
Float,
48+
Bool,
49+
CommandId,
50+
Vector2,
51+
Vector3,
52+
Vector4,
53+
Vector2Int,
54+
Vector3Int,
55+
}
56+
}

UnityEditorExtension/CommandForgeGeneratorUtil/Assets/Scripts/Editor/Generator/CommandsSemantics.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace CommandForgeGeneratorUtil
2+
{
3+
public static class StringExtension
4+
{
5+
public static string Indent(this string code, bool firstLine = false, int level = 1)
6+
{
7+
var indent = new string(' ', 4 * level);
8+
return firstLine ? indent : "" + code.Replace("\n", $"\n{indent}");
9+
}
10+
11+
public static string CommandNameToClassName(this string commandName)
12+
{
13+
return commandName.ToUpper(0) + "Command";
14+
}
15+
16+
/// <summary>
17+
/// 指定した n 番目の文字を大文字に変換します。
18+
/// </summary>
19+
public static string ToUpper(this string self, int no = 0)
20+
{
21+
if (no > self.Length)
22+
{
23+
return self;
24+
}
25+
26+
var _array = self.ToCharArray();
27+
var up = char.ToUpper(_array[no]);
28+
_array[no] = up;
29+
return new string(_array);
30+
}
31+
}
32+
}

UnityEditorExtension/CommandForgeGeneratorUtil/Assets/Scripts/Editor/Generator/StringExtension.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityEditorExtension/CommandForgeGeneratorUtil/Assets/Scripts/Editor/Json.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)