Category Archives: Software

WebService::Discord::Webhook

My first Perl CPAN module is online! It’s a class to help interact with Webhook objects provided by the Discord chat service.

https://metacpan.org/pod/WebService::Discord::Webhook

Webhooks are basically a “write-only” way to put a message into the chat from your external service. Use them to provide notifications in chat, where a full-blown “bot” client is not needed (e.g. no need to interact with users).

Modest Bricks

Modest Bricks gameplay video

Modest Bricks is a Tetris clone. Originally written in 2005, it uses SDL 1.2 and the SDL_Mixer library for sound effects and music. This was one of the first “serious” games I wrote – though I did a number of QBasic and Visual Basic things before, I had never attempted a game in C, or with this level of polish. I wrote this for a few reasons:

  • Learn SDL and practice game dev in C, tools used (at the time) by Professionals to make Real Games
  • Make the source available so I could teach and demonstrate SDL to the University of Arkansas Game Development Club, a group I helped co-found around the same time.
  • Follow the advice of this GameDev.net tutorial, “How do I make games?” which recommended to start small by creating a polished version of Tetris to get the basics down and have something to show.
  • Put my music into a game!

Recently I took an interest in revamping some old code, and ended up rewriting much of this. The effort is complete so I’m releasing it. A Windows version is included in the .zip below, as well as the source code in C. Recompiling on *nix should be easy: cc `sdl-config --libs --config` -lSDL_Mixer -o modest main.c should be reasonably close, assuming SDL and SDL_Mixer development libraries installed.

Probably the most interesting thing about this game is that after writing, I pushed it to the SDL webpage, which (at the time) had a gallery for people to post their games Made With SDL. From there it caught the attention of a Dreamcast homebrew developer Ian Micheal, who was porting SDL games to the DC. He altered the graphics and added more sounds and cool effects to create “Ghouls ‘N TriX” – a Tetris game for the Dreamcast! It was really cool to know my code was running on a video games console, and I made sure alllll my friends knew about it 🙂

Download the game:

BBCode Parser (in PHP)

For a while now I’ve been working on a little blog engine in PHP. As part of the post rendering, I’ve settled on BBCode to do all markup and layout.

This meant I would need a BBCode parser. So, I started writing one. And because my projects always go this way, I ended up sinking more time into the BBCode parser than the blog engine itself – trying to get every weird corner case and not produce malformed HTML on output. The primary goals were:

  • Easy to use (one file with one function)
  • Correct (Unicode safe, always-valid HTML, reasonable fallbacks)
  • Easy to understand (avoid massive regexes)

Eventually, I ended up just splitting this piece off into its own project, since it may be useful beyond just my tiny blog.

https://github.com/greg-kennedy/php-bbcode

I know there are a million BBCode parsers out there (and even a PHP extension to do it), some extensible and so on. This one is mine. Would love pull requests.

Writing a WebSocket Client in Perl 5

WebSockets are the latest way to provide bi-directional data transfer for HTTP applications. They replace outdated workarounds like AJAX, repeated polling, Comet, etc. WebSockets are a special protocol atop HTTP (which in itself runs over TCP/IP), and can be wrapped in SSL for security (as in HTTPS). Being a Web Technology, it seems to have been developed by the JavaScript people exclusively for the JavaScript people – working with it outside a web browser or Node.js server can seem convoluted. But that’s the world they built, and we just we live in it.

Basic Perl support / documentation for WebSockets was difficult for me to find. The Mojolicious framework (specifically the UserAgent module) has native WebSockets support and can act as a client, but I was looking for info on using with WebSockets on a lower level / without Mojo or other frameworks. Hopefully, this post can shed some light on how you can use Perl to connect to a remote server using WebSockets, and get access to those sweet real-time services using our favorite language.

First off, if you can, you should just use AnyEvent::WebSocket::Client or Net::Async::WebSocket::Client (depending on your preference of async framework). This package has already done the hard work of combining the two packages you’d probably use anyway, Protocol::WebSocket::Client (for encoding / decoding WebSocket frames), and AnyEvent (for cross-platform non-blocking I/O and doing all the messy TCP socket stuff for you). Having already established my status as a Luddite a desire to know what’s really going on, let’s try to reinvent the wheel and write our own client.

A Client for the Echo Test Service

The goal of this project is to interoperate with the WebSocket Echo Server at ws://echo.websocket.org. The Echo Server simply listens to any messages sent to it, and returns the same message to the caller. This is enough to build a simple client that we can then customize for other services. There are two things we need to make this work:

  • a plain Internet Socket, for managing the TCP connection to the remote server, and
  • a Protocol handler, for encoding / decoding data in the WebSocket format.

The second part of this is already done for us by Protocol::WebSocket::Client: given a stream of bytes, it can identify WebSocket frames and parse them into data from the server, and it can take data from our program and encapsulate it for sending. This tripped me up at first, so pay attention: Protocol::WebSocket does NOT actually do anything with the TCP socket itself – meaning it does not send or receive any data on its own! The class is responsible for only these things: packing/unpacking data, generating a properly formatted handshake for initiating a WebSocket connection, and sending a “close” message to the server signalling intent to disconnect.

Given that Protocol::WebSocket::Client doesn’t do any TCP socket stuff itself, we have to manage all that. Fortunately, there’s the core module IO::Socket::INET which we can use. Protocol::WebSocket::Client also provides some hooks for points in the WebSocket flow, so that we can insert our own handlers at those points. Let’s get started with some code.

Continue reading

Info Page – @FPAdventuresBot

MYST Book – screenshot from Myst (PC, 1993)

@FPAdventuresBot is a Twitter account that regularly posts screenshots from first-person, point-and-click adventure games. Images are randomly selected from a pool, pre-extracted from original game data files. The bot source code is available on GitHub:

https://github.com/greg-kennedy/FPAdventuresBot

It uses Twitter::API to manage file uploads and posting to Twitter. For some games, I have written custom tools to extract images. Source code for these is in the tools/ subdirectory. There are also some audit scripts in the main folder to find duplicate images or solid-color images. These use Image::PNG::Libpng for working with PNG images.

The account currently posts images from the following games:

Continue reading