unit Game.Chivalry.MedievalWarfare;

interface
uses
  System.Generics.Collections,

  Game.Base,
  Game.Chivalry,
  Game.Intf;


type
  { I haven't actually done any research into the dedicated server
    for the other Chivalry game, so for now most stuff is implemented
    in the base Chivalry class. }
  TChivalryMedievalWarfareGame = class(TChivalryGame)
  protected
    procedure LoadPredefinedMapList(AList: TList<TGameMap>); override;
  public
    class function GameName: string; override;
    class function AutoDetect: TCustomGame; override;
  end;


implementation
uses
  System.Classes,
  System.IniFiles,
  System.SysUtils,
  System.Win.Registry,
  Winapi.Windows,

  Resources,
  Game.Registry;


const
  SteamKey = '\Software\Valve\Steam';
  SteamValuePath = 'SteamPath';

  ChivalryServerSteamPath = 'steamapps\common\chivalry_ded_server\';


{ TChivalryMedievalWarfareGame }
class function TChivalryMedievalWarfareGame.GameName: string;
begin
  Result := 'Chivalry: Medieval Warfare';
end;


class function TChivalryMedievalWarfareGame.AutoDetect: TCustomGame;
var
  registry: TRegistry;
  steamPath: string;
  serverPath: string;

begin
  Result := nil;
  steamPath := '';

  registry := TRegistry.Create(KEY_READ);
  try
    registry.RootKey := HKEY_CURRENT_USER;
    if registry.OpenKeyReadOnly(SteamKey) then
    try
      if registry.ValueExists(SteamValuePath) then
        steamPath := StringReplace(registry.ReadString(SteamValuePath), '/', '\', [rfReplaceAll]);
    finally
      registry.CloseKey;
    end;
  finally
    FreeAndNil(registry);
  end;

  if (Length(steamPath) > 0) then
  begin
    serverPath := IncludeTrailingPathDelimiter(steamPath) + ChivalryServerSteamPath;
    if DirectoryExists(serverPath) then
      Result := TChivalryMedievalWarfareGame.Create(serverPath);
  end;
end;


procedure TChivalryMedievalWarfareGame.LoadPredefinedMapList(AList: TList<TGameMap>);
var
  mapListFileName: string;
  mapList: TMemIniFile;
  categories: TStringList;
  categoryName: string;
  maps: TStringList;
  mapIndex: Integer;

begin
  mapListFileName := Resources.GetAssetPath(Resources.AssetChivalryMedievalWarfareMapListFileName);
  if not FileExists(mapListFileName) then
    exit;

  mapList := TMemIniFile.Create(mapListFileName);
  try
    categories := nil;
    maps := nil;
    try
      categories := TStringList.Create;
      maps := TStringList.Create;

      mapList.ReadSections(categories);

      for categoryName in categories do
      begin
        maps.Clear;
        mapList.ReadSectionValues(categoryName, maps);

        for mapIndex := 0 to Pred(maps.Count) do
          AList.Add(TGameMap.Create(Trim(maps.Names[mapIndex]),
                                    Trim(maps.ValueFromIndex[mapIndex]),
                                    Trim(categoryName)));
      end;
    finally
      FreeAndNil(maps);
      FreeAndNil(categories);
    end;
  finally
    FreeAndNil(mapList);
  end;
end;


initialization
  TGameRegistry.Register(TChivalryMedievalWarfareGame);

end.
