C
C#β€’14mo ago
TheOneAndOnly

❔ How to make a program that can read this?

I know how to open and read files. But in this case its not as simple as reading every line. There is a structure in the file which the program has to understand. Things are belligerent aren't preset keywords but in game names. What would be a good way of being able to read this file?
No description
97 Replies
Jimmacle
Jimmacleβ€’14mo ago
Is this a defined format or some one off thing?
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Its a defined format by Paradox
Jimmacle
Jimmacleβ€’14mo ago
Do they provide a serialization library for it or at least a spec?
Pobiega
Pobiegaβ€’14mo ago
You could probably write a parser fairly easily. There is a nuget called pidgin that helps a lot
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Not that Im aware off
Pobiega
Pobiegaβ€’14mo ago
I used it to write a bencode parser a few days ago
JakenVeina
JakenVeinaβ€’14mo ago
definitely look at pidgin and do some research into "syntax parsing"
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Alright will do Thanks
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
So I tried installing the package but than I got this?
No description
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
I installed packages before and no error was given
Jimmacle
Jimmacleβ€’14mo ago
.net framework FeelsBadMan
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
🧐
Jimmacle
Jimmacleβ€’14mo ago
looks like you'll have to use an older version of it that supports that framework or stop using .NET Framework if you have a choice
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Ah so the package doesnt support it I see
Jimmacle
Jimmacleβ€’14mo ago
the latest version doesn't looks like 2.5.0 is the last one that targets anything below .NET 5
Jimmacle
Jimmacleβ€’14mo ago
Pidgin 2.5.0
A lightweight, fast, and flexible parsing library for C#, developed at Stack Overflow.
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
I see, thanks for checking it out for me Time to make my own parsing library xD
Jimmacle
Jimmacleβ€’14mo ago
or just use that older version if it still does the job i'm assuming you're stuck on .netfx because this is some kind of mod if not then definitely use a newer version of .NET
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Yeah that is probatly the more sane idea
Jimmacle
Jimmacleβ€’14mo ago
yeah the only reason to still be using .NET Framework is if you have some external requirement that forces you to
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Well, it is a Windows Forms App
Pobiega
Pobiegaβ€’14mo ago
Winforms works fine with .net 7 Stop using framework if you can
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
I cant seem to select the new framework for my project even though the installer says I do have it installed
JakenVeina
JakenVeinaβ€’14mo ago
you can't, you'll have to rebuild it from scratch
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Makes sense Still hate it
JakenVeina
JakenVeinaβ€’14mo ago
or, more usefully, create a brand new WinForms project in .NET 5+, to see what it looks like, and manually change over your existing .csproj or even MORE usefully, also stop using WinForms
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Haha and than use what?
JakenVeina
JakenVeinaβ€’14mo ago
WPF, UWP, MAUI, etc. I think there's a tag for this... $gui hang on
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Im hanging on
JakenVeina
JakenVeinaβ€’14mo ago
$guiframeworks
MODiX
MODiXβ€’14mo ago
Windows Presentation Foundation (WPF): + Has a lot of resources online due to how old it is. + There are a lot of UI libraries for it - No longer maintained, as in, didn't receive any major updates since a long time. Universal Windows Platform (UWP): + Fast, and has a small binary size. + Has a nice UI out of the box (And also there's WinUI 2.7 to achieve Windows 11's looks and feel). - Is packaged and requires a developer license. Avalonia: + Cross-platform and is under active development. - Relatively new, so there are not a lot of online resources for it. Note: All of the mentioned GUI frameworks are XAML-based.
JakenVeina
JakenVeinaβ€’14mo ago
hmmm, the one I was thinking of has even more options
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Which is?
JakenVeina
JakenVeinaβ€’14mo ago
there's Xamarin.Forms, which is geared towards cross-platform mobile apps there's MAUI which also geared towards cross-platform mobile, but with Microsoft leanings there's WinUI which is the most "modern" Windows platform, like if you want to deploy to the Windows Store if all you care about is running on the Windows Desktop, go with WPF or Avalonia and protip: if you're gonna do this, you definitely SHOULD learn about the MVVM pattern, and XAML, but you 100% do not have to. You can still write traditional imperative UI apps if you want a lower barrier to entry
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Hhhmm okay Will have to look into that than
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
So I tried reading the file byte for byte but wtf are those \0
No description
Jimmacle
Jimmacleβ€’14mo ago
that looks like you read each line into an array much longer than the actual text \0 is a null character, which is the same as a byte with a value of 0 since it's text based you should be using text-oriented reading APIs
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
I run a foreach on the line so that shouldn't be possible
Jimmacle
Jimmacleβ€’14mo ago
i'd have to see code
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
C++
var list = new List<Ideology>();
var files = Directory.GetFiles(folderLocation);
byte[] tempBytes = new byte[999];
List<string> words = new List<string>();
foreach (var file in files)
{
var linesRead = File.ReadLines(file);

foreach (var lineRead in linesRead)
{
int counter = 0;
tempBytes = new byte[999];
foreach (byte c in lineRead){
if (c > 32 && c < 127)
{
tempBytes[counter] = c;
counter++;
}
}
string tempWord = Encoding.ASCII.GetString(tempBytes);
words.Add(tempWord);
if (tempWord == "ideologies = {")
{
Console.WriteLine(Encoding.ASCII.GetString(tempBytes));
}

}
}
return list;
C++
var list = new List<Ideology>();
var files = Directory.GetFiles(folderLocation);
byte[] tempBytes = new byte[999];
List<string> words = new List<string>();
foreach (var file in files)
{
var linesRead = File.ReadLines(file);

foreach (var lineRead in linesRead)
{
int counter = 0;
tempBytes = new byte[999];
foreach (byte c in lineRead){
if (c > 32 && c < 127)
{
tempBytes[counter] = c;
counter++;
}
}
string tempWord = Encoding.ASCII.GetString(tempBytes);
words.Add(tempWord);
if (tempWord == "ideologies = {")
{
Console.WriteLine(Encoding.ASCII.GetString(tempBytes));
}

}
}
return list;
Jimmacle
Jimmacleβ€’14mo ago
tempBytes = new byte[999]; string tempWord = Encoding.ASCII.GetString(tempBytes); you are converting the entire array into a string when you only want part of it
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Oooo Ofcourse Stupid me haha I preset the length of the array so he takes the whole array Thanks
Jimmacle
Jimmacleβ€’14mo ago
what's the goal of this code? it seems like you're stripping out non alphanumeric (or printable?) characters but there are significantly easier ways to do that
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Well my goal hasnt changed since the beginning of this post
Jimmacle
Jimmacleβ€’14mo ago
i know what your overall goal is but what's this specific code doing
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Its all for the game hearts of iron 4. The modding there is done by scripting using a langue created by the developers of the game. I though it would be a fun hobby project to try making a interface for modding it.
Jimmacle
Jimmacleβ€’14mo ago
that's the opposite direction of specificity i was going for lol the code you posted, just describe the things it's doing
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
While within the game countries have there ideologies. The diffrent kind of possible ideologies are defined in that file.
Jimmacle
Jimmacleβ€’14mo ago
that isn't describing what that code is doing
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
What do you want from me haha
Jimmacle
Jimmacleβ€’14mo ago
just go through the code and explain what each part does because i'm trying to help you avoid writing something overcomplicated
Pobiega
Pobiegaβ€’14mo ago
You do ReadLines, then treat each line as a byte array when it's a string. Why?
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
first line is useless no clue why its needed. than we have the name for a ideology (belligerent). than its defining three diffrent subideologies and setting if it can be randomly selected by a country than its defining three possible faction names for countrie with those ideology than the color for that ideology is set than same more variables. just a lot of setting I better things to do than writing this you get the point haha Just trying ways to read a file to find out what works
Pobiega
Pobiegaβ€’14mo ago
Dude, we are not asking about the files content..we are asking about why your code is really weird
Jimmacle
Jimmacleβ€’14mo ago
yes, the structure of the file you're trying to read is clear your code is not for example, why are you doing
foreach (byte c in lineRead){
if (c > 32 && c < 127)
{
tempBytes[counter] = c;
counter++;
}
}
foreach (byte c in lineRead){
if (c > 32 && c < 127)
{
tempBytes[counter] = c;
counter++;
}
}
that seems like an overcomplicated way to remove certain characters from the text
Pobiega
Pobiegaβ€’14mo ago
Why is there a byte array at all? It's already a string, why are you doing encoding on your own etc
Jimmacle
Jimmacleβ€’14mo ago
frankly i wouldn't read it as lines anyway, the file structure implies it is not sensitive to line breaks, only whitespace for example, color looks like its value is an array of numbers but the whole thing is on one line
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Well how would I be able to get the word "belligerent" from the file if I dont read it byte for byte to find out when the word started and stopped? Other things are predefined and could be looked at with string.Contains
Jimmacle
Jimmacleβ€’14mo ago
text.IndexOf("belligerent")
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
But it has to be dynamic
Jimmacle
Jimmacleβ€’14mo ago
what isn't dynamic about that?
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
"belligerent"
Jimmacle
Jimmacleβ€’14mo ago
so don't use a string literal use a variable that holds the text you want to search for
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
But I dont now what I want to search for at the start That is defined in the file That is when the name for the ideology is defined
Jimmacle
Jimmacleβ€’14mo ago
also, reading text byte for byte is actually incorrectly assuming its encoding fwiw
Pobiega
Pobiegaβ€’14mo ago
Yuup You're actually reading the ascii values
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Ah okay My bad
Jimmacle
Jimmacleβ€’14mo ago
i mean, it looks like it'll work here but it may not later and then you'll run into problems it's more of a coincidence that the file is only using ASCII characters
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
All the files only use ASCII characters as far as Im aware off The files relevant for this project
Jimmacle
Jimmacleβ€’14mo ago
is there a reason you aren't pursuing the parser library pobiega suggested? seems like a good fit since this format is practically "json but different"
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Challenges are fun
Jimmacle
Jimmacleβ€’14mo ago
i mean yeah, if your goal is to learn to write a parser go for it
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
And since my project is outdated I would have to convert that which sucks
Jimmacle
Jimmacleβ€’14mo ago
you should do that anyway
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
True true But here is my second argument
Jimmacle
Jimmacleβ€’14mo ago
you could even use this which i've had decent luck with updating legacy projects https://dotnet.microsoft.com/en-us/platform/upgrade-assistant
Microsoft
Upgrade Assistant | .NET
Upgrade your projects from .NET Framework to .NET.
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Im to stubbornly I like to write my own code
Jimmacle
Jimmacleβ€’14mo ago
you'll need to get over that if you want to be productive in an environment like a job
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
But maybe, just maybe it is better to just do that
Jimmacle
Jimmacleβ€’14mo ago
for learning sure it's fine
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
I know, it is that this is a hobby project
Jimmacle
Jimmacleβ€’14mo ago
but if you want to learn effectively, don't restrict yourself to outdated tools for no reason
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Fair enough Well, something to add to my tasks for tomorrow Thanks again for your help. I'm going to bed
Jimmacle
Jimmacleβ€’14mo ago
<a:PU_PepeWave:775091969311244289>
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Cant seem to select it though? Am I missing something here?
No description
Pobiega
Pobiegaβ€’14mo ago
As you've already been told, you need to re-create your project there was a hard break between .NET Framework 4.8 and .NET 5 (technically earlier than that, but w/e) you cant just set a higher framework version, everything has changed. new SDK, new base class library, new runtime..
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
That was in the project create screen
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
No description
Pobiega
Pobiegaβ€’14mo ago
you picked the wrong project type go back $newproject
MODiX
MODiXβ€’14mo ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
Pobiega
Pobiegaβ€’14mo ago
avoid the NOs, pick a green one if you want winforms, thats the one that says "yes, but prefer WPF"
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
What is the reason to avoid those? oo wait read the text I feel like a idiot but thanksπŸ˜…
Pobiega
Pobiegaβ€’14mo ago
.NET 4.8 came out in 2017. .NET 7 was last november, and .net 8 will be out this november .net framework is "dead" .NET is the modern, living version
TheOneAndOnly
TheOneAndOnlyOPβ€’14mo ago
Yeah I understand
Pobiega
Pobiegaβ€’14mo ago
fantastic
Accord
Accordβ€’14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server