Skip to content

Commit

Permalink
don't overwrite existing name on reimport with empty game name. cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
cadon committed Nov 28, 2023
1 parent ee63cad commit 69d086b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 50 deletions.
16 changes: 11 additions & 5 deletions ARKBreedingStats/Form1.collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,12 @@ private void OpenRecentlyUsedFile(object sender, EventArgs e)

/// <summary>
/// Imports creature from file created by the export gun mod.
/// Returns true if the last imported creature already exists in the library.
/// Returns already existing Creature or null if it's a new creature.
/// </summary>
private bool ImportExportGunFiles(string[] filePaths, out bool creatureAdded, out Creature lastAddedCreature)
private Creature ImportExportGunFiles(string[] filePaths, out bool creatureAdded, out Creature lastAddedCreature, out bool copiedNameToClipboard)
{
creatureAdded = false;
copiedNameToClipboard = false;
var newCreatures = new List<Creature>();

var importedCounter = 0;
Expand All @@ -807,8 +808,10 @@ private bool ImportExportGunFiles(string[] filePaths, out bool creatureAdded, ou
string serverMultipliersHash = null;
bool? multipliersImportSuccessful = null;
string serverImportResult = null;
bool creatureAlreadyExists = false;
Creature alreadyExistingCreature = null;
var gameSettingBefore = _creatureCollection.Game;
Species lastSpecies = null;
Creature[] creaturesOfSpecies = null;

foreach (var filePath in filePaths)
{
Expand All @@ -818,6 +821,10 @@ private bool ImportExportGunFiles(string[] filePaths, out bool creatureAdded, ou
newCreatures.Add(c);
importedCounter++;
lastCreatureFilePath = filePath;

IsCreatureAlreadyInLibrary(c.guid, c.ArkId, out alreadyExistingCreature);
copiedNameToClipboard = SetNameOfImportedCreature(c, lastSpecies == c.Species ? creaturesOfSpecies : null, out creaturesOfSpecies, alreadyExistingCreature);
lastSpecies = c.Species;
}
else if (lastError != null)
{
Expand Down Expand Up @@ -861,7 +868,6 @@ private bool ImportExportGunFiles(string[] filePaths, out bool creatureAdded, ou
lastAddedCreature = newCreatures.LastOrDefault();
if (lastAddedCreature != null)
{
creatureAlreadyExists = IsCreatureAlreadyInLibrary(lastAddedCreature.guid, lastAddedCreature.ArkId, out _);
creatureAdded = true;
}

Expand All @@ -887,7 +893,7 @@ private bool ImportExportGunFiles(string[] filePaths, out bool creatureAdded, ou
SelectCreatureInLibrary(lastAddedCreature);
}

return creatureAlreadyExists;
return alreadyExistingCreature;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions ARKBreedingStats/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3357,15 +3357,15 @@ private void ProcessDroppedFiles(string[] files)
OpenCompressedFile(filePath, true);
return;
case ".ini" when files.Length == 1:
ExtractExportedFileInExtractor(filePath);
ExtractExportedFileInExtractor(filePath, out _);
break;
case ".ini":
ShowExportedCreatureListControl();
_exportedCreatureList.LoadFiles(files);
break;
case ".sav":
case ".json":
ImportExportGunFiles(files, out _, out _);
ImportExportGunFiles(files, out _, out _, out _);
break;
case ".asb":
case ".xml":
Expand Down
19 changes: 8 additions & 11 deletions ARKBreedingStats/Form1.extractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -867,9 +867,10 @@ private void CopyExtractionToClipboard()
/// Returns true if the creature already exists in the library.
/// Returns null if file couldn't be loaded.
/// </summary>
private bool? ExtractExportedFileInExtractor(string exportFilePath)
private bool? ExtractExportedFileInExtractor(string exportFilePath, out bool nameCopiedToClipboard)
{
CreatureValues cv = null;
nameCopiedToClipboard = false;

// if the file is blocked, try it again
const int waitingTimeBase = 200;
Expand Down Expand Up @@ -917,7 +918,7 @@ private void CopyExtractionToClipboard()
&& LoadModValuesOfCollection(_creatureCollection, true, true)
&& oldModHash != _creatureCollection.modListHash)
{
return ExtractExportedFileInExtractor(exportFilePath);
return ExtractExportedFileInExtractor(exportFilePath, out nameCopiedToClipboard);
}

return false;
Expand All @@ -926,7 +927,7 @@ private void CopyExtractionToClipboard()
tabControlMain.SelectedTab = tabPageExtractor;

bool creatureAlreadyExists = ExtractValuesInExtractor(cv, exportFilePath, true);
GenerateCreatureNameAndCopyNameToClipboardIfSet(creatureAlreadyExists);
nameCopiedToClipboard= GenerateCreatureNameAndCopyNameToClipboardIfSet(creatureAlreadyExists);

return creatureAlreadyExists;
}
Expand All @@ -935,7 +936,7 @@ private void CopyExtractionToClipboard()
/// Copies the creature name to the clipboard if the conditions according to the user settings are fulfilled.
/// </summary>
/// <param name="creatureAlreadyExists"></param>
private void GenerateCreatureNameAndCopyNameToClipboardIfSet(bool creatureAlreadyExists)
private bool GenerateCreatureNameAndCopyNameToClipboardIfSet(bool creatureAlreadyExists)
{
if (Properties.Settings.Default.applyNamePatternOnAutoImportAlways
|| (Properties.Settings.Default.applyNamePatternOnImportIfEmptyName
Expand All @@ -945,13 +946,10 @@ private void GenerateCreatureNameAndCopyNameToClipboardIfSet(bool creatureAlread
)
{
CreatureInfoInput_CreatureDataRequested(creatureInfoInputExtractor, false, false, false, 0);
if (Properties.Settings.Default.copyNameToClipboardOnImportWhenAutoNameApplied)
{
Clipboard.SetText(string.IsNullOrEmpty(creatureInfoInputExtractor.CreatureName)
? "<no name>"
: creatureInfoInputExtractor.CreatureName);
}
return CopyCreatureNameToClipboardOnImportIfSetting(creatureInfoInputExtractor.CreatureName);
}

return false;
}

/// <summary>
Expand All @@ -974,7 +972,6 @@ private void ExtractExportedFileInExtractor(importExported.ExportedCreatureContr
creatureInfoInputExtractor.CreatureOwner += _exportedCreatureList.ownerSuffix;
}


/// <summary>
/// Sets the values of a creature to the extractor and extracts its levels.
/// It returns true if the creature is already present in the library.
Expand Down
90 changes: 58 additions & 32 deletions ARKBreedingStats/Form1.importExported.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void ImportLastExportedCreature()
firstExportFolder.FolderPath = lastExportFile.DirectoryName;
exportFolders[0] = firstExportFolder.ToString();

ExtractExportedFileInExtractor(lastExportFile.FullName);
ExtractExportedFileInExtractor(lastExportFile.FullName, out _);
}
return;
}
Expand All @@ -132,7 +132,7 @@ private void ImportLastExportedCreature()
{
case ".ini":
// ini files need to be processed by the extractor
ExtractExportedFileInExtractor(newestExportFile);
ExtractExportedFileInExtractor(newestExportFile, out _);
return;
case ".sav":
case ".json":
Expand Down Expand Up @@ -195,7 +195,7 @@ private Creature ImportExportedAddIfPossible(string filePath)
switch (Path.GetExtension(filePath))
{
case ".ini":
var loadResult = ExtractExportedFileInExtractor(filePath);
var loadResult = ExtractExportedFileInExtractor(filePath, out copiedNameToClipboard);
if (loadResult == null) return null;
alreadyExists = loadResult.Value;

Expand All @@ -210,40 +210,13 @@ private Creature ImportExportedAddIfPossible(string filePath)
SetMessageLabelText($"Successful {(alreadyExists ? "updated" : "added")} {creature.name} ({species.name}) of the exported file\r\n" + filePath, MessageBoxIcon.Information, filePath);
addedToLibrary = true;
}

copiedNameToClipboard = Properties.Settings.Default.copyNameToClipboardOnImportWhenAutoNameApplied
&& (Properties.Settings.Default.applyNamePatternOnAutoImportAlways
|| Properties.Settings.Default.applyNamePatternOnImportIfEmptyName
|| (!alreadyExists && Properties.Settings.Default.applyNamePatternOnAutoImportForNewCreatures)
);
break;
case ".sav":
case ".json":
alreadyExists = ImportExportGunFiles(new[] { filePath }, out addedToLibrary, out creature);
var alreadyExistingCreature = ImportExportGunFiles(new[] { filePath }, out addedToLibrary, out creature, out copiedNameToClipboard);
alreadyExists = alreadyExistingCreature != null;
if (!addedToLibrary || creature == null) return null;
uniqueExtraction = true;

if (Properties.Settings.Default.applyNamePatternOnAutoImportAlways
|| (Properties.Settings.Default.applyNamePatternOnImportIfEmptyName
&& string.IsNullOrEmpty(creature.name))
|| (!alreadyExists
&& Properties.Settings.Default.applyNamePatternOnAutoImportForNewCreatures)
)
{
creaturesOfSpecies = _creatureCollection.creatures.Where(c => c.Species == creature.Species).ToArray();
creature.name = NamePattern.GenerateCreatureName(creature, creaturesOfSpecies,
_topLevels.TryGetValue(creature.Species, out var topLevels) ? topLevels : null,
_lowestLevels.TryGetValue(creature.Species, out var lowestLevels) ? lowestLevels : null,
_customReplacingNamingPattern, false, 0);

if (Properties.Settings.Default.copyNameToClipboardOnImportWhenAutoNameApplied)
{
Clipboard.SetText(string.IsNullOrEmpty(creature.name)
? "<no name>"
: creature.name);
copiedNameToClipboard = true;
}
}
break;
default: return null;
}
Expand Down Expand Up @@ -343,6 +316,59 @@ private Creature ImportExportedAddIfPossible(string filePath)
return addedToLibrary ? creature : null;
}

/// <summary>
/// Sets the name of an imported creature and copies it to the clipboard depending on the user settings.
/// </summary>
/// <returns>True if name was copied to clipboard</returns>
private bool SetNameOfImportedCreature(Creature creature, Creature[] creaturesOfSpeciesIn, out Creature[] creaturesOfSpecies, Creature alreadyExistingCreature)
{
creaturesOfSpecies = creaturesOfSpeciesIn;
if (ApplyNamingPattern(creature, alreadyExistingCreature))
{
// don't overwrite existing ASB creature name with empty ingame name
if (!string.IsNullOrEmpty(alreadyExistingCreature?.name) && string.IsNullOrEmpty(creature.name))
{
creature.name = alreadyExistingCreature.name;
}
else
{
if (creaturesOfSpecies == null)
creaturesOfSpecies = _creatureCollection.creatures.Where(c => c.Species == creature.Species)
.ToArray();
creature.name = NamePattern.GenerateCreatureName(creature, creaturesOfSpecies,
_topLevels.TryGetValue(creature.Species, out var topLevels) ? topLevels : null,
_lowestLevels.TryGetValue(creature.Species, out var lowestLevels) ? lowestLevels : null,
_customReplacingNamingPattern, false, 0);
}

return CopyCreatureNameToClipboardOnImportIfSetting(creature.name);
}

return false;
}

/// <summary>
/// Returns true if the naming pattern should be applied according to the settings.
/// </summary>
private bool ApplyNamingPattern(Creature creature, Creature alreadyExistingCreature) =>
Properties.Settings.Default.applyNamePatternOnAutoImportAlways
|| (Properties.Settings.Default.applyNamePatternOnImportIfEmptyName
&& string.IsNullOrEmpty(creature.name))
|| (alreadyExistingCreature == null
&& Properties.Settings.Default.applyNamePatternOnAutoImportForNewCreatures);

/// <summary>
/// Copies name to clipboard if the according setting is enabled. Returns true if copied.
/// </summary>
private bool CopyCreatureNameToClipboardOnImportIfSetting(string creatureName)
{
if (!Properties.Settings.Default.copyNameToClipboardOnImportWhenAutoNameApplied) return false;
Clipboard.SetText(string.IsNullOrEmpty(creatureName)
? "<no name>"
: creatureName);
return true;
}

/// <summary>
/// Give feedback in overlay for imported creature.
/// </summary>
Expand Down

0 comments on commit 69d086b

Please sign in to comment.