Exemplo n.º 1
0
        /// <summary>
        /// Creates an auto-formatted debug log, typically used to display information about the module. Use String.Format to assign variables.
        /// </summary>
        /// <param name="module">The module that called this method, since it needs to access the module's name and id.</param>
        /// <param name="log">The information to log.</param>
        /// <param name="logType">The icon that displays on the log.</param>
        internal static void Log(this ModuleScript module, object log, LogType logType = LogType.Log)
        {
            string formattedLog = "[{0} #{1}]: {2}".Form(module.ModuleName, module.ModuleId, log);

            switch (logType)
            {
            case LogType.Error:
                Debug.LogError(formattedLog);
                break;

            case LogType.Assert:
                Debug.LogAssertion(formattedLog);
                break;

            case LogType.Warning:
                Debug.LogWarning(formattedLog);
                break;

            case LogType.Log:
                Debug.Log(formattedLog);
                break;

            case LogType.Exception:
                Debug.LogException((Exception)log);
                break;

            default:
                throw new NotImplementedException(logType.ToString() + " is not a valid log type.");
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
 internal static void Unassign(this KMBombModule kmBombModule,
                               Func <Action> onActivate,
                               ModuleScript moduleScript = null)
 {
     if (kmBombModule == null)
     {
         throw new ArgumentNullException("The KMBombModule is null. You cannot assign events to a KMBombModule without a reference to a KMBombModule.");
     }
     if (onActivate == null)
     {
         throw new ArgumentNullException("The OnActivate event is null. Considering that KMBombModule has only 1 event, this is considered a mistake.");
     }
     kmBombModule.OnActivate -= delegate() { if (moduleScript != null)
                                             {
                                                 moduleScript.IsActivate = true;
                                             }
                                             onActivate.Invoke().Invoke(); };
 }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
 internal static void Unassign(this KMNeedyModule kmNeedyModule,
                               Func <Action> onActivateNeedy     = null,
                               Func <Action> onNeedyActivation   = null,
                               Func <Action> onNeedyDeactivation = null,
                               Func <Action> onTimerExpired      = null,
                               ModuleScript moduleScript         = null)
 {
     if (kmNeedyModule == null)
     {
         throw new ArgumentNullException("The KMNeedyModule is null. You cannot assign events to a KMNeedyModule without a reference to a KMNeedyModule.");
     }
     if (onActivateNeedy != null)
     {
         kmNeedyModule.OnActivate -= delegate() { onActivateNeedy.Invoke().Invoke(); }
     }
     ;
     if (onNeedyActivation != null)
     {
         kmNeedyModule.OnNeedyActivation -= delegate() { if (moduleScript != null)
                                                         {
                                                             moduleScript.IsNeedyActive = true;
                                                         }
                                                         onNeedyActivation.Invoke().Invoke(); }
     }
     ;
     if (onNeedyDeactivation != null)
     {
         kmNeedyModule.OnNeedyDeactivation -= delegate() { if (moduleScript != null)
                                                           {
                                                               moduleScript.IsNeedyActive = false;
                                                           }
                                                           onNeedyDeactivation.Invoke().Invoke(); }
     }
     ;
     if (onTimerExpired != null)
     {
         kmNeedyModule.OnTimerExpired -= delegate() { onTimerExpired.Invoke().Invoke(); }
     }
     ;
 }