Beispiel #1
0
        private int loadxmlfile(IntPtr L)
        {
            var fileName = Melua.luaL_checkstring(L, 1);

            Melua.lua_pop(L, 1);

            fileName = ModPack.NormalizePath(fileName);

            if (!fileName.EndsWith(".xml") && !fileName.EndsWith(".xml.compiled"))
            {
                return(Melua.melua_error(L, "Expected XML file extension."));
            }

            if (!FileExistsInPackages(fileName))
            {
                return(Melua.melua_error(L, "File '{0}' not found in packages.", fileName));
            }

            try
            {
                _loadedXmlFile   = fileName;
                _loadedXmlModder = _modPack.GetXmlModder(fileName);

                return(0);
            }
            catch (XmlException ex)
            {
                _loadedXmlFile   = null;
                _loadedXmlModder = null;

                return(Melua.melua_error(L, "Failed to parse XML: '{0}'", ex.Message));
            }
        }
Beispiel #2
0
 /// <summary>
 /// Creates new instance.
 /// </summary>
 /// <param name="modder">Modder to use.</param>
 /// <param name="selector">Path of the elements to modify.</param>
 /// <param name="attributeName">Name of the attribute to set.</param>
 /// <param name="value">New value of the attribute, use null to remove it.</param>
 public XmlAttributeSetter(XmlModder modder, string selector, string attributeName, string value)
 {
     this.XmlModder     = modder;
     this.Selector      = selector;
     this.AttributeName = attributeName;
     this.Value         = value;
 }
Beispiel #3
0
        /// <summary>
        /// Returns modder for the given XML file from the packages.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public XmlModder GetXmlModder(string fileName)
        {
            fileName = NormalizePath(fileName);

            lock (_syncLock)
            {
                if (_xmlModders.ContainsKey(fileName))
                {
                    return(_xmlModders[fileName]);
                }
            }

            var isCompiled = false;

            if (!fileName.EndsWith(".xml") && !(isCompiled = fileName.EndsWith(".xml.compiled")))
            {
                throw new ArgumentException("Expected XML file extension.");
            }

            var packReader = this.GetPackReader();

            if (!packReader.Exists(fileName))
            {
                throw new FileNotFoundException("File not found in packages: '" + fileName + "'");
            }

            var entry       = _packReader.GetEntry(fileName);
            var tmpFilePath = Path.GetTempFileName();

            var modder = new XmlModder();

            if (isCompiled)
            {
                var featuresFile = FeaturesFile.CompiledToXml(entry.GetDataAsStream());
                using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(featuresFile)))
                    modder.LoadFromStream(ms);
            }
            else
            {
                using (var stream = entry.GetDataAsStream())
                    using (var sr = new StreamReader(stream))
                    {
                        var xml = sr.ReadToEnd();
                        xml = FixXmlCode(fileName, xml);

                        modder.LoadFromString(xml);
                    }
            }

            lock (_syncLock)
                _xmlModders[fileName] = modder;

            return(modder);
        }
Beispiel #4
0
        /// <summary>
        /// Saves the modified XML file from the modder to the given path,
        /// either raw or compiled, depending on the extension.
        /// </summary>
        /// <param name="modder"></param>
        /// <param name="filePath"></param>
        private void SaveXmlModder(XmlModder modder, string filePath)
        {
            PrepareFolder(filePath);

            var isCompiled = filePath.EndsWith(".xml.compiled");

            if (isCompiled)
            {
                var xml = modder.GetString();
                FeaturesFile.SaveCompiledFromXml(filePath, xml);
            }
            else
            {
                modder.Save(filePath);
            }
        }
Beispiel #5
0
 /// <summary>
 /// Creates new instance.
 /// </summary>
 /// <param name="modder">Modder to execute the removal on.</param>
 /// <param name="selector">Path of the elements to remove.</param>
 public XmlElementRemover(XmlModder modder, string selector)
 {
     this.XmlModder = modder;
     this.Selector  = selector;
 }
Beispiel #6
0
 /// <summary>
 /// Creates new instance.
 /// </summary>
 /// <param name="modder">Modder to execute the replace on.</param>
 /// <param name="search">Regular expression to search for.</param>
 /// <param name="replace">String to replace matches with.</param>
 public XmlReplacer(XmlModder modder, string search, string replace)
 {
     this.XmlModder = modder;
     this.Search    = search;
     this.Replace   = replace;
 }