I am creating a software that basically modifies a .json file based on the profile username that the user entered. All of the code successfully retrieves the file location of the .json file based on the username they entered. However, no matter what I do, I cannot pass the “autosavePath” string from the first class (which holds the path) to the second class. Additionally, I have set up a log system to try track what happened and as soon as the code in the second class starts, the string remains empty whilst the string holds the correct path in the first class.
First class:
public class firstClass
{
public bool isValidProfile = false;
public bool dirsExist = false;
public string careerProfileName = "";
public string careerProfilePath = "";
public string autosavePath = "";
public async Task checkProfileAsync(MainWindow mw)
{
rearrangeSaveDirs rearrangeDirs = new rearrangeSaveDirs();
playerAttributes playerAttributes = new playerAttributes();
//loading animation
animCF anim = new animCF();
anim.canvasLoadAnim(mw);
await Task.Run(() =>
{
Thread.Sleep(2500);
});
await Task.Run(() =>
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string profilesPath = $"{appDataPath}\software\settings\cloud\saves";
if (Directory.Exists(profilesPath))
{
Debug.WriteLine($"{profilesPath} exists");
logger.log($"{profilesPath} exists");
//check if the name the user entered into the textbox is a profile in the saves folder
string profileName = string.Empty;
mw.Dispatcher.Invoke(() =>
{
profileName = mw.tbProfileName.Text.Trim();
});
if (string.IsNullOrWhiteSpace(profileName))
{
logger.log($"User entered a non-valid career profile name: {profileName}");
mw.Dispatcher.Invoke(() =>
{
MessageBox.Show($"Please enter a valid career profile name.",
"BCModZ", MessageBoxButton.OK, MessageBoxImage.Error);
});
isValidProfile = false;
logger.log("isValidProfile = false");
}
else
{
string profilePath = Path.Combine(profilesPath, profileName);
if (Directory.Exists(profilePath))
{
Debug.WriteLine($"profile {profileName} exists at {profilePath}");
logger.log($"profile {profileName} exists at {profilePath}");
isValidProfile = true;
logger.log("isValidProfile = true");
careerProfileName = $"{profileName}";
careerProfilePath = $"{profilePath}";
Debug.WriteLine($"career profile set as {careerProfileName} and path set to {careerProfilePath}");
logger.log($"career profile set as {careerProfileName} and path set to {careerProfilePath}");
//make sure that the autosave files were successfully rearranged
bool success = rearrangeDirs.rearrangeAutosaves(this);
if (success)
{
logger.log("rearrangeAutosaves function returned 'success'; setting dirsExist = true");
dirsExist = true;
}
Task.Delay(1500).Wait();
}
else
{
Debug.WriteLine($"profile {profileName} was not found at {profilePath}");
logger.log($"profile {profileName} was not found at {profilePath}");
mw.Dispatcher.Invoke(() =>
{
MessageBox.Show($"The career profile, {profileName}, does not exist. " +
$"Please double check if this is truly the name of your career profile, free from any spelling errors.",
"BCModZ", MessageBoxButton.OK, MessageBoxImage.Error);
});
isValidProfile = false;
logger.log("isValidProfile = false");
}
}
}
else
{
Debug.WriteLine($"{profilesPath} does not exist");
logger.log($"{profilesPath} does not exist");
mw.Dispatcher.Invoke(() =>
{
errorBox.sendMSB();
});
isValidProfile = false;
logger.log("isValidProfile = false");
}
});
if (isValidProfile == true && dirsExist == true)
{
autosavePath = rearrangeDirs.keptAutosaveDir;
logger.log($"autosavePath = {autosavePath}");
logger.log("isValidProfile returned true and dirsExist returned true");
await Task.Run(() =>
{
Thread.Sleep(5500);
});
playerAttributes.attributesFilePath = autosavePath;
MessageBox.Show($"{autosavePath}");
anim.canvasLoadAnimOUT(mw);
await Task.Run(() =>
{
Thread.Sleep(1500);
});
anim.canvasAnim(mw);
}
else
{
logger.log("Either isValidProfile or dirsExist didn't return true");
anim.canvasLoadAnimOUT(mw);
}
}
}
And heres the second class:
public class playerAttributes
{
//file path to the attributes file
public string attributesFilePath = "nothing";
private readonly Dictionary<string, string> optionMappings = new Dictionary<string, string>
{
};
public void modifyAttributes(string option, double value)
{
//modify the playerAttributes.json file correspondingly to what the user selected
logger.log($"attributesFilePath was set to {attributesFilePath}");
if (Directory.Exists(attributesFilePath))
{
try
{
string json = File.ReadAllText(attributesFilePath);
dynamic data = JsonConvert.DeserializeObject(json);
string jsonKey = optionMappings[option];
if (data.ContainsKey(jsonKey))
{
data[jsonKey]["value"] = value;
logger.log($"jsonKey value = {value}");
}
else
{
logger.log($"Option '{option}' not found in JSON.");
throw new KeyNotFoundException($"Option '{option}' not found in JSON.");
}
string updatedJson = JsonConvert.SerializeObject(data, Formatting.Indented);
File.WriteAllText(attributesFilePath, updatedJson);
logger.log($"Wrote {updatedJson} at {attributesFilePath}.");
MessageBox.Show($"Set the {option} to {value}");
}
catch (Exception ex)
{
MessageBox.Show($"Error modifying attributes: {ex.Message}");
logger.log($"Error modifying attributes: {ex.Message}.");
}
}
else
{
logger.log($"Directory attributesFilePath does not exist: {attributesFilePath}");
errorBox.sendMSB();
}
}
}
this is what the logs showed:
6/07/2024 3:24:48 PM: rearrangeAutosaves function returned ‘success’; setting dirsExist = true
6/07/2024 3:06:48 PM: autosavePath = C:UserszrylxAppDataLocalsoftwaresettingscloudsavesZrylxautosave3
6/07/2024 3:06:48 PM: isValidProfile returned true and dirsExist returned true
6/07/2024 3:07:00 PM: attributesFilePath was set to nothing
6/07/2024 3:07:00 PM: Directory attributesFilePath does not exist: nothing
also I am using MaterialDesign for wpf if that matters
I have been sitting here for hours researching, modifying my code, and creating more logs, however, I still have absolutely no idea why this is happening. Please help.