using UnityEngine; using System; using System.Collections.Generic; using Sfs2X.Core; using Sfs2X.Entities; using Sfs2X.Requests; using Sfs2X.Entities.Data; public class BaseSFSExtension { public GardenSmartFox GardenSmartFox; public List Instances = new List(); public Dictionary> DatabaseDictionary = new Dictionary>(); public Dictionary> CommandDictionary = new Dictionary>(); public BaseSFSExtension Init(GardenSmartFox smartFox) { GardenSmartFox = smartFox; GardenSmartFox.SmartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse); GardenSmartFox.SmartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnRoomJoin); GardenSmartFox.SmartFox.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnRoomJoinError); GardenSmartFox.SmartFox.AddEventListener(SFSEvent.USER_ENTER_ROOM, OnUserEnterRoom); GardenSmartFox.SmartFox.AddEventListener(SFSEvent.USER_EXIT_ROOM, OnUserExitRoom); return this; } public void OnExtensionResponse(BaseEvent baseEvent) { //DebugManager.PrintKeysAndValuesOfBaseEvent("OnExtensionResponse", baseEvent); string[] strings = baseEvent.Params["cmd"].ToString().Split('|'); int requestID = int.Parse(strings[0]); int id = int.Parse(strings[1]); if (requestID == RequestID.Command.GetHashCode()) HandleExtensionResponse(baseEvent, CommandDictionary, typeof(CommandID), id); else if (requestID == RequestID.Database.GetHashCode()) HandleExtensionResponse(baseEvent, DatabaseDictionary, typeof(DatabaseID), id); } private void HandleExtensionResponse(BaseEvent baseEvent, Dictionary> dictionary, Type type, int id) { if (dictionary.ContainsKey(id)) { dictionary[id].Invoke(baseEvent); } else { Action callback = GetExtensionResponseMethod(type, id); dictionary.Add(id, callback); callback.Invoke(baseEvent); } } private Action GetExtensionResponseMethod(Type type, int id) { for (int i = 0; i < Instances.Count; i++) { Action method = (Action)Delegate.CreateDelegate(typeof(Action), Instances[i], Enum.GetName(type, id), false, false); if (method != null) return method; } throw new Exception("没有找到方法 : " + id); } public void OnRoomJoin(BaseEvent baseEvent) { //DebugManager.PrintKeysAndValuesOfBaseEvent("OnRoomJoin", baseEvent); Room room = (Room) baseEvent.Params["room"]; if (room.GroupId == GardenSmartFox.ExtensionManager.PlazaRoomExtension.PlazaRoomGroupID) { GardenSmartFox.ExtensionManager.PlazaRoomExtension.OnJoinPlazaRoom(room); } } public void OnRoomJoinError(BaseEvent baseEvent) { DebugManager.PrintKeysAndValuesOfBaseEvent("OnRoomJoinError", baseEvent); } public void OnUserExitRoom(BaseEvent baseEvent) { //DebugManager.PrintKeysAndValuesOfBaseEvent("OnUserExitRoom", baseEvent); User user = (User) baseEvent.Params["user"]; Room room = (Room) baseEvent.Params["room"]; if (room.GroupId == GardenSmartFox.ExtensionManager.PlazaRoomExtension.PlazaRoomGroupID) { GardenSmartFox.ExtensionManager.PlazaRoomExtension.OnUserExitPlazaRoom(room, user); } } public void OnUserEnterRoom(BaseEvent baseEvent) { //DebugManager.PrintKeysAndValuesOfBaseEvent("OnUserEnterRoom", baseEvent); User user = (User)baseEvent.Params["user"]; Room room = (Room)baseEvent.Params["room"]; if (room.GroupId == GardenSmartFox.ExtensionManager.PlazaRoomExtension.PlazaRoomGroupID) { GardenSmartFox.ExtensionManager.PlazaRoomExtension.OnUserEnterPlazaRoom(room, user); } } }