Advertisement
Guest User

map_scenario_scripts.lua

a guest
Mar 9th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.61 KB | None | 0 0
  1. -- Config
  2.  
  3. INDENT = "    "
  4. PRINT_TO_CONSOLE = true -- only prints basic script info
  5. SAVE_DIRECTORY = "sapp\\map scripts" -- this sapp folder is located in the haloded.exe/haloceded.exe directory, not in your documents folder
  6.  
  7. -- End of Config
  8.  
  9. script_types = {
  10.     [0] = "startup",
  11.     [1] = "dormant",
  12.     [2] = "continuous",
  13.     [3] = "static",
  14.     [4] = "stub"
  15. }
  16. return_types = {
  17.     [0] = "unparsed",
  18.     [1] = "\"special form\"",
  19.     [2] = "\"function name\"",
  20.     [3] = "passthrough",
  21.     [4] = "void",
  22.     [5] = "boolean",
  23.     [6] = "real",
  24.     [7] = "short",
  25.     [8] = "long",
  26.     [9] = "string",
  27.     [10] = "script",
  28.     [11] = "trigger_volume",
  29.     [12] = "cutscene_flag",
  30.     [13] = "cutscene_camera_point",
  31.     [14] = "cutscene_title",
  32.     [15] = "cutscene_recording",
  33.     [16] = "device_group",
  34.     [17] = "ai",
  35.     [18] = "ai_command_list",
  36.     [19] = "starting_profile",
  37.     [20] = "conversation",
  38.     [21] = "navpoint",
  39.     [22] = "hud_message",
  40.     [23] = "object_list",
  41.     [24] = "sound",
  42.     [25] = "effect",
  43.     [26] = "damage",
  44.     [27] = "looping_sound",
  45.     [28] = "animation_graph",
  46.     [29] = "actor_variant",
  47.     [30] = "damage_effect",
  48.     [31] = "object_definition",
  49.     [32] = "game_difficulty",
  50.     [33] = "team",
  51.     [34] = "ai_default_state",
  52.     [35] = "actor_type",
  53.     [36] = "hud_corner",
  54.     [37] = "object",
  55.     [38] = "unit",
  56.     [39] = "vehicle",
  57.     [40] = "weapon",
  58.     [41] = "device",
  59.     [42] = "scenery",
  60.     [43] = "object_name",
  61.     [44] = "unit_name",
  62.     [45] = "vehicle_name",
  63.     [46] = "weapon_name",
  64.     [47] = "device_name",
  65.     [48] = "scenery_name"
  66. }
  67. script_syntax_nodes = {}
  68. script_string_offset = nil
  69. api_version = "1.9.0.0"
  70.  
  71. function OnScriptLoad()
  72.     register_callback(cb['EVENT_GAME_START'], "OnGameStart")
  73. end
  74.  
  75. function OnGameStart()
  76.     local map_name = get_var(0,"$map")
  77.    
  78.     local scenario_metaid = read_dword(0x40440004)
  79.     local scenario_tag = lookup_tag(scenario_metaid)
  80.     local scenario_data = read_dword(scenario_tag + 0x14)
  81.    
  82.     local globals_count = read_dword(scenario_data + 0x4A8)
  83.     local scripts_count = read_dword(scenario_data + 0x49C)
  84.     if(globals_count == 0 and scripts_count == 0) then
  85.         if(PRINT_TO_CONSOLE) then
  86.             cprint(string.format("No scenario scripts found in \"%s.map\"", map_name))
  87.         end
  88.         return
  89.     end
  90.    
  91.     local file, error = io.open(SAVE_DIRECTORY .. "\\" .. map_name .. ".txt", "w")
  92.     if(error) then
  93.         os.execute("mkdir \"" .. SAVE_DIRECTORY .. "\"")
  94.         file, error = io.open(SAVE_DIRECTORY .. "\\" .. map_name .. ".txt", "w")
  95.         if(error) then
  96.             if(PRINT_TO_CONSOLE) then
  97.                 cprint(error)
  98.             end
  99.             return
  100.         end
  101.     end
  102.    
  103.     script_string_offset = read_dword(scenario_data + 0x494)
  104.  
  105.     local script_syntax_data = read_dword(scenario_data + 0x480)
  106.     local script_syntax_node_count = read_short(script_syntax_data + 0x2E)
  107.     local script_syntax_node_data = script_syntax_data + 0x38
  108.     for i=0,script_syntax_node_count-1 do
  109.         script_syntax_nodes[i] = {
  110.             ["salt"] = read_short(script_syntax_node_data + i*20),
  111.             ["return_type"] = read_short(script_syntax_node_data + i*20 + 0x4),
  112.             ["expression_type"] = read_short(script_syntax_node_data + i*20 + 0x6),
  113.             ["sibling_id"] = read_int(script_syntax_node_data + i*20 + 0x8),
  114.             ["sibling_index"] = read_short(script_syntax_node_data + i*20 + 0x8),
  115.             ["sibling_salt"] = read_short(script_syntax_node_data + i*20 + 0xA),
  116.             ["string_offset"] = read_dword(script_syntax_node_data + i*20 + 0xC),
  117.             ["real"] = read_float(script_syntax_node_data + i*20 + 0x10),
  118.             ["child_id"] = read_dword(script_syntax_node_data + i*20 + 0x10),
  119.             ["child_index"] = read_short(script_syntax_node_data + i*20 + 0x10),
  120.             ["child_salt"] = read_short(script_syntax_node_data + i*20 + 0x12)
  121.         }
  122.     end
  123.    
  124.     local globals_data = read_dword(scenario_data + 0x4AC)
  125.     for i=0,globals_count-1 do
  126.         local global_name = read_string(globals_data + i*92)
  127.         local global_type = read_byte(globals_data + i*92 + 0x20)
  128.         local global_init_index = read_short(globals_data + i*92 + 0x28)
  129.         local global_data = get_syntax_node_data(global_init_index, 0, true)
  130.         file:write(string.format("(global %s %s %s)", return_types[global_type], global_name, global_data))
  131.         if(i ~= globals_count-1) then
  132.             file:write("\n")
  133.         end
  134.     end
  135.    
  136.     local scripts_data = read_dword(scenario_data + 0x4A0)
  137.     for i=0,scripts_count-1 do
  138.         local script_name = read_string(scripts_data + i*92)
  139.         local script_type = read_byte(scripts_data + i*92 + 0x20)
  140.         local return_type = read_byte(scripts_data + i*92 + 0x22)
  141.         local root_id = read_int(scripts_data + i*92 + 0x24)
  142.         local root_index = read_short(scripts_data + i*92 + 0x24)
  143.         local root_salt = read_short(scripts_data + i*92 + 0x26)
  144.        
  145.         local script_data = get_syntax_node_data(root_index, 1, false)
  146.        
  147.         if(script_type > 2) then -- static and stub
  148.             if(PRINT_TO_CONSOLE) then
  149.                 cprint(string.format("[%s] \"%s\" - script type: %s %s", i, script_name, script_types[script_type], return_types[return_type]))
  150.             end
  151.             file:write(string.format("\n\n(script %s %s %s %s\n)", script_types[script_type], return_types[return_type], script_name, script_data))
  152.         else
  153.             if(PRINT_TO_CONSOLE) then
  154.                 cprint(string.format("[%s] \"%s\" - script type: %s", i, script_name, script_types[script_type]))
  155.             end
  156.             file:write(string.format("\n\n(script %s %s %s\n)", script_types[script_type], script_name, script_data))
  157.         end
  158.     end
  159.    
  160.     script_syntax_nodes = {}
  161.     script_string_offset = nil
  162.     file:close()
  163. end
  164.  
  165. function get_syntax_node_data(Index, Indent, IsGlobal)
  166.     local node = script_syntax_nodes[Index]
  167.    
  168.     local indent = ""
  169.     for i=0,Indent-1 do
  170.         indent = indent .. INDENT
  171.     end
  172.    
  173.     local str = ""
  174.    
  175.     if(node["expression_type"] == 8) then
  176.         if(IsGlobal) then
  177.             str = "(" .. get_syntax_node_data(node["child_index"], Indent, IsGlobal) .. ")"
  178.         else
  179.             str = "\n" .. indent .. "(" .. get_syntax_node_data(node["child_index"], Indent+1, IsGlobal)
  180.            
  181.             local child_expressions = false
  182.             local child_node = script_syntax_nodes[node["child_index"]]
  183.             while(child_node["expression_type"] ~= 8 and child_node["sibling_id"] ~= -1) do
  184.                 local temp_node = script_syntax_nodes[child_node["sibling_index"]]
  185.                 if(temp_node["expression_type"] == 8) then
  186.                     child_expressions = true
  187.                 end
  188.                 child_node = temp_node
  189.             end
  190.            
  191.             if(child_expressions) then
  192.                 str = str .. "\n" .. indent .. ")"
  193.             else
  194.                 str = str .. ")"
  195.             end
  196.         end
  197.     elseif(node["expression_type"] == 9) then
  198.         if(return_types[node["return_type"]] == "boolean") then
  199.             if(node["child_index"] == -256) then
  200.                 str = "false"
  201.             elseif(node["child_index"] == -255) then
  202.                 str = "true"
  203.             end
  204.         elseif(return_types[node["return_type"]] == "real") then
  205.             str = node["real"]
  206.         elseif(return_types[node["return_type"]] == "short") then
  207.             str = node["child_index"]
  208.         elseif(return_types[node["return_type"]] == "long") then
  209.             str = node["child_id"]
  210.         elseif(return_types[node["return_type"]] == "string") then
  211.             str = "\"" .. read_string(script_string_offset + node["string_offset"]) .. "\""
  212.         elseif(return_types[node["return_type"]] ~= "void") then
  213.             local temp_str = read_string(script_string_offset + node["string_offset"])
  214.             if(temp_str:find(" ") or temp_str:len() == 0) then
  215.                 temp_str = "\"" .. temp_str .. "\""
  216.             end
  217.             str = temp_str
  218.         end
  219.     elseif(node["expression_type"] == 10) then
  220.         if(node["child_salt"] ~= 0) then
  221.             str = "\n" .. indent .. "(" .. get_syntax_node_data(node["child_index"], Indent, IsGlobal) .. ")"
  222.         end
  223.     elseif(node["expression_type"] == 13) then
  224.         str = read_string(script_string_offset + node["string_offset"])
  225.     end
  226.    
  227.     if(node["sibling_id"] ~= -1 and node["sibling_salt"] ~= 0) then
  228.         str = str .. " " .. get_syntax_node_data(node["sibling_index"], Indent, IsGlobal)
  229.     end
  230.    
  231.     return(str)
  232. end
  233.  
  234. function OnScriptUnload() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement