private void _save(string path = null) { _async.SetAndRunOperation(new GrfThread(delegate { try { Progress = -1; LuaParser accId = new LuaParser(new byte[0], true, p => new Lub(p).Decompile(), EncodingService.DisplayEncoding, EncodingService.DisplayEncoding); Dictionary <string, string> accIdT = new Dictionary <string, string>(); accId.Tables["ACCESSORY_IDs"] = accIdT; LuaParser accName = new LuaParser(new byte[0], true, p => new Lub(p).Decompile(), EncodingService.DisplayEncoding, EncodingService.DisplayEncoding); Dictionary <string, string> accNameT = new Dictionary <string, string>(); accName.Tables["AccNameTable"] = accNameT; foreach (var item in _obItems.Where(item => item.Id > 0).Where(item => !String.IsNullOrEmpty(item.AccId))) { accIdT[item.AccId] = item.Id.ToString(CultureInfo.InvariantCulture); } if (path == null) { _multiGrf.Clear(); string file = TemporaryFilesManager.GetTemporaryFilePath("tmp2_{0:0000}.lua"); _writeAccName(file, EncodingService.DisplayEncoding); _multiGrf.SetData(ProjectConfiguration.SyncAccName, File.ReadAllBytes(file)); file = TemporaryFilesManager.GetTemporaryFilePath("tmp2_{0:0000}.lua"); accId.Write(file, EncodingService.DisplayEncoding); _multiGrf.SetData(ProjectConfiguration.SyncAccId, File.ReadAllBytes(file)); _multiGrf.SaveAndReload(); } else { Directory.CreateDirectory(path); _writeAccName(GrfPath.Combine(path, "accname.lub"), EncodingService.DisplayEncoding); accId.Write(GrfPath.Combine(path, "accessoryid.lub"), EncodingService.DisplayEncoding); } } catch (Exception err) { ErrorHandler.HandleException(err); } finally { Progress = 100f; } }, this, 200)); }
private static void _writeLuaFiles(LuaParser accId, LuaParser accName, AbstractDb <int> db) { string file = TemporaryFilesManager.GetTemporaryFilePath("tmp2_{0:0000}.lua"); accId.Write(file, EncodingService.DisplayEncoding); db.ProjectDatabase.MetaGrf.SetData(ProjectConfiguration.SyncAccId, File.ReadAllBytes(file)); file = TemporaryFilesManager.GetTemporaryFilePath("tmp2_{0:0000}.lua"); accName.Write(file, EncodingService.DisplayEncoding); db.ProjectDatabase.MetaGrf.SetData(ProjectConfiguration.SyncAccName, File.ReadAllBytes(file)); }
public static void WriteMobLuaFiles(AbstractDb <int> db) { // Ensures this is only written once if (ProjectConfiguration.SynchronizeWithClientDatabases && db.DbSource == ServerDbs.Mobs && ProjectConfiguration.SyncMobTables) { var metaTable = db.ProjectDatabase.GetMetaTable <int>(ServerDbs.Mobs); //var table = Attached["jobtbl_T"] as Dictionary<string, string>; // Load the tables DbDebugItem <int> debug = new DbDebugItem <int>(db); DbAttachLuaLoaderUpper(debug, "jobtbl", ProjectConfiguration.SyncMobId); var table = db.Attached["jobtbl_T"] as Dictionary <string, string>; if (table != null) { Dictionary <int, Npc> npcs = new Dictionary <int, Npc>(); var dataJobName = debug.AbsractDb.ProjectDatabase.MetaGrf.GetData(ProjectConfiguration.SyncMobName); if (dataJobName == null) { return; } LuaParser parser = new LuaParser(dataJobName, true, p => new Lub(p).Decompile(), EncodingService.DetectEncoding(dataJobName), EncodingService.DisplayEncoding); var jobNames = parser.Tables["JobNameTable"]; // Load the npcs from the lua files first foreach (var keyPair in table) { npcs[Int32.Parse(keyPair.Value)] = new Npc { NpcName = keyPair.Key }; } foreach (var keyPair in jobNames) { var key = keyPair.Key.Trim('[', ']'); var ingameSprite = keyPair.Value.Trim('\"'); int ival; if (!Int32.TryParse(key, out ival)) { key = key.Substring(7); var npcKeyPair = npcs.FirstOrDefault(p => p.Value.NpcName == key); if (npcKeyPair.Equals(default(KeyValuePair <int, Npc>))) { // Key not found // We ignore it } else { npcs[npcKeyPair.Key] = new Npc(npcKeyPair.Value) { IngameSprite = ingameSprite }; //npcKeyPair.Value = new ingameSprite; } continue; } npcs[ival] = new Npc { IngameSprite = ingameSprite }; } foreach (var tuple in metaTable.FastItems.OrderBy(p => p.Key)) { var ssprite = "JT_" + (tuple.GetValue <string>(ServerMobAttributes.SpriteName) ?? ""); var csprite = tuple.GetValue <string>(ServerMobAttributes.ClientSprite); if (ssprite != "JT_") { // not empty if (npcs.ContainsKey(tuple.Key)) { npcs[tuple.Key] = new Npc(npcs[tuple.Key]) { IngameSprite = csprite, NpcName = ssprite }; } else { Npc npc = new Npc { IngameSprite = csprite, NpcName = ssprite }; npcs[tuple.Key] = npc; } } } // Validation HashSet <string> duplicates = new HashSet <string>(); foreach (var npc in npcs) { if (!duplicates.Add(npc.Value.NpcName)) { DbIOErrorHandler.Handle(StackTraceException.GetStrackTraceException(), "Duplicate mob name (" + npc.Value.NpcName + ") for mobid " + npc.Key + " while saving npcidentity and jobname. The files have not been resaved."); DbIOErrorHandler.Focus(); return; } if (LatinOnly(npc.Value.NpcName) != npc.Value.NpcName) { DbIOErrorHandler.Handle(StackTraceException.GetStrackTraceException(), "The mob name (" + npc.Value.NpcName + ") is invalid, only ASCII characters are allowed. Consider using '" + LatinOnly(npc.Value.NpcName) + "' as the name instead. The files have not been resaved."); DbIOErrorHandler.Focus(); return; } } // Converts back to a lua format { BackupEngine.Instance.BackupClient(ProjectConfiguration.SyncMobId, db.ProjectDatabase.MetaGrf); string file = TemporaryFilesManager.GetTemporaryFilePath("tmp2_{0:0000}.lua"); parser.Tables.Clear(); var dico = new Dictionary <string, string>(); parser.Tables["jobtbl"] = dico; foreach (var npc in npcs.OrderBy(p => p.Key)) { dico[npc.Value.NpcName] = npc.Key.ToString(CultureInfo.InvariantCulture); } parser.Write(file, EncodingService.DisplayEncoding); db.ProjectDatabase.MetaGrf.SetData(ProjectConfiguration.SyncMobId, File.ReadAllBytes(file)); } { BackupEngine.Instance.BackupClient(ProjectConfiguration.SyncMobName, db.ProjectDatabase.MetaGrf); string file = TemporaryFilesManager.GetTemporaryFilePath("tmp2_{0:0000}.lua"); parser.Tables.Clear(); var dico = new Dictionary <string, string>(); parser.Tables["JobNameTable"] = dico; foreach (var npc in npcs.OrderBy(p => p.Key)) { var ingameSprite = LatinUpper((npc.Value.IngameSprite ?? "")); if (!String.IsNullOrEmpty(ingameSprite.GetExtension())) { ingameSprite = ingameSprite.ReplaceExtension(ingameSprite.GetExtension().ToLower()); } if (string.IsNullOrEmpty(ingameSprite)) { continue; } dico["[jobtbl." + npc.Value.NpcName + "]"] = "\"" + ingameSprite + "\""; } parser.Write(file, EncodingService.DisplayEncoding); db.ProjectDatabase.MetaGrf.SetData(ProjectConfiguration.SyncMobName, File.ReadAllBytes(file)); } } } }