Example #1
0
        /// <summary>
        /// Creates a log containing all properties and fields. This is meant for quick debugging, hence why it uses LogWarning to remind the user to clean it up later.
        /// </summary>
        /// <param name="module">The module that called this method, since it needs to access the module's name and id</param>
        /// <param name="allowInGame">Whether it should allow the code to run in-game, and not only on Unity.</param>
        internal static void Dump(this ModuleScript module, bool allowInGame = false)
        {
            if (!allowInGame && !module.IsEditor)
            {
                return;
            }

            var values = new List <object>();
            int index  = 0;

            var type = module.GetType();

            foreach (var descriptor in type.GetFields(DeclaredOnlyLookup))
            {
                string name  = descriptor.Name;
                var    value = descriptor.GetValue(module);
                values.Add(DumpFormat.Form(index++, name, value == null ? "Null" : value.GetType().ToString(), value.ToEnumerableUnwrap().Join(", ")));
            }

            foreach (var descriptor in type.GetProperties(DeclaredOnlyLookup))
            {
                string name  = descriptor.Name;
                var    value = descriptor.GetValue(module, null);
                values.Add(DumpFormat.Form(index++, name, value == null ? "Null" : value.GetType().ToString(), value.ToEnumerableUnwrap().Join(", ")));
            }

            string formattedLog = "[{0} #{1}]: <DUMP>{2}".Form(
                module.ModuleName,
                module.ModuleId,
                values.Select(o => o.ToEnumerableUnwrap().Join(", ")).Join(""));

            Debug.LogWarning(formattedLog);
        }
Example #2
0
        /// <summary>
        /// Creates a log containing the values of the arguments. This is meant for quick debugging, hence why it uses LogWarning to remind the user to clean it up later. Make the first index of the string array a string empty or null if you want it to run in-game!
        /// </summary>
        /// <param name="module">The module that called this method, since it needs to access the module's name and id</param>
        /// <param name="logs">The information to log.</param>
        internal static void Dump(this ModuleScript module, params string[] logs)
        {
            if (!logs[0].IsNullOrEmpty() && !module.IsEditor)
            {
                return;
            }

            string[] formatted = new string[logs.Length];

            for (int i = 0; i < logs.Length; i++)
            {
                if (logs[i].IsNullOrEmpty())
                {
                    continue;
                }

                var    type     = module.GetType();
                var    field    = type.GetField(logs[i], DefaultLookup);
                var    property = type.GetProperty(logs[i], DefaultLookup);
                object value;

                if (field != null)
                {
                    value = field.GetValue(module);
                }
                else if (property != null)
                {
                    value = property.GetValue(module, null);
                }
                else
                {
                    throw new NotSupportedException("Argument {0} ({1}) couldn't be found as a field or property in {2}.".Form(logs[i], i, module));
                }

                formatted[i] = DumpFormat.Form(i, logs[i], value == null ? "Null" : value.GetType().ToString(), value.ToEnumerableUnwrap().Join(", "));
            }

            string formattedLog = "[{0} #{1}]: <DUMP>{2}".Form(module.ModuleName, module.ModuleId, formatted.Join(""));

            Debug.LogWarning(formattedLog);
        }