/// <summary> /// Возвращает информацию о сессии /// </summary> /// <param name="fileName">Имя файла сессии</param> /// <returns>Информация о сессии</returns> public BalanceSessionInfo LoadSessionInfo(string fileName, int versionNumber = 1) { try { if (versionNumber == 1) { using (System.IO.Packaging.Package package = System.IO.Packaging.Package.Open(fileName, FileMode.Open)) { return(LoadSessionInfoFromPackage(package.GetPart(System.IO.Packaging.PackUriHelper.CreatePartUri(new Uri(PART_Info, UriKind.Relative))))); } } else if (versionNumber == 0) { return(LoadSessionInfoFromOldFileFormat(fileName)); } else { throw new ArgumentOutOfRangeException("Session file version number"); } } catch (FileFormatException) { return(LoadSessionInfoFromOldFileFormat(fileName)); } catch (IOException ioe) { EmcosSiteWrapperApp.LogException(ioe); return(null); } catch { return(null); } }
/// <summary> /// Возвращает версию файла сессии /// </summary> /// <param name="fileName">Имя файла сессии</param> /// <returns>-1 - файл не поддреживается, 0 - файл старого формата, 1 - нового</returns> public int GetSessionVersion(string fileName) { byte[] zipHeader = { 0x1F, 0x8B, 0x08 }; byte[] packageHeader = { 0x50, 0x4B, 0x03, 0x04, 0x14 }; if (Path.GetExtension(fileName) == SESSION_FILE_EXTENSION) { byte[] bytes = new byte[5]; using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)) using (System.IO.MemoryStream mso = new System.IO.MemoryStream()) { fs.Read(bytes, 0, bytes.Length); mso.Write(bytes, 0, bytes.Length); } if (bytes.Take(3).SequenceEqual(zipHeader)) { return(IsSupportedSessionVersion(LoadVersionFromZip(fileName)) ? 0 : -1); } else if (bytes.Take(5).SequenceEqual(packageHeader)) { try { BalanceSessionInfo info = LoadSessionInfo(fileName); return(IsSupportedSessionVersion(info.Version) ? 1 : -1); } catch (IOException ioe) { EmcosSiteWrapperApp.LogException(ioe); return(-1); } catch (FileFormatException ffe) { EmcosSiteWrapperApp.LogException(ffe); return(-1); } catch { return(-1); } } else { return(-1); } } else { return(-1); } }
/// <summary> /// Загрузка сессии /// </summary> /// <param name="fileName">Имя файла, если не указано, то загрузка из стандартного файла <see cref="BALANCE_SESSION_FILENAME"/></param> /// <returns>True если загрузка произошла успешно</returns> private bool LoadSessionData(string fileName = null) { bool mustStoreLastSessionFileName = true; try { if (String.IsNullOrWhiteSpace(fileName)) { fileName = BALANCE_SESSION_FILENAME + SESSION_FILE_EXTENSION; mustStoreLastSessionFileName = false; } else if (Path.GetExtension(fileName).ToLowerInvariant() != SESSION_FILE_EXTENSION) { fileName = fileName + SESSION_FILE_EXTENSION; } var fi = new FileInfo(Path.Combine(SESSIONS_FOLDER, fileName)); if (fi.Exists == false) { return(false); } BalanceSession balanceSession = null; bool isOldVersionFile = false; try { if (Path.IsPathRooted(fileName) == false) { fileName = Path.Combine(SESSIONS_FOLDER, fileName); } // попытка прочитать файл как пакет using (System.IO.Packaging.Package package = System.IO.Packaging.Package.Open(fileName, FileMode.Open, FileAccess.Read)) { BalanceSessionInfo info = LoadSessionInfoFromPackage(package.GetPart(System.IO.Packaging.PackUriHelper.CreatePartUri(new Uri(PART_Info, UriKind.Relative)))); if (IsSupportedSessionVersion(info.Version)) { void unknownVersion() { string msg = String.Format("Файл '{1}'\nнеизвестной версии - {0}\nЗагрузка невозможна.\nОбновите программу или обратитесь к разработчику.", info.Version, fi.FullName); EmcosSiteWrapperApp.LogError(msg); EmcosSiteWrapperApp.ShowError(msg); } switch (info.Version.Major) { case 1: switch (info.Version.Minor) { case 0: isOldVersionFile = true; break; case 1: balanceSession = LoadDataFromFileVersion_1_1(package); break; default: unknownVersion(); return(false); } break; default: unknownVersion(); return(false); } } } } catch (IOException ioe) { EmcosSiteWrapperApp.LogException(ioe); isOldVersionFile = true; } catch (Exception e) { isOldVersionFile = true; } if (isOldVersionFile) { balanceSession = LoadDataFromFileVersion_1_0(fi.FullName); } if (mustStoreLastSessionFileName) { File.WriteAllText(Path.Combine(SESSIONS_FOLDER, "lastsession"), fileName); } ActiveSession = balanceSession; return(balanceSession != null); } catch (Exception ex) { _callBackAction(ex); ActiveSession = null; return(false); } }