4848import com .google .gson .GsonBuilder ;
4949import com .google .gson .JsonArray ;
5050import com .google .gson .JsonElement ;
51+ import com .google .gson .JsonIOException ;
5152import com .google .gson .JsonObject ;
5253import com .google .gson .JsonParser ;
5354import com .google .gson .JsonPrimitive ;
55+ import com .google .gson .JsonSyntaxException ;
5456
5557import io .undertow .Handlers ;
5658import io .undertow .Undertow ;
@@ -235,14 +237,14 @@ private static void generateAssetsFile(Path tmpdir, String assetsPath) {
235237 List <String > content = Files .readAllLines (f );
236238 String newContent = "" ;
237239 Pattern regex = Pattern .compile ("<page.*file=\\ \" ([^\\ \" ]+)\\ \" .*\\ />" ); // looking for the font resources
238- for (String line : content ) {
240+ for (String line : content ) {
239241 Matcher ressourcesMatcher = regex .matcher (line );
240242 String newLine = "" ;
241243 int startCpy = 0 ;
242- while (ressourcesMatcher .find ()) {
244+ while (ressourcesMatcher .find ()) {
243245 int startMatch = ressourcesMatcher .start (1 );
244246 int endMatch = ressourcesMatcher .end (1 );
245- newLine = newLine .concat (line .substring (startCpy ,startMatch ));
247+ newLine = newLine .concat (line .substring (startCpy , startMatch ));
246248 Path assetPath = f .getParent ().resolve (ressourcesMatcher .group (1 ));
247249 newLine = newLine .concat (hashAsset (assetPath ));
248250 startCpy = endMatch ;
@@ -255,7 +257,8 @@ private static void generateAssetsFile(Path tmpdir, String assetsPath) {
255257 Files .write (
256258 tmpdir .resolve ("hashed_assets" ).resolve (newName ),
257259 newContent .getBytes (),
258- StandardOpenOption .CREATE );
260+ StandardOpenOption .CREATE
261+ );
259262 } else {
260263 fonts .add (
261264 tmpdir .relativize (f ).toString ().replace ("\\ " , "/" )
@@ -295,9 +298,11 @@ private static void generateAssetsFile(Path tmpdir, String assetsPath) {
295298 private static boolean isSpriteJson (Path f ) {
296299 return "json" .equals (FilenameUtils .getExtension (f .toString ()));
297300 }
301+
298302 private static boolean isFont (Path f ) {
299303 return "fnt" .equals (FilenameUtils .getExtension (f .toString ()));
300304 }
305+
301306 public static List <Path > generateView (String jsonResult , String assetsPath ) {
302307 List <Path > paths ;
303308
@@ -664,11 +669,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
664669
665670 try (PrintWriter out = new PrintWriter (demoFile )) {
666671 out .println ("export const demo = " );
667-
668- List <String > lines = Files .readAllLines (gameFile .toPath ());
669- for (String line : lines ) {
670- out .println (line );
671- }
672+ out .print (extractDemoFromGameJson (gameFile ).toString ());
672673 out .println (";" );
673674 }
674675
@@ -683,6 +684,22 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
683684 }
684685 }
685686
687+ private JsonObject extractDemoFromGameJson (File gameFile ) {
688+ JsonObject result = new JsonObject ();
689+ JsonParser parser = new JsonParser ();
690+ try {
691+ JsonElement obj = parser .parse (new FileReader (gameFile ));
692+ JsonElement views = obj .getAsJsonObject ().get ("views" );
693+ JsonElement agents = obj .getAsJsonObject ().get ("agents" );
694+ result .add ("views" , views );
695+ result .add ("agents" , agents );
696+
697+ } catch (IllegalStateException | JsonIOException | JsonSyntaxException | FileNotFoundException e ) {
698+ e .printStackTrace ();
699+ }
700+ return result ;
701+ }
702+
686703 private void sendException (HttpServerExchange exchange , Exception e , int statusCode ) {
687704 exchange .getResponseHeaders ().put (Headers .CONTENT_TYPE , "text/plain" );
688705 exchange .setStatusCode (statusCode );
0 commit comments