public static bool json2sql_quest(FileInfo fi, DirectoryInfo outputFolder = null) { // read json quest if (!GDLELoader.TryLoadQuest(fi.FullName, out var result)) { Console.WriteLine($"Failed to parse {fi.FullName}"); return(false); } // convert to sql quest if (!GDLEConverter.TryConvert(result, out var quest)) { Console.WriteLine($"Failed to convert {fi.FullName}"); return(false); } // output to sql try { if (QuestSQLWriter == null) { QuestSQLWriter = new QuestSQLWriter(); } if (quest.LastModified == DateTime.MinValue) { quest.LastModified = DateTime.UtcNow; } var sqlFilename = fi.Name.Replace(".json", ".sql"); var sqlFolder = outputFolder ?? fi.Directory; var sqlFile = new StreamWriter(sqlFolder.FullName + Path.DirectorySeparatorChar + sqlFilename); QuestSQLWriter.CreateSQLDELETEStatement(quest, sqlFile); sqlFile.WriteLine(); QuestSQLWriter.CreateSQLINSERTStatement(quest, sqlFile); sqlFile.Close(); Console.WriteLine($"Converted {fi.FullName} to {fi.DirectoryName}{Path.DirectorySeparatorChar}{sqlFilename}"); } catch (Exception e) { Console.WriteLine(e); Console.WriteLine($"Failed to convert {fi.FullName}"); return(false); } return(true); }
public static bool json2sql_recipe(FileInfo fi, string[] lines = null, DirectoryInfo outputFolder = null) { // read json into lsd recipe RecipeCombined result = null; if (lines == null) { if (!GDLELoader.TryLoadRecipeCombined(fi.FullName, out result)) { Console.WriteLine($"Failed to parse {fi.FullName}"); return(false); } } else { if (!GDLELoader.TryLoadRecipeCombined(lines, out result)) { Console.WriteLine($"Failed to parse recipe"); return(false); } } // convert to ace cookbooks + recipe if (!GDLEConverter.TryConvert(result, out var cookbooks, out var recipe)) { Console.WriteLine($"Failed to convert {fi.FullName}"); return(false); } // output to sql try { if (RecipeSQLWriter == null) { RecipeSQLWriter = new RecipeSQLWriter(); RecipeSQLWriter.WeenieNames = IDToString.Reader.GetIDToNames("WeenieName.txt"); } if (CookBookSQLWriter == null) { CookBookSQLWriter = new CookBookSQLWriter(); CookBookSQLWriter.WeenieNames = IDToString.Reader.GetIDToNames("WeenieName.txt"); } if (recipe.LastModified == DateTime.MinValue) { recipe.LastModified = DateTime.UtcNow; } foreach (var cookbook in cookbooks) { if (cookbook.LastModified == DateTime.MinValue) { cookbook.LastModified = DateTime.UtcNow; } } StreamWriter sqlFile = null; string sqlFilename = null; MemoryStream memoryStream = null; if (lines == null) { sqlFilename = RecipeSQLWriter.GetDefaultFileName(recipe, cookbooks); var sqlFolder = outputFolder ?? fi.Directory; sqlFile = new StreamWriter(sqlFolder.FullName + Path.DirectorySeparatorChar + sqlFilename); } else { memoryStream = new MemoryStream(); sqlFile = new StreamWriter(memoryStream); } RecipeSQLWriter.CreateSQLDELETEStatement(recipe, sqlFile); sqlFile.WriteLine(); RecipeSQLWriter.CreateSQLINSERTStatement(recipe, sqlFile); sqlFile.WriteLine(); CookBookSQLWriter.CreateSQLDELETEStatement(cookbooks, sqlFile); sqlFile.WriteLine(); CookBookSQLWriter.CreateSQLINSERTStatement(cookbooks, sqlFile); sqlFile.Close(); if (lines != null) { Output = memoryStream.ToString(); return(true); } Console.WriteLine($"Converted {fi.FullName} to {fi.DirectoryName}{Path.DirectorySeparatorChar}{sqlFilename}"); } catch (Exception e) { Console.WriteLine(e); Console.WriteLine($"Failed to convert {fi.FullName}"); return(false); } return(true); }
public static bool json2sql_landblock(FileInfo fi, string[] lines = null, DirectoryInfo outputFolder = null) { // read json into gdle spawnmap Landblock result = null; if (lines == null) { if (!GDLELoader.TryLoadLandblock(fi.FullName, out result)) { Console.WriteLine($"Failed to parse {fi.FullName}"); return(false); } } else { if (!GDLELoader.TryLoadLandblock(lines, out result)) { Console.WriteLine($"Failed to parse landblock"); return(false); } } // convert to sql landblock instances if (!GDLEConverter.TryConvert(result, out var landblockInstances, out var landblockInstanceLinks)) { Console.WriteLine($"Failed to convert {fi.FullName}"); return(false); } // link up instances // TODO: move this to TryConvert foreach (var link in landblockInstanceLinks) { var parent = landblockInstances.FirstOrDefault(i => i.Guid == link.ParentGuid); if (parent == null) { Console.WriteLine($"Couldn't find parent guid for {link.ParentGuid:X8}"); continue; } parent.LandblockInstanceLink.Add(link); var child = landblockInstances.FirstOrDefault(i => i.Guid == link.ChildGuid); if (child == null) { Console.WriteLine($"Couldn't find child guid for {link.ChildGuid:X8}"); continue; } child.IsLinkChild = true; } // output to sql try { if (LandblockSQLWriter == null) { LandblockSQLWriter = new LandblockSQLWriter(); LandblockSQLWriter.WeenieNames = IDToString.Reader.GetIDToNames("WeenieName.txt"); } foreach (var landblockInstance in landblockInstances) { if (landblockInstance.LastModified == DateTime.MinValue) { landblockInstance.LastModified = DateTime.UtcNow; } } foreach (var landblockInstanceLink in landblockInstanceLinks) { if (landblockInstanceLink.LastModified == DateTime.MinValue) { landblockInstanceLink.LastModified = DateTime.UtcNow; } } StreamWriter sqlFile = null; string sqlFilename = null; MemoryStream memoryStream = null; if (lines == null) { sqlFilename = LandblockSQLWriter.GetDefaultFileName(landblockInstances[0]); var sqlFolder = outputFolder ?? fi.Directory; sqlFile = new StreamWriter(sqlFolder.FullName + Path.DirectorySeparatorChar + sqlFilename); } else { memoryStream = new MemoryStream(); sqlFile = new StreamWriter(memoryStream); } LandblockSQLWriter.CreateSQLDELETEStatement(landblockInstances, sqlFile); sqlFile.WriteLine(); LandblockSQLWriter.CreateSQLINSERTStatement(landblockInstances, sqlFile); sqlFile.Close(); if (lines != null) { Output = memoryStream.ToString(); return(true); } Console.WriteLine($"Converted {fi.FullName} to {fi.DirectoryName}{Path.DirectorySeparatorChar}{sqlFilename}"); } catch (Exception e) { Console.WriteLine(e); Console.WriteLine($"Failed to convert {fi.FullName}"); return(false); } return(true); }