Extract() public static method

Extract cartridge data from specified luaCode in given stream.
public static Extract ( Stream stream ) : Cartridge
stream Stream
return Cartridge
Ejemplo n.º 1
0
        public Cartridge Load()
        {
            if (cartridge == null)
            {
                // Extract cartridge data from Lua file
                cartridge = LUA.Extract(_stream);
            }

            // All media files should be in the same directory as the Lua file
            string path = Path.GetDirectoryName(_luaFileName);

            // Retrive input streams for medias
            foreach (Media media in cartridge.Medias)
            {
                foreach (MediaResource r in media.Resources)
                {
                    // Load data of file into byte array
                    var br = new BinaryReader(new FileStream(Path.Combine(path, r.Filename), FileMode.Open));
                    r.Data = new byte[br.BaseStream.Length];
                    r.Data = br.ReadBytes(r.Data.Length);
                    br     = null;
                }
            }

            return(cartridge);
        }
Ejemplo n.º 2
0
        public Cartridge Load()
        {
            // Is there a valid gwz file
            if (_zip == null)
            {
                return(null);
            }

            // Is there a valid Lua file
            if (_luaFile == null)
            {
                return(null);
            }

            if (cartridge == null)
            {
                // Extract cartridge data from Lua file
                cartridge = LUA.Extract(_zip[_luaFile.FileName].OpenReader());
            }

            // Retrive input streams for medias
            foreach (Media media in cartridge.Medias)
            {
                foreach (MediaResource r in media.Resources)
                {
                    // Load data of file into byte array
                    var br = new BinaryReader(_zip[r.Filename].OpenReader());
                    r.Data = new byte[br.BaseStream.Length];
                    r.Data = br.ReadBytes(r.Data.Length);
                    br     = null;
                }
            }

            return(cartridge);
        }
Ejemplo n.º 3
0
        public void Check()
        {
            // Is there a valid input stream
            if (_stream == null)
            {
                throw new FileNotFoundException("No valid file");
            }

            // Any compilation errors of the Lua file
            _stream.Position = 0;
            LUA.Check(_stream, _luaFileName);

            // Extract cartridge data from Lua file
            _stream.Position = 0;
            cartridge        = LUA.Extract(_stream);

            // Save Lua file name for later use
            cartridge.LuaFileName = _luaFileName;

            // All media files should be in the same directory as the Lua file
            string path = Path.GetDirectoryName(_luaFileName);

            // Now check, if all media resources files exist
            foreach (Media media in cartridge.Medias)
            {
                foreach (MediaResource resource in media.Resources)
                {
                    // Check, if filename is in list of files
                    if (!File.Exists(Path.Combine(path, resource.Filename)))
                    {
                        throw new FileNotFoundException("Folder don't contain file", resource.Filename);
                    }
                }
            }

            // Now all is checked without any problems
            // So it seams, that this folder is valid
        }
Ejemplo n.º 4
0
        public void Check()
        {
            // Is there a valid input stream
            if (_stream == null)
            {
                throw new FileNotFoundException("No valid file");
            }

            // Now read gwz file and save for later use
            _zip = ZipFile.Read(_stream);

            if (_zip == null)
            {
                throw new FileLoadException("No valid gwz file");
            }

            foreach (ZipEntry zipEntry in _zip.Entries)
            {
                switch (Path.GetExtension(zipEntry.FileName).ToLower())
                {
                case ".lua":
                    _luaFile   = zipEntry;
                    _luaFiles += 1;
                    break;
                }
            }

            // Is there a Lua file?
            if (_luaFile == null)
            {
                throw new FileNotFoundException("No valid Lua file found");
            }

            // Is there more than one Lua file
            if (_luaFiles > 1)
            {
                throw new FileLoadException("More than one Lua file found");
            }

            // Any compilation errors of the Lua file
            LUA.Check(_zip[_luaFile.FileName].OpenReader(), _luaFile.FileName);

            // Extract cartridge data from Lua file
            cartridge = LUA.Extract(_zip[_luaFile.FileName].OpenReader());

            // Save Lua file name for later use
            cartridge.LuaFileName = _luaFile.FileName;

            // Now check, if all media resources files exist
            foreach (Media media in cartridge.Medias)
            {
                foreach (MediaResource resource in media.Resources)
                {
                    // Check, if filename is in list of files
                    if (!_zip.EntryFileNames.Contains(resource.Filename))
                    {
                        if (string.IsNullOrWhiteSpace(resource.Filename))
                        {
                            throw new FileNotFoundException("The Lua file is referencing a file without a filename");
                        }
                        else
                        {
                            throw new FileNotFoundException(String.Format("The GWZ is missing a file referred to by the cartridge's code. The file name is: {0}", resource.Filename));
                        }
                    }
                }
            }

            // Now all is checked without any problems
            // So it seams, that this GWZ file is valid
        }