There is a program that uses a .cfg file. The file's name is "mods.cfg". Once you run the program a launcher opens that reads this file, and files listed in this .cfg file will be automatically selected in the launcher. They are also in the order in which they are listed in the .cfg file.
In this file, there is a list of file names, each on their own line. As an example, there is a file named "mod1.mod" and a file named "mod2.mod" in the files of the program. In the "mods.cfg" file, there are two lines:
mod2.mod
mod1.mod
Once the launcher is opened, these two files will be selected, and shown in this order, where "mod2.mod" will be on top of "mod1.mod". If they would swap places in the file, they would swap places as you run the launcher.
Here is an issue I ran into, which I don't understand.
I made a CMD program using Visual Studio 22. The program reads this .cfg file and stores it in a string. It takes the string apart into an array, separating the lines using "\n". It organizes them into alphabetical order, clears the file, and then writes each entry back into the file in that order, and that is it.
The result in the "mods.cfg" are as intended. All file names in the file have been organized into the correct order. Here is where the problem begins.
As I open the launcher, no files are selected. The ones in the list are not selected, nor are the ones not on the list.
- If I cut the list out of the file, then save it, then open the launcher, close the launcher, then paste it, and then open the launcher again, nothing is selected.
- If I cut everything out of the file, save it, launch it, close the launcher, type in a file I know is available, save it, and launch the program, the file I typed in is selected in the launcher.
Clearly something is missing in the file as the CMD program writes onto the file, but I cannot see what it is.
Here is my program. Notice, that if I enter the directory of the file either directly, or using the command "debug", the results are the same. I am pretty sure there is some simple solution to this that I am not familiar with at all. Thank you for any assistance you could give me. I can survive without this program, but I cannot easily live with not knowing what the difference is with two superficially identical files.
EDIT:
When I changed program to write a string directly onto the file, it works. I am testing out different ways of doing this, since the program is far from optimized or sensible when it comes to code.
In this file, there is a list of file names, each on their own line. As an example, there is a file named "mod1.mod" and a file named "mod2.mod" in the files of the program. In the "mods.cfg" file, there are two lines:
mod2.mod
mod1.mod
Once the launcher is opened, these two files will be selected, and shown in this order, where "mod2.mod" will be on top of "mod1.mod". If they would swap places in the file, they would swap places as you run the launcher.
Here is an issue I ran into, which I don't understand.
I made a CMD program using Visual Studio 22. The program reads this .cfg file and stores it in a string. It takes the string apart into an array, separating the lines using "\n". It organizes them into alphabetical order, clears the file, and then writes each entry back into the file in that order, and that is it.
The result in the "mods.cfg" are as intended. All file names in the file have been organized into the correct order. Here is where the problem begins.
As I open the launcher, no files are selected. The ones in the list are not selected, nor are the ones not on the list.
- If I cut the list out of the file, then save it, then open the launcher, close the launcher, then paste it, and then open the launcher again, nothing is selected.
- If I cut everything out of the file, save it, launch it, close the launcher, type in a file I know is available, save it, and launch the program, the file I typed in is selected in the launcher.
Clearly something is missing in the file as the CMD program writes onto the file, but I cannot see what it is.
Here is my program. Notice, that if I enter the directory of the file either directly, or using the command "debug", the results are the same. I am pretty sure there is some simple solution to this that I am not familiar with at all. Thank you for any assistance you could give me. I can survive without this program, but I cannot easily live with not knowing what the difference is with two superficially identical files.
C#:
// See [URL='https://aka.ms/new-console-template']C# console app template changes in .NET 6+ - .NET[/URL] for more information
//Console.WriteLine("Hello, World!");
//Check file exists in folder
//Read everything in file
//Separate one line at a time
//Organize in alphabetical order
//Clear original file
//Write the organized list to the file
//Give message that list is organized
//Ready - Test it out
using System;
public class Program
{
public static void Main()
{
//Variables
int lineCount;
bool finished = false;
string modFileDirectory;
string modFileContent;
string[] modFileContentArray;
string modFileContentSorted;
//Actual
while (!finished)
{
//Intro text, getting the directory, displaying it
modFileDirectory = GetModFileDirectory();
if (File.Exists(modFileDirectory))
{
//Get content from file in given directory as a string
modFileContent = GetModFileContent(modFileDirectory);
//Splice up the string into an array, separating with \n
modFileContentArray = modFileContent.Split("\n");
//Sort the array
Array.Sort(modFileContentArray);
//Sorted string
//Get length of array
lineCount = modFileContentArray.Length;
File.WriteAllText(modFileDirectory, ""); // Delete all text in the file so that you get a fresh file to write into
//For every string in array, write it down into the file
for (int i = 0; i < lineCount; i++)
{
using (StreamWriter sw = File.AppendText(modFileDirectory))
{
if (modFileContentArray[i] != "")
{
//sw.WriteLine(modFileContentArray[i]); //Write it into the file and add line afterwards
sw.Write(modFileContentArray[i]);
}
if (i == lineCount)
{
modFileContentSorted = GetModFileContent(modFileDirectory);
Console.WriteLine(modFileContentSorted);
}
}
}
Console.WriteLine("Selected mods should now be in alphabetical order.");
finished = true;
}
else if (modFileDirectory == "")
{
Console.WriteLine("Entry empty. Try again.\n\n");
}
else
{
Console.WriteLine("File does not exist. Check your directory path or try fully launching and then exiting Kenshi and try again.\n\n");
}
}
}
public static string GetModFileDirectory()
{
Console.WriteLine("Enter the directory to the Kenshi \'mods.cfg\' or the \'__mods.list\' file");
Console.WriteLine("File is found in ../Kenshi/data/__mods.list");
Console.WriteLine("Right click the \'mods.cfg\' file -> Copy as Path -> Paste it here");
Console.WriteLine("Enter or paste the file location here: ");
string txt = Console.ReadLine();
if (txt == "debug")
{
txt = "D:\\SteamLibrary\\steamapps\\common\\Kenshi\\data\\mods.cfg";
}
else if (txt == "debug2")
{
txt = "D:\\SteamLibrary\\steamapps\\common\\Kenshi\\data\\__mods.list";
}
else
{
txt = txt.Replace("\"", string.Empty); //replace " with Empty
}
Console.WriteLine($"\nDirectory selected: {txt}" + "\n"); //Display the directory
return txt;
}
public static string GetModFileContent(string modFileDirectory)
{
string txt = File.ReadAllText(modFileDirectory);
Console.WriteLine(txt);
return txt;
}
}
EDIT:
When I changed program to write a string directly onto the file, it works. I am testing out different ways of doing this, since the program is far from optimized or sensible when it comes to code.
Last edited: