@@ -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,82 @@ 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+ properties . AppendLine ( $ "var { property . CodeProperty } Token = json[\" { property . Name } \" ];") ;
160+
161+ if ( property . Type is CommandPropertyType . CommandId )
162+ {
163+ properties . AppendLine ( $ "{ type } { property . CodeProperty } = { property . CodeProperty } Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : (CommandId?)((int){ property . CodeProperty } Token);") ;
164+ }
165+ else if ( property . Type is CommandPropertyType . Vector2 )
166+ {
167+ properties . AppendLine ( $ "{ type } { property . CodeProperty } = { property . CodeProperty } Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector2((float){ property . CodeProperty } Token[0], (float){ property . CodeProperty } Token[1]);") ;
168+ }
169+ else if ( property . Type is CommandPropertyType . Vector3 )
170+ {
171+ properties . AppendLine ( $ "{ type } { property . CodeProperty } = { property . CodeProperty } Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector3((float){ property . CodeProperty } Token[0], (float){ property . CodeProperty } Token[1], (float){ property . CodeProperty } Token[2]);") ;
172+ }
173+ else if ( property . Type is CommandPropertyType . Vector4 )
174+ {
175+ properties . AppendLine ( $ "{ type } { property . CodeProperty } = { property . CodeProperty } Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector4((float){ property . CodeProperty } Token[0], (float){ property . CodeProperty } Token[1], (float){ property . CodeProperty } Token[2], (float){ property . CodeProperty } Token[3]);") ;
176+ }
177+ else if ( property . Type is CommandPropertyType . Vector2Int )
178+ {
179+ properties . AppendLine ( $ "{ type } { property . CodeProperty } = { property . CodeProperty } Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector2Int((int){ property . CodeProperty } Token[0], (int){ property . CodeProperty } Token[1]);") ;
180+ }
181+ else if ( property . Type is CommandPropertyType . Vector3Int )
182+ {
183+ properties . AppendLine ( $ "{ type } { property . CodeProperty } = { property . CodeProperty } Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : new global::UnityEngine.Vector3Int((int){ property . CodeProperty } Token[0], (int){ property . CodeProperty } Token[1], (int){ property . CodeProperty } Token[2]);") ;
184+ }
185+ else if ( property . Type is CommandPropertyType . String )
186+ {
187+ properties . AppendLine ( $ "{ type } { property . CodeProperty } = { property . CodeProperty } Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : (string?){ property . CodeProperty } Token;") ;
188+ }
189+ else
190+ {
191+ properties . AppendLine ( $ "{ type } { property . CodeProperty } = { property . CodeProperty } Token?.Type == global::Newtonsoft.Json.Linq.JTokenType.Null ? null : ({ type } ){ property . CodeProperty } Token;") ;
192+ }
150193 }
151194 }
152195
@@ -169,7 +212,7 @@ public static string GenerateConstructorPropertiesCode(List<CommandProperty> com
169212 var properties = new StringBuilder ( ) ;
170213 foreach ( var property in commandProperties )
171214 {
172- var type = GetTypeCode ( property . Type ) ;
215+ var type = GetTypeCode ( property . Type , property . IsRequired ) ;
173216 properties . Append ( $ ", { type } { property . CodeProperty } ") ;
174217
175218 }
@@ -189,9 +232,9 @@ private static string GenerateConstructSetPropertiesCode(List<CommandProperty> c
189232 return construct . ToString ( ) ;
190233 }
191234
192- private static string GetTypeCode ( CommandPropertyType type )
235+ private static string GetTypeCode ( CommandPropertyType type , bool isRequired )
193236 {
194- return type switch
237+ var baseType = type switch
195238 {
196239 CommandPropertyType . String => "string" ,
197240 CommandPropertyType . Int => "int" ,
@@ -205,6 +248,20 @@ private static string GetTypeCode(CommandPropertyType type)
205248 CommandPropertyType . Vector3Int => "global::UnityEngine.Vector3Int" ,
206249 _ => throw new ArgumentOutOfRangeException ( nameof ( type ) , type , null )
207250 } ;
251+
252+ // 値型でrequiredでない場合は nullable にする
253+ if ( ! isRequired && type != CommandPropertyType . String )
254+ {
255+ return baseType + "?" ;
256+ }
257+
258+ // 参照型でも明示的に nullable にする(C# 8.0以降)
259+ if ( ! isRequired && type == CommandPropertyType . String )
260+ {
261+ return baseType + "?" ;
262+ }
263+
264+ return baseType ;
208265 }
209266
210267 public static string GenerateLoaderCode ( CommandsSemantics commandsSemantics )
0 commit comments