Esempio n. 1
0
        /// <summary>
        /// 存档为压缩包
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static SaveData ParseZip(string path)
        {
            SaveData data = null;

            using (var zip = new ZipFile(path))
            {
                if (zip.ContainsEntry("date.json"))
                {
                    using (var stream = new MemoryStream())
                    {
                        zip.SelectEntries("date.json").First().Extract(stream);
                        stream.Seek(0, SeekOrigin.Begin);
                        using (var reader = new StreamReader(stream))
                        {
                            var serializer = JsonSerializer.Create();
                            data = serializer.Deserialize(reader,
                                                          typeof(SaveData)) as SaveData;
                        }
                    }
                }
                else if (!zip.ContainsEntry("TW_Save_Date_0.twV0") &&
                         !zip.ContainsEntry("TW_Save_Date_0.tw"))
                {
                    throw new Exception(path); // 错误存档
                }
                else // 不含加速文件
                {
                    var tmp = Path.Combine(
                        Environment.GetEnvironmentVariable("TEMP"),
                        Guid.NewGuid().ToString() + ".tw");

                    bool rijndeal = true;
                    using (var stream = File.OpenWrite(tmp))
                    {
                        if (zip.ContainsEntry("TW_Save_Date_0.twV0"))
                        {
                            zip.SelectEntries("TW_Save_Date_0.twV0").First().Extract(stream);
                            rijndeal = false;
                        }
                        else if (zip.ContainsEntry("TW_Save_Date_0.tw"))
                        {
                            zip.SelectEntries("TW_Save_Date_0.tw").First().Extract(stream);
                            rijndeal = true;
                        }
                    }
                    DateFile.SaveDate date = ReadSaveDate(tmp, rijndeal);

                    File.Delete(tmp);

                    data = new SaveData(date._mainActorName, date._year, date._samsara,
                                        date._dayTrun, date._playerSeatName, date._playTime);
                    //  添加加速文件
                    zip.AddEntry("date.json", JsonConvert.SerializeObject(data));
                    zip.Save();
                }
            }
            return(data);
        }
Esempio n. 2
0
 private static void Postfix(DateFile.SaveDate __instance)
 {
     _saveCache.ExpireCache();
     if (!Main.Enabled)
     {
         return;
     }
     if (Main.settings.enableTurboQuickLoadAfterSave)
     {
         _saveCache.StartSetCloneCache(__instance);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// 存档为压缩包
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static SaveData ParseZip(string path)
        {
            SaveData data  = null;
            var      items = SevenZipHelper.List(path).ToList();

            if (items.Any(t => t.path == "date.json"))
            {
                using (var tmp = new TempDir())
                {
                    SevenZipHelper.Extract(path, tmp.Dir, include: "date.json");
                    var json = File.ReadAllText(Path.Combine(tmp.Dir, "date.json"), Encoding.UTF8);
                    data = JsonConvert.DeserializeObject(json, typeof(SaveData)) as SaveData;
                }
            }
            else if (!items.Any(t => t.path == "TW_Save_Date_0.twV0") &&
                     !items.Any(t => t.path == "TW_Save_Date_0.tw"))
            {
                throw new InvalidDataException(path); // 错误存档
            }
            else // 不含加速文件
            {
                bool   rijndeal = true;
                string files    = null;
                if (items.Any(t => t.path == "TW_Save_Date_0.twV0"))
                {
                    rijndeal = false;
                    files    = "TW_Save_Date_0.twV0";
                }
                else if (items.Any(t => t.path == "TW_Save_Date_0.tw"))
                {
                    rijndeal = true;
                    files    = "TW_Save_Date_0.tw";
                }
                if (files != null)
                {
                    using (var tmp = new TempDir())
                    {
                        SevenZipHelper.Extract(path, tmp.Dir, include: files);
                        DateFile.SaveDate date = ReadSaveDate(Path.Combine(tmp.Dir, files), rijndeal);
                        data = new SaveData(date._mainActorName, date._year, date._samsara,
                                            date._dayTrun, date._playerSeatName, date._playTime);
                        var dataPath = Path.Combine(tmp.Dir, "date.json");
                        File.WriteAllText(dataPath, JsonConvert.SerializeObject(data));
                        SevenZipHelper.Update(path, dataPath);
                    }
                }
            }
            return(data);
        }
Esempio n. 4
0
        /// <summary>
        /// 存档为游戏存档
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static SaveData ParseDirectory(string path)
        {
            SaveData data = null;

            if (File.Exists(Path.Combine(path, "date.json")))
            {
                var content = File.ReadAllText(Path.Combine(path, "date.json"));
                data = JsonConvert.DeserializeObject(content, typeof(SaveData)) as SaveData;
            }
            else if (!File.Exists(Path.Combine(path, "TW_Save_Date_0.twV0")) &&
                     !File.Exists(Path.Combine(path, "TW_Save_Date_0.tw")))
            {
                throw new Exception(path);
            }
            else
            {
                string file     = null;
                bool   rijndeal = true;
                if (File.Exists(Path.Combine(path, "TW_Save_Date_0.twV0")))
                {
                    file     = Path.Combine(path, "TW_Save_Date_0.twV0");
                    rijndeal = false;
                }
                else
                {
                    file     = Path.Combine(path, "TW_Save_Date_0.tw");
                    rijndeal = true;
                }
                DateFile.SaveDate date = ReadSaveDate(file, rijndeal);

                data = new SaveData(date._mainActorName, date._year, date._samsara,
                                    date._dayTrun, date._playerSeatName, date._playTime);
                File.WriteAllText(Path.Combine(path, "date.json"),
                                  JsonConvert.SerializeObject(data));
            }
            return(data);
        }
Esempio n. 5
0
        /// <summary>
        /// 解析指定压缩存档
        /// </summary>
        /// <param name="path">压缩存档路径</param>
        /// <returns></returns>
        public static SaveData Parse(string path)
        {
            SaveData ans = null;

            if (!path.EndsWith(".zip"))
            {
                if (File.Exists(Path.Combine(path, "date.json")))
                {
                    var content = File.ReadAllText(Path.Combine(path, "date.json"));
                    ans = JsonConvert.DeserializeObject(content, typeof(SaveData)) as SaveData;
                }
                else if (!File.Exists(Path.Combine(path, "TW_Save_Date_0.twV0")) &&
                         !File.Exists(Path.Combine(path, "TW_Save_Date_0.tw")))
                {
                    throw new System.Exception(path);
                }
                else
                {
                    string file     = null;
                    bool   rijndeal = true;
                    if (File.Exists(Path.Combine(path, "TW_Save_Date_0.twV0")))
                    {
                        file     = Path.Combine(path, "TW_Save_Date_0.twV0");
                        rijndeal = false;
                    }
                    else
                    {
                        file     = Path.Combine(path, "TW_Save_Date_0.tw");
                        rijndeal = true;
                    }
                    DateFile.SaveDate date = null;
                    try
                    {
                        date = typeof(SaveDateFile)
                               .GetMethod("GetData", BindingFlags.Public | BindingFlags.Instance)
                               .Invoke(SaveDateFile.instance,
                                       new object[] { file, typeof(DateFile.SaveDate), rijndeal })
                               as DateFile.SaveDate;
                    }
                    catch (AmbiguousMatchException)
                    {
                        date = typeof(SaveDateFile)
                               .GetMethod("GetData", BindingFlags.Public | BindingFlags.Instance)
                               .Invoke(SaveDateFile.instance,
                                       new object[] { file, typeof(DateFile.SaveDate) })
                               as DateFile.SaveDate;
                    }
                    ans = new SaveData(date._mainActorName, date._year, date._samsara,
                                       date._dayTrun, date._playerSeatName, date._playTime);
                    File.WriteAllText(Path.Combine(path, "date.json"),
                                      JsonConvert.SerializeObject(ans));
                }
            }
            else
            {
                using (var zip = new Ionic.Zip.ZipFile(path))
                {
                    if (zip.ContainsEntry("date.json"))
                    {
                        using (var stream = new MemoryStream())
                        {
                            zip.SelectEntries("date.json").First().Extract(stream);
                            stream.Seek(0, SeekOrigin.Begin);
                            using (var reader = new StreamReader(stream))
                            {
                                var serializer = JsonSerializer.Create();
                                ans = serializer.Deserialize(reader,
                                                             typeof(SaveData)) as SaveData;
                            }
                        }
                    }
                    else if (!zip.ContainsEntry("TW_Save_Date_0.twV0") &&
                             !zip.ContainsEntry("TW_Save_Date_0.tw"))
                    {
                        throw new System.Exception(path); // 错误存档
                    }
                    else // 不含加速文件
                    {
                        var tmp = Path.Combine(
                            System.Environment.GetEnvironmentVariable("TEMP"),
                            "SaveDate.tw");

                        if (File.Exists(tmp))
                        {
                            File.Delete(tmp);
                        }

                        bool rijndeal = true;
                        using (var stream = File.OpenWrite(tmp))
                        {
                            if (zip.ContainsEntry("TW_Save_Date_0.twV0"))
                            {
                                zip.SelectEntries("TW_Save_Date_0.twV0").First().Extract(stream);
                                rijndeal = false;
                            }
                            else if (zip.ContainsEntry("TW_Save_Date_0.tw"))
                            {
                                zip.SelectEntries("TW_Save_Date_0.tw").First().Extract(stream);
                                rijndeal = true;
                            }
                        }
                        DateFile.SaveDate date = null;
                        try
                        {
                            date = typeof(SaveDateFile)
                                   .GetMethod("GetData", BindingFlags.Public | BindingFlags.Instance)
                                   .Invoke(SaveDateFile.instance,
                                           new object[] { tmp, typeof(DateFile.SaveDate), rijndeal })
                                   as DateFile.SaveDate;
                        }
                        catch (AmbiguousMatchException)
                        {
                            date = typeof(SaveDateFile)
                                   .GetMethod("GetData", BindingFlags.Public | BindingFlags.Instance)
                                   .Invoke(SaveDateFile.instance,
                                           new object[] { tmp, typeof(DateFile.SaveDate) })
                                   as DateFile.SaveDate;
                        }
                        ans = new SaveData(date._mainActorName, date._year, date._samsara,
                                           date._dayTrun, date._playerSeatName, date._playTime);
                        //  添加加速文件
                        zip.AddEntry("date.json", JsonConvert.SerializeObject(ans));
                        zip.Save();
                    }
                }
            }
            return(ans);
        }