Exemple #1
0
        public static ZipStateLoader LoadAndDetect(string filename, bool isMovieLoad = false)
        {
            var ret = new ZipStateLoader();

            using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                byte[] data = new byte[4];
                fs.Read(data, 0, 4);
                if (!data.SequenceEqual(Zipheader))
                {
                    return(null);
                }
            }

            try
            {
                ret._zip = new ZipFile(filename);
                ret.PopulateEntries();
                if (!isMovieLoad && !ret.GetLump(BinaryStateLump.Versiontag, false, ret.ReadVersion))
                {
                    ret._zip.Close();
                    return(null);
                }

                return(ret);
            }
            catch (ZipException)
            {
                return(null);
            }
        }
Exemple #2
0
 protected override void LoadFields(ZipStateLoader bl, bool preload)
 {
     LoadBk2Fields(bl, preload);
     if (!preload)
     {
         LoadTasprojExtras(bl);
     }
 }
        protected override void LoadFields(ZipStateLoader bl, bool preload)
        {
            LoadBk2Fields(bl, preload);

            if (!preload)
            {
                if (MovieService.IsCurrentTasVersion(Header[HeaderKeys.MovieVersion]))
                {
                    LoadTasprojExtras(bl);
                }
                else
                {
                    Session.PopupMessage("The current .tasproj is not compatible with this version of BizHawk! .tasproj features failed to load.");
                }
            }
        }
Exemple #4
0
        public virtual bool Load(bool preload)
        {
            var file = new FileInfo(Filename);

            if (!file.Exists)
            {
                return(false);
            }

            using var bl = ZipStateLoader.LoadAndDetect(Filename, true);
            if (bl == null)
            {
                return(false);
            }

            ClearBeforeLoad();
            LoadFields(bl, preload);

            Changes = false;
            return(true);
        }
Exemple #5
0
        protected void LoadBk2Fields(ZipStateLoader bl)
        {
            bl.GetLump(BinaryStateLump.Movieheader, true, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        var pair = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);

                        if (pair.Length > 1)
                        {
                            if (!Header.ContainsKey(pair[0]))
                            {
                                Header.Add(pair[0], pair[1]);
                            }
                        }
                    }
                }
            });

            bl.GetLump(BinaryStateLump.Comments, false, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        Comments.Add(line);
                    }
                }
            });

            bl.GetLump(BinaryStateLump.Subtitles, false, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        Subtitles.AddFromString(line);
                    }
                }

                Subtitles.Sort();
            });

            bl.GetLump(BinaryStateLump.SyncSettings, false, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        _syncSettingsJson = line;
                    }
                }
            });

            bl.GetLump(BinaryStateLump.Input, true, delegate(TextReader tr)
            {
                IsCountingRerecords = false;
                ExtractInputLog(tr, out _);
                IsCountingRerecords = true;
            });

            if (StartsFromSavestate)
            {
                bl.GetCoreState(
                    delegate(BinaryReader br, long length)
                {
                    BinarySavestate = br.ReadBytes((int)length);
                },
                    delegate(TextReader tr)
                {
                    TextSavestate = tr.ReadToEnd();
                });
                bl.GetLump(BinaryStateLump.Framebuffer, false,
                           delegate(BinaryReader br, long length)
                {
                    SavestateFramebuffer = new int[length / sizeof(int)];
                    for (int i = 0; i < SavestateFramebuffer.Length; i++)
                    {
                        SavestateFramebuffer[i] = br.ReadInt32();
                    }
                });
            }
            else if (StartsFromSaveRam)
            {
                bl.GetLump(BinaryStateLump.MovieSaveRam, false,
                           delegate(BinaryReader br, long length)
                {
                    SaveRam = br.ReadBytes((int)length);
                });
            }
        }
Exemple #6
0
 protected virtual void LoadFields(ZipStateLoader bl, bool preload)
 {
     LoadBk2Fields(bl);
 }
Exemple #7
0
        private void LoadTasprojExtras(ZipStateLoader bl, bool preload)
        {
            bl.GetLump(BinaryStateLump.LagLog, false, delegate(TextReader tr)
            {
                LagLog.Load(tr);
            });

            bl.GetLump(BinaryStateLump.StateHistorySettings, false, delegate(TextReader tr)
            {
                var json = tr.ReadToEnd();
                try
                {
                    TasStateManager.Settings = JsonConvert.DeserializeObject <TasStateManagerSettings>(json);
                }
                catch
                {
                    // Do nothing, and use default settings instead
                }
            });

            bl.GetLump(BinaryStateLump.Markers, false, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        Markers.Add(new TasMovieMarker(line));
                    }
                }
            });

            if (GetClientSettingsOnLoad != null)
            {
                string clientSettings = "";
                bl.GetLump(BinaryStateLump.ClientSettings, false, delegate(TextReader tr)
                {
                    string line;
                    while ((line = tr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            clientSettings = line;
                        }
                    }
                });

                if (!string.IsNullOrWhiteSpace(clientSettings))
                {
                    GetClientSettingsOnLoad(clientSettings);
                }
            }

            bl.GetLump(BinaryStateLump.VerificationLog, false, delegate(TextReader tr)
            {
                VerificationLog.Clear();
                while (true)
                {
                    var line = tr.ReadLine();
                    if (string.IsNullOrEmpty(line))
                    {
                        break;
                    }

                    if (line.StartsWith("|"))
                    {
                        VerificationLog.Add(line);
                    }
                }
            });

            Branches.Load(bl, this);

            bl.GetLump(BinaryStateLump.Session, false, delegate(TextReader tr)
            {
                var json = tr.ReadToEnd();
                try
                {
                    TasSession = JsonConvert.DeserializeObject <TasSession>(json);
                }
                catch
                {
                    // Do nothing, and use default settings instead
                }
            });

            if (!preload)
            {
                if (TasStateManager.Settings.SaveStateHistory)
                {
                    bl.GetLump(BinaryStateLump.StateHistory, false, delegate(BinaryReader br, long length)
                    {
                        TasStateManager.Load(br);
                    });
                }
            }
        }
Exemple #8
0
        public bool Load(string path)
        {
            // try to detect binary first
            var bl = ZipStateLoader.LoadAndDetect(path);

            if (bl != null)
            {
                try
                {
                    var succeed = false;

                    // Movie timeline check must happen before the core state is loaded
                    if (_movieSession.Movie.IsActive())
                    {
                        bl.GetLump(BinaryStateLump.Input, true, tr => succeed = _movieSession.CheckSavestateTimeline(tr));
                        if (!succeed)
                        {
                            return(false);
                        }
                    }

                    using (new SimpleTime("Load Core"))
                    {
                        bl.GetCoreState(br => _statable.LoadStateBinary(br), tr => _statable.LoadStateText(tr));
                    }

                    // We must handle movie input AFTER the core is loaded to properly handle mode changes, and input latching
                    if (_movieSession.Movie.IsActive())
                    {
                        bl.GetLump(BinaryStateLump.Input, true, tr => succeed = _movieSession.HandleLoadState(tr));
                        if (!succeed)
                        {
                            return(false);
                        }
                    }

                    if (_videoProvider != null)
                    {
                        bl.GetLump(BinaryStateLump.Framebuffer, false, br => PopulateFramebuffer(br, _videoProvider, _quickBmpFile));
                    }

                    string userData = "";
                    bl.GetLump(BinaryStateLump.UserData, false, delegate(TextReader tr)
                    {
                        string line;
                        while ((line = tr.ReadLine()) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(line))
                            {
                                userData = line;
                            }
                        }
                    });

                    if (!string.IsNullOrWhiteSpace(userData))
                    {
                        var bag = (Dictionary <string, object>)ConfigService.LoadWithType(userData);
                        _userBag.Clear();
                        foreach (var(k, v) in bag)
                        {
                            _userBag.Add(k, v);
                        }
                    }

                    if (_movieSession.Movie.IsActive() && _movieSession.Movie is ITasMovie)
                    {
                        bl.GetLump(BinaryStateLump.LagLog, false, delegate(TextReader tr)
                        {
                            ((ITasMovie)_movieSession.Movie).LagLog.Load(tr);
                        });
                    }
                }
                finally
                {
                    bl.Dispose();
                }

                return(true);
            }

            return(false);
        }
Exemple #9
0
        public virtual bool Load(bool preload)
        {
            var file = new FileInfo(Filename);

            if (!file.Exists)
            {
                return(false);
            }

            using var bl = ZipStateLoader.LoadAndDetect(Filename, true);
            if (bl == null)
            {
                return(false);
            }

            ClearBeforeLoad();

            bl.GetLump(BinaryStateLump.Movieheader, true, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        var pair = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);

                        if (pair.Length > 1)
                        {
                            if (!Header.ContainsKey(pair[0]))
                            {
                                Header.Add(pair[0], pair[1]);
                            }
                        }
                    }
                }
            });

            bl.GetLump(BinaryStateLump.Comments, false, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        Comments.Add(line);
                    }
                }
            });

            bl.GetLump(BinaryStateLump.Subtitles, false, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        Subtitles.AddFromString(line);
                    }
                }

                Subtitles.Sort();
            });

            bl.GetLump(BinaryStateLump.SyncSettings, false, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        _syncSettingsJson = line;
                    }
                }
            });

            bl.GetLump(BinaryStateLump.Input, true, delegate(TextReader tr)
            {
                IsCountingRerecords = false;
                ExtractInputLog(tr, out _);
                IsCountingRerecords = true;
            });

            if (StartsFromSavestate)
            {
                bl.GetCoreState(
                    delegate(BinaryReader br, long length)
                {
                    BinarySavestate = br.ReadBytes((int)length);
                },
                    delegate(TextReader tr)
                {
                    TextSavestate = tr.ReadToEnd();
                });
                bl.GetLump(BinaryStateLump.Framebuffer, false,
                           delegate(BinaryReader br, long length)
                {
                    SavestateFramebuffer = new int[length / sizeof(int)];
                    for (int i = 0; i < SavestateFramebuffer.Length; i++)
                    {
                        SavestateFramebuffer[i] = br.ReadInt32();
                    }
                });
            }
            else if (StartsFromSaveRam)
            {
                bl.GetLump(BinaryStateLump.MovieSaveRam, false,
                           delegate(BinaryReader br, long length)
                {
                    SaveRam = br.ReadBytes((int)length);
                });
            }

            Changes = false;
            return(true);
        }
Exemple #10
0
        public override bool Load(bool preload)
        {
            var file = new FileInfo(Filename);

            if (!file.Exists)
            {
                return(false);
            }

            using (var bl = ZipStateLoader.LoadAndDetect(Filename, true))
            {
                if (bl == null)
                {
                    return(false);
                }

                ClearBeforeLoad();
                ClearTasprojExtras();

                bl.GetLump(BinaryStateLump.Movieheader, true, delegate(TextReader tr)
                {
                    string line;
                    while ((line = tr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            var pair = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);

                            if (pair.Length > 1)
                            {
                                Header.Add(pair[0], pair[1]);
                            }
                        }
                    }
                });

                bl.GetLump(BinaryStateLump.Comments, true, delegate(TextReader tr)
                {
                    string line;
                    while ((line = tr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            Comments.Add(line);
                        }
                    }
                });

                bl.GetLump(BinaryStateLump.Subtitles, true, delegate(TextReader tr)
                {
                    string line;
                    while ((line = tr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            Subtitles.AddFromString(line);
                        }
                    }
                });

                bl.GetLump(BinaryStateLump.SyncSettings, true, delegate(TextReader tr)
                {
                    string line;
                    while ((line = tr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            SyncSettingsJson = line;
                        }
                    }
                });

                bl.GetLump(BinaryStateLump.Input, true, delegate(TextReader tr)                 // Note: ExtractInputLog will clear Lag and State data potentially, this must come before loading those
                {
                    IsCountingRerecords = false;
                    ExtractInputLog(tr, out _);
                    IsCountingRerecords = true;
                });

                if (StartsFromSavestate)
                {
                    bl.GetCoreState(
                        delegate(BinaryReader br, long length)
                    {
                        BinarySavestate = br.ReadBytes((int)length);
                    },
                        delegate(TextReader tr)
                    {
                        TextSavestate = tr.ReadToEnd();
                    });
                }
                else if (StartsFromSaveRam)
                {
                    bl.GetLump(BinaryStateLump.MovieSaveRam, false,
                               delegate(BinaryReader br, long length)
                    {
                        SaveRam = br.ReadBytes((int)length);
                    });
                }

                // TasMovie enhanced information
                bl.GetLump(BinaryStateLump.LagLog, false, delegate(TextReader tr)
                {
                    LagLog.Load(tr);
                });

                bl.GetLump(BinaryStateLump.StateHistorySettings, false, delegate(TextReader tr)
                {
                    var json = tr.ReadToEnd();
                    try
                    {
                        TasStateManager.Settings = JsonConvert.DeserializeObject <TasStateManagerSettings>(json);
                    }
                    catch
                    {
                        // Do nothing, and use default settings instead
                    }
                });

                bl.GetLump(BinaryStateLump.Markers, false, delegate(TextReader tr)
                {
                    string line;
                    while ((line = tr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            Markers.Add(new TasMovieMarker(line));
                        }
                    }
                });

                if (GetClientSettingsOnLoad != null)
                {
                    string clientSettings = "";
                    bl.GetLump(BinaryStateLump.ClientSettings, false, delegate(TextReader tr)
                    {
                        string line;
                        while ((line = tr.ReadLine()) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(line))
                            {
                                clientSettings = line;
                            }
                        }
                    });

                    if (!string.IsNullOrWhiteSpace(clientSettings))
                    {
                        GetClientSettingsOnLoad(clientSettings);
                    }
                }

                bl.GetLump(BinaryStateLump.VerificationLog, false, delegate(TextReader tr)
                {
                    VerificationLog.Clear();
                    while (true)
                    {
                        var line = tr.ReadLine();
                        if (string.IsNullOrEmpty(line))
                        {
                            break;
                        }

                        if (line.StartsWith("|"))
                        {
                            VerificationLog.Add(line);
                        }
                    }
                });

                Branches.Load(bl, this);

                bl.GetLump(BinaryStateLump.Session, false, delegate(TextReader tr)
                {
                    var json = tr.ReadToEnd();
                    try
                    {
                        TasSession = JsonConvert.DeserializeObject <TasSession>(json);
                    }
                    catch
                    {
                        // Do nothing, and use default settings instead
                    }
                });

                if (!preload)
                {
                    if (TasStateManager.Settings.SaveStateHistory)
                    {
                        bl.GetLump(BinaryStateLump.StateHistory, false, delegate(BinaryReader br, long length)
                        {
                            TasStateManager.Load(br);
                        });
                    }
                }
            }

            Changes = false;
            return(true);
        }
Exemple #11
0
        public bool Load(string path, IDialogParent dialogParent)
        {
            // try to detect binary first
            using var bl = ZipStateLoader.LoadAndDetect(path);
            if (bl is null)
            {
                return(false);
            }
            var succeed = false;

            if (!VersionInfo.DeveloperBuild)
            {
                bl.GetLump(BinaryStateLump.BizVersion, true, tr => succeed = tr.ReadLine() == VersionInfo.GetEmuVersion());
                if (!succeed)
                {
                    var result = dialogParent.ModalMessageBox2(
                        "This savestate was made with a different version, so it's unlikely to work.\nChoose OK to try loading it anyway.",
                        "Savestate version mismatch",
                        EMsgBoxIcon.Question,
                        useOKCancel: true);
                    if (!result)
                    {
                        return(false);
                    }
                }
            }

            // Movie timeline check must happen before the core state is loaded
            if (_movieSession.Movie.IsActive())
            {
                bl.GetLump(BinaryStateLump.Input, true, tr => succeed = _movieSession.CheckSavestateTimeline(tr));
                if (!succeed)
                {
                    return(false);
                }
            }

            using (new SimpleTime("Load Core"))
            {
                bl.GetCoreState(br => _statable.LoadStateBinary(br), tr => _statable.LoadStateText(tr));
            }

            // We must handle movie input AFTER the core is loaded to properly handle mode changes, and input latching
            if (_movieSession.Movie.IsActive())
            {
                bl.GetLump(BinaryStateLump.Input, true, tr => succeed = _movieSession.HandleLoadState(tr));
                if (!succeed)
                {
                    return(false);
                }
            }

            if (_videoProvider != null)
            {
                bl.GetLump(BinaryStateLump.Framebuffer, false, br => PopulateFramebuffer(br, _videoProvider, _quickBmpFile));
            }

            string userData = "";

            bl.GetLump(BinaryStateLump.UserData, false, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        userData = line;
                    }
                }
            });

            if (!string.IsNullOrWhiteSpace(userData))
            {
                var bag = (Dictionary <string, object>)ConfigService.LoadWithType(userData);
                _userBag.Clear();
                foreach (var(k, v) in bag)
                {
                    _userBag.Add(k, v);
                }
            }

            if (_movieSession.Movie.IsActive() && _movieSession.Movie is ITasMovie)
            {
                bl.GetLump(BinaryStateLump.LagLog, false, delegate(TextReader tr)
                {
                    ((ITasMovie)_movieSession.Movie).LagLog.Load(tr);
                });
            }

            return(true);
        }
Exemple #12
0
        private void LoadTasprojExtras(ZipStateLoader bl)
        {
            bl.GetLump(BinaryStateLump.LagLog, false, delegate(TextReader tr)
            {
                LagLog.Load(tr);
            });

            bl.GetLump(BinaryStateLump.Markers, false, delegate(TextReader tr)
            {
                string line;
                while ((line = tr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        Markers.Add(new TasMovieMarker(line));
                    }
                }
            });

            if (GetClientSettingsOnLoad != null)
            {
                string clientSettings = "";
                bl.GetLump(BinaryStateLump.ClientSettings, false, delegate(TextReader tr)
                {
                    string line;
                    while ((line = tr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            clientSettings = line;
                        }
                    }
                });

                if (!string.IsNullOrWhiteSpace(clientSettings))
                {
                    GetClientSettingsOnLoad(clientSettings);
                }
            }

            bl.GetLump(BinaryStateLump.VerificationLog, false, delegate(TextReader tr)
            {
                VerificationLog.Clear();
                while (true)
                {
                    var line = tr.ReadLine();
                    if (string.IsNullOrEmpty(line))
                    {
                        break;
                    }

                    if (line.StartsWith("|"))
                    {
                        VerificationLog.Add(line);
                    }
                }
            });

            Branches.Load(bl, this);

            bl.GetLump(BinaryStateLump.Session, false, delegate(TextReader tr)
            {
                var json = tr.ReadToEnd();
                try
                {
                    TasSession = JsonConvert.DeserializeObject <TasSession>(json);
                }
                catch
                {
                    // Do nothing, and use default settings instead
                }
            });

            ZwinderStateManagerSettings settings = new ZwinderStateManagerSettings();

            bl.GetLump(BinaryStateLump.StateHistorySettings, false, delegate(TextReader tr)
            {
                var json = tr.ReadToEnd();
                try
                {
                    settings = JsonConvert.DeserializeObject <ZwinderStateManagerSettings>(json);
                }
                catch
                {
                    // Do nothing, and use default settings instead
                }
            });

            bl.GetLump(BinaryStateLump.StateHistory, false, delegate(BinaryReader br, long length)
            {
                try
                {
                    TasStateManager?.Dispose();
                    TasStateManager = ZwinderStateManager.Create(br, settings, IsReserved);
                }
                catch
                {
                    // Continue with a fresh manager. If state history got corrupted, the file is still very much useable
                    // and we would want the user to be able to load, and regenerate their state history
                    // however, we still have an issue of how state history got corrupted
                    TasStateManager = new ZwinderStateManager(
                        Session.Settings.DefaultTasStateManagerSettings,
                        IsReserved);
                    Session.PopupMessage("State history was corrupted, clearing and working with a fresh history.");
                }
            });
        }