コード例 #1
0
        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]);
        }
コード例 #2
0
        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]);
        }
コード例 #3
0
        /// <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);
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
        /// <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);
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
        /// <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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
                }
            }
        }
コード例 #11
0
        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));
            }
        }