Skip to content

File Structure

Andrey Klyuev edited this page Aug 13, 2025 · 1 revision

File Structure Documentation

Root Directory

Configuration Files

  • appsettings.json: Server configuration with network settings, spawn coordinates, debug options
  • sphdbsettings.json: Database-specific settings
  • project.godot: Godot engine project configuration
  • SphServer.sln: Visual Studio solution file
  • SphServer.csproj: C# project file with .NET 9.0 target

Data Files

  • MonsterSpawnData.txt: Monster spawn definitions
  • AlchemyResourceSpawnData.txt: Alchemy resource spawn points
  • sphereclient_patched.exe: Client executable

Server Directory (/Server)

Server/SphereServer.cs

Purpose: Main server class managing TCP connections and server initialization.

Methods:

  • override void _Ready(): Initializes server, database, connection handlers
  • override void _Process(double delta): Main server loop handling incoming connections
  • static void InitializeCollections(): Initializes database collections
  • static void SetupTcpServer(): Configures TCP server on specified port

Server/Config/ServerConfig.cs

Purpose: Configuration management with JSON loading and automatic defaults.

Classes:

  • AppConfig: Configuration data model
  • ServerConfig: Static configuration manager

Methods:

  • static AppConfig Get(): Loads configuration from appsettings.json with defaults
  • static void CreateDefaultAppConfig(string configPath): Creates default configuration file
  • static Dictionary<string, string> GetDefaultAppConfigDict(): Returns default values
  • static void SaveAppConfig(string configPath, Dictionary<string, string> config): Saves configuration

Server/Handlers/ConnectionHandler.cs

Purpose: Handles new client connections.

Methods:

  • void Handle(StreamPeerTcp streamPeer): Processes new client connection, creates SphereClient instance

Server/Login/Auth/LoginManager.cs

Purpose: Player authentication and account management.

Methods:

  • static PlayerDbEntry? CheckLoginAndGetPlayer(string login, string password, ushort playerIndex, bool createOnNewLogin = true): Validates login credentials
  • static bool IsNameValid(string name): Checks character name availability
  • static string GetHashedString(string str): Creates PBKDF2 password hash
  • static bool EqualsHashed(string password, string hashedPassword): Verifies password hash

Client Directory (/Client)

Client/SphereClient.cs

Purpose: Connected client management and state handling.

Methods:

  • void Setup(StreamPeerTcp streamPeer, ushort localId): Initializes client with network connection
  • override void _Ready(): Client initialization
  • override void _Process(double delta): Client update loop for packet processing

Client/State/ClientState.cs

Purpose: Client connection states definition.

Enum Values:

  • I_AM_BREAD: Initial connection state
  • INIT_READY_FOR_INITIAL_DATA: Ready for server data
  • INIT_WAITING_FOR_LOGIN_DATA: Awaiting login credentials
  • INIT_WAITING_FOR_CHARACTER_SELECT: Character selection phase
  • INIT_WAITING_FOR_CLIENT_INGAME_ACK: Game entry confirmation
  • INIT_NEW_DUNGEON_TELEPORT_DELAY: Teleport preparation
  • INIT_NEW_DUNGEON_TELEPORT_READY_TO_INIT: Teleport initialization
  • INIT_NEW_DUNGEON_TELEPORT_INITIATED: Teleport execution
  • INGAME_DEFAULT: In-game state

Client/Networking/ClientConnection.cs

Purpose: Client network connection management.

Client/Networking/Handlers/ISphereClientNetworkingHandler.cs

Purpose: Interface for client packet handlers.

BeforeGame Handlers (/Client/Networking/Handlers/BeforeGame)

Client/Networking/Handlers/BeforeGame/BeforeGameHandlers.cs

Purpose: Factory for creating handlers based on client state.

Methods:

  • static ISphereClientNetworkingHandler? GetHandlerForState(ClientState currentState, StreamPeerTcp streamPeerTcp, ushort localId, ClientConnection clientConnection): Returns appropriate handler for current state

Client/Networking/Handlers/BeforeGame/HandshakeHandler.cs

Purpose: Handles initial client handshake (I_AM_BREAD state).

Methods:

  • async Task Handle(double delta): Sends ReadyToLoadInitialData packet and advances to next state

Client/Networking/Handlers/BeforeGame/ServerCredentialsHandler.cs

Purpose: Handles server credential exchange (INIT_READY_FOR_INITIAL_DATA state).

Methods:

  • async Task Handle(double delta): Waits for client data, sends server credentials with client ID

Client/Networking/Handlers/BeforeGame/LoginDataHandler.cs

Purpose: Handles login credential processing (INIT_WAITING_FOR_LOGIN_DATA state).

Methods:

  • async Task Handle(double delta): Processes login credentials, validates with LoginManager, sends character list

Client/Networking/Handlers/BeforeGame/CharacterSelectHandler.cs

Purpose: Handles character selection, creation, deletion (INIT_WAITING_FOR_CHARACTER_SELECT state).

Methods:

  • async Task Handle(double delta): Processes character selection packets, handles create/delete operations

Client/Networking/Handlers/BeforeGame/IngameAckHandler.cs

Purpose: Handles client acknowledgment to enter game (INIT_WAITING_FOR_CLIENT_INGAME_ACK state).

Client/Networking/Handlers/BeforeGame/NewPlayerDungeonHandler.cs

Purpose: Handles new player dungeon teleport initialization.

InGame Handlers (/Client/Networking/Handlers/InGame)

Client/Networking/Handlers/InGame/PingHandler.cs

Purpose: Handles client-server ping/keepalive packets.

Methods:

  • async Task Keepalive(double delta): Manages periodic ping timers (15s, 6s, 3s intervals)
  • async Task Handle(double delta): Processes incoming ping responses and maintains connection

Client/Networking/Handlers/InGame/Chat/ClientChatHandler.cs

Purpose: Handles chat message processing and broadcasting.

Client/Networking/Handlers/InGame/ObjectMovement/MoveObjectForClientHandler.cs

Purpose: Handles object movement synchronization for clients.

Client/Networking/Handlers/InGame/Items/

Purpose: Item interaction handlers.

  • PickupItemHandler.cs: Handles item pickup from ground
  • UseItemHandler.cs: Handles item usage
  • MoveItemHandler.cs: Handles item movement between containers
  • DropItemToGroundHandler.cs: Handles dropping items to ground
  • MainhandTakeItemHandler.cs: Handles taking items to main hand
  • BuyItemFromTargetHandler.cs: Handles item purchase from NPCs

Shared Directory (/Shared)

Shared/Db/DbConnection.cs

Purpose: Database connection management and LiteDB collection access.

Shared/Logger/SphLogger.cs

Purpose: Logging system with console and file output, automatic log rotation.

Methods:

  • static void Initialize(string? filePath, bool enableConsole, bool enableFile, LogLevel minConsoleLevel, LogLevel minFileLevel): Configures logging
  • static void Debug/Info/Warning/Error(string message): Log messages at different levels
  • static void CleanupOldLogFiles(string? logDirectory): Removes old log files, keeps latest 20
  • static string GenerateTimestampedLogPath(string originalPath): Creates timestamped log file names

Shared/GameData/Enums/

  • LootRarity.cs: Item rarity levels
  • MonsterTypes.cs: Monster classification types

Shared/Networking/Packets/

  • Packet.cs: Base packet class
  • PacketPart.cs: Packet component system

Shared/WorldState/

Purpose: Global world state and active entity collections management.

Shared/BitStream/

Purpose: Binary data serialization for network packets.

Godot Directory (/Godot)

Godot/Scenes/

  • MainServer.tscn: Main server scene
  • Client.tscn: Client scene template

Godot/Nodes/

  • Godot-specific node scripts

System Directory (/System)

System/SphEncoding.cs

Purpose: Encoding utilities for text processing.

System/SphRng.cs

Purpose: Random number generation utilities.

System/SphereTimer.cs

Purpose: Timer functionality for game systems.

Sphere.Game Directory (/Sphere.Game)

Sphere.Game/GameObjectDb.cs

Purpose: Game object database management.

Sphere.Game/WorldObject/

Purpose: World object implementations and spawning.

Sphere.Game/Loot/

Purpose: Loot system implementation.

Build Files

  • .gitignore: Git exclusions
  • LICENSE.md: Project license
  • README.md: Project overview
  • logs/: Runtime log files (auto-managed, keeps latest 20)

SphereEmu Wiki

Getting Started

Architecture

Core Components

  • Server
    • SphereServer
    • Connection Handling
    • Authentication
    • Configuration
  • Client
    • SphereClient
    • Client States
    • Networking
  • Shared
    • Database
    • Logging
    • Game Data
    • BitStream

Development

Reference

Clone this wiki locally