Advertisement
liquidspark

mod_halo_filetypes.c (in progress)

Oct 15th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.52 KB | None | 0 0
  1. /* Include the required headers from httpd */
  2. #include "httpd.h"
  3. #include "http_core.h"
  4. #include "http_protocol.h"
  5. #include "http_request.h"
  6.  
  7. #include "apr_strings.h"
  8. #include "apr_network_io.h"
  9. #include "apr_md5.h"
  10. #include "apr_sha1.h"
  11. #include "apr_hash.h"
  12. #include "apr_base64.h"
  13. #include "apr_dbd.h"
  14. #include <apr_file_info.h>
  15. #include <apr_file_io.h>
  16. #include <apr_tables.h>
  17. #include "util_script.h"
  18.  
  19. // byte-swapping functions
  20. // special thanks to: http://stackoverflow.com/a/2637138
  21.  
  22. #include <stdint.h>
  23.  
  24. /* Define prototypes of our functions in this module */
  25. static void register_hooks(apr_pool_t *pool);
  26. static int halo_filetypes_handler(request_rec *r);
  27. uint16_t swap_uint16(uint16_t val);
  28. int16_t swap_int16(int16_t val);
  29. uint32_t swap_uint32(uint32_t val);
  30. int32_t swap_int32(int32_t val);
  31. int64_t swap_int64(int64_t val);
  32. uint64_t swap_uint64(uint64_t val);
  33.  
  34. //! Byte swap unsigned short
  35. uint16_t swap_uint16( uint16_t val )
  36. {
  37.     return (val << 8) | (val >> 8 );
  38. }
  39.  
  40. //! Byte swap short
  41. int16_t swap_int16( int16_t val )
  42. {
  43.     return (val << 8) | ((val >> 8) & 0xFF);
  44. }
  45.  
  46. //! Byte swap unsigned int
  47. uint32_t swap_uint32( uint32_t val )
  48. {
  49.     val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
  50.     return (val << 16) | (val >> 16);
  51. }
  52.  
  53. //! Byte swap int
  54. int32_t swap_int32( int32_t val )
  55. {
  56.     val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );
  57.     return (val << 16) | ((val >> 16) & 0xFFFF);
  58. }
  59.  
  60. int64_t swap_int64( int64_t val )
  61. {
  62.     val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
  63.     val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
  64.     return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);
  65. }
  66.  
  67. uint64_t swap_uint64( uint64_t val )
  68. {
  69.     val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
  70.     val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
  71.     return (val << 32) | (val >> 32);
  72. }
  73.  
  74.  
  75. // file format structures
  76.  
  77. typedef struct {
  78.     // 0, 36
  79.     char *tagtype;              // actr
  80.     uint32_t random_number1;
  81.     uint32_t header_size;       // 64
  82.     // 48, 8
  83.     uint16_t tag_version;       // 2
  84.     uint16_t engine_version;    // 255
  85.     char *engine;               // blam
  86. }header_actr;
  87.  
  88. typedef struct {
  89.     uint32_t flags_bf;
  90.     uint32_t more_flags_bf;
  91.     // 72, 12
  92.     uint16_t actor_type_list;
  93.     // 86, 2
  94.     float perception_maximum_vision_distance;
  95.     float perception_central_vision_angle_rad;
  96.     float perception_maximum_vision_angle_rad;
  97.     // 100, 4
  98.     float perception_peripheral_vision_angle_rad;
  99.     float perception_peripheral_distance;
  100.     // 112, 4
  101.     float perception_standing_gun_offset_i;
  102.     float perception_standing_gun_offset_j;
  103.     float perception_standing_gun_offset_k;
  104.     float perception_crouching_gun_offset_i;
  105.     float perception_crouching_gun_offset_j;
  106.     float perception_crouching_gun_offset_k;
  107.     float perception_hearing_distance;
  108.     float perception_notice_projectile_chance;
  109.     float perception_notice_vehicle_chance;
  110.     // 152, 8
  111.     float perception_combat_perception_time;
  112.     float perception_guard_perception_time;
  113.     float perception_non_combat_perception_time;
  114.     // 172 -- 3 calculated float values, 0 until compiled, compiled defaults:
  115.     float unknown_1;    // 0.0555556
  116.     float unknown_2;    // 0.0238095
  117.     float unknown_3;    // 0.0166667 or 0
  118.     // 184, 8
  119.     float movement_dive_into_cover_chance;
  120.     float movement_emerge_from_cover_chance;
  121.     float movement_dive_from_grenade_chance;
  122.     float movement_path_finding_radius;
  123.     float movement_glass_ignorance_chance;
  124.     float movement_stationary_movement_distance;
  125.     float movement_free_flying_sidestep;
  126.     float movement_begin_moving_angle_rad;
  127.     // 224, 4
  128.     float looking_maximum_aiming_deviation_yaw_rad;
  129.     float looking_maximum_aiming_deviation_pitch_rad;
  130.     float looking_maximum_looking_deviation_yaw_rad;
  131.     float looking_maximum_looking_deviation_pitch_rad;
  132.     float looking_non_combat_look_delta_left_rad;
  133.     float looking_non_combat_look_delta_right_rad;
  134.     float looking_combat_look_delta_left_rad;
  135.     float looking_combat_look_delta_right_rad;
  136.     float looking_idle_aiming_range_yaw_rad;
  137.     float looking_idle_aiming_range_pitch_rad;
  138.     float looking_idle_looking_range_yaw_rad;
  139.     float looking_idle_looking_range_pitch_rad;
  140.     float looking_event_look_time_modifier_from;
  141.     float looking_event_look_time_modifier_to;
  142.     float looking_non_combat_idle_facing_from;
  143.     float looking_non_combat_idle_facing_to;
  144.     float looking_non_combat_idle_aiming_from;
  145.     float looking_non_combat_idle_aiming_to;
  146.     float looking_non_combat_idle_looking_from;
  147.     float looking_non_combat_idle_looking_to;
  148.     float looking_guard_idle_facing_from;
  149.     float looking_guard_idle_facing_to;
  150.     float looking_guard_idle_aiming_from;
  151.     float looking_guard_idle_aiming_to;
  152.     float looking_guard_idle_looking_from;
  153.     float looking_guard_idle_looking_to;
  154.     float looking_combat_idle_facing_from;
  155.     float looking_combat_idle_facing_to;
  156.     float looking_combat_idle_aiming_from;
  157.     float looking_combat_idle_aiming_to;
  158.     float looking_combat_idle_looking_from;
  159.     float looking_combat_idle_looking_to;
  160.     // 356, 8
  161.     // calculated values, 0 until compiled: 3 unknown values of the same data (0xb33bbd2e) followed by 1 float value of 0.5
  162.     uint32_t unknown_4;
  163.     uint32_t unknown_5;
  164.     uint32_t unknown_6;
  165.     float unknown_7;
  166.    
  167.     // dependency 1: looking -- DO NOT USE -- weap
  168.     char *d1_tagclass;
  169.     uint32_t d1_tagnamepointer;
  170.     uint32_t d1_tagnamestringlength;
  171.     uint32_t d1_tagid;
  172.    
  173.     // 396, 268
  174.    
  175.     // dependency 2: looking -- DO NOT USE -- proj
  176.     char *d2_tagclass;
  177.     uint32_t d2_tagnamepointer;
  178.     uint32_t d2_tagnamestringlength;
  179.     uint32_t d2_tagid;
  180.    
  181.     uint16_t unopposable_unreachable_danger_trigger_list;
  182.     uint16_t unopposable_vehicle_danger_trigger_list;
  183.     uint16_t unopposable_player_danger_trigger_list;
  184.     // 686, 2
  185.     float unopposable_danger_trigger_time_from;
  186.     float unopposable_danger_trigger_time_to;
  187.     uint16_t unopposable_friends_killed_trigger;
  188.     uint16_t unopposable_friends_retreating_trigger;
  189.     // 700, 12
  190.     float unopposable_retreat_time_from;
  191.     float unopposable_retreat_time_to;
  192.     // 720, 8
  193.     float panic_cowering_time_from;
  194.     float panic_cowering_time_to;
  195.     float panic_friend_killed_panic_chance;
  196.     uint16_t panic_leader_type_list;
  197.     // 742, 2
  198.     float panic_leader_killed_panic_chance;
  199.     float panic_panic_damage_threshold;
  200.     float panic_surprise_distance;
  201.     // 756, 28
  202.     float defensive_hide_behind_cover_time_from;
  203.     float defensive_hide_behind_cover_time_to;
  204.     float defensive_hide_target_not_visible_time;
  205.     float defensive_hide_shield_fraction;
  206.     float defensive_attack_shield_fraction;
  207.     float defensive_pursue_shield_fraction;
  208.     // 808, 16
  209.     uint16_t defensive_defensive_crouch_type_list;
  210.     // 826, 2
  211.     float defensive_attacking_crouch_threshold;
  212.     float defensive_defending_crouch_threshold;
  213.     float defensive_minimum_stand_time;
  214.     float defensive_minimum_crouch_time;
  215.     float defensive_defending_hide_time_modifier;
  216.     float defensive_attacking_evasion_threshold;
  217.     float defensive_defending_evasion_threshold;
  218.     float defensive_evasion_seek_cover_chance;
  219.     float defensive_evasion_delay_time;
  220.     float defensive_maximum_seek_cover_distance;
  221.     float defensive_cover_damage_threshold;
  222.     float defensive_stalking_discovery_time;
  223.     float defensive_stalking_maximum_distance;
  224.     float defensive_stationary_facing_angle_rad;
  225.     float defensive_change_facing_stand_time;
  226.     // 888, 4
  227.     float pursuit_uncover_delay_time_from;
  228.     float pursuit_uncover_delay_time_to;
  229.     float pursuit_target_search_time_from;
  230.     float pursuit_target_search_time_to;
  231.     float pursuit_pursuit_position_time_from;
  232.     float pursuit_pursuit_position_time_to;
  233.     uint16_t pursuit_num_positions_coord;
  234.     uint16_t pursuit_num_positions_normal;
  235.     // 920, 32
  236.     float berserk_melee_attack_delay;
  237.     float berserk_melee_fudge_factor;
  238.     float berserk_melee_charge_time;
  239.     float berserk_melee_leap_range_from;
  240.     float berserk_melee_leap_range_to;
  241.     float berserk_melee_leap_velocity;
  242.     float berserk_melee_leap_chance;
  243.     float berserk_melee_leap_ballistic;
  244.     float berserk_berserk_damage_amount;
  245.     float berserk_berserk_damage_threshold;
  246.     float berserk_berserk_proximity;
  247.     float berserk_suicide_sensing_distance;
  248.     float berserk_berserk_grenade_chance;
  249.     // 1004, 12
  250.     float firing_positions_guard_position_time_from;
  251.     float firing_positions_guard_position_time_to;
  252.     float firing_positions_combat_position_time_from;
  253.     float firing_positions_combat_position_time_to;
  254.     float firing_positions_old_position_avoid_distance;
  255.     float firing_positions_friend_avoid_distance;
  256.     // 1040, 40
  257.     float communication_non_combat_idle_speech_time_from;
  258.     float communication_non_combat_idle_speech_time_to;
  259.     float communication_combat_idle_speech_time_from;
  260.     float communication_combat_idle_speech_time_to;
  261.     // 1096, 176
  262.    
  263.     // dependency 3: communication -- DO NOT USE -- actr
  264.     char *d3_tagclass;
  265.     uint32_t d3_tagnamepointer;
  266.     uint32_t d3_tagnamestringlength;
  267.     uint32_t d3_tagid;
  268.    
  269.     // 1288, 48
  270.    
  271.     // 1336, tpns 1-3
  272.    
  273. } meta_actr;
  274.  
  275. /* Define our module as an entity and assign a function for registering hooks  */
  276.  
  277. module AP_MODULE_DECLARE_DATA   halo_filetypes_module =
  278. {
  279.     STANDARD20_MODULE_STUFF,
  280.     NULL,            // Per-directory configuration handler
  281.     NULL,            // Merge handler for per-directory configurations
  282.     NULL,            // Per-server configuration handler
  283.     NULL,            // Merge handler for per-server configurations
  284.     NULL,            // Any directives we may have for httpd
  285.     register_hooks   // Our hook registering function
  286. };
  287.  
  288.  
  289. /* register_hooks: Adds a hook to the httpd process */
  290. static void register_hooks(apr_pool_t *pool)
  291. {
  292.    
  293.     /* Hook the request handler */
  294.     ap_hook_handler(halo_filetypes_handler, NULL, NULL, APR_HOOK_LAST);
  295. }
  296.  
  297. /* The handler function for our module.
  298.  * This is where all the fun happens!
  299.  */
  300.  
  301. static int halo_filetypes_handler(request_rec *r)
  302. {
  303.     int rcheck, file_exists_nodir;
  304.     apr_finfo_t finfo;
  305.     apr_file_t* file;
  306.     apr_status_t error;
  307.     char *filename_localname;
  308.     uint32_t filesize;
  309.     apr_size_t readBytes;
  310.    
  311.    
  312.     /* First off, we need to check if this is a call for the "example" handler.
  313.      * If it is, we accept it and do our things, it not, we simply return DECLINED,
  314.      * and Apache will try somewhere else.
  315.      */
  316.     if (!r->handler || strcmp(r->handler, "halo-filetypes-handler")) return (DECLINED);
  317.    
  318.     // check to see if file exists, it isn't a directory, and we have permissions to read it
  319.    
  320.     rcheck = apr_stat(&finfo, r->filename, APR_FINFO_MIN, r->pool);
  321.     if (rcheck == APR_SUCCESS) {
  322.         file_exists_nodir =
  323.         (
  324.             (finfo.filetype != APR_NOFILE)
  325.         &&  !(finfo.filetype & APR_DIR)
  326.         );
  327.         if (!file_exists_nodir) return HTTP_NOT_FOUND; // Return a 404 if not found.
  328.     }
  329.     // If apr_stat failed, we're probably not allowed to check this file.
  330.     //else return HTTP_FORBIDDEN;
  331.     else return HTTP_NOT_FOUND;
  332.    
  333.     filename_localname = strstr(r->filename, "tags/");
  334.    
  335.     // The first thing we will do is write a simple "Hello, world!" back to the client.
  336.     ap_set_content_type(r, "text/html");
  337.    
  338.     char *html_header = apr_pstrcat(r->pool, \
  339.     "\
  340. <html>\n\
  341.     <head>\n\
  342.         <title>GXV INCY | ", filename_localname, "</title>\n\
  343.         <link rel=\"stylesheet\" href=\"http://files.galaxyverge.com/styles/incy/main.css\">\n\
  344.     </head>\n\
  345.     <body>\n\
  346.         ", NULL);
  347.        
  348.     char *html_body = "";
  349.    
  350.     char *html_footer = apr_pstrcat(r->pool, \
  351.     "\
  352.     </body>\n\
  353. </html>", NULL);
  354.    
  355.    
  356.     //ap_rprintf(r, "The requested filename is:<br>%s", r->filename); // full path, e.g. "/home/vm_shared/http/incy/tags/sparky/sound/charity_ambience.sound"
  357.    
  358.     // check for valid file extension present and parse accordingly
  359.    
  360.     //if (strstr(r->filename,".actor_variant") != NULL)
  361.     if (strcmp(".actor" , strstr(r->filename,".")) == 0)
  362.     {
  363.         // actor tag type
  364.        
  365.         header_actr *head = apr_pcalloc(r->pool, sizeof(header_actr));
  366.         meta_actr *meta = apr_pcalloc(r->pool, sizeof(meta_actr));
  367.        
  368.         // open the file
  369.         error = apr_file_open(&file, r->filename, APR_READ, APR_OS_DEFAULT, r->pool);
  370.         // check for errors with the file opening attempt
  371.         if (error == 0)
  372.         {
  373.             //ap_rprintf(r, "[successfully opened the tag file]");
  374.         }
  375.         else if (error == 404)
  376.         {
  377.             return HTTP_NOT_FOUND;
  378.         }
  379.         else
  380.         {
  381.             return error;
  382.         }
  383.        
  384.         // get the file size
  385.        
  386.         //apr_stat(&finfo, r->filename, APR_FINFO_SIZE, r->pool);
  387.         //ap_rprintf(r, "[The file is %u bytes.]", (uint32_t)finfo.size);   // works fine
  388.        
  389.         // copy the file into a memory buffer
  390.         readBytes = finfo.size;
  391.         char memorybuffer[(uint32_t)readBytes];// = apr_pcalloc(r->pool, (uint32_t)finfo.size);
  392.         apr_file_read(file, memorybuffer, &readBytes);
  393.        
  394.         // parse the memory buffer
  395.        
  396.         //ap_rprintf(r, "Memory buffer is %u bytes.", (uint32_t)sizeof(memorybuffer));  // works fine
  397.        
  398.         head->tagtype = apr_pstrndup(r->pool, &memorybuffer[36], 4);
  399.         head->random_number1 = swap_uint32(*(uint32_t*)(void*)&memorybuffer[40]);   // special thanks to nil
  400.        
  401.         // output the information
  402.         html_body = apr_pstrcat(r->pool, \
  403.         "<p>The <a href=\"http://halo.galaxyverge.com/index/FileFormats/Actor\">Actor</a> tag type stores information about basic character behavior for artificial intelligence. It is partnered with <a href=\"http://halo.galaxyverge.com/index/FileFormats/ActorVariant\">Actor Variant</a> which specifies behavior variations in greater detail.</p>\n\
  404.         <div id=\"tag_header\">\n\
  405.             <span class=\"section_name\">Information</span><br>\n\
  406.             <span class=\"section_body\">\n\
  407.                 <span class=\"entry_name\">File Size on Server</span>\n             <span class=\"entry_meta\">", apr_psprintf(r->pool, "%u", (uint32_t)finfo.size), " bytes</span>\n\
  408.                 <span class=\"entry_name\">Tag Class</span>\n               <span class=\"entry_meta\">", apr_psprintf(r->pool, "%s", head->tagtype), "</span>\n\
  409.                 <span class=\"entry_name\">Tag Random ID</span>\n               <span class=\"entry_meta\">", apr_psprintf(r->pool, "%#X", head->random_number1), "</span>\n\
  410.             </span>\n\
  411.         </div>\n", NULL);
  412.         //ap_rprintf(r, "");
  413.     }
  414.    
  415.     else if (strcmp(".actor_variant" , strstr(r->filename,".")) == 0)
  416.     {
  417.         // actor variant tag type
  418.         ap_rprintf(r, "This is an <strong>actor variant</strong> tag type.");
  419.     }
  420.    
  421.     ap_rprintf(r, "%s%s%s", html_header, html_body, html_footer);
  422.    
  423.     return OK;
  424. }
  425.  
  426. /* Commands for incremental builds:
  427. cd /home/programs/apache_modules
  428. apxs -i -c -n mod_halo_filetypes mod_halo_filetypes.c
  429. service httpd restart
  430. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement