You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							163 lines
						
					
					
						
							4.1 KiB
						
					
					
				
			
		
		
	
	
							163 lines
						
					
					
						
							4.1 KiB
						
					
					
				#!/usr/bin/env php
 | 
						|
<?php
 | 
						|
// This script rebuilds the static files enum, extern symbols pointing to the embedded byte buffers,
 | 
						|
// and the look-up structs table. To add more files, simply add them in the 'files' directory.
 | 
						|
 | 
						|
// Note that all files will be accessible by the webserver, unless you filter them in embedded_files.c.
 | 
						|
 | 
						|
 | 
						|
// List all files
 | 
						|
$files = scandir(__DIR__.'/embed');
 | 
						|
 | 
						|
$files = array_filter(array_map(function ($f) {
 | 
						|
  if (!is_file(__DIR__.'/embed/'.$f)) return null;
 | 
						|
  if (preg_match('/^\.|\.kate-swp|\.bak$|~$|\.sh|\.ignore\..*$/', $f)) return null;
 | 
						|
 | 
						|
  echo "Found: $f\n";
 | 
						|
  return $f;
 | 
						|
}, $files));
 | 
						|
 | 
						|
sort($files);
 | 
						|
 | 
						|
$formatted = array_filter(array_map(function ($f) {
 | 
						|
  return "\"files/embed/$f\"";
 | 
						|
}, $files));
 | 
						|
 | 
						|
$cmake = file_get_contents(__DIR__.'/../CMakeLists.txt');
 | 
						|
 | 
						|
$cmake = preg_replace('/#begin staticfiles\n.*#end staticfiles/s', 
 | 
						|
  "#begin staticfiles\n".
 | 
						|
  "# generated by rebuild_file_tables\n".
 | 
						|
  "set(COMPONENT_EMBED_FILES\n        ".
 | 
						|
  implode("\n        ", $formatted) . ")\n".
 | 
						|
  "#end staticfiles", 
 | 
						|
  $cmake);
 | 
						|
 | 
						|
file_put_contents(__DIR__.'/../CMakeLists.txt', $cmake);
 | 
						|
 | 
						|
 | 
						|
// Generate a list of files
 | 
						|
 | 
						|
$num = 0;
 | 
						|
$enum_keys = array_map(function ($f) use(&$num) {
 | 
						|
  $a = preg_replace("/[^A-Z0-9_]+/", "_", strtoupper($f));
 | 
						|
  return 'FILE_'. $a.' = '.($num++);
 | 
						|
}, $files);
 | 
						|
 | 
						|
$keylist = implode(",\n    ", $enum_keys);
 | 
						|
 | 
						|
$struct_array = [];
 | 
						|
 | 
						|
$externs = array_map(function ($f) use (&$struct_array) {
 | 
						|
  $a = preg_replace("/[^A-Z0-9_]+/", "_", strtoupper($f));
 | 
						|
  
 | 
						|
  $start = '_binary_'. strtolower($a).'_start';
 | 
						|
  $end = '_binary_'. strtolower($a).'_end';
 | 
						|
  
 | 
						|
  static $mimes = array(
 | 
						|
    'txt' => 'text/plain',
 | 
						|
    'htm' => 'text/html',
 | 
						|
    'html' => 'text/html',
 | 
						|
    'php' => 'text/html',
 | 
						|
    'css' => 'text/css',
 | 
						|
    'js' => 'application/javascript',
 | 
						|
    'json' => 'application/json',
 | 
						|
    'xml' => 'application/xml',
 | 
						|
    'swf' => 'application/x-shockwave-flash',
 | 
						|
    'flv' => 'video/x-flv',
 | 
						|
    
 | 
						|
    'pem' => 'application/x-pem-file',
 | 
						|
 | 
						|
    // images
 | 
						|
    'png' => 'image/png',
 | 
						|
    'jpe' => 'image/jpeg',
 | 
						|
    'jpeg' => 'image/jpeg',
 | 
						|
    'jpg' => 'image/jpeg',
 | 
						|
    'gif' => 'image/gif',
 | 
						|
    'bmp' => 'image/bmp',
 | 
						|
    'ico' => 'image/vnd.microsoft.icon',
 | 
						|
    'tiff' => 'image/tiff',
 | 
						|
    'tif' => 'image/tiff',
 | 
						|
    'svg' => 'image/svg+xml',
 | 
						|
    'svgz' => 'image/svg+xml',
 | 
						|
 | 
						|
    // archives
 | 
						|
    'zip' => 'application/zip',
 | 
						|
    'rar' => 'application/x-rar-compressed',
 | 
						|
    'exe' => 'application/x-msdownload',
 | 
						|
    'msi' => 'application/x-msdownload',
 | 
						|
    'cab' => 'application/vnd.ms-cab-compressed',
 | 
						|
 | 
						|
    // audio/video
 | 
						|
    'mp3' => 'audio/mpeg',
 | 
						|
    'qt' => 'video/quicktime',
 | 
						|
    'mov' => 'video/quicktime',
 | 
						|
 | 
						|
    // adobe
 | 
						|
    'pdf' => 'application/pdf',
 | 
						|
    'psd' => 'image/vnd.adobe.photoshop',
 | 
						|
    'ai' => 'application/postscript',
 | 
						|
    'eps' => 'application/postscript',
 | 
						|
    'ps' => 'application/postscript',
 | 
						|
 | 
						|
    // ms office
 | 
						|
    'doc' => 'application/msword',
 | 
						|
    'rtf' => 'application/rtf',
 | 
						|
    'xls' => 'application/vnd.ms-excel',
 | 
						|
    'ppt' => 'application/vnd.ms-powerpoint',
 | 
						|
 | 
						|
    // open office
 | 
						|
    'odt' => 'application/vnd.oasis.opendocument.text',
 | 
						|
    'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
 | 
						|
  );
 | 
						|
  
 | 
						|
  $parts = explode('.', $f);
 | 
						|
  $suffix = end($parts);
 | 
						|
  $mime = $mimes[$suffix] ?? 'application/octet-stream';
 | 
						|
  
 | 
						|
  $len = filesize('embed/'.$f);
 | 
						|
  
 | 
						|
  $struct_array[] = "[FILE_$a] = {{$start}, {$end}, \"{$f}\", \"{$mime}\"},";
 | 
						|
  
 | 
						|
  return 
 | 
						|
    'extern const uint8_t '.$start.'[];'."\n".
 | 
						|
    'extern const uint8_t '.$end.'[];';
 | 
						|
}, $files);
 | 
						|
 | 
						|
$externlist = implode("\n", $externs);
 | 
						|
$structlist = implode("\n    ", $struct_array);
 | 
						|
 | 
						|
 | 
						|
file_put_contents('files_enum.h', <<<FILE
 | 
						|
// Generated by 'rebuild_file_tables'
 | 
						|
 | 
						|
#ifndef _EMBEDDED_FILES_ENUM_H
 | 
						|
#define _EMBEDDED_FILES_ENUM_H
 | 
						|
 | 
						|
#include "fileserver/embedded_files.h"
 | 
						|
 | 
						|
enum embedded_file_id {
 | 
						|
    $keylist
 | 
						|
};
 | 
						|
 | 
						|
#endif // _EMBEDDED_FILES_ENUM_H
 | 
						|
 | 
						|
FILE
 | 
						|
);
 | 
						|
 | 
						|
$files_count = count($struct_array);
 | 
						|
file_put_contents("files_enum.c", <<<FILE
 | 
						|
// Generated by 'rebuild_file_tables'
 | 
						|
#include <stdint.h>
 | 
						|
#include "files_enum.h"
 | 
						|
 | 
						|
$externlist
 | 
						|
 | 
						|
const struct embedded_file_info EMBEDDED_FILE_LOOKUP[] = {
 | 
						|
    $structlist
 | 
						|
};
 | 
						|
 | 
						|
const size_t EMBEDDED_FILE_LOOKUP_LEN = $files_count;
 | 
						|
 | 
						|
FILE
 | 
						|
);
 | 
						|
 |