Example #1
0
        static App()
        {
            var runningCount = System.Diagnostics.Process.GetProcesses().Count(p => p.ProcessName == "FileSaver");

            if (runningCount > 1)
            {
                Environment.Exit(1);
            }
            EmbeddedAssemblyResolver.HookResolve();
        }
Example #2
0
        internal static int Main(string[] args)
        {
            string oldDirectory = Directory.GetCurrentDirectory();

            try
            {
                AdjustCurrentDirectory();
                EmbeddedAssemblyResolver.Init((name) => Logger.WriteLine("Loaded assembly {0}", name.Name),
                                              (name) => Logger.WriteLine("Could not find assembly {0}", name.Name));
                Logger.WriteLine("Started process as {0}", Environment.Is64BitProcess ? "x64" : "x86");
                Options options = Options.TryParse(args, Logger);
                if (options == null)
                {
                    return((int)ProcessCode.InvalidArguments);
                }

                string targetFilePath = options.EncryptionTargetPath;
                bool   useArchive     = IsDirectory(options.EncryptionTargetPath);

                using (var tempFiles = new TempFileCollection())
                {
                    if (useArchive)
                    {
                        targetFilePath = Combine(AppDomain.CurrentDomain.BaseDirectory, $"{GetRandomFileName()}.zip");
                        tempFiles.AddFile(targetFilePath, false);
                        ZipFile.CreateFromDirectory(options.EncryptionTargetPath, targetFilePath, CompressionLevel.NoCompression, false);
                    }

                    using (var compiler = new SelfExtractorCompiler(new EncryptFileTransformation(options.PublicKey))
                                          .IncludingFiles(targetFilePath))
                    {
                        string path = compiler.Compile(options.OutputAssemblyPath);

                        Logger.WriteLine($"Self extractable executable was generated at {path} using key {options.PublicKey}");
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLine(ex);
                return((int)ProcessCode.Failed);
            }
            finally
            {
                Directory.SetCurrentDirectory(oldDirectory);
            }
            return((int)ProcessCode.Success);
        }
Example #3
0
        protected ModuleDefinition ReadModule(string modulePath, Version version)
        {
            taskInterface.SetStatus("Loading " + Path.GetFileName(modulePath));
            var resolver = new EmbeddedAssemblyResolver();
            var module   = ModuleDefinition.ReadModule(modulePath,
                                                       new ReaderParameters {
                AssemblyResolver = resolver
            });

            resolver.baseModule = module;

            if (module.Assembly.Name.Version != version)
            {
                throw new Exception($"{module.Assembly.Name.Name} version {module.Assembly.Name.Version}. Expected {version}");
            }

            return(module);
        }
Example #4
0
        public static ModuleDef Embed(ModuleDef module, ILCombinerDependency[] dependencies, CombinerMethod method)
        {
            ICombiner combiner;

            switch (method)
            {
            case CombinerMethod.EmbeddedAssemblyResolver:
                combiner = new EmbeddedAssemblyResolver();
                break;

            case CombinerMethod.AssemblyMerger:
                combiner = new AssemblyMerger();
                break;

            default:
                throw new ILCombinerException("Unknown or unsupported method!");
            }

            return(combiner.Combine(module, dependencies));
        }
Example #5
0
        internal void Encrypt(string target, string keyPath, string outputPath)
        {
            try
            {
                EmbeddedAssemblyResolver.Init((name) => { }, (name) => { });
                string publicKey = GetPublicKey(keyPath);

                using (var tempFiles = new TempFileCollection())
                {
                    using (var compiler = new SelfExtractorCompiler(new EncryptFileTransformation(publicKey))
                                          .IncludingFiles(target))
                    {
                        string path = compiler.Compile(outputPath);
                        logInformational($"Self extractable executable was generated at {path} using key {publicKey}");
                    }
                }
            }
            catch (Exception ex)
            {
                logError($"Error during exncryption: {ex.Message}");
            }
        }
Example #6
0
 public App()
 {
     EmbeddedAssemblyResolver.Init((name) => SafeWriteLine($"Loaded assembly {name.Name}", name.Name),
                                   (name) => SafeWriteLine($"Could not find assembly {name.Name}", name.Name));
 }
Example #7
0
 private static void Init()
 {
     EmbeddedAssemblyResolver.Init((name) => Logger.Debug("Loaded assembly {0}", name.Name),
                                   (name) => Logger.Debug("Could not find assembly {0}", name.Name));
     AppDomain.CurrentDomain.UnhandledException += (s, a) => Logger.Error("Unhandled Exception: " + a.ExceptionObject.ToString()); //TODO Application.ThreadException += (s, a) => LogException(a.Exception);
 }
Example #8
0
        public override void Run()
        {
            taskInterface.SetStatus("Deleting Old Src");

            if (Directory.Exists(FullSrcDir))
            {
                Directory.Delete(FullSrcDir, true);
            }

            var resolver   = new EmbeddedAssemblyResolver();
            var readParams = new ReaderParameters()
            {
                AssemblyResolver = resolver
            };

            taskInterface.CancellationToken().ThrowIfCancellationRequested();
            taskInterface.SetStatus("Loading Terraria.exe");
            clientModule = ModuleDefinition.ReadModule(TerrariaPath, readParams);

            taskInterface.CancellationToken().ThrowIfCancellationRequested();
            taskInterface.SetStatus("Loading TerrariaServer.exe");
            serverModule = ModuleDefinition.ReadModule(TerrariaServerPath, readParams);

            resolver.baseModule = clientModule;

            VersionCheck(clientModule.Assembly);
            VersionCheckAlt(serverModule.Assembly);

            var options = new DecompilationOptions {
                FullDecompilation      = true,
                CancellationToken      = taskInterface.CancellationToken(),
                SaveAsProjectDirectory = FullSrcDir
            };

            var clientSources   = GetCodeFiles(clientModule, options).ToList();
            var serverSources   = GetCodeFiles(serverModule, options).ToList();
            var clientResources = GetResourceFiles(clientModule, options).ToList();
            var serverResources = GetResourceFiles(serverModule, options).ToList();

            var sources   = CombineFiles(clientSources, serverSources, src => src.Key);
            var resources = CombineFiles(clientResources, serverResources, res => res.Item1);

            var items = new List <WorkItem>();

            items.AddRange(sources.Select(src => new WorkItem(
                                              "Decompiling: " + src.Key, () => DecompileSourceFile(src, options))));

            items.AddRange(resources.Select(res => new WorkItem(
                                                "Extracting: " + res.Item1, () => ExtractResource(res, options))));

            items.Add(new WorkItem("Writing Assembly Info",
                                   () => WriteAssemblyInfo(clientModule, options)));

            items.Add(new WorkItem("Writing Terraria" + lang.ProjectFileExtension,
                                   () => WriteProjectFile(clientModule, clientGuid, clientSources, clientResources, options)));

            items.Add(new WorkItem("Writing TerrariaServer" + lang.ProjectFileExtension,
                                   () => WriteProjectFile(serverModule, serverGuid, serverSources, serverResources, options)));

            items.Add(new WorkItem("Writing Terraria" + lang.ProjectFileExtension + ".user",
                                   () => WriteProjectUserFile(clientModule, SteamDir.Get(), options)));

            items.Add(new WorkItem("Writing TerrariaServer" + lang.ProjectFileExtension + ".user",
                                   () => WriteProjectUserFile(serverModule, SteamDir.Get(), options)));

            ExecuteParallel(items, maxDegree: SingleDecompileThread.Get() ? 1 : 0);
        }