Ejemplo n.º 1
0
        /// <summary>
        /// Update the level's metadata on disk.
        /// Does not change the level's timestamp.
        /// </summary>
        /// <param name="level"></param>
        public static void UpdateWorldMetadata(LevelMetadata level)
        {
            try
            {
                string bucket = Utils.FolderNameFromFlags(level.Genres);

                string fullPath = BokuGame.Settings.MediaPath + bucket + level.WorldId.ToString() + @".Xml";

                Xml.XmlWorldData xml = XmlWorldData.Load(fullPath, XnaStorageHelper.Instance);
                if (xml != null)
                {
                    level.ToXml(xml);

                    bool isDownload = (level.Genres & Genres.Downloads) != 0;

                    // Manage the stream ourselves so avoid level timestamp being changed.
                    Stream stream = Storage4.OpenWrite(fullPath);
                    xml.Save(stream, isDownload);
                    Storage4.Close(stream);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Update the level's xml on disk.
        /// Does not change the level's timestamp.
        /// </summary>
        /// <param name="level"></param>
        public static void UpdateWorldXml(XmlWorldData xml)
        {
            try
            {
                string bucket = Utils.FolderNameFromFlags((Genres)xml.genres);

                string fullPath = BokuGame.Settings.MediaPath + bucket + xml.id.ToString() + @".Xml";

                //make sure the world exists
                if (!Storage4.FileExists(fullPath, StorageSource.All))
                {
                    return;
                }

                if (xml != null)
                {
                    bool isDownload = (xml.genres & (int)Genres.Downloads) != 0;

                    // Manage the stream ourselves so avoid level timestamp being changed.
                    Stream stream = Storage4.OpenWrite(fullPath);
                    xml.Save(stream, isDownload);
                    Storage4.Close(stream);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads Actors.xml and initializes the list of static actors.
        /// </summary>
        /// <remarks>
        /// The actors' model and xml file isn't loaded until LoadModels is called.
        /// </remarks>
        public static void LoadActors()
        {
            if (Storage4.FileExists(XmlActorsListFileName, StorageSource.TitleSpace))
            {
                // Read our actor list from file
                var stream     = Storage4.OpenRead(XmlActorsListFileName, StorageSource.TitleSpace);
                var serializer = new XmlSerializer(typeof(XmlActorsList));
                var xmlActors  = serializer.Deserialize(stream) as XmlActorsList;
                Storage4.Close(stream);

                if (xmlActors != null)
                {
                    for (int i = 0; i < xmlActors.Actors.Length; i++)
                    {
                        var xmlStaticActor = xmlActors.Actors[i];
                        Actors.Add(xmlStaticActor.NonLocalizedName, new StaticActor(xmlStaticActor));
                    }
                }
            }
            else
            {
                Debug.Assert(false, "Missing actor file.");
            }
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        }   // end of XmlHelpTextData ReadFromXml()

        private static XmlHelpTextData Load(string filename)
        {
            XmlHelpTextData data   = null;
            Stream          stream = null;

            // First try with StorageSoruce.All so we get the version downloaded
            // from the servers.  If that fails then get the TitleSpace version.
            try
            {
                stream = Storage4.OpenRead(filename, StorageSource.All);

                XmlSerializer serializer = new XmlSerializer(typeof(XmlHelpTextData));
                data = (XmlHelpTextData)serializer.Deserialize(stream);
            }
            catch (Exception e)
            {
                data = null;
                if (e != null)
                {
#if !NETFX_CORE
                    string message = e.Message;
                    if (e.InnerException != null)
                    {
                        message += e.InnerException.Message;
                    }
                    System.Windows.Forms.MessageBox.Show(
                        message,
                        "Error reading " + filename,
                        System.Windows.Forms.MessageBoxButtons.OK,
                        System.Windows.Forms.MessageBoxIcon.Error
                        );
#endif
                }
            }
            finally
            {
                Storage4.Close(stream);
            }

            // If we don't have data.  Delete the server version of
            // the file and try loading the TitleSpace version.
            if (data == null)
            {
                // Don't delete the server version since this might actually be someone
                // trying to do a localization.
                //Storage4.Delete(filename);

                try
                {
                    stream = Storage4.OpenRead(filename, StorageSource.TitleSpace);

                    XmlSerializer serializer = new XmlSerializer(typeof(XmlHelpTextData));
                    data = (XmlHelpTextData)serializer.Deserialize(stream);
                }
                catch (Exception)
                {
                    data = null;
                }
                finally
                {
                    Storage4.Close(stream);
                }
            }

            return(data);
        } // end of XmlHelpTextData Load()
Ejemplo n.º 7
0
 public override void Close(Stream stream)
 {
     Storage4.Close(stream);
 }