From 2f7fdc18590bdb676162dbd1bb8f70e201d7344f Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Mon, 22 Jul 2019 02:49:09 -0500 Subject: [PATCH] Finished backport to Net3 --- IPA.Injector/GameVersionEarly.cs | 6 ++++++ IPA.Injector/Injector.cs | 9 +++++++-- IPA.Injector/PermissionFix.cs | 10 +++++----- IPA.Injector/Updates.cs | 8 +++++++- IPA.Loader/Loader/PluginLoader.cs | 9 +++++---- IPA.Loader/Loader/PluginManager.cs | 3 ++- IPA.Loader/Logging/StandardLogger.cs | 2 +- IPA.Loader/Utilities/Utils.cs | 10 ++++++++++ Net3-Proxy/Directory.cs | 29 ++++++++++++++++++++++++++++ Net3-Proxy/Extensions.cs | 7 +++++++ Net3-Proxy/File.cs | 1 + Net3-Proxy/Net3-Proxy.csproj | 1 + Net3-Proxy/Path.cs | 1 + 13 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 Net3-Proxy/Directory.cs diff --git a/IPA.Injector/GameVersionEarly.cs b/IPA.Injector/GameVersionEarly.cs index 7b251157..ece652f7 100644 --- a/IPA.Injector/GameVersionEarly.cs +++ b/IPA.Injector/GameVersionEarly.cs @@ -3,6 +3,12 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +#if NET3 +using Net3_Proxy; +using Path = Net3_Proxy.Path; +using File = Net3_Proxy.File; +using Directory = Net3_Proxy.Directory; +#endif namespace IPA.Injector { diff --git a/IPA.Injector/Injector.cs b/IPA.Injector/Injector.cs index 5c247c31..f0316662 100644 --- a/IPA.Injector/Injector.cs +++ b/IPA.Injector/Injector.cs @@ -2,17 +2,22 @@ using IPA.Injector.Backups; using IPA.Loader; using IPA.Logging; +using IPA.Utilities; using Mono.Cecil; using Mono.Cecil.Cil; using System; using System.IO; using System.Linq; using System.Reflection; -using System.Text.RegularExpressions; using System.Threading.Tasks; using UnityEngine; using static IPA.Logging.Logger; using MethodAttributes = Mono.Cecil.MethodAttributes; +#if NET3 +using Net3_Proxy; +using Path = Net3_Proxy.Path; +using Directory = Net3_Proxy.Directory; +#endif namespace IPA.Injector { @@ -276,7 +281,7 @@ namespace IPA.Injector pluginAsyncLoadTask.Wait(); permissionFixTask.Wait(); log.Debug("Plugins loaded"); - log.Debug(string.Join(", ", PluginLoader.PluginsMetadata)); + log.Debug(string.Join(", ", PluginLoader.PluginsMetadata.StrJP())); PluginComponent.Create(); } } diff --git a/IPA.Injector/PermissionFix.cs b/IPA.Injector/PermissionFix.cs index bd8c4723..58e9dadb 100644 --- a/IPA.Injector/PermissionFix.cs +++ b/IPA.Injector/PermissionFix.cs @@ -1,11 +1,11 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Security.AccessControl; using System.Security.Principal; -using System.Text; using System.Threading.Tasks; +#if NET3 +using Net3_Proxy; +#endif namespace IPA.Injector { @@ -13,9 +13,9 @@ namespace IPA.Injector { public static Task FixPermissions(DirectoryInfo root) { - if (!root.Exists) return Task.CompletedTask; + if (!root.Exists) return new Task(() => { }); - return Task.Run(() => + return Task.Factory.StartNew(() => { try { diff --git a/IPA.Injector/Updates.cs b/IPA.Injector/Updates.cs index 382f4f77..6ded9dc3 100644 --- a/IPA.Injector/Updates.cs +++ b/IPA.Injector/Updates.cs @@ -6,6 +6,12 @@ using System.IO; using System.Linq; using System.Reflection; using static IPA.Logging.Logger; +#if NET3 +using Net3_Proxy; +using Path = Net3_Proxy.Path; +using File = Net3_Proxy.File; +using Directory = Net3_Proxy.Directory; +#endif namespace IPA.Injector { @@ -33,7 +39,7 @@ namespace IPA.Injector { // will never actually be null FileName = path, - Arguments = $"\"-nw={Process.GetCurrentProcess().Id},s={string.Join(" ", Environment.GetCommandLineArgs().Skip(1)).Replace("\\", "\\\\").Replace(",", "\\,")}\"", + Arguments = $"\"-nw={Process.GetCurrentProcess().Id},s={string.Join(" ", Environment.GetCommandLineArgs().Skip(1).StrJP()).Replace("\\", "\\\\").Replace(",", "\\,")}\"", UseShellExecute = false }); diff --git a/IPA.Loader/Loader/PluginLoader.cs b/IPA.Loader/Loader/PluginLoader.cs index d30111f7..65ad6f89 100644 --- a/IPA.Loader/Loader/PluginLoader.cs +++ b/IPA.Loader/Loader/PluginLoader.cs @@ -15,6 +15,7 @@ using Version = SemVer.Version; using Net3_Proxy; using Path = Net3_Proxy.Path; using File = Net3_Proxy.File; +using Directory = Net3_Proxy.Directory; #endif namespace IPA.Loader @@ -276,7 +277,7 @@ namespace IPA.Loader if (meta.IsBare) { Logger.loader.Warn($"Bare manifest cannot specify description file"); - meta.Manifest.Description = string.Join("\n", lines.Skip(1)); // ignore first line + meta.Manifest.Description = string.Join("\n", lines.Skip(1).StrJP()); // ignore first line continue; } @@ -290,7 +291,7 @@ namespace IPA.Loader if (resc == null) { Logger.loader.Warn($"Could not find description file for plugin {meta.Name} ({name}); ignoring include"); - meta.Manifest.Description = string.Join("\n", lines.Skip(1)); // ignore first line + meta.Manifest.Description = string.Join("\n", lines.Skip(1).StrJP()); // ignore first line continue; } @@ -398,7 +399,7 @@ namespace IPA.Loader internal static void ComputeLoadOrder() { #if DEBUG - Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString()))); + Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString()).StrJP())); #endif bool InsertInto(HashSet root, PluginMetadata meta, bool isRoot = false) @@ -454,7 +455,7 @@ namespace IPA.Loader DeTree(PluginsMetadata, pluginTree); #if DEBUG - Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString()))); + Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString()).StrJP())); #endif } diff --git a/IPA.Loader/Loader/PluginManager.cs b/IPA.Loader/Loader/PluginManager.cs index 05d0d8e4..78aa4378 100644 --- a/IPA.Loader/Loader/PluginManager.cs +++ b/IPA.Loader/Loader/PluginManager.cs @@ -17,6 +17,7 @@ using static IPA.Loader.PluginLoader; using Net3_Proxy; using Path = Net3_Proxy.Path; using File = Net3_Proxy.File; +using Directory = Net3_Proxy.Directory; #endif namespace IPA.Loader @@ -472,7 +473,7 @@ namespace IPA.Loader catch (ReflectionTypeLoadException e) { Logger.loader.Error($"Could not load the following types from {Path.GetFileName(file)}:"); - Logger.loader.Error($" {string.Join("\n ", e.LoaderExceptions?.Select(e1 => e1?.Message) ?? new string[0])}"); + Logger.loader.Error($" {string.Join("\n ", e.LoaderExceptions?.Select(e1 => e1?.Message).StrJP() ?? new string[0])}"); } catch (Exception e) { diff --git a/IPA.Loader/Logging/StandardLogger.cs b/IPA.Loader/Logging/StandardLogger.cs index ea12a922..1f9566b0 100644 --- a/IPA.Loader/Logging/StandardLogger.cs +++ b/IPA.Loader/Logging/StandardLogger.cs @@ -215,7 +215,7 @@ namespace IPA.Logging if (lineNo == 0) { // no debug info var method = stackFrame.GetMethod(); - var paramString = string.Join(", ", method.GetParameters().Select(p => p.ParameterType.FullName)); + var paramString = string.Join(", ", method.GetParameters().Select(p => p.ParameterType.FullName).StrJP()); message = $"{{{method.DeclaringType?.FullName}::{method.Name}({paramString})}} {message}"; } diff --git a/IPA.Loader/Utilities/Utils.cs b/IPA.Loader/Utilities/Utils.cs index a9270b83..bad26ccd 100644 --- a/IPA.Loader/Utilities/Utils.cs +++ b/IPA.Loader/Utilities/Utils.cs @@ -1,6 +1,8 @@ using System; using System.IO; using System.Text; +using System.Linq; +using System.Collections.Generic; #if NET3 using File = Net3_Proxy.File; #endif @@ -185,5 +187,13 @@ namespace IPA.Utilities cmpVal = l.Patch - r.Patch; return cmpVal; } + +#if NET4 + internal static IEnumerable StrJP(this IEnumerable a) => a; +#endif +#if NET3 + internal static string[] StrJP(this IEnumerable a) => a.ToArray(); + internal static string[] StrJP(this IEnumerable a) => a.Select(o => $"{o}" /* safer than .ToString() */).ToArray(); +#endif } } diff --git a/Net3-Proxy/Directory.cs b/Net3-Proxy/Directory.cs new file mode 100644 index 00000000..4329fed9 --- /dev/null +++ b/Net3-Proxy/Directory.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.IO; +using System.Security.AccessControl; +using OgDir = System.IO.Directory; + +namespace Net3_Proxy +{ + public static class Directory + { + public static void Move(string f, string t) => OgDir.Move(f, t); + public static string[] GetFiles(string d) => OgDir.GetFiles(d); + public static string[] GetFiles(string d, string s) => OgDir.GetFiles(d, s); + public static string[] GetFiles(string d, string s, SearchOption o) => OgDir.GetFiles(d, s, o); + public static string[] GetDirectories(string d) => OgDir.GetDirectories(d); + public static string[] GetDirectories(string d, string s) => OgDir.GetDirectories(d, s); + public static string[] GetDirectories(string d, string s, SearchOption o) => OgDir.GetDirectories(d, s, o); + public static bool Exists(string d) => OgDir.Exists(d); + public static void Delete(string d) => OgDir.Delete(d); + public static void Delete(string d, bool r) => OgDir.Delete(d, r); + public static void CreateDirectory(string d) => OgDir.CreateDirectory(d); + public static void CreateDirectory(string d, DirectorySecurity s) => OgDir.CreateDirectory(d, s); + public static IEnumerable EnumerateFiles(string d) => GetFiles(d); + public static IEnumerable EnumerateFiles(string d, string s) => GetFiles(d, s); + public static IEnumerable EnumerateFiles(string d, string s, SearchOption o) => GetFiles(d, s, o); + public static IEnumerable EnumerateDirectories(string d) => GetDirectories(d); + public static IEnumerable EnumerateDirectories(string d, string s) => GetDirectories(d, s); + public static IEnumerable EnumerateDirectories(string d, string s, SearchOption o) => GetDirectories(d, s, o); + } +} diff --git a/Net3-Proxy/Extensions.cs b/Net3-Proxy/Extensions.cs index 64386279..ac1cc0bf 100644 --- a/Net3-Proxy/Extensions.cs +++ b/Net3-Proxy/Extensions.cs @@ -24,6 +24,13 @@ namespace Net3_Proxy public static StringBuilder Clear(this StringBuilder sb) => sb.Remove(0, sb.Length); + + public static bool HasFlag(this E e, E o) where E : Enum + { + var ei = Convert.ToUInt64(e); + var oi = Convert.ToUInt64(o); + return (ei & oi) == oi; + } } public static class DirectoryInfoExtensions diff --git a/Net3-Proxy/File.cs b/Net3-Proxy/File.cs index abd23c19..6b4c0060 100644 --- a/Net3-Proxy/File.cs +++ b/Net3-Proxy/File.cs @@ -23,5 +23,6 @@ namespace Net3_Proxy textWriter.WriteLine(value); } public static string[] ReadAllLines(string p) => OgFile.ReadAllLines(p); + public static FileStream OpenRead(string p) => OgFile.OpenRead(p); } } diff --git a/Net3-Proxy/Net3-Proxy.csproj b/Net3-Proxy/Net3-Proxy.csproj index e05dcfd2..c46d4293 100644 --- a/Net3-Proxy/Net3-Proxy.csproj +++ b/Net3-Proxy/Net3-Proxy.csproj @@ -39,6 +39,7 @@ + diff --git a/Net3-Proxy/Path.cs b/Net3-Proxy/Path.cs index 97a2a235..e645bc6b 100644 --- a/Net3-Proxy/Path.cs +++ b/Net3-Proxy/Path.cs @@ -52,5 +52,6 @@ namespace Net3_Proxy begin = Combine(begin, p); return begin; } + public static char PathSeparator => OgPath.PathSeparator; } }