Skip to content

Commit

Permalink
Merge pull request #262 from fosspill/defragmentation
Browse files Browse the repository at this point in the history
Move defragmentation to its own function
  • Loading branch information
fosspill authored Aug 9, 2021
2 parents 532e832 + 6378f18 commit 5752034
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
9 changes: 7 additions & 2 deletions FFXIV_Modding_Tool/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public void SetupDicts()
{"setup", new Dictionary<string, Action>{
{"", new Action(() => { setup.ExecuteSetup(); })}
}},
{"defragment", new Dictionary<string, Action>{
{"", new Action(() => { main.ReclaimSpace(); })}
}},
};
actionAliases = new Dictionary<string, string>{
{"mpi", "modpack import"},
Expand All @@ -93,7 +96,8 @@ public void SetupDicts()
{"pc", "problemcheck"},
{"v", "version"},
{"h", "help"},
{"s", "setup"}
{"s", "setup"},
{"d", "defragment"}
};
argumentsDict = new Dictionary<List<string>, Action<string>>{
{new List<string>{"-g", "--gamedirectory"}, new Action<string>((extraArg) => { MainClass._gameDirectory = new DirectoryInfo(Path.Combine(extraArg, "game"));
Expand Down Expand Up @@ -189,7 +193,7 @@ void ProcessArguments(string[] args)

public bool ActionRequirementsChecker(string requestedAction)
{
List<string> requiresGameDirectory = new List<string> { "modpack import", "modpack create", "mods refresh", "mods enable", "mods disable", "backup", "reset", "problemcheck" };
List<string> requiresGameDirectory = new List<string> { "modpack import", "modpack create", "mods refresh", "mods enable", "mods disable", "backup", "reset", "problemcheck", "defragment" };
List<string> requiresBackupDirectory = new List<string> { "modpack import", "mods refresh", "mods enable", "mods disable", "backup", "reset", "problemcheck" };
List<string> requiresConfigDirectory = new List<string> { "modpack import", "problemcheck" };
List<string> requiresUpdatedBackups = new List<string> { "modpack import", "mods refresh", "mods enable", "mods disable", "reset" };
Expand Down Expand Up @@ -311,6 +315,7 @@ public void SendHelpText()
version, v Display current application and game version
help, h Display this text
setup, s Run First-time Setup Wizard
defragment, d Defragments DAT files, attempting to reclaim unused file space
Available arguments:
-g, --gamedirectory Full path to game install, including 'FINAL FANTASY XIV - A Realm Reborn'
Expand Down
66 changes: 33 additions & 33 deletions FFXIV_Modding_Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,45 +1206,12 @@ List<string> CheckMods()
problemsFound.Add($"{fileName} has an unknown file type ({fileType}), offset is most likely corrupt");
}
Console.Write("\n");
Validators validation = new Validators();
if (validation.PromptContinuation("Would you like to defragment your DAT files in an attempt to reclaim some space?", true))
{
PrintMessage("Defragmenting...");
try
{
long savedBytes = 0;
Progress<(int Count, int Total, string Message)> reporter = new Progress<(int Count, int Total, string Message)>(ReportProgress);
var modding = new Modding(_gameDirectory);
var defragmentation = modding.DefragmentModdedDats(reporter);
defragmentation.Wait();
savedBytes = defragmentation.Result;
var savedSpace = FormatBytes(savedBytes);
PrintMessage($"\nDAT file defragmentation completed successfully. {savedSpace} of unused space has been recovered.", 1);
}
catch (Exception ex)
{
problemsFound.Add($"An error occurred during the defragmentation process: {ex.Message}");
}
}
}
else
PrintMessage("No entries found in the modlist, skipping", 3);
return problemsFound;
}

static string FormatBytes(long bytes)
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}

return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
}

Dictionary<string, bool> CheckLoD()
{
Dictionary<string, bool> problemsFound = new Dictionary<string, bool>();
Expand Down Expand Up @@ -1330,6 +1297,39 @@ Dictionary<string, bool> CheckLoD()
}
#endregion

public void ReclaimSpace()
{
PrintMessage("Defragmenting...");
try
{
long savedBytes = 0;
Progress<(int Count, int Total, string Message)> reporter = new Progress<(int Count, int Total, string Message)>(ReportProgress);
var modding = new Modding(_gameDirectory);
var defragmentation = modding.DefragmentModdedDats(reporter);
defragmentation.Wait();
savedBytes = defragmentation.Result;
var savedSpace = FormatBytes(savedBytes);
PrintMessage($"\nDAT file defragmentation completed successfully. {savedSpace} of unused space has been recovered.", 1);
}
catch (Exception ex)
{
PrintMessage($"An error occurred during the defragmentation process: {ex.Message}", 2);
}
}

static string FormatBytes(long bytes)
{
string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
int i;
double dblSByte = bytes;
for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}

return String.Format("{0:0.##}{1}", dblSByte, Suffix[i]);
}

public void ToggleModStates(bool enable)
{
string modstate = "";
Expand Down

0 comments on commit 5752034

Please sign in to comment.