Allow returning null when loading game
[gamestick-pjgsapi.git] / bin / generate-apps-cache.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * Generate cache file with information about apps
5  *
6  * Needed by:
7  * - www/api/rest/connect/stick/stick/generate.php
8  */
9 require_once __DIR__ . '/functions.php';
10 require_once __DIR__ . '/../config.php';
11
12 $cacheDir = dirname(__FILE__, 2) . '/cache/';
13 if (!is_dir($cacheDir)) {
14     if (!mkdir($cacheDir)) {
15         fwrite(STDERR, "Failed to create cache dir: $cacheDir\n");
16         exit(10);
17     }
18 }
19
20 if (!isset($argv[1])) {
21     fwrite(STDERR, "Pass the path to a \"folders\" file with game data json files folder names\n");
22     exit(1);
23 }
24
25 $foldersFile = $argv[1];
26 if (!is_file($foldersFile)) {
27     fwrite(STDERR, 'Given path is not a file: ' . $foldersFile . "\n");
28     exit(1);
29 }
30
31 $baseDir   = dirname($foldersFile);
32 $gameFiles = [];
33 foreach (file($foldersFile) as $line) {
34     $line = trim($line);
35     if (strlen($line)) {
36         if (strpos($line, '..') !== false) {
37             fwrite(STDERR, 'Path attack in ' . $folder . "\n");
38         }
39         $folder = $baseDir . '/' . $line;
40         if (!is_dir($folder)) {
41             fwrite(STDERR, 'Folder does not exist: ' . $folder . "\n");
42         }
43         $gameFiles = array_merge($gameFiles, glob($folder . '/*.json'));
44     }
45 }
46
47 $appsCacheFile         = $cacheDir . 'connect-apps.min.json';
48 $featuredAgesCacheFile = $cacheDir . 'connect-featured-ages.min.json';
49
50
51 $games = loadGames($gameFiles);
52 loadConfigFeaturedFile();
53 loadConfigPopularTxtFile();
54 //make it "package name => number"
55 array_unshift($GLOBALS['popular'], 'dummy');
56 $GLOBALS['popular'] = array_flip($GLOBALS['popular']);
57 unset($GLOBALS['popular']['dummy']);
58
59
60 $connectGames = [];
61 foreach ($games as $gameData) {
62     $connectGame = convertGameDataForConnect($gameData, $GLOBALS['popular']);
63     if ($connectGame !== null) {
64         $connectGames[] = $connectGame;
65     }
66 }
67 file_put_contents(
68     $appsCacheFile,
69     json_encode($connectGames, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
70 );
71
72
73 $featuredMenu = buildFeaturedMenu($GLOBALS['featured'], $games);
74 file_put_contents(
75     $featuredAgesCacheFile,
76     json_encode($featuredMenu, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
77 );
78 ?>