/// <summary> /// Save the type file but only if has been loaded before /// </summary> private void SaveTypeFile() { if (m_TypeFile != null) { // Replace value m_TypeFile = Regex.Replace(m_TypeFile, "Language[ ]{0,1}=[ ]{0,1}[\"]{0,1}([a-zA-Z]*)[\"]{0,1}", "Language = \"" + m_Language + "\""); // Import to Pk2 if (Pk2Writer.Initialize("GFXFileManager.dll")) { if (Pk2Writer.Open(LauncherSettings.PATH_PK2_MEDIA, LauncherSettings.CLIENT_BLOWFISH_KEY)) { // Create a temporary file if (!Directory.Exists("Temp")) { Directory.CreateDirectory("Temp"); } File.WriteAllText("Temp\\Type.txt", m_TypeFile); // Edit Pk2 Pk2Writer.ImportFile("Type.txt", "Temp\\Type.txt"); // Delete temporary file File.Delete("Temp\\Type.txt"); // Close Pk2 Pk2Writer.Close(); Pk2Writer.Deinitialize(); } } } }
/// <summary> /// Updates the SV.T file with the newer version /// </summary> private static void UpdateSilkroadVersion() { // Set the new version into pk2 automagically... if (Pk2Writer.Open(LauncherSettings.PATH_PK2_MEDIA, LauncherSettings.CLIENT_BLOWFISH_KEY)) { var buffer = Encoding.ASCII.GetBytes(DownloadVersion.ToString()); // Add blowfish minimum padding Array.Resize(ref buffer, 8); // Init blowfish for encoding Blowfish bf = new Blowfish(); bf.Initialize(Encoding.ASCII.GetBytes("SILKROADVERSION"), 0, 8); // Create the SV.T file using (FileStream fs = File.Create("Temp\\SV.T")) using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(buffer.Length); // Encode it buffer = bf.Encode(buffer, 0, buffer.Length); // Add empty data Array.Resize(ref buffer, 1024); // Save it all bw.Write(buffer); } // Update Pk2 if (Pk2Writer.ImportFile("SV.T", "Temp\\SV.T")) { System.Diagnostics.Debug.WriteLine($"New version created and imported into the Pk2"); } // Close the Pk2 Pk2Writer.Close(); // Delete it File.Delete("Temp\\SV.T"); } }
public static void Server_FileCompleted(Packet p, Session s) { byte result = p.ReadByte(); // File downloaded var file = DownloadFiles[0]; System.Diagnostics.Debug.WriteLine($"Server_FileCompleted: {file.ID} - Result:{result}"); // Close the file m_FileStream.Close(); m_FileStream.Dispose(); // Process the file if (file.ImportToPk2) { // Get the Pk2 to open int pk2FileNameLength = file.Path.IndexOf("\\"); string pk2FileName = file.Path.Remove(pk2FileNameLength); // Open the Pk2 and insert the file if (Pk2Writer.Open(pk2FileName, LauncherSettings.CLIENT_BLOWFISH_KEY)) { if (Pk2Writer.ImportFile(file.Path.Substring(pk2FileNameLength + 1) + "\\" + file.Name, "Temp\\" + file.ID)) { System.Diagnostics.Debug.WriteLine($"File {file.Name} imported into the Pk2"); } // Close the Pk2 Pk2Writer.Close(); } // Delete the file File.Delete("Temp\\" + file.ID); } else { // Create an empty file to decompress (zlib) using (FileStream outFileStream = File.Create("Temp\\" + file.Name)) { using (FileStream inFileStream = new FileStream("Temp\\" + file.ID, FileMode.Open, FileAccess.Read)) { // Read past unknown bytes (4) and zlib header bytes (2) inFileStream.Seek(6, SeekOrigin.Begin); using (DeflateStream zlib = new DeflateStream(inFileStream, CompressionMode.Decompress)) { zlib.CopyTo(outFileStream); } } // Delete decompressed file File.Delete("Temp\\" + file.ID); } // Check file path if (file.Path != string.Empty) { // Create directory if doesn't exists if (!Directory.Exists(file.Path)) { Directory.CreateDirectory(file.Path); } // Fix path for easy file moving file.Path += "\\"; } // Check if it's the Launcher to process it at the end var launcherFilename = Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); if (file.Path == string.Empty && file.Name.Equals(launcherFilename, StringComparison.InvariantCultureIgnoreCase)) { ForceMovingFile("Temp\\" + file.Name, "Temp\\_" + file.Name); } else { // Move or replace from Temp to the folder required ForceMovingFile("Temp\\" + file.Name, file.Path + file.Name); } } // Remove the file and delete it from Temp DownloadFiles.RemoveAt(0); // Continue protocol if (DownloadFiles.Count > 0) { RequestFileDownload(s); } else { System.Diagnostics.Debug.WriteLine($"Download finished!"); // Update SV.T on media.pk2 UpdateSilkroadVersion(); // Update done! LauncherViewModel.Instance.IsUpdating = false; // Dispose pk2 writer Pk2Writer.Deinitialize(); // Replace the Launcher if exists if (File.Exists("Temp\\_" + Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName))) { // Replace launcher required StartReplacer(); } else { // Update launcher version LauncherViewModel.Instance.Version = DownloadVersion; LauncherViewModel.Instance.CanStartGame = true; // Stop connection s.Stop(); } } }