using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xna.Framework; using Revitalize.Framework.Objects; using StardewValley; namespace Revitalize.Framework.Utilities { /// /// Deals with syncing objects in multiplayer. /// public static class MultiplayerUtilities { public static string RequestGUIDMessage = "Revitalize.RequestGUIDObject"; public static string RequestGUIDMessage_Tile = "Revitalize.RequestGUIDObject_Tile"; public static string ReceieveGUIDMessage = "Revitalize.ReceieveGUIDObject"; public static string ReceieveGUIDMessage_Tile = "Revitalize.ReceieveGUIDObject_Tile"; public static string RequestALLModObjects = "Revitalize.EndOfDayRequestAllObjects"; public static string RequestObjectUpdateSync = "Revitalize.RequestObjectUpdateSync"; public static Multiplayer GameMultiplayer; /// /// Handles receiving mod messages. /// /// /// public static void GetModMessage(object o, StardewModdingAPI.Events.ModMessageReceivedEventArgs e) { //ModCore.log("Get a mod message: "+e.Type); if (e.Type.Equals(RequestGUIDMessage)) { //ModCore.log("Send GUID Request"); Guid request = Guid.Parse(e.ReadAs()); SendGuidObject(request); } if (e.Type.Equals(ReceieveGUIDMessage)) { //ModCore.log("Receieve GUID Request"); string objStr = e.ReadAs (); CustomObject v=(CustomObject)ModCore.Serializer.DeserializeFromJSONString(objStr); if (ModCore.CustomObjects.ContainsKey((v as CustomObject).guid) == false) { ModCore.CustomObjects.Add((v as CustomObject).guid, v); v.info.forceUpdate(); v.updateInfo(); } else { ModCore.CustomObjects[(v as CustomObject).guid] = v; v.info.forceUpdate(); v.updateInfo(); } } if(e.Type.Equals(RequestGUIDMessage_Tile)) { //odCore.log("Send GUID Request FOR TILE"); Guid request = Guid.Parse(e.ReadAs()); SendGuidObject_Tile(request); } if(e.Type.Equals(ReceieveGUIDMessage_Tile)) { //ModCore.log("Receieve GUID Request FOR TILE"); string objStr = e.ReadAs(); CustomObject v =(CustomObject)ModCore.Serializer.DeserializeFromJSONString(objStr); if (ModCore.CustomObjects.ContainsKey((v as CustomObject).guid) == false) { ModCore.CustomObjects.Add((v as CustomObject).guid, v); v.info.forceUpdate(); v.updateInfo(); } else { ModCore.CustomObjects[(v as CustomObject).guid] = v; v.info.forceUpdate(); v.updateInfo(); } } if (e.Type.Equals(RequestALLModObjects)) { List < KeyValuePair > list = ModCore.CustomObjects.ToList(); foreach(var v in list) { (v.Value).updateInfo(); SendGuidObject(v.Key); } } if (e.Type.Equals(RequestObjectUpdateSync)) { string guidString = e.ReadAs(); Guid guid = Guid.Parse(guidString); if (ModCore.CustomObjects.ContainsKey(guid)) { ModCore.CustomObjects[guid].getUpdate(); } } } /// /// Sends a custom object to be synced. /// /// public static void SendGuidObject(Guid request) { if (ModCore.CustomObjects.ContainsKey(request)) { //ModCore.log("Send guid request: "+request.ToString()); //ModCore.CustomObjects[request].forceUpdate(); ModCore.CustomObjects[request].info.forceUpdate(); ModCore.CustomObjects[request].updateInfo(); ModCore.ModHelper.Multiplayer.SendMessage(ModCore.Serializer.ToJSONString(ModCore.CustomObjects[request]), ReceieveGUIDMessage, new string[] { Revitalize.ModCore.Manifest.UniqueID.ToString() }); } else { ModCore.log("This mod doesn't have the guid object"); } } /// /// Sends the container object from the tile component object's guid. /// /// public static void SendGuidObject_Tile(Guid request) { if (ModCore.CustomObjects.ContainsKey(request)) { //ModCore.log("Send guid tile request!"); //(ModCore.CustomObjects[request] as MultiTiledComponent).forceUpdate(); //(ModCore.CustomObjects[request] as MultiTiledComponent).containerObject.forceUpdate(); try { (ModCore.CustomObjects[request] as MultiTiledComponent).containerObject.updateInfo(); } catch(Exception err) { } ModCore.ModHelper.Multiplayer.SendMessage(ModCore.Serializer.ToJSONString( (ModCore.CustomObjects[request] as MultiTiledComponent).containerObject), ReceieveGUIDMessage_Tile , new string[] { Revitalize.ModCore.Manifest.UniqueID.ToString() }); } else { //ModCore.log("This mod doesn't have the guid tile"); } } /// /// Requests the object from the given guid. /// /// public static void RequestGuidObject(Guid request) { ModCore.ModHelper.Multiplayer.SendMessage(request.ToString(),RequestGUIDMessage, new string[] { ModCore.Manifest.UniqueID.ToString() }); } /// /// Requests a container object from tile component object's guid. /// /// public static void RequestGuidObject_Tile(Guid request) { ModCore.ModHelper.Multiplayer.SendMessage(request.ToString(), RequestGUIDMessage_Tile, new string[] { ModCore.Manifest.UniqueID.ToString() }); } /// /// Send a request to all other revitalize mods to get all of the synced guid objects that isn't held by this mod. /// public static void RequestALLGuidObjects() { ModCore.ModHelper.Multiplayer.SendMessage(RequestALLModObjects, RequestALLModObjects,new string[] { ModCore.Manifest.UniqueID.ToString() }); } /// /// Sends a request to all other revitalize mods to update the given guid object. /// /// public static void RequestUpdateSync(Guid request) { ModCore.ModHelper.Multiplayer.SendMessage(request.ToString(), RequestObjectUpdateSync, new string[] { ModCore.Manifest.UniqueID.ToString() }); } public static StardewValley.Multiplayer GetMultiplayer() { if (GameMultiplayer == null) { Multiplayer multiplayer = ModCore.ModHelper.Reflection.GetField(typeof(Game1), "multiplayer", true).GetValue(); if (multiplayer == null) return null; else { GameMultiplayer = multiplayer; return GameMultiplayer; } } else { return GameMultiplayer; } } } }