kiwidog

AWFUL SNDEBUG C# Script

Oct 28th, 2019
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace SnDataSymbols
  7. {
  8.     class Program
  9.     {
  10.         struct SnDebugHeader
  11.         {
  12.             public uint Magic; // SNR2
  13.             public uint Unknown04;
  14.             public uint Unknown08;
  15.             public uint Unknown0C; // This is the same as Unknown04
  16.             public uint SymbolCount; // 7409
  17.             public uint Unknown14; // Zero
  18.             public uint Unknown18; // Zero
  19.             public uint Unknown1C; // Zero
  20.             public uint Unknown20;
  21.             public uint Unknown24; // Zero
  22.             public uint Unknown28; // Zero
  23.             public uint EndOfTable; // Offset to end of table, from this point on is zeros
  24.             public uint Unknown30;
  25.             public uint Unknown34;
  26.             public uint Unknown38;
  27.  
  28.             public SnDebugHeader(BinaryReader p_Reader)
  29.             {
  30.                 Magic = p_Reader.ReadUInt32();
  31.                 Unknown04 = p_Reader.ReadUInt32();
  32.                 Unknown08 = p_Reader.ReadUInt32();
  33.                 Unknown0C = p_Reader.ReadUInt32();
  34.                 SymbolCount = p_Reader.ReadUInt32();
  35.                 Unknown14 = p_Reader.ReadUInt32();
  36.                 Unknown18 = p_Reader.ReadUInt32();
  37.                 Unknown1C = p_Reader.ReadUInt32();
  38.                 Unknown20 = p_Reader.ReadUInt32();
  39.                 Unknown24 = p_Reader.ReadUInt32();
  40.                 Unknown28 = p_Reader.ReadUInt32();
  41.                 EndOfTable = p_Reader.ReadUInt32();
  42.                 Unknown30 = p_Reader.ReadUInt32();
  43.                 Unknown34 = p_Reader.ReadUInt32();
  44.                 Unknown38 = p_Reader.ReadUInt32();
  45.             }
  46.         }
  47.  
  48.         // Starting at 0x245DC
  49.         // Size of the entire table = 15B4C
  50.         // Maybe length of
  51.         struct SnDebugEntry
  52.         {
  53.             // In order to get offset in file, subtract the segment header-> address from this to get offset in section header
  54.             public uint NameAddress;
  55.  
  56.             // Address of the function/data that needs to be set
  57.             public uint TargetAddress;
  58.  
  59.             public uint Unknown08;
  60.  
  61.             public SnDebugEntry(BinaryReader p_Reader)
  62.             {
  63.                 NameAddress = p_Reader.ReadUInt32();
  64.                 TargetAddress = p_Reader.ReadUInt32();
  65.                 Unknown08 = p_Reader.ReadUInt32();
  66.             }
  67.         }
  68.  
  69.         struct SnStringEntry
  70.         {
  71.             // This is *NOT* in the data, we just keep this for lookup later
  72.             public long Offset;
  73.  
  74.             // The actual string we want
  75.             public string StringData;
  76.         }
  77.  
  78.         static string ReadAsciiString(BinaryReader p_Reader)
  79.         {
  80.             var s_Ret = string.Empty;
  81.             char s_Char = '\0';
  82.  
  83.             while ((s_Char = p_Reader.ReadChar()) != '\0')
  84.                 s_Ret += s_Char;
  85.  
  86.             return s_Ret;
  87.         }
  88.  
  89.         public static void Align(BinaryReader p_Reader, int p_Alignment)
  90.         {
  91.             if (p_Reader.BaseStream.Position % p_Alignment == 0)
  92.                 return;
  93.  
  94.             var s_Number = p_Alignment - (p_Reader.BaseStream.Position % p_Alignment);
  95.             p_Reader.ReadBytes((int)s_Number);
  96.         }
  97.  
  98.         static void Main(string[] args)
  99.         {
  100.             Console.WriteLine("Hello World!");
  101.  
  102.             using var s_Reader = new BinaryReader(new FileStream(@"D:\Data\Projects\SnDebug\re4_sndata.bin", FileMode.Open, FileAccess.Read));
  103.  
  104.             var s_SnDebugHeader = new SnDebugHeader(s_Reader);
  105.             if (s_SnDebugHeader.Magic != 844254803)
  106.             {
  107.                 Console.WriteLine("invalid header magic");
  108.                 return;
  109.             }
  110.  
  111.             Console.WriteLine($"Count: {s_SnDebugHeader.SymbolCount} EndOfTabe: {s_SnDebugHeader.EndOfTable}");
  112.  
  113.             s_Reader.BaseStream.Seek(60, SeekOrigin.Begin);
  114.  
  115.             var s_SymbolStringList = new List<SnStringEntry>();
  116.  
  117.             // Read out all of the symbols
  118.             for (var i = 0; i < s_SnDebugHeader.SymbolCount; ++i)
  119.                 s_SymbolStringList.Add(new SnStringEntry
  120.                 {
  121.                     Offset = s_Reader.BaseStream.Position + 0x0035E900, // Hardcode this for now, s_addr of the .sndebug section
  122.                     StringData = ReadAsciiString(s_Reader)
  123.                 });
  124.  
  125.             Align(s_Reader, 4);
  126.  
  127.             var s_Entries = new List<SnDebugEntry>();
  128.  
  129.             for (var i = 0; i < s_SnDebugHeader.SymbolCount; ++i)
  130.                 s_Entries.Add(new SnDebugEntry(s_Reader));
  131.  
  132.             // Print all of the symbol information
  133.             using TextWriter s_Writer = new StreamWriter("script.py");
  134.  
  135.  
  136.  
  137.             for (var i = 0; i < s_SnDebugHeader.SymbolCount; ++i)
  138.             {
  139.                 var s_Entry = s_Entries[i];
  140.  
  141.                 var s_SymbolNames = s_SymbolStringList.Where(p_Entry => p_Entry.Offset == s_Entry.NameAddress);
  142.                 if (s_SymbolNames.Count() > 1)
  143.                     throw new Exception("investigate this");
  144.  
  145.                 //Console.WriteLine($"0x{s_Entry.TargetAddress.ToString("X")} {s_SymbolNames.First().StringData}");
  146.                 // idaapi.set_name(function_address, new_name, idaapi.SN_NOWARN)
  147.                 s_Writer.WriteLine($"idaapi.set_name(0x{s_Entry.TargetAddress.ToString("X")}, \"{s_SymbolNames.First().StringData}\", idaapi.SN_NOWARN)");
  148.             }
  149.  
  150.  
  151.             s_Writer.Close();
  152.  
  153.             Console.WriteLine("Done");
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment