Based on the original Advertisements plugin version 2.1.2, this fork adds database support for storing advertisement messages.
The plugin now supports storing advertisements in a database (SQLite, MySQL, PostgreSQL, or any SourceMod-compatible database) with automatic fallback to flat files if the database cannot be accessed.
- Plugin version: 2.1.2-db (database-enabled fork)
sm_advertisements_database- Controls whether to use database (1) or flat files (0). Default: 1sm_advertisements_dbconfig- Database config name from databases.cfg. Default: "advertisements"
g_bUseDatabase- Boolean flag indicating if database is currently in useg_hUseDatabase- ConVar handle for database toggleg_hDatabaseConfig- ConVar handle for database config nameg_hDatabase- Database connection handle
-
ConnectToDatabase() - Establishes database connection
- Reads config name from ConVar
- Uses async connection via Database.Connect
- Falls back to flat files on failure
-
OnDatabaseConnected() - Database connection callback
- Handles connection success/failure
- Sets UTF-8 charset for MySQL databases
- Calls CreateDatabaseTables() on success
-
CreateDatabaseTables() - Creates database schema
- Detects database driver (MySQL, SQLite, PostgreSQL, etc.)
- Creates
advertisementstable with driver-specific syntax - Calls LoadAdsFromDatabase() after table creation
-
LoadAdsFromDatabase() - Loads advertisements from database
- Detects database driver for query syntax
- Queries
advertisementstable - Orders by 'order' field, then 'id'
- Only loads enabled advertisements (enabled = 1)
- Automatically falls back to flat files on any error
- Logs database driver type on success
- OnPluginStart() - Added database ConVars and change hooks
- OnConfigsExecuted() - Checks database ConVar and connects to DB or loads flat files
- ConVarChanged_File() - Only reloads if not using database
- ConVarChanged_Database() - New hook to handle database-related ConVar changes
- Command_ReloadAds() - Reloads from DB or files based on mode
For detailed setup and usage instructions, see DATABASE_USAGE.md in the configs folder.
- Configure database connection in
databases.cfg - Initialize database with provided SQL files
- Set ConVars:
sm_advertisements_database 1 - Reload plugin or restart server
The plugin now supports multiple database backends:
- SQLite - Local database file, no additional setup required
- MySQL/MariaDB - Remote or local MySQL server with utf8mb4 charset support
- PostgreSQL - Enterprise PostgreSQL server
- Any SourceMod-compatible database driver
Database connections are configured in addons/sourcemod/configs/databases.cfg:
"advertisements"
{
"driver" "sqlite" // or "mysql", "pgsql", etc.
"database" "advertisements"
// Additional connection parameters for MySQL/PostgreSQL
}
The ConVar sm_advertisements_database controls whether to use the database (1) or flat files (0).
- Stores advertisement messages
- Fields: id, enabled, order, center, chat, hint, menu, top, flags
- Supports all existing advertisement types and features
- Ordered by 'order' field, then 'id'
The plugin falls back to flat files in the following scenarios:
-
Initial Connection Failure
- Database cannot be connected
- Logs error and loads from flat file
-
Table Creation Errors
- Non-critical: logs error but continues
-
Message Loading Errors
- Cannot query advertisements table
- Falls back to flat files
-
ConVar Override
sm_advertisements_databaseset to 0- Uses flat files instead of database
The plugin automatically detects the database driver and adjusts its behavior:
- Uses
INTEGER PRIMARY KEY AUTOINCREMENTfor auto-increment columns - Uses double quotes for reserved word
"order" - No charset configuration needed
- Uses
INT AUTO_INCREMENTfor auto-increment columns - Uses backticks for reserved word
`order` - Automatically sets
utf8mb4charset for emoji support - Uses
InnoDBengine with proper character set - Uses
VARCHAR(64)for key columns instead of TEXT
- Uses standard SQL syntax
- Uses double quotes for reserved words
- Proper data type handling
addons/sourcemod/scripting/advertisements.sp- Main plugin file with multi-database support
addons/sourcemod/configs/advertisements_sqlite.sql- SQLite initialization scriptaddons/sourcemod/configs/advertisements_mysql.sql- MySQL initialization scriptaddons/sourcemod/configs/databases.cfg.example- Database configuration examplesaddons/sourcemod/configs/DATABASE_USAGE.md- User documentationREADME.md- This technical summary
- Fully backwards compatible with original plugin
- Existing installations continue to work with flat files
- New installations default to database mode but fall back if needed
- No breaking changes to existing configurations
- ConVar names are new and don't conflict with original plugin
Original Plugin: ErikMinekus/sm-advertisements
Original Author: Tsunami
Database Fork: Leggers