/// <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();
             }
         }
     }
 }
Beispiel #2
0
 /// <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");
     }
 }
Beispiel #3
0
        public static void Server_PatchResponse(Packet p, Session s)
        {
            System.Diagnostics.Debug.WriteLine("Server_PatchResponse");
            LauncherViewModel.Instance.IsCheckingUpdates = false;

            // Analyze the patch
            switch (p.ReadByte())
            {
            case 1:
                LauncherViewModel.Instance.CanStartGame = true;
                break;

            case 2:
                byte errorCode = p.ReadByte();
                switch (errorCode)
                {
                case 2:
                {
                    string DownloadServerIP   = p.ReadAscii();
                    ushort DownloadServerPort = p.ReadUShort();
                    DownloadModule.DownloadVersion = p.ReadUInt();

                    System.Diagnostics.Debug.WriteLine("Version outdate. New version available (v" + DownloadModule.DownloadVersion + ")");
                    ulong bytesToDownload = 0;

                    while (p.ReadByte() == 1)
                    {
                        FileEntry file = new FileEntry()
                        {
                            ID          = p.ReadUInt(),
                            Name        = p.ReadAscii(),
                            Path        = p.ReadAscii(),
                            Size        = p.ReadUInt(),
                            ImportToPk2 = p.ReadByte() == 1
                        };

                        DownloadModule.DownloadFiles.Add(file);

                        bytesToDownload += file.Size;
                    }

                    // Set progress bar values
                    LauncherViewModel.Instance.UpdatingBytesMaxDownloading = bytesToDownload;
                    LauncherViewModel.Instance.UpdatingBytesDownloading    = 0;

                    // Start downloader protocol
                    if (DownloadModule.DownloadFiles.Count > 0)
                    {
                        // Try to load the GFXFileManager
                        if (Pk2Writer.Initialize("GFXFileManager.dll"))
                        {
                            // Start downloading patch
                            LauncherViewModel.Instance.IsUpdating = true;
                            System.Diagnostics.Debug.WriteLine("Downloading updates...");

                            Session downloaderSession = new Session();
                            downloaderSession.AddHandler(Opcode.GLOBAL_IDENTIFICATION, new PacketHandler(Server_GlobalIdentification));
                            downloaderSession.AddHandler(DownloadModule.Opcode.SERVER_READY, new PacketHandler(DownloadModule.Server_Ready));
                            downloaderSession.AddHandler(DownloadModule.Opcode.SERVER_FILE_CHUNK, new PacketHandler(DownloadModule.Server_FileChunk));
                            downloaderSession.AddHandler(DownloadModule.Opcode.SERVER_FILE_COMPLETED, new PacketHandler(DownloadModule.Server_FileCompleted));

                            downloaderSession.Disconnect += new System.EventHandler((_Session, _Event) => {
                                    System.Diagnostics.Debug.WriteLine("Download: Session disconnected");
                                });

                            System.Threading.Tasks.Task.Run(() => downloaderSession.Start(DownloadServerIP, DownloadServerPort, 10000));
                        }
                        else
                        {
                            LauncherViewModel.Instance.ShowMessage("GFXFileManager not found!");
                        }
                    }
                }
                break;

                case 4:
                    LauncherViewModel.Instance.ShowMessage(LauncherSettings.MSG_PATCH_UNABLE);
                    break;

                case 5:
                    LauncherViewModel.Instance.ShowMessage(LauncherSettings.MSG_PATCH_TOO_OLD);
                    break;

                case 1:
                    LauncherViewModel.Instance.ShowMessage(LauncherSettings.MSG_PATCH_TOO_NEW);
                    break;

                default:
                    LauncherViewModel.Instance.ShowMessage("Patch Error: [" + errorCode + "]");
                    break;
                }
                break;
            }

            // Request shard list just for fun c:
            System.Diagnostics.Debug.WriteLine("CLIENT_SHARD_LIST_REQUEST");
            s.Send(new Packet(Opcode.CLIENT_SHARD_LIST_REQUEST, true));
            s.Send(new Packet(Opcode.CLIENT_SHARD_LIST_PING_REQUEST, true)); // Not even sure what is this..
            // Request notice
            System.Diagnostics.Debug.WriteLine("CLIENT_WEB_NOTICE_REQUEST");
            Packet packet = new Packet(Opcode.CLIENT_WEB_NOTICE_REQUEST);

            packet.WriteByte(LauncherViewModel.Instance.Locale);
            s.Send(packet);
        }
Beispiel #4
0
        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();
                }
            }
        }