public void Pack(string sourcePath, string destinationPath) { var isFile = File.Exists(sourcePath); var unpackedFiles = isFile ? new[] { sourcePath } : Directory.GetFiles(sourcePath, "*.xnb.js", SearchOption.AllDirectories); var tempDir = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "Gnomoria.ContentExtractor", Guid.NewGuid().ToString())).FullName; foreach (var file in unpackedFiles) { logger.Info("Creating intermediate file for {0}", Path.GetFileNameWithoutExtension(file)); var doc = JsonConvert.DeserializeXmlNode(File.ReadAllText(file)); var assetPath = (isFile ? Path.GetFileName(file) : file.Substring(sourcePath.Length)).Split('.')[0] + ".xml"; Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(tempDir, assetPath))); doc.Save(Path.Combine(tempDir, assetPath)); } logger.Info("Packing files"); var filesToBePacked = Directory.GetFiles(tempDir, "*.xml", SearchOption.AllDirectories); var libPath = Path.Combine(tempDir, "gnomorialib.dll"); var preppedLibrary = false; try { var startInfo = new ProcessStartInfo() { FileName = "lib\\de4dot", Arguments = "gnomorialib.dll -o {0} --keep-names ef".FormatWith(libPath), RedirectStandardOutput = true, CreateNoWindow = true, UseShellExecute = false, WorkingDirectory = "lib" }; using (var process = new Process { StartInfo = startInfo }) { process.Start(); process.StandardOutput.ReadToEnd(); preppedLibrary = process.ExitCode == 0; } } catch (Exception e) { logger.Debug(e); } if (!preppedLibrary) { logger.Error("Couldn't prep library"); Cleanup(tempDir); return; } var project = new ContentProject(destinationPath, tempDir); project.AddReference("gnomorialib", libPath); foreach (var file in filesToBePacked) project.AddItem(file, "XmlImporter", Path.GetFileName(file), Path.GetFileNameWithoutExtension(file)); if (!project.Build()) logger.Error("Error while packing data. See log for details"); Cleanup(tempDir); }
private void PackSkinImages(string[] images, string imagesDir, string destination, string tempDir) { var project = new ContentProject(destination, tempDir); project.AddReference("Microsoft.Xna.Framework.Content.Pipeline.TextureImporter, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=MSIL"); foreach (var file in images) project.AddItem(file, "TextureImporter", Path.GetFileName(file), Path.GetFileNameWithoutExtension(file), "TextureProcessor"); if (!project.Build()) logger.Error("Error while packing images. See log for details"); }
public bool Build(string fileName) { if (prepath != "" && File.Exists(prepath)) File.Move(prepath, prepath + "TEMP"); string exepath = System.Windows.Forms.Application.StartupPath; _project = new ContentProject(); string ext = Path.GetExtension(fileName).ToLower(); _project.ProjectOptions.RootDirectory = Path.GetFullPath(fileName).Replace(Path.GetFileName(fileName), ""); switch (ext) { case ".bmp": case ".jpg": case ".jpeg": case ".tga": case ".dds": _project.ProjectOptions.OutputDirectory = _project.ProjectOptions.RootDirectory; _project.ProjectOptions.IntermediateDirectory = _project.ProjectOptions.RootDirectory; break; case ".lua": _project.ProjectOptions.OutputDirectory = _project.ProjectOptions.RootDirectory; _project.ProjectOptions.IntermediateDirectory = _project.ProjectOptions.RootDirectory; break; } _project.InitContentFile(fileName); bool ret = _project.Build(false); if (ret == false) return false; if (prepath != "" && File.Exists(prepath + "TEMP")) File.Move(prepath + "TEMP", prepath); prepath = _project.ProjectOptions.OutputDirectory + "\\" +Path.GetFileNameWithoutExtension(fileName) + ".xnb"; switch(ext) { case ".lua": string outlilfile = _project.ProjectOptions.OutputDirectory + Path.GetFileNameWithoutExtension(fileName) + ".lil"; string outpdbfile = _project.ProjectOptions.OutputDirectory + Path.GetFileNameWithoutExtension(fileName) + ".pdb"; string outxnbfile = _project.ProjectOptions.OutputDirectory + Path.GetFileNameWithoutExtension(fileName) + ".xnb"; string contPipeli = _project.ProjectOptions.OutputDirectory + "ContentPipeline.xml"; File.Delete(contPipeli); if (Path.GetDirectoryName(fileName) != (exepath + "\\scripts")) { File.Copy(fileName, exepath + "\\scripts\\" + Path.GetFileNameWithoutExtension(fileName) + ".lua"); File.Delete(fileName); } string xnbfile = exepath + "\\Content\\scripts\\" + Path.GetFileNameWithoutExtension(fileName) + ".xnb"; File.Delete(xnbfile); File.Move(outxnbfile, xnbfile); string lilfile = exepath + "\\Content\\" + Path.GetFileNameWithoutExtension(fileName) + ".lil"; File.Delete(lilfile); File.Move(outlilfile, lilfile); string pdbfile = exepath + "\\Content\\" + Path.GetFileNameWithoutExtension(fileName) + ".pdb"; File.Delete(pdbfile); File.Move(outpdbfile, pdbfile); break; case ".bmp": case ".jpg": case ".jpeg": case ".tga": case ".dds": // string outfile = _project.ProjectOptions.OutputDirectory + Path.GetFileNameWithoutExtension(fileName) + ".xnb"; contPipeli = _project.ProjectOptions.OutputDirectory + "ContentPipeline.xml"; File.Delete(contPipeli); string file = exepath + "\\Content\\textures\\" + Path.GetFileNameWithoutExtension(fileName) + ".xnb"; File.Delete(file); File.Move(outfile, file); break; } return ret; }
// Runs the action passed, with the original command line arguments tacked on (path should be args[1]) public static int RunAction(string action, string[] args, bool verbose) { // Try to load the content project file string filePath = args[1]; ContentProject project = null; try { // Check for any parameter override cmd line args var oargs = ArgParser.Params(args); project = ContentProject.LoadFromFile(filePath, oargs); } catch (Exception e) { CConsole.Error($"Could not load content project file, reason: {e.Message}."); if (verbose && (e.InnerException != null)) { CConsole.Error($"{e.InnerException.GetType().Name}"); CConsole.Error(e.InnerException.StackTrace); } return(-1); } // Report project information if (verbose) { CConsole.Info($" ------ Project Info ------ "); CConsole.Info($" Content Root: {project.Paths.ContentRoot}"); CConsole.Info($" Intermediate Root: {project.Paths.IntermediateRoot}"); CConsole.Info($" Output Root: {project.Paths.OutputRoot}"); } // Get the parallelization info uint threadCount = (action == "clean") ? 1 : ArgParser.Parallel(args); if (threadCount > 1) { CConsole.Info($"Using {threadCount} threads for {action} process."); } // Create the build engine to manage this action BuildEngine engine = null; bool useStats = ArgParser.Stats(args); try { engine = new BuildEngine(project, new CommandLineLogger(verbose, useStats), threadCount); } catch (Exception e) { CConsole.Error($"Unable to create build engine, reason: {e.Message}."); if (verbose) { CConsole.Error($"{e.GetType().Name}"); CConsole.Error(e.StackTrace); } return(-1); } // Get the build type bool releaseBuild = ArgParser.Release(args); // Start the action task and logging using (engine) { try { // Launch the correct task Task task = null; switch (action) { case "build": task = engine.Build(false, releaseBuild, useStats); break; case "rebuild": task = engine.Build(true, releaseBuild, useStats); break; case "clean": task = engine.Clean(); break; default: CConsole.Error($"The action '{action}' was not understood."); return(-1); // Should never be reached } task.Start(); // Wait for the task to finish, logging while we go while (!task.IsCompleted) { Thread.Sleep(50); engine.Logger.Poll(); } // Check that the task did not encounter an exception if (task.IsFaulted) { var te = task.Exception.InnerException; CConsole.Error($"Action '{action}' encountered an exception, message: {te?.Message}."); if (verbose) { CConsole.Error($"{te?.GetType().Name}"); CConsole.Error(te?.StackTrace); } return(-1); } } catch (Exception e) { CConsole.Error($"Unhandled exception during action '{action}', message: {e.Message}."); if (verbose) { CConsole.Error($"{e.GetType().Name}"); CConsole.Error(e.StackTrace); } return(-1); } } // Everything went well return(0); }
public ContentBuilder(ContentProject project) { Project = project; builtFiles = new Dictionary <string, ContentFile>(); }