예제 #1
0
        public static BokuShared.Wire.WorldPacket ReadWorldPacketFromDisk(string worldFullPathAndName, string bucket)
        {
            BokuShared.Wire.WorldPacket packet = null;
            Stream file = null;

            try
            {
                string localLevelPath = BokuGame.Settings.MediaPath + bucket;
                string worldFilename  = Path.GetFileName(worldFullPathAndName);

                // Read contents of world xml to retrieve the names of the dependent
                // files we need to upload
                Xml.XmlWorldData xmlWorldData = XmlWorldData.Load(localLevelPath + worldFilename, XnaStorageHelper.Instance);
                if (xmlWorldData == null)
                {
                    return(null);
                }

                // Clear virtual genre bits in case they got saved (server clears them too).
                xmlWorldData.genres &= ~(int)Genres.Virtual;

                packet = new BokuShared.Wire.WorldPacket();
                packet.Info.WorldId     = packet.Data.WorldId = xmlWorldData.id;
                packet.Info.Name        = xmlWorldData.name;
                packet.Info.Description = xmlWorldData.description;
                packet.Info.Creator     = xmlWorldData.creator;
                packet.Info.IdHash      = "";
                packet.Info.Genres      = xmlWorldData.genres;

                string imageFileName = xmlWorldData.GetImageFilenameWithoutExtension();

                // VirtualMap
                file = Storage4.OpenRead(BokuGame.Settings.MediaPath + xmlWorldData.xmlTerrainData2.virtualMapFile, StorageSource.All);
                packet.Data.VirtualMapBytes = new byte[file.Length];
                file.Read(packet.Data.VirtualMapBytes, 0, (int)file.Length);
                Storage4.Close(file);

                // Stuff xml
                file = Storage4.OpenRead(BokuGame.Settings.MediaPath + xmlWorldData.stuffFilename, StorageSource.All);
                packet.Data.StuffXmlBytes = new byte[file.Length];
                file.Read(packet.Data.StuffXmlBytes, 0, (int)file.Length);
                Storage4.Close(file);

                // Optional: don't worry if we don't have a thumbnail image.
                try
                {
                    file = null;
                    file = Storage4.TextureFileOpenRead(localLevelPath + imageFileName);

                    if (file != null)
                    {
                        packet.Info.ThumbnailBytes = new byte[file.Length];
                        file.Read(packet.Info.ThumbnailBytes, 0, (int)file.Length);
                        Storage4.Close(file);
                    }
                }
                catch { }


                // Try To load Snapshot image.
                try
                {
                    file = null;
                    file = Storage4.TextureFileOpenRead(localLevelPath + imageFileName, Storage4.TextureFileType.jpg);

                    if (file != null)
                    {
                        packet.Info.ScreenshotBytes = new byte[file.Length];
                        file.Read(packet.Info.ScreenshotBytes, 0, (int)file.Length);
                        Storage4.Close(file);
                    }
                }
                catch { }


                // We've successfully read all required files. We may now upload them to the server.
                file = Storage4.OpenRead(localLevelPath + worldFilename, StorageSource.All);
                packet.Data.WorldXmlBytes = new byte[file.Length];
                file.Read(packet.Data.WorldXmlBytes, 0, (int)file.Length);
                Storage4.Close(file);

                Instrumentation.RecordEvent(Instrumentation.EventId.LevelUploaded, xmlWorldData.name);
            }
            catch
            {
                if (file != null)
                {
                    Storage4.Close(file);
                }
                packet = null;
            }

            return(packet);
        }
예제 #2
0
        public static bool WriteWorldDataPacketToDisk(BokuShared.Wire.WorldDataPacket packet, byte[] thumbnailBytes, DateTime timeStamp)
        {
            Stream file = null;

            try
            {
                // Check for presence of the essential world data
                if (packet == null)
                {
                    return(false);
                }
                if (packet.WorldXmlBytes == null)
                {
                    return(false);
                }
                if (packet.StuffXmlBytes == null)
                {
                    return(false);
                }

                // Read in contents of world xml buffer
                Xml.XmlWorldData xmlWorldData = Xml.XmlWorldData.Load(packet.WorldXmlBytes);
                if (xmlWorldData == null)
                {
                    return(false);
                }

                xmlWorldData.overrideLastWriteTime = timeStamp;

                xmlWorldData.id            = packet.WorldId;
                xmlWorldData.stuffFilename = BokuGame.DownloadsStuffPath + xmlWorldData.Filename;

                // Non-essential file: Write thumbnail image to disk
                if (thumbnailBytes != null)
                {
                    string ext           = Storage4.TextureExt(thumbnailBytes);
                    string thumbFilename = xmlWorldData.GetImageFilenameWithoutExtension() + "." + ext;
                    file = Storage4.OpenWrite(BokuGame.Settings.MediaPath + BokuGame.DownloadsPath + thumbFilename);
                    file.Write(thumbnailBytes, 0, thumbnailBytes.Length);
                    Storage4.Close(file);
                    file = null;
                }

                // Cubeworld virtual terrain map
                if (packet.VirtualMapBytes != null)
                {
                    file = Storage4.OpenWrite(BokuGame.Settings.MediaPath + xmlWorldData.xmlTerrainData2.virtualMapFile);
                    file.Write(packet.VirtualMapBytes, 0, packet.VirtualMapBytes.Length);
                    Storage4.Close(file);
                    file = null;
                }

                // Write stuff xml to disk
                file = Storage4.OpenWrite(BokuGame.Settings.MediaPath + xmlWorldData.stuffFilename);
                file.Write(packet.StuffXmlBytes, 0, packet.StuffXmlBytes.Length);
                Storage4.Close(file);
                file = null;

                // Clear virtual genre bits because they should not be stored.
                xmlWorldData.genres &= ~(int)Genres.Virtual;
                xmlWorldData.genres &= ~(int)Genres.Favorite;

                // Serialize xmlWorldData to disk
                string fullPath = BokuGame.Settings.MediaPath + BokuGame.DownloadsPath + xmlWorldData.Filename;
                xmlWorldData.Save(fullPath, XnaStorageHelper.Instance);

                Instrumentation.RecordEvent(Instrumentation.EventId.LevelDownloaded, xmlWorldData.name);

                return(true);
            }
            catch
            {
                if (file != null)
                {
                    Storage4.Close(file);
                }

                return(false);
            }
        }
예제 #3
0
        }   // end of GetDeviceDesc()

        /// <summary>
        /// Detect attached microbits.
        /// </summary>
        /// <param name="createDevices">Whether or not to open interfaces to attached microbits.</param>
        /// <returns>The number of attached microbits.</returns>
        public static int RefreshDevices(bool createDevices = true)
        {
            // If specified on the command line, do not scan for microbits.
            if (Program2.CmdLine.Exists("NoMicrobit"))
            {
                return(0);
            }

            int prevDeviceCount = Microbits.Count;

            ReleaseDevices();

#if false
            // Print the Caption and DeviceID of all connected PNP devices. This
            // is helpful when you need to discover what the exact device id is
            // for a particular device you want to be able to detect.
            {
                using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity"))
                    using (var collection = searcher.Get())
                    {
                        foreach (var device in collection)
                        {
                            string Caption  = (string)device.GetPropertyValue("Caption");
                            string DeviceID = (string)device.GetPropertyValue("DeviceID");
                            System.Diagnostics.Debug.WriteLine(Caption + " ---- " + DeviceID);
                        }
                    }
            }
#endif

            List <MicrobitDesc> microbitDescs = new List <MicrobitDesc>();

            // Detect microbits.
            microbitDescs = GetDeviceDesc();

            // Open interfaces to devices.
            if (createDevices)
            {
                if (DriverInstalled)
                {
                    if (microbitDescs != null && microbitDescs.Count > 0)
                    {
                        int microbitIndex = (int)GamePadSensor.PlayerId.One;
                        foreach (MicrobitDesc desc in microbitDescs)
                        {
                            Microbit microbit = Microbit.Create(desc);
                            if (microbit != null)
                            {
                                Microbits.TryAdd(microbitIndex++, microbit);
                            }
                        }
                    }
                }
                else
                {
                    // Do nothing here.  The main thread loop will notice that DirverInstalled is false
                    // and should put up the needed dialog there.  We can't do it here since the dialog
                    // can't be shown on a background thread.
                }
            }

            // If none were found, check if user tried the command line.
            if (microbitDescs.Count == 0 && Program2.MicrobitCmdLine != null)
            {
                if (Program2.MicrobitCmdLine.Length == 7)
                {
                    MicrobitDesc desc = new MicrobitDesc();
                    desc.COM   = Program2.MicrobitCmdLine.Substring(0, 4);
                    desc.Drive = Program2.MicrobitCmdLine.Substring(5);
                    microbitDescs.Add(desc);
                }
            }

            int deviceCount = createDevices ? Microbits.Count : microbitDescs.Count;

            // If any microbits were detected, then permanently enable visibility of the microbit programming tiles.
            if (!XmlOptionsData.ShowMicrobitTiles && deviceCount > 0)
            {
                XmlOptionsData.ShowMicrobitTiles = true;
                Instrumentation.RecordEvent(Instrumentation.EventId.MicrobitTilesEnabled, "");
            }

            // Track the number of microbits attached at one time.
            if (prevDeviceCount < deviceCount)
            {
                Instrumentation.SetCounter(Instrumentation.CounterId.MicrobitCount, deviceCount);
            }

            return(deviceCount);
        }