private static ListViewItem CreateItem(SledLuaCompileConfigurationType configType)
        {
            var lstItem =
                new ListViewItem(
                    new[]
            {
                configType.Name,
                configType.LittleEndian ? Little : Big,
                configType.SizeOfInt.ToString(),
                configType.SizeOfSizeT.ToString(),
                configType.SizeOfLuaNumber.ToString(),
                configType.StripDebugInfo ? Yes : No,
                configType.OutputPath,
                configType.OutputExtension = Bin,
                configType.PreserveRelativePathInfo ? Yes : No
            })
            {
                Tag = configType, Checked = configType.Selected
            };

            return(lstItem);
        }
Esempio n. 2
0
        private void Compile(SledLuaCompileConfigurationType configType)
        {
            if (configType == null)
            {
                return;
            }

            // Gather files owned by the Lua plugin
            IList <SledProjectFilesFileType> lstFiles =
                new List <SledProjectFilesFileType>(
                    m_projectFileGathererService.Get.GetFilesOwnedByPlugin(
                        m_luaLanguagePlugin));

            // No files so nothing to compile
            if (lstFiles.Count <= 0)
            {
                SledOutDevice.OutLine(
                    SledMessageType.Info,
                    Localization.SledLuaCompilerErrorNoPluginOwnedFiles);

                return;
            }

            IList <SledProjectFilesFileType> lstNoCompileFiles =
                new List <SledProjectFilesFileType>();

            // Gather any files aren't supposed to be compiled
            foreach (var file in lstFiles)
            {
                var bCompileFile = false;

                foreach (SledLuaCompileAttributeType attr in file.Attributes)
                {
                    if (!attr.Is <SledLuaCompileAttributeType>())
                    {
                        continue;
                    }

                    var luaAttr =
                        attr.As <SledLuaCompileAttributeType>();

                    bCompileFile = luaAttr.Compile;
                }

                // Don't compile the file
                if (!bCompileFile)
                {
                    lstNoCompileFiles.Add(file);
                }
            }

            // Remove any files that aren't supposed to be compiled
            foreach (var file in lstNoCompileFiles)
            {
                lstFiles.Remove(file);
            }

            // No files so nothing to compile
            if (lstFiles.Count <= 0)
            {
                SledOutDevice.OutLine(
                    SledMessageType.Info,
                    Localization.SledLuaCompilerErrorNoFiles);

                return;
            }

            ILuaCompiler compiler = null;

            try
            {
                // Create compiler based on platform
                compiler = SledLuaCompilerServiceFactory.Create();

                // Set up configuration
                var config =
                    new LuaCompilerConfig(
                        configType.LittleEndian
                            ? LuaCompilerConfig.Endian.Little
                            : LuaCompilerConfig.Endian.Big,
                        configType.SizeOfInt,
                        configType.SizeOfSizeT,
                        configType.SizeOfLuaNumber,
                        configType.StripDebugInfo);

                // Compile each file individually
                foreach (var file in lstFiles)
                {
                    try
                    {
                        //
                        // Fix up path to respect users' configType settings
                        //

                        // Fix up extension
                        var dumpPath = Path.ChangeExtension(file.AbsolutePath, configType.OutputExtension);

                        // Grab new file name w/ updated extension
                        var newName = Path.GetFileName(dumpPath);
                        if (string.IsNullOrEmpty(newName))
                        {
                            throw new InvalidOperationException("new filename null or empty");
                        }

                        // Fix up output directory
                        if (configType.PreserveRelativePathInfo)
                        {
                            // Get relative path hierarchy
                            var dirHierarchy = Path.GetDirectoryName(file.Path);
                            dumpPath = string.Format("{0}{1}{2}{1}{3}", configType.OutputPath, Path.DirectorySeparatorChar, dirHierarchy, newName);
                        }
                        else
                        {
                            // Just take output path + new name (which includes new extension)
                            dumpPath = string.Format("{0}{1}{2}", configType.OutputPath, Path.DirectorySeparatorChar, newName);
                        }

                        dumpPath = Path.GetFullPath(dumpPath);

                        if (m_bVerbose)
                        {
                            SledOutDevice.OutLine(
                                SledMessageType.Info,
                                "[Lua compiler] Compiling {0} to {1}",
                                file.AbsolutePath, dumpPath);
                        }

                        // Make sure directory exists before trying to place compiled script there
                        var newDir = Path.GetDirectoryName(dumpPath);
                        if (string.IsNullOrEmpty(newDir))
                        {
                            throw new InvalidOperationException("new directory null or empty");
                        }

                        if (!Directory.Exists(newDir))
                        {
                            var bDirExists = false;
                            var message    = string.Empty;

                            try
                            {
                                Directory.CreateDirectory(newDir);
                                bDirExists = true;
                            }
                            catch (UnauthorizedAccessException ex)  { message = ex.Message; }
                            catch (ArgumentNullException ex)        { message = ex.Message; }
                            catch (ArgumentException ex)            { message = ex.Message; }
                            catch (PathTooLongException ex)         { message = ex.Message; }
                            catch (DirectoryNotFoundException ex)   { message = ex.Message; }
                            catch (NotSupportedException ex)        { message = ex.Message; }
                            catch (IOException ex)                  { message = ex.Message; }

                            // Show message if directory couldn't be created
                            if (!bDirExists)
                            {
                                // Can't compile script to user supplied destination directory
                                // because the directory doesn't exist and can't be created!
                                SledOutDevice.OutLine(
                                    SledMessageType.Error,
                                    SledUtil.TransSub(Localization.SledLuaCompilerErrorCantCreateDir, message));

                                // Skip this file...
                                continue;
                            }
                        }

                        // Try and compile file
                        var succeeded = compiler.Compile(new Uri(file.AbsolutePath), new Uri(dumpPath), config);
                        SledOutDevice.OutLine(
                            SledMessageType.Info,
                            succeeded
                                ? SledUtil.TransSub(Localization.SledLuaCompilerErrorSuccess, file.Name)
                                : SledUtil.TransSub(Localization.SledLuaCompilerErrorFailed, file.Name, compiler.Error));
                    }
                    catch (Exception ex2)
                    {
                        SledOutDevice.OutLine(
                            SledMessageType.Error,
                            SledUtil.TransSub(Localization.SledLuaCompilerErrorExceptionCompilingFile, file.Name, ex2.Message));
                    }
                }
            }
            catch (Exception ex1)
            {
                SledOutDevice.OutLine(
                    SledMessageType.Error,
                    SledUtil.TransSub(Localization.SledLuaCompilerErrorExceptionInLuaCompiler, ex1.Message));
            }
            finally
            {
                if (compiler != null)
                {
                    compiler.Dispose();
                }
            }
        }