/// <summary> /// Try to load the Type file from the Pk2 /// </summary> /// <param name="Pk2Reader">Pk2 used to search</param> /// <returns>Return success</returns> private bool LoadTypeFile(Pk2Reader Pk2Reader) { var temp = Pk2Reader.GetFileText("Type.txt"); // Check if file has been found if (temp != null) { // Analyze the file and extract language var match = Regex.Match(temp, "Language[ ]{0,1}=[ ]{0,1}[\"]{0,1}([a-zA-Z]*)[\"]{0,1}"); if (match.Success) { // Try to find the index selected for (int i = 0; i < LauncherSettings.CLIENT_LANGUAGE_SUPPORTED.Length; i++) { if (LauncherSettings.CLIENT_LANGUAGE_SUPPORTED[i] == match.Groups[1].Value) { m_TypeFile = temp; SupportedLanguageIndex = i; return(true); } } } } return(false); }
/// <summary> /// Initialize all stuffs required for the connection and settings /// </summary> private void Initialize() { Pk2Reader pk2Reader = null; try { // Load pk2 reader pk2Reader = new Pk2Reader(LauncherSettings.PATH_PK2_MEDIA, LauncherSettings.CLIENT_BLOWFISH_KEY); // Load settings m_Config = new ConfigViewModel(); m_Config.Load(pk2Reader); // Extract essential stuffs for the process if (pk2Reader.TryGetDivisionInfo(out m_DivisionInfo) && pk2Reader.TryGetGateport(out m_Gateport) && pk2Reader.TryGetVersion(out m_Version) && pk2Reader.TryGetLocale(out m_Locale)) { IsLoaded = true; // Force string to be updated Version = m_Version; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } finally { pk2Reader?.Close(); } }
/// <summary> /// Try to get the Silkroad version from Pk2 /// </summary> /// <returns>Return success</returns> public static bool TryGetVersion(this Pk2Reader Pk2Reader, out uint Version) { try { // Localize the file and prepare to read it Stream data = Pk2Reader.GetFileStream("SV.T"); BinaryReader buffer = new BinaryReader(data, Encoding.ASCII); // Reading the encrypted file int versionLength = buffer.ReadInt32(); byte[] versionBuffer = buffer.ReadBytes(versionLength); // Initialize the blowfish to decrypt the file Blowfish bf = new Blowfish(); bf.Initialize(Encoding.ASCII.GetBytes("SILKROADVERSION"), 0, 8); // Decrypting versionBuffer = bf.Decode(versionBuffer); // Only four starting bytes contains the numbers Version = uint.Parse(Encoding.ASCII.GetString(versionBuffer, 0, 4)); // Success return(true); } catch { Version = uint.MinValue; return(false); } }
/// <summary> /// Load settings or create a new one /// </summary> public void Load(Pk2Reader pk2Reader) { // Loads language from pk2 LoadTypeFile(pk2Reader); // Try to load configs or create a new one bool createNew = false; if (!LoadSilkCfg()) { m_SilkCfg = new SilkCfg(); createNew = true; } if (!LoadSROptionSet()) { m_SROptionSet = new SROptionSet(); createNew = true; } BindConfigs(); // Save changes created if (createNew) { Save(); } }
/// <summary> /// Try to loads all required everything to create the connection to server /// </summary> private void LoadPk2() { Pk2Reader pk2Reader = null; try { // Load pk2 reader pk2Reader = new Pk2Reader(LauncherSettings.PATH_PK2_MEDIA, LauncherSettings.CLIENT_BLOWFISH_KEY); // Try to load Type.txt m_Config.LoadTypeFile(pk2Reader); // Extract essential stuffs for the process if (pk2Reader.TryGetDivisionInfo(out m_DivisionInfo) && pk2Reader.TryGetGateport(out m_Gateport) && pk2Reader.TryGetVersion(out m_Version) && pk2Reader.TryGetLocale(out m_Locale)) { IsLoaded = true; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } finally { pk2Reader?.Close(); } }
/// <summary> /// Try to get the gateway list by division names /// </summary> /// <returns>Return success</returns> public static bool TryGetDivisionInfo(this Pk2Reader Pk2Reader, out Dictionary <string, List <string> > DivisionInfo) { try { // Localize the file and prepare to read it Stream data = Pk2Reader.GetFileStream("DivisionInfo.txt"); BinaryReader buffer = new BinaryReader(data, Encoding.ASCII); // initialize DivisionInfo = new Dictionary <string, List <string> >(); // Ignore locale byte buffer.ReadByte(); // Reading all divitions byte divisionCount = buffer.ReadByte(); for (byte i = 0; i < divisionCount; i++) { // Division Name string name = new string(buffer.ReadChars(buffer.ReadInt32())); // skip value (0) buffer.ReadByte(); // Division hosts byte hostCount = buffer.ReadByte(); List <string> hosts = new List <string>(hostCount); for (byte j = 0; j < hostCount; j++) { // host address string host = new string(buffer.ReadChars(buffer.ReadInt32())); // skip value (0) buffer.ReadByte(); // Add host hosts.Add(host); } // Add/overwrite division DivisionInfo[name] = hosts; } // Success return(true); } catch { DivisionInfo = null; return(false); } }
/// <summary> /// Try to load the Type file from the Pk2 /// </summary> /// <param name="Pk2Reader">Pk2 used to search</param> /// <returns>Return success</returns> public bool LoadTypeFile(Pk2Reader Pk2Reader) { var temp = Pk2Reader.GetFileText("Type.txt"); // Check if file has been found if (temp != null) { // Analyze the file and extract language var match = Regex.Match(temp, "Language[ ]{0,1}=[ ]{0,1}[\"]{0,1}([a-zA-Z]*)[\"]{0,1}"); if (match.Success) { m_TypeFile = temp; m_Language = match.Groups[1].Value; return(true); } } return(false); }
public static bool Load(string path, string key) { SilkroadPath = GetSetting("SilkroadPath", path); MediaPk2Key = GetSetting("MediaPk2Key", key); if (string.IsNullOrEmpty(SilkroadPath) || string.IsNullOrEmpty(MediaPk2Key)) { if (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(key)) { SilkroadPath = GetSetting("SilkroadPath", path, true); MediaPk2Key = GetSetting("MediaPk2Key", key, true); } else { return(false); } } _reader = new Pk2Reader(SilkroadPath + @"\Media.pk2", MediaPk2Key); var isUpdate = GetSetting("GameVersion", 0); var needUpdate = isUpdate == 0 || isUpdate < Version(); GameVerison = GetSetting("GameVersion", Version(), needUpdate); GatewayPort = ushort.Parse(GetSetting("GatewayPort", GatePort(), needUpdate).ToString()); DivisionServers = DivisionInfo().ToList(); if (!needUpdate) { return(true); } LiteDatabase.DropCollection(Skills.Name); LiteDatabase.DropCollection(Items.Name); LoadTextData(); Skills.Insert(LoadSkillsData().GroupBy(x => x.Id).Select(x => x.OrderBy(y => y.Id).First())); Items.Insert(LoadItemsData().GroupBy(x => x.Id).Select(x => x.OrderBy(y => y.Id).First())); Characters.Insert(LoadCharacterData().GroupBy(x => x.Id).Select(x => x.OrderBy(y => y.Id).First())); ItemNames.Clear(); SkillNames.Clear(); CharacterNames.Clear(); RegionNames.Clear(); GC.Collect(); GC.WaitForPendingFinalizers(); return(true); }
/// <summary> /// Try to get the port available to connect to the server /// </summary> /// <returns>Return success</returns> public static bool TryGetGateport(this Pk2Reader Pk2Reader, out ushort Gateport) { try { // Localize the file and prepare to read it string data = Pk2Reader.GetFileText("Gateport.txt"); // The file contains the port only Gateport = ushort.Parse(data.Trim()); // Success return(true); } catch { Gateport = ushort.MinValue; return(false); } }
/// <summary> /// Try to get the file localization type /// </summary> /// <returns>Return success</returns> public static bool TryGetLocale(this Pk2Reader Pk2Reader, out byte Locale) { try { // Localize the file and prepare to read it Stream data = Pk2Reader.GetFileStream("DivisionInfo.txt"); BinaryReader buffer = new BinaryReader(data, Encoding.ASCII); // Read first byte only Locale = buffer.ReadByte(); // Success return(true); } catch { Locale = byte.MinValue; return(false); } }
/// <summary> /// Try to generate the database. /// </summary> public void ThreadGenerateData() { Log("Opening Pk2 file using " + (tbxBlowfishKey.Text != "" ? "blowfish key: " + tbxBlowfishKey.Text : "default blowfish key")); LogState("Opening Pk2 file..."); try { pk2 = new Pk2Reader(MediaPk2Path, tbxBlowfishKey.Text); } catch { Log("Error opening Pk2 file. Possibly wrong blowfish key"); LogState("Error"); btnStart.InvokeIfRequired(() => { btnStart.Enabled = true; }); return; } Log("Pk2 file opened!"); LogState(); // Fill info to Main GUI Window w = Window.Get; try { Log("Extracting Silkroad Version"); LogState("Extracting..."); // Reading Packet p = new Packet(0, false, false, pk2.GetFileBytes("SV.T")); p.Lock(); int dataLength = p.ReadInt(); byte[] dataBuffer = p.ReadByteArray(dataLength); // Decoding Blowfish bf = new Blowfish(); bf.Initialize(Encoding.ASCII.GetBytes("SILKROADVERSION"), 0, dataLength); byte[] dataDecoded = bf.Decode(dataBuffer); this.Version = uint.Parse(Encoding.ASCII.GetString(dataDecoded, 0, 4)); } catch (Exception ex) { Log("Extracting error, the version cannot be readed. " + ex.Message); LogState("Error"); btnStart.InvokeIfRequired(() => { btnStart.Enabled = true; }); return; } try { Log("Extracting Locale & Gateway"); LogState("Extracting..."); // Reading Packet p = new Packet(0, false, false, pk2.GetFileBytes("DIVISIONINFO.TXT")); p.Lock(); this.Locale = p.ReadByte(); byte divisionCount = p.ReadByte(); for (int i = 0; i < divisionCount; i++) { string DivisionName = p.ReadString(p.ReadInt()); p.ReadByte(); // 0 byte gatewayCount = p.ReadByte(); Gateways = new System.Collections.Generic.List <string>(gatewayCount); for (int j = 0; j < gatewayCount; j++) { string gatewayHost = p.ReadString(p.ReadInt()); p.ReadByte(); // 0 Gateways.Add(gatewayHost); } } } catch (Exception ex) { Log("Extracting error, gateways cannot be readed. " + ex.Message); LogState("Error"); btnStart.InvokeIfRequired(() => { btnStart.Enabled = true; }); return; } try { Log("Extracting Gateport"); LogState("Extracting..."); // Reading Packet p = new Packet(0, false, false, pk2.GetFileBytes("GATEPORT.TXT")); p.Lock(); string test = p.ReadString(p.RemainingRead()); this.Gateport = ushort.Parse(test); } catch (Exception ex) { Log("Extracting error, the gateport cannot be readed. " + ex.Message); LogState("Error"); btnStart.InvokeIfRequired(() => { btnStart.Enabled = true; }); return; } // Updating database Log("Creating Database..."); string dbPath = GetDatabasePath(SilkroadName); if (File.Exists(dbPath)) { LogState("Deleting old database"); if (!WinAPI.FileTryDelete(dbPath)) { // Deleting issues Log("The database from \"" + SilkroadName + "\" is being used by another program. Please, close all the bots and try again!"); LogState("Error"); btnStart.InvokeIfRequired(() => { btnStart.Font = new Font(btnStart.Font, FontStyle.Regular); }); return; } } // Creating the database LogState("Creating database"); db = new SQLDatabase(dbPath); if (!db.Create()) { Log("Error creating the database. Please, close all the bots and try again!"); LogState("Error"); btnStart.InvokeIfRequired(() => { btnStart.Font = new Font(btnStart.Font, FontStyle.Regular); }); return; } Log("Database has been created!"); // Create connection LogState("Connecting to database"); if (!db.Connect()) { Log("Database connection error!"); Log("Error"); return; } LogState("Connected"); // Generating database Log("Generating database (this may take a while)"); SetLanguageIndex(); Log("Loading name references..."); LoadNameReferences(); Log("Loading & Adding text references..."); LoadTextReferences(); AddTextReferences(); Log("Adding Items..."); AddItems(); Log("Adding Magic options..."); AddMagicOptions(); Log("Adding Characters & Mobs..."); AddModels(); Log("Adding Masteries & Skills..."); AddMasteries(); AddSkills(); Log("Adding Exp. & Levels..."); AddLevelExperience(); Log("Adding Shops..."); AddShops(); Log("Loading Teleport references"); LoadTeleportData(); Log("Adding Teleports & Structures..."); AddTeleportBuildings(); AddTeleportLinks(); Log("Adding Regions..."); AddRegions(); Log("Database has been generated correctly!"); Log("Creating Item icons..."); AddItemIcons(); Log("Creating Skill icons..."); AddSkillIcons(); if (this.cbxMinimap.Checked) { Log("Creating minimap images to the bot folder..."); AddMinimap(); } Log("All has been generated succesfully, Enjoy! :)"); db.Close(); pk2.Close(); pk2.Dispose(); LogState("Closing Pk2 file..."); Thread.Sleep(1000); WinAPI.InvokeIfRequired(this, () => { this.DialogResult = DialogResult.OK; this.Activate(); this.Close(); }); }