Skip to content

Commit c6a3e09

Browse files
authored
Merge pull request #13 from moorestech/feature/revert-code
コード生成をrevertする
2 parents 13e5ec4 + 53c2f46 commit c6a3e09

5 files changed

Lines changed: 38 additions & 116 deletions

File tree

CommandForgeGenerator/CodeGenerate/CodeGenerator.cs

Lines changed: 33 additions & 93 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, property.IsRequired);
104+
var type = GetTypeCode(property.Type);
105105
properties.AppendLine($"public readonly {type} {property.CodeProperty};");
106106
}
107107

@@ -114,85 +114,39 @@ private static string GenerateCreateMethodTempVariables(List<CommandProperty> co
114114
properties.AppendLine();
115115
foreach (var property in commandProperties)
116116
{
117-
var type = GetTypeCode(property.Type, property.IsRequired);
118-
119-
if (property.IsRequired)
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)
120138
{
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-
}
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)
143+
{
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]);");
155146
}
156147
else
157148
{
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-
}
149+
properties.AppendLine($"var {property.CodeProperty} = ({type})json[\"{property.Name}\"];");
196150
}
197151
}
198152

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

221175
}
@@ -235,9 +189,9 @@ private static string GenerateConstructSetPropertiesCode(List<CommandProperty> c
235189
return construct.ToString();
236190
}
237191

238-
private static string GetTypeCode(CommandPropertyType type, bool isRequired)
192+
private static string GetTypeCode(CommandPropertyType type)
239193
{
240-
var baseType = type switch
194+
return type switch
241195
{
242196
CommandPropertyType.String => "string",
243197
CommandPropertyType.Int => "int",
@@ -251,20 +205,6 @@ private static string GetTypeCode(CommandPropertyType type, bool isRequired)
251205
CommandPropertyType.Vector3Int => "global::UnityEngine.Vector3Int",
252206
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
253207
};
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;
268208
}
269209

270210
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.5</Version>
21+
<Version>1.0.6</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: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,7 @@ CommandsSemantics ParseCommandsSchema(JsonObject root)
9090
_ => throw new Exception($"未知の property type \"{typeStr}\"")
9191
};
9292

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));
93+
properties.Add(new CommandProperty(mappedType, propName));
11094
}
11195

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

CommandForgeGenerator/Semantic/CommandsSemantics.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ 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;
3231

3332
public string CodeProperty => Name.ToUpper(0);
3433

35-
public CommandProperty(CommandPropertyType type, string name, bool isRequired)
34+
public CommandProperty(CommandPropertyType type, string name)
3635
{
3736
Type = type;
3837
Name = name;
39-
IsRequired = isRequired;
4038
}
4139

4240
}

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", true),
12-
new(CommandPropertyType.Bool, "isCollapsed", true),
11+
new(CommandPropertyType.String, "groupName"),
12+
new(CommandPropertyType.Bool, "isCollapsed"),
1313
});
1414
var groupEnd = new CommandSemantics("group_end", new List<CommandProperty>());
1515
return new List<CommandSemantics>(){groupStart, groupEnd};

0 commit comments

Comments
 (0)