Skip to content

Add ability to import CSVs into DBCs from the command line #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion WDBXEditor/ConsoleHandler/ConsoleCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using WDBXEditor.Archives.CASC.Handlers;
using WDBXEditor.Archives.MPQ;
using WDBXEditor.Common;
using WDBXEditor.Reader;
using WDBXEditor.Storage;
using static WDBXEditor.Common.Constants;

Expand Down Expand Up @@ -181,7 +182,41 @@ public static void ExtractCommand(string[] args)
Console.WriteLine("");
}
#endregion


#region Import
/// <summary>
/// Imports a csv into a DBC
/// <para>-import -f "foo.dbc" -b 11802 -c "foo.csv" -h true -u replace -i FixIds</para>
/// -f name of dbc file
/// -b build number to use when loading dbc
/// -c name of csv file
/// -h sets whether csv has header row
/// -u updateMode (0:Insert|1:Update|2:Replace)
/// -i idImportMode (1:FixIds|2:TakeNewest)
/// </summary>
/// <param name="args"></param>
public static void ImportArgCommand(string[] args)
{
LoadCommand(args);

var pmap = ConsoleManager.ParseCommand(args);
var csvFileName = ParamCheck<string>(pmap, "-c");
var hasHeader = ParamCheck<bool>(pmap, "-h", false);
var updateMode = ParamCheck<UpdateMode>(pmap, "-u");
var importMode = ParamCheck<ImportFlags>(pmap, "-i");

var entry = Database.Entries[0];

if ( !entry.ImportCSV(csvFileName, hasHeader, updateMode, out var importError, importMode) )
{
var dbcFileName = ParamCheck<string>(pmap, "-f");
throw new Exception($" Error importing {csvFileName} into {dbcFileName}: {importError}");
}

new DBReader().Write(entry, entry.SavePath);
}
#endregion

#region Export
/// <summary>
/// Exports a file to either SQL, JSON or CSV
Expand Down Expand Up @@ -258,6 +293,9 @@ private static T ParamCheck<T>(Dictionary<string, string> map, string field, boo
{
try
{
if (typeof(T).IsEnum)
return (T)Enum.Parse(typeof(T), map[field], true);

return (T)Convert.ChangeType(map[field], typeof(T));
}
catch
Expand Down
1 change: 1 addition & 0 deletions WDBXEditor/ConsoleHandler/ConsoleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static void LoadCommandDefinitions()
DefineCommand("-export", ConsoleCommands.ExportArgCommand);
DefineCommand("-sqldump", ConsoleCommands.SqlDumpArgCommand);
DefineCommand("-extract", ConsoleCommands.ExtractCommand);
DefineCommand("-import", ConsoleCommands.ImportArgCommand);
}

/// <summary>
Expand Down