Esempio n. 1
0
        /// <summary>
        /// Compiles a mod from a managed assembly.
        /// </summary>
        /// <param name="assemblyPath">The path to the assembly file, either relative to the working directory or an absolute path.</param>
        /// <returns>The output of the compiler.</returns>
        public CompilerOutput CompileFromAssembly(string assemblyPath)
        {
            for (int i = 0; i < Loggers.Count; i++)
                Loggers[i].compiler_wr = new WeakReference<ModCompiler>(this);

            assemblyPath = Environment.ExpandEnvironmentVariables(assemblyPath);

            Log("Compiling assembly from path " + assemblyPath, MessageImportance.Normal);

            building = new ModData(this);

            building.OriginPath = assemblyPath;
            building.OriginName = Path.GetFileNameWithoutExtension(assemblyPath);

            if (!BeginCompile(assemblyPath))
            {
                Exception cause;
                LogError(cause = new CompilerException("Mod already building!"));

                CompilerOutput o;
                LogResult(o = CreateOutput(new List<CompilerError>()
                {
                    new CompilerError(building)
                    {
                        Cause = cause,
                        FilePath = assemblyPath,
                        IsWarning = true,
                        Message = "The mod is already being built."
                    }
                }));

                return o;
            }

            try
            {
                #region check if file exists
                if (!File.Exists(assemblyPath))
                {
                    CompilerOutput o;
                    LogResult(o = new CompilerOutput()
                    {
                        Succeeded = false,
                        errors = new List<CompilerError>()
                        {
                            new CompilerError(building)
                            {
                                Cause = new FileNotFoundException("File '" + assemblyPath + "' not found."),
                                Message = "The assembly '" + assemblyPath + "' was not found."
                            }
                        }
                    });

                    return o;
                }
                #endregion

                Assembly asm;

                try
                {
                    asm = Assembly.LoadFile(assemblyPath);
                }
                #region check if assembly is valid
                catch (BadImageFormatException e)
                // if (e is BadImageFormatException || e is DllNotFoundException || e is InvalidProgramException || e is TypeLoadException)
                {
                    LogError(e);

                    CompilerOutput o;
                    LogResult(o = new CompilerOutput()
                    {
                        Succeeded = false,
                        errors = new List<CompilerError>()
                        {
                            new CompilerError(building)
                            {
                                Cause = e,
                                Message = "The assembly is not a manged assembly -or- is not compiled with the x86 architecture.",
                                FilePath = assemblyPath
                            }
                        }
                    });

                    return o;
                }
                catch (Exception e)
                {
                    LogError(e);

                    CompilerOutput o;
                    LogResult(o = new CompilerOutput()
                    {
                        Succeeded = false,
                        errors = new List<CompilerError>()
                        {
                            new CompilerError(building)
                            {
                                Cause = e,
                                Message = "The assembly could not be loaded.",
                                FilePath = assemblyPath
                            }
                        }
                    });

                    return o;
                }
                #endregion

                return CompileFromAssembly(asm);
            }
            catch (Exception e)
            {
                LogError(e, "Unexpected error.");

                EndCompile(assemblyPath);

                CompilerOutput o;
                LogResult(o = CreateOutput(new List<CompilerError>()
                {
                    new CompilerError(building)
                    {
                        Cause = e,
                        FilePath = assemblyPath,
                        IsWarning = false,
                        Message = "An unexpected error occured while compiling."
                    }
                }));

                return o;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Compiles a mod from a managed assembly.
        /// </summary>
        /// <param name="asm">The assembly to compile.</param>
        /// <returns>The output of the compiler.</returns>
        public CompilerOutput CompileFromAssembly(Assembly asm)
        {
            for (int i = 0; i < Loggers.Count; i++)
                Loggers[i].compiler_wr = new WeakReference<ModCompiler>(this);

            Log("Compiling assembly " + asm, MessageImportance.Normal);

            if (building == null)
            {
                building = new ModData(this);

                building.OriginPath = asm.Location;
                building.OriginName = Path.GetFileNameWithoutExtension(asm.Location);
            }

            if (!BeginCompile(asm.Location))
            {
                Exception cause;
                LogError(cause = new CompilerException("Mod already building!"));

                CompilerOutput o;
                LogResult(o = CreateOutput(new List<CompilerError>()
                {
                    new CompilerError(building)
                    {
                        Cause = cause,
                        FilePath = asm.Location,
                        IsWarning = true,
                        Message = "The mod is already being built."
                    }
                }));

                return o;
            }

            try
            {
                CompilerOutput outp;

                Log("Extracting files.", MessageImportance.High);

                var extracted = new Extractor(this).ExtractData(asm);
                outp = CreateOutput(extracted.Item3);
                if (!outp.Succeeded)
                {
                    LogResult(outp);
                    EndCompile(asm.Location);
                    return outp;
                }

                Log("Validating JSONs.", MessageImportance.High);

                outp = Validate(extracted.Item1, extracted.Item2, true);
                if (!outp.Succeeded)
                {
                    LogResult(outp);
                    EndCompile(asm.Location);
                    return outp;
                }

                return MainCompileStuff(building, asm);
            }
            catch (Exception e)
            {
                EndCompile(asm.Location);

                LogError(e, "Unexpected error.");

                CompilerOutput o;
                LogResult(o = CreateOutput(new List<CompilerError>()
                {
                    new CompilerError(building)
                    {
                        Cause = e,
                        FilePath = asm.Location,
                        IsWarning = false,
                        Message = "An unexpected error occured while compiling."
                    }
                }));

                return o;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Compiles a mod from its source folder.
        /// </summary>
        /// <param name="folder">The mod's folder. Either an absolute path,
        /// relative to the working directory or the name of a folder in the Mods\Sources folder</param>
        /// <returns>The output of the compiler.</returns>
        public CompilerOutput CompileFromSource  (string folder)
        {
            for (int i = 0; i < Loggers.Count; i++)
                Loggers[i].compiler_wr = new WeakReference<ModCompiler>(this);

            folder = Environment.ExpandEnvironmentVariables(folder);

            if (folder.EndsWith("\\"))
                folder = folder.Remove(folder.Length - 1);

            if (folder[1] != ':' && !folder.StartsWith("\\")) // <drive>:\path or \\ServerName\path
                // if the folder doesn't exist, it's maybe a folder in the Mods\Sources directory?
                if (!Directory.Exists(folder))
                    folder = Mods.pathSources + "\\" + folder;
                else
                    folder = Path.GetFullPath(folder);

            Log("Compiling source " + folder, MessageImportance.Normal);

            building = new ModData(this);

            try
            {
                building.OriginPath = folder;
                building.OriginName = Path.GetFileName(folder);

                if (!BeginCompile(folder))
                {
                    Exception cause;
                    LogError(cause = new CompilerException("Mod already building!"));

                    CompilerOutput o;
                    LogResult(o = CreateOutput(new List<CompilerError>()
                    {
                        new CompilerError(building)
                        {
                            Cause = cause,
                            FilePath = folder,
                            IsWarning = true,
                            Message = "The mod is already being built."
                        }
                    }));

                    return o;
                }

                #region check if folder exists
                if (!Directory.Exists(folder))
                {
                    EndCompile(folder);

                    Exception cause;
                    LogError(cause = new DirectoryNotFoundException("Directory '" + folder + "' not found."));

                    CompilerOutput o;
                    LogResult(o = new CompilerOutput()
                    {
                        Succeeded = false,
                        errors = new List<CompilerError>()
                        {
                            new CompilerError(building)
                            {
                                Cause = cause,
                                Message = "The mod directory (" + folder + ") was not found",
                                IsWarning = false,
                                FilePath = folder
                            }
                        }
                    });

                    return o;
                }
                #endregion

                CompilerOutput outp;

                Log("Loading files.", MessageImportance.High);

                var readFiles = new FileLoader(this).LoadFiles(folder);
                outp = CreateOutput(readFiles.Item3);
                if (!outp.Succeeded)
                {
                    LogResult (outp  );
                    EndCompile(folder);
                    return outp;
                }

                Log("Validating JSONs.", MessageImportance.High);

                outp = Validate(readFiles.Item1, readFiles.Item2, true);
                if (!outp.Succeeded)
                {
                    LogResult (outp  );
                    EndCompile(folder);
                    return outp;
                }

                Log("Building sources.", MessageImportance.High);

                var compiled = new Builder(this).Build();
                outp = CreateOutput(compiled.Item3);
                outp.Succeeded &= compiled.Item1 != null;
                if (!outp.Succeeded)
                {
                    LogResult (outp  );
                    EndCompile(folder);
                    return outp;
                }

                return MainCompileStuff(building, compiled.Item1);
            }
            catch (Exception e)
            {
                LogError(e, "Unexpected error.");

                EndCompile(folder);

                CompilerOutput o;
                LogResult(o = CreateOutput(new List<CompilerError>()
                {
                    new CompilerError(building)
                    {
                        Cause     = e,
                        FilePath  = folder,
                        IsWarning = false,
                        Message   = "An unexpected error occured while compiling."
                    }
                }));

                return o;
            }
        }