static void saveDefaultPack() { string packId = "1"; string packName = "Default pack"; LevelData[] levels = new LevelData[] { generateLevel1(), generateLevel2() }; PackData pack = new PackData(packId, packName, levels); FileStream stream = new FileStream(path + "/packs/" + packId + ".pack", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, pack); stream.Close(); string[] packIds = new string[1] { packId }; stream = new FileStream(path + "/packs.dat", FileMode.Create); formatter = new BinaryFormatter(); formatter.Serialize(stream, packIds); stream.Close(); }
public static PackResult Pack(int borderSize, bool dense, IEnumerable <FrameImage> images) { // all the data, sorted by item height for maximum storage efficiency when using a // shelf-stacking sprite packer. var selector = PackData.Factory(dense); var raw = images .Select(selector) .ToList(); var packList = raw .OrderBy(t => t, new SortOrder()) .ToList(); var packResult = PackAtlas(packList, borderSize); if (packResult.AtlasTextureSize > 2048) { Debug.LogWarning("Generate atlas size is larger than 2048, this might force Unity to compress the image."); } foreach (var p in packResult.Positions) { Debug.Log($"{p.FrameImage.BaseName}_{p.FrameImage.FrameId}_{p.FrameImage.SubImageIndex} = ({p.X}, {p.Y}) ({p.Width}, {p.Height})"); } return(packResult); }
private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { var files = e.Argument as string[]; PackData pack = new PackData(); List <MemoryStream> ls = new List <MemoryStream>(); decimal count = 1; foreach (var f in files) { var td = (count / files.Length) * 100; var percent = decimal.ToInt32(td); Sync.Send(setPbar, percent); var buf = GetSingleBitmap(f); MagickImage img = new MagickImage(buf) { Format = MagickFormat.Xbm }; var width = Convert.ToInt32(nbxWidth.Value); var height = Convert.ToInt32(nbxHeight.Value); img.Resize(new MagickGeometry($"{width}x{height}!")); buf = img.ToByteArray(); MemoryStream ms = new MemoryStream(); ms.Write(buf, 0, buf.Length); ls.Add(ms); count++; } }
/// <summary> /// 发送没有包头和包尾的数据 /// </summary> /// <param name="s"></param> /// <param name="m"></param> /// <param name="clientSocket"></param> public void SendMessage(string s, MessageStatus m, Socket clientSocket) { if (PackData.Instance().StringToByte(s, m, out sendDataPackage)) { clientSocket.BeginSend(sendDataPackage, 0, sendDataPackage.Length, SocketFlags.None, new System.AsyncCallback(M_Send), clientSocket); } }
public void RecordSendPack(ushort protocol, object pack) { if (!IsEnableRecord() && !IsEnableLogFile()) { return; } System.Type pack_type; if (Protocol2SendType.TryGetValue(protocol, out pack_type)) { var pack_data = new PackData(protocol, pack, PackData.EPackType.Send, pack_type); OnLogPackData(pack_data); #if UNITY_EDITOR bool not_record = false; if (NotRecordDict.TryGetValue(protocol, out not_record) && not_record) { return; } RecvedPackList.Add(pack_data); OnRecordPackData(pack_data); #endif } }
public static PackData Generate(int packId, IEnumerable <CardData> cardData) { var cards = cardData.ToArray(); if (cards.Length != 5) { Log.Error("Invalid card count: " + cards.Length); return(null); } var accId = Mirror.Reflection.GetAccountId(); if (accId == null || accId.Hi == 0 || accId.Lo == 0) { Log.Error("Could not get account id"); return(null); } var data = new PackData { AccountHi = accId.Hi, AccountLo = accId.Lo, BoosterType = packId, Date = DateTime.Now.ToString("o"), Cards = cards }; return(data); }
/// <summary> /// 发送给soc其他客户端的信息 /// </summary> /// <param name="s"></param> /// <param name="m"></param> /// <param name="soc"></param> public void OtherSendToThis(MessageStatus m, Socket soc) { for (int i = 0; i < this.clientList.Count; i++) { if (clientList[i].m_Socket.RemoteEndPoint.ToString() != soc.RemoteEndPoint.ToString()) { switch (m) { case MessageStatus.Name: if (PackData.Instance().StringToByte(clientList[i].m_name, m, out sendDataPackage)) { soc.BeginSend(sendDataPackage, 0, sendDataPackage.Length, SocketFlags.None, new System.AsyncCallback(M_Send), soc); } break; case MessageStatus.TransformColor: if (PackData.Instance().StringToByte(clientList[i].m_Color, m, out sendDataPackage)) { soc.BeginSend(sendDataPackage, 0, sendDataPackage.Length, SocketFlags.None, new System.AsyncCallback(M_Send), soc); } break; } } } }
public void ReloadLevels() { // Read Levels.xml file. PackCollectionDataXML collectionXML; TextAsset levelsXML = Resources.Load <TextAsset>("Levels/Levels"); if (levelsXML != null) { XmlSerializer serializer = new XmlSerializer(typeof(PackCollectionDataXML)); StringReader reader = new StringReader(levelsXML.text); collectionXML = serializer.Deserialize(reader) as PackCollectionDataXML; } else { Debug.LogError("Can't find Levels.xml file!"); collectionXML = new PackCollectionDataXML(); } // Make those PackDatas from the XML! int numPacks = collectionXML.packDataXMLs.Count; packDatas = new PackData[numPacks]; for (int i = 0; i < numPacks; i++) { LevelAddress packAddress = new LevelAddress(i, -1); packDatas[i] = new PackData(packAddress, collectionXML.packDataXMLs[i]); } }
public static PackData Generate(int packId, IEnumerable<CardData> cardData) { var cards = cardData.ToArray(); if(cards.Length != 5) { Log.Error("Invalid card count: " + cards.Length); return null; } var accId = HearthMirror.Reflection.GetAccountId(); if(accId == null || accId.Hi == 0 || accId.Lo == 0) { Log.Error("Could not get account id"); return null; } var data = new PackData { AccountHi = accId.Hi, AccountLo = accId.Lo, BoosterType = packId, Date = DateTime.Now.ToString("o"), Cards = cards }; return data; }
private PackData GeneratePayload(Account account, Pack pack) { if (pack.Cards.Count != 5) { Log.Error("Invalid card count: " + pack.Cards.Count); return(null); } if (account == null || account.AccountHi == 0 || account.AccountLo == 0) { Log.Error("Could not get account id"); return(null); } var data = new PackData { AccountHi = account.AccountHi, AccountLo = account.AccountLo, BoosterType = (int)pack.BoosterType, Date = DateTime.Now.ToString("o"), Cards = pack.Cards.Select(x => new CardData { CardId = x.Id, Premium = x.Golden > 0 }).ToArray() }; return(data); }
public void RecordRecvPack(ushort protocol, byte[] data) { if (!IsEnableRecord() && !IsEnableLogFile()) { return; } System.Type pack_type; if (Protocol2RecvType.TryGetValue(protocol, out pack_type)) { var pack = Net.S2CPackBase.DeserializePack(data, pack_type); var pack_data = new PackData(protocol, pack, PackData.EPackType.Recv, pack_type); OnLogPackData(pack_data); #if UNITY_EDITOR bool not_record = false; if (NotRecordDict.TryGetValue(protocol, out not_record) && not_record) { return; } RecvedPackList.Add(pack_data); OnRecordPackData(pack_data); #endif } }
internal void SetData(PackData data) { this.Data = data; Oxygen.Data = data.Oxygen; Power.Data = data.Power; Water.Data = data.Water; Food.Data = data.Food; }
//joint pack list private static void JoinPackData(List <PackData> list) { for (int i = 0; i < list.Count; i++) { //Debuger.Log("get one pack"); PackData data = list[i]; //Debug.LogError(string.Format("datasize:{0}cursize:{1}last_pak:{2}datalength:{3}", data.datasize, data.cursize, data.last_pak, data.data.Length)); if (frameBuffer == null || frameBuffer.Length != data.datasize) { frameBuffer = new byte[data.datasize]; //Debug.LogError("buffersize:" + data.datasize); } //若为最後一片 if (data.last_pak == 1) { //成功获取一个数据包 if ((cursize + data.cursize) == data.datasize) { //Debug.LogError("cursize:" + cursize); Array.Copy(data.data, 0, frameBuffer, cursize, data.cursize); //Debuger.Log("get one frame success!!!!"); if (oneFrameBuffer == null || oneFrameBuffer.Length != data.datasize) { oneFrameBuffer = new byte[data.datasize]; } lock (oneFrameBuffer) { frameBuffer.CopyTo(oneFrameBuffer, 0); cursize = 0; CreateColors(oneFrameBuffer); BigScreen.flag = true; //send to jni //YUVDeCoder.Decode(oneFrameBuffer); } } else { //丢弃该帧 cursize = 0; } } else if (cursize < data.datasize && (cursize + data.cursize) <= data.datasize) { //往下拼接 data.data.CopyTo(frameBuffer, cursize); cursize += data.cursize; //Debug.LogError("往下拼接:cursize:" + cursize); } else { //丢弃该帧 cursize = 0; } } }
public static PackData Instance() { if (instance == null) { instance = new PackData(); } return(instance); }
void StartGame(PackData p) { GameData.currentStage = 1; GameData.currentDeath = 0; GameData.packName = p.packName; GameData.maxStage = p.maxStage; GameData.maxDeath = p.maxDeath; GameData.path = p.path; Application.LoadLevel("GameDynamic"); }
public LevelData GetLevelData(int pack, int level) { PackData packData = GetPackData(pack); if (packData == null) { return(null); } // Safety check. return(packData.GetLevelData(level)); }
//read frame data //private byte[] onepack = new byte[totalSizePerFrame]; //public void ReadFrameData(byte[] data, int length) //{ // packlist.Clear(); // int curpos = 0; // while (curpos < data.Length && curpos < length && (curpos + totalSizePerFrame) <= data.Length && (curpos + totalSizePerFrame) <= length) // { // Array.Copy(data, curpos, onepack, 0, totalSizePerFrame); // packlist.Add(ReadOnePack(onepack)); // curpos += totalSizePerFrame; // } // JoinPackData(packlist); //} //read one pack private PackData ReadOnePack(byte[] data) { PackData pack = packDataPool.Create(); pack.datasize = System.BitConverter.ToInt32(data, 0); pack.cursize = System.BitConverter.ToInt32(data, 4); pack.last_pak = System.BitConverter.ToInt32(data, 8); Array.Copy(data, 12, pack.data, 0, dataSizePerFrame); return(pack); }
/// <summary> /// 给所有客户端发送消息 /// </summary> /// <param name="s"></param> public void SendMessageToAll(string s, MessageStatus m) { for (int i = 0; i < clientList.Count; i++) { if (PackData.Instance().StringToByte(s, m, out sendDataPackage)) { clientList[i].m_Socket.BeginSend(sendDataPackage, 0, sendDataPackage.Length, SocketFlags.None, new System.AsyncCallback(M_Send), clientList[i]); } } }
/// <summary> /// 接收数据 /// </summary> /// <param name="ar"></param> private void M_Recive(System.IAsyncResult ar) { Debug.Log("Recive"); Socket SeverSocket = ar.AsyncState as Socket; if (!SeverSocket.Connected) { Debug.Log("掉线了"); return; } int count = 0; count = SeverSocket.EndReceive(ar); string content = ""; //数据包转字符串 if (count > 0) { content = PackData.Instance().ByteToString(reciveDataPackage, count); Debug.Log("ClientContent:" + content); } //处理粘包 for (int i = 0; i < content.Length;) { //包长度内 if (i <= content.Length - PackData.Instance().packageEndLength) { //如果字符串不是包尾 添加到暂存字符串中 if (content.Substring(i, PackData.Instance().packageEndLength) != "/*/") { sb.Append(content[i]); i++; } //字符串是包尾 调用回调函数 清空暂存字符串 同时数组指针指向包尾后字符串 else { callBack(sb.ToString()); sb = new StringBuilder(); i += PackData.Instance().packageEndLength; } } //包长度外 即出现分包情况 储存当前包的后半部 和下个包结合 else { sb.Append(content[i]); i++; } } SeverSocket.BeginReceive(reciveDataPackage, 0, reciveDataPackage.Length, SocketFlags.None, new System.AsyncCallback(M_Recive), SeverSocket); }
//将Byte转换为结构体类型 private static void StructToBytes(PackData structObj, ref byte[] data) { byte[] byte1 = BitConverter.GetBytes(structObj.datasize); byte[] byte2 = BitConverter.GetBytes(structObj.cursize); byte[] byte3 = BitConverter.GetBytes(structObj.last_pak); byte[] byte4 = structObj.data; byte1.CopyTo(data, 0); byte2.CopyTo(data, 4); byte3.CopyTo(data, 8); byte4.CopyTo(data, 12); }
public NoPackAsset(List <PackData> list) { if (list != null) { mlist = list.ToArray(); List <string> tempPahtData = new List <string>(); List <string> tempPathExData = new List <string>(); List <EncryPath> tempEncryData = new List <EncryPath>(); List <string> encryKey = new List <string>(); tempPathExData.Add(".prefab"); tempPathExData.Add(""); for (int i = 0; i < list.Count; i++) { PackData pd = list[i]; string path = pd.allName; int dPos = path.LastIndexOf("."); int pathPos = path.IndexOf(ResConfig.ASSETBUNDLE); if (dPos != -1 && pathPos != -1) { string DEx = path.Substring(dPos); string noDPath = path.Substring(0, pathPos + ResConfig.ASSETBUNDLE.Length); string otherPath = path.Substring(pathPos + ResConfig.ASSETBUNDLE.Length + 1); otherPath = FEPath.GetDirectoryName(otherPath); string tempOtherPath = FUniversalFunction.GetChunk(otherPath, "[", "]")[0]; if (otherPath != tempOtherPath) { if (!encryKey.Contains(tempOtherPath)) { EncryPath ep = new EncryPath(); ep.key = tempOtherPath.ToLower(); ep.value = otherPath; tempEncryData.Add(ep); encryKey.Add(tempOtherPath); } } if (!tempPahtData.Contains(noDPath)) { tempPahtData.Add(noDPath); } if (!tempPathExData.Contains(DEx)) { tempPathExData.Add(DEx); } } } mPahtList = tempPahtData.ToArray(); mPathEx = tempPathExData.ToArray(); mEncryPath = tempEncryData.ToArray(); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="s"></param> public void SendMessage(string s, MessageStatus m) { if (!m_Socket.Connected) { Debug.Log("掉线了"); return; } if (PackData.Instance().StringToByte(s, m, out sendDataPackage)) { m_Socket.BeginSend(sendDataPackage, 0, sendDataPackage.Length, SocketFlags.None, new System.AsyncCallback(M_Send), m_Socket); } }
//read one pack private static PackData ReadOnePack(byte[] data) { byte[] pack_data = new byte[dataSize]; PackData pack = new PackData(); pack.datasize = System.BitConverter.ToInt32(data, 0); pack.cursize = System.BitConverter.ToInt32(data, 4); pack.last_pak = System.BitConverter.ToInt32(data, 8); Array.Copy(data, 12, pack_data, 0, dataSize); pack.data = pack_data; return(pack); }
private void OnRecordPackData(PackData pack_data) { if (!IsEnableRecord()) { return; } var window = MainGame.debugWindow; if (window != null) { window.AddMsg(pack_data); } }
//joint pack list private void JoinPackData(List <PackData> list) { for (int i = 0; i < list.Count; i++) { PackData data = list[i]; if (frameBuffer == null || frameBuffer.Length != data.datasize) { frameBuffer = new byte[data.datasize]; } //若为最後一片 if (data.last_pak == 1) { //成功获取一个数据包 if ((cursize + data.cursize) == data.datasize) { Array.Copy(data.data, 0, frameBuffer, cursize, data.cursize); if (oneFrameBuffer == null || oneFrameBuffer.Length != data.datasize) { oneFrameBuffer = new byte[data.datasize]; } lock (oneFrameBuffer) { frameBuffer.CopyTo(oneFrameBuffer, 0); cursize = 0; CreateColors(oneFrameBuffer); BigScreen.flag = true; } } else { //丢弃该帧 cursize = 0; } } else if (cursize < data.datasize && (cursize + data.cursize) <= data.datasize) { //往下拼接 data.data.CopyTo(frameBuffer, cursize); cursize += data.cursize; } else { //丢弃该帧 cursize = 0; } } }
/// <summary> /// 建立连接 /// </summary> /// <param name="ip"></param> /// <param name="name"></param> /// <param name="cb"></param> public void Init(string ip, string name, clientCallBack cb) { this.callBack = cb; //创建 m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint m_IPEndPoint = new IPEndPoint(IPAddress.Parse(ip), 23555); //连接 m_Socket.Connect(m_IPEndPoint); m_Socket.BeginReceive(reciveDataPackage, 0, reciveDataPackage.Length, SocketFlags.None, new System.AsyncCallback(M_Recive), m_Socket); callBack(PackData.Instance().PackStringDataHead("连接成功", MessageStatus.Warring)); callBack(PackData.Instance().PackStringDataHead(name, MessageStatus.Name)); SendMessage(name, MessageStatus.Name); }
public LevelAddress NextLevelAddress(LevelAddress addr) { PackData pack = GetPackData(addr.pack); if (pack == null) { return(LevelAddress.undefined); } // Safety check. addr.level++; if (addr.level >= pack.NumLevels) // Wrap over to next pack. { addr.pack++; addr.level = 0; } return(addr); }
/// <summary> /// 给除了发送者以外的所有客户端发送消息 /// </summary> public void SendMessageToOther(string s, MessageStatus m, Socket soc) { //发送给其他客户端 for (int i = 0; i < this.clientList.Count; i++) { if (soc.RemoteEndPoint.ToString() != clientList[i].m_EndPoint.ToString() && clientList[i].m_Socket.Connected) { if (PackData.Instance().StringToByte(s, m, out sendDataPackage)) { clientList[i].m_Socket.BeginSend(sendDataPackage, 0, sendDataPackage.Length, SocketFlags.None, new System.AsyncCallback(M_Send), clientList[i]); } ////发送数据 因为已是打包好的 所以直接发 //SendMessage(sb.ToString(), clientList[j].m_Socket); //Debug.Log("TOOTHER:" + sb.ToString()); } } }
/// <summary> /// 输出到日志文件 /// </summary> /// <param name="pack_data"></param> private void OnLogPackData(PackData pack_data) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(mLogFilePath, true)) { string timeStr = DateHelper.GetServerDateTime().ToString(); string packTypeStr = ""; if (pack_data.PackType == PackData.EPackType.Send) { packTypeStr = "c2s"; } else { packTypeStr = "s2c"; } file.WriteLine(timeStr + " " + packTypeStr + " " + pack_data.Protocol + " " + pack_data.Type.ToString() + " " + ": " + pack_data.GetJson2()); file.Close(); } }
/// <summary> /// 打开服务端 /// </summary> /// <param name="sc"></param> public void Init(SeverCallBack sc) { this.severCallBack = sc; //创建 m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint m_EndPoint = new IPEndPoint(IPAddress.Any, 23555); //绑定 m_socket.Bind(m_EndPoint); //监听 m_socket.Listen(10); //接受 m_socket.BeginAccept(new System.AsyncCallback(M_Accept), m_socket); severCallBack(PackData.Instance().PackStringDataHead("服务器已打开", MessageStatus.Message), m_socket); }
/// <summary> /// 向特定ip的主机的端口发送数据报 /// </summary> private void sendMsg() { while (client == null) { Thread.Sleep(500); } NetworkStream sendStream = client.GetStream(); PackData packdata = new PackData(); byte[] buff = new byte[max_buff_size + 12]; while (true) { if (BitmapCoder.instance.packDataQueue.Count != 0) { byte[] bits = BitmapCoder.instance.packDataQueue.Peek(); int offset = bits.Length % max_buff_size; int count = bits.Length / max_buff_size + (offset == 0 ? 0 : 1); for (int i = 0; i < count; i++) { if (i < count - 1) { packdata.cursize = max_buff_size; packdata.last_pak = 0; } else { packdata.cursize = offset == 0 ? max_buff_size : offset; packdata.last_pak = 1; } packdata.datasize = bits.Length; Array.Copy(bits, i * max_buff_size, packdata.data, 0, packdata.cursize); StructToBytes(packdata, ref buff); sendStream.Write(buff, 0, buff.Length); } BitmapCoder.instance.packDataQueue.Dequeue(); } } }
/// <summary> /// 向特定ip的主机的端口发送数据报 /// </summary> private void sendMsg() { EndPoint point1 = new IPEndPoint(IPAddress.Parse(clientIP), 6000); //EndPoint point2 = new IPEndPoint(IPAddress.Parse(client_ip2), 6000); PackData packdata = new PackData(); byte[] buff = new byte[buffSizePerFrame + 12]; while (true) { if (BitmapCoder.instance.packDataQueue.Count != 0) { byte[] bits = BitmapCoder.instance.packDataQueue.Peek(); int offset = bits.Length % buffSizePerFrame; int count = bits.Length / buffSizePerFrame + (offset == 0 ? 0 : 1); for (int i = 0; i < count; i++) { if (i < count - 1) { packdata.cursize = buffSizePerFrame; packdata.last_pak = 0; } else { packdata.cursize = offset == 0 ? buffSizePerFrame : offset; packdata.last_pak = 1; } packdata.datasize = bits.Length; Array.Copy(bits, i * buffSizePerFrame, packdata.data, 0, packdata.cursize); StructToBytes(packdata, ref buff); server.SendTo(buff, point1); //server.SendTo(buff, point2); } BitmapCoder.instance.packDataQueue.Dequeue(); } } }
public static async Task UploadPack(PackData data) => await Client.UploadPack(data, await GetUploadToken());
public PackData ReadPack(string fileName) { PackData p = new PackData (); p.path = fileName; TextAsset t = Resources.Load ("Stage/" + fileName + "/pack", typeof(TextAsset)) as TextAsset; TextReader sr = new StringReader (t.text); //StreamReader sr = File.OpenText(fileName + "/pack.txt"); string input = ""; while(true){ input = sr.ReadLine(); if(input == null) break; switch(input){ case "#PACKNAME": input = sr.ReadLine (); p.packName = input; break; case "#MAXSTAGE": input = sr.ReadLine (); p.maxStage = int.Parse (input); break; case "#MAXDEATH": input = sr.ReadLine (); p.maxDeath = int.Parse (input); break; } } sr.Close(); return p; }