Exemple #1
0
        /// <summary>Creates a new Harmony instance</summary>
        /// <param name="id">A unique identifier</param>
        /// <returns>A Harmony instance</returns>
        ///
        public Harmony(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException($"{nameof(id)} cannot be null or empty");
            }

            Logger.Log(Logger.LogChannel.Info, () =>
            {
                var assembly         = typeof(Harmony).Assembly;
                var version          = assembly.GetName().Version;
                var assemblyLocation = assembly.Location;

                if (string.IsNullOrEmpty(assemblyLocation))
                {
                    assemblyLocation = new Uri(assembly.CodeBase).LocalPath;
                }

                var callingMethod   = AccessTools.GetOutsideCaller();
                var callingAssembly = callingMethod.DeclaringType?.Assembly;                 // Can be null in <Module>

                var callingAssemblyLocation = callingAssembly?.Location;
                if (string.IsNullOrEmpty(callingAssemblyLocation))
                {
                    callingAssemblyLocation = callingAssembly != null ? new Uri(callingAssembly.CodeBase).LocalPath : string.Empty;
                }

                return($"Created Harmony instance id={id}, version={version}, location={assemblyLocation} - Started from {callingMethod.GetID()} location={callingAssemblyLocation}");
            });

            Id = id;
        }
Exemple #2
0
        /// <summary>Creates a new Harmony instance</summary>
        /// <param name="id">A unique identifier</param>
        /// <returns>A Harmony instance</returns>
        ///
        public Harmony(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException($"{nameof(id)} cannot be null or empty");
            }

            if (DEBUG)
            {
                var assembly = typeof(Harmony).Assembly;
                var version  = assembly.GetName().Version;
                var location = assembly.Location;
                if (string.IsNullOrEmpty(location))
                {
                    location = new Uri(assembly.CodeBase).LocalPath;
                }
                FileLog.Log($"### Harmony id={id}, version={version}, location={location}");
                var callingMethod = AccessTools.GetOutsideCaller();
                if (callingMethod.DeclaringType != null)
                {
                    var callingAssembly = callingMethod.DeclaringType.Assembly;
                    location = callingAssembly.Location;
                    if (string.IsNullOrEmpty(location))
                    {
                        location = new Uri(callingAssembly.CodeBase).LocalPath;
                    }
                    FileLog.Log($"### Started from {callingMethod.FullDescription()}, location {location}");
                    FileLog.Log($"### At {DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss")}");
                }
            }

            Id = id;
        }
Exemple #3
0
        /// <summary>Creates a new Harmony instance</summary>
        /// <param name="id">A unique identifier</param>
        /// <returns>A Harmony instance</returns>
        ///
        public Harmony(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id) + " cannot be null or empty");
            }

            if (DEBUG)
            {
                var assembly = typeof(Harmony).Assembly;
                var version  = assembly.GetName().Version;
                var location = assembly.Location;
                if (string.IsNullOrEmpty(location))
                {
                    location = new Uri(assembly.CodeBase).LocalPath;
                }
                FileLog.Log("### Harmony id=" + id + ", version=" + version + ", location=" + location);
                var callingMethod   = AccessTools.GetOutsideCaller();
                var callingAssembly = callingMethod.DeclaringType.Assembly;
                location = callingAssembly.Location;
                if (string.IsNullOrEmpty(location))
                {
                    location = new Uri(callingAssembly.CodeBase).LocalPath;
                }
                FileLog.Log("### Started from " + callingMethod.FullDescription() + ", location " + location);
                FileLog.Log("### At " + DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss"));
            }

            Id = id;

            if (!selfPatchingDone)
            {
                selfPatchingDone = true;
            }
        }
Exemple #4
0
        /// <summary>Creates a new Harmony instance</summary>
        /// <param name="id">A unique identifier (you choose your own)</param>
        /// <returns>A Harmony instance</returns>
        ///
        public Harmony(string id)
        {
#pragma warning disable 618
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException($"{nameof(id)} cannot be null or empty");
            }

            try
            {
                var envDebug = Environment.GetEnvironmentVariable("HARMONY_DEBUG");
                if (envDebug is object && envDebug.Length > 0)
                {
                    envDebug = envDebug.Trim();
                    DEBUG    = envDebug == "1" || bool.Parse(envDebug);
                }
            }
            catch
            {
            }

            if (DEBUG)
            {
                HarmonyFileLog.Enabled = true;
            }

            Logger.Log(Logger.LogChannel.Info, () =>
            {
                var sb          = new StringBuilder();
                var assembly    = typeof(Harmony).Assembly;
                var version     = assembly.GetName().Version;
                var location    = assembly.Location;
                var environment = Environment.Version.ToString();
                var platform    = Environment.OSVersion.Platform.ToString();
                if (string.IsNullOrEmpty(location))
                {
                    location = new Uri(assembly.CodeBase).LocalPath;
                }

                sb.AppendLine($"### Harmony id={id}, version={version}, location={location}, env/clr={environment}, platform={platform}");
                var callingMethod = AccessTools.GetOutsideCaller();
                if (callingMethod.DeclaringType is object)
                {
                    var callingAssembly = callingMethod.DeclaringType.Assembly;
                    location            = callingAssembly.Location;
                    if (string.IsNullOrEmpty(location))
                    {
                        location = new Uri(callingAssembly.CodeBase).LocalPath;
                    }
                    sb.AppendLine($"### Started from {callingMethod.FullDescription()}, location {location}");
                    sb.Append($"### At {DateTime.Now:yyyy-MM-dd hh.mm.ss}");
                }

                return(sb.ToString());
            });

            Id = id;
#pragma warning restore 618
        }
Exemple #5
0
        /// <summary>Creates a new Harmony instance</summary>
        /// <param name="id">A unique identifier (you choose your own)</param>
        /// <returns>A Harmony instance</returns>
        ///
        public Harmony(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException($"{nameof(id)} cannot be null or empty");
            }

            try
            {
                var envDebug = Environment.GetEnvironmentVariable("HARMONY_DEBUG");
                if (envDebug is object && envDebug.Length > 0)
                {
                    envDebug = envDebug.Trim();
                    DEBUG    = envDebug == "1" || bool.Parse(envDebug);
                }
            }
            catch
            {
            }

            if (DEBUG)
            {
                var assembly    = typeof(Harmony).Assembly;
                var version     = assembly.GetName().Version;
                var location    = assembly.Location;
                var environment = Environment.Version.ToString();
                var platform    = Environment.OSVersion.Platform.ToString();
#if !NET5_0
                if (string.IsNullOrEmpty(location))
                {
                    location = new Uri(assembly.CodeBase).LocalPath;
                }
#endif
                var ptr_runtime = IntPtr.Size;
                var ptr_env     = PlatformHelper.Current;
                FileLog.Log($"### Harmony id={id}, version={version}, location={location}, env/clr={environment}, platform={platform}, ptrsize:runtime/env={ptr_runtime}/{ptr_env}");
                var callingMethod = AccessTools.GetOutsideCaller();
                if (callingMethod.DeclaringType is object)
                {
                    var callingAssembly = callingMethod.DeclaringType.Assembly;
                    location = callingAssembly.Location;
#if !NET5_0
                    if (string.IsNullOrEmpty(location))
                    {
                        location = new Uri(callingAssembly.CodeBase).LocalPath;
                    }
#endif
                    FileLog.Log($"### Started from {callingMethod.FullDescription()}, location {location}");
                    FileLog.Log($"### At {DateTime.Now:yyyy-MM-dd hh.mm.ss}");
                }
            }

            Id = id;
        }
Exemple #6
0
        /// <summary>Creates a new Harmony instance</summary>
        /// <param name="id">A unique identifier (you choose your own)</param>
        /// <returns>A Harmony instance</returns>
        ///
        public Harmony(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException($"{nameof(id)} cannot be null or empty");
            }

            try
            {
                var envDebug = Environment.GetEnvironmentVariable("HARMONY_DEBUG");
                if (envDebug != null && envDebug.Length > 0)
                {
                    envDebug = envDebug.Trim();
                    DEBUG    = envDebug == "1" || bool.Parse(envDebug);
                }
            }
            catch
            {
            }

            if (DEBUG)
            {
                var assembly = typeof(Harmony).Assembly;
                var version  = assembly.GetName().Version;
                var location = assembly.Location;
                if (string.IsNullOrEmpty(location))
                {
                    location = new Uri(assembly.CodeBase).LocalPath;
                }
                FileLog.Log($"### Harmony id={id}, version={version}, location={location}");
                var callingMethod = AccessTools.GetOutsideCaller();
                if (callingMethod.DeclaringType != null)
                {
                    var callingAssembly = callingMethod.DeclaringType.Assembly;
                    location = callingAssembly.Location;
                    if (string.IsNullOrEmpty(location))
                    {
                        location = new Uri(callingAssembly.CodeBase).LocalPath;
                    }
                    FileLog.Log($"### Started from {callingMethod.FullDescription()}, location {location}");
                    FileLog.Log($"### At {DateTime.Now:yyyy-MM-dd hh.mm.ss}");
                }
            }

            Id = id;
        }