Esempio n. 1
0
        /// <summary>
        /// Compiles the mod
        /// </summary>
        /// <returns></returns>
        public bool Compile()
        {
            // Compile
            if (m_compile)
            {
                if (m_type == WorkshopType.Mod)
                {
                    MySandboxGame.Log.WriteLineAndConsole("Compiling...");
                    var mod = WorkshopHelper.GetContext(m_modPath, m_workshopItems.GetValueOrDefault(m_modId.FirstOrDefault()), m_modId, m_title);
                    if (WorkshopHelper.LoadScripts(m_modPath, mod))
                    {
                        // Process any errors
                        var errors = WorkshopHelper.GetErrors();

                        if (errors.Count > 0)
                        {
                            int errorCount   = 0;
                            int warningCount = 0;

                            // This is not efficient, but I'm lazy
                            foreach (var error in errors)
                            {
                                if (error.Severity >= TErrorSeverity.Error)
                                {
                                    errorCount++;
                                }
                                if (error.Severity == TErrorSeverity.Warning)
                                {
                                    warningCount++;
                                }
                            }

                            if (errorCount > 0)
                            {
                                MySandboxGame.Log.WriteLineError(string.Format("There are {0} compile errors:", errorCount));
                            }
                            if (warningCount > 0)
                            {
                                MySandboxGame.Log.WriteLineWarning(string.Format("There are {0} compile warnings:", warningCount));
                            }

                            // Output raw message, which is usually in msbuild friendly format, for automated tools
                            foreach (var error in errors)
                            {
                                var color = error.Severity == TErrorSeverity.Warning ? ConsoleColor.Yellow : ConsoleColor.Red;
                                ProgramBase.ConsoleWriteColored(color, () =>
                                                                System.Console.Error.WriteLine(error.GetErrorText()));
                            }

                            WorkshopHelper.ClearErrors();

                            if (errorCount > 0)
                            {
                                MySandboxGame.Log.WriteLineError("Compilation FAILED!");
                                return(false);
                            }
                        }
                        MySandboxGame.Log.WriteLineAndConsole("Compilation successful!");
                    }
                }
#if SE
                else if (m_type == WorkshopType.IngameScript)
                {
                    MySandboxGame.Log.WriteLineAndConsole("Compiling...");
                    // Load the ingame script from the disk
                    // I don't like this, but meh
                    var input   = new StreamReader(Path.Combine(m_modPath, "Script.cs"));
                    var program = input.ReadToEnd();
                    input.Close();
                    var scripts = new List <Script>();
                    scripts.Add(MyScriptCompiler.Static.GetIngameScript(program, "Program", typeof(Sandbox.ModAPI.Ingame.MyGridProgram).Name, "sealed partial"));

                    var messages = new List <Message>();
                    var assembly = MyVRage.Platform.Scripting.CompileIngameScriptAsync(Path.Combine(VRage.FileSystem.MyFileSystem.UserDataPath, "SEWT-Script - " + Path.GetFileName(m_modPath)), program, out messages, "SEWT Compiled PB Script", "Program", typeof(Sandbox.ModAPI.Ingame.MyGridProgram).Name).Result;

                    if (messages.Count > 0)
                    {
                        MySandboxGame.Log.WriteLineAndConsole(string.Format("There are {0} compile messages:", messages.Count));
                        int errors = 0;
                        foreach (var msg in messages)
                        {
                            var color = msg.IsError ? ConsoleColor.Red : ConsoleColor.Gray;
                            ProgramBase.ConsoleWriteColored(color, () =>
                                                            MySandboxGame.Log.WriteLineAndConsole(msg.Text));

                            if (msg.IsError)
                            {
                                errors++;
                            }
                        }
                        if (errors > 0)
                        {
                            MySandboxGame.Log.WriteLineError("Compilation FAILED!");
                            return(false);
                        }
                    }

                    if (assembly == null)
                    {
                        // How can this happen?
                        MySandboxGame.Log.WriteLineError("Compilation FAILED!");
                        return(false);
                    }

                    MySandboxGame.Log.WriteLineAndConsole("Compilation successful!");
                }
#endif
                return(true);
            }
            return(true);
        }
Esempio n. 2
0
        public virtual int InitGame(string[] args)
        {
            var options = new Options();
            var parser  = new CommandLine.Parser(with => with.HelpWriter = Console.Error);

            if (parser.ParseArgumentsStrict(args, options, HandleInputError))
            {
                if (options.ModPaths == null &&
                    options.Blueprints == null &&
#if SE
                    options.IngameScripts == null &&
#endif
                    options.Scenarios == null &&
                    options.Worlds == null &&
                    options.Collections == null)
                {
                    if (!options.ClearSteamCloud && !options.ListDLCs)
                    {
                        Console.WriteLine(CommandLine.Text.HelpText.AutoBuild(options).ToString());
                        return(Cleanup(1));
                    }
                }

                // If a "0" or "none" was specified for DLC, that means remove them all.
                if (options.DLCs?.Length > 0 &&
                    (options.DLCs.Contains("0") || options.DLCs.Contains("none", StringComparer.InvariantCultureIgnoreCase)))
                {
                    options.DLCs = new string[0];
                }

                // If a 0 was specified for dependencies, that means remove them all.
                if (options.Dependencies?.Length > 0 && options.Dependencies.Contains((ulong)0))
                {
                    options.Dependencies = new ulong[0];
                }

                // SE requires -appdata, but the commandline dll requires --appdata, so fix it
                for (var idx = 0; idx < args.Length; idx++)
                {
                    if (string.Compare(args[idx], "--appdata", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        args[idx] = "-appdata";
                    }
                }

                m_useModIO = options.ModIO;
                try
                {
                    // Initialize game code
                    InitSandbox(args);
                }
                catch (Exception ex)
                {
                    ex.Log("ERROR: An exception occurred intializing game libraries: ");
                    return(Cleanup(2));
                }

                if (!SteamAPI.IsSteamRunning())
                {
                    MySandboxGame.Log.WriteLineAndConsole("ERROR: * Steam not detected. Is Steam running and not as Admin? *");
                    MySandboxGame.Log.WriteLineAndConsole("* Only compile testing is available. *");
                    MySandboxGame.Log.WriteLineAndConsole("");

                    if (options.Download)
                    {
                        return(Cleanup(3));
                    }

                    options.Upload = false;
                }

                MySandboxGame.Log.WriteLineAndConsole($"{AppName} {Assembly.GetExecutingAssembly().GetName().Version}");

                ProgramBase.CheckForUpdate(MySandboxGame.Log.WriteLineAndConsole);

                MySandboxGame.Log.WriteLineToConsole(string.Empty);
                MySandboxGame.Log.WriteLineAndConsole($"Log file: {MySandboxGame.Log.GetFilePath()}");
                MySandboxGame.Log.WriteLineToConsole(string.Empty);

                // Make sure file paths are properly rooted based on the user's current directory at launch
                MySandboxGame.Log.WriteLineAndConsole($"Relative root: {LaunchDirectory}");

#if SE
                ParameterInfo[] parameters;
                if (options.Compile)
                {
                    // Init ModAPI
                    var initmethod = typeof(MySandboxGame).GetMethod("InitModAPI", BindingFlags.Instance | BindingFlags.NonPublic);
                    MyDebug.AssertRelease(initmethod != null);

                    if (initmethod != null)
                    {
                        parameters = initmethod.GetParameters();
                        MyDebug.AssertRelease(parameters.Count() == 0);

                        if (!(parameters.Count() == 0))
                        {
                            initmethod = null;
                        }
                    }

                    if (initmethod != null)
                    {
                        initmethod.Invoke(m_game, null);
                    }
                    else
                    {
                        MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "InitModAPI"));
                    }
                }
#endif
                ReplaceMethods();

                System.Threading.Tasks.Task <bool> Task;

                if (options.Download)
                {
                    Task = DownloadMods(options);
                }
                else if (options.ClearSteamCloud)
                {
                    Task = ClearSteamCloud(options.DeleteSteamCloudFiles, options.Force);
                }
                else if (options.ListDLCs)
                {
                    Task = System.Threading.Tasks.Task <bool> .Factory.StartNew(() => { ListDLCs(); return(true); });
                }
                else
                {
                    Task = UploadMods(options);
                }

                try
                {
                    // Wait for file transfers to finish (separate thread)
                    while (!Task.Wait(100))
                    {
                        MyGameService.Update();
                    }
                }
                catch (AggregateException ex)
                {
                    MyDebug.AssertRelease(Task.IsFaulted);
                    MyDebug.AssertRelease(ex.InnerException != null);
                    ex.InnerException.Log();
                    return(Cleanup(4));
                }
                catch (Exception ex)
                {
                    ex.Log();
                    return(Cleanup(5));
                }

                // If the task reported any error, return exit code
                if (!Task.Result)
                {
                    return(Cleanup(-1));
                }
            }

            return(Cleanup());
        }