public static Kaede FromFile(string Path) { Kaede instance = new Kaede(); BinaryReader b = new BinaryReader(File.Open(Path, FileMode.Open)); if (string.Compare(new string(b.ReadChars(6)), "KAEDE!") != 0) // check for the header { b.Close(); throw new System.Exception("Invalid Kaede Translation file!"); } int ver = b.ReadInt32(); // get teh version int fbLocaleID = b.ReadInt32(); // get the fall back locale id if (ver != Version) // version checks pls { b.Close(); throw new System.Exception(string.Format("This Kaede Translation file was designed to be used with v{0}, tho I'm running v{1}.", ver, Version)); } int cntLocales = b.ReadInt32(); // get how many locales are there if (cntLocales < 0) // if negative(wtf?) { b.Close(); throw new System.Exception("Invalid structure!"); } for (int i = 0; i < cntLocales; i++) // import em. { Locale l = Locale.FromBinaryReader(b); if (l.Id == fbLocaleID && instance.FallbackLocale == null) { instance.FallbackLocale = l; } instance.Locales.Add(l); } b.Close(); instance.TranslationFilePath = Path; return(instance); }
// optimised function to not take up memory; loads only required locales public static KaedeEngine LoadLocale(string TargetLocale) { Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ItekiSwitcher.Translation.ItekiSwitcher.ktf"); KaedeEngine instance = new KaedeEngine(); BinaryReader b = new BinaryReader(stream); if (string.Compare(new string(b.ReadChars(6)), "KAEDE!") != 0) // check for the header { b.Close(); throw new System.Exception("Invalid Kaede Translation file!"); } int ver = b.ReadInt32(); // get teh version int fbLocaleID = b.ReadInt32(); // get the fall back locale id if (ver != Version) // version checks pls { b.Close(); throw new System.Exception(string.Format("Invalid translation file; It was designed for v{0}.", ver)); } int cntLocales = b.ReadInt32(); // get how many locales are there if (cntLocales < 0) // if negative(wtf?) { b.Close(); throw new System.Exception("Invalid structure!"); } for (int i = 0; i < cntLocales; i++) { Locale l = Locale.FromBinaryReader(b); if (l.Id == fbLocaleID && instance.FallbackLocale == null) { instance.FallbackLocale = l; } if (string.Compare(l.Code, TargetLocale) == 0 && instance.Locale == null) { instance.Locale = l; } instance.AllLocales.Add(new LocalePreview() { LocalisedName = l.LocalisedName, Id = l.Id, Code = l.Code }); } b.Close(); if (instance.Locale == null) { throw new System.Exception(string.Format("Locale {0} not found!", TargetLocale)); } return(instance); }