Ejemplo n.º 1
0
        private void LoadSlice(string mapName, int rowNum, int colNum, float sliceWorldWidth, float sliceWorldHeight, int i, int j)
        {
            string    picFilename = string.Format("{0:d2}_{1:d2}", i + 1, j + 1);
            Texture2D texture     = Resources.Load(mapName + "/bg/" + picFilename, typeof(Texture2D)) as Texture2D;

            if (texture == null)
            {
                XxdwDebugger.LogError("Fail to load texture: " + mapName + "/bg/" + picFilename);
            }
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));   // 以左上角为pivot

            if (sprite == null)
            {
                XxdwDebugger.LogError("Fail to load bg picture " + picFilename);
            }
            GameObject     go = new GameObject(picFilename);
            SpriteRenderer sr = go.AddComponent <SpriteRenderer>();

            sr.sprite = sprite;
            go.transform.SetParent(this.transform);

            // 计算世界坐标
            float r = (float)rowNum / 2 - 1 - i;
            float c = -(float)colNum / 2 + j;

            go.transform.position = new Vector3(c * sliceWorldWidth, r * sliceWorldHeight);
        }
Ejemplo n.º 2
0
        private ClientSocket Connect()
        {
            ClientSocket cs = null;

            try
            {
                cs = new ClientSocket(this.DestName, this.DestPort);
                return(cs);
            }
            catch (Exception e)
            {
                XxdwDebugger.LogError(e.Message);
            }

            return(null);
        }
Ejemplo n.º 3
0
        private void DataThreadFunc()
        {
            XxdwDebugger.Log("GpsMap: DataThreadFunc() entering...");
            try
            {
                this.LoadingStatus = SafeStatus.LS_NULL;

                int    pos        = this.mapXmlText.IndexOf("<cells>");
                string strMapData = this.mapXmlText.Substring(0, pos);
                strMapData += "</grid>";
                string strCellAndPathData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<grid>\n";
                strCellAndPathData += this.mapXmlText.Substring(pos);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(new StringReader(strMapData));

                XmlNode node = xmlDoc.SelectSingleNode("/grid/mapName");
                this.name            = (node == null ? "" : node.InnerText.Trim());
                node                 = xmlDoc.SelectSingleNode("/grid/sliceWidth");
                this.sliceWidth      = (node == null ? 0 : int.Parse(node.InnerText.Trim()));
                node                 = xmlDoc.SelectSingleNode("/grid/sliceHeight");
                this.sliceHeight     = (node == null ? 0 : int.Parse(node.InnerText.Trim()));
                node                 = xmlDoc.SelectSingleNode("/grid/triggerDistance");
                this.triggerDistance = (node == null ? 0 : int.Parse(node.InnerText.Trim()));

                node                  = xmlDoc.SelectSingleNode("/grid/pixelWidth");
                this.pixelWidth       = int.Parse(node.InnerText.Trim());
                node                  = xmlDoc.SelectSingleNode("/grid/pixelHeight");
                this.pixelHeight      = int.Parse(node.InnerText.Trim());
                node                  = xmlDoc.SelectSingleNode("/grid/gridCellPixelNum");
                this.gridCellPixelNum = int.Parse(node.InnerText.Trim());
                this.rowNum           = this.pixelHeight / this.gridCellPixelNum;
                this.colNum           = this.pixelWidth / this.gridCellPixelNum;

                this.mapRange = new Rect(-(float)this.PixelWidth / 2 / UNIT_PIXEL_NUM,
                                         -(float)this.pixelHeight / 2 / UNIT_PIXEL_NUM,
                                         (float)this.PixelWidth / UNIT_PIXEL_NUM,
                                         (float)this.PixelHeight / UNIT_PIXEL_NUM);
                XxdwDebugger.Log("rowNum=" + this.rowNum + "colNum=" + this.colNum
                                 + " mapRange:(" + this.mapRange.xMin + "," + this.mapRange.yMin + "),(" + this.mapRange.xMax + "," + this.mapRange.yMax + ")");

                node = xmlDoc.SelectSingleNode("/grid/rolePositionIndex");
                if (node != null)
                {
                    this.rolePositionIndex = int.Parse(node.InnerText.Trim());
                }

                node = xmlDoc.SelectSingleNode("/grid/gridCellLongitudeDiff");
                this.gridCellLongitudeDiff = float.Parse(node.InnerText.Trim());
                node = xmlDoc.SelectSingleNode("/grid/gridCellLatitudeDiff");
                this.gridCellLatitudeDiff = float.Parse(node.InnerText.Trim());
                node = xmlDoc.SelectSingleNode("/grid/gridCellDistance");
                this.gridCellDistance = float.Parse(node.InnerText.Trim());
                node = xmlDoc.SelectSingleNode("/grid/gridCellDiagonalDistance");
                this.gridCellDiagonalDistance = float.Parse(node.InnerText.Trim());
                node = xmlDoc.SelectSingleNode("/grid/zeroLongitude");
                this.zeroLongitude = float.Parse(node.InnerText.Trim());
                node = xmlDoc.SelectSingleNode("/grid/zeroLatitude");
                this.zeroLatitude = float.Parse(node.InnerText.Trim());

                node                     = xmlDoc.SelectSingleNode("/grid/baseIndex1");
                this.baseIndex1          = int.Parse(node.InnerText.Trim());
                node                     = xmlDoc.SelectSingleNode("/grid/baseIndex2");
                this.baseIndex2          = int.Parse(node.InnerText.Trim());
                node                     = xmlDoc.SelectSingleNode("/grid/basePoint1Longitude");
                this.basePoint1Longitude = float.Parse(node.InnerText.Trim());
                node                     = xmlDoc.SelectSingleNode("/grid/basePoint1Latitude");
                this.basePoint1Latitude  = float.Parse(node.InnerText.Trim());
                node                     = xmlDoc.SelectSingleNode("/grid/basePoint2Longitude");
                this.basePoint2Longitude = float.Parse(node.InnerText.Trim());
                node                     = xmlDoc.SelectSingleNode("/grid/basePoint2Latitude");
                this.basePoint2Latitude  = float.Parse(node.InnerText.Trim());

                this.LoadingStatus |= SafeStatus.LS_INFO_DATA;
                XxdwDebugger.Log("GpsMap: DataThreadFunc(), post NOTI_MAP_INFO_READY");
                AsyncNotification.Instance.PostNotification(this, Notification.NOTI_MAP_INFO_READY, this.LoadingStatus, this.name);

                //--------------------------------------------------------------------------------------
                xmlDoc.Load(new StringReader(strCellAndPathData));

                // read cells of the grid
                this.grid = new GpsMapCell[this.rowNum, this.colNum];

                string      xmlPathPattern = "/grid/cells/r";
                XmlNodeList rows           = xmlDoc.SelectNodes(xmlPathPattern);
                if (rows != null && rows.Count > 0)
                {
                    foreach (XmlNode xn in rows)
                    {
                        int         i    = int.Parse(xn.Attributes["v"].InnerText);
                        XmlNodeList cols = xn.SelectNodes("./c");
                        foreach (XmlNode cell in cols)
                        {
                            int j = int.Parse(cell.Attributes["v"].InnerText);
                            if (!(i >= 0 && i < this.rowNum && j >= 0 && j < this.colNum))
                            {
                                continue;
                            }

                            node = cell.SelectSingleNode("./t");
                            int type = int.Parse(node.InnerText.Trim());

                            node = cell.SelectSingleNode("./d");
                            string data = (node == null ? "" : node.InnerText.Trim());

                            int entryNumber = 0;
                            if ((type & (int)GpsMapCell.EType.EN_CROSS) != 0)
                            {
                                node = cell.SelectSingleNode("./n");
                                if (node != null)
                                {
                                    entryNumber = int.Parse(node.InnerText.Trim());
                                }
                            }

                            this.grid[i, j] = new GpsMapCell(i * this.colNum + j, i, j, (GpsMapCell.EType)type, data, entryNumber);

                            //---------------------------------------------------------------------------------
                            // 触发点,以触发点编号排序插入
                            if ((type & (int)GpsMapCell.EType.EN_MONITOR_POINT) != 0)
                            {
                                node = cell.SelectSingleNode("./d2");
                                if (node != null)
                                {
                                    string   data2       = node.InnerText.Trim();
                                    string[] ssid_number = data2.Split(new char[] { '_' });
                                    if (ssid_number.Length == 2)
                                    {
                                        GpsMapMonitorSpot ms = new GpsMapMonitorSpot(data2, ssid_number[0], i * this.colNum + j);
                                        int index            = this.listMonitorSpots.BinarySearch(ms);
                                        if (index < 0)
                                        {
                                            this.listMonitorSpots.Insert(~index, ms);
                                        }
                                    }
                                    else
                                    {
                                        XxdwDebugger.LogWarning("Invalid monitor spot data at: " + (i * this.colNum + j));
                                    }
                                }
                                else
                                {
                                    XxdwDebugger.LogWarning("Monitor spot without data at: " + (i * this.colNum + j));
                                }
                            }
                        }
                    }
                }

                this.LoadingStatus |= SafeStatus.LS_CELL_DATA;
                XxdwDebugger.Log("GpsMap: DataThreadFunc(), post NOTI_MAP_DATA_READY cell_data ok");
                AsyncNotification.Instance.PostNotification(this, Notification.NOTI_MAP_DATA_READY, this.LoadingStatus, this.name);

                //--------------------------------------------------------------------------------------
                // read the road data of the grid, which will shape a Data Structure of Graph.
                xmlPathPattern = "/grid/edges/e";
                XmlNodeList edges = xmlDoc.SelectNodes(xmlPathPattern);

                if (edges != null && edges.Count > 0)
                {
                    for (int i = 0; i < edges.Count; i++)
                    {
                        node = edges[i].SelectSingleNode("./cl1");
                        int cell1 = int.Parse(node.InnerText.Trim());
                        node = edges[i].SelectSingleNode("./cl2");
                        int cell2 = int.Parse(node.InnerText.Trim());
                        node = edges[i].SelectSingleNode("./w");
                        float weight = float.Parse(node.InnerText.Trim());
                        node = edges[i].SelectSingleNode("./ir");
                        string identifier = node.InnerText.Trim();
                        node = edges[i].SelectSingleNode("./s");
                        string   strSpots = node.InnerText.Trim();
                        string[] sSpots   = strSpots.Split(new char[] { ',' });
                        if (sSpots.Length == 0)
                        {
                            continue;
                        }
                        int[] spots = new int[sSpots.Length];
                        for (int j = 0; j < sSpots.Length; j++)
                        {
                            spots[j] = int.Parse(sSpots[j]);
                        }

                        this.pathGraph.AddEdge(cell1, cell2, weight, spots, identifier, CompareEdgeByRadian);
                    }
                }

                //--------------------------------------------------------------------------------------
                // read the scenic spot data of the grid
                xmlPathPattern = "/grid/scenic_spot/ss";
                XmlNodeList scenicSpots = xmlDoc.SelectNodes(xmlPathPattern);
                this.listScenicSpots.Clear();
                if (scenicSpots != null)
                {
                    foreach (XmlNode xn in scenicSpots)
                    {
                        node = xn.SelectSingleNode("./ir");
                        string identifier = node.InnerText.Trim();
                        node = xn.SelectSingleNode("./en");
                        string cells = node.InnerText.Trim();

                        GpsMapScenicSpot ss = new GpsMapScenicSpot(identifier, cells);
                        int index           = listScenicSpots.BinarySearch(ss);
                        if (index < 0)
                        {
                            this.listScenicSpots.Insert(~index, ss);
                        }
                    }
                }

                //------------------------------------------------------
                this.LoadingStatus |= SafeStatus.LS_PATH_DATA;
                XxdwDebugger.Log("GpsMap: DataThreadFunc(), post NOTI_MAP_DATA_READY path_data ok");
                AsyncNotification.Instance.PostNotification(this, Notification.NOTI_MAP_DATA_READY, this.LoadingStatus, this.name);
            }
            catch (System.Exception ex)
            {
                XxdwDebugger.LogError(ex.Message);
                this.LoadingStatus = SafeStatus.LS_NULL;
            }

            //this.pathGraph.TraverseAllAdjacents();
            XxdwDebugger.Log("GpsMap: DataThreadFunc() leaving...");
        }
Ejemplo n.º 4
0
        private void OnDownloadMapAndVoiceData(object objData, ResponseData result)
        {
            ClientSocket cs  = null;
            SslAppData   sad = new SslAppData();

            try
            {
                SimpleJson.JsonObject jsonPathData = new SimpleJson.JsonObject();
                jsonPathData.Add("op", "UPLOAD_PATH_DATA");
                jsonPathData.Add("data", (string)objData);
                //jsonPathData.Add("data", "no actual data.");
                string strJsonText = jsonPathData.ToString();

                cs = Connect();
                if (cs == null)
                {
                    result.Code = ResponseData.RESP_SERVER_UNREACHABLE;
                    result.Msg  = TypeAndParameter.STR_CONNECT_WITH_SERVER_FAILED;
                    return;
                }

                // 发送数据
                sad.SetType(SslAppData.APP_TYPE_PLAIN_JSON);
                byte[] allBytes = System.Text.Encoding.Default.GetBytes(strJsonText);
                if (allBytes.Length > SslAppData.SSL_APP_CONTENT_MAX_SIZE)
                {
                    sad.SetFirst(true);
                    sad.SetLast(false);
                    sad.SetData(allBytes, 0, SslAppData.SSL_APP_CONTENT_MAX_SIZE);
                    cs.Send(sad);

                    sad.SetFirst(false);
                    int i = SslAppData.SSL_APP_CONTENT_MAX_SIZE;
                    while (i < allBytes.Length)
                    {
                        if (allBytes.Length - i <= SslAppData.SSL_APP_CONTENT_MAX_SIZE)
                        {
                            sad.SetLast(true);
                            sad.SetData(allBytes, i);
                            i += allBytes.Length - i;
                        }
                        else
                        {
                            sad.SetData(allBytes, i, SslAppData.SSL_APP_CONTENT_MAX_SIZE);
                            i += SslAppData.SSL_APP_CONTENT_MAX_SIZE;
                        }
                        cs.Send(sad);
                    }
                }
                else
                {
                    sad.SetData(allBytes);
                    //sad.SetData(System.Text.Encoding.UTF8.GetBytes(strJsonText));
                    cs.Send(sad);
                }

                // 接收数据
                cs.Recv(sad);
                byte[] jsonData = sad.GetData();
                //strJsonText = System.Text.Encoding.Default.GetString(jsonData);
                strJsonText = System.Text.Encoding.UTF8.GetString(jsonData);

                var rsp = (IDictionary <string, object>)SimpleJson.SimpleJson.DeserializeObject(strJsonText);
                if ("AE_SUCCESS" == (rsp["err"] as string))
                {
                    result.Code = ResponseData.RESP_SUCCESS;
                    result.Msg  = TypeAndParameter.STR_UPLOAD_PATH_DATA_FINISHED;
                }
                else
                {
                    result.Msg = rsp["msg"] as string;
                }
            }
            catch (Exception e)
            {
                XxdwDebugger.LogError(e.StackTrace);
            }
            finally
            {
                if (cs != null)
                {
                    cs.Close();
                }
            }
        }
Ejemplo n.º 5
0
        private void OnUploadPathData2(object objData, ResponseData result)
        {
            ClientSocket cs  = null;
            SslAppData   sad = new SslAppData();

            try
            {
                string str = (string)objData;
                cs = Connect();
                if (cs == null)
                {
                    result.Code = ResponseData.RESP_SERVER_UNREACHABLE;
                    result.Msg  = TypeAndParameter.STR_CONNECT_WITH_SERVER_FAILED;
                    return;
                }

                // 发送数据
                sad.SetType(SslAppData.APP_TYPE_FILE);
                byte[] allBytes      = System.Text.Encoding.Default.GetBytes(str);
                byte[] filenameBytes = System.Text.Encoding.Default.GetBytes("test.txt\0");
                byte[] d             = new byte[SslAppData.APP_TYPE_FILE_HEADER_SIZE + filenameBytes.Length + SslAppData.SSL_APP_CONTENT_MAX_SIZE];
                int    s             = IPAddress.HostToNetworkOrder(allBytes.Length); // 文件大小
                System.Array.Copy(BitConverter.GetBytes(s), 0, d, 0, 4);
                //System.Array.Copy(BitConverter.GetBytes(91), 0, d, 4, 4); // 偏移,为调试
                short t = IPAddress.HostToNetworkOrder((short)filenameBytes.Length);
                System.Array.Copy(BitConverter.GetBytes(t), 0, d, 8, 2);
                System.Array.Copy(filenameBytes, 0, d, SslAppData.APP_TYPE_FILE_HEADER_SIZE, filenameBytes.Length);

                if (allBytes.Length > SslAppData.SSL_APP_CONTENT_MAX_SIZE)
                {
                    sad.SetFirst(true);
                    sad.SetLast(false);

                    System.Array.Copy(allBytes, 0, d, SslAppData.APP_TYPE_FILE_HEADER_SIZE + filenameBytes.Length, SslAppData.SSL_APP_CONTENT_MAX_SIZE);
                    sad.SetData(d, 0, SslAppData.APP_TYPE_FILE_HEADER_SIZE + filenameBytes.Length + SslAppData.SSL_APP_CONTENT_MAX_SIZE);
                    cs.Send(sad);

                    sad.SetFirst(false);
                    int i = SslAppData.SSL_APP_CONTENT_MAX_SIZE;
                    while (i < allBytes.Length)
                    {
                        if (allBytes.Length - i <= SslAppData.SSL_APP_CONTENT_MAX_SIZE)
                        {
                            sad.SetLast(true);
                            sad.SetData(allBytes, i);
                            i += allBytes.Length - i;
                        }
                        else
                        {
                            sad.SetData(allBytes, i, SslAppData.SSL_APP_CONTENT_MAX_SIZE);
                            i += SslAppData.SSL_APP_CONTENT_MAX_SIZE;
                        }
                        cs.Send(sad);
                    }
                }
                else
                {
                    System.Array.Copy(allBytes, 0, d, SslAppData.APP_TYPE_FILE_HEADER_SIZE + filenameBytes.Length, allBytes.Length);
                    sad.SetData(d, 0, SslAppData.APP_TYPE_FILE_HEADER_SIZE + filenameBytes.Length + allBytes.Length);
                    cs.Send(sad);
                }

                // 接收数据
                cs.Recv(sad);
                byte[] jsonData = sad.GetData();
                //string strJsonText = System.Text.Encoding.Default.GetString(jsonData);
                string strJsonText = System.Text.Encoding.UTF8.GetString(jsonData);

                var rsp = (IDictionary <string, object>)SimpleJson.SimpleJson.DeserializeObject(strJsonText);
                if ("AE_SUCCESS" == (rsp["err"] as string))
                {
                    result.Code = ResponseData.RESP_SUCCESS;
                    result.Msg  = TypeAndParameter.STR_UPLOAD_PATH_DATA_FINISHED;
                }
                else
                {
                    // utf-8格式的消息转unicode
                    //byte[] buffer= Encoding.UTF8.GetBytes(rsp["msg"] as string);
                    //result.Msg = Encoding.Unicode.GetString(buffer);
                    result.Msg = rsp["msg"] as string;
                }
            }
            catch (Exception e)
            {
                XxdwDebugger.LogError(e.StackTrace);
            }
            finally
            {
                if (cs != null)
                {
                    cs.Close();
                }
            }
        }
Ejemplo n.º 6
0
        private void DataThreadFunc()
        {
            try
            {
                StringReader sr = new StringReader(this.csvText);

                List <string> row = new List <string>();
                int           quotaNumNotMatched = 0;
                bool          previousQuota      = false;
                string        cell = "";
                string        line;
                while ((line = sr.ReadLine()) != null)  // 返回的串不包括“\r\n”
                {
                    for (int i = 0; i < line.Length; i++)
                    {
                        char ch = line[i];
                        if (ch == '\"')
                        {
                            if (previousQuota)      // 两个引号,忽略前一个,加后一个
                            {
                                cell         += ch;
                                previousQuota = false;
                                quotaNumNotMatched--;
                            }
                            else if (quotaNumNotMatched > 0 &&
                                     (i == line.Length - 1 || line[i + 1] != '\"')) // 引用结束
                            {
                                quotaNumNotMatched--;
                            }
                            else
                            {
                                previousQuota = true;
                                quotaNumNotMatched++;
                            }
                        }
                        else if (ch == ',')
                        {
                            if (quotaNumNotMatched > 0)
                            {
                                cell += ch;
                            }
                            else
                            {
                                row.Add(cell.Trim());
                                cell = "";
                            }
                            previousQuota = false;
                        }
                        else
                        {
                            cell         += ch;
                            previousQuota = false;
                        }
                    } // for

                    if (quotaNumNotMatched == 0)
                    {
                        cell = cell.Trim();
                        if (cell.Length > 0)
                        {
                            row.Add(cell);
                            cell = "";
                        }

                        bool emptyRow = true;
                        foreach (string cont in row)
                        {
                            if (cont != "")
                            {
                                emptyRow = false;
                                break;
                            }
                        }

                        if (!emptyRow)  // 排除空行
                        {
                            if (row.Count > this.colNum)
                            {
                                this.colNum = row.Count;
                            }
                            this.grid.Add(row);
                        }

                        row = new List <string>();
                    }
                    else
                    {
                        cell += "\n";   // 单元格中包含换行符
                    }
                } // while
                this.rowNum = this.grid.Count - (this.firstRowAsTitle ? 1 : 0);
                sr.Close();

                this.ready = true;
                XxdwDebugger.Log("CsvSheet: DataThreadFunc() post NOTI_MODEL_DATA_READY.");
                AsyncNotification.Instance.PostNotification(this, Notification.NOTI_MODEL_DATA_READY, this.name);
            }
            catch (System.Exception ex)
            {
                XxdwDebugger.LogError(ex.Message);
            }
        }