Esempio n. 1
0
        /// <summary>
        /// Reads the "stuff" file from the given XmlWorldData.
        /// </summary>
        /// <param name="xmlWorldData"></param>
        /// <param name="gameThingList"></param>
        /// <returns>true on success, false on failure</returns>
        public bool ReadFromXml(XmlWorldData xmlWorldData, InGame.AddThingDelegate AddThing)
        {
            bool success = true;

            // Clear any existing paths.
            WayPoint.ClearPaths();

            // Read the Xml file into local data.
            XmlLevelData data;

            try
            {
                data = Load(BokuGame.Settings.MediaPath + xmlWorldData.stuffFilename, XnaStorageHelper.Instance);
            }
            catch
            {
#if !NETFX_CORE
                Debug.Print("Fail to read file " + BokuGame.Settings.MediaPath + xmlWorldData.stuffFilename);
#endif
                data    = null;
                success = false;
            }

            if (data != null)
            {
                this.waypoints = data.waypoints;
                this.actor     = data.actor;

                data = null;

                ToGame(AddThing);
            }

            return(success);
        }   // end of XmlLevelData ReadFromXml()
        /// <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);
            }
        }
        public static LevelMetadata LoadMetadataByGenre(Guid worldId, Genres genres)
        {
            string bucket = BokuGame.MyWorldsPath;

            if (genres != 0)
            {
                bucket = Utils.FolderNameFromFlags(genres);
            }

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

            Xml.XmlWorldData xml = XmlWorldData.Load(fullPath, XnaStorageHelper.Instance);
            if (xml != null)
            {
                LevelMetadata data = new LevelMetadata();
                data.FromXml(xml);

                //minor hackery - seems previous versions of kodu will sometimes leave the genres set to 0
                //even though they should be updated for the bucket the level is in.  Then at load time, a run-time
                //genre is set.  This code will maintain that behavior for levels loaded through this helper
                if (bucket == BokuGame.DownloadsPath)
                {
                    //ensure downloads always have the downloads flag
                    data.Genres |= Genres.Downloads;
                }
                else if (bucket == BokuGame.BuiltInWorldsPath)
                {
                    //ensure built in worlds always have the built in flag
                    data.Genres |= Genres.BuiltInWorlds;
                }
                else if (bucket == BokuGame.MyWorldsPath)
                {
                    data.Genres |= Genres.MyWorlds;
                }

                return(data);
            }

            return(null);
        }
Esempio n. 4
0
        }   // end of OnAfterSave()

        protected override bool OnLoad()
        {
            // Need to check for localization of any SayStrings in each reflex of each actor's brains.
            foreach (XmlData.Actor a in actor)
            {
                var brain = a.brain;
                foreach (Task task in brain.tasks)
                {
                    foreach (Reflex reflex in task.reflexes)
                    {
                        ReflexData rd = reflex.Data;

                        rd.sayString = TextHelper.CleanUpString(rd.sayString);
                        rd.sayString = XmlWorldData.OnLoadLocalizedString(rd.sayString, ref rd.OriginalSayString, ref rd.LocalizedSayString, ref rd.LocalizedSayStringDict);

                        rd.saidString = TextHelper.CleanUpString(rd.saidString);
                        rd.saidString = XmlWorldData.OnLoadLocalizedString(rd.saidString, ref rd.OriginalSaidString, ref rd.LocalizedSaidString, ref rd.LocalizedSaidStringDict);
                    }
                }
            }

            return(base.OnLoad());
        } // end of OnLoad()
        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);
            }
        }
        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);
        }
Esempio n. 7
0
 public static void getXML(String filename)
 {
     Boku.Common.Xml.XmlWorldData xml = Boku.Common.Xml.XmlWorldData.Load(filename, null);
 }