Example #1
0
 public IAssetBundle Read(string filename, int filterindex)
 {
     using (var stream = File.OpenRead(filename))
     {
         return(Read(stream, filename, ReaderManager.FriendlyFilename(filename)));
     }
 }
Example #2
0
        private static IAssetBundle ReadZip(string filename, Stream file, Flavour flavour)
        {
            // If this is a MS-DOS compatible zip file,
            // the filenames are encoded with the created machine's OEM codepage,
            // which we don't know what it is.
            // Windows assumes it is same as this machine's ANSI.
            // It works fine in most cases, and on SBCS world,
            // the worst-case symptom is getting files with broken filename.
            // On MBCS world, however, when receiving a zip file from other region,
            // some particular combinations of bytes in a filename
            // could be considered illegal per the receiving system's MBCS ANSI,
            // and opening a file may fail.
            // It is unacceptable, especially given that a name of a zip entry is
            // not important for the purpose of this program.
            // That's why we explicitly say it is 850,
            // even though it is very unlikely today.
            // Please be reminded that the specified encoding only affects the decoding of
            // zip entry names and has nothing to do with the contents.

            using (var zip = new ZipArchive(file, ZipArchiveMode.Read, true, Encoding.GetEncoding(850)))
            {
                var assets = new List <IAsset>();
                foreach (var entry in zip.Entries)
                {
                    // It seems the stream from ZipEntry.Open() doesn't suppoprt Seek.
                    // We need a trick here.
                    using (var f = entry.Open())
                    {
                        if (!IsXliff(f, true))
                        {
                            continue;
                        }
                    }
                    using (var f = entry.Open())
                    {
                        var bundle = ReadXliff(filename, f, flavour, entry);
                        if (bundle != null)
                        {
                            assets.AddRange(bundle.Assets);
                        }
                    }
                }
                return(assets.Count == 0 ? null : new SimpleAssetBundle(assets, ReaderManager.FriendlyFilename(filename)));
            }
        }
Example #3
0
        private static ReaderManager CreateInstance()
        {
            lock (Lock)
            {
                if (_Current == null)
                {
                    var manager = new ReaderManager();

                    // Add standard readers.
                    manager.Add(new XliffReader());
                    manager.Add(new TmxReader());
                    //manager.Add(new SdltmReader());

                    // Add plugin readers.
                    manager.AddRange(PluginManager.Current.Readers.Cast <IAssetReader>());

                    _Current = manager;
                }
                return(_Current);
            }
        }
Example #4
0
        /// <summary>Parses an xliff (root) element into an asset bundle.</summary>
        /// <param name="xliff">An XML element that is supposed to be an xliff element.</param>
        /// <param name="filename">The (full path) name of the file <paramref name="xliff"/> was taken from.</param>
        /// <param name="flavour">The flavour of XLIFF to assume.</param>
        /// <returns>An asset bundle from <see cref="xliff"/>.</returns>
        /// <remarks>The returned bundle's <see cref="IAssetBundle.CanRefresh"/> will be false.</remarks>
        public IAssetBundle Parse(XElement xliff, string filename, Flavour flavour = Flavour.Auto)
        {
            if (xliff == null)
            {
                throw new ArgumentNullException("xliff");
            }
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            var parser = new XliffParser();

            parser.Filename = filename;
            parser.Flavour  = flavour;
            var assets = parser.Parse(xliff);

            if (assets == null)
            {
                throw new ArgumentException("Not a valid <xliff> element", "xliff");
            }
            return(new SimpleAssetBundle(assets, ReaderManager.FriendlyFilename(filename)));
        }
Example #5
0
        public IAssetBundle Read(Stream stream)
        {
            XElement xliff;

            try
            {
                // I experienced that some import filter (used with some CAT software)
                // produces some illegal entities, e.g., "&#x1F;".
                // Although it is NOT a wellformed XML in theory, we need to take care of them.
                // Another issue is that some XML file includes DOCTYPE declaration
                // with a system identifier,
                // that XmlReader tries to access to to get a DTD,
                // so we need to instruct explicitly not to do so.
                var settings = new XmlReaderSettings()
                {
                    CheckCharacters  = false,
                    IgnoreWhitespace = false,
                    DtdProcessing    = DtdProcessing.Ignore,
                    XmlResolver      = null,
                    CloseInput       = true
                };
                using (var rd = XmlReader.Create(stream, settings))
                {
                    xliff = XElement.Load(rd);
                }
            }
            catch (XmlException)
            {
                // This is usually thrown when the given file was not an XML document,
                // or it was meant to be an XML but contained some error.
                // The first case is normal, since we try to parse everything as an XML.
                // The latter case needs diagnostic,
                // but I don't know how I can identify the cases.
                // We need some diagnostic logging feature.  FIXME.
                return(null);
            }
            catch (IOException)
            {
                // This must be an indication of an issue in the underlying file access.
                throw;
            }
            catch (Exception)
            {
                // If we get an exception of other type,
                // I believe it is an indication of some unexpected error.
                // However, the API document of XElement.Load(Strream) is too vague,
                // and I don't know what we should do.
                return(null);
            }

            // We are probably seeing a wellformed XML document.
            // Check if it is an XLIFF document.
            var X = xliff.Name.Namespace;

            if (X != XliffAsset.XLIFF && X != XNamespace.None)
            {
                return(null);
            }
            if (xliff.Name.LocalName != "xliff")
            {
                return(null);
            }

            // OK.  It seems an XLIFF.  Try to detect a flavour if set to Auto.
            if (Flavour == XliffReader.Flavour.Auto)
            {
                Flavour = DetectFlavour(xliff);
            }

            return(new SimpleAssetBundle(xliff.Elements(X + "file").Select(CreateAsset).ToArray(), ReaderManager.FriendlyFilename(Filename)));
        }
Example #6
0
 /// <summary>Reads a TMX file to create a reloadable asset bundle.</summary>
 /// <param name="filename">A full path name of a TMX file.</param>
 /// <param name="filterindex_UNUSED">Ignored.</param>
 /// <returns>An asset bundle.</returns>
 public IAssetBundle Read(string filename, int filterindex_UNUSED)
 {
     return(LoaderAssetBundle.Create(
                ReaderManager.FriendlyFilename(filename),
                () => ReadAssets(filename)));
 }
Example #7
0
 public IAssetBundle Read(string filename, Flavour flavour = Flavour.Auto)
 {
     return(LoaderAssetBundle.Create(
                ReaderManager.FriendlyFilename(filename),
                () => ReadAssets(filename, flavour)));
 }