public static Track LoadTrack(string trackfile)
        {
            Track ret = new Track();

            ret.Filename = trackfile;
            track_json trackobj;

            using (var file = File.OpenRead(trackfile))
            {
                try
                {
                    trackobj = JsonSerializer.Deserialize <track_json>(file);
                }
                catch (Exception ex)
                {
                    throw new TrackIO.TrackLoadException(
                              "Json parsing error.\n" + ex.Message);
                }
            }
            switch (trackobj.version)
            {
            case "6.1":
                ret.SetVersion(61);
                break;

            case "6.2":
                ret.SetVersion(62);
                break;

            default:
                throw new TrackIO.TrackLoadException(
                          "Unsupported physics.");
            }
            ret.Name = trackobj.label;
            if (trackobj.startZoom != 0)
            {
                ret.StartZoom = (float)MathHelper.Clamp(
                    trackobj.startZoom,
                    Utils.Constants.MinimumZoom,
                    Utils.Constants.MaxZoom);
            }
            ret.ZeroStart = trackobj.zeroStart;
            if (trackobj.startPosition != null)
            {
                ret.StartOffset = new Vector2d(
                    trackobj.startPosition.x, trackobj.startPosition.y);
            }
            if (!string.IsNullOrEmpty(trackobj.linesArrayCompressed))
            {
                var json2 = LZString.decompressBase64(trackobj.linesArrayCompressed);
                trackobj.linesArray           = JsonSerializer.Deserialize <object[][]>(json2);
                trackobj.linesArrayCompressed = null;
            }
            if (trackobj.linesArray?.Length > 0)
            {
                ReadLinesArray(ret, trackobj.linesArray);
            }
            else if (trackobj.lines != null)
            {
                foreach (var line in trackobj.lines)
                {
                    AddLine(ret, line);
                }
            }
            if (trackobj.triggers != null)
            {
                foreach (var trigger in trackobj.triggers)
                {
                    if (ret.LineLookup.TryGetValue(trigger.ID, out GameLine line))
                    {
                        if (line is StandardLine stl)
                        {
                            if (trigger.zoom)
                            {
                                stl.Trigger = new LineTrigger()
                                {
                                    ZoomTrigger = trigger.zoom,
                                    ZoomTarget  = (float)MathHelper.Clamp(
                                        trigger.target,
                                        Utils.Constants.MinimumZoom,
                                        Utils.Constants.MaxZoom),
                                    ZoomFrames = trigger.frames
                                };
                            }
                        }
                    }
                }
            }
            return(ret);
        }
Exemple #2
0
        public static Track LoadTrack(string trackfile)
        {
            Track ret = new Track();

            ret.Filename = trackfile;
            track_json trackobj;

            using (var file = File.OpenRead(trackfile))
            {
                try
                {
                    trackobj = JsonSerializer.Deserialize <track_json>(file);
                }
                catch (Exception ex)
                {
                    throw new TrackIO.TrackLoadException(
                              "Json parsing error.\n" + ex.Message);
                }
            }
            switch (trackobj.version)
            {
            case "6.1":
                ret.SetVersion(61);
                break;

            case "6.2":
                ret.SetVersion(62);
                break;

            default:
                throw new TrackIO.TrackLoadException(
                          "Unsupported physics.");
            }
            ret.Name = trackobj.label;
            if (trackobj.startZoom != 0)
            {
                ret.StartZoom = (float)MathHelper.Clamp(
                    trackobj.startZoom,
                    Utils.Constants.MinimumZoom,
                    Utils.Constants.MaxZoom);
            }

            ret.YGravity        = trackobj.yGravity;
            ret.XGravity        = trackobj.xGravity;
            ret.GravityWellSize = trackobj.gravityWellSize;
            ret.BGColorR        = trackobj.bgR;
            ret.BGColorG        = trackobj.bgG;
            ret.BGColorB        = trackobj.bgB;

            ret.LineColorR = trackobj.lineR;
            ret.LineColorG = trackobj.lineG;
            ret.LineColorB = trackobj.lineB;

            ret.ZeroStart = trackobj.zeroStart;
            if (trackobj.startPosition != null)
            {
                ret.StartOffset = new Vector2d(
                    trackobj.startPosition.x, trackobj.startPosition.y);
            }
            if (!string.IsNullOrEmpty(trackobj.linesArrayCompressed))
            {
                var json2 = LZString.decompressBase64(trackobj.linesArrayCompressed);
                trackobj.linesArray           = JsonSerializer.Deserialize <object[][]>(json2);
                trackobj.linesArrayCompressed = null;
            }
            if (trackobj.linesArray?.Length > 0)
            {
                ReadLinesArray(ret, trackobj.linesArray);
            }
            else if (trackobj.lines != null)
            {
                foreach (var line in trackobj.lines)
                {
                    AddLine(ret, line);
                }
            }
            if (trackobj.triggers != null)
            {
                List <LineTrigger> linetriggers = new List <LineTrigger>();
                foreach (var trigger in trackobj.triggers)
                {
                    if (ret.LineLookup.TryGetValue(trigger.ID, out GameLine line))
                    {
                        if (line is StandardLine stl)
                        {
                            if (trigger.zoom)
                            {
                                linetriggers.Add(new LineTrigger()
                                {
                                    ZoomTrigger = trigger.zoom,
                                    ZoomTarget  = (float)MathHelper.Clamp(
                                        trigger.target,
                                        Utils.Constants.MinimumZoom,
                                        Utils.Constants.MaxZoom),
                                    ZoomFrames = trigger.frames,
                                    LineID     = trigger.ID
                                });
                            }
                        }
                    }
                }
                ret.Triggers = TriggerConverter.ConvertTriggers(linetriggers, ret);
            }
            if (trackobj.gameTriggers != null)
            {
                foreach (var t in trackobj.gameTriggers)
                {
                    if (t.start < 1 || t.end < 1 || t.end < t.start)
                    {
                        throw new TrackIO.TrackLoadException(
                                  "Trigger timing was outside of range");
                    }
                    TriggerType ttype;
                    try
                    {
                        ttype = (TriggerType)t.triggerType;
                    }
                    catch
                    {
                        throw new TrackIO.TrackLoadException(
                                  "Unsupported trigger type " + t.triggerType);
                    }
                    switch (ttype)
                    {
                    case TriggerType.Zoom:
                        ret.Triggers.Add(new GameTrigger()
                        {
                            Start       = t.start,
                            End         = t.end,
                            TriggerType = ttype,
                            ZoomTarget  = t.zoomTarget,
                        });
                        break;

                    case TriggerType.BGChange:
                        ret.Triggers.Add(new GameTrigger()
                        {
                            Start           = t.start,
                            End             = t.end,
                            TriggerType     = ttype,
                            backgroundRed   = t.backgroundred,
                            backgroundGreen = t.backgroundgreen,
                            backgroundBlue  = t.backgroundblue,
                        });
                        break;

                    case TriggerType.LineColor:
                        ret.Triggers.Add(new GameTrigger()
                        {
                            Start       = t.start,
                            End         = t.end,
                            TriggerType = ttype,
                            lineRed     = t.lineRed,
                            lineGreen   = t.lineGreen,
                            lineBlue    = t.lineBlue,
                        });
                        break;
                    }
                }
            }
            return(ret);
        }