/// <summary> /// Load language pack from bytes /// </summary> /// <param name="s"></param> /// <returns></returns> public static LanguagePack Load(byte[] s) { LanguagePack lp = null; BinaryReader br = null; byte[] o_data = null; try { o_data = Compressor.Decompress(s); } catch (Exception ex) { throw new CannotLoadLanguagePack("Cannot decompress language pack, " + ex.Message, LanguagePackErrorCode.CompressionError, ex); } try { br = new BinaryReader(new MemoryStream(o_data)); short version = br.ReadInt16(); if (version > LPVERSION) { throw new CannotLoadLanguagePack("The language pack is newer than the pack reader in this program, update!", LanguagePackErrorCode.VersionHigher); } else if (version < LPVERSION) { throw new CannotLoadLanguagePack("The language pack is old, use the Language Packer tool to upgrade it!", LanguagePackErrorCode.VersionLower); } lp = new LanguagePack(); lp.Name = br.ReadString(); lp.Author = br.ReadString(); lp.Language = br.ReadString(); int i_length = br.ReadInt32(); byte[] image = br.ReadBytes(i_length); lp.Picture = Util.BytesToImage(image); int s_length = br.ReadInt32(); lp.Table = new Dictionary <string, string>(); for (int i = 1; i <= s_length; i++) { lp.Table[br.ReadString()] = br.ReadString(); } lp.Valid = true; } catch (Exception ex) { throw new CannotLoadLanguagePack("Language pack is corrupted, " + ex.Message, LanguagePackErrorCode.PackCorrupted, ex); } br.Close(); return(lp); }
/// <summary> /// Compile all plugin language packs /// </summary> public static void CompileLanguagePacks() { if (Plugins.Count == 0) { return; } foreach (PluginData pd in Plugins) { IPluginClient plugin = pd.Plugin; if (plugin.LanguagePacks == null) { continue; } foreach (ILanguagePack lp in plugin.LanguagePacks) { LanguagePack l = new LanguagePack(); l.Valid = true; l.Author = lp.Author; l.Language = lp.Language; l.Name = lp.Name; l.Picture = lp.Flag; l.Table = lp.Table; LanguageManager.AddPack(l); } } }
/// <summary> /// Add a pack to main list /// </summary> /// <param name="task"></param> public static void AddPack(LanguagePack pack) { if (Packs == null) { Packs = new Dictionary <string, LanguagePack>(); } Packs.Add(pack.Language, pack); }
/// <summary> /// Load pack from bytes /// </summary> /// <param name="s">Bytes to load</param> public static void LoadFromBytes(byte[] s) { if (Packs == null) { Packs = new Dictionary <string, LanguagePack>(); } LanguagePack lp = LanguagePack.Load(s); Packs.Add(lp.Language, lp); }
/// <summary> /// Save language pack /// </summary> /// <param name="pack">Language Pack</param> /// <param name="path">The location to save</param> /// <returns>If it was a success</returns> public static bool Save(LanguagePack pack, String path) { FileStream fs = null; BinaryWriter bw = null; byte[] buffer; //Try and open the path for writing try { fs = new FileStream(path, FileMode.Create); bw = new BinaryWriter(fs); } catch (Exception ex) { throw new CannotLoadLanguagePack("Unable to open the path target for writing!", LanguagePackErrorCode.FileSystemError, ex); } try { byte[] image = Util.ImageToBytes(pack.Picture); bw.Write((Int16)LPVERSION); bw.Write(pack.Name); bw.Write(pack.Author); bw.Write(pack.Language); bw.Write((Int32)image.Length); bw.Write(image, 0, image.Length); //Write strings bw.Write((Int32)pack.Table.Count); foreach (KeyValuePair <String, String> entry in pack.Table) { bw.Write(entry.Key); bw.Write(entry.Value); } bw.Close(); buffer = File.ReadAllBytes(path); File.Delete(path); File.WriteAllBytes(path, Compressor.Compress(buffer)); fs.Close(); return(true); } catch (Exception ex) { if (File.Exists(path)) { try { File.Delete(path); } catch (Exception ex2) { throw new CannotLoadLanguagePack("Could not remove buffer!", LanguagePackErrorCode.FileSystemError, ex2); } } throw new CannotLoadLanguagePack("Unable to create the language pack!", LanguagePackErrorCode.PackCorrupted, ex); } }
/// <summary> /// Save this pack /// </summary> /// <param name="path">The path</param> /// <returns>If it was a success</returns> public bool Save(String path) { return(LanguagePack.Save(this, path)); }