Exemple #1
0
        public void CopyTo(SSBaseLinkUrl newPath)
        {
            SSBaseUrl InstallationUrl = new SSBaseUrl(newPath.Base);
            SSFullUrl SourceUrl       = InstallationUrl + this.SourceMod.ModUrl.GetLink() + this.RelativeUrl;

            SSFullUrl TargetUrl = newPath + this.RelativeUrl;

            FileInfo sourceInfo = new FileInfo(SourceUrl.ToString());
            FileInfo targetInfo = new FileInfo(TargetUrl.ToString());

            if (targetInfo.Exists)
            {
                targetInfo = new FileInfo(TargetUrl.ToString() + SourceMod.ModName);
            }
            DirectoryInfo targetDir = targetInfo.Directory;

            if (!targetDir.Exists)
            {
                targetDir.Create();
            }
            sourceInfo.CopyTo(targetInfo.FullName);
        }
        public void ReadMods(string targetFolder = null)
        {
            Mods.Clear();
            modFactory = new SSModFactory(InstallationUrl);
            SSBaseUrl     ModFolderPath = InstallationUrl + "mods";
            DirectoryInfo ModsDirectory = new DirectoryInfo(ModFolderPath.ToString());
            IEnumerable <DirectoryInfo> ModsEnumerable = ModsDirectory.EnumerateDirectories();


            SSMod currentMod = modFactory.CreateMod(new SSLinkUrl("starsector-core"));

            //currentMod.FindFiles();
            Mods.Add(currentMod);
            foreach (DirectoryInfo ModDirectory in ModsEnumerable)
            {
                SSLinkUrl modLink = new SSLinkUrl(Path.Combine("mods", ModDirectory.Name));
                SSMod     exist   = Mods.FirstOrDefault(M => M.ModUrl.Link.Equals(modLink.Link));
                if (exist != null)
                {
                    throw new ArgumentException("Cannot add existing mod to directory");
                }
                try
                {
                    currentMod = modFactory.CreateMod(modLink);
                    //if (ModDirectory.Name == targetFolder)
                    //currentMod.ChangeType(ModType.Skip);
                    //else
                    //currentMod.FindFiles();

                    Mods.Add(currentMod);
                }
                catch (FileNotFoundException)
                {
                    //incomplete mod, lets just not add it
                }
            }
        }
 public void SetUrl(string directoryUrl)
 {
     InstallationUrl = new SSBaseUrl(directoryUrl);
 }
 public SSDirectory(SSBaseUrl url)
 {
     InstallationUrl = url;
 }
        public override void WriteTo(SSBaseLinkUrl newPath)
        {
            SSBaseUrl InstallationUrl = new SSBaseUrl(newPath.Base);
            SSFullUrl TargetUrl       = newPath + this.RelativeUrl;
            //we do not merge core csv on the patch, it would be pointless
            List <SSCsv> NonCoreFile = (from SSCsv file in this.CommonFiles
                                        where file.SourceMod.CurrentType != ModType.Core
                                        select file).ToList <SSCsv>();

            if (NonCoreFile.Count() == 0)
            {
                return;
            }

            //we need to make sure the directory exist
            FileInfo      targetInfo = new FileInfo(TargetUrl.ToString());
            DirectoryInfo targetDir  = targetInfo.Directory;

            if (!targetDir.Exists)
            {
                targetDir.Create();
            }
            Factory csvFactory = new Factory();

            //csv merge requires us to match column tutle
            using (StreamWriter sw = File.CreateText(TargetUrl.ToString()))
            {
                CsvWriter             csvWriter  = new CsvWriter(sw, CultureInfo.InvariantCulture);
                List <List <String> > allHeaders = new List <List <string> >();
                foreach (SSCsv file in NonCoreFile)
                {
                    SSFullUrl SourceUrl = InstallationUrl + file.SourceMod.ModUrl.GetLink() + file.RelativeUrl;

                    using (StreamReader sr = File.OpenText(SourceUrl.ToString()))
                    {
                        IParser parser = csvFactory.CreateParser(sr, CultureInfo.InvariantCulture);
                        allHeaders.Add(parser.Read().ToList <string>());
                    }
                }
                List <string> finalHeaders = allHeaders.SelectMany(c => c).Distinct().ToList <string>();
                //lets write column title once
                foreach (string head in finalHeaders)
                {
                    csvWriter.WriteField(head);
                }
                csvWriter.NextRecord();

                for (int fileIndex = 0; fileIndex < NonCoreFile.Count; fileIndex++)
                {
                    SSFullUrl     SourceUrl            = InstallationUrl + NonCoreFile[fileIndex].SourceMod.ModUrl.GetLink() + NonCoreFile[fileIndex].RelativeUrl;
                    List <string> localHeaders         = allHeaders[fileIndex];
                    int[]         finalSourceFromLocal = new int[finalHeaders.Count];
                    for (int finalIndex = 0; finalIndex < finalHeaders.Count; finalIndex++)
                    {
                        finalSourceFromLocal[finalIndex] = localHeaders.FindIndex(c => c == finalHeaders[finalIndex]);
                    }
                    using (StreamReader sr = File.OpenText(SourceUrl.ToString()))
                    {
                        IParser parser = csvFactory.CreateParser(sr, CultureInfo.InvariantCulture);
                        parser.Configuration.AllowComments = true;
                        parser.Configuration.BadDataFound  = null;

                        string[] line = parser.Read(); //skip first line that contain headers
                        line = parser.Read();
                        while (line != null)
                        {
                            if (line.Count() != localHeaders.Count)
                            {
                                line = parser.Read();
                                continue;
                            }
                            foreach (int source in finalSourceFromLocal)
                            {
                                if (source == -1)
                                {
                                    csvWriter.WriteField("");
                                }
                                else
                                {
                                    csvWriter.WriteField(line[source]);
                                }
                            }
                            csvWriter.NextRecord();
                            line = parser.Read();
                        }
                    }
                }
            }
        }