Skip to content

Commit

Permalink
Better support for MOH expansions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom committed Sep 16, 2023
1 parent b888ef7 commit 7bcdf16
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 22 deletions.
32 changes: 30 additions & 2 deletions JKClient/ClientGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum CtfMessageType : int {
public interface IJKClientImport {
internal void OnEntityEvent(EntityEventArgs entityEventArgs);
internal int MaxClients { get; }
public int Protocol { get; }
public void GetCurrentSnapshotNumber(out int snapshotNumber, out int serverTime);
internal bool GetSnapshot(in int snapshotNumber, ref Snapshot snapshot);
internal bool GetServerCommand(in int serverCommandNumber, out Command command);
Expand Down Expand Up @@ -249,11 +250,38 @@ protected virtual void NewClientInfo(int clientNum) {
} else {
var info = new InfoString(configstring);
this.ClientInfo[clientNum].ClientNum = clientNum;
this.ClientInfo[clientNum].Team = (Team)info["t"].Atoi();
if(this is MOHClientGame)
if (isMOH)
{
this.ClientInfo[clientNum].Team = Team.Spectator;
this.ClientInfo[clientNum].IsActiveMOH = true;
if (!string.IsNullOrEmpty(info["team"])) // MOH expansions actually do this :)
{
int mohTeam = info["team"].Atoi();
switch (mohTeam)
{
case 0: // TEAM_NONE
this.ClientInfo[clientNum].IsActiveMOH = false;
this.ClientInfo[clientNum].Team = Team.Spectator;
break;
default:
case 1: // TEAM_SPECTATOR
this.ClientInfo[clientNum].Team = Team.Spectator;
break;
case 2: // TEAM_FREEFORALL
this.ClientInfo[clientNum].Team = Team.Free;
break;
case 3: // TEAM_ALLIES
this.ClientInfo[clientNum].Team = Team.Blue;
break;
case 4: // TEAM_AXIS
this.ClientInfo[clientNum].Team = Team.Red;
break;
}

}
} else
{
this.ClientInfo[clientNum].Team = (Team)info["t"].Atoi();
}
if (info.ContainsKey("skill"))
{
Expand Down
11 changes: 8 additions & 3 deletions JKClient/ClientGames/MOH/MOHClientGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ namespace JKClient
{
public class MOHClientGame : ClientGame
{
private bool isExpansion = false;

public MOHClientGame(IJKClientImport client, int serverMessageNum, int serverCommandSequence, int clientNum)
: base(client, serverMessageNum, serverCommandSequence, clientNum) { }
: base(client, serverMessageNum, serverCommandSequence, clientNum) {
isExpansion = client.Protocol > (int)ProtocolVersion.Protocol8;
}
internal override int GetConfigstringIndex(Configstring index)
{
switch (index)
Expand All @@ -17,7 +21,7 @@ internal override int GetConfigstringIndex(Configstring index)
case Configstring.Players:
return (int)ConfigstringMOH.Players;
case Configstring.LevelStartTime:
return (int)ConfigstringMOH.LevelStartTime;
return isExpansion ? (int)ConfigstringMOH.LevelStartTime : (int)ConfigstringMOH.LevelStartTimeOld;
}
return 0;
}
Expand Down Expand Up @@ -59,7 +63,8 @@ protected override int GetEntityType(EntityType entityType)
}
public enum ConfigstringMOH
{
LevelStartTime = 12,
LevelStartTime = 10,
LevelStartTimeOld = 12,
Sounds = 1076,
Players = 1684
}
Expand Down
2 changes: 2 additions & 0 deletions JKClient/ClientInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
public struct ClientInfo {
public int ClientNum { get; internal set; }
public bool InfoValid { get; internal set; }
public bool IsActiveMOH { get; internal set; } // MOH will set team 0 (TEAM_NONE) for connecting and disconnected players. This reflects that.
public string Name { get; internal set; }
public string Model { get; internal set; }
public string Color1 { get; internal set; }
Expand All @@ -13,6 +14,7 @@ public struct ClientInfo {
internal void Clear() {
this.ClientNum = 0;
this.InfoValid = false;
this.IsActiveMOH = false;
this.Name = null;
this.Model = null;
this.Color1 = null;
Expand Down
8 changes: 4 additions & 4 deletions JKClient/JKClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ private void ConnectionlessPacket(NetAddress address, Message msg) {
this.ServerCommandExecuted?.Invoke(new CommandEventArgs(command, -1));
} else if (string.Compare(c, "print", StringComparison.OrdinalIgnoreCase) == 0) {
if (address == this.serverAddress) {
s = msg.ReadStringAsString((ProtocolVersion)this.Protocol);
s = msg.ReadStringAsString((ProtocolVersion)this.Protocol,true);
var cmd = new Command(new string []{ "print", s });
this.ServerCommandExecuted?.Invoke(new CommandEventArgs(cmd, -1));
Debug.WriteLine(s);
Expand All @@ -1179,7 +1179,7 @@ private void ConnectionlessPacket(NetAddress address, Message msg) {
}
} else if (string.Compare(c, "droperror", StringComparison.OrdinalIgnoreCase) == 0) {
if (address == this.serverAddress) {
s = msg.ReadStringAsString((ProtocolVersion)this.Protocol);
s = msg.ReadStringAsString((ProtocolVersion)this.Protocol,true);
var cmd = new Command(new string []{ "droperror", s });
this.ServerCommandExecuted?.Invoke(new CommandEventArgs(cmd, -1));
Debug.WriteLine(s);
Expand Down Expand Up @@ -1730,8 +1730,8 @@ private unsafe bool StartRecording(DemoName_t demoName,bool timeStampDemoname=fa
msg.WriteByte((int)ServerCommandOperations.Configstring);
msg.WriteShort(i);
len = Common.StrLen(cs);
byte[] bytes = new byte[len];
Marshal.Copy((IntPtr)cs, bytes, 0, len);
byte[] bytes = new byte[len+1];
Marshal.Copy((IntPtr)cs, bytes, 0, len+1);
msg.WriteBigString((sbyte[])(Array)bytes,(ProtocolVersion)this.Protocol);
}
}
Expand Down
10 changes: 5 additions & 5 deletions JKClient/JKClientParse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ private unsafe void ParseSnapshot(in Message msg) {
MessageNum = this.serverMessageSequence
};

Debug.WriteLine(newSnap.ServerTime);
Debug.WriteLine(newSnap.ServerTimeResidual);
//Debug.WriteLine(newSnap.ServerTime);
//Debug.WriteLine(newSnap.ServerTimeResidual);

lock (bufferedDemoMessages)
{
Expand All @@ -482,14 +482,14 @@ private unsafe void ParseSnapshot(in Message msg) {
}

int deltaNum = msg.ReadByte();
Debug.WriteLine(deltaNum);
//Debug.WriteLine(deltaNum);
if (deltaNum == 0) {
newSnap.DeltaNum = -1;
} else {
newSnap.DeltaNum = newSnap.MessageNum - deltaNum;
}
newSnap.Flags = msg.ReadByte();
Debug.WriteLine(newSnap.Flags);
//Debug.WriteLine(newSnap.Flags);
if (newSnap.DeltaNum <= 0) {
newSnap.Valid = true;
oldSnap = null;
Expand Down Expand Up @@ -582,7 +582,7 @@ private unsafe void ParseSnapshot(in Message msg) {

int len = msg.ReadByte();

Debug.WriteLine(len);
//Debug.WriteLine(len);
//if (len > sizeof(byte)*32) {
//oldSnapHandle.Free();
//throw new JKClientException("ParseSnapshot: Invalid size %d for areamask");
Expand Down
14 changes: 6 additions & 8 deletions JKClient/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,10 @@ public unsafe void WriteString(sbyte []s, ProtocolVersion protocol) {
}
if (isMOHWithScrambledString)
{
for (int i = 0; i < l; i++)
for (int i = 0; i <= l; i++)
{
this.WriteByte(StrCharToNetByte[s[i]]);
this.WriteByte(StrCharToNetByte[(byte)s[i]]);
}
this.WriteByte(StrCharToNetByte[0]);
} else
{
byte []b = new byte[l+1];
Expand All @@ -606,11 +605,10 @@ public unsafe void WriteBigString(sbyte []s, ProtocolVersion protocol) { // TODO
}
if (isMOHWithScrambledString)
{
for (int i = 0; i < l; i++)
for (int i = 0; i <= l; i++)
{
this.WriteByte(StrCharToNetByte[s[i]]);
this.WriteByte(StrCharToNetByte[(byte)s[i]]);
}
this.WriteByte(StrCharToNetByte[0]);
} else
{
byte[] b = new byte[l + 1];
Expand Down Expand Up @@ -823,8 +821,8 @@ public sbyte []ReadString(ProtocolVersion protocol,bool forceNonScrambled = fals
}
return str;
}
public string ReadStringAsString(ProtocolVersion protocol) {
sbyte []str = this.ReadString(protocol);
public string ReadStringAsString(ProtocolVersion protocol, bool forceNonScrambled = false) {
sbyte []str = this.ReadString(protocol, forceNonScrambled);
return Common.ToString(str);
}
public sbyte []ReadBigString(bool mohScrambledString) {
Expand Down

0 comments on commit 7bcdf16

Please sign in to comment.