internal void LoadSkills(Player player) { if (skillTypes.Count == 0) { return; } Logger.Log($"Loading {skillTypes.Count} skill type(s) for player {player.playerID + 1}\n"); foreach (Type skillType in skillTypes) { Logger.Log($"Loading skill {skillType.FullName} for player {player.playerID + 1}"); try { Player.SkillState skill = (Player.SkillState)Activator.CreateInstance(skillType, player.fsm, player); Sprite sprite = LoadSkillSprite(skill); ModManager.AddModSkill(skill, sprite); player.fsm.AddState(skill); } catch (Exception ex) { Externs.MessageBox("Error!", ex.ToString()); } } Logger.Log(""); }
public static ReturnData ConvertStringToClassAndMethod(string str, AssemblyDefinition assembly, Dictionary <string, TypeDefinition> typeDefinitions, HookData hookData) { string[] _strSplit = str.Split('.'); string className = str.Substring(0, str.Substring(0, str.Length - 1).LastIndexOf('.')); string methodName = _strSplit.Last(); if (!typeDefinitions.TryGetValue(className, out TypeDefinition typeDef)) { string desc = string.Format("Could not find class {0} in assembly {1}", className, assembly.Name); Externs.MessageBox("Could not find class!", desc); return(null); } List <MethodDefinition> methodDefs = typeDef.GetMethods(methodName).ToList(); if (methodDefs.Count == 0) { string desc = string.Format("Could not find method {0} in type {1}", methodName, className); Externs.MessageBox("Could not find method!", desc); return(null); } MethodDefinition methodDef = GetHook(methodDefs, hookData, className, methodName); return(new ReturnData(typeDef, methodDef)); }
private static Sprite LoadObjectSprite(object obj, string id, string type, string image) { Sprite sprite = null; string lcType = type.ToLower(); try { string modDir = GetModFolder(obj); string objDir = Path.Combine(modDir, type + 's'); string iconPath = Path.GetFullPath(Path.Combine(objDir, image)); Logger.Log($"Loading {lcType} image {id}"); sprite = LoadSprite(iconPath); if (sprite != null) { sprite.name = id; sprite.texture.name = id; } else { Logger.Log($"Failed to load {lcType} image {id}"); } } catch (Exception ex) { Externs.MessageBox($"Loading {lcType} image failed", ex.ToString()); } return(sprite); }
private static void Patch(string assembly, string assemblyClean, string lizard, string attributes) { // keep original assembly before modification if (!File.Exists(assemblyClean)) { File.Copy(assembly, assemblyClean); } File.Copy(LizardDll, lizard); File.Copy(AttributesDll, attributes); Mono.Cecil.AssemblyDefinition assemblyDefinition = Cecil.ReadAssembly(assembly); if (assemblyDefinition == null) { Externs.MessageBox("Assembly is null", "Failed to load assembly " + assembly); Environment.Exit(-1); } int injectedCorrectly = Util.InjectAllHooks(Attributes, assemblyDefinition); if (injectedCorrectly > 0) { Cecil.WriteChanges(assemblyDefinition); } ModCompiler.CompileMods(); }
public static bool InjectNewMethod(TypeDefinition type, AssemblyDefinition assembly, Dictionary <string, TypeDefinition> typeDefinitions, string methodName, HookData hook) { try { MethodDefinition methodDef = new MethodDefinition(methodName, MethodAttributes.Public, assembly.MainModule.TypeSystem.Void) { IsStatic = type.IsSealed }; type.Methods.Add(methodDef); ILProcessor il = methodDef.Body.GetILProcessor(); methodDef.Body.Instructions.Add(il.Create(OpCodes.Call, assembly.MainModule.Import(hook.Method))); methodDef.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); return(true); } catch (Exception ex) { Externs.MessageBox(ex.GetType().Name, ex.ToString()); return(false); } }
private static Sprite LoadSprite(string path) { try { Logger.Log($"Loading sprite {path}"); byte[] textureBytes = null; if (!File.Exists(path)) { Logger.Log($"Could not find texture {path}"); return(null); } else { textureBytes = File.ReadAllBytes(path); } Texture2D tex = new Texture2D(1, 1); if (tex.LoadImage(textureBytes)) { return(Sprite.Create(tex, new Rect(0, 0, 64, 64), Vector2.one * 0.5f)); } Externs.MessageBox("Failed loading texture!", $"Failed to load texture {path}"); } catch (Exception ex) { Externs.MessageBox("Error loading sprite!", ex.ToString()); } return(null); }
public static TypeDefinition ConvertStringToClass(string className, AssemblyDefinition assembly, Dictionary <string, TypeDefinition> typeDefinitions) { if (!typeDefinitions.TryGetValue(className, out TypeDefinition typeDef)) { string desc = string.Format("Could not find class {0} in assembly {1}", className, assembly.Name); Externs.MessageBox("Could not find class!", desc); return(null); } return(typeDef); }
internal static bool WriteChanges(AssemblyDefinition targetAssembly) { try { targetAssembly.Write(Program.GameAssembly); return(true); } catch (Exception ex) { Externs.MessageBox(ex.GetType().Name, ex.ToString()); } return(false); }
public static AssemblyDefinition ReadAssembly(string assemblyPath) { try { DefaultAssemblyResolver resolver = new DefaultAssemblyResolver(); resolver.AddSearchDirectory(Program.ManagedFolder); return(AssemblyDefinition.ReadAssembly(assemblyPath, new ReaderParameters { AssemblyResolver = resolver })); } catch (Exception ex) { Externs.MessageBox(ex.GetType().Name, ex.ToString()); return(null); } }
static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Attributes = new Dictionary <string, List <HookData> >(); if (!Directory.Exists(ModFolder)) { Directory.CreateDirectory(ModFolder); } ManagedFolder = Util.GetManagedDirectory(); if (ManagedFolder == null) { Externs.MessageBox("Managed folder missing!", "Could not find the Managed folder"); Environment.Exit(-1); } GameAssembly = Path.Combine(ManagedFolder, GameAssembly); if (!Assemblies.LoadLizard()) { Environment.Exit(-1); } string assemblyClean = GameAssembly + ".clean"; string lizard = Path.Combine(ManagedFolder, LizardDll); string attributes = Path.Combine(ManagedFolder, AttributesDll); Unpatch(GameAssembly, assemblyClean, lizard, attributes); // no hooks if (Attributes.Sum(x => x.Value.Count) == 0) { RunGame(); Environment.Exit(0); } Patch(GameAssembly, assemblyClean, lizard, attributes); RunGame(); }
public static T CreateProjectile <T>(Player.SkillState skill, bool setAttackInfo = true) where T : Projectile { try { GameObject projGO = new GameObject(typeof(T).FullName); Transform projTr = projGO.transform; Transform ownerTr = skill.parent.transform; projTr.position = ownerTr.position; projTr.rotation = ownerTr.rotation; T proj = projGO.AddComponent <T>(); proj.destroyOnDisable = true; proj.pointTowardMoveVector = true; if (proj.impactAudioID == null) { proj.impactAudioID = ""; } if (proj.particleEffectName == null) { proj.particleEffectName = ""; } proj.parentObject = skill.parent.gameObject; proj.parentEntity = skill.parent; if (setAttackInfo) { proj.attackBox.SetAttackInfo(skill.parent.skillCategory, skill.skillID); proj.UpdateCalculatedDamage(true); } return(proj); } catch (System.Exception ex) { Externs.MessageBox("Error!", ex.ToString()); } return(null); }
private static void RunGame() { DirectoryInfo gameDir = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent; string[] files = Directory.GetFiles(gameDir.FullName, "*.exe"); string gameFile = files.Length > 0 ? files[0] : null; if (gameFile == null) { Externs.MessageBox("Game executable missing!", "Failed to find game executable"); } else { System.Diagnostics.Process proc = new System.Diagnostics.Process { StartInfo = new System.Diagnostics.ProcessStartInfo(gameFile) }; proc.Start(); } }
public static bool LoadLizard() { try { AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(Path.GetFullPath(Program.LizardDll)); List <HookData> allAttributes = AttributesHelper.GetAllAttributesInAssembly(assembly); Dictionary <string, List <HookData> > filteredAttributes = AttributesHelper.FindAndInvokeAllAttributes(allAttributes); foreach (KeyValuePair <string, List <HookData> > kvp in filteredAttributes) { Program.Attributes.Add(kvp.Key, kvp.Value); } return(true); } catch (Exception ex) { Externs.MessageBox(ex.GetType().Name, ex.ToString()); } return(false); }
public Assembly GetAssembly(string assemblyPath) { try { Assembly assembly = Assembly.LoadFile(assemblyPath); HashSet <string> loadedReferences = new HashSet <string>(); foreach (AssemblyName reference in assembly.GetReferencedAssemblies()) { LoadReference(reference, loadedReferences); } return(assembly); } catch (Exception ex) { Externs.MessageBox("Failed to load assembly", ex.ToString()); } return(null); }
internal static bool Inject(ReturnData data, HookData hook) { try { InjectionDefinition injector = GetInjectionDefinition(data, hook); if ((bool)hook.Attribute.ConstructorArguments[1].Value) { injector.Inject(-1); } else { injector.Inject(); } return(true); } catch (Exception ex) { Externs.MessageBox(ex.GetType().Name, ex.ToString()); } return(false); }
private static void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs ex) { Externs.MessageBox("Error!", ex.ExceptionObject.ToString()); }
public static void MessageBox(string title, string description) => Externs.MessageBox(title, description);
private static MethodDefinition GetHook(List <MethodDefinition> methodDefs, HookData hookData, string className, string methodName) { MethodDefinition methodDef = methodDefs[0]; if (methodDefs.Count == 1) { return(methodDef); } ParameterCollection hookTypes = hookData.Method.Parameters; bool hookStatic = hookData.Method.Parameters.Count == 0 || hookData.Method.Parameters[0].ParameterType.Name != className; for (int i = 0; i < methodDefs.Count; i++) { if (methodDefs[i].IsStatic != hookStatic) { methodDefs.RemoveAt(i--); } } if (hookTypes.Count == 0 || !hookStatic && hookTypes.Count == 1) { for (int i = 0; i < methodDefs.Count; i++) { if (methodDefs[i].Parameters.Count == 0) { return(methodDefs[i]); } } return(methodDef); } for (int i = 0; i < methodDefs.Count; i++) { if (!SafeMatch(methodDefs[i], hookData)) { methodDefs.RemoveAt(i--); } } if (methodDefs.Count > 1) { System.Text.StringBuilder hooks = new System.Text.StringBuilder("Could not determine which function to hook:\n"); foreach (MethodDefinition md in methodDefs) { hooks.Append($"{className}.{md.Name}("); if (md.Parameters.Count > 0) { hooks.Append(md.Parameters[0].ParameterType.Name); for (int i = 1; i < md.Parameters.Count; i++) { hooks.Append($", {md.Parameters[i].ParameterType.Name}"); } } hooks.Append(")\n"); } Externs.MessageBox("Ambiguous hook!", hooks.ToString()); } if (methodDefs.Count > 0) { methodDef = methodDefs[0]; } return(methodDef); }
private static void ModDomain_UnhandledException(object sender, UnhandledExceptionEventArgs ex) { string caption = "Error in " + Assembly.GetExecutingAssembly().GetName(); Externs.MessageBox(caption, ex.ExceptionObject.ToString()); }