@@ -101,7 +101,7 @@ private static string GeneratePropertiesCode(List<CommandProperty> commandProper
101
101
properties . AppendLine ( ) ;
102
102
foreach ( var property in commandProperties )
103
103
{
104
- var type = GetTypeCode ( property . Type ) ;
104
+ var type = GetTypeCode ( property . Type , property . IsRequired ) ;
105
105
properties . AppendLine ( $ "public readonly { type } { property . CodeProperty } ;") ;
106
106
}
107
107
@@ -114,39 +114,85 @@ private static string GenerateCreateMethodTempVariables(List<CommandProperty> co
114
114
properties . AppendLine ( ) ;
115
115
foreach ( var property in commandProperties )
116
116
{
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 )
143
120
{
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
+ }
146
155
}
147
156
else
148
157
{
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
+ }
150
196
}
151
197
}
152
198
@@ -169,7 +215,7 @@ public static string GenerateConstructorPropertiesCode(List<CommandProperty> com
169
215
var properties = new StringBuilder ( ) ;
170
216
foreach ( var property in commandProperties )
171
217
{
172
- var type = GetTypeCode ( property . Type ) ;
218
+ var type = GetTypeCode ( property . Type , property . IsRequired ) ;
173
219
properties . Append ( $ ", { type } { property . CodeProperty } ") ;
174
220
175
221
}
@@ -189,9 +235,9 @@ private static string GenerateConstructSetPropertiesCode(List<CommandProperty> c
189
235
return construct . ToString ( ) ;
190
236
}
191
237
192
- private static string GetTypeCode ( CommandPropertyType type )
238
+ private static string GetTypeCode ( CommandPropertyType type , bool isRequired )
193
239
{
194
- return type switch
240
+ var baseType = type switch
195
241
{
196
242
CommandPropertyType . String => "string" ,
197
243
CommandPropertyType . Int => "int" ,
@@ -205,6 +251,20 @@ private static string GetTypeCode(CommandPropertyType type)
205
251
CommandPropertyType . Vector3Int => "global::UnityEngine.Vector3Int" ,
206
252
_ => throw new ArgumentOutOfRangeException ( nameof ( type ) , type , null )
207
253
} ;
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 ;
208
268
}
209
269
210
270
public static string GenerateLoaderCode ( CommandsSemantics commandsSemantics )
0 commit comments