예제 #1
0
 private static void InjectionDetectorTargetCompatibleCheck()
 {
     if (!IsInjectionDetectorTargetCompatible())
     {
         if (!File.Exists(ACTkEditorGlobalStuff.injectionDataPath))
         {
             return;
         }
         Debug.LogWarning(ACTkEditorGlobalStuff.LogPrefix + "Injection Detector is not available on selected platform (" + EditorUserBuildSettings.activeBuildTarget + ") and will be disabled!");
         ACTkEditorGlobalStuff.CleanInjectionDetectorData();
     }
 }
예제 #2
0
        internal static void InjectionAssembliesScan(bool forced)
        {
            if (!IsInjectionDetectorTargetCompatible() && !forced)
            {
                InjectionDetectorTargetCompatibleCheck();
                return;
            }

#if (ACTK_DEBUG || ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
            Stopwatch sw = Stopwatch.StartNew();
#if (ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
            sw.Stop();
            Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Injection Detector Assemblies Scan\n");
            Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Paths:\n" +

                      "Assets: " + ACTkEditorGlobalStuff.ASSETS_PATH + "\n" +
                      "Assemblies: " + ACTkEditorGlobalStuff.ASSEMBLIES_PATH + "\n" +
                      "Injection Detector Data: " + ACTkEditorGlobalStuff.INJECTION_DATA_PATH);
            sw.Start();
#endif
#endif

#if (ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
            sw.Stop();
            Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Looking for all assemblies in current project...");
            sw.Start();
#endif
            allLibraries.Clear();
            allowedAssemblies.Clear();

            allLibraries.AddRange(ACTkEditorGlobalStuff.FindLibrariesAt(ACTkEditorGlobalStuff.assetsPath));
            allLibraries.AddRange(ACTkEditorGlobalStuff.FindLibrariesAt(ACTkEditorGlobalStuff.assembliesPath));
#if (ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
            sw.Stop();
            Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Total libraries found: " + allLibraries.Count);
            sw.Start();
#endif
            const string editorSubdir            = "/editor/";
            var          assembliesPathLowerCase = ACTkEditorGlobalStuff.AssembliesPathRelative.ToLower();
            foreach (var libraryPath in allLibraries)
            {
                var libraryPathLowerCase = libraryPath.ToLower();
#if (ACTK_DEBUG_PARANIOD)
                sw.Stop();
                Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Checking library at the path: " + libraryPathLowerCase);
                sw.Start();
#endif
                if (libraryPathLowerCase.Contains(editorSubdir))
                {
                    continue;
                }
                if (libraryPathLowerCase.Contains("-editor.dll") && libraryPathLowerCase.Contains(assembliesPathLowerCase))
                {
                    continue;
                }

                try
                {
                    var assName = AssemblyName.GetAssemblyName(libraryPath);
                    var name    = assName.Name;
                    var hash    = ACTkEditorGlobalStuff.GetAssemblyHash(assName);

                    var allowed = allowedAssemblies.FirstOrDefault(allowedAssembly => allowedAssembly.name == name);

                    if (allowed != null)
                    {
                        allowed.AddHash(hash);
                    }
                    else
                    {
                        allowed = new AllowedAssembly(name, new[] { hash });
                        allowedAssemblies.Add(allowed);
                    }
                }
                catch
                {
                    // not a valid IL assembly, skipping
                }
            }

#if (ACTK_DEBUG || ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
            sw.Stop();
            string trace = ACTkEditorGlobalStuff.LogPrefix + "Found assemblies (" + allowedAssemblies.Count + "):\n";

            foreach (AllowedAssembly allowedAssembly in allowedAssemblies)
            {
                trace += "  Name: " + allowedAssembly.name + "\n";
                trace  = allowedAssembly.hashes.Aggregate(trace, (current, hash) => current + ("    Hash: " + hash + "\n"));
            }

            Debug.Log(trace);
            sw.Start();
#endif
            if (!Directory.Exists(ACTkEditorGlobalStuff.resourcesPath))
            {
#if (ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
                sw.Stop();
                Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Creating resources folder: " + ACTkEditorGlobalStuff.RESOURCES_PATH);
                sw.Start();
#endif
                Directory.CreateDirectory(ACTkEditorGlobalStuff.resourcesPath);
            }

            ACTkEditorGlobalStuff.RemoveReadOnlyAttribute(ACTkEditorGlobalStuff.injectionDataPath);
            var bw = new BinaryWriter(new FileStream(ACTkEditorGlobalStuff.injectionDataPath, FileMode.Create, FileAccess.Write, FileShare.Read));
            var allowedAssembliesCount = allowedAssemblies.Count;

            int totalWhitelistedAssemblies;

#if (ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
            sw.Stop();
            Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Processing default whitelist");
            sw.Start();
#endif

            var defaultWhitelistPath = ACTkEditorGlobalStuff.ResolveInjectionDefaultWhitelistPath();
            if (File.Exists(defaultWhitelistPath))
            {
                var br = new BinaryReader(new FileStream(defaultWhitelistPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                var assembliesCount = br.ReadInt32();
                totalWhitelistedAssemblies = assembliesCount + allowedAssembliesCount;

                bw.Write(totalWhitelistedAssemblies);

                for (var i = 0; i < assembliesCount; i++)
                {
                    bw.Write(br.ReadString());
                }
                br.Close();
            }
            else
            {
#if (ACTK_DEBUG || ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
                sw.Stop();
#endif
                bw.Close();
                Debug.LogError(ACTkEditorGlobalStuff.LogPrefix + "Can't find " + ACTkEditorGlobalStuff.InjectionDefaultWhitelistFile + " file!\nPlease, report to " + ACTkEditorGlobalStuff.ReportEmail);
                return;
            }

#if (ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
            sw.Stop();
            Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Processing user whitelist");
            sw.Start();
#endif

            var userWhitelistPath = ACTkEditorGlobalStuff.ResolveInjectionUserWhitelistPath();
            if (File.Exists(userWhitelistPath))
            {
                var br = new BinaryReader(new FileStream(userWhitelistPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                var assembliesCount = br.ReadInt32();

                bw.Seek(0, SeekOrigin.Begin);
                bw.Write(totalWhitelistedAssemblies + assembliesCount);
                bw.Seek(0, SeekOrigin.End);
                for (var i = 0; i < assembliesCount; i++)
                {
                    bw.Write(br.ReadString());
                }
                br.Close();
            }

#if (ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
            sw.Stop();
            Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Processing project assemblies");
            sw.Start();
#endif

            for (var i = 0; i < allowedAssembliesCount; i++)
            {
                var assembly = allowedAssemblies[i];
                var name     = assembly.name;
                var hashes   = "";

                for (var j = 0; j < assembly.hashes.Length; j++)
                {
                    hashes += assembly.hashes[j];
                    if (j < assembly.hashes.Length - 1)
                    {
                        hashes += ACTkEditorGlobalStuff.InjectionDataSeparator;
                    }
                }

                var line = ObscuredString.EncryptDecrypt(name + ACTkEditorGlobalStuff.InjectionDataSeparator + hashes, "Elina");

#if (ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
                Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Writing assembly:\n" + name + ACTkEditorGlobalStuff.INJECTION_DATA_SEPARATOR + hashes);
#endif
                bw.Write(line);
            }

            bw.Close();
#if (ACTK_DEBUG || ACTK_DEBUG_VERBOSE || ACTK_DEBUG_PARANIOD)
            sw.Stop();
            Debug.Log(ACTkEditorGlobalStuff.LogPrefix + "Assemblies scan duration: " + sw.ElapsedMilliseconds + " ms.");
#endif

            if (allowedAssembliesCount == 0)
            {
                Debug.LogError(ACTkEditorGlobalStuff.LogPrefix + "Can't find any assemblies!\nPlease, report to " + ACTkEditorGlobalStuff.ReportEmail);
            }

            AssetDatabase.Refresh();
            //EditorApplication.UnlockReloadAssemblies();
        }