CDN Integration

This commit is contained in:
dingo
2025-12-19 10:12:17 -05:00
parent 351478fedb
commit 260e6fa072
11 changed files with 2316 additions and 63 deletions
+52 -17
View File
@@ -80,7 +80,9 @@ public sealed class DungeonHandler : IClientMessageHandler
// Dungeon browser
DungeonOpCodes.DUNGEON_LIST_REQUEST,
DungeonOpCodes.DUNGEON_CREATE,
DungeonOpCodes.DUNGEON_JOIN
DungeonOpCodes.DUNGEON_JOIN,
// Asset management
DungeonOpCodes.ASSET_MANIFEST_REQUEST
};
public async Task InitializeAsync()
@@ -228,6 +230,9 @@ public sealed class DungeonHandler : IClientMessageHandler
case DungeonOpCodes.DUNGEON_JOIN:
await HandleDungeonJoin(connection, session, payload, ct);
break;
case DungeonOpCodes.ASSET_MANIFEST_REQUEST:
await HandleAssetManifestRequest(connection, ct);
break;
}
}
catch (Exception ex)
@@ -783,7 +788,7 @@ public sealed class DungeonHandler : IClientMessageHandler
var emptyTime = DateTime.UtcNow - instance.LastPlayerActivity;
// If boss not killed and empty for 10+ minutes, persist to database
if (!instance.BossKilled && !instance.IsPersisted && emptyTime.TotalMinutes >= 5)
if (!instance.BossKilled && !instance.IsPersisted && emptyTime.TotalMinutes >= 10)
{
_ = PersistDungeonAsync(instance, CancellationToken.None);
_dungeons.TryRemove(dungeonId, out _);
@@ -1614,6 +1619,38 @@ public sealed class DungeonHandler : IClientMessageHandler
#region Dungeon Browser Handlers
/// <summary>
/// Handle request for asset manifest (list of all CDN files for hot-update)
/// </summary>
private async Task HandleAssetManifestRequest(IConnection connection, CancellationToken ct)
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
var assets = await db.ClientAssets
.Where(a => a.IsEnabled)
.Select(a => new
{
filePath = a.FilePath,
assetType = (int)a.AssetType,
fileHash = a.FileHash,
fileSize = a.FileSize,
version = a.Version,
isRequired = a.IsRequired,
minClientVersion = a.MinClientVersion,
cdnPath = a.CdnPath ?? a.FilePath
})
.ToListAsync(ct);
await SendAsync(connection, DungeonOpCodes.ASSET_MANIFEST_RESPONSE, new
{
cdnBaseUrl = "https://assets.aeternalisgames.com/downloads/",
assets = assets
}, ct);
_log.LogInformation("Sent asset manifest with {Count} files", assets.Count);
}
/// <summary>
/// Handle request for list of available dungeons
/// </summary>
@@ -1905,10 +1942,9 @@ public sealed class DungeonHandler : IClientMessageHandler
y = m.Y,
health = m.Health,
maxHealth = m.MaxHealth,
spriteSheet = m.SpriteSheet,
spriteWidth = m.SpriteWidth,
spriteHeight = m.SpriteHeight,
tintColor = m.TintColor
scenePath = m.ScenePath,
tintColor = m.TintColor,
scale = m.Scale
}),
chests = instance.Simulation.Chests.Values.Select(c => new
{
@@ -2022,13 +2058,13 @@ public sealed class DungeonHandler : IClientMessageHandler
instance.Simulation.DroppedItems.Clear();
// Reset portals (exit portal should be inactive until boss killed)
//foreach (var portal in instance.Simulation.Portals.Values)
//{
// if (portal.Type == PortalType.Exit)
// {
// portal.IsActive = false;
// }
//}
foreach (var portal in instance.Simulation.Portals.Values)
{
if (portal.Type == PortalType.Exit)
{
portal.IsActive = false;
}
}
_log.LogDebug("Respawned {MobCount} mobs in dungeon {Id}", instance.Simulation.Mobs.Count, instance.Id);
}
@@ -2455,10 +2491,9 @@ public sealed class DungeonHandler : IClientMessageHandler
health = m.Health,
maxHealth = m.MaxHealth,
state = m.State.ToString().ToLower(),
spriteSheet = m.SpriteSheet,
spriteWidth = m.SpriteWidth,
spriteHeight = m.SpriteHeight,
tintColor = m.TintColor
scenePath = m.ScenePath,
tintColor = m.TintColor,
scale = m.Scale
}),
chests = sim.Chests.Values.Select(c => new
+2 -3
View File
@@ -123,10 +123,9 @@ public class DungeonSimulation
MaxHealth = stats.MaxHealth,
Stats = stats,
State = MobState.Idle,
SpriteSheet = mobDef.SpriteSheet,
SpriteWidth = mobDef.SpriteWidth,
SpriteHeight = mobDef.SpriteHeight,
ScenePath = mobDef.ScenePath,
TintColor = mobDef.TintColor,
Scale = mobDef.Scale,
Type = mobDef.Type
};
}
+3 -4
View File
@@ -46,11 +46,10 @@ public class MobInstance
public ulong? TargetId { get; set; }
public long LastAttackTick { get; set; }
// Visual
public string SpriteSheet { get; set; } = "";
public int SpriteWidth { get; set; } = 32;
public int SpriteHeight { get; set; } = 32;
// Visual - CDN scene path
public string? ScenePath { get; set; }
public string TintColor { get; set; } = "#ffffff";
public float Scale { get; set; } = 1.0f;
// Carried loot from player kills
public List<CarriedLoot> CarriedPlayerLoot { get; set; } = new();
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,161 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SystemRevivalServer.Migrations
{
/// <inheritdoc />
public partial class CDNIntegration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AttackFrames",
table: "dungeon_mob_definitions");
migrationBuilder.DropColumn(
name: "DeathFrames",
table: "dungeon_mob_definitions");
migrationBuilder.DropColumn(
name: "IdleFrames",
table: "dungeon_mob_definitions");
migrationBuilder.DropColumn(
name: "SpriteIndex",
table: "dungeon_mob_definitions");
migrationBuilder.DropColumn(
name: "WalkFrames",
table: "dungeon_mob_definitions");
migrationBuilder.AddColumn<float>(
name: "Scale",
table: "dungeon_mob_definitions",
type: "float",
nullable: false,
defaultValue: 0f);
migrationBuilder.AddColumn<string>(
name: "ScenePath",
table: "dungeon_mob_definitions",
type: "varchar(256)",
maxLength: 256,
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<string>(
name: "SpritePath",
table: "dungeon_item_definitions",
type: "varchar(256)",
maxLength: 256,
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<string>(
name: "TintColor",
table: "dungeon_item_definitions",
type: "varchar(16)",
maxLength: 16,
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "client_assets",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
FilePath = table.Column<string>(type: "varchar(256)", maxLength: 256, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
AssetType = table.Column<int>(type: "int", nullable: false),
FileHash = table.Column<string>(type: "varchar(32)", maxLength: 32, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
FileSize = table.Column<long>(type: "bigint", nullable: false),
Version = table.Column<int>(type: "int", nullable: false),
IsRequired = table.Column<bool>(type: "tinyint(1)", nullable: false),
MinClientVersion = table.Column<string>(type: "varchar(16)", maxLength: 16, nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
CdnPath = table.Column<string>(type: "varchar(256)", maxLength: 256, nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
UpdatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
IsEnabled = table.Column<bool>(type: "tinyint(1)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_client_assets", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "client_assets");
migrationBuilder.DropColumn(
name: "Scale",
table: "dungeon_mob_definitions");
migrationBuilder.DropColumn(
name: "ScenePath",
table: "dungeon_mob_definitions");
migrationBuilder.DropColumn(
name: "SpritePath",
table: "dungeon_item_definitions");
migrationBuilder.DropColumn(
name: "TintColor",
table: "dungeon_item_definitions");
migrationBuilder.AddColumn<string>(
name: "AttackFrames",
table: "dungeon_mob_definitions",
type: "varchar(256)",
maxLength: 256,
nullable: false,
defaultValue: "")
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<string>(
name: "DeathFrames",
table: "dungeon_mob_definitions",
type: "varchar(256)",
maxLength: 256,
nullable: false,
defaultValue: "")
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<string>(
name: "IdleFrames",
table: "dungeon_mob_definitions",
type: "varchar(256)",
maxLength: 256,
nullable: false,
defaultValue: "")
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddColumn<int>(
name: "SpriteIndex",
table: "dungeon_mob_definitions",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<string>(
name: "WalkFrames",
table: "dungeon_mob_definitions",
type: "varchar(256)",
maxLength: 256,
nullable: false,
defaultValue: "")
.Annotation("MySql:CharSet", "utf8mb4");
}
}
}
+67 -23
View File
@@ -81,6 +81,58 @@ namespace SystemRevivalServer.Migrations
b.ToTable("accounts", (string)null);
});
modelBuilder.Entity("Server.Data.Entities.ClientAsset", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<int>("AssetType")
.HasColumnType("int");
b.Property<string>("CdnPath")
.HasMaxLength(256)
.HasColumnType("varchar(256)");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<string>("FileHash")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("varchar(32)");
b.Property<string>("FilePath")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("varchar(256)");
b.Property<long>("FileSize")
.HasColumnType("bigint");
b.Property<bool>("IsEnabled")
.HasColumnType("tinyint(1)");
b.Property<bool>("IsRequired")
.HasColumnType("tinyint(1)");
b.Property<string>("MinClientVersion")
.HasMaxLength(16)
.HasColumnType("varchar(16)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("datetime(6)");
b.Property<int>("Version")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("client_assets");
});
modelBuilder.Entity("Server.Data.Entities.DeathChest", b =>
{
b.Property<ulong>("Id")
@@ -425,6 +477,10 @@ namespace SystemRevivalServer.Migrations
b.Property<int>("SpriteId")
.HasColumnType("int");
b.Property<string>("SpritePath")
.HasMaxLength(256)
.HasColumnType("varchar(256)");
b.Property<bool>("Stackable")
.HasColumnType("tinyint(1)");
@@ -434,6 +490,10 @@ namespace SystemRevivalServer.Migrations
b.Property<int?>("TeachesSpellId")
.HasColumnType("int");
b.Property<string>("TintColor")
.HasMaxLength(16)
.HasColumnType("varchar(16)");
b.Property<int>("Type")
.HasColumnType("int");
@@ -584,11 +644,6 @@ namespace SystemRevivalServer.Migrations
b.Property<float>("ArmorPerTier")
.HasColumnType("float");
b.Property<string>("AttackFrames")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("varchar(256)");
b.Property<float>("AttackRange")
.HasColumnType("float");
@@ -622,11 +677,6 @@ namespace SystemRevivalServer.Migrations
b.Property<float>("DamagePerTier")
.HasColumnType("float");
b.Property<string>("DeathFrames")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("varchar(256)");
b.Property<string>("Description")
.IsRequired()
.HasMaxLength(256)
@@ -638,11 +688,6 @@ namespace SystemRevivalServer.Migrations
b.Property<float>("HealthPerTier")
.HasColumnType("float");
b.Property<string>("IdleFrames")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("varchar(256)");
b.Property<bool>("IsEnabled")
.HasColumnType("tinyint(1)");
@@ -671,10 +716,14 @@ namespace SystemRevivalServer.Migrations
.HasMaxLength(1024)
.HasColumnType("varchar(1024)");
b.Property<int>("SpriteHeight")
.HasColumnType("int");
b.Property<float>("Scale")
.HasColumnType("float");
b.Property<int>("SpriteIndex")
b.Property<string>("ScenePath")
.HasMaxLength(256)
.HasColumnType("varchar(256)");
b.Property<int>("SpriteHeight")
.HasColumnType("int");
b.Property<string>("SpriteSheet")
@@ -693,11 +742,6 @@ namespace SystemRevivalServer.Migrations
b.Property<int>("Type")
.HasColumnType("int");
b.Property<string>("WalkFrames")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("varchar(256)");
b.Property<float>("XpPerTier")
.HasColumnType("float");
+4
View File
@@ -21,6 +21,10 @@ public static class DungeonOpCodes
public const int DUNGEON_LIST_RESPONSE = 311;
public const int DUNGEON_CREATE = 312;
public const int DUNGEON_JOIN = 313;
// Asset Management (320-329)
public const int ASSET_MANIFEST_REQUEST = 320;
public const int ASSET_MANIFEST_RESPONSE = 321;
// Input (400-449)
public const int DUNGEON_INPUT_MOVE = 400;
+1
View File
@@ -34,6 +34,7 @@ public sealed class GameDbContext : DbContext
public DbSet<DeathChest> DeathChests => Set<DeathChest>();
public DbSet<DeathChestItem> DeathChestItems => Set<DeathChestItem>();
public DbSet<DungeonPersistence> DungeonInstances => Set<DungeonPersistence>();
public DbSet<ClientAsset> ClientAssets => Set<ClientAsset>();
protected override void OnModelCreating(ModelBuilder b)
{
b.ApplyConfigurationsFromAssembly(typeof(GameDbContext).Assembly);
+111
View File
@@ -0,0 +1,111 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Server.Data.Entities;
/// <summary>
/// Tracks all client-side files that can be hot-updated from CDN.
/// Files are stored at: https://assets.aeternalisgames.com/downloads/{FilePath}
/// </summary>
[Table("client_assets")]
public class ClientAsset
{
[Key]
public int Id { get; set; }
/// <summary>
/// Path relative to client root (e.g., "scenes/mobs/skeleton.tscn", "scripts/ui/hud.gd")
/// This is where the file will be saved on the client
/// </summary>
[Required]
[MaxLength(256)]
public string FilePath { get; set; } = "";
/// <summary>
/// Type of asset for categorization and loading behavior
/// </summary>
public ClientAssetType AssetType { get; set; } = ClientAssetType.Texture;
/// <summary>
/// MD5 hash of file contents for change detection
/// </summary>
[Required]
[MaxLength(32)]
public string FileHash { get; set; } = "";
/// <summary>
/// File size in bytes (for download progress)
/// </summary>
public long FileSize { get; set; } = 0;
/// <summary>
/// Version number - increment when file changes
/// </summary>
public int Version { get; set; } = 1;
/// <summary>
/// If true, must be downloaded before game can start
/// If false, can be lazy-loaded when needed
/// </summary>
public bool IsRequired { get; set; } = false;
/// <summary>
/// Minimum client version required to use this asset
/// Clients below this version will be told to update
/// Format: "1.0.0"
/// </summary>
[MaxLength(16)]
public string? MinClientVersion { get; set; }
/// <summary>
/// Optional CDN path if different from FilePath
/// If null, uses FilePath as CDN path
/// </summary>
[MaxLength(256)]
public string? CdnPath { get; set; }
/// <summary>
/// When the file was last updated
/// </summary>
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
/// <summary>
/// When the file was first added
/// </summary>
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public bool IsEnabled { get; set; } = true;
/// <summary>
/// Get the CDN download path
/// </summary>
[NotMapped]
public string DownloadPath => CdnPath ?? FilePath;
}
public enum ClientAssetType
{
/// <summary>Scene file (.tscn) - mobs, UI, effects</summary>
Scene = 0,
/// <summary>GDScript file (.gd)</summary>
Script = 1,
/// <summary>Image file (.png, .jpg, .webp)</summary>
Texture = 2,
/// <summary>Resource file (.tres) - themes, materials, etc.</summary>
Resource = 3,
/// <summary>Audio file (.ogg, .wav, .mp3)</summary>
Audio = 4,
/// <summary>Shader file (.gdshader)</summary>
Shader = 5,
/// <summary>Font file (.ttf, .otf)</summary>
Font = 6,
/// <summary>JSON data file</summary>
Data = 7
}
+14
View File
@@ -64,6 +64,20 @@ public class DungeonItemDefinition
public int MaxStack { get; set; } = 1;
// Visuals
/// <summary>
/// Path to the item's sprite image on CDN (e.g., "textures/items/sword_iron.png")
/// If null/empty, client uses fallback placeholder based on item type
/// </summary>
[MaxLength(256)]
public string? SpritePath { get; set; }
/// <summary>
/// Optional color tint as hex (e.g., "#ff0000" for red tint)
/// </summary>
[MaxLength(16)]
public string? TintColor { get; set; }
// Legacy field - kept for backwards compatibility
public int SpriteId { get; set; } = 0;
public bool IsEnabled { get; set; } = true;
+21 -16
View File
@@ -68,26 +68,31 @@ public class DungeonMobDefinition
[MaxLength(1024)]
public string AbilitiesJson { get; set; } = "{}";
// Visuals - reference to sprite/animation
// Visuals
/// <summary>
/// Path to the mob's .tscn scene file on CDN (e.g., "scenes/mobs/skeleton.tscn")
/// If null/empty, client uses fallback placeholder shape
/// </summary>
[MaxLength(256)]
public string? ScenePath { get; set; }
/// <summary>
/// Color tint applied to the mob (hex, e.g., "#ff0000" for red variant)
/// </summary>
[MaxLength(16)]
public string TintColor { get; set; } = "#ffffff";
/// <summary>
/// Scale multiplier for the mob (1.0 = normal size)
/// </summary>
public float Scale { get; set; } = 1.0f;
// Legacy fields - kept for backwards compatibility, ScenePath takes priority
[MaxLength(128)]
public string SpriteSheet { get; set; } = "mobs/skeleton.png";
public int SpriteIndex { get; set; } = 0; // Index in spritesheet
public string SpriteSheet { get; set; } = "";
public int SpriteWidth { get; set; } = 32;
public int SpriteHeight { get; set; } = 32;
[MaxLength(16)]
public string TintColor { get; set; } = "#ffffff"; // Color tint for variants
// Animation frames (JSON array of frame indices)
[MaxLength(256)]
public string IdleFrames { get; set; } = "[0]";
[MaxLength(256)]
public string WalkFrames { get; set; } = "[0,1,2,3]";
[MaxLength(256)]
public string AttackFrames { get; set; } = "[4,5]";
[MaxLength(256)]
public string DeathFrames { get; set; } = "[6,7]";
public float AnimationSpeed { get; set; } = 8f; // Frames per second
public bool IsEnabled { get; set; } = true;