This static libray contains all function for handling the Lua source code.
示例#1
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);
        }
示例#2
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);
        }
示例#3
0
        public void CreateZonesFile(string inputFilename, string outputFilename, string guid, string userName, long userId, string completitionCode, DeviceType device, EngineVersion version)
        {
            FileStream inputStream = null;
            Cartridge  cartridge;

            try
            {
                // Open Lua file
                inputStream = new FileStream(inputFilename, FileMode.Open);

                // Create input object for plain folders
                IInput input = new Folder(inputStream, inputFilename);

                // Check Lua file
                input.Check();

                // Load Lua code and extract all required data
                cartridge = input.Load();

                // Close input
                input = null;
            }
            finally
            {
                if (inputStream != null)
                {
                    inputStream.Close();
                    inputStream = null;
                }
            }

            // Create selected engine
            IEngine engine = Compiler.CreateEngine(device);

            // Convert Lua code and insert special code for this player
            cartridge = engine.ConvertCartridge(cartridge);
            userName  = engine.ConvertString(userName);

            // ---------- Compile Lua code into binary chunk ----------

            // Compile Lua code
            cartridge.Chunk = LUA.Compile(cartridge.LuaCode, cartridge.LuaFileName);

            // ---------- Save cartridge as GWC file ----------

            // Create object for output format (could be also WFC or any other IOutput)
            var outputFormat = new GWC();

            // Write output file
            // Create output in correct format
            var ms = outputFormat.Create(cartridge, userName, userId, completitionCode);

            // Save output to file
            using (FileStream ofs = new FileStream(outputFilename, FileMode.Create)) {
                ms.CopyTo(ofs);
                // Close output
                ofs.Flush();
                ofs.Close();
            }
        }
示例#4
0
        /// <summary>
        /// Compiler entry for online compilation.
        /// </summary>
        /// <param name="ifs">Stream of the input file.</param>
        /// <param name="device">Device.</param>
        /// <param name="userName">User name.</param>
        /// <param name="completitionCode">Completition code.</param>
        public static MemoryStream Download(Stream ifs, DeviceType device = DeviceType.Emulator, string userName = "******", string completitionCode = "1234567890ABCDE")
        {
            // ---------- Check device ----------

            // ---------- Create GWZ file only (required for upload and download of GWZ file) ----------

            // Create object für reading input file (could be also any other format implementing IInput)
            var inputFormat = new GWZ(ifs);

            // ---------- Check GWZ file (only required for upload of GWZ file) ----------

            // Check gwz file for errors (Lua code and all files included)
            // Now there shouldn't be any errors, because files on the server are checked.
            inputFormat.Check();

            // ---------- Load cartridge from GWZ file (required when downloading cartridge) ----------

            // Load Lua code and extract all required data
            Cartridge cartridge = inputFormat.Load();

            // ---------- Convert cartridge for engine ----------

            // Create selected player
            IEngine engine = CreateEngine(device);

            // Convert Lua code and insert special code for this player
            cartridge = engine.ConvertCartridge(cartridge);
            userName  = engine.ConvertString(userName);

            // Now we can close the input, because we don't require it anymore
            ifs.Close();

            // ---------- Compile Lua code into binary chunk ----------

            // Compile Lua code
            cartridge.Chunk = LUA.Compile(cartridge.LuaCode, cartridge.LuaFileName);

            // ---------- Save cartridge as GWC file ----------

            // Create object for output format (could be also WFC or any other IOutput)
            var outputFormat = new GWC();

            // Write output file
            try {
                // Create output in correct format
                var ms = outputFormat.Create(cartridge, userName, 0, completitionCode);
                return(ms);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }
        }
示例#5
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
        }
示例#6
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
        }
示例#7
0
        /// <summary>
        /// The entry point of the program, where the program control starts and ends when used from command line.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        public static void Main(string[] args)
        {
            var start  = DateTime.Now;
            var device = DeviceType.Unknown;

            Console.WriteLine("WF.Compiler, Version {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
            Console.WriteLine("Copyright 2014 by Wherigo Foundation");
            Console.WriteLine();

            string fileInput  = null;
            string fileOutput = null;

            int    userId           = 0;
            string userName         = "******";
            string completitionCode = "123456789ABCDEF";

            int i = 0;

            while (i < args.Length)
            {
                switch (args[i].ToLower())
                {
                case "-d":
                case "-device":
                    if (i + 1 >= args.Length)
                    {
                        Usage();
                        return;
                    }
                    string deviceName = args[i + 1].ToLower();
                    i++;
                    foreach (DeviceType d in Enum.GetValues(typeof(DeviceType)))
                    {
                        if (d.ToString().ToLower() == deviceName)
                        {
                            device = d;
                        }
                    }
                    break;

                case "-o":
                case "-output":
                    if (i + 1 >= args.Length)
                    {
                        Usage();
                        return;
                    }
                    fileOutput = args[i + 1].ToLower();
                    i++;
                    break;

                case "-u":
                case "-username":
                    if (i + 1 >= args.Length)
                    {
                        Usage();
                        return;
                    }
                    userName = args[i + 1];
                    i++;
                    break;

                case "-c":
                case "-code":
                    if (i + 1 >= args.Length)
                    {
                        Usage();
                        return;
                    }
                    completitionCode = args[i + 1];
                    i++;
                    break;

                default:
                    fileInput = args[i];
                    break;
                }
                i++;
            }

            if (fileInput == null)
            {
                Usage();
                return;
            }

            if (!File.Exists(fileInput))
            {
                Usage();
                return;
            }


            if (fileOutput == null)
            {
                fileOutput = Path.ChangeExtension(fileInput, ".gwc");
            }

            Console.WriteLine("Input file: " + fileInput);
            Console.WriteLine("Output file: " + fileOutput);
            Console.WriteLine("Device: " + device.ToString());
            Console.WriteLine("Username: "******"CompletionCode: " + completitionCode);

            FileStream ifs = new FileStream(fileInput, FileMode.Open);

            // ---------- Create GWZ file only (required for upload and download of GWZ file) ----------

            // Create object für reading input file (could be also any other format implementing IInput)
            var inputFormat = new GWZ(ifs);

            // ---------- Check GWZ file (only required for upload of GWZ file) ----------

            // Check gwz file for errors (Lua code and all files included)
            try {
                inputFormat.Check();
            }
            catch (CompilerLuaException e)
            {
                Console.WriteLine();
                Console.WriteLine("Error");
                Console.WriteLine(e.Message);
                Console.WriteLine();
                Console.WriteLine("Line {0}: {1}", e.Line - 1, e.CodeBefore);
                Console.WriteLine("Line {0}: {1}", e.Line, e.Code);
                Console.WriteLine("Line {0}: {1}", e.Line + 1, e.CodeAfter);
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine();
                Console.WriteLine("Error");
                Console.WriteLine(e.Message);
                ifs.Close();
                return;
            }

            // ---------- Load cartridge from GWZ file (required when downloading cartridge) ----------

            // Load Lua code and extract all required data
            Cartridge cartridge = inputFormat.Load();

            // ---------- Convert cartridge for engine ----------

            // Create selected engine
            IEngine engine = CreateEngine(device);

            // Convert Lua code and insert special code for this player
            cartridge = engine.ConvertCartridge(cartridge);
            userName  = engine.ConvertString(userName);

            // Now we can close the input, because we don't require it anymore
            ifs.Close();

            // ---------- Compile Lua code into binary chunk ----------

            try {
                // Compile Lua code
                cartridge.Chunk = LUA.Compile(cartridge.LuaCode, cartridge.LuaFileName);
            }
            catch (Exception e)
            {
                var t = e.Message;
            }

            // ---------- Save cartridge as GWC file ----------

            // Create object for output format (could be also WFC or any other IOutput)
            var outputFormat = new GWC();

            // Write output file
            try {
                // Create output in correct format
                var ms = outputFormat.Create(cartridge, userName, userId, completitionCode);
                // Save output to file
                using (FileStream ofs = new FileStream(fileOutput, FileMode.Create)) {
                    ms.CopyTo(ofs);
                    // Close output
                    ofs.Flush();
                    ofs.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine();
                Console.WriteLine("Error");
                Console.WriteLine(e.Message);
                return;
            }

            Console.WriteLine("Compiletime: {0}", DateTime.Now - start);
        }