/// <summary>
        /// This function finds the new info.pac and common2.pac. It should be called whenever you change the working directory.
        /// It also clears the list of edited ("dirty") strings, and records the current names (for the "restore" button).
        /// </summary>
        public String findInfoFile()
        {
            _index = -1;

            info              = info_training = null;
            _currentFile      = _currentTrainingFile = null;
            common2_titledata = new List <SongIndexEntry>(0);

            string tempfile = Path.GetTempFileName();

            string[] sndBgmTitleDataPaths = { "..\\..\\system\\common2.pac", "..\\..\\system\\common2_en.pac", "..\\common2.pac" };
            foreach (string relativepath in sndBgmTitleDataPaths)
            {
                string s = Path.GetFullPath(relativepath);
                if (new FileInfo(s).Exists)
                {
                    File.Copy(s, tempfile, true);
                    using (var node = NodeFactory.FromFile(null, tempfile)) {
                        foreach (ResourceNode child in node.Children)
                        {
                            if (child is Common2MiscDataNode)
                            {
                                SndBgmTitleDataNode sndBgmTitleData = child.Children.FirstOrDefault() as SndBgmTitleDataNode;
                                if (sndBgmTitleData != null)
                                {
                                    common2_titledata = sndBgmTitleData.Children.Select(n => new SongIndexEntry()
                                    {
                                        ID    = (ushort)((SndBgmTitleEntryNode)n).ID,
                                        Index = ((SndBgmTitleEntryNode)n).SongTitleIndex
                                    }).ToList();
                                    break;
                                }
                            }
                        }
                    }
                }
                if (common2_titledata.Count > 0)
                {
                    break;
                }
            }

            if (common2_titledata.Count == 0)
            {
                common2_titledata = SongIDMap.Songs.Where(s => s.InfoPacIndex != null).Select(s => new SongIndexEntry()
                {
                    ID    = s.ID,
                    Index = s.InfoPacIndex ?? 0
                }).ToList();
            }

            tempfile = Path.GetTempFileName();
            if (new FileInfo("Misc Data [140].msbin").Exists)
            {
                _currentFile = "Misc Data [140].msbin";
                File.Copy("Misc Data [140].msbin", tempfile, true);
                info = NodeFactory.FromFile(null, tempfile) as MSBinNode;
                return("Loaded .\\Misc Data [140].msbin");
            }
            else if (new FileInfo("\\Misc Data [140].msbin").Exists)
            {
                _currentFile = "\\Misc Data [140].msbin";
                File.Copy("\\Misc Data [140].msbin", tempfile, true);
                info = NodeFactory.FromFile(null, tempfile) as MSBinNode;
                return("Loaded \\Misc Data [140].msbin");
            }
            else
            {
                string[] infopaths = { "..\\..\\info2\\info.pac", "..\\..\\info2\\info_en.pac", "..\\info.pac" };

                foreach (string relativepath in infopaths)
                {
                    if (info == null)
                    {
                        string s = Path.GetFullPath(relativepath);
                        if (new FileInfo(s).Exists)
                        {
                            _currentFile = s;
                            File.Copy(s, tempfile, true);
                            info_pac = NodeFactory.FromFile(null, tempfile);
                            info     = (MSBinNode)info_pac.FindChild("Misc Data [140]", true);
                        }
                    }
                }

                if (info == null)
                {
                    return("No song list loaded");
                }
                else
                {
                    modifiedStringIndices.Clear();
                    copyIntoFileStrings();

                    // info found; try info_training in same directory
                    string trainingpath = _currentFile.Replace("info.pac", "info_training.pac").Replace("info_en.pac", "info_training_en.pac");
                    if (trainingpath != _currentFile && new FileInfo(trainingpath).Exists)
                    {
                        _currentTrainingFile = trainingpath;
                        string tempfile_training = Path.GetTempFileName();
                        File.Copy(trainingpath, tempfile_training, true);
                        info_training_pac = NodeFactory.FromFile(null, tempfile_training);
                        info_training     = (MSBinNode)info_training_pac.FindChild("Misc Data [140]", true);
                        if (info._strings.Count != info_training._strings.Count)
                        {
                            MessageBox.Show("info.pac and info_training.pac have different Misc Data [140] lengths. Ignoring info_training.pac.");
                            info_training     = null;
                            info_training_pac = null;
                        }
                    }
                    return(info_training != null ? "Loaded info.pac and info_training.pac" : "Loaded info.pac");
                }
            }
        }
示例#2
0
        private ActionResult Generate(string csv, string gctPath, ResourceNode sc_selmap, string relpath, ResourceNode info_pac, ResourceNode common2, string brstmPath)
        {
            Func <string, List <string> > split = s => {
                List <string> list    = new List <string>();
                StringBuilder current = new StringBuilder();
                bool          inQuote = false;
                foreach (char c in s)
                {
                    if (c == '"')
                    {
                        inQuote = !inQuote;
                    }
                    else if (c == ',' && !inQuote)
                    {
                        list.Add(current.ToString());
                        current.Clear();
                    }
                    else
                    {
                        current.Append(c);
                    }
                }
                list.Add(current.ToString());
                return(list);
            };

            List <CEPStage> stages    = new List <CEPStage>();
            IList <string>  firstLine = null;

            foreach (string line in System.IO.File.ReadAllLines(csv, Encoding.UTF8))
            {
                if (firstLine == null)
                {
                    firstLine = split(line);
                }
                else
                {
                    CEPStage s = new CEPStage();
                    int      i = 0;
                    foreach (string cell in split(line))
                    {
                        if (cell != "")
                        {
                            var prop = typeof(CEPStage).GetProperty(firstLine[i]);
                            if (prop?.PropertyType == typeof(bool))
                            {
                                prop.SetValue(s, bool.Parse(cell));
                            }
                            else
                            {
                                prop?.SetValue(s, cell);
                            }
                        }
                        i++;
                    }
                    stages.Add(s);
                }
            }

            byte[] gct = System.IO.File.ReadAllBytes(gctPath);
            var    sss = new BrawlManagerLib.CustomSSSCodeset(gct);

            foreach (CEPStage s in stages)
            {
                try {
                    string relname = StageIDMap.RelNameForPac(s.Filename, true);
                    using (var stream = new FileStream(Path.Combine(relpath, relname), FileMode.Open, FileAccess.Read)) {
                        stream.Seek(3, SeekOrigin.Begin);
                        s.ModuleBase = RelNames[stream.ReadByte()];
                    }
                } catch (FileNotFoundException) { }

                if (s.Alternate)
                {
                    continue;
                }
                int iconId          = sss.IconForStage(s.Stage.ID);
                TextureContainer tc = new TextureContainer(sc_selmap, iconId);
                if (tc.icon_tex0 != null)
                {
                    using (MemoryStream ms = new MemoryStream()) {
                        using (Bitmap b = tc.icon_tex0.GetImage(0)) {
                            b.Save(ms, ImageFormat.Png);
                            s.PngIcon = ms.ToArray();
                        }
                    }
                }
            }

            MSBinNode info = (MSBinNode)info_pac.FindChild("MiscData[140]", true);
            var       common2_titledata = new List <SongNameBar.SongIndexEntry>(0);

            foreach (ResourceNode child in common2.Children)
            {
                if (child is Common2MiscDataNode)
                {
                    SndBgmTitleDataNode sndBgmTitleData = child.Children.FirstOrDefault() as SndBgmTitleDataNode;
                    if (sndBgmTitleData != null)
                    {
                        common2_titledata = sndBgmTitleData.Children.Select(n => new SongNameBar.SongIndexEntry()
                        {
                            ID    = (ushort)((SndBgmTitleEntryNode)n).ID,
                            Index = ((SndBgmTitleEntryNode)n).SongTitleIndex
                        }).ToList();
                        break;
                    }
                }
            }

            List <string> brstms = Directory.EnumerateFiles(brstmPath)
                                   .Select(s => Path.GetFileNameWithoutExtension(s)).ToList();

            var sdsl = new StageDependentSongLoader(gct);

            foreach (CEPStage stage in stages)
            {
                Song song = sdsl.GetSong(stage.Stage.ID);
                if (song != null)
                {
                    stage.SongFilename = song.Filename;
                    stage.SongTitle    = info._strings[common2_titledata.Where(c => c.ID == song.ID).Select(c => c.Index).First()];
                    //brstms.RemoveAll(s => s == song.Filename);
                }
            }

            var expStages = stages.Where(s => s.Filename.Contains("CUSTOM"));

            return(View(new MainModel {
                Stages = stages.Except(expStages).Concat(expStages).ToList(),
                Songs = brstms.Select(b => {
                    var song = SongIDMap.Songs.SingleOrDefault(s => s.Filename == b);
                    return new CEPSong {
                        SongFilename = b,
                        SongTitle = info._strings[(
                                                      from c in common2_titledata
                                                      where c.ID == song.ID
                                                      select c.Index
                                                      ).SingleOrDefault()],
                        OriginalSongTitle = b.StartsWith("0000") ? null : song.DefaultName
                    };
                })
            }));
        }