// In this case, we make an asset reader for our ExampleAsset. public static ExampleAsset ExampleAssetOf(IFileHandle file) { // Open a raw stream to the file using var raw = file.OpenRead(); // Wrap it in a StreamReader, so we can read text using var text = new StreamReader(raw); // Iterate through each line in the file string?line; var lineNumber = 0; while ((line = text.ReadLine()) != null) { const string selector = "> "; ++lineNumber; // Ignore all lines without the selector if (!line.StartsWith(selector)) { continue; } // Trim off the selector var selected = line.Substring(selector.Length); // Return the trimmed line return(new ExampleAsset { Line = lineNumber, Message = selected }); } // Return an empty asset as a last resort return(new ExampleAsset { Line = 0, Message = string.Empty }); }
/// <summary> /// Loads a texture2D from the sent file. Source: https://stackoverflow.com/questions/1080442/how-to-convert-an-stream-into-a-byte-in-c /// </summary> /// <param name="file"></param> /// <returns></returns> public static Texture2D LoadTexture(IFileHandle file) { // Load a PNG or JPG file from disk to a Texture2D // Returns null if load fails Stream fileStream = file.OpenRead(); MemoryStream mem = new MemoryStream(); CopyStream(fileStream, mem); byte[] fileData = mem.ToArray(); Texture2D tex2D = new Texture2D(2, 2); if (tex2D.LoadImage(fileData)) { return(tex2D); } return(null); }
public static void EnsureLegacyFolderExists(IFileHandle legacyManifest) { if (legacyManifest is null) { return; } var manifest = Path.Combine(Constants.LegacyLevelsDirectory, "manifest.json"); if (File.Exists(manifest)) { return; } Directory.CreateDirectory(Constants.LegacyLevelsDirectory); Directory.CreateDirectory(Path.Combine(Constants.LegacyLevelsDirectory, "TakeAndHold")); Directory.CreateDirectory(Path.Combine(Constants.LegacyLevelsDirectory, "Other")); var stream = legacyManifest.OpenRead(); var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); File.WriteAllBytes(manifest, buffer); }
/// <inheritdoc cref="IFileHandle.OpenRead"/> public Stream OpenRead() { this.ThrowIfDead(); return(_handle.OpenRead()); }