Class BrainSerializerBase<TState, TBot, TAction>
A helper class for loading and saving bot brains.
Inheritance
Inherited Members
Namespace: Microsoft.XboxStudios.BotBrain.Composition
Assembly: BotBrain.Core.dll
Syntax
public abstract class BrainSerializerBase<TState, TBot, TAction> : ResourceHelper, IBrainSerializer, ITitleResourceHelper, IResourceHelper, IMetadataAccess where TBot : IAutomatedBot<TAction>
Type Parameters
| Name | Description |
|---|---|
| TState | A title-specific state object. |
| TBot | A title-specific bot object that implements IAutomatedBot<TAction>. |
| TAction | The title-specific bot action that implements IEquatable<T>. |
Constructors
View SourceBrainSerializerBase(IServiceProvider, ILogger<BrainSerializerBase<TState, TBot, TAction>>?, string?, Assembly?)
Initializes a new instance of the BrainSerializerBase<TState, TBot, TAction> class.
Declaration
protected BrainSerializerBase(IServiceProvider services, ILogger<BrainSerializerBase<TState, TBot, TAction>>? logger = null, string? outputFolder = null, Assembly? brainAssembly = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceProvider | services | The service provider used for construction. |
| ILogger<BrainSerializerBase<TState, TBot, TAction>> | logger | The logger to use. |
| string | outputFolder | The folder path containing brain resources. |
| Assembly | brainAssembly | The assembly containing brain types to register and resources to extract. |
Fields
View SourceDefaultBotBrainOutputFolder
The default output folder for bot brain resources.
Declaration
public const string DefaultBotBrainOutputFolder = "BotBrain"
Field Value
| Type | Description |
|---|---|
| string |
Properties
View SourceBotBrainFileExtension
Gets the file extension for bot brain files.
Declaration
public virtual string BotBrainFileExtension { get; }
Property Value
| Type | Description |
|---|---|
| string |
BrainAssembly
Gets the assembly containing brain types to register and resources to extract.
Declaration
protected Assembly BrainAssembly { get; }
Property Value
| Type | Description |
|---|---|
| Assembly |
BrainCoreAssembly
Gets the assembly containing brain core types to register.
Declaration
protected static Assembly BrainCoreAssembly { get; }
Property Value
| Type | Description |
|---|---|
| Assembly |
BrainDefaultsFilePath
Gets the absolute path to the default brain parameters file.
Declaration
public virtual string BrainDefaultsFilePath { get; }
Property Value
| Type | Description |
|---|---|
| string |
BrainTemplatesFolderPath
Gets the absolute path to the folder containing brain templates.
Declaration
public virtual string BrainTemplatesFolderPath { get; }
Property Value
| Type | Description |
|---|---|
| string |
BrainsFolder
Gets or sets the path to the resources folder containing bot brain files.
Declaration
public string BrainsFolder { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
JsonConverters
Gets the JSON converters for serializing and deserializing bot brains.
Declaration
protected abstract IEnumerable<JsonConverter> JsonConverters { get; }
Property Value
| Type | Description |
|---|---|
| IEnumerable<JsonConverter> |
Logger
Gets the logger to use.
Declaration
public ILogger<BrainSerializerBase<TState, TBot, TAction>> Logger { get; }
Property Value
| Type | Description |
|---|---|
| ILogger<BrainSerializerBase<TState, TBot, TAction>> |
ParameterNames
Gets an enumeration of parameter names.
Declaration
public IEnumerable<string> ParameterNames { get; }
Property Value
| Type | Description |
|---|---|
| IEnumerable<string> |
Services
Gets the services used for construction.
Declaration
public IServiceProvider? Services { get; }
Property Value
| Type | Description |
|---|---|
| IServiceProvider |
TitleOutputFolder
Gets the output folder for title resources.
Declaration
public virtual string TitleOutputFolder { get; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
View SourceDeserializeBrain(string)
Deserializes a brain from a string.
Declaration
public abstract IBotBrain DeserializeBrain(string contents)
Parameters
| Type | Name | Description |
|---|---|---|
| string | contents | A string representation of a brain. |
Returns
| Type | Description |
|---|---|
| IBotBrain | The brain. |
DeserializeBrainFile(string)
Deserializes a brain from a file.
Declaration
public virtual IBotBrain DeserializeBrainFile(string file)
Parameters
| Type | Name | Description |
|---|---|---|
| string | file | The file containing a brain. |
Returns
| Type | Description |
|---|---|
| IBotBrain | The brain. |
GetBrainFilePath(string)
Gets the file path for the given bot brain.
Declaration
protected virtual string GetBrainFilePath(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The brain's name. |
Returns
| Type | Description |
|---|---|
| string | A file path. |
GetDefaultSerializerSettings(bool)
Gets the default serializer settings for the given options.
Declaration
protected virtual JsonSerializerSettings GetDefaultSerializerSettings(bool indented = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | indented | If true, the JSON output is indented. |
Returns
| Type | Description |
|---|---|
| JsonSerializerSettings | A settings object. |
GetDeserializerSettings(bool)
Gets the deserializer settings for the given options.
Declaration
protected virtual JsonSerializerSettings GetDeserializerSettings(bool indented = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | indented | If true, the JSON output is indented. |
Returns
| Type | Description |
|---|---|
| JsonSerializerSettings | A settings object. |
GetJsonConverters<TBrain>()
Gets the JSON converters for serializing bot brains.
Declaration
protected IEnumerable<JsonConverter> GetJsonConverters<TBrain>() where TBrain : IBotBrain
Returns
| Type | Description |
|---|---|
| IEnumerable<JsonConverter> | An enumeration of converters. |
Type Parameters
| Name | Description |
|---|---|
| TBrain | The brain type. |
GetParameterMetadata(string)
Gets the metadata for a parameter.
Declaration
public ParameterMetadata? GetParameterMetadata(string parameterName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | parameterName | The parameter name. |
Returns
| Type | Description |
|---|---|
| ParameterMetadata | The metadata, if it exists. |
GetSerializerSettings(bool)
Gets the serializer settings for the given options.
Declaration
protected virtual JsonSerializerSettings GetSerializerSettings(bool indented = true)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | indented | If true, the JSON output is indented. |
Returns
| Type | Description |
|---|---|
| JsonSerializerSettings | A settings object. |
GetTitleResourcePath(string?)
Gets the output file path for the given title resource.
Declaration
public string GetTitleResourcePath(string? filepath = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | filepath | The resource file path. |
Returns
| Type | Description |
|---|---|
| string | A file path. |
Initialize()
Initializes the brain helper.
Declaration
public virtual void Initialize()
IsReservedBrainFilename(string)
Determines if the given filename is reserved for brain metadata like defaults or parameters.
Declaration
public virtual bool IsReservedBrainFilename(string filename)
Parameters
| Type | Name | Description |
|---|---|---|
| string | filename | The filename. |
Returns
| Type | Description |
|---|---|
| bool | True if it is reserved. |
LoadBrain(string)
Loads a brain file.
Declaration
public virtual IBotBrain? LoadBrain(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The name of the brain. |
Returns
| Type | Description |
|---|---|
| IBotBrain | The brain, or null if it couldn't be loaded. |
LoadDefaultBrainParameters()
Loads the default brain parameters.
Declaration
public virtual IParameterManager? LoadDefaultBrainParameters()
Returns
| Type | Description |
|---|---|
| IParameterManager | The default parameters, or null if they don't exist. |
LoadFileItem<T>(string, Func<string, T>, string)
Loads an object from a file.
Declaration
protected T LoadFileItem<T>(string filePath, Func<string, T> deserializeFunc, string itemType)
Parameters
| Type | Name | Description |
|---|---|---|
| string | filePath | The file to load. |
| Func<string, T> | deserializeFunc | The deserialization function. |
| string | itemType | The item type name. |
Returns
| Type | Description |
|---|---|
| T | The object, or a default value if it failed to load. |
Type Parameters
| Name | Description |
|---|---|
| T | The object type. |
LoadParameterMetadata()
Loads parameter metadata embedded within assemblies.
Declaration
protected virtual MetadataContainer LoadParameterMetadata()
Returns
| Type | Description |
|---|---|
| MetadataContainer | A metadata container. |
LoadTemplates<T>(string, JsonSerializerSettings, Func<string, bool>?)
Loads templates from a folder.
Declaration
protected void LoadTemplates<T>(string folder, JsonSerializerSettings serializerSettings, Func<string, bool>? ignore = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | folder | The folder to search. |
| JsonSerializerSettings | serializerSettings | The JSON serializer settings. |
| Func<string, bool> | ignore | An optional function specifying which files to ignore. |
Type Parameters
| Name | Description |
|---|---|
| T | The object type. |
RegisterBrainAssemblyTypes(Assembly)
Registers brain-related types for deserialization.
Declaration
protected static void RegisterBrainAssemblyTypes(Assembly a)
Parameters
| Type | Name | Description |
|---|---|---|
| Assembly | a | The assembly to index. |
SerializeBrain(IBotBrain, bool)
Serializes a brain to a string.
Declaration
public virtual string SerializeBrain(IBotBrain brain, bool indented = true)
Parameters
| Type | Name | Description |
|---|---|---|
| IBotBrain | brain | The brain. |
| bool | indented | If true, the output is indented. |
Returns
| Type | Description |
|---|---|
| string | A string representation of a brain. |
WriteCoreResources()
Writes the embedded bot brain core resources to disk.
Declaration
protected void WriteCoreResources()
WriteTitleResources()
Writes the embedded bot brain title-specific resources to disk.
Declaration
protected virtual void WriteTitleResources()