Ejemplo n.º 1
0
        private bool PreProcessDownload(string sTargetFolder)
        {
            switch (UpdateMode)
            {
            case UpdateOtherPluginMode.PlgxDirect:
            case UpdateOtherPluginMode.DllDirect:
                PluginDebug.AddInfo("Other plugin update", 0, "Nothing to do");
                return(true);

            case UpdateOtherPluginMode.ZipExtractPlgx:
            case UpdateOtherPluginMode.ZipExtractDll:
                string sSourceFile = m_lDownloaded[0];
                byte[] pb          = File.ReadAllBytes(sSourceFile);
                File.Delete(sSourceFile);
                string sPattern = UpdateMode == UpdateOtherPluginMode.ZipExtractDll ? "*.dll" : "*.plgx";
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(pb, 0, pb.Length);
                    ms.Position = 0;
                    pb          = null;
                    using (Ionic.Zip.ZipFile z = Ionic.Zip.ZipFile.Read(ms))
                    {
                        List <Ionic.Zip.ZipEntry> f = z.SelectEntries(sPattern) as List <Ionic.Zip.ZipEntry>;
                        using (MemoryStream msTarget = new MemoryStream())
                        {
                            f[0].Extract(msTarget);
                            pb = msTarget.ToArray();
                            string sTargetFile = UrlUtil.GetFileDirectory(m_lDownloaded[0], true, true) + f[0].FileName;
                            File.WriteAllBytes(sTargetFile, pb);
                            m_lDownloaded[0] = sTargetFile;
                            PluginDebug.AddInfo("Other plugin update", 0, "Extracted file: " + f[0].FileName);
                        }
                    }
                    return(true);
                }

            default: return(false);
            }
        }
Ejemplo n.º 2
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);
        }