public static SoundEffect GetSound(string name) { if (Main.dedServ) { return(null); } if (_sounds.ContainsKey(name)) { return(_sounds[name]); } string path = Path.Combine(DNMT.modRootFolder, name + @".wav"); if (!File.Exists(path)) { DNMT.LogError("Error loading sound effect '" + path + "' - file not exisit!"); return(null); } try { using (FileStream fileStream = new FileStream(path, FileMode.Open)) { _sounds[name] = SoundEffect.FromStream(fileStream); } } catch (System.Exception ex) { DNMT.LogError("Error loading sound effect '" + path + "' - " + ex.Message + "!"); _sounds[name] = null; } return(_sounds[name]); }
public static Texture2D GetTexture(string name) { if (Main.dedServ) { return(null); } if (_textures.ContainsKey(name)) { return(_textures[name]); } string path = Path.Combine(DNMT.modRootFolder, name + @".png"); if (!File.Exists(path)) { DNMT.LogError("Error loading texture '" + path + "' - file not exisit!"); return(null); } try { using (FileStream fileStream = new FileStream(path, FileMode.Open)) { _textures[name] = Texture2D.FromStream(Main.instance.GraphicsDevice, fileStream); } } catch (System.Exception ex) { DNMT.LogError("Error loading texture '" + path + "' - " + ex.Message + "!"); _textures[name] = null; } return(_textures[name]); }
/// <summary> /// Only use this instead of GetTypeDefinition() when the type is not within the Terraria module (eg. an XNA type). /// </summary> /// <param name="fullName"></param> /// <returns></returns> public static TypeReference GetTypeReference(ModuleDefinition moduleDefinition, string fullName) { TypeReference reference; if (!moduleDefinition.TryGetTypeReference(fullName, out reference)) { DNMT.LogError(string.Format("Failed to locate {0} type!", fullName)); } return(reference); }
/// <summary> /// Returns a method definition. /// </summary> /// <param name="t"></param> /// <param name="methodName"></param> /// <returns></returns> public static ModuleDefinition GetModuleDefinition(AssemblyDefinition definition, string fullyQualifiedName) { ModuleDefinition module = definition.Modules.FirstOrDefault(p => p.FullyQualifiedName == fullyQualifiedName); if (module == null) { DNMT.LogError(string.Format("Failed to locate {0} reference!", fullyQualifiedName)); module = definition.MainModule; } return(module); }
/// <summary> /// Returns a type definition. /// </summary> /// <param name="typeName"></param> /// <returns></returns> public static TypeDefinition GetTypeDefinition(ModuleDefinition moduleDefinition, string typeName) { var result = (from TypeDefinition t in moduleDefinition.Types where t.Name == typeName select t).FirstOrDefault(); if (result == null) { DNMT.LogError(string.Format("Failed to locate {0} type!", typeName)); } return(result); }
/// <summary> /// Returns a property definition. /// </summary> /// <param name="t"></param> /// <param name="fieldName"></param> /// <returns></returns> public static FieldDefinition GetFieldDefinition(TypeDefinition t, string fieldName) { var result = (from FieldDefinition f in t.Fields where f.Name == fieldName select f).FirstOrDefault(); if (result == null) { DNMT.LogError(string.Format("Failed to locate {0}.{1} field!", t.FullName, fieldName)); } return(result); }
/// <summary> /// Returns a method definition. /// </summary> public static MethodDefinition GetMethodDefinition(TypeDefinition t, string methodName, int parameterCount = -1) { var result = (from MethodDefinition m in t.Methods where m.Name == methodName && (parameterCount == -1 || m.Parameters.Count + m.GenericParameters.Count == parameterCount) select m).FirstOrDefault(); if (result == null) { DNMT.LogError(string.Format("Failed to locate {0}.{1}() method!", t.FullName, methodName)); } return(result); }
/// <summary> /// Returns a property definition. /// </summary> /// <param name="t"></param> /// <param name="propName"></param> /// <returns></returns> public static PropertyDefinition GetPropertyDefinition(TypeDefinition t, string propName) { var result = (from PropertyDefinition p in t.Properties where p.Name == propName select p).FirstOrDefault(); if (result == null) { DNMT.LogError(string.Format("Failed to locate {0}.{1} property!", t.FullName, propName)); } return(result); }
public static void OnChatCommand(string command, string[] args) { try { foreach (var e in _pluginEventMap[(int)PluginEvent.OnChatCommand]) { e.OnChatCommand(command, args); } } catch (System.Exception ex) { DNMT.LogWarning(ex.Message); } }
public static object OnUpdate(object rv, object obj, params object[] args) { try { foreach (var e in _pluginEventMap[(int)PluginEvent.OnUpdate]) { e.OnUpdate(); } } catch (System.Exception ex) { DNMT.LogWarning(ex.Message); } return(null); }
public static FieldDefinition AddStaticField(TypeDefinition classType, string field, TypeReference type, object value = null) { var classStaticConstructor = GetMethodDefinition(classType, ".cctor"); if (classStaticConstructor == null) { return(null); } var fld = new FieldDefinition(field, FieldAttributes.Static | FieldAttributes.Public, type); classType.Fields.Add(fld); if (value != null) { var il = classStaticConstructor.Body.GetILProcessor(); var first = il.Body.Instructions[0]; if (type.Name == "String") { il.InsertBefore(first, il.Create(OpCodes.Ldstr, (string)value)); } else if (type.Name == "Int32") { il.InsertBefore(first, il.Create(OpCodes.Ldc_I4, (int)value)); } else if (type.Name == "Boolean") { il.InsertBefore(first, il.Create(OpCodes.Ldc_I4, (bool)value ? 1 : 0)); } else if (type.Name == "Single") { il.InsertBefore(first, il.Create(OpCodes.Ldc_R4, (Single)value)); } else if (value is Instruction) { il.InsertBefore(first, (Instruction)value); } else { DNMT.LogError(string.Format("AddStaticField(): Unrecognized type '{0}'!", type.FullName)); } il.InsertBefore(first, il.Create(OpCodes.Stsfld, fld)); } return(fld); }
public static object OnNetMessageSendData(object rv, object obj, params object[] args) { var msgType = (int)args[0]; var remoteClient = (int)args[1]; var ignoreClient = (int)args[2]; var text = (string)args[3]; var number = (int)args[4]; bool ret = false; try { foreach (var e in _pluginEventMap[(int)PluginEvent.OnNetMessageSendData]) { ret = ret || e.OnNetMessageSendData(msgType, remoteClient, ignoreClient, text, number, (float)args[5], (float)args[6], (float)args[7], (int)args[8], (int)args[9], (int)args[10]); } if (msgType == 25 && number == Main.myPlayer && !string.IsNullOrEmpty(text) && text[0] == '.') { ret = true; var split = text.Substring(1).Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); var cmd = split[0].ToLower(); var cmdArgs = split.Length > 1 ? split[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) : new string[0]; if (cmd == "plugins") { CORE.Print("Loaded plugins:", Color.Green); CORE.Print(string.Join(", ", _pluginList.Select(plugin => plugin.GetType().Name))); //pluginList.ForEach(plugin => { CORE.Print(" " + plugin.GetType().Name); }); } else { foreach (var e in _pluginEventMap[(int)PluginEvent.OnChatCommand]) { e.OnChatCommand(cmd, cmdArgs); } } } } catch (System.Exception ex) { DNMT.LogWarning(ex.Message); } return(ret ? (object)true : null); }
public static void OnUpdateControlInput() { try { KeyboardState stateKeyboard = Keyboard.GetState(); MouseState stateMouse = Mouse.GetState(); GamePadState stateGamePad = GamePad.GetState(Game1.playerOneIndex); foreach (var e in _pluginEventMap[(int)PluginEvent.OnUpdateControlInput]) { e.OnUpdateControlInput(stateKeyboard, stateMouse, stateGamePad); } } catch (System.Exception ex) { DNMT.LogWarning(ex.Message); } }
public static object OnPlayerKillMe(object rv, object obj, params object[] args) { bool ret = false; try { foreach (var e in _pluginEventMap[(int)PluginEvent.OnPlayerKillMe]) { ret = ret || e.OnPlayerKillMe((Player)obj, (PlayerDeathReason)args[0], (double)args[1], (int)args[2], (bool)args[3]); } } catch (System.Exception ex) { DNMT.LogWarning(ex.Message); } return(ret ? (object)true : null); }
static EventRouter() { // LoadPlugins _pluginList = new List <PluginBase>(); var pluginBaseType = typeof(PluginBase); var pluginTypeList = pluginBaseType.Assembly.GetTypes() .Where(t => pluginBaseType.IsAssignableFrom(t) && pluginBaseType != t); Array events = Enum.GetValues(typeof(PluginEvent)); // string[] = Enum.GetNames(typeof(PluginEvent)); _pluginEventMap = new List <PluginBase> [events.Length]; for (int i = 0; i < events.Length; i++) { _pluginEventMap[i] = new List <PluginBase>(); } foreach (var type in pluginTypeList) { if (!DNMT.Config.Get("plugins", type.FullName, true)) { continue; } PluginBase instance = null; try { instance = (PluginBase)Activator.CreateInstance(type); } catch (System.Exception ex) { DNMT.LogError("Error loading plugin: " + type.FullName + " - " + ex.Message + "!"); continue; } _pluginList.Add(instance); foreach (PluginEvent e in events) { var method = type.GetMethod(e.ToString()); if (method == null || method.DeclaringType == pluginBaseType) { continue; } _pluginEventMap[(int)e].Add(instance); } } }
static HotkeyCore() { // Load hotkey binds var result = DNMT.Config.EnumerateKeys("HotkeyBinds"); foreach (var k in result) { Hotkey key; string command; if (DNMT.Config.TryGet("HotkeyBinds", k, out command) && Hotkey.TryParse(k, out key) && command.StartsWith(".")) { RegisterHotkey(command, key); } else { DNMT.LogWarning("Invalid record in [HotkeyBinds]: " + k + "."); } } }
public static object OnPreUpdate(object rv, object obj, params object[] args) { try { if (CORE.IsCanUseHotKeys()) { HotkeyCore.Process(); } foreach (var e in _pluginEventMap[(int)PluginEvent.OnPreUpdate]) { e.OnPreUpdate(); } } catch (System.Exception ex) { DNMT.LogWarning(ex.Message); } return(null); }
public static void ModifyStaticField(TypeDefinition classType, string field, object newValue) { var classStaticConstructor = GetMethodDefinition(classType, ".cctor"); if (classStaticConstructor == null) { return; } if (newValue is string) { ModifyStaticField(classStaticConstructor, field, instr => { instr.OpCode = OpCodes.Ldstr; instr.Operand = newValue; }); } else if (newValue is int || newValue is bool) { ModifyStaticField(classStaticConstructor, field, instr => { instr.OpCode = OpCodes.Ldc_I4; instr.Operand = newValue; }); } else if (newValue is float) { ModifyStaticField(classStaticConstructor, field, instr => { instr.OpCode = OpCodes.Ldc_R4; instr.Operand = newValue; }); } else { DNMT.LogError(string.Format("ModifyStaticField(): Unrecognized type '{0}'!", newValue.GetType().FullName)); } }
public static object OnLightingGetColor(object rv, object obj, params object[] args) { bool ret = false; var result = Color.White; try { foreach (var e in _pluginEventMap[(int)PluginEvent.OnLightingGetColor]) { Color temp; if (e.OnLightingGetColor((int)args[0], (int)args[1], out temp)) { ret = true; result = temp; } } } catch (System.Exception ex) { DNMT.LogWarning(ex.Message); } return(ret ? (object)result : null); }
public static object OnPlayerHurt(object rv, object obj, params object[] args) { double result = 0.0; bool ret = false; try { foreach (var e in _pluginEventMap[(int)PluginEvent.OnPlayerHurt]) { double temp; if (e.OnPlayerHurt((Player)obj, (PlayerDeathReason)args[0], (int)args[1], (int)args[2], (bool)args[3], (bool)args[4], (bool)args[5], (int)args[6], out temp)) { result = temp; ret = true; } } } catch (System.Exception ex) { DNMT.LogWarning(ex.Message); } return(ret ? (object)result : null); }