2015-11-06 14:31:50 +00:00
using System ;
using System.IO ;
using Newtonsoft.Json ;
//this file contains some cumbersome self-"serialization" in order to gain a modicum of control over what the serialized output looks like
//I don't want them to look like crufty json
2018-12-22 18:40:30 +00:00
namespace BizHawk.Client.Common
2015-11-06 14:31:50 +00:00
{
public interface IOpenAdvanced
{
string TypeName { get ; }
string DisplayName { get ; }
/// <summary>
/// returns a sole path to use for opening a rom (not sure if this is a good idea)
/// </summary>
string SimplePath { get ; }
void Deserialize ( string str ) ;
void Serialize ( TextWriter tw ) ;
}
public interface IOpenAdvancedLibretro
{
string CorePath { get ; set ; }
}
public static class OpenAdvancedTypes
{
public const string OpenRom = "OpenRom" ;
public const string Libretro = "Libretro" ;
public const string LibretroNoGame = "LibretroNoGame" ;
2019-10-29 15:37:27 +00:00
public const string MAME = "MAME" ;
2015-11-06 14:31:50 +00:00
}
public class OpenAdvancedSerializer
{
public static IOpenAdvanced ParseWithLegacy ( string text )
{
2020-03-01 20:45:18 +00:00
return text . StartsWith ( "*" )
? Deserialize ( text . Substring ( 1 ) )
: new OpenAdvanced_OpenRom { Path = text } ;
2015-11-06 14:31:50 +00:00
}
private static IOpenAdvanced Deserialize ( string text )
{
int idx = text . IndexOf ( '*' ) ;
string type = text . Substring ( 0 , idx ) ;
string token = text . Substring ( idx + 1 ) ;
2019-10-29 15:37:27 +00:00
2020-03-01 21:49:48 +00:00
var ioa = type switch
2019-11-04 01:55:38 +00:00
{
2020-03-01 21:49:48 +00:00
OpenAdvancedTypes . OpenRom = > ( IOpenAdvanced ) new OpenAdvanced_OpenRom ( ) ,
OpenAdvancedTypes . Libretro = > new OpenAdvanced_Libretro ( ) ,
OpenAdvancedTypes . LibretroNoGame = > new OpenAdvanced_LibretroNoGame ( ) ,
OpenAdvancedTypes . MAME = > new OpenAdvanced_MAME ( ) ,
_ = > null
} ;
2019-10-29 15:37:27 +00:00
2019-11-04 01:55:38 +00:00
if ( ioa = = null )
2019-10-29 15:37:27 +00:00
{
2019-11-04 01:55:38 +00:00
throw new InvalidOperationException ( $"{nameof(IOpenAdvanced)} deserialization error" ) ;
2019-10-29 15:37:27 +00:00
}
2015-11-06 14:31:50 +00:00
ioa . Deserialize ( token ) ;
return ioa ;
}
public static string Serialize ( IOpenAdvanced ioa )
{
2020-03-01 21:49:48 +00:00
var sw = new StringWriter ( ) ;
2015-11-06 14:31:50 +00:00
sw . Write ( "{0}*" , ioa . TypeName ) ;
ioa . Serialize ( sw ) ;
return sw . ToString ( ) ;
}
}
2018-12-22 18:40:30 +00:00
public class OpenAdvanced_Libretro : IOpenAdvanced , IOpenAdvancedLibretro
2015-11-06 14:31:50 +00:00
{
public struct Token
{
public string Path , CorePath ;
}
2020-03-01 20:45:18 +00:00
2020-02-22 18:29:12 +00:00
public Token token ;
2015-11-06 14:31:50 +00:00
2020-02-22 18:29:12 +00:00
public string TypeName = > "Libretro" ;
public string DisplayName = > $"{Path.GetFileNameWithoutExtension(token.CorePath)}: {token.Path}" ;
public string SimplePath = > token . Path ;
2015-11-06 14:31:50 +00:00
public void Deserialize ( string str )
{
token = JsonConvert . DeserializeObject < Token > ( str ) ;
}
public void Serialize ( TextWriter tw )
{
tw . Write ( JsonConvert . SerializeObject ( token ) ) ;
}
2020-01-25 04:53:45 +00:00
public string CorePath
{
get = > token . CorePath ;
set = > token . CorePath = value ;
}
2015-11-06 14:31:50 +00:00
}
2018-12-22 18:40:30 +00:00
public class OpenAdvanced_LibretroNoGame : IOpenAdvanced , IOpenAdvancedLibretro
2015-11-06 14:31:50 +00:00
{
2020-02-22 18:29:12 +00:00
// you might think ideally we'd fetch the libretro core name from the core info inside it
// but that would involve spinning up excess libretro core instances, which probably isn't good for stability, no matter how much we wish otherwise, not to mention slow.
// moreover it's kind of complicated here,
// and finally, I think the DisplayName should really be file-based in all cases, since the user is going to be loading cores by filename and
// this is related to the recent roms filename management.
// so, leave it.
2015-11-06 14:31:50 +00:00
public OpenAdvanced_LibretroNoGame ( )
{
}
2020-02-22 18:29:12 +00:00
public OpenAdvanced_LibretroNoGame ( string corePath )
2015-11-06 14:31:50 +00:00
{
2020-03-01 20:45:18 +00:00
CorePath = corePath ;
2015-11-06 14:31:50 +00:00
}
2020-02-22 18:29:12 +00:00
public string TypeName = > "LibretroNoGame" ;
2020-03-01 20:45:18 +00:00
public string DisplayName = > Path . GetFileName ( CorePath ) ; // assume we like the filename of the core
2020-02-22 18:29:12 +00:00
public string SimplePath = > "" ; // effectively a signal to not use a game
2015-11-06 14:31:50 +00:00
public void Deserialize ( string str )
{
2020-03-01 20:45:18 +00:00
CorePath = str ;
2015-11-06 14:31:50 +00:00
}
public void Serialize ( TextWriter tw )
{
2020-03-01 20:45:18 +00:00
tw . Write ( CorePath ) ;
2015-11-06 14:31:50 +00:00
}
2020-03-01 20:45:18 +00:00
public string CorePath { get ; set ; }
2015-11-06 14:31:50 +00:00
}
2018-12-22 18:40:30 +00:00
public class OpenAdvanced_OpenRom : IOpenAdvanced
2015-11-06 14:31:50 +00:00
{
public string Path ;
2020-02-22 18:29:12 +00:00
public string TypeName = > "OpenRom" ;
public string DisplayName = > Path ;
public string SimplePath = > Path ;
2015-11-06 14:31:50 +00:00
public void Deserialize ( string str )
{
Path = str ;
}
public void Serialize ( TextWriter tw )
{
tw . Write ( Path ) ;
}
}
2019-10-29 15:37:27 +00:00
public class OpenAdvanced_MAME : IOpenAdvanced
{
public string Path ;
2020-02-22 18:29:12 +00:00
public string TypeName = > "MAME" ;
public string DisplayName = > $"{TypeName}: {Path}" ;
public string SimplePath = > Path ;
2019-10-29 15:37:27 +00:00
public void Deserialize ( string str )
{
Path = str ;
}
public void Serialize ( TextWriter tw )
{
tw . Write ( Path ) ;
}
}
2015-11-06 14:31:50 +00:00
}