aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit c3f9ca4d75a70f88d00362e9ed2d072fbe145234
parent e808063439b9aa8d6995e5952af3a4c65c34d83e
Author: anon <anon@example.com>
Date:   Tue, 10 Nov 2026 08:13:22 +0000

Use pure-Go SQLite for standalone server builds

Diffstat:
MDEVELOPERS.md | 7++++---
MREADME.md | 2+-
Mdocs/world-state.md | 4++--
Mgo.mod | 14++++++++++++--
Mgo.sum | 50++++++++++++++++++++++++++++++++++++++++++++++++--
Mserver/admin_pages.go | 4+++-
Mserver/admin_sql.go | 27++++++++++++++-------------
Mserver/database_metrics.go | 41++++++++++++++++++++++++++++++-----------
Mserver/fling_crash_test.go | 2+-
Mserver/player_checkpoint_batch_test.go | 2+-
Mserver/visitor_names.go | 7++++---
11 files changed, 120 insertions(+), 40 deletions(-)

diff --git a/DEVELOPERS.md b/DEVELOPERS.md @@ -9,16 +9,17 @@ Game definitions and their provenance are embedded YAML in `server/gamedata`. NPC behavior is Go code in that directory, linked to YAML group IDs through `NPCScripts`; see [the conversation API and source evidence](docs/npc-scripts.md). Static resources live in `server/assets`. Default builds read `./assets/` from -their launch directory. Build with Go 1.26 or newer from the repository root: +their launch directory. The server and SQLite driver are pure Go; no C compiler +is needed. Build with Go 1.26 or newer from the repository root: ```sh -go build -o aorecomp ./server +CGO_ENABLED=0 go build -o aorecomp ./server cd server ../aorecomp ``` For a standalone release, build from the repository root with -`go build -tags embedassets -o aorecomp ./server`. Distribute that binary by +`CGO_ENABLED=0 go build -tags embedassets -o aorecomp ./server`. Distribute that binary by itself (or the corresponding Windows binary); it can run from an empty directory. An external-assets installation instead needs `assets/` containing `anarchy-assets-14.9.1/`, `client/`, `maps/`, `favicon.ico`, `terrain.bin` and diff --git a/README.md b/README.md @@ -58,7 +58,7 @@ Edit Go code in `server/` and game definitions (NPC spawns and scripts) in `server/gamedata/`, then restart the server to apply your changes. The compiled browser client is included, so you do not need a WASM toolchain for server-side work. Default builds read `./assets/` from the launch directory. To build a standalone -release from the repository root, use `go build -tags embedassets -o aorecomp ./server`. +release from the repository root, use `CGO_ENABLED=0 go build -tags embedassets -o aorecomp ./server`. ## 4. Modify and compile the client diff --git a/docs/world-state.md b/docs/world-state.md @@ -108,8 +108,8 @@ transaction. YAML is only an editing format; the server does not read or write mutable YAML files. There is no migration path for previous server databases. To reset the world, stop the server and delete `data/world.sqlite` and any matching `-wal`/`-shm` -files before restarting. Server builds require CGO and a C compiler for the -[SQLite driver](https://github.com/mattn/go-sqlite3). +files before restarting. The server uses the pure-Go +[SQLite driver](https://modernc.org/sqlite/) and builds with `CGO_ENABLED=0`. Editor checks cover separate inventory/equipment slot validation, missing and duplicate instances, incompatible equipment, ownership, explicit unassignment, diff --git a/go.mod b/go.mod @@ -4,9 +4,19 @@ go 1.26.0 require ( github.com/coder/websocket v1.8.15 - github.com/mattn/go-sqlite3 v1.14.52 go.yaml.in/yaml/v3 v3.0.5 golang.org/x/crypto v0.58.0 + modernc.org/libc v1.80.1 + modernc.org/sqlite v1.62.0 ) -require golang.org/x/sys v0.49.0 // indirect +require ( + github.com/dustin/go-humanize v1.1.0 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect + github.com/ncruces/go-strftime v1.1.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/sys v0.49.0 // indirect + modernc.org/mathutil v1.7.1 // indirect + modernc.org/memory v1.12.1 // indirect +) diff --git a/go.sum b/go.sum @@ -1,10 +1,56 @@ github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA= github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg= -github.com/mattn/go-sqlite3 v1.14.52 h1:wVbm2Qnf4OXkqhBTSPuCRZDRnxfbVrrmiCEroVdog8U= -github.com/mattn/go-sqlite3 v1.14.52/go.mod h1:6JTjA44L93a0QCyJef5YvlPoKXntQPjzWv5gtm9sB6w= +github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= +github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= +github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50 h1:PlNWJRxu5APSV0pWqfIsZ0qt9OOGHSWfZQ4zvx+WgWs= +github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50/go.mod h1:yUo1AMMpsPIDFGuwDK8nLcf8+vvAd5l2O6JDZ6hpwrM= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL47Hk= +github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= +golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= +golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= +golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= +modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b h1:WE5SLOURcNd8s6uPR/n4WV4weQxxYKi5OpEZkfNVvdE= +modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI= +modernc.org/ccgo/v4 v4.38.1 h1:wdxJVBuFT8JufZnnQyS6Bbz3NOWhViD0xJOGR96Bpd4= +modernc.org/ccgo/v4 v4.38.1/go.mod h1:kOpEkJRbFwBvjUWwn4j1j9B7iFHbl1+orhgTMCDkDBk= +modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM= +modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU= +modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI= +modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= +modernc.org/gc/v3 v3.2.0 h1:/Y3LGDMQaoYf55PebP1qsZ5FDGgAZyG1RYKEHeQzh1o= +modernc.org/gc/v3 v3.2.0/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY= +modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks= +modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI= +modernc.org/libc v1.80.1 h1:ejn7HtTV3GknNKbklqWQ1nvUrfX8mTxs1D+xp/JQwy4= +modernc.org/libc v1.80.1/go.mod h1:pvo25Rs3OJEbDqhUV9jdEGevxeLJrJeDmNid/AcQV1g= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.12.1 h1:nFMiWrpStgZczNl6XI9GnIk/rWhYIyHGUaR04pGbp9g= +modernc.org/memory v1.12.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg= +modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns= +modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w= +modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE= +modernc.org/sqlite v1.62.0 h1:gMaCjSM1qpl+iOjiWhR1LYP3zqrYDI+5GYLGt6veP2g= +modernc.org/sqlite v1.62.0/go.mod h1:Jzjh1mt4s0HuQ0KvtB1LIuHiGRmU2D5pgj8dUhW7/SE= +modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0= +modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/server/admin_pages.go b/server/admin_pages.go @@ -170,7 +170,9 @@ func (server *server) sqlPage(w http.ResponseWriter, r *http.Request) { db := sql.OpenDB(databaseConnector{&server.databaseQueries}) defer db.Close() db.SetMaxOpenConns(1) - for _, statement := range sqlStatements(query) { + statements, err := sqlStatements(query) + queryError = err + for _, statement := range statements { rows, err := db.QueryContext(ctx, statement) queryError = err if err != nil { diff --git a/server/admin_sql.go b/server/admin_sql.go @@ -1,27 +1,28 @@ package main -/* -#include <stdlib.h> -// Use the SQLite parser linked by go-sqlite3, including trigger bodies and quotes. -int sqlite3_complete(const char *sql); -*/ -import "C" - import ( "strings" - "unsafe" + + "modernc.org/libc" + "modernc.org/sqlite/lib" ) -func sqlStatements(query string) []string { +func sqlStatements(query string) ([]string, error) { + // Use SQLite's Go-translated parser for trigger bodies and quoted semicolons. + tls := libc.NewTLS() + defer tls.Close() var statements []string start := 0 for index, character := range query { if character != ';' { continue } - text := C.CString(query[start : index+1]) - complete := C.sqlite3_complete(text) != 0 - C.free(unsafe.Pointer(text)) + text, err := libc.CString(query[start : index+1]) + if err != nil { + return nil, err + } + complete := sqlite3.Xsqlite3_complete(tls, text) != 0 + libc.Xfree(tls, text) if complete { statements = append(statements, query[start:index+1]) start = index + 1 @@ -30,5 +31,5 @@ func sqlStatements(query string) []string { if strings.TrimSpace(query[start:]) != "" { statements = append(statements, query[start:]) } - return statements + return statements, nil } diff --git a/server/database_metrics.go b/server/database_metrics.go @@ -5,53 +5,72 @@ import ( "database/sql/driver" "sync/atomic" - "github.com/mattn/go-sqlite3" + "modernc.org/sqlite" ) // Count executions at the driver boundary, including prepared transaction queries. // Transaction control and SQLite's internal trigger statements are not queries. type databaseConnector struct{ queries *atomic.Uint64 } -func (connector databaseConnector) Driver() driver.Driver { return &sqlite3.SQLiteDriver{} } +func (connector databaseConnector) Driver() driver.Driver { return &sqlite.Driver{} } func (connector databaseConnector) Connect(ctx context.Context) (driver.Conn, error) { connection, err := connector.Driver().Open("data/world.sqlite?_foreign_keys=on&_journal_mode=WAL&_synchronous=FULL&_busy_timeout=5000") if err != nil { return nil, err } - return &databaseConnection{connection.(*sqlite3.SQLiteConn), connector.queries}, nil + return &databaseConnection{connection.(sqliteConnection), connector.queries}, nil +} + +// Preserve the driver's context and connection-pool behavior while counting queries. +type sqliteConnection interface { + driver.Conn + driver.ConnBeginTx + driver.ConnPrepareContext + driver.ExecerContext + driver.QueryerContext + driver.Pinger + driver.SessionResetter + driver.Validator + RegisterCommitHook(sqlite.CommitHookFn) } type databaseConnection struct { - *sqlite3.SQLiteConn + sqliteConnection queries *atomic.Uint64 } func (connection *databaseConnection) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { connection.queries.Add(1) - return connection.SQLiteConn.ExecContext(ctx, query, args) + return connection.sqliteConnection.ExecContext(ctx, query, args) } func (connection *databaseConnection) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { connection.queries.Add(1) - return connection.SQLiteConn.QueryContext(ctx, query, args) + return connection.sqliteConnection.QueryContext(ctx, query, args) } func (connection *databaseConnection) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { - statement, err := connection.SQLiteConn.PrepareContext(ctx, query) + statement, err := connection.sqliteConnection.PrepareContext(ctx, query) if err != nil { return nil, err } - return &databaseStatement{statement.(*sqlite3.SQLiteStmt), connection.queries}, nil + return &databaseStatement{statement.(sqliteStatement), connection.queries}, nil +} + +type sqliteStatement interface { + driver.Stmt + driver.StmtExecContext + driver.StmtQueryContext } type databaseStatement struct { - *sqlite3.SQLiteStmt + sqliteStatement queries *atomic.Uint64 } func (statement *databaseStatement) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { statement.queries.Add(1) - return statement.SQLiteStmt.ExecContext(ctx, args) + return statement.sqliteStatement.ExecContext(ctx, args) } func (statement *databaseStatement) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { statement.queries.Add(1) - return statement.SQLiteStmt.QueryContext(ctx, args) + return statement.sqliteStatement.QueryContext(ctx, args) } diff --git a/server/fling_crash_test.go b/server/fling_crash_test.go @@ -81,7 +81,7 @@ func runFlingCrashChild(t *testing.T, mode string) { t.Fatal(err) } err = connection.Raw(func(driver any) error { - driver.(*databaseConnection).RegisterCommitHook(func() int { checkpoint(nil); return 0 }) + driver.(*databaseConnection).RegisterCommitHook(func() int32 { checkpoint(nil); return 0 }) return nil }) connection.Close() diff --git a/server/player_checkpoint_batch_test.go b/server/player_checkpoint_batch_test.go @@ -44,7 +44,7 @@ func TestPlayerCheckpointBatchCommitsOnceAndRetriesTogether(t *testing.T) { } commits := 0 err = connection.Raw(func(driver any) error { - driver.(*databaseConnection).RegisterCommitHook(func() int { commits++; return 0 }) + driver.(*databaseConnection).RegisterCommitHook(func() int32 { commits++; return 0 }) return nil }) connection.Close() diff --git a/server/visitor_names.go b/server/visitor_names.go @@ -5,7 +5,8 @@ import ( "math/rand/v2" "strings" - "github.com/mattn/go-sqlite3" + "modernc.org/sqlite" + "modernc.org/sqlite/lib" ) // CV/CCV beginnings, CV middles and CV/CVC endings keep names at 6-8 letters. @@ -88,8 +89,8 @@ func (coordinator *worldCoordinator) saveVisitor(player *character) error { // The index also protects against a competing database writer after the check. write := &allocatedPlayerWrite{player: prepareBrowserPlayerWrite(player, []byte{}, []byte{})} err := coordinator.commitWrite(write) - var constraint sqlite3.Error - if errors.As(err, &constraint) && constraint.ExtendedCode == sqlite3.ErrConstraintUnique { + var constraint *sqlite.Error + if errors.As(err, &constraint) && constraint.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE { continue } if err == nil {