33// SPDX-License-Identifier: MIT
44
55using System . Collections . Immutable ;
6+ using System . Runtime . InteropServices . Marshalling ;
67using System . Text ;
8+ using System . Text . Json ;
9+ using System . Text . Json . Serialization . Metadata ;
710using Cesium . Ast ;
811using Cesium . CodeGen ;
912using Cesium . CodeGen . Contexts ;
1013using Cesium . Core ;
1114using Cesium . Parser ;
1215using Cesium . Preprocessor ;
16+ using CommandLine . Text ;
17+ using JetBrains . Annotations ;
1318using Mono . Cecil ;
1419using Yoakke . Streams ;
1520using Yoakke . SynKit . C . Syntax ;
@@ -21,6 +26,69 @@ namespace Cesium.Compiler;
2126
2227internal static class Compilation
2328{
29+ public class CompiledObjectJson
30+ {
31+ public required IEnumerable < string > inputFilePaths ;
32+ public required CompilationOptions compilationOptions ;
33+ }
34+
35+ internal class CompiledObjectJsonTypeInfoResolver : IJsonTypeInfoResolver
36+ {
37+
38+ public JsonTypeInfo ? GetTypeInfo ( Type type , JsonSerializerOptions options )
39+ {
40+ if ( type . Name == typeof ( CompiledObjectJson ) . Name )
41+ {
42+ // return new JsonTypeInfo;
43+ }
44+ throw new NotImplementedException ( ) ;
45+ }
46+ }
47+
48+ public static IJsonTypeInfoResolver GetResolver ( this CompiledObjectJson json )
49+ {
50+ throw new NotImplementedException ( ) ;
51+ }
52+
53+ public static bool IsObjectFileName ( string filename )
54+ {
55+ return filename . EndsWith ( ".json.obj" ) || filename . EndsWith ( ".obj" ) ;
56+ }
57+ public static async Task < int > DumpToObjectJson (
58+ IEnumerable < string > inputFilePaths ,
59+ string outputFilePath ,
60+ CompilationOptions compilationOptions
61+ )
62+ {
63+ CompiledObjectJson compiledObjectJson = new CompiledObjectJson ( )
64+ {
65+ inputFilePaths = inputFilePaths ,
66+ compilationOptions = compilationOptions
67+ } ;
68+
69+ if ( ! outputFilePath . EndsWith ( ".obj" ) )
70+ {
71+ outputFilePath += ".json.obj" ;
72+ }
73+
74+ StreamWriter outObjectWriter = new StreamWriter ( outputFilePath ) ;
75+ await outObjectWriter . WriteAsync ( JsonSerializer . Serialize ( compiledObjectJson ) ) ;
76+ return 0 ;
77+ }
78+
79+ public static async Task < CompiledObjectJson > UndumpObjectJson ( string inputObjectJsonFilePath )
80+ {
81+ StreamReader inObjectJsonReader = new StreamReader ( inputObjectJsonFilePath ) ;
82+
83+ var inObjectJsonStr = await inObjectJsonReader . ReadToEndAsync ( ) ;
84+ var result = JsonSerializer . Deserialize < CompiledObjectJson > ( inObjectJsonStr ) ;
85+ if ( result == null )
86+ {
87+ throw new Exception ( $ "Invalid json from file { inputObjectJsonFilePath } ") ;
88+ }
89+ return result ;
90+ }
91+
2492 public static async Task < int > Compile (
2593 IEnumerable < string > inputFilePaths ,
2694 string outputFilePath ,
@@ -54,7 +122,21 @@ public static async Task<int> Compile(
54122
55123 foreach ( var inputFilePath in inputFilePaths )
56124 {
57- Console . WriteLine ( $ "Processing input file \" { inputFilePath } \" .") ;
125+ bool isObjectFile = IsObjectFileName ( inputFilePath ) ;
126+ Console . WriteLine ( $ "Processing input { ( isObjectFile ? "Cesium JSON object " : "" ) } file \" { inputFilePath } \" .") ;
127+ if ( isObjectFile )
128+ {
129+ var result = UndumpObjectJson ( inputFilePath ) ;
130+ if ( result != null )
131+ {
132+ var newFiles = inputFilePaths . Intersect < string > ( result . Result . inputFilePaths ) ;
133+ foreach ( var newFile in newFiles )
134+ {
135+ Console . WriteLine ( $ "Processing input file (from Cesium JSON object file \" { inputFilePath } \" ) \" { newFile } \" .") ;
136+ await GenerateCode ( assemblyContext , newFile ) ;
137+ }
138+ }
139+ }
58140 await GenerateCode ( assemblyContext , inputFilePath ) ;
59141 }
60142
0 commit comments