Advertisement
Guest User

Explosia Example v1.1 :P

a guest
Jun 2nd, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.67 KB | None | 0 0
  1. // UNTESTED example file by thaCURSEDpie
  2. // 2012-06-02
  3. //
  4. // Should work though, it at least compiles without warnings and errors.
  5.  
  6.  
  7.  
  8. namespace Explosia_Example
  9. {  
  10.     using System;
  11.     using System.Collections.Generic;
  12.     using System.Diagnostics;
  13.     using System.IO;
  14.  
  15.     using GTA;
  16.     using Explosia_DLL;
  17.  
  18.     public class Explosia_Example : Script
  19.     {
  20.         // Current process (== GTAIV.exe, or EFLC.exe in case of EFLC)
  21.         private Process currentProcess;
  22.  
  23.         // Store base address here, because getting the .BaseAddress property is expensive!
  24.         //          (I learned this the hard way)
  25.         private IntPtr baseAddress;
  26.  
  27.         // List of the structures we're going to create
  28.         private List<CEditableStruct> structures;
  29.  
  30.         // Path we use to look for our input files
  31.         string searchStructPath = ".\\explosia\\structureinfo";
  32.  
  33.         // File extension to look for
  34.         string structFileExtension = ".sif";
  35.  
  36.         public Explosia_Example()
  37.         {            
  38.             this.GeneralInfo = "Example for Explosia by thaCURSEDpie";
  39.  
  40.             // Console commands.
  41.             //      Exercise for the reader interested in learning basic C#: add a cmd which rebuilds structures from a specific directory.
  42.             BindConsoleCommand("expl_liststructs", this.listStructs_console, "Lists all currently loaded structures. Usage: expl_liststructs");
  43.             BindConsoleCommand("expl_listparams", this.listParams_console, "Lists a structure's parameters. Usage: expl_listparams <structure name>");
  44.             BindConsoleCommand("expl_setparam", this.setParam_console, "Sets a param value. Usage: expl_setparam <strucure name> <param name> <index to affect> <new value>");
  45.             BindConsoleCommand("expl_getparam", this.setParam_console, "Gets a param value. Usage: expl_getparam <strucure name> <param name> <index to look at>");
  46.  
  47.             //////-------------------
  48.             // Example input could be:
  49.             // > expl_set param CVehicleInfo m_fDriveForce 0 0.1337
  50.             // Which would change the Admiral's (first car in the list, see the list on gtamodding.com) m_fDriveForce to 0.1337
  51.  
  52.             // Init stuff
  53.             this.currentProcess = Process.GetCurrentProcess();
  54.             this.baseAddress = this.currentProcess.MainModule.BaseAddress;
  55.  
  56.             // Build stuff :)
  57.             this.structures = this.buildStructures(this.searchStructPath);
  58.         }
  59.  
  60.  
  61.         // Get a structure by name (just a handy function, nothing fancy)
  62.         private CEditableStruct getStructByName(string name)
  63.         {
  64.             // Just iterate through all the structures, until we find a match.
  65.             for (int i = 0; i < this.structures.Count; i++)
  66.             {
  67.                 if (this.structures[i].Name == name)
  68.                 {
  69.                     return this.structures[i];
  70.                 }
  71.             }
  72.  
  73.             return null;
  74.         }
  75.  
  76.  
  77.         // Console wrapper for the getParamValue function
  78.         private void getParam_console(ParameterCollection p)
  79.         {
  80.             if (p.Count != 3)
  81.             {
  82.                 Game.Console.Print("Usage: expl_getparam <strucure name> <param name> <index to affect>");
  83.                 return;
  84.             }
  85.  
  86.             string value = string.Empty;
  87.             int retVal = this.getParamValue(p.ToString(0), p.ToString(1), p.ToInteger(2), out value);
  88.  
  89.             if (retVal == 0)
  90.             {
  91.                 Game.Console.Print(string.Format("Value:\n{0}", value));
  92.             }
  93.             else
  94.             {
  95.                 Game.Console.Print(string.Format("[EXPLOSIA]: Getting value failed with error code {0}!", retVal));
  96.             }
  97.         }
  98.  
  99.  
  100.         // Console wrapper for the setParamValue function
  101.         private void setParam_console(ParameterCollection p)
  102.         {
  103.             if (p.Count != 4)
  104.             {
  105.                 Game.Console.Print("Usage: expl_setparam <strucure name> <param name> <index to affect> <new value>");
  106.                 return;
  107.             }
  108.  
  109.             this.setParamValue(p.ToString(0), p.ToString(1), p.ToInteger(2), p.ToString(3));
  110.         }
  111.  
  112.  
  113.         // List all created structures
  114.         private void listStructs_console(ParameterCollection p)
  115.         {
  116.             Game.Console.Print("[EXPLOSIA]: Showing loaded structures:");
  117.  
  118.             for (int i = 0; i < this.structures.Count; i++)
  119.             {
  120.                 Game.Console.Print(this.structures[i].Name);
  121.             }
  122.         }
  123.  
  124.  
  125.         // Console wrapper for listParams structure
  126.         private void listParams_console(ParameterCollection p)
  127.         {
  128.             if (p.Count < 1)
  129.             {
  130.                 Game.Console.Print("[EXPLOSIA]: Error! You must specify a structure!");
  131.                 return;
  132.             }
  133.  
  134.             this.listParams(p.ToString(0));
  135.         }
  136.  
  137.  
  138.         // Example how to get values
  139.         private int getParamValue(string structure, string param, int index, out string value)
  140.         {
  141.             // We get the relevant structure
  142.             CEditableStruct s = this.getStructByName(structure);
  143.             value = string.Empty;
  144.  
  145.             // Always check
  146.             if (s == null)
  147.             {
  148.                 Game.Console.Print(string.Format("[EXPLOSIA]: Error! No structure with name {0} found!", structure));
  149.                 return 0xBADBAD;
  150.             }
  151.  
  152.             // Voila, done.
  153.             return s.GetParamValueAsString(this.currentProcess.Handle, this.baseAddress, param, index, out value);
  154.         }
  155.  
  156.  
  157.         // Example how to change values
  158.         private void setParamValue(string structure, string param, int index, string newValue)
  159.         {
  160.             // We get the structure.
  161.             CEditableStruct s = this.getStructByName(structure);
  162.  
  163.             // Always check.
  164.             if (s == null)
  165.             {
  166.                 Game.Console.Print(string.Format("[EXPLOSIA]: Error! No structure with name {0} found!", structure));
  167.                 return;
  168.             }
  169.  
  170.             // Let's do it.
  171.             int retVal = s.SetParamValue(this.currentProcess.Handle, this.baseAddress, param, index, newValue);
  172.  
  173.             // For the sake of inconcistency, lol
  174.             if (retVal == 0)
  175.             {
  176.                 Game.Console.Print(string.Format("[EXPLOSIA]: Param value successfully changed!"));
  177.             }
  178.             else
  179.             {
  180.                 Game.Console.Print(string.Format("[EXPLOSIA]: Setting parameter value failed with error code {0}!", retVal));
  181.             }
  182.         }
  183.  
  184.  
  185.         // List all params in a structure
  186.         private void listParams(string structure)
  187.         {
  188.             // get the structure
  189.             CEditableStruct s = this.getStructByName(structure);
  190.  
  191.             // Always check
  192.             if (s == null)
  193.             {
  194.                 Game.Console.Print(string.Format("[EXPLOSIA]: Error! No structure with name {0} found!", structure));
  195.                 return;
  196.             }
  197.  
  198.             Game.Console.Print(string.Format("Param info for {0}. # params: {1}. Names:", structure, s.NumParams));
  199.  
  200.             SParameter p;
  201.  
  202.             // Just loop through all params
  203.             for (int i = 0; i < s.NumParams; i++)
  204.             {
  205.                 // I don't check if the param != null, because the structures are not being changed after they have been created
  206.                 s.GetParamByIndex(i, out p);
  207.                 Game.Console.Print(s.Name);
  208.             }
  209.         }
  210.  
  211.  
  212.         // Build the structures from the input files.
  213.         private List<CEditableStruct> buildStructures(string searchDir)
  214.         {
  215.             // Set up input-file search.
  216.             DirectoryInfo di = new DirectoryInfo(searchDir);
  217.             FileInfo[] fi = di.GetFiles("*" + this.structFileExtension);
  218.  
  219.             int numStructsLoaded = 0;
  220.  
  221.             List<CEditableStruct> structs = new List<CEditableStruct>();
  222.  
  223.             // We must use the right input file for the right GTA version, otherwise we might be working with wrong offsets.
  224.             string version = this.currentProcess.MainModule.FileVersionInfo.FileVersion;
  225.  
  226.             // Because the FileVersion property gives versions containing spaces and commas instead of just periods.
  227.             version = version.Replace(" ", string.Empty);
  228.             version = version.Replace(",", ".");
  229.  
  230.             // Loop through all input files.
  231.             for (int i = 0; i < fi.Length; i++)
  232.             {
  233.                 // Initialize vars
  234.                 CEditableStruct structure = null;
  235.  
  236.                 // Variable containing data to be used if offset needs to be searched for
  237.                 SArraySearchData sData = new SArraySearchData();
  238.                 int errorLine = 0;
  239.  
  240.                 // We create a structure from an inputfile. Easy as can be :).
  241.                 int retVal = CInputParser.CreateStructure(
  242.                                                             fi[i].FullName,         // Note! FULL path to input file!
  243.                                                             this.currentProcess,
  244.                                                             version,
  245.                                                             out structure,
  246.                                                             ref sData,
  247.                                                             ref errorLine);
  248.                                                            
  249.  
  250.                 // Success
  251.                 if (retVal == 0)
  252.                 {
  253.                     structs.Add(structure);
  254.                     Game.Console.Print("[EXPLOSIA]: Structure info for '" + structure.Name + "' was successfully loaded.");
  255.  
  256.                     numStructsLoaded++;
  257.                 }
  258.                 else if (retVal == 20)
  259.                 {
  260.                     // Version mismatch or no offset specified. We must search for the offset ourselves.
  261.                     Game.Console.Print("[EXPLOSIA]: Searching array address of '" + structure.Name + "'...");
  262.  
  263.                     int addr = 0;
  264.  
  265.                     // Searching is done by calling this function
  266.                     //      (this can take a long while sometimes :(   )
  267.                     retVal = CRemoteHelper.FindArrayAddress(this.currentProcess, sData, out addr);
  268.  
  269.                     // Success
  270.                     if (retVal == 0)
  271.                     {
  272.                         structure.ArrayOffset = addr - (int)this.baseAddress;
  273.                         structs.Add(structure);
  274.  
  275.                         Game.Console.Print("[EXPLOSIA]: Address found!");
  276.  
  277.                         numStructsLoaded++;
  278.                     }
  279.                     else
  280.                     {
  281.                         // Boohoo!
  282.                         Game.Console.Print("[EXPLOSIA]: Address NOT found!");
  283.                     }
  284.  
  285.                 }
  286.                 else if (retVal == 6)
  287.                 {
  288.                     // File is not made for GTAIV.exe (or EFLC.exe in case of EFLC)
  289.                     Game.Console.Print("[EXPLOSIA]: file '" + fi[i].Name + "' is not meant to be used with '" + this.currentProcess.ProcessName + ".exe'. This file is being ignored.");
  290.                 }
  291.                 else
  292.                 {
  293.                     // some other error I don't care to handle here.
  294.                     Game.Console.Print("[EXPLOSIA]: Error loading info from'" + fi[i].Name + "'. Code: " + retVal.ToString() + " at line " + errorLine.ToString() + ".");
  295.                 }
  296.             }
  297.  
  298.             Game.Console.Print(string.Format("finished parsing input files and building structs.\n# Structs loaded: {0}", numStructsLoaded));
  299.  
  300.             return structs;
  301.         }        
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement