コード例 #1
0
        /// <summary>
        /// Transforms the name of the output. This will strip the leading directories from the stanza file from
        /// output name and then combine it with the installDir.
        /// EX: "kOS-1.1/GameData/kOS", "kOS-1.1/GameData/kOS/Plugins/kOS.dll", "GameData" will be transformed
        /// to "GameData/kOS/Plugins/kOS.dll"
        /// </summary>
        /// <returns>The output name.</returns>
        /// <param name="file">The file directive of the stanza.</param>
        /// <param name="outputName">The name of the file to transform.</param>
        /// <param name="installDir">The installation dir where the file should end up with.</param>
        internal static string TransformOutputName(string file, string outputName, string installDir, string @as)
        {
            string leadingPathToRemove = KSPPathUtils.GetLeadingPathElements(file);

            // Special-casing, if stanza.file is just "GameData" or "Ships", strip it.
            // TODO: Do we need to do anything special for tutorials or GameRoot?
            if (
                leadingPathToRemove == string.Empty &&
                (file == "GameData" || file == "Ships")
                )
            {
                leadingPathToRemove = file;

                // It's unclear what the behavior should be in this special case if `as` is specified, therefore
                // disallow it.
                if (!string.IsNullOrWhiteSpace(@as))
                {
                    throw new BadMetadataKraken(null, "Cannot specify `as` if `file` is GameData or Ships.");
                }
            }

            // If there's a leading path to remove, then we have some extra work that
            // needs doing...
            if (leadingPathToRemove != string.Empty)
            {
                string leadingRegEx = "^" + Regex.Escape(leadingPathToRemove) + "/";
                if (!Regex.IsMatch(outputName, leadingRegEx))
                {
                    throw new BadMetadataKraken(null,
                                                String.Format("Output file name ({0}) not matching leading path of stanza.file ({1})",
                                                              outputName, leadingRegEx
                                                              )
                                                );
                }
                // Strip off leading path name
                outputName = Regex.Replace(outputName, leadingRegEx, "");
            }

            // If an `as` is specified, replace the first component in the file path with the value of `as`
            // This works for both when `find` specifies a directory and when it specifies a file.
            if (!string.IsNullOrWhiteSpace(@as))
            {
                if ([email protected]("/") && [email protected]("\\"))
                {
                    var components = outputName.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    components[0] = @as;

                    outputName = string.Join("/", components);
                }
                else
                {
                    throw new BadMetadataKraken(null, "`as` may not include path seperators.");
                }
            }

            // Return our snipped, normalised, and ready to go output filename!
            return(KSPPathUtils.NormalizePath(
                       Path.Combine(installDir, outputName)
                       ));
        }
コード例 #2
0
ファイル: ModuleInstaller.cs プロジェクト: CodingWoes/CKAN
        /// <summary>
        /// Transforms the name of the output. This will strip the leading directories from the stanza file from
        /// output name and then combine it with the installDir.
        /// EX: "kOS-1.1/GameData/kOS", "kOS-1.1/GameData/kOS/Plugins/kOS.dll", "GameData" will be transformed
        /// to "GameData/kOS/Plugins/kOS.dll"
        /// </summary>
        /// <returns>The output name.</returns>
        /// <param name="file">The file directive of the stanza.</param>
        /// <param name="outputName">The name of the file to transform.</param>
        /// <param name="installDir">The installation dir where the file should end up with.</param>
        internal static string TransformOutputName(string file, string outputName, string installDir)
        {
            string leadingPathToRemove = KSPPathUtils.GetLeadingPathElements(file);

            // Special-casing, if stanza.file is just "GameData" or "Ships", strip it.
            // TODO: Do we need to do anything special for tutorials or GameRoot?
            if (
                leadingPathToRemove == string.Empty &&
                (file == "GameData" || file == "Ships")
                )
            {
                leadingPathToRemove = file;
            }

            // If there's a leading path to remove, then we have some extra work that
            // needs doing...
            if (leadingPathToRemove != string.Empty)
            {
                string leadingRegEx = "^" + Regex.Escape(leadingPathToRemove) + "/";
                if (!Regex.IsMatch(outputName, leadingRegEx))
                {
                    throw new BadMetadataKraken(null,
                                                String.Format("Output file name ({0}) not matching leading path of stanza.file ({1})",
                                                              outputName, leadingRegEx
                                                              )
                                                );
                }
                // Strip off leading path name
                outputName = Regex.Replace(outputName, leadingRegEx, "");
            }

            // Return our snipped, normalised, and ready to go output filename!
            return(KSPPathUtils.NormalizePath(
                       Path.Combine(installDir, outputName)
                       ));
        }