mlvzk / discocss

A tiny Discord css-injector for Linux and MacOS.
Mozilla Public License 2.0
211 stars 16 forks source link

Unique symlink name inside /tmp #21

Open zkwinkle opened 1 year ago

zkwinkle commented 1 year ago

Summary

This PR addressed #20.

The method to generate the random ID's is to transform the $USER variable into a numerical seed and then use awk srand(seed) and rand() to generate the 7 character long ID.

The solution is a little bit over-engineered I think, but it works perfectly!

I will explain my reasoning but feel free to change anything.

Rationale

I put the procedure inside a gen_id() function only for the reason that I thought it was orderly.

Didn't want to add any dependencies so that's why I went with the current approach. The hashing commands I found depnded on Perl, and even the $RANDOM variable apparently wasn't POSIX compliant. That's why I ended up using awk for the RNG.

The reason the string is decoded into ASCII values is because the seed must be numerical, if you try to pass a string rand() will always return the same values.

The SEED=$((SEED % 4294967295)) is there because srand() takes in an unsigned int and 4294967295 is the max value of one.

The while loop where I'm checking while [ ${#ID} -lt $WIDTH ] is there because even if I multiplied rand() by a large enough number, I think there's always a chance it returns really low numbers so this while loop guarantees that ID will reach at least 7 characters long. Could've also added padding to the string but I think to keep generating more random numbers generates more unique numbers than adding padding does?

The reason I multiply rand()*32768 in specific is simply because that's what they multiply it by in the shellcheck example for RNG. I think it could be literally any number and it wouldn't matter anyways.

Shellcheck

I made sure the commit didn't add any shellcheck warnings or anything, this is my shellcheck output:

λ [master] >>> shellcheck discocss

In discocss line 104:
LC_ALL=C sed $sed_options "$app_preload_replace; $launch_main_app_replace; $frame_true_replace; $causing_the_window_replace" \
             ^----------^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: 
LC_ALL=C sed "$sed_options" "$app_preload_replace; $launch_main_app_replace; $frame_true_replace; $causing_the_window_replace" \

For more information:
  https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
mlvzk commented 1 year ago

Sorry, I haven't had time to properly review this. I will probably implement this later using a $RANDOM generated once and stored in $XDG_CONFIG_HOME, which will be used as the ${ID} in preloadLink, or use something simpler as a consistent ID

zkwinkle commented 1 year ago

That's okay, but I urge you to reconsider my method because it's self contained, and doesn't create any unnecessary files in the system. Right now, discocss is POSIX shell compliant, making it basically as portable as a shell script can be, but the $RANDOM magic variable isn't POSIX shell.