private void TestXml2() { GamePadInput pad = GamePadInput.GetGamePad0(); if (pad.ButtonY.WasPressed) { doTest = 0; } if (doTest >= 0) { if (!Storage4.FileExists(BokuGame.Settings.MediaPath + @"Xml\OptionsData.Xml")) { doTest = 0; /// This breakpoint isn't ever hit, OptionsData.xml exists here } Stream hoseStream = Storage4.OpenWrite(@"Xml\Hoser.xml"); XmlHose hoser = new XmlHose(); hoser.hoser = true; XmlSerializer serializer = new XmlSerializer(typeof(XmlHose)); serializer.Serialize(hoseStream, hoser); Storage4.Close(hoseStream); if (!Storage4.FileExists(BokuGame.Settings.MediaPath + @"Xml\OptionsData.Xml")) { doTest = 0; /// THis breakpoint is hit on failure, after the flush /// OptionsData.xml is not longer there. } ++testsDone; } }
private void TestStorage() { GamePadInput pad = GamePadInput.GetGamePad0(); if (pad.ButtonY.WasPressed) { doTest = 0; } if (doTest >= 0) { string fileName = "Test0.dat"; if (!Storage4.FileExists(fileName)) { doTest = 0; } Stream writeStream = Storage4.OpenWrite(fileName); if (writeStream == null) { doTest = 0; } byte[] writeBytes = new byte[10000]; writeStream.Write(writeBytes, 0, writeBytes.Length); Storage4.Close(writeStream); if (!Storage4.FileExists(fileName)) { doTest = 0; } ++testsDone; } }
/// <summary> /// Load current options data. /// </summary> /// <returns></returns> private static XmlOptionsData Load() { XmlOptionsData xmlData = null; Stream stream = null; DateTime xmlFileTime = new DateTime();//used to compare against InstallLanguage.txt try { // Try the real filename first. string xmlFileName = FileName(); if (Storage4.FileExists(xmlFileName, StorageSource.All)) { xmlFileTime = Storage4.GetLastWriteTimeUtc(xmlFileName, StorageSource.All); stream = Storage4.OpenRead(xmlFileName, StorageSource.All); XmlSerializer serializer = new XmlSerializer(typeof(XmlOptionsData)); xmlData = serializer.Deserialize(stream) as XmlOptionsData; } else { // If not there, try the back compat version. xmlFileName = BackCompatFileName(); if (Storage4.FileExists(xmlFileName, StorageSource.All)) { xmlFileTime = Storage4.GetLastWriteTimeUtc(xmlFileName, StorageSource.All); stream = Storage4.OpenRead(xmlFileName, StorageSource.All); XmlSerializer serializer = new XmlSerializer(typeof(XmlOptionsData)); xmlData = serializer.Deserialize(stream) as XmlOptionsData; } } } catch (Exception e) { Debug.WriteLine(e.Message); } if (stream != null) { Storage4.Close(stream); } //Check for override of language. if (xmlData != null && Storage4.FileExists(InstalledLanguageFileName(), StorageSource.All)) { var langFileTime = Storage4.GetLastWriteTimeUtc(InstalledLanguageFileName(), StorageSource.All); //If InstallerLanguage.txt is later than selected language. if (langFileTime > xmlFileTime) { //Update language to match newly installed version. xmlData.langauge = GetInstallerLanguageOrDefault(); Save(); } } return(xmlData); } // end of Load()
public void Serialize(string filepath) { string pathName = MapFolder + filepath; XmlSerializer serializer = new XmlSerializer(typeof(CommandMap)); Stream stream = Storage4.OpenWrite(pathName); serializer.Serialize(stream, this); Storage4.Close(stream); }
} // end of SetXButton() public override void Update(ref Matrix parentMatrix) { // Check for input but only if selected. if (selected) { GamePadInput pad = GamePadInput.GetGamePad0(); if (Actions.Select.WasPressed) { Actions.Select.ClearAllWasPressedState(); onSetNextLevel(); } if (Actions.X.WasPressed) { Actions.X.ClearAllWasPressedState(); NextLevel = null; if (onClear != null) { onClear(); } } } //load thumbnail if it hasn't loaded yet if (nextLevel != null && !nextLevel.Thumbnail.IsLoaded) { if (nextLevel.ThumbnailBytes == null) { string texFilename = BokuGame.Settings.MediaPath + Utils.FolderNameFromFlags(nextLevel.Genres) + nextLevel.WorldId.ToString(); Stream texStream = Storage4.TextureFileOpenRead(texFilename); if (texStream != null) { nextLevel.Thumbnail.Texture = Storage4.TextureLoad(texStream); Storage4.Close(texStream); } } else { MemoryStream stream = new MemoryStream(nextLevel.ThumbnailBytes); nextLevel.Thumbnail.Texture = Storage4.TextureLoad(stream); nextLevel.ThumbnailBytes = null; } } //refresh the render target RefreshTexture(); base.Update(ref parentMatrix); } // end of UIGridModularCheckboxElement Update()
} // end of HeightMap c'tor /// <summary> /// Writes the current height map to a file. /// </summary> /// <param name="filename"></param> public void Save(string filename) { Stream fs = Storage4.OpenWrite(filename); BinaryWriter bw = new BinaryWriter(fs); Save(bw); #if NETFX_CORE bw.Flush(); bw.Dispose(); #else bw.Close(); #endif Storage4.Close(fs); } // end of HeightMap Save()
public HeightMap(string filename, Point size, Vector3 scale) { this.filename = filename; Init(Vector2.Zero, size, scale); // Load the heightmap. Stream fs = Storage4.OpenRead(filename, StorageSource.All); BinaryReader br = new BinaryReader(fs); Load(br); #if NETFX_CORE br.Dispose(); #else br.Close(); #endif Storage4.Close(fs); } // end of HeightMap c'tor
public static void UnloadContent() { if (instance == null) { return; } #if DEBUG try { Stream stream = Storage4.OpenWrite(LocalizationResourceManager.HelpResource.Name); XmlSerializer serializer = new XmlSerializer(typeof(Help)); serializer.Serialize(stream, instance); Storage4.Close(stream); } catch { } #endif }
} // end of XmlHelpScreensData ReadFromXml() private static XmlHelpScreensData Load(string filename) { // Fix up the filename with the full path. filename = BokuGame.Settings.MediaPath + @"Xml\" + filename; XmlHelpScreensData data = null; try { Stream stream = Storage4.OpenRead(filename, StorageSource.All); XmlSerializer serializer = new XmlSerializer(typeof(XmlHelpScreensData)); data = (XmlHelpScreensData)serializer.Deserialize(stream); Storage4.Close(stream); } catch (Exception) { data = null; } return(data); } // end of XmlHelpScreensData Load()
} // end of Load() private static void Save() { Stream stream = null; try { string xmlFileName = FileName(); XmlSerializer serializer = new XmlSerializer(typeof(XmlOptionsData)); stream = Storage4.OpenWrite(xmlFileName); serializer.Serialize(stream, _instance); } catch (Exception e) { Debug.WriteLine(e.Message); } if (stream != null) { Storage4.Close(stream); } } // end of Save()
static protected CommandMap Deserialize(string filepath) { string oldPathName = Storage4.TitleLocation + MapFolder + filepath; string pathName = MapFolder + filepath; CommandMap profile; // try { Stream stream = Storage4.OpenRead(pathName, StorageSource.TitleSpace); XmlSerializer serializer = new XmlSerializer(typeof(CommandMap)); profile = (CommandMap)serializer.Deserialize(stream); Storage4.Close(stream); } /* * catch (FileNotFoundException ex) * { * profile = new CommandMap(); * profile.Serialize(pathName); * } */ return(profile); }
/// <summary> /// Load a named resource, assuming file naming convention. /// </summary> /// <param name="name"></param> /// <returns></returns> public static XmlGameActor Deserialize(string name) { XmlGameActor xmlActor = null; string xmlFileName = Prefix + name + Suffix; if (Storage4.FileExists(xmlFileName, StorageSource.TitleSpace)) { Stream stream = Storage4.OpenRead(xmlFileName, StorageSource.TitleSpace); XmlSerializer serializer = new XmlSerializer(typeof(XmlGameActor)); xmlActor = serializer.Deserialize(stream) as XmlGameActor; Storage4.Close(stream); xmlActor.XmlFileName = xmlFileName; if (xmlActor != null) { xmlActor.OnLoad(); allActors.Add(xmlActor); } } else { //Debug.Assert(false, "Missing actor file"); } return(xmlActor); }
} // end of Init() #endregion #region Internal /// <summary> /// Loads the Crumb information. Will first try the downloaded /// version. If that fails, will then try the TitleSpace version. /// </summary> /// <param name="filename"></param> /// <param name="source">Where to look for the file.</param> /// <returns></returns> private static CrumbList Load(string filename, StorageSource source) { CrumbList 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, source); XmlSerializer serializer = new XmlSerializer(typeof(CrumbList)); data = (CrumbList)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 TutorialCrumbs.Xml", 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(CrumbList)); data = (CrumbList)serializer.Deserialize(stream); } catch (Exception) { data = null; } finally { Storage4.Close(stream); } } return(data); } // end of Load()
/// <summary> /// Shows the current file. Should be called each frame with /// the same name until this returns true indicating that the /// frame has been displayed for enough time. /// </summary> /// <param name="filename"></param> /// <returns>True when done</returns> private bool ShowFile(string filename) { // New file has been sent. Init. if (filename != curFilename) { curFilename = filename; curPage = -1; startTime = 0; try { stream = Storage4.OpenRead(BokuGame.Settings.MediaPath + filename, StorageSource.All); } catch { return(true); } if (stream == null) { return(true); } fileSize = (int)stream.Length; numPages = fileSize / bytesPerPage; if (numPages * bytesPerPage != fileSize) { ++numPages; } } if (setStartTime) { startTime = Time.WallClockTotalSeconds; setStartTime = false; } // If we timed out, then either load/show next // page or return done if on last page. Double curTime = Time.WallClockTotalSeconds; if (startTime + kPageTime < curTime) { ++curPage; if (curPage >= numPages) { // Done with this file. Storage4.Close(stream); // Allow the same file to be sent twice in a row. curFilename = null; return(true); } else { // Not done so send the next block. setStartTime = true; startTime = curTime; // Clear the data block. for (int j = 0; j < blockH; j++) { for (int i = 0; i < blockW; i++) { shared.data[i + j * blockW] = 0; } } // Load next page of data. int n = fileSize - curPage * bytesPerPage; if (n > bytesPerPage) { n = bytesPerPage; } for (int i = 0; i < n; i++) { shared.data[i] = (byte)stream.ReadByte(); } // Put file length on line blockH - 4 starting at x = 0 int index = (blockH - 4) * blockW; shared.data[index++] = (byte)(fileSize & 0x000000ff); shared.data[index++] = (byte)((fileSize & 0x0000ff00) >> 8); shared.data[index++] = (byte)((fileSize & 0x00ff0000) >> 16); shared.data[index++] = (byte)((fileSize & 0xff000000) >> 24); // numPages shared.data[index++] = (byte)(numPages & 0x000000ff); shared.data[index++] = (byte)((numPages & 0x0000ff00) >> 8); shared.data[index++] = (byte)((numPages & 0x00ff0000) >> 16); shared.data[index++] = (byte)((numPages & 0xff000000) >> 24); // bytesPerPage shared.data[index++] = (byte)(bytesPerPage & 0x000000ff); shared.data[index++] = (byte)((bytesPerPage & 0x0000ff00) >> 8); shared.data[index++] = (byte)((bytesPerPage & 0x00ff0000) >> 16); shared.data[index++] = (byte)((bytesPerPage & 0xff000000) >> 24); // curPage shared.data[index++] = (byte)(curPage & 0x000000ff); shared.data[index++] = (byte)((curPage & 0x0000ff00) >> 8); shared.data[index++] = (byte)((curPage & 0x00ff0000) >> 16); shared.data[index++] = (byte)((curPage & 0xff000000) >> 24); // Put filename on line blockH - 3. index = (blockH - 3) * blockW; for (int i = 0; i < curFilename.Length; i++) { char c = curFilename[i]; shared.data[index++] = (byte)(c & 0x00ff); shared.data[index++] = (byte)((c >> 8) & 0x00ff); } // Calc CRC32 for block, skipping bottom row. byte[] crc = MyMath.CRC32(shared.data, blockW * (blockH - 1)); // Set CRC32 data in bottom line. for (int i = 0; i < 4; i++) { shared.data[i + (blockH - 1) * blockW] = crc[i]; } shared.dataChanged = true; } // end of setting up next block } // end if timed out return(false); } // end of ShowFile()
/// <summary> /// This is the worker thread that loads the datasources from disk. /// </summary> private void ReadLevelsProc() { ReadDataSource("BuiltInWorlds", Genres.BuiltInWorlds, StorageSource.TitleSpace); ReadDataSource("MyWorlds", Genres.MyWorlds, StorageSource.UserSpace); ReadDataSource("Downloads", Genres.Downloads, StorageSource.UserSpace); #if NETFX_CORE working = false; #else thread = null; #endif for (; ;) { if (!running || !BokuGame.Running) { break; } // Wait for a wake-up signal #if NETFX_CORE if (!signal.WaitOne(10)) { continue; } #else if (!signal.WaitOne(10, false)) { continue; } #endif // Process all queued thumbnail load requests. for (; ;) { lock (Synch) { if (thumbnailQueue.Count == 0 || !running || !BokuGame.Running) { break; } LevelMetadata level = thumbnailQueue[thumbnailQueue.Count - 1]; thumbnailQueue.RemoveAt(thumbnailQueue.Count - 1); try { string texFilename = BokuGame.Settings.MediaPath + Utils.FolderNameFromFlags(level.Genres) + level.WorldId.ToString(); Stream texStream = OpenTextureStream(texFilename); if (texStream != null) { level.ThumbnailBytes = new byte[texStream.Length]; texStream.Read(level.ThumbnailBytes, 0, (int)texStream.Length); Storage4.Close(texStream); thumbnailCompletions.Add(level); } } catch { } } // Let the main thread have the cpu so it can deliver the thumbnail to the level. #if NETFX_CORE { System.Threading.Tasks.Task delayTask = System.Threading.Tasks.Task.Delay(1); delayTask.ConfigureAwait(false); delayTask.Wait(); } #else Thread.Sleep(1); #endif } } }