Skip to content

Commit

Permalink
Hues.mul in cedserver, cleanup and more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Jan 5, 2024
1 parent 76d5e4e commit 5823ea9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 62 deletions.
14 changes: 2 additions & 12 deletions Server/CEDServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,7 @@ public CEDServer(ConfigRoot config, TextWriter? logOutput = default)
ProtocolVersion = Config.CentrEdPlus ? ProtocolVersion.CentrEDPlus : ProtocolVersion.CentrED;
_logger.LogInfo("Running as " + (Config.CentrEdPlus ? "CentrED+ 0.7.9" : "CentrED 0.6.3"));
Console.CancelKeyPress += ConsoleOnCancelKeyPress;
Landscape = new ServerLandscape
(
this,
Config.Map.MapPath,
Config.Map.Statics,
Config.Map.StaIdx,
Config.Tiledata,
Config.Radarcol,
Config.Map.Width,
Config.Map.Height,
out _valid
);
Landscape = new ServerLandscape(config, _logger, out _valid);
Listener = Bind(new IPEndPoint(IPAddress.Any, Config.Port));
Quit = false;
if (_valid)
Expand Down Expand Up @@ -95,6 +84,7 @@ private Socket Bind(IPEndPoint endPoint)
{
s.Bind(endPoint);
s.Listen(32);
_logger.LogInfo($"Listening on {s.LocalEndPoint}");
return s;
}
catch (Exception e)
Expand Down
3 changes: 2 additions & 1 deletion Server/Config/ConfigRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ public class ConfigRoot
private static string DefaultPath =>
Path.GetFullPath(Path.ChangeExtension(Application.GetCurrentExecutable(), ".xml"));

[XmlIgnore] public const int CurrentVersion = 4;
[XmlIgnore] public const int CurrentVersion = 5;
[XmlAttribute] public int Version { get; set; } = CurrentVersion;
[XmlElement] public bool CentrEdPlus { get; set; }
[XmlElement] public int Port { get; set; } = 2597;
[XmlElement] public Map Map { get; set; } = new();
[XmlElement] public string Tiledata { get; set; } = "tiledata.mul";
[XmlElement] public string Radarcol { get; set; } = "radarcol.mul";
[XmlElement] public string Hues { get; set; } = "hues.mul";
[XmlArray] public List<Account> Accounts { get; set; } = new();
[XmlArray] public List<Region> Regions { get; set; } = new();
[XmlElement] public Autobackup AutoBackup { get; set; } = new();
Expand Down
68 changes: 37 additions & 31 deletions Server/Map/ServerLandscape.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,54 @@
using System.Text;
using CentrED.Network;
using CentrED.Server.Config;
using CentrED.Utility;

namespace CentrED.Server.Map;

public sealed partial class ServerLandscape : BaseLandscape, IDisposable
{
private CEDServer _cedServer;
private Logger _logger;

public ServerLandscape
(
CEDServer cedServer,
string mapPath,
string staticsPath,
string staidxPath,
string tileDataPath,
string radarcolPath,
ushort width,
ushort height,
ConfigRoot config,
Logger logger,
out bool valid
) : base(width, height)
) : base(config.Map.Width, config.Map.Height)
{
_cedServer = cedServer;
_logger = cedServer._logger;

_logger.LogInfo("Loading Map");
if (!File.Exists(mapPath))
_logger = logger;
if (!File.Exists(config.Map.MapPath))
{
Console.WriteLine("Map file not found, do you want to create it? [y/n]");
if (Console.ReadLine() == "y")
{
InitMap(mapPath);
InitMap(config.Map.MapPath);
}
}
_map = File.Open(mapPath, FileMode.Open, FileAccess.ReadWrite);
_map = File.Open(config.Map.MapPath, FileMode.Open, FileAccess.ReadWrite);
_mapReader = new BinaryReader(_map, Encoding.UTF8);
_mapWriter = new BinaryWriter(_map, Encoding.UTF8);
var fi = new FileInfo(mapPath);
var fi = new FileInfo(config.Map.MapPath);
IsUop = fi.Extension == ".uop";
if (IsUop)
{
string uopPattern = fi.Name.Replace(fi.Extension, "").ToLowerInvariant();
ReadUopFiles(uopPattern);
}
_logger.LogInfo($"Loaded {_map.Name}");

_logger.LogInfo($"Loaded {fi.Name}");
if (!File.Exists(staticsPath) || !File.Exists(staidxPath))
if (!File.Exists(config.Map.Statics) || !File.Exists(config.Map.StaIdx))
{
Console.WriteLine("Statics files not found, do you want to create it? [y/n]");
if (Console.ReadLine() == "y")
{
InitStatics(staticsPath, staidxPath);
InitStatics(config.Map.Statics, config.Map.StaIdx);
}
}
_logger.LogInfo("Loading Statics");
_statics = File.Open(staticsPath, FileMode.Open, FileAccess.ReadWrite);
_logger.LogInfo("Loading StaIdx");
_staidx = File.Open(staidxPath, FileMode.Open, FileAccess.ReadWrite);
_statics = File.Open(config.Map.Statics, FileMode.Open, FileAccess.ReadWrite);
_logger.LogInfo($"Loaded {_statics.Name}");
_staidx = File.Open(config.Map.StaIdx, FileMode.Open, FileAccess.ReadWrite);
_logger.LogInfo($"Loaded {_staidx.Name}");
_staticsReader = new BinaryReader(_statics, Encoding.UTF8);
_staticsWriter = new BinaryWriter(_statics, Encoding.UTF8);
_staidxReader = new BinaryReader(_staidx, Encoding.UTF8);
Expand All @@ -66,13 +57,27 @@ out bool valid
valid = Validate();
if (valid)
{
_logger.LogInfo("Loading Tiledata");
TileDataProvider = new TileDataProvider(tileDataPath, true);
TileDataProvider = new TileDataProvider(config.Tiledata);
_logger.LogInfo($"Loaded {config.Tiledata}");
if (File.Exists(config.Hues))
{
if (HueProvider.GetHueCount(config.Hues, out HueCount))
{
_logger.LogInfo($"Loaded {config.Hues}: {HueCount} entries");
}
else
{
_logger.LogInfo($"{config.Hues} not found, using default hue count");
}
}
else
{
_logger.LogInfo($"File {config.Hues} not found, using default hue count");
}
_radarMap = new RadarMap(this, _mapReader, _staidxReader, _staticsReader, config.Radarcol);
_logger.LogInfo($"Loaded {config.Radarcol}");
_logger.LogInfo("Creating Cache");
BlockUnloaded += OnRemovedCachedObject;

_logger.LogInfo("Creating RadarMap");
_radarMap = new RadarMap(this, _mapReader, _staidxReader, _staticsReader, radarcolPath);
PacketHandlers.RegisterPacketHandler(0x06, 8, OnDrawMapPacket);
PacketHandlers.RegisterPacketHandler(0x07, 10, OnInsertStaticPacket);
PacketHandlers.RegisterPacketHandler(0x08, 10, OnDeleteStaticPacket);
Expand Down Expand Up @@ -139,6 +144,7 @@ private void InitStatics(string staticsPath, string staidxPath)
private UopFile[] UopFiles { get; set; } = null!;

public TileDataProvider TileDataProvider { get; } = null!;
private int HueCount = 3000;
private RadarMap _radarMap = null!;

private void OnRemovedCachedObject(Block block)
Expand All @@ -163,7 +169,7 @@ internal void AssertLandTileId(ushort tileId)

internal void AssertHue(ushort hue)
{
if (hue > 3000)
if (hue > HueCount)
throw new ArgumentException($"Invalid hue {hue}");
}

Expand Down
19 changes: 19 additions & 0 deletions Shared/HueProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace CentrED;

public static class HueProvider
{
public static bool GetHueCount(string huePath, out int hueCount)
{
if (File.Exists(huePath))
{
using var file = File.Open(huePath, FileMode.Open, FileAccess.Read, FileShare.Read);
//Header + 8 entries * (colortable + tablestart + tableend + name)
int groupSize = 4 + 8 * (32 * 2 + 2 + 2 + 20);
int entrycount = (int)file.Length / groupSize;
hueCount = entrycount * 8;
return true;
}
hueCount = 3000;
return false;
}
}
30 changes: 12 additions & 18 deletions Shared/TileDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ namespace CentrED;

public class TileDataProvider
{
private FileStream Stream { get; }
private BinaryReader Reader { get; }
public TileDataProvider(String tileDataPath, bool initOnly)
public TileDataProvider(String tileDataPath)
{
Stream = File.Open(tileDataPath, FileMode.Open, FileAccess.Read, FileShare.Read);
Reader = new BinaryReader(Stream, Encoding.UTF8);
Version = Stream.Length >= 3188736 ? TileDataVersion.HighSeas : TileDataVersion.Legacy;
Stream.Position = 0;
var file = File.Open(tileDataPath, FileMode.Open, FileAccess.Read, FileShare.Read);
var reader = new BinaryReader(file, Encoding.UTF8);
Version = file.Length >= 3188736 ? TileDataVersion.HighSeas : TileDataVersion.Legacy;
file.Position = 0;
for (var i = 0; i < 0x4000; i++)
{
//In High Seas, the first header comes AFTER the unknown tile (for whatever reason).
//Therefore special handling is required.
if ((Version == TileDataVersion.Legacy && i % 32 == 0) ||
Version >= TileDataVersion.HighSeas && (i == 1 || (i > 1 && i % 32 == 0)))
{
Stream.Seek(4, SeekOrigin.Current);
file.Seek(4, SeekOrigin.Current);
}

LandTiles[i] = ReadLandTileData(Reader);
LandTiles[i] = ReadLandTileData(reader);
}

var tsize = Version switch
Expand All @@ -33,22 +31,18 @@ public TileDataProvider(String tileDataPath, bool initOnly)
};
var xsize = 4 + 32 * tsize;

var staticCount = (uint)((Stream.Length - Stream.Position) / xsize * 32);
var staticCount = (uint)((file.Length - file.Position) / xsize * 32);
StaticTiles = new StaticTiles[staticCount];
for (var i = 0; i < staticCount; i++)
{
if (i % 32 == 0)
{
Stream.Seek(4, SeekOrigin.Current); // skip header
file.Seek(4, SeekOrigin.Current); // skip header
}
StaticTiles[i] = ReadStaticTileData(Reader);
}

if (initOnly)
{
Reader.Dispose();
Stream.Dispose();
StaticTiles[i] = ReadStaticTileData(reader);
}
reader.Dispose();
file.Dispose();
}

public TileDataVersion Version { get; }
Expand Down

0 comments on commit 5823ea9

Please sign in to comment.