void Start() { if (ball == null) { throw new Exception("ball is required"); } if (hook == null) { throw new Exception("hook is required"); } if (camera == null) { throw new Exception("camera is required"); } hookL = hook.transform.GetComponent <HookLogic>(); hook.gameObject.SetActive(false); fishGroups = GameObject.FindObjectsOfType <FishGroup>(); }
public void TryHook(GameKB gameKnowledge) { // Validate all command line options. CheckOptions(); // Test HookRegistry library. ProcessHookRegistry(gameKnowledge); // Copy our injected library to the location of the 'to hook' assemblies. CopyLibraries(gameKnowledge); List <HOOK_ENTRY> hookEntries = ReadHooksFile(_options.HooksFilePath); using (Program.Log.OpenBlock("Parsing libary files")) { // Iterate all libraries known for the provided game. // An assembly blueprint will be created from the yielded filenames. // The blueprints will be edited, saved and eventually replaces the original assembly. foreach (string libraryFilePath in gameKnowledge.LibraryFilePaths) { using (Program.Log.OpenBlock(libraryFilePath)) { if (!File.Exists(libraryFilePath)) { Program.Log.Warn("File does not exist!"); continue; } string libBackupPath = AssemblyHelper.GetPathBackup(libraryFilePath); string libPatchedPath = AssemblyHelper.GetPathOut(libraryFilePath); AssemblyDefinition assembly = null; try { // Load the assembly file assembly = AssemblyHelper.LoadAssembly(libraryFilePath, gameKnowledge.LibraryPath); } catch (BadImageFormatException e) { Program.Log.Warn("Library file is possibly encrypted!"); Program.Log.Info("Library skipped because it cannot be read."); Program.Log.Debug("Full exception: {0}", e.Message); continue; } if (assembly.HasPatchMark()) { Program.Log.Warn(ASSEMBLY_ALREADY_PATCHED); continue; } // Construct a hooker wrapper around the main Module of the assembly. // The wrapper facilitates hooking into method calls. ModuleDefinition mainModule = assembly.MainModule; var wrapper = HookLogic.New(mainModule, _options); // Keep track of hooked methods bool isHooked = false; // Loop each hook entry looking for registered types and methods foreach (HOOK_ENTRY hookEntry in hookEntries) { try { wrapper.AddHookBySuffix(hookEntry.TypeName, hookEntry.MethodName, ExpectedMethods); isHooked = true; } catch (MissingMethodException) { // The method is not found in the current assembly. // This is no error because we run all hook entries against all libraries! } } try { // Only save if the file actually changed! if (isHooked) { // Generate backup from original file try { // This throws if the file already exists. File.Copy(libraryFilePath, libBackupPath, false); } catch (Exception) { // Do nothing } // Save the manipulated assembly. assembly.Save(libPatchedPath); // Overwrite the original with the hooked one File.Copy(libPatchedPath, libraryFilePath, true); } else { Program.Log.Warn(ASSEMBLY_NOT_PATCHED, libraryFilePath); } } catch (IOException e) { // The file could be locked! Notify user. // .. or certain libraries could not be resolved.. // Try to find the path throwing an exception.. but this method is not foolproof! object path = typeof(IOException).GetField("_maybeFullPath", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase)?.GetValue(e); Program.Log.Warn("Could not write patched data to file `{0}`!", path); throw e; } } } } }