Skip to content

Commit 6c5cd75

Browse files
authored
Merge pull request #14 from moorestech/revert-13-feature/revert-code
Revert "コード生成をrevertする"
2 parents c6a3e09 + 39f304f commit 6c5cd75

File tree

5 files changed

+116
-38
lines changed

5 files changed

+116
-38
lines changed

CommandForgeGenerator/CodeGenerate/CodeGenerator.cs

Lines changed: 93 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static string GeneratePropertiesCode(List<CommandProperty> commandProper
101101
properties.AppendLine();
102102
foreach (var property in commandProperties)
103103
{
104-
var type = GetTypeCode(property.Type);
104+
var type = GetTypeCode(property.Type, property.IsRequired);
105105
properties.AppendLine($"public readonly {type} {property.CodeProperty};");
106106
}
107107

@@ -114,39 +114,85 @@ private static string GenerateCreateMethodTempVariables(List<CommandProperty> co
114114
properties.AppendLine();
115115
foreach (var property in commandProperties)
116116
{
117-
var type = GetTypeCode(property.Type);
118-
if (property.Type is CommandPropertyType.CommandId)
119-
{
120-
properties.AppendLine($"var {property.CodeProperty} = (CommandId)((int)json[\"{property.Name}\"]);");
121-
}
122-
else if (property.Type is CommandPropertyType.Vector2)
123-
{
124-
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
125-
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector2((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1]);");
126-
}
127-
else if (property.Type is CommandPropertyType.Vector3)
128-
{
129-
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
130-
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector3((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1], (float){property.CodeProperty}Array[2]);");
131-
}
132-
else if (property.Type is CommandPropertyType.Vector4)
133-
{
134-
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
135-
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector4((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1], (float){property.CodeProperty}Array[2], (float){property.CodeProperty}Array[3]);");
136-
}
137-
else if (property.Type is CommandPropertyType.Vector2Int)
138-
{
139-
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
140-
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector2Int((int){property.CodeProperty}Array[0], (int){property.CodeProperty}Array[1]);");
141-
}
142-
else if (property.Type is CommandPropertyType.Vector3Int)
117+
var type = GetTypeCode(property.Type, property.IsRequired);
118+
119+
if (property.IsRequired)
143120
{
144-
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
145-
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector3Int((int){property.CodeProperty}Array[0], (int){property.CodeProperty}Array[1], (int){property.CodeProperty}Array[2]);");
121+
// required の場合は従来通り
122+
if (property.Type is CommandPropertyType.CommandId)
123+
{
124+
properties.AppendLine($"var {property.CodeProperty} = (CommandId)((int)json[\"{property.Name}\"]);");
125+
}
126+
else if (property.Type is CommandPropertyType.Vector2)
127+
{
128+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
129+
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector2((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1]);");
130+
}
131+
else if (property.Type is CommandPropertyType.Vector3)
132+
{
133+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
134+
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector3((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1], (float){property.CodeProperty}Array[2]);");
135+
}
136+
else if (property.Type is CommandPropertyType.Vector4)
137+
{
138+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
139+
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector4((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1], (float){property.CodeProperty}Array[2], (float){property.CodeProperty}Array[3]);");
140+
}
141+
else if (property.Type is CommandPropertyType.Vector2Int)
142+
{
143+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
144+
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector2Int((int){property.CodeProperty}Array[0], (int){property.CodeProperty}Array[1]);");
145+
}
146+
else if (property.Type is CommandPropertyType.Vector3Int)
147+
{
148+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
149+
properties.AppendLine($"var {property.CodeProperty} = new global::UnityEngine.Vector3Int((int){property.CodeProperty}Array[0], (int){property.CodeProperty}Array[1], (int){property.CodeProperty}Array[2]);");
150+
}
151+
else
152+
{
153+
properties.AppendLine($"var {property.CodeProperty} = ({type})json[\"{property.Name}\"];");
154+
}
146155
}
147156
else
148157
{
149-
properties.AppendLine($"var {property.CodeProperty} = ({type})json[\"{property.Name}\"];");
158+
// nullable の場合はnullチェックを追加
159+
if (property.Type is CommandPropertyType.String)
160+
{
161+
properties.AppendLine($"var {property.CodeProperty} = json[\"{property.Name}\"] == null ? null : (string)json[\"{property.Name}\"];");
162+
}
163+
else if (property.Type is CommandPropertyType.CommandId)
164+
{
165+
properties.AppendLine($"var {property.CodeProperty} = json[\"{property.Name}\"] == null ? null : (CommandId?)((int)json[\"{property.Name}\"]);");
166+
}
167+
else if (property.Type is CommandPropertyType.Vector2)
168+
{
169+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
170+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector2((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1]);");
171+
}
172+
else if (property.Type is CommandPropertyType.Vector3)
173+
{
174+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
175+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector3((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1], (float){property.CodeProperty}Array[2]);");
176+
}
177+
else if (property.Type is CommandPropertyType.Vector4)
178+
{
179+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
180+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector4((float){property.CodeProperty}Array[0], (float){property.CodeProperty}Array[1], (float){property.CodeProperty}Array[2], (float){property.CodeProperty}Array[3]);");
181+
}
182+
else if (property.Type is CommandPropertyType.Vector2Int)
183+
{
184+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
185+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector2Int((int){property.CodeProperty}Array[0], (int){property.CodeProperty}Array[1]);");
186+
}
187+
else if (property.Type is CommandPropertyType.Vector3Int)
188+
{
189+
properties.AppendLine($"var {property.CodeProperty}Array = json[\"{property.Name}\"];");
190+
properties.AppendLine($"var {property.CodeProperty} = {property.CodeProperty}Array == null ? null : new global::UnityEngine.Vector3Int((int){property.CodeProperty}Array[0], (int){property.CodeProperty}Array[1], (int){property.CodeProperty}Array[2]);");
191+
}
192+
else
193+
{
194+
properties.AppendLine($"var {property.CodeProperty} = json[\"{property.Name}\"] == null ? null : ({type})json[\"{property.Name}\"];");
195+
}
150196
}
151197
}
152198

@@ -169,7 +215,7 @@ public static string GenerateConstructorPropertiesCode(List<CommandProperty> com
169215
var properties = new StringBuilder();
170216
foreach (var property in commandProperties)
171217
{
172-
var type = GetTypeCode(property.Type);
218+
var type = GetTypeCode(property.Type, property.IsRequired);
173219
properties.Append($", {type} {property.CodeProperty}");
174220

175221
}
@@ -189,9 +235,9 @@ private static string GenerateConstructSetPropertiesCode(List<CommandProperty> c
189235
return construct.ToString();
190236
}
191237

192-
private static string GetTypeCode(CommandPropertyType type)
238+
private static string GetTypeCode(CommandPropertyType type, bool isRequired)
193239
{
194-
return type switch
240+
var baseType = type switch
195241
{
196242
CommandPropertyType.String => "string",
197243
CommandPropertyType.Int => "int",
@@ -205,6 +251,20 @@ private static string GetTypeCode(CommandPropertyType type)
205251
CommandPropertyType.Vector3Int => "global::UnityEngine.Vector3Int",
206252
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
207253
};
254+
255+
// 値型でrequiredでない場合は nullable にする
256+
if (!isRequired && type != CommandPropertyType.String)
257+
{
258+
return baseType + "?";
259+
}
260+
261+
// 参照型でも明示的に nullable にする(C# 8.0以降)
262+
if (!isRequired && type == CommandPropertyType.String)
263+
{
264+
return baseType + "?";
265+
}
266+
267+
return baseType;
208268
}
209269

210270
public static string GenerateLoaderCode(CommandsSemantics commandsSemantics)

CommandForgeGenerator/CommandForgeGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<!-- NuGet Package Metadata -->
1919
<Title>CommandForge Generator</Title>
2020
<Authors>Moorestech</Authors>
21-
<Version>1.0.6</Version>
21+
<Version>1.0.5</Version>
2222
<Description>CommandForgeEditorのcommands.yamlのC#コードを生成するSourceGenerator</Description>
2323
<PackageProjectUrl>https://github.com/moorestech/CommandForgeGenerator</PackageProjectUrl>
2424
<RepositoryUrl>https://github.com/moorestech/CommandForgeGenerator</RepositoryUrl>

CommandForgeGenerator/Semantic/CommandSemanticsLoader.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,23 @@ CommandsSemantics ParseCommandsSchema(JsonObject root)
9090
_ => throw new Exception($"未知の property type \"{typeStr}\"")
9191
};
9292

93-
properties.Add(new CommandProperty(mappedType, propName));
93+
// required フィールドを読み取る(デフォルトはfalse)
94+
var isRequired = false;
95+
if (propObj.Nodes.ContainsKey("required"))
96+
{
97+
var requiredVal = propObj["required"];
98+
if (requiredVal is JsonBoolean requiredBool)
99+
{
100+
isRequired = requiredBool.Literal;
101+
}
102+
else if (requiredVal is JsonString requiredStr)
103+
{
104+
// YAMLが文字列としてbooleanを返す場合がある
105+
isRequired = requiredStr.Literal.ToLowerInvariant() == "true";
106+
}
107+
}
108+
109+
properties.Add(new CommandProperty(mappedType, propName, isRequired));
94110
}
95111

96112
commands.Add(new CommandSemantics(commandName, properties));

CommandForgeGenerator/Semantic/CommandsSemantics.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ public CommandSemantics(string name, List<CommandProperty> properties)
2828
public class CommandProperty{
2929
public readonly string Name;
3030
public readonly CommandPropertyType Type;
31+
public readonly bool IsRequired;
3132

3233
public string CodeProperty => Name.ToUpper(0);
3334

34-
public CommandProperty(CommandPropertyType type, string name)
35+
public CommandProperty(CommandPropertyType type, string name, bool isRequired)
3536
{
3637
Type = type;
3738
Name = name;
39+
IsRequired = isRequired;
3840
}
3941

4042
}

CommandForgeGenerator/Semantic/ReservedCommandSemantics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public static List<CommandSemantics> GetReservedCommand()
88
{
99
var groupStart = new CommandSemantics("group_start", new List<CommandProperty>()
1010
{
11-
new(CommandPropertyType.String, "groupName"),
12-
new(CommandPropertyType.Bool, "isCollapsed"),
11+
new(CommandPropertyType.String, "groupName", true),
12+
new(CommandPropertyType.Bool, "isCollapsed", true),
1313
});
1414
var groupEnd = new CommandSemantics("group_end", new List<CommandProperty>());
1515
return new List<CommandSemantics>(){groupStart, groupEnd};

0 commit comments

Comments
 (0)