Matt
Matt
CC#
Created by Matt on 12/8/2023 in #help
WPF Stack panel visibility within datagrid
I have a datagrid with a templatecolumn containing a stack panel of buttons. My goal is to have only the selected row's buttons show up but despite numerous attempts, I can't figure out why my current xaml doesn't work. I have checked with snoop and the visibility of the stack panel is NOT being changed. Changing it manually does cause the buttons to show up so something is wrong in my xaml for sure.
3 replies
CC#
Created by Matt on 4/23/2023 in #help
❔ Array.IndexOf being weird
I'm very confused. Been trying to use Array.IndexOf to find a null terminator in a byte array
int endOfString = Array.IndexOf(Data, 0x0, EntryNameTableOffset + entry.NameTableOffset);
int endOfString = Array.IndexOf(Data, 0x0, EntryNameTableOffset + entry.NameTableOffset);
But it fails to find the byte and returns -1 Doing this however, it finds the byte... O_O Why does Array.IndexOf not work in this situation?
for (int x = EntryNameTableOffset + entry.NameTableOffset; x < Data.Length; x++)
{
if (Data[x] == 0x0)
{
endOfString = x;
break;
}
}
for (int x = EntryNameTableOffset + entry.NameTableOffset; x < Data.Length; x++)
{
if (Data[x] == 0x0)
{
endOfString = x;
break;
}
}
12 replies
CC#
Created by Matt on 4/11/2023 in #help
❔ MediaPlayer in WPF occasionally fails
public static void PlaySound(string path)
{
Task.Run(() =>
{
MediaPlayer player = new();
Task.Delay(50).Wait();
Uri uri = new(path);
player.Open(uri);
player.Volume = 0.1;
player.Play();
Task.Delay((int)player.NaturalDuration.TimeSpan.TotalMilliseconds).Wait();
player.Stop();
player.Close();
});
}
public static void PlaySound(string path)
{
Task.Run(() =>
{
MediaPlayer player = new();
Task.Delay(50).Wait();
Uri uri = new(path);
player.Open(uri);
player.Volume = 0.1;
player.Play();
Task.Delay((int)player.NaturalDuration.TimeSpan.TotalMilliseconds).Wait();
player.Stop();
player.Close();
});
}
I'm using this fire-forget pattern to play a sound through the media player. Sometimes the sound simply refuses to play. After the app has been open for long enough, all sound completely refuses to play no matter what. Any idea why?
2 replies
CC#
Created by Matt on 3/27/2023 in #help
✅ Exception Handling for NRE without modifying every call.
I have a ReadData function which reads the memory of a process. If the process exits or crashes during the read, an exception is thrown and control is handed to the catch block. However, the calling function (the function that calls ReadData) can sometimes throw an NRE if the ReadData was aborted and returned null instead of a byte[]. How can I avoid the NREs without needing to change every single call of ReadData? Ideally I would abort the calling function as if it had never been called but I know that would require a large scale rewrite. I have toyed with the idea of modifying the ReadData signature to return a tuple of type (byte[], bool) and changing the behaviour of the calling function depending on the success of the ReadData (bool) but this would also require custom behaviour for every call. There are about 20 calls and I figure adding custom behaviour or exception handling for all of them is inefficient and bad practice. Any solutions?
37 replies
CC#
Created by Matt on 3/16/2023 in #help
❔ WPF Observable Collection
Hallo, working on a window which displays a console pretty much. Made the console using an observable collection of strings and using a write method to add a string to the collection. For some reason, the collection refuses to display anything in the GUI. Any idea why this might be? (Very beginnery when it comes to wpf as I am not the person doing the front end but we're both stumped)
5 replies
CC#
Created by Matt on 2/11/2023 in #help
❔ What is the difference between these two pieces of code?
static PlayerHandler HPlayer => Program.HPlayer;
static PlayerHandler HPlayer => Program.HPlayer;
static PlayerHandler HPlayer = Program.HPlayer;
static PlayerHandler HPlayer = Program.HPlayer;
In essence I'm asking what the purpose of the lambda is here? I've used this pattern to help with a loose singleton pattern for a long time but I have no idea what this is even doing or why I use => over =
13 replies
CC#
Created by Matt on 2/11/2023 in #help
❔ How to shorten switch statement
public void SwitchAvailability(string koalaName)
{
switch(koalaName)
{
case "Boonie":
BoonieAvailable = !BoonieAvailable;
break;
case "Mim":
MimAvailable = !MimAvailable;
break;
case "Snugs":
SnugsAvailable = !SnugsAvailable;
break;
case "Gummy":
GummyAvailable = !GummyAvailable;
break;
case "Dubbo":
DubboAvailable = !DubboAvailable;
break;
case "Elizabeth":
ElizabethAvailable = !ElizabethAvailable;
break;
case "Kiki":
KikiAvailable = !KikiAvailable;
break;
case "Katie":
KatieAvailable = !KatieAvailable;
break;
};
}
public void SwitchAvailability(string koalaName)
{
switch(koalaName)
{
case "Boonie":
BoonieAvailable = !BoonieAvailable;
break;
case "Mim":
MimAvailable = !MimAvailable;
break;
case "Snugs":
SnugsAvailable = !SnugsAvailable;
break;
case "Gummy":
GummyAvailable = !GummyAvailable;
break;
case "Dubbo":
DubboAvailable = !DubboAvailable;
break;
case "Elizabeth":
ElizabethAvailable = !ElizabethAvailable;
break;
case "Kiki":
KikiAvailable = !KikiAvailable;
break;
case "Katie":
KatieAvailable = !KatieAvailable;
break;
};
}
I feel like a noob again. I can't think of how to remove the switch statement. I think I need either the ref keyword or reflections but not sure which and not sure what to do
14 replies
CC#
Created by Matt on 1/25/2023 in #help
❔ Check if list contains object with a specific property/variable value
I have a list of Players where Player is a simple object with a Name and ID. I need to check to see if a player with a specific ID is on the player list. I know that I could use a dictionary to do this but if there is a way to keep the list, that would be ideal. Here's a rough outline of what I'm trying to achieve
class Player
{
public string Name;
public ushort Id;

public Player(string name, ushort id)
{
Name = name;
Id = id;
}
}

class PlayerHandler
{
public static List<Player> Players = new();

public static void GetPlayer(ushort id)
{
if(Players.Contains()) return Player
}
}
class Player
{
public string Name;
public ushort Id;

public Player(string name, ushort id)
{
Name = name;
Id = id;
}
}

class PlayerHandler
{
public static List<Player> Players = new();

public static void GetPlayer(ushort id)
{
if(Players.Contains()) return Player
}
}
10 replies
CC#
Created by Matt on 11/29/2022 in #help
❔ Help me fix my byte-bit conversion function
So I'll first explain my problem, then the solution I've implemented and how it appears to be failing. I am trying to save an object to a byte array where each item is stored as a bit. So I have a byte array of 38 bytes. This means I have roughly 300 bits. (actually 304 but we won't use half of a byte) Now let's say I have the input that I want to save item number 3. In this case, item number 3's bit will obviously be in the first byte and all I need to do is find the value to add to the byte which essentially does the same as flipping one of the bits from 0 to 1. So if our bit representation of byte 1 is 00000000 and we want to save item number 3, our bit array should end up looking like 00000100 And this ends up meaning we have to add 4 to the byte. If we need to save item number 10, it'll be in the second byte or byte index 1. So there's a really neat way of making all of this work. The quotient on dividing the index of the item to be saved by 8 gives us the byte we want to write to (actually we need to put the quotient into the floor function first) Then the remainder can actually tell us the position of the bit to write to. So if the remainder is 0, we add 128 to the value of the byte and if it isn't, we add Math.Pow(2, rem-1) I am pretty sure my math is correct here, but for some reason, the amounts being written to the bytes and which bytes they're being written to is wrong.
11 replies
CC#
Created by Matt on 11/20/2022 in #help
❔ Better way of handling this?
switch (syncMessage.type)
{
case "Obj1":
Obj1.HandleUpdate(syncMessage.data);
break;
case "Obj2":
Obj2.HandleUpdate(syncMessage.data);
break;
case "Obj2":
Obj2.HandleUpdate(syncMessage.data);
break;
default:
break;
}
switch (syncMessage.type)
{
case "Obj1":
Obj1.HandleUpdate(syncMessage.data);
break;
case "Obj2":
Obj2.HandleUpdate(syncMessage.data);
break;
case "Obj2":
Obj2.HandleUpdate(syncMessage.data);
break;
default:
break;
}
Is there a better way of handling this? The objects all inherit from the same base abstract class which contains the HandleUpdate method. The syncMessage.type is a string and I don't think I can change that (not 100% sure) It feels like the strategy pattern might be what I'm after but this implementation already uses 3 classes per object and I'd rather not add more. Any ideas? Edit: Cleaned code up to remove information irrelevant to the question
5 replies
CC#
Created by Matt on 11/17/2022 in #help
❔ alternative to switch statement or if statements?
is there something better I can use in this situation?
int size = 300;
switch (HLevel.CurrentLevelId)
{
case 0:
address = HOpal.address1;
size = 25;
break;
case 10:
address = HOpal.address2;
break;
default:
address = HOpal.address3;
}
int size = 300;
switch (HLevel.CurrentLevelId)
{
case 0:
address = HOpal.address1;
size = 25;
break;
case 10:
address = HOpal.address2;
break;
default:
address = HOpal.address3;
}
29 replies
CC#
Created by Matt on 11/16/2022 in #help
❔ Messy Build Directory
43 replies
CC#
Created by Matt on 11/9/2022 in #help
❔ abstract class virtual methods vs default implementation interface
Okay so I’m a bit shaky on this but the way I understand it: An abstract class tells the code that the implementation of the class is incomplete. Therefore, another class must inherit the abstract base class. By using virtual methods within the base class, these methods can be either overridden or implemented directly. These virtual methods are therefore allowed to have a method body. If an override is not defined, the derived class simply inherits the virtual method’s base class definition. An interface provides a contract between the base and derived class that all methods must have an implementation. In C#8, an interface method is allowed a method body. If the method is not defined in the derived class, the default implementation is used. So what really is the difference between the two? Are my definitions incorrect? and are fields easier to use in abstract classes than interfaces?
22 replies
CC#
Created by Matt on 11/8/2022 in #help
How to force implementation of concrete interface methods
How do I force the default the requirement for implementation of interface methods which have a body / default implementation? (C# 8)
7 replies
CC#
Created by Matt on 10/26/2022 in #help
Condense Byte[] to BitArray to smaller Byte[]
I have an array of 300 bytes, all either 0 or 1. I need a function that takes in the byte array and performs a bitwise operation to convert the bytes to bits. As an example: If the first 8 bytes of the input array are
{ 01, 01, 00, 00, 00, 00, 00, 00 }
{ 01, 01, 00, 00, 00, 00, 00, 00 }
Then the BitArray should end up as
{ 00000011 }
{ 00000011 }
Which ends up as a byte 03 Obviously the array will have to be handled in chunks of 8. The resulting byte array should be about 40 bytes long, hence condensing the 300 byte array to a 40 bytes array. I can't deviate from this structure unfortunately, I wish I could but this is what is required. Any advice on anything I should read up on or a fancy linq statement that will do this all for me would be greatly appreciated. Thank you
17 replies
CC#
Created by Matt on 10/16/2022 in #help
Count updating before it's supposed to
public void CheckCounts()
{
ReadCounts();

Console.WriteLine($"Current TE Count = {CollectibleCounts[0]}\nPrevious TE Count = {PreviousCollectibleCounts[0]}");
if (!Enumerable.SequenceEqual(PreviousCollectibleCounts, CollectibleCounts))
{
PreviousCollectibleCounts = CollectibleCounts;
UpdateServerData(HeroHandler.CurrentLevelId, LevelData[HeroHandler.CurrentLevelId], "Collectible");
}
}
public void CheckCounts()
{
ReadCounts();

Console.WriteLine($"Current TE Count = {CollectibleCounts[0]}\nPrevious TE Count = {PreviousCollectibleCounts[0]}");
if (!Enumerable.SequenceEqual(PreviousCollectibleCounts, CollectibleCounts))
{
PreviousCollectibleCounts = CollectibleCounts;
UpdateServerData(HeroHandler.CurrentLevelId, LevelData[HeroHandler.CurrentLevelId], "Collectible");
}
}
This function is called in an infinite loop. When run, the code writes to the console with the previous and current collectible counts. For example, let's say I have 2 collectibles. The code writes: "Current TE Count = 2" "Previous TE Count = 2" Every time the loop executes. When I then collect a collectible, since there is literally nowhere else in the code assigning anything to the previous count other than what I've posted, I would expect to see: "Current TE Count = 3" "Previous TE Count = 2" Then the if statement would be called, updating the previous count and only then should I see: "Current TE Count = 3" "Previous TE Count = 3" In reality, I never see a console write where the current and previous counts are different. Any idea why? I'm aware this is difficult to answer without any other code but believe me when I say I have checked to see if previous count is being updated somewhere else many times.
11 replies
CC#
Created by Matt on 10/15/2022 in #help
Tricky networking issue
This is less of a directly c# related question and more of a networking issue but I guess this is still a good place to ask. So I have two clients, each with a set of data. Let's say for simplicity that 2 clients have a data set of { 0, 0 } each. I want to alert client 2 if one of client 1's zeros flips to a one. So I have a function watching this byte array and when it changes, I send a message to a server with the updated byte array. This in turn sends a message to client 2 with the updated byte array and client 2 then applies the updated array. Everything is good so far. But then the issues begin. Because client 2 is also watching their own byte array and they've just seen that their byte array has been updated. This means client 2 will now send a message to the server with their updated byte array which then sends the array to client 1 and so on. Is there a way around this or am I missing something?
2 replies
CC#
Created by Matt on 10/14/2022 in #help
Union? of two binary arrays
Let's say we have two byte arrays:
byte[] ba1 = { 01, 01, 00, 00 };
byte[] ba2 = { 01, 00, 01, 00 };
byte[] ba1 = { 01, 01, 00, 00 };
byte[] ba2 = { 01, 00, 01, 00 };
I want to perform some operation on the arrays such that if the first byte in BOTH arrays is 0, then the resulting array's first byte is 0 but if EITHER byte is a 1, then the resulting array's first byte is a 1. (Extend this logic to each byte) Thus, the resulting array should be:
byte[] baRes = { 01, 01, 01, 00 }
byte[] baRes = { 01, 01, 01, 00 }
Is there any nice function that will do this for me?
45 replies
CC#
Created by Matt on 9/11/2022 in #help
get the address from pointer offsets.
Trying to make a function to grab an address from pointer offsets. New to pointers and tried to follow a tutorial using Pymem but write it in c# which I'm far more comfortable with. Hasn't gone so well. The function returns 0 from a correct input. Would really appreciate the help if someone knows what I'm doing wrong
public int GetPointerAddress(int baseAddress, int[] offsets)
{
int bytesRead = 0;
byte[] newAddrBytes = new byte[4];
Program.ReadProcessMemory((int)tyexeHandle, baseAddress, newAddrBytes, 4, ref bytesRead);
int addr = BitConverter.ToInt32(newAddrBytes, 0);
foreach(int i in offsets)
{
if (i != offsets[offsets.Length - 1])
{
bytesRead = 0;
byte[] newAddr = new byte[4];
Program.ReadProcessMemory((int)tyexeHandle, baseAddress + 1, newAddr, 4, ref bytesRead);
addr = BitConverter.ToInt32(newAddrBytes, 0);
}
}
return addr + offsets[offsets.Length - 1];
}
public int GetPointerAddress(int baseAddress, int[] offsets)
{
int bytesRead = 0;
byte[] newAddrBytes = new byte[4];
Program.ReadProcessMemory((int)tyexeHandle, baseAddress, newAddrBytes, 4, ref bytesRead);
int addr = BitConverter.ToInt32(newAddrBytes, 0);
foreach(int i in offsets)
{
if (i != offsets[offsets.Length - 1])
{
bytesRead = 0;
byte[] newAddr = new byte[4];
Program.ReadProcessMemory((int)tyexeHandle, baseAddress + 1, newAddr, 4, ref bytesRead);
addr = BitConverter.ToInt32(newAddrBytes, 0);
}
}
return addr + offsets[offsets.Length - 1];
}
12 replies