Skip to content

Commit

Permalink
Fix: Saved ouput pattern with invalid character in name resulting in …
Browse files Browse the repository at this point in the history
…SC crashing on startup
  • Loading branch information
Piotrekol committed Jun 17, 2023
1 parent fcd62a0 commit ac679d5
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 330 deletions.
12 changes: 12 additions & 0 deletions osu!StreamCompanion/Code/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;

namespace osu_StreamCompanion.Code.Helpers
{
Expand All @@ -17,5 +18,16 @@ public static bool TryFormat(this string str, out string result, params string[]
return false;
}
}

public static string RemoveInvalidFileNameChars(this string fileName)
{
foreach (var c in Path.GetInvalidFileNameChars())
{
if (fileName.Contains(c))
fileName = fileName.Replace(c.ToString(), "");
}

return fileName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using StreamCompanionTypes.Enums;
using StreamCompanionTypes.Interfaces.Services;
using StreamCompanionTypes.Interfaces.Sources;
using osu_StreamCompanion.Code.Helpers;

namespace osu_StreamCompanion.Code.Modules.MapDataParsers.Parser1
{
Expand Down Expand Up @@ -194,7 +195,7 @@ private void Load()
if (deserializedPatterns != null)
foreach (var p in deserializedPatterns)
{
p.Name = p.Name.RemoveWhitespace();
p.Name = p.Name.RemoveInvalidFileNameChars().RemoveWhitespace();
_patterns.Add(p);
}
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using StreamCompanion.Common;
using StreamCompanionTypes;
using StreamCompanionTypes.Enums;
using osu_StreamCompanion.Code.Helpers;

namespace osu_StreamCompanion.Code.Modules.MapDataParsers.Parser1
{
Expand Down Expand Up @@ -145,17 +146,7 @@ private void button_Click(object sender, EventArgs e)
DeletePattern?.Invoke(this, Current);
}
}

private void textBox_FileName_KeyPress(object sender, KeyPressEventArgs e)
{
foreach (var c in Path.GetInvalidFileNameChars())
{
textBox_FileName.Text = textBox_FileName.Text.Replace(c.ToString(), "");
}

textBox_FileName.Text = textBox_FileName.Text.RemoveWhitespace();
}


private bool longFormat = false;
public async void textBoxUpdateLoop(Tokens replacements)
{
Expand Down Expand Up @@ -273,5 +264,10 @@ private void numericUpDown_colorAlpha_ValueChanged(object sender, EventArgs e)
panel_ColorPreview.BackColor = label_TestText.ForeColor =
Color.FromArgb((int)numericUpDown_colorAlpha.Value, panel_ColorPreview.BackColor);
}

private void textBox_FileName_KeyUp(object sender, KeyEventArgs e)
{
textBox_FileName.Text = textBox_FileName.Text.RemoveInvalidFileNameChars().RemoveWhitespace();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down
8 changes: 6 additions & 2 deletions plugins/FileMapDataSender/FileMapDataSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@
using System.Threading;
using System.Threading.Tasks;
using StreamCompanionTypes.Interfaces.Consumers;
using StreamCompanionTypes.Interfaces.Services;

namespace FileMapDataSender
{
[SupportedOSPlatform("windows")]
public class FileMapDataSender : IPlugin, IMapDataConsumer, IDisposable, IHighFrequencyDataConsumer
{
public bool Started { get; set; }
private readonly FileMapManager _fileMapManager = new FileMapManager();
private readonly FileMapManager _fileMapManager;

public string Description { get; } = "";
public string Name { get; } = "FileMapDataSender";
public string Author { get; } = "Piotrekol";
public string Url { get; } = "";
public string UpdateUrl { get; } = "";


public FileMapDataSender(ILogger logger)
{
_fileMapManager = new FileMapManager(logger);
}
public void Save(string fileName, string content)
{
_fileMapManager.Write(fileName, content);
Expand Down
23 changes: 19 additions & 4 deletions plugins/FileMapDataSender/FileMapManager.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
using StreamCompanionTypes.Interfaces.Services;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Runtime.Versioning;
using System.Text;
using StreamCompanionTypes.Enums;

namespace FileMapDataSender
{
public class FileMapManager
{
Dictionary<string, MapContainer> _files = new Dictionary<string, MapContainer>();
private ILogger _logger;
private readonly object _lockingObject = new object();

public FileMapManager(ILogger logger)
{
_logger = logger;
}

private class MapContainer
{
public MemoryMappedFile File { get; set; }
Expand Down Expand Up @@ -58,9 +67,15 @@ private MapContainer GetFile(string pipeName)
[SupportedOSPlatform("windows")]
public void Write(string name, string value)
{
var file = GetFile(name);
file.Write(value);

try
{
var file = GetFile(name);
file.Write(value);
}
catch (IOException ex)
{
_logger.Log(ex, LogLevel.Warning);
}
}
}
}

0 comments on commit ac679d5

Please sign in to comment.