[HN Gopher] My thoughts on writing a Minecraft server from scrat...
       ___________________________________________________________________
        
       My thoughts on writing a Minecraft server from scratch in Bash
        
       Author : jscob
       Score  : 369 points
       Date   : 2022-02-15 15:16 UTC (7 hours ago)
        
 (HTM) web link (sdomi.pl)
 (TXT) w3m dump (sdomi.pl)
        
       | Brian_K_White wrote:
       | You can read and write binary, including null, in pure bash,
       | without even subshells, let alone external processes like dd and
       | xxd.
       | 
       | This is a driver/client for a vintage portable disk drive that
       | has an rs232 interface. Any disk drive obviously has to handle
       | arbitrary data including binary including 0x00.
       | 
       | It's entirely in native bash with no external tools and not even
       | any subshells. It does have to call stty and mkfifo once at
       | startup, but that's just startup like to open the serial port,
       | not part of the work.
       | 
       | https://github.com/bkw777/pdd.sh
       | 
       | The gimmick for reading nulls is not exactly efficient. Basically
       | you read one character at a time and look at the errorlevel from
       | read to detect the difference between "got nothing" and "got
       | 0x00"
       | 
       | It's fine for this case because the drive is so slow and small
       | that even the entire disk is only 200k max. Making bash read one
       | byte at a time for 200k is still nothing today but only because
       | hardware is insane today.
       | 
       | But it's possible. And the beginning of the post does say "as a
       | thought experiment".
       | 
       | Similarly, you don't need xxd to convert back & forth between raw
       | binary and text encoding.
       | 
       | My own similar thought experiment included the idea that, if I'm
       | not going to use any external process I can possibly avoid and
       | only use internal bash features, on the flip side of that, I will
       | squeez bash for all it's worth, allow every bashism possible.
        
       | dolmen wrote:
       | Using awk is cheating.
        
         | NieDzejkob wrote:
         | I'm sure patches are welcome.
        
       | blurker wrote:
       | I am in awe. Also, I think your are a huge masochist! It's like
       | wood working with cutlery. But it's actually really informative
       | about some advanced bash knowledge. Kudos!
        
       | js2 wrote:
       | Where there's a will, there's a way. I once needed to do network
       | calculations where I only had some very limited tools, so I used
       | awk to convert 32-bit ints to a 32-character ascii bit string of
       | "1" and "0" and the converse function, then wrote and, or, and
       | not functions that worked on the ascii strings.
       | 
       | https://gist.github.com/jaysoffian/e41ca479d70e60efe59fded93...
        
       | gorgoiler wrote:
       | The _one thing_ that people who understand bash have in the
       | forefront of their minds is error handling.
       | 
       | You can be a wizard in bash if nothing ever goes wrong. Functions
       | calling functions calling functions. It's basically programming.
       | 
       | Until something goes wrong. Then there's nothing you can do.
       | 
       | Anyone who has ever set-dash-ee'd and then seen a function
       | execute without set-dash-ee when used in an _if_ context knows
       | what I mean.
        
         | xg15 wrote:
         | That's why the cool kids today use set -euo pipefail.
         | 
         | Or set -x if you're debugging.
        
           | naniwaduni wrote:
           | That ... doesn't solve the problem GP is noting.
        
           | gorgoiler wrote:
           | You may have missed my point.
           | 
           | Set -e is not global. It only applies in some cases. In other
           | contexts (inside a function when called as if <function>)
           | errors will not be caught and once you realise that you
           | realise there's no hope of writing error proof logic in bash.
        
             | xg15 wrote:
             | Ah, I'm sorry. I didn't realize the problem with functions
             | and indeed misunderstood.
             | 
             | I mean, while it's amazing what you can do with bash, I'd
             | be wary to use it for "production" stuff. So far, I mostly
             | used it for "toolbox" type if stuff that is only supposed
             | to be used by the devs. For that purpose, it worked well
             | though.
        
       | fao_ wrote:
       | I'm surprised this person didn't switch to using wcalc, instead
       | of awk. For comparison:
       | 
       | fao_@blob:~$ time $(echo '' | awk '{print (2*-1)}' >/dev/null)
       | 
       | real 0m0.006s user 0m0.006s sys 0m0.000s
       | 
       | fao_@blob:~$ time $(wcalc '2*-1' >/dev/null)
       | 
       | real 0m0.005s user 0m0.001s sys 0m0.005s
       | 
       | I'd imagine you could use something like xargs to insert the
       | number so that wcalc can interpret the bits for you
        
         | folmar wrote:
         | The results you post don't seem to differ much, do you mean the
         | difference is bigger as the inputs get larger? Spawning an
         | extra subshell inside 'time' seems strange, as does using echo
         | instead of </dev/null.
         | 
         | Also: people wiriting things in bash usually try not to rely on
         | utilities that are not present by default, and wcalc is not.
        
       | arendtio wrote:
       | This reminds me of the xmpp chat bot written in sed:
       | https://github.com/horazont/xmpp-echo-bot
       | 
       | While I admire the skill involved, I keep wondering what drives
       | people into starting such projects :D
        
       | chaps wrote:
       | At one point I've had a NBT parser implementation implemented
       | almost fully, but I decided it was not worth the hassle to finish
       | it. The code is currently lost, due to my extensive use of tmpfs
       | as a project directory, and a system crash.
       | 
       | Hah, are you me? I also lost some Minecraft server management
       | stuffs written in bash in a tmpfs dir. It was years ago, but I
       | 100% feel your pain.
       | 
       | Also, well done! This looks like it was a lot of fun.
        
       | rootsudo wrote:
       | this is so cool.
        
       | userland_tech wrote:
       | Not as cool as this :) but I recently released an app for running
       | Minecraft servers from your phone. CraftBox:
       | https://play.google.com/store/apps/details?id=tech.ula.craft...
        
       | lsferreira42 wrote:
       | I just love this, this is why i love programming and writing a
       | forking minecraft server in C was one of the most fun things i
       | did in computing, i still have the source for that experiment,
       | good times!
        
       | ShortStretto wrote:
       | Projects like this make me warm inside. Lovely work.
        
       | mmastrac wrote:
       | This is pretty wild. It could actually be useful as a quick and
       | dirty lobby that doesn't require a full server.
        
         | puyoxyz wrote:
         | true, but better projects for that already exist
        
       | stnmtn wrote:
       | Reading stuff like this makes me think about how much more...
       | "Fun" game programming is than the stuff I do in web dev.
       | 
       | These problems (yes, self-constrained) look like so much fun to
       | solve, in a way that "I did xyz thing in pure CSS" is just less
       | so to me.
       | 
       | Maybe It's that I miss rapid cycle of Completely Lost -> Earth-
       | shattering Realization of How To Make This Work that the first
       | ~1-2 years of my programming journey was full of
        
       | _hl_ wrote:
       | What's next, a database in pure bash? It's really impressive what
       | some people can do with a couple of shell scripts.
        
         | Izkata wrote:
         | From a quick search didn't find anyone going all out, but did
         | find this that could be used as a starting point:
         | https://matt.might.net/articles/sql-in-the-shell/
        
       | Klasiaster wrote:
       | Besides the nostalgia, the website font is so easy on my eyes I'm
       | considering to give up space and start to use it as well
        
       | zymhan wrote:
       | > The code is currently lost, due to my extensive use of tmpfs as
       | a project directory, and a system crash.
       | 
       | As if the project wasn't enough of a challenge on it's own, let's
       | throw in some russian roulette!
        
       | mig39 wrote:
       | Appreciate the QR code! I scanned it. You should too.
        
       | h2odragon wrote:
       | gloriously insane. So how do we represent /proc in such a way as
       | to make interacting with it through minecraft useful?
       | 
       | "psDoom" was great fun now i see "tunnel through the DB's page
       | tables searching for a more direct route to the application
       | engine"
        
         | netr0ute wrote:
         | > So how do we represent /proc in such a way as to make
         | interacting with it through minecraft useful?
         | 
         | Not exactly what you wanted, but Hajime can already query info
         | from /proc to make it accessible from within Minecraft.
         | 
         | https://github.com/Slackadays/Hajime
        
       | touisteur wrote:
       | Such a fan. 'The code is currently lost, due to my extensive use
       | of tmpfs as a project directory, and a system crash.'
       | 
       | And I was sad to see some things farmed out to awk. I mean what's
       | next? bc? ed? ;-)
        
       | chairmanwow1 wrote:
       | I think the first Q in the FAQ warms my heart:
       | 
       | > Q: Why?
       | 
       | > A: Because I could. And it was fun!
        
         | asddubs wrote:
         | my full question was actually "why would you do that to
         | yourself?"
        
           | r3trohack3r wrote:
           | I've found bash to be a pretty fulfilling language to work
           | in.
           | 
           | I fed The Linux Documentation Project and Pure Bash Bible
           | into my Anki decks which was really a turning point in
           | writing bash for me.
           | 
           | It's pretty amazing how much can be accomplished in a
           | relatively small amount of bash.
        
             | Nuzzerino wrote:
             | Can you give some examples? I've personally found bash to
             | be among the most difficult to work with. If I need to do
             | something in bash I'd rather keep it as a lightweight
             | wrapper around a ruby script or something, though I know
             | that isn't always an option
        
               | [deleted]
        
               | fabiomaia wrote:
               | If you are manipulating files and processes, bash is a
               | very natural and succint language to do so. Programs are
               | much more elegant than a generic programming language.
        
               | bradwood wrote:
               | Bash is about pipelines which is a rather functional
               | concept. If you can exploit these you can write succinct,
               | readable and effective bash IMHO. If you're just
               | stringing together a lot of conditionals it gets ugly
               | fast.
        
             | munk-a wrote:
             | I haven't worked extensively in Bash alone but I did work
             | extensively in Make for a while and I can say with
             | confidence: the specialized old tooling languages are
             | extremely good at what they do, Make goes out of its way to
             | make building things extremely slick. These language tools
             | were designed to tackle every problem in their realm
             | because, for a lot of people, there wasn't an alternative -
             | their structure shows clear and careful design intent.
        
         | MaxLeiter wrote:
         | the best praise I've received for something I worked on was:
         | This is an absolutely batshit crazy idea. I love it.
         | 
         | Sometimes its just fun to do something that's ridiculous. We're
         | all talking about it here, aren't we?
        
       | Waterluvian wrote:
       | I was expecting this to just be "the netcode was written in Bash.
       | For the actual game state management, we just use existing X."
       | But no, it looks like they've covered ALL of that.
       | 
       | Wow.
        
         | rightbyte wrote:
         | I guess inline awk is fair game.
        
           | unixhero wrote:
           | and sed
        
           | baq wrote:
           | There's a fine line between classy masochism and utter
           | batshit insane lunacy, sir.
        
           | Waterluvian wrote:
           | It's such a nebulous thing, eh? I'd say `Awk` (like curl,
           | sed, grep, etc.) is part of the Bash toolkit. But then why
           | isn't Python?
           | 
           | I guess the idea is that Bash is about piping together these
           | small programs that typically take stdio and produce stdout.
           | I think these have a name.. like GNU-style programs or
           | something.
        
             | maccolgan wrote:
             | It's called UNIX scripts, GNU is Not Unix.
        
       | xg15 wrote:
       | Anime avatar, uwu - und completely insane networking projects.
       | Glad to know hacker culture is alive and well xD
        
         | oh_sigh wrote:
         | anime avatars and uwu are as much a part of hacker culture as
         | burritos are
        
           | bobthebuilders wrote:
           | anime has been a part of hacker culture since a while now
        
             | TadeusTaD wrote:
             | I dare say it's a part of the hacker culture for as long as
             | both coexist
        
               | bozhark wrote:
               | Ghost in the fucking she'll yo
        
           | dijit wrote:
           | There's definitely a strong correlation among certain
           | segments of hacker culture.
           | 
           | There's a lot of furries too.
           | 
           | Might be confirmation bias, we see the people that stick out,
           | but to say there isn't a large cohort that strongly
           | identifies with anime is just wrong.
        
             | maccolgan wrote:
             | >There's a lot of furries too.
             | 
             | Wrong part of the internet.
        
               | detaro wrote:
               | Nope. Internet infrastructure almost runs on furries
               | nowadays.
        
               | bozhark wrote:
               | Furries run them tubes
        
               | xg15 wrote:
               | owo.
        
           | bobobob420 wrote:
           | out of touch with new gen
        
             | oh_sigh wrote:
             | nope.
        
               | weakfish wrote:
        
               | bobobob420 wrote:
               | fair enough
        
         | daptaq wrote:
         | > uwu
         | 
         | Debatable part of hacker culture. I'd say it's more a part of
         | postmodern culture.
        
         | bloqs wrote:
        
         | alexjplant wrote:
         | The Fixedsys-esque font in conjunction with the Anime aesthetic
         | seems to tilt more towards vaporwave IMO.
        
       | chasil wrote:
       | I notice that the author is going into awk for floating point
       | processing. The 1993 language standard for the Korn shell brings
       | floating point into the language as a native data type. This
       | advanced version of the Korn language does not appear to be under
       | active maintenance, but it will likely be much, much faster than
       | handling floating point in awk. Unfortunately, none of the BSD-
       | licensed clones of Korn support the 1993 standards (including
       | floating point).
       | 
       | I also see that the author is having some trouble reading
       | individual bytes. Perhaps these shell tricks might be useful.
       | 
       | http://www.etalabs.net/sh_tricks.html
        
       | TkTech wrote:
       | > As in my opinion the wiki page doesn't explain it well enough
       | to quickly comprehend, here's another drawing:
       | 
       | Contributions welcome :) Our wiki is open and anyone can edit. If
       | you can ELI5 something to make it easier to understand, go for
       | it.
        
       ___________________________________________________________________
       (page generated 2022-02-15 23:00 UTC)