Skip to content

Commit

Permalink
add encounter import/export commands for content devs (#4252)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmriggs authored Dec 9, 2024
1 parent 3f9e59e commit edcc7cd
Showing 1 changed file with 112 additions and 2 deletions.
114 changes: 112 additions & 2 deletions Source/ACE.Server/Command/Handlers/DeveloperContentCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static FileType GetContentType(string[] parameters, ref string param)

if (fileType.StartsWith("landblock"))
return FileType.LandblockInstance;
else if (fileType.StartsWith("encounter"))
return FileType.Encounter;
else if (fileType.StartsWith("quest"))
return FileType.Quest;
else if (fileType.StartsWith("recipe"))
Expand Down Expand Up @@ -220,7 +222,7 @@ public static void HandleImportSQLFolders(Session session, params string[] param
ImportSQLWeenie(session, param, true);
}

[CommandHandler("import-sql", AccessLevel.Developer, CommandHandlerFlag.None, 1, "Imports sql data from the Content folder", "<type> <wcid>\n<type> - landblock, quest, recipe, spell, weenie (default if not specified)\n<wcid> - filename prefix to search for. can be 'all' to import all files for this content type")]
[CommandHandler("import-sql", AccessLevel.Developer, CommandHandlerFlag.None, 1, "Imports sql data from the Content folder", "<type> <wcid>\n<type> - landblock, encounter, quest, recipe, spell, weenie (default if not specified)\n<wcid> - filename prefix to search for. can be 'all' to import all files for this content type")]
public static void HandleImportSQL(Session session, params string[] parameters)
{
var param = parameters[0];
Expand All @@ -244,6 +246,10 @@ public static void HandleImportSQL(Session session, params string[] parameters)
ImportSQLLandblock(session, param);
break;

case FileType.Encounter:
ImportSQLEncounter(session, param);
break;

case FileType.Quest:
ImportSQLQuest(session, param);
break;
Expand Down Expand Up @@ -354,6 +360,34 @@ public static void ImportSQLLandblock(Session session, string landblockId)
ImportSQLLandblock(session, sql_folder, file.Name);
}

public static void ImportSQLEncounter(Session session, string landblockId)
{
DirectoryInfo di = VerifyContentFolder(session);
if (!di.Exists) return;

var sep = Path.DirectorySeparatorChar;

var sql_folder = $"{di.FullName}{sep}sql{sep}encounters{sep}";

var prefix = landblockId;

if (landblockId.Equals("all", StringComparison.OrdinalIgnoreCase))
prefix = "";

di = new DirectoryInfo(sql_folder);

var files = di.Exists ? di.GetFiles($"{prefix}*.sql") : null;

if (files == null || files.Length == 0)
{
CommandHandlerHelper.WriteOutputInfo(session, $"Couldn't find {sql_folder}{prefix}*.sql");
return;
}

foreach (var file in files)
ImportSQLEncounter(session, sql_folder, file.Name);
}

public static void ImportSQLQuest(Session session, string questName)
{
DirectoryInfo di = VerifyContentFolder(session);
Expand Down Expand Up @@ -919,6 +953,22 @@ private static void ImportSQLLandblock(Session session, string sql_folder, strin
sql2json_landblock(session, instances, sql_folder, sql_file);
}

private static void ImportSQLEncounter(Session session, string sql_folder, string sql_file)
{
if (!ushort.TryParse(Regex.Match(sql_file, @"[0-9A-F]{4}", RegexOptions.IgnoreCase).Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var landblockId))
{
CommandHandlerHelper.WriteOutputInfo(session, $"Couldn't find landblock id from {sql_file}");
return;
}

// import sql to db
ImportSQL(sql_folder + sql_file);
CommandHandlerHelper.WriteOutputInfo(session, $"Imported {sql_file}");

// clear any cached encounters for this landblock
DatabaseManager.World.ClearCachedEncountersByLandblock(landblockId);
}

private static void ImportSQLQuest(Session session, string sql_folder, string sql_file)
{
// import sql to db
Expand Down Expand Up @@ -2041,7 +2091,7 @@ public static void HandleExportSqlFolder(Session session, params string[] parame
ExportSQLWeenie(session, param, true);
}

[CommandHandler("export-sql", AccessLevel.Developer, CommandHandlerFlag.None, 1, "Exports content from database to SQL file", "<optional type> <id>\n<optional type> - landblock, quest, recipe, spell, weenie (default if not specified)\n<id> - wcid or content id to export")]
[CommandHandler("export-sql", AccessLevel.Developer, CommandHandlerFlag.None, 1, "Exports content from database to SQL file", "<optional type> <id>\n<optional type> - landblock, encounter, quest, recipe, spell, weenie (default if not specified)\n<id> - wcid or content id to export")]
public static void HandleExportSql(Session session, params string[] parameters)
{
var param = parameters[0];
Expand All @@ -2063,6 +2113,10 @@ public static void HandleExportSql(Session session, params string[] parameters)
ExportSQLLandblock(session, param);
break;

case FileType.Encounter:
ExportSQLEncounter(session, param);
break;

case FileType.Quest:
ExportSQLQuest(session, param);
break;
Expand Down Expand Up @@ -2304,6 +2358,62 @@ public static void ExportSQLLandblock(Session session, string param)
CommandHandlerHelper.WriteOutputInfo(session, $"Exported {sql_folder}{sql_filename}");
}

public static void ExportSQLEncounter(Session session, string param)
{
DirectoryInfo di = VerifyContentFolder(session, false);

var sep = Path.DirectorySeparatorChar;

if (!ushort.TryParse(Regex.Match(param, @"[0-9A-F]{4}", RegexOptions.IgnoreCase).Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var landblockId))
{
CommandHandlerHelper.WriteOutputInfo(session, $"{param} not a valid landblock");
return;
}

var encounters = DatabaseManager.World.GetCachedEncountersByLandblock(landblockId);

if (encounters == null || encounters.Count == 0)
{
CommandHandlerHelper.WriteOutputInfo(session, $"Couldn't find encounters for landblock {landblockId:X4}");
return;
}

var sql_folder = $"{di.FullName}{sep}sql{sep}encounters{sep}";

di = new DirectoryInfo(sql_folder);

if (!di.Exists)
di.Create();

var sql_filename = $"{landblockId:X4}.sql";

try
{
if (LandblockEncounterWriter == null)
{
LandblockEncounterWriter = new EncounterSQLWriter();
LandblockEncounterWriter.WeenieNames = DatabaseManager.World.GetAllWeenieNames();
}

using (var sqlFile = new StreamWriter(sql_folder + sql_filename))
{
LandblockEncounterWriter.CreateSQLDELETEStatement(encounters, sqlFile);

sqlFile.WriteLine();

LandblockEncounterWriter.CreateSQLINSERTStatement(encounters, sqlFile);
}
}
catch (Exception e)
{
Console.WriteLine(e);
CommandHandlerHelper.WriteOutputInfo(session, $"Failed to export {sql_folder}{sql_filename}");
return;
}

CommandHandlerHelper.WriteOutputInfo(session, $"Exported {sql_folder}{sql_filename}");
}

public static void ExportSQLQuest(Session session, string questName)
{
DirectoryInfo di = VerifyContentFolder(session, false);
Expand Down

0 comments on commit edcc7cd

Please sign in to comment.