Example #1
0
        private static NMSTemplate ReadTemplateFromXmlReader(XmlReader reader)
        {
            EXmlData    root         = (EXmlData)Serializer.Deserialize(reader);
            NMSTemplate rootTemplate = NMSTemplate.DeserializeEXml(root);

            return(rootTemplate);
        }
        /// <summary>
        /// Construct a new ParentedEXmlObject from an existing Base.
        /// </summary>
        /// <param name="baseObject">The existing base.</param>
        protected ParentedEXmlObject(EXmlBase baseObject)
        {
            Name     = baseObject.Name;
            Elements = baseObject.Elements;
            Children = new List <ParentedEXmlObject>();
            EXmlData     data = baseObject as EXmlData;
            EXmlMeta     meta = baseObject as EXmlMeta;
            EXmlProperty prop = baseObject as EXmlProperty;

            if (data != null)
            {
                Attribute = new EXmlAttribute {
                    AttributeName = "template",
                    Value         = data.Template
                };
            }
            else if (meta != null)
            {
                Attribute = new EXmlAttribute {
                    AttributeName = "comment",
                    Value         = meta.Comment,
                };
            }
            else if (prop != null)
            {
                Attribute = new EXmlAttribute {
                    AttributeName = "value",
                    Value         = prop.Value
                };
            }
            else
            {
                Attribute = null;
            }
        }
Example #3
0
        public static string WriteTemplate(NMSTemplate template)
        {
            var xmlSettings = new XmlWriterSettings
            {
                Indent   = true,
                Encoding = Encoding.UTF8
            };

            using (var stringWriter = new EncodedStringWriter(Encoding.UTF8))
                using (var xmlTextWriter = XmlWriter.Create(stringWriter, xmlSettings))
                {
                    EXmlData data = template.SerializeEXml();
                    Serializer.Serialize(xmlTextWriter, data, Namespaces);
                    xmlTextWriter.Flush();
                    return(stringWriter.GetStringBuilder().ToString());
                }
        }
Example #4
0
        public static string WriteTemplate(NMSTemplate template)
        {
            var origCulture = Thread.CurrentThread.CurrentCulture;

            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            var xmlSettings = new XmlWriterSettings
            {
                Indent   = true,
                Encoding = Encoding.UTF8
            };

            using (var stringWriter = new EncodedStringWriter(Encoding.UTF8))
                using (var xmlTextWriter = XmlWriter.Create(stringWriter, xmlSettings))
                {
                    EXmlData data = template.SerializeEXml();
                    Serializer.Serialize(xmlTextWriter, data, Namespaces);
                    xmlTextWriter.Flush();

                    var xmlData = stringWriter.GetStringBuilder().ToString();
                    Thread.CurrentThread.CurrentCulture = origCulture;
                    return(xmlData);
                }
        }
        /// <summary>
        /// Converts this custom object back into an EXmlBase so that it can be serialized back into an MBIN.
        /// </summary>
        /// <returns></returns>
        public EXmlBase ConvertToSerializableEXML(EXmlBase parent = null, ParentedEXmlObject parentAsNative = null)
        {
            EXmlBase root;

            if (parent == null)
            {
                // Attribute is gonna be template. If it's not, there's a problem.
                if (Attribute.AttributeName != "template")
                {
                    throw new Exception("Top level attribute is not a template!");
                }
                EXmlData newData = new EXmlData();
                newData.Name     = Name;
                newData.Template = Attribute.Value;
                root             = newData;
            }
            else
            {
                root = parent;
            }

            //EXmlBase root = parent ?? this;
            parentAsNative = parentAsNative ?? this;
            root.Elements  = new List <EXmlBase>();
            foreach (ParentedEXmlObject child in parentAsNative.Children)
            {
                EXmlBase childAsBase = child;
                if (child.Attribute?.AttributeName == "template")
                {
                    EXmlData childAsTyped = new EXmlData();
                    childAsTyped.Name     = childAsBase.Name;
                    childAsTyped.Elements = childAsBase.Elements;
                    childAsTyped.Template = child.Attribute.Value;
                    root.Elements.Add(childAsTyped);
                    childAsBase = childAsTyped;
                }
                else if (child.Attribute?.AttributeName == "value")
                {
                    EXmlProperty childAsTyped = new EXmlProperty();
                    childAsTyped.Name     = childAsBase.Name;
                    childAsTyped.Elements = childAsBase.Elements;
                    childAsTyped.Value    = child.Attribute.Value;
                    root.Elements.Add(childAsTyped);
                    childAsBase = childAsTyped;
                }
                else if (child.Attribute?.AttributeName == "comment")
                {
                    EXmlMeta childAsTyped = new EXmlMeta();
                    childAsTyped.Name     = childAsBase.Name;
                    childAsTyped.Elements = childAsBase.Elements;
                    childAsTyped.Comment  = child.Attribute.Value;
                    root.Elements.Add(childAsTyped);
                    childAsBase = childAsTyped;
                }
                else
                {
                    root.Elements.Add(childAsBase);
                }

                ConvertToSerializableEXML(childAsBase, child);
            }
            return(root);
        }
Example #6
0
        static void Init(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            string        vanillaMBINPath = null;
            List <string> patchFiles      = new List <string>();

            // [0] = vanilla file
            // [1] = custom file 1
            // [2] = custom file 2
            // [...] = ...
            if (args.Length < 3)
            {
                PopulateDirectoryInfo(out vanillaMBINPath, out patchFiles);
            }
            else
            {
                vanillaMBINPath = args[0];
                if (!File.Exists(vanillaMBINPath))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("ERROR: The file you input does not exist!");
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("Failed to find: " + vanillaMBINPath);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Press enter to quit...");
                    Console.ReadLine();
                    return;
                }
                foreach (string data in args.Skip(1))
                {
                    if (!File.Exists(data))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("ERROR: The file you input does not exist!");
                        Console.ForegroundColor = ConsoleColor.DarkRed;
                        Console.WriteLine("Failed to find: " + data);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Press enter to quit...");
                        Console.ReadLine();
                        return;
                    }
                    patchFiles.Add(data);
                }
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Loading up all of the MBIN files...");

            FileStream vanillaMBIN = File.OpenRead(vanillaMBINPath);

            FileStream[] otherMBINs = new FileStream[patchFiles.Count];
            for (int idx = 0; idx < patchFiles.Count; idx++)
            {
                otherMBINs[idx] = File.OpenRead(patchFiles[idx]);
            }

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("Processing MBIN File: {0}", vanillaMBINPath);

            MBINFile[] otherBnkObjects = new MBINFile[otherMBINs.Length];
            MBINFile   vanillaFile     = new MBINFile(vanillaMBIN);

            vanillaFile.Load();

            for (int idx = 0; idx < otherBnkObjects.Length; idx++)
            {
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine("Processing MBIN File: {0}", patchFiles[idx]);
                MBINFile newFile = new MBINFile(otherMBINs[idx]);
                newFile.Load();
                otherBnkObjects[idx] = newFile;
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Done processing MBIN files! Enumerating... Please be present so that you may resolve any file conflicts.");

            MultiMBINEnumerator iterator = new MultiMBINEnumerator(vanillaFile, otherBnkObjects, vanillaMBINPath, patchFiles);
            EXmlData            newData  = iterator.PatchEverything();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Done enumerating BNK files! Saving in the same directory as the EXE...");

            string name = "MergedMBIN-" + DateTime.Now.ToFileTimeUtc().ToString() + ".MBIN";

            //FileStream save = File.OpenWrite(@".\" + name);

            // Since the MBINFile class refuses to populate the data properly (Am I just not getting how to use it? Ech.)
            // It kept throwing null pointer exceptions when I called SetData. wtf?

            // Edit: Turns out I have to specify the header myself. wat.
            // Monkey if you're reading this, I love you nohomo it's just this is written really awkwardly and I'm not used to it.
            using (MBINFile outFile = new MBINFile(@".\" + name)) {
                NMSTemplate template = NMSTemplate.DeserializeEXml(newData);
                MBINHeader  header   = new MBINHeader();
                header.SetDefaults();

                outFile.Header = header;
                outFile.SetData(template);
                outFile.Save();
            }

            vanillaMBIN.Dispose();
            foreach (FileStream str in otherMBINs)
            {
                str.Dispose();
            }

            Console.Write("Saved as ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(name);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Press enter to quit.");
            Console.ReadLine();
        }