Skip to content

Commit

Permalink
Better handling of export-sql functions (#4235)
Browse files Browse the repository at this point in the history
* Handle empty landlocks when attempting to export-sql

* Adjusted all sqlFile output in a `using` loop to help prevent locked files on failure.

---------

Co-authored-by: OptimShi <[email protected]>
  • Loading branch information
OptimShi and OptimShi authored Oct 24, 2024
1 parent 83fd7d4 commit f108bb3
Showing 1 changed file with 72 additions and 66 deletions.
138 changes: 72 additions & 66 deletions Source/ACE.Server/Command/Handlers/DeveloperContentCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,21 +571,21 @@ public static string json2sql_weenie(Session session, string folder, string json
output.LastModified = DateTime.UtcNow;

sqlFilename = WeenieSQLWriter.GetDefaultFileName(output);
var sqlFile = new StreamWriter(sqlFolder + sqlFilename);
using (StreamWriter sqlFile = new StreamWriter(sqlFolder + sqlFilename))
{

WeenieSQLWriter.CreateSQLDELETEStatement(output, sqlFile);
sqlFile.WriteLine();
WeenieSQLWriter.CreateSQLDELETEStatement(output, sqlFile);
sqlFile.WriteLine();

WeenieSQLWriter.CreateSQLINSERTStatement(output, sqlFile);
WeenieSQLWriter.CreateSQLINSERTStatement(output, sqlFile);

var metadata = new Adapter.GDLE.Models.Metadata(weenie);
if (metadata.HasInfo)
{
var jsonEx = JsonSerializer.Serialize(metadata, LifestonedConverter.SerializerSettings);
sqlFile.WriteLine($"\n/* Lifestoned Changelog:\n{jsonEx}\n*/");
var metadata = new Adapter.GDLE.Models.Metadata(weenie);
if (metadata.HasInfo)
{
var jsonEx = JsonSerializer.Serialize(metadata, LifestonedConverter.SerializerSettings);
sqlFile.WriteLine($"\n/* Lifestoned Changelog:\n{jsonEx}\n*/");
}
}

sqlFile.Close();
}
catch (Exception e)
{
Expand Down Expand Up @@ -659,20 +659,18 @@ public static string json2sql_recipe(Session session, string folder, string json
}

sqlFilename = RecipeSQLWriter.GetDefaultFileName(recipe, cookbooks);
var sqlFile = new StreamWriter(sqlFolder + sqlFilename);
using (StreamWriter sqlFile = new StreamWriter(sqlFolder + sqlFilename)) {
RecipeSQLWriter.CreateSQLDELETEStatement(recipe, sqlFile);
sqlFile.WriteLine();

RecipeSQLWriter.CreateSQLDELETEStatement(recipe, sqlFile);
sqlFile.WriteLine();
RecipeSQLWriter.CreateSQLINSERTStatement(recipe, sqlFile);
sqlFile.WriteLine();

RecipeSQLWriter.CreateSQLINSERTStatement(recipe, sqlFile);
sqlFile.WriteLine();
CookBookSQLWriter.CreateSQLDELETEStatement(cookbooks, sqlFile);
sqlFile.WriteLine();

CookBookSQLWriter.CreateSQLDELETEStatement(cookbooks, sqlFile);
sqlFile.WriteLine();

CookBookSQLWriter.CreateSQLINSERTStatement(cookbooks, sqlFile);

sqlFile.Close();
CookBookSQLWriter.CreateSQLINSERTStatement(cookbooks, sqlFile);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -760,14 +758,14 @@ public static string json2sql_landblock(Session session, string folder, string j
}

sqlFilename = LandblockInstanceWriter.GetDefaultFileName(landblockInstances[0]);
var sqlFile = new StreamWriter(sqlFolder + sqlFilename);

LandblockInstanceWriter.CreateSQLDELETEStatement(landblockInstances, sqlFile);
sqlFile.WriteLine();

LandblockInstanceWriter.CreateSQLINSERTStatement(landblockInstances, sqlFile);
using (StreamWriter sqlFile = new StreamWriter(sqlFolder + sqlFilename))
{
LandblockInstanceWriter.CreateSQLDELETEStatement(landblockInstances, sqlFile);
sqlFile.WriteLine();

sqlFile.Close();
LandblockInstanceWriter.CreateSQLINSERTStatement(landblockInstances, sqlFile);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -822,14 +820,13 @@ public static string json2sql_quest(Session session, string folder, string json_
if (quest.LastModified == DateTime.MinValue)
quest.LastModified = DateTime.UtcNow;

var sqlFile = new StreamWriter(sqlFolder + sqlFilename);

QuestSQLWriter.CreateSQLDELETEStatement(quest, sqlFile);
sqlFile.WriteLine();

QuestSQLWriter.CreateSQLINSERTStatement(quest, sqlFile);
using (StreamWriter sqlFile = new StreamWriter(sqlFolder + sqlFilename))
{
QuestSQLWriter.CreateSQLDELETEStatement(quest, sqlFile);
sqlFile.WriteLine();

sqlFile.Close();
QuestSQLWriter.CreateSQLINSERTStatement(quest, sqlFile);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -2217,20 +2214,19 @@ public static void ExportSQLRecipe(Session session, string param)

try
{
var sqlFile = new StreamWriter(sql_folder + sql_filename);

RecipeSQLWriter.CreateSQLDELETEStatement(recipe, sqlFile);
sqlFile.WriteLine();

RecipeSQLWriter.CreateSQLINSERTStatement(recipe, sqlFile);
sqlFile.WriteLine();
using (StreamWriter sqlFile = new StreamWriter(sql_folder + sql_filename))
{
RecipeSQLWriter.CreateSQLDELETEStatement(recipe, sqlFile);
sqlFile.WriteLine();

CookBookSQLWriter.CreateSQLDELETEStatement(cookbooks, sqlFile);
sqlFile.WriteLine();
RecipeSQLWriter.CreateSQLINSERTStatement(recipe, sqlFile);
sqlFile.WriteLine();

CookBookSQLWriter.CreateSQLINSERTStatement(cookbooks, sqlFile);
CookBookSQLWriter.CreateSQLDELETEStatement(cookbooks, sqlFile);
sqlFile.WriteLine();

sqlFile.Close();
CookBookSQLWriter.CreateSQLINSERTStatement(cookbooks, sqlFile);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -2278,14 +2274,25 @@ public static void ExportSQLLandblock(Session session, string param)
LandblockInstanceWriter.WeenieNames = DatabaseManager.World.GetAllWeenieNames();
}

var sqlFile = new StreamWriter(sql_folder + sql_filename);

LandblockInstanceWriter.CreateSQLDELETEStatement(instances, sqlFile);
sqlFile.WriteLine();

LandblockInstanceWriter.CreateSQLINSERTStatement(instances, sqlFile);
using (StreamWriter sqlFile = new StreamWriter(sql_folder + sql_filename))
{
// Check if the Landblock is empty
if(instances.Count > 0)
LandblockInstanceWriter.CreateSQLDELETEStatement(instances, sqlFile);
else
{
// We'll just create a dummy list with a fake instance in our landblock so we don't anger CreateSQLDeleteStatement()
CommandHandlerHelper.WriteOutputInfo(session, $"Landblock {landblockId:X4} is empty.");
List<LandblockInstance> dummyList = new List<LandblockInstance> ();
LandblockInstance dummyInstance = new LandblockInstance();
dummyInstance.ObjCellId = (uint)(landblockId << 16);
dummyList.Add(dummyInstance);
LandblockInstanceWriter.CreateSQLDELETEStatement(dummyList, sqlFile);
}
sqlFile.WriteLine();

sqlFile.Close();
LandblockInstanceWriter.CreateSQLINSERTStatement(instances, sqlFile);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -2325,14 +2332,14 @@ public static void ExportSQLQuest(Session session, string questName)

try
{
var sqlFile = new StreamWriter(sql_folder + sql_filename);

QuestSQLWriter.CreateSQLDELETEStatement(quest, sqlFile);
sqlFile.WriteLine();
using (StreamWriter sqlFile = new StreamWriter(sql_folder + sql_filename))
{

QuestSQLWriter.CreateSQLINSERTStatement(quest, sqlFile);
QuestSQLWriter.CreateSQLDELETEStatement(quest, sqlFile);
sqlFile.WriteLine();

sqlFile.Close();
QuestSQLWriter.CreateSQLINSERTStatement(quest, sqlFile);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -2379,14 +2386,13 @@ public static void ExportSQLSpell(Session session, string param)

try
{
var sqlFile = new StreamWriter(sql_folder + sql_filename);

SpellSQLWriter.CreateSQLDELETEStatement(spell, sqlFile);
sqlFile.WriteLine();

SpellSQLWriter.CreateSQLINSERTStatement(spell, sqlFile);
using (StreamWriter sqlFile = new StreamWriter(sql_folder + sql_filename))
{
SpellSQLWriter.CreateSQLDELETEStatement(spell, sqlFile);
sqlFile.WriteLine();

sqlFile.Close();
SpellSQLWriter.CreateSQLINSERTStatement(spell, sqlFile);
}
}
catch (Exception e)
{
Expand Down

0 comments on commit f108bb3

Please sign in to comment.