Skip to content

Commit

Permalink
Added some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagord committed Mar 25, 2023
1 parent 07d33fd commit ab5a85d
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new CodeInstruction(OpCodes.Callvirt, AccessTools.Method(typeof(ChatCommandRouter), "FindAndExecute", null, null)),
new CodeInstruction(OpCodes.Brtrue, afterIf),
new CodeInstruction(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(String), "Empty")),
new CodeInstruction(OpCodes.Ldsfld, AccessTools.Field(typeof(string), "Empty")),
new CodeInstruction(OpCodes.Stfld, AccessTools.Field(typeof(PLNetworkManager), "CurrentChatText")),
new CodeInstruction(OpCodes.Nop),
};
Expand Down
75 changes: 72 additions & 3 deletions PulsarModLoader/MPModChecks/MPModCheckManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,25 @@

namespace PulsarModLoader.MPModChecks
{
/// <summary>
/// Manages Mod Checks.
/// </summary>
public class MPModCheckManager
{
/// <summary>
/// Instantiates the ModCheckManager.
/// </summary>
public MPModCheckManager()
{
Instance = this;
RefreshData();
ModManager.Instance.OnModUnloaded += RefreshData;
}

/// <summary>
/// Updates modlists
/// </summary>
/// <param name="mod"></param>
public void RefreshData(PulsarMod mod = null)
{
UpdateMyModList();
Expand All @@ -40,16 +50,27 @@ private void UpdateLobbyModList() //Update Photon Lobby Listing with mod list
}
}

/// <summary>
/// Static instance of the ModCheckManager
/// </summary>
public static MPModCheckManager Instance = null;

private MPModDataBlock[] MyModList = null;

/// <summary>
/// List of clients that have requested mod lists of other clients.
/// </summary>
public List<PhotonPlayer> RequestedModLists = new List<PhotonPlayer>();

private Dictionary<PhotonPlayer, MPUserDataBlock> NetworkedPeersModLists = new Dictionary<PhotonPlayer, MPUserDataBlock>();

private int HighestLevelOfMPMods = 0;

/// <summary>
/// Gets full mod list of Networked Peer.
/// </summary>
/// <param name="Photonplayer"></param>
/// <returns></returns>
public MPUserDataBlock GetNetworkedPeerMods(PhotonPlayer Photonplayer)
{
if (NetworkedPeersModLists.TryGetValue(Photonplayer, out MPUserDataBlock value))
Expand All @@ -62,6 +83,12 @@ public MPUserDataBlock GetNetworkedPeerMods(PhotonPlayer Photonplayer)
}
}

/// <summary>
/// Checks if given player has mod, checked by HarmonyID
/// </summary>
/// <param name="player"></param>
/// <param name="HarmonyIdentifier"></param>
/// <returns>Returns true if player has mod</returns>
public bool NetworkedPeerHasMod(PhotonPlayer player, string HarmonyIdentifier)
{
MPUserDataBlock userData = GetNetworkedPeerMods(player);
Expand All @@ -78,6 +105,11 @@ public bool NetworkedPeerHasMod(PhotonPlayer player, string HarmonyIdentifier)
return false;
}

/// <summary>
/// Finds all Networked Peers with a given harmony ID
/// </summary>
/// <param name="HarmonyIdentifier"></param>
/// <returns>NetworkedPeers using given mod</returns>
public List<PhotonPlayer> NetworkedPeersWithMod(string HarmonyIdentifier)
{
List<PhotonPlayer> playerList = new List<PhotonPlayer>();
Expand All @@ -94,6 +126,11 @@ public List<PhotonPlayer> NetworkedPeersWithMod(string HarmonyIdentifier)
return playerList;
}

/// <summary>
/// Adds a player's mod list to the local NetworkedPeersModLists
/// </summary>
/// <param name="Photonplayer"></param>
/// <param name="modList"></param>
public void AddNetworkedPeerMods(PhotonPlayer Photonplayer, MPUserDataBlock modList)
{
if (NetworkedPeersModLists.ContainsKey(Photonplayer))
Expand All @@ -104,6 +141,10 @@ public void AddNetworkedPeerMods(PhotonPlayer Photonplayer, MPUserDataBlock modL
NetworkedPeersModLists.Add(Photonplayer, modList);
}

/// <summary>
/// Clears player from NetworkedPeersModLists
/// </summary>
/// <param name="Photonplayer"></param>
public void RemoveNetworkedPeerMods(PhotonPlayer Photonplayer)
{
NetworkedPeersModLists.Remove(Photonplayer);
Expand Down Expand Up @@ -152,6 +193,10 @@ private void UpdateMyModList()
Logger.Info("Finished Building MyModList, time elapsted: " + stopwatch.ElapsedMilliseconds.ToString());
}

/// <summary>
/// Serilizes user data into a byte array for network transfer. Does not contain a hash
/// </summary>
/// <returns>Serilized User data (Hashless)</returns>
public byte[] SerializeHashlessUserData()
{
MemoryStream dataStream = new MemoryStream();
Expand All @@ -172,7 +217,11 @@ public byte[] SerializeHashlessUserData()
}
return dataStream.ToArray();
}


/// <summary>
/// Serilizes user data into a byte array for network transfer. Contains a hash.
/// </summary>
/// <returns>Serilized User data (Hashfull)</returns>
public byte[] SerializeHashfullUserData()
{
MemoryStream dataStream = new MemoryStream();
Expand All @@ -195,6 +244,11 @@ public byte[] SerializeHashfullUserData()
return dataStream.ToArray();
}

/// <summary>
/// Deserializes bytes representing a serialized MPUserDataBlock which does not contain a hash.
/// </summary>
/// <param name="byteData"></param>
/// <returns>MPUserDataBlock (Hashless)</returns>
public static MPUserDataBlock DeserializeHashlessMPUserData(byte[] byteData)
{
MemoryStream memoryStream = new MemoryStream(byteData);
Expand Down Expand Up @@ -227,6 +281,11 @@ public static MPUserDataBlock DeserializeHashlessMPUserData(byte[] byteData)
}
}

/// <summary>
/// Deserializes bytes representing a serialized MPUserDataBlock containing a hash.
/// </summary>
/// <param name="byteData"></param>
/// <returns>MPUserDataBlock (Hashfull)</returns>
public static MPUserDataBlock DeserializeHashfullMPUserData(byte[] byteData)
{
MemoryStream memoryStream = new MemoryStream(byteData);
Expand Down Expand Up @@ -260,6 +319,11 @@ public static MPUserDataBlock DeserializeHashfullMPUserData(byte[] byteData)

}

/// <summary>
/// Converts a ModDataBlock array to a string list, Ussually for logging purposes.
/// </summary>
/// <param name="ModDatas"></param>
/// <returns>Converts ModDataBLocks to a string list.</returns>
public static string GetModListAsString(MPModDataBlock[] ModDatas)
{
string ModList = string.Empty;
Expand Down Expand Up @@ -296,10 +360,10 @@ private static void KickClient(PhotonPlayer client)
}

/// <summary>
/// overall basic description: checks if it is possible to join room based on mods installed locally and on the server
/// Upon attempting to join a room, the client checks if it is allowed to join based on mods installed locally and on the server
/// </summary>
/// <param name="room"></param>
/// <returns></returns>
/// <returns>Returns true if client can connect to host.</returns>
public bool ClientClickJoinRoom(RoomInfo room)
{
MPUserDataBlock HostModData = GetHostModList(room);
Expand Down Expand Up @@ -401,6 +465,11 @@ public bool ClientClickJoinRoom(RoomInfo room)
}
}

/// <summary>
/// Run by host on client connect to compare mod info.
/// </summary>
/// <param name="Player"></param>
/// <returns>Returns true if client should be allowed to join.</returns>
public bool HostOnClientJoined(PhotonPlayer Player)
{
MPModDataBlock[] ClientMods = null;
Expand Down
33 changes: 33 additions & 0 deletions PulsarModLoader/ModManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace PulsarModLoader
{
/// <summary>
/// Manages all mods.
/// </summary>
public class ModManager
{
public static bool IsOldVersion;
Expand All @@ -29,6 +32,9 @@ public class ModManager

private static ModManager _instance = null;

/// <summary>
/// Static instance of the mod manager.
/// </summary>
public static ModManager Instance
{
get
Expand All @@ -42,6 +48,9 @@ public static ModManager Instance
}
}

/// <summary>
/// Manages all mods.
/// </summary>
public ModManager()
{
Logger.Info($"Starting {PMLVersionInfo.ProductName} v{PMLVersionInfo.FileVersion}");
Expand Down Expand Up @@ -77,6 +86,11 @@ public ModManager()
#endif
}

/// <summary>
/// Gets the PulsarMod Class of the given mod.
/// </summary>
/// <param name="name"></param>
/// <returns>True if Loaded</returns>
public PulsarMod GetMod(string name)
{
if (activeMods.TryGetValue(name, out PulsarMod mod))
Expand All @@ -89,16 +103,29 @@ public PulsarMod GetMod(string name)
}
}

/// <summary>
/// Checks if given mod is loaded.
/// </summary>
/// <param name="name"></param>
/// <returns>Returns true if loaded</returns>
public bool IsModLoaded(string name)
{
return activeMods.ContainsKey(name);
}

/// <summary>
/// Returns an IEnumerable of all loaded PulsarMods.
/// </summary>
/// <returns></returns>
public IEnumerable<PulsarMod> GetAllMods()
{
return activeMods.Values;
}

/// <summary>
/// Loads all mods found in the given directory
/// </summary>
/// <param name="modsDir"></param>
public void LoadModsDirectory(string modsDir)
{
OnModSuccessfullyLoaded += Events.EventHelper.RegisterEventHandlers;
Expand Down Expand Up @@ -166,6 +193,12 @@ private Assembly ResolveModsDirectory(object sender, ResolveEventArgs args)
return null;
}

/// <summary>
/// Loads a mod by assemblypath.
/// </summary>
/// <param name="assemblyPath"></param>
/// <returns>PulsarMod of loaded mod</returns>
/// <exception cref="IOException"></exception>
public PulsarMod LoadMod(string assemblyPath)
{

Expand Down
3 changes: 3 additions & 0 deletions PulsarModLoader/ModMessage/ModMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace PulsarModLoader
{
/// <summary>
/// Abstract class for ModMessages.
/// </summary>
public abstract class ModMessage
{
/// <summary>
Expand Down
Loading

0 comments on commit ab5a85d

Please sign in to comment.