public int ChangeKey(string SourcePackage, string TargetPackage, byte[] OldKey, byte[] NewKey)
        {
            log("Changing key for {0} / {1}", SourcePackage, TargetPackage);

            string osz2FilenameOld = s3getToTemp("osz2/" + SourcePackage);
            string osz2FilenameNew = osz2FilenameOld + ".new";

            try
            {
                //ensure an existing new file doesn't existing exist.
                File.Delete(osz2FilenameNew);

                if (!File.Exists(osz2FilenameOld))
                {
                    log("couldn't find local file {0}", osz2FilenameOld);
                    return((int)UpdateResponseCode.FileDoesNotExist);
                }

                using (MapPackage oldPackage = new MapPackage(osz2FilenameOld, OldKey, false, false))
                    using (MapPackage newPackage = new MapPackage(osz2FilenameNew, NewKey, true, false))
                    {
                        Dictionary <MapMetaType, string> metaData = oldPackage.GetAllMetadata();

                        foreach (KeyValuePair <MapMetaType, string> mapMeta in metaData)
                        {
                            newPackage.AddMetadata(mapMeta.Key, mapMeta.Value);
                        }

                        List <FileInfo> fileInfo = oldPackage.GetFileInfo();
                        foreach (FileInfo fo in fileInfo)
                        {
                            using (var br = new BinaryReader(oldPackage.GetFile(fo.Filename)))
                            {
                                newPackage.AddFile(fo.Filename, br.ReadBytes((int)br.BaseStream.Length),
                                                   fo.CreationTime, fo.ModifiedTime);
                            }
                        }

                        newPackage.Save();
                    }

                s3putFile("osz2/" + TargetPackage, osz2FilenameNew);
                File.Delete(osz2FilenameNew);
            }
            catch (Exception e)
            {
                File.Delete(osz2FilenameOld);
                File.Delete(osz2FilenameNew);
                log(e);
            }

            return((int)UpdateResponseCode.UpdateSuccessful);
        }
Example #2
0
        private static void writePackage(string oscFilename, string osz2Filename, string audioFilename, List <BeatmapDifficulty> difficulties, List <string> ordered)
        {
            bool isPreview = osz2Filename.Contains("_preview");

            int hitObjectCutoff = 0;

            using (StreamWriter output = new StreamWriter(oscFilename))
            {
                //write headers first (use first difficulty as arbitrary source)
                foreach (string l in headerContent)
                {
                    if (isPreview)
                    {
                        if (l.StartsWith("Bookmarks:") && osz2Filename.Contains("_preview"))
                        {
                            //may need to double up on bookmarks if they don't occur often often

                            List <int> switchPoints = Player.Beatmap.StreamSwitchPoints;

                            if (switchPoints.Count < 10 || switchPoints[9] > 60000)
                            {
                                string switchString = "Bookmarks:";

                                foreach (int s in switchPoints)
                                {
                                    switchString += s.ToString(nfi) + ",";
                                    switchString += s.ToString(nfi) + ","; //double bookmark hack for previews
                                }

                                output.WriteLine(switchString.Trim(','));

                                hitObjectCutoff = switchPoints.Count < 10 ? switchPoints[4] : switchPoints[9];
                                continue;
                            }
                        }
                    }

                    output.WriteLine(l);
                }

                //keep track of how many hitObject lines are remaining for each difficulty
                int[] linesRemaining = new int[difficulties.Count];
                for (int i = 0; i < difficulties.Count; i++)
                {
                    linesRemaining[i] = difficulties[i] == null ? 0 : difficulties[i].HitObjectLines.Count;
                }

                int currentTime = 0;

                while (!linesRemaining.All(i => i == 0))
                {
                    int           bestMatchDifficulty = -1;
                    HitObjectLine bestMatchLine       = null;

                    for (int i = 0; i < difficulties.Count; i++)
                    {
                        if (linesRemaining[i] == 0)
                        {
                            continue;
                        }

                        int holOffset = difficulties[i].HitObjectLines.Count - linesRemaining[i];

                        HitObjectLine line = difficulties[i].HitObjectLines[holOffset];

                        if (isPreview && hitObjectCutoff > 0 && line.Time > hitObjectCutoff)
                        {
                            linesRemaining[i]--;
                            continue;
                        }

                        if (line.Time >= currentTime && (bestMatchLine == null || line.Time < bestMatchLine.Time))
                        {
                            bestMatchDifficulty = i;
                            bestMatchLine       = line;
                        }
                    }

                    if (bestMatchLine != null)
                    {
                        output.WriteLine(bestMatchDifficulty + "," + bestMatchLine.StringRepresentation);
                        linesRemaining[bestMatchDifficulty]--;
                    }
                }
            }

            using (MapPackage package = new MapPackage(osz2Filename, true))
            {
                package.AddMetadata(MapMetaType.BeatmapSetID, "0");

                string versionsAvailable = "";
                if (ordered[0] != null)
                {
                    versionsAvailable += "|Easy";
                }
                if (ordered[1] != null)
                {
                    versionsAvailable += "|Normal";
                }
                if (ordered[2] != null)
                {
                    versionsAvailable += "|Hard";
                }
                if (ordered[3] != null)
                {
                    versionsAvailable += "|Expert";
                }
                package.AddMetadata(MapMetaType.Version, versionsAvailable.Trim('|'));

                if (string.IsNullOrEmpty(audioFilename))
                {
                    throw new Exception("FATAL ERROR: audio file not found");
                }

                package.AddFile(Path.GetFileName(oscFilename), oscFilename, DateTime.MinValue, DateTime.MinValue);
                if (isPreview)
                {
                    if (!File.Exists(audioFilename.Replace(".m4a", "_lq.m4a")))
                    {
                        Console.WriteLine("WARNING: missing preview audio file (_lq.m4a)");
                        return;
                    }

                    package.AddFile("audio.m4a", audioFilename.Replace(".m4a", "_lq.m4a"), DateTime.MinValue, DateTime.MinValue);
                }
                else
                {
                    package.AddFile(audioFilename.EndsWith(".m4a") ? "audio.m4a" : "audio.mp3", audioFilename, DateTime.MinValue, DateTime.MinValue);
                }

                string dir = Path.GetDirectoryName(audioFilename);

                string metadata = dir + "\\metadata.txt";
                if (File.Exists(metadata))
                {
                    foreach (string line in File.ReadAllLines(metadata))
                    {
                        if (line.Length == 0)
                        {
                            continue;
                        }

                        string[] var = line.Split(':');
                        string   key = string.Empty;
                        string   val = string.Empty;
                        if (var.Length > 1)
                        {
                            key = line.Substring(0, line.IndexOf(':'));
                            val = line.Substring(line.IndexOf(':') + 1).Trim();

                            MapMetaType t = (MapMetaType)Enum.Parse(typeof(MapMetaType), key, true);
                            package.AddMetadata(t, val);
                        }
                    }
                }

                if (isPreview)
                {
                    package.AddMetadata(MapMetaType.Revision, "preview");
                }

                string thumb = dir + "\\thumb-128.jpg";
                if (File.Exists(thumb))
                {
                    package.AddFile("thumb-128.jpg", thumb, DateTime.MinValue, DateTime.MinValue);
                }

                thumb = Path.GetDirectoryName(audioFilename) + "\\thumb-256.jpg";
                if (File.Exists(thumb))
                {
                    package.AddFile("thumb-256.jpg", thumb, DateTime.MinValue, DateTime.MinValue);
                }

                thumb = Path.GetDirectoryName(audioFilename) + "\\thumb-512.jpg";
                if (File.Exists(thumb))
                {
                    package.AddFile("thumb-512.jpg", thumb, DateTime.MinValue, DateTime.MinValue);
                }

                package.Save();
            }
        }