Home

CastleDB


Building from source

In my experience building fully from source is challenging due to version compatibility issues with nwjs. However, you don't actually have to build the nwjs app to customize CastleDB for personal use: you can just replace the castle.js and index.html files in the distribution directory and those changes will automatically be taken up by the exe.

Parsing tilemap data

Tilemap data is stored as a base64 encoded 1D Array<Int> of 2 bytes. The array must be unfolded to get the 2D data per cell. The data per cell itself corresponds to the 1D index of the content in the source tileset file, which must itself be unfolded to get the actual tile index.

Index 0 is used as empty, so the x-index will need to start at 1.

Example:

class LayerData {
    public var file:String;
    public var size:Int;
    public var stride:Int;
    public var data:Array<Int>;

    public function new(data:Dynamic) {
        this.file = data.file;
        this.size = data.size;
        this.stride = data.stride;
        if(data.data != null){
            this.data = decodeBase64ToIntArray(data.data);
        }
    }

    public function render(): h2d.Object {
        var tile = Res.load(file).toTile();

        var tileGroup = new h2d.TileGroup(tile);
        var tiles = TileUtils.cutTiles(tile, size);

        var width = Std.int(this.layer.map.width);
        var height = Std.int(this.layer.map.height);
        for (y in 0...height) {
            for (x in 0...width) {
                var index = y * width + x;
                var tilePos = data[index];

                    var tileX = Std.int(tilePos % (tile.width / size));
                    var tileY = Std.int(tilePos / (tile.width / size));
                    if(tileX == 0 && tileY == 0){
                        tileGroup.add(x * size, y * size, Tile.fromColor(0x030101, size, size, 0));
                    }
                    else {
                        tileGroup.add(x * size, y * size, tiles.get(tileX - 1, tileY));
                    }

            }
        }

        return tileGroup;
    }

    private function decodeBase64ToIntArray(base64:String):Array<Int> {
        var bytes = Base64.decode(base64);
        var input = new BytesInput(bytes);
        var result:Array<Int> = [];

        while(input.position < input.length) {
            result.push(input.readUInt16());
        }

        return result;
    }
}

THEORETICAL: Embed in VS Code

It should practical to embed the entire CastleDB application into VS Code as an IDE extension, allowing .cdb files to be edited within the IDE itself.

The more important benefit of this would be the ability to use entire code files as a data type. This would be a much more stable way to map logic or behaviors to row items. You could even interoperate with the Language Server Protocol. This sort of thing is a developer's holy grail imo.

You would start by using a CustomEditorProvider in VSCode. Documentation on this is sparse and nasty. I have "the simplest possible example" here: https://github.com/c-g-dev/CustomEditorProvider_SimpleExample

I doubt you would be able to embed the nwjs app into the extension itself, you would need to change CastleDB into a pure node.js webview app. That shouldn't be too difficult, I believe that the only things it uses from nwjs specifically are the menu items and other trivial guis. Still, this would probably be a pretty involved project.

Edit

Pub: 01 Aug 2024 13:23 UTC

Edit: 09 Sep 2024 06:04 UTC

Views: 74