/// <summary> /// Compiles the mod /// </summary> /// <returns></returns> public bool Compile() { // Compile if (m_compile) { if (m_type == WorkshopType.Mod) { if (_compileMethod != null) { MySandboxGame.Log.WriteLineAndConsole("Compiling..."); #if SE var mod = new MyModContext(); mod.Init(m_title, null, m_modPath); #else var workshopItem = new MyLocalWorkshopItem(new VRage.ObjectBuilders.SerializableModReference(Path.GetFileName(m_modPath), 0)); var mod = new MyModContext(workshopItem, 0); #endif _compileMethod( #if SE m_modPath, #endif mod ); // Process any errors #if SE var errors = MyDefinitionErrors.GetErrors(); #else var compileMessages = _scriptManager.GetType().GetField("m_messages", BindingFlags.NonPublic | BindingFlags.Instance); var errors = (compileMessages.GetValue(_scriptManager) as List <MyScriptCompiler.Message>) ?? new List <MyScriptCompiler.Message>(); #endif 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.WriteLineAndConsole(string.Format("There are {0} compile errors:", errorCount)); } if (warningCount > 0) { MySandboxGame.Log.WriteLineAndConsole(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) #if SE { System.Console.WriteLine(error.Message); } #else { System.Console.WriteLine(error.Text); } #endif #if SE MyDefinitionErrors.Clear(); // Clear old ones, so next mod starts fresh #endif if (errorCount > 0) { MySandboxGame.Log.WriteLineAndConsole("Compilation FAILED!"); return(false); } } MySandboxGame.Log.WriteLineAndConsole("Compilation successful!"); } else { MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "LoadScripts")); } } #if SE else if (m_type == WorkshopType.IngameScript) { // 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 ingamescript = MyScriptCompiler.Static.GetIngameScript(program, "Program", typeof(Sandbox.ModAPI.Ingame.MyGridProgram).Name, "sealed partial"); var messages = new List <MyScriptCompiler.Message>(); var assembly = MyScriptCompiler.Static.Compile(MyApiTarget.Ingame, null, ingamescript, messages, null).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) { MySandboxGame.Log.WriteLineAndConsole(msg.Text); if (msg.Severity > TErrorSeverity.Warning) { errors++; } } if (errors > 0) { return(false); } } if (assembly == null) { return(false); } } #endif return(true); } return(true); }
/// <summary> /// Compiles the mod /// </summary> /// <returns></returns> public bool Compile() { // Compile if (m_compile) { if (m_type == WorkshopType.Mod) { if (_compileMethod != null) { MySandboxGame.Log.WriteLineAndConsole("Compiling..."); #if SE var mod = new MyModContext(); // Because of a regression in SE, we need to create a checkpoint ModItem to set the Id. var modob = new MyObjectBuilder_Checkpoint.ModItem(); modob.Name = Path.GetFileName(m_modPath); if (ModId.Length > 0) { modob.PublishedFileId = m_workshopItems[m_modId[0]].Id; modob.PublishedServiceName = m_workshopItems[m_modId[0]].ServiceName; modob.FriendlyName = m_workshopItems[m_modId[0]].Title; modob.SetModData(m_workshopItems[m_modId[0]]); } else { // Fake it, so the compile still works modob.PublishedFileId = 0; modob.PublishedServiceName = MyGameService.GetDefaultUGC().ServiceName; modob.FriendlyName = Title; } mod.Init(modob); // Call init again, to make sure the path in set properly to the local mod directory mod.Init(m_title, null, m_modPath); #else var workshopItem = new MyLocalWorkshopItem(new VRage.ObjectBuilders.SerializableModReference(Path.GetFileName(m_modPath), 0)); var mod = new MyModContext(workshopItem, 0); #endif _compileMethod( #if SE m_modPath, #endif mod ); // Process any errors #if SE var errors = MyDefinitionErrors.GetErrors(); #else var compileMessages = _scriptManager.GetType().GetField("m_messages", BindingFlags.NonPublic | BindingFlags.Instance); var errors = (compileMessages.GetValue(_scriptManager) as List <MyScriptCompiler.Message>) ?? new List <MyScriptCompiler.Message>(); #endif 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.WriteLineAndConsole(string.Format("There are {0} compile errors:", errorCount)); } if (warningCount > 0) { MySandboxGame.Log.WriteLineAndConsole(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) #if SE { System.Console.WriteLine(error.Message); } #else { System.Console.WriteLine(error.Text); } #endif #if SE MyDefinitionErrors.Clear(); // Clear old ones, so next mod starts fresh #endif if (errorCount > 0) { MySandboxGame.Log.WriteLineAndConsole("Compilation FAILED!"); return(false); } } MySandboxGame.Log.WriteLineAndConsole("Compilation successful!"); } else { MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "LoadScripts")); } } #if SE else if (m_type == WorkshopType.IngameScript) { // 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) { MySandboxGame.Log.WriteLineAndConsole(msg.Text); if (msg.IsError) { errors++; } } if (errors > 0) { return(false); } } if (assembly == null) { return(false); } } #endif return(true); } return(true); }
/// <summary> /// Compiles the mod /// </summary> /// <returns></returns> public bool Compile() { // Compile if (m_compile) { if (m_type == WorkshopType.Mod) { if (_compileMethod != null) { MySandboxGame.Log.WriteLineAndConsole("Compiling..."); var mod = new MyModContext(); mod.Init(m_title, null, m_modPath); _compileMethod.Invoke(_scriptManager, new object[] { m_modPath, mod }); // Process any errors var errors = MyDefinitionErrors.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.WriteLineAndConsole(string.Format("There are {0} compile errors:", errorCount)); } if (warningCount > 0) { MySandboxGame.Log.WriteLineAndConsole(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) { System.Console.WriteLine(error.Message); } MyDefinitionErrors.Clear(); // Clear old ones, so next mod starts fresh if (errorCount > 0) { MySandboxGame.Log.WriteLineAndConsole("Compilation FAILED!"); return(false); } } MySandboxGame.Log.WriteLineAndConsole("Compilation successful!"); } else { MySandboxGame.Log.WriteLineAndConsole(string.Format(Constants.ERROR_Reflection, "LoadScripts")); } } else if (m_type == WorkshopType.IngameScript) { // 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 ingamescript = MyScriptCompiler.Static.GetIngameScript(program, "Program", typeof(Sandbox.ModAPI.Ingame.MyGridProgram).Name, "sealed partial"); var messages = new List <MyScriptCompiler.Message>(); var assembly = MyScriptCompiler.Static.Compile(MyApiTarget.Ingame, null, ingamescript, messages).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) { MySandboxGame.Log.WriteLineAndConsole(msg.Text); if (msg.Severity > TErrorSeverity.Warning) { errors++; } } if (errors > 0) { return(false); } } if (assembly == null) { return(false); } } return(true); } return(true); }