-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from BUTR/dev
v1.0.10
- Loading branch information
Showing
10 changed files
with
199 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...b.Implementation/CampaignIdentifier/CampaignBehaviors/BinaryCampaignDescriptorProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Bannerlord.ButterLib.CampaignIdentifier; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using System.Runtime.Serialization.Formatters; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
|
||
using TaleWorlds.Engine; | ||
|
||
using Path = System.IO.Path; | ||
|
||
namespace Bannerlord.ButterLib.Implementation.CampaignIdentifier.CampaignBehaviors | ||
{ | ||
internal sealed class BinaryCampaignDescriptorProvider : ICampaignDescriptorProvider | ||
{ | ||
private static readonly string ExistingCampaignDescriptorsLogFile = | ||
Path.Combine(Utilities.GetConfigsPath(), "ButterLib", "CampaignIdentifier", "ExistingCampaignIdentifiers.bin"); | ||
|
||
public IEnumerable<CampaignDescriptor> Load() | ||
{ | ||
try | ||
{ | ||
var path = Path.GetDirectoryName(ExistingCampaignDescriptorsLogFile); | ||
if (string.IsNullOrEmpty(path) || !Directory.Exists(path) || !File.Exists(ExistingCampaignDescriptorsLogFile)) | ||
return Enumerable.Empty<CampaignDescriptor>(); | ||
|
||
using var fileStream = File.OpenRead(ExistingCampaignDescriptorsLogFile); | ||
var binaryFormatter = new BinaryFormatter | ||
{ | ||
AssemblyFormat = FormatterAssemblyStyle.Simple, | ||
Binder = new ButterLibSerializationBinder() | ||
}; | ||
return (List<CampaignDescriptorImplementation>) binaryFormatter.Deserialize(fileStream); | ||
} | ||
catch (Exception e) when (e is SerializationException) | ||
{ | ||
return Enumerable.Empty<CampaignDescriptor>(); | ||
} | ||
} | ||
|
||
public void Save(IEnumerable<CampaignDescriptor> descriptors) | ||
{ | ||
var path = Path.GetDirectoryName(ExistingCampaignDescriptorsLogFile); | ||
if (!string.IsNullOrEmpty(path) && !Directory.Exists(path)) | ||
Directory.CreateDirectory(path!); | ||
|
||
using var fileStream = File.OpenWrite(ExistingCampaignDescriptorsLogFile); | ||
var binaryFormatter = new BinaryFormatter | ||
{ | ||
AssemblyFormat = FormatterAssemblyStyle.Simple, | ||
Binder = new ButterLibSerializationBinder() | ||
}; | ||
binaryFormatter.Serialize(fileStream, descriptors.ToList()); | ||
} | ||
|
||
internal sealed class ButterLibSerializationBinder : SerializationBinder | ||
{ | ||
public override Type? BindToType(string assemblyName, string typeName) | ||
{ | ||
if (assemblyName.StartsWith("Bannerlord.ButterLib.Implementation")) | ||
return typeof(ButterLibSerializationBinder).Assembly.GetType(typeName); | ||
|
||
var type = Type.GetType($"{typeName}, {assemblyName}"); | ||
if (type != null) | ||
return type; | ||
|
||
var tokens = typeName.Split(new [] {"[[", "]]", "],["}, StringSplitOptions.RemoveEmptyEntries); | ||
if (tokens.Length == 1) | ||
return Type.GetType(typeName, true); | ||
|
||
var generic = tokens[0]; | ||
var genericTypeArgs = new List<string>(); | ||
foreach (var token in tokens.Skip(1)) | ||
{ | ||
var (typeName1, assemblyName1) = GetTokenInfo(token); | ||
var type1 = assemblyName1.StartsWith("Bannerlord.ButterLib.Implementation") | ||
? typeof(ButterLibSerializationBinder).Assembly.GetType(typeName1) | ||
: Type.GetType($"{typeName1}, {assemblyName1}", true); | ||
genericTypeArgs.Add(type1.AssemblyQualifiedName); | ||
} | ||
|
||
return Type.GetType($"{generic}[[{string.Join("],[", genericTypeArgs)}]]", true); | ||
} | ||
|
||
private static (string TypeName, string AssemblyName) GetTokenInfo(string str) | ||
{ | ||
var split = str.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); | ||
return (split[0].Trim(), string.Join(",", split.Skip(1)).Trim()); | ||
} | ||
|
||
public override void BindToName(Type serializedType, out string assemblyName, out string typeName) | ||
{ | ||
assemblyName = "Bannerlord.ButterLib.Implementation"; | ||
typeName = serializedType.FullName!; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...Lib.Implementation/CampaignIdentifier/CampaignBehaviors/JsonCampaignDescriptorProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Bannerlord.ButterLib.CampaignIdentifier; | ||
|
||
using Newtonsoft.Json; | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
using TaleWorlds.Engine; | ||
|
||
using Path = System.IO.Path; | ||
|
||
namespace Bannerlord.ButterLib.Implementation.CampaignIdentifier.CampaignBehaviors | ||
{ | ||
internal sealed class JsonCampaignDescriptorProvider : ICampaignDescriptorProvider | ||
{ | ||
private static readonly string ExistingCampaignDescriptorsLogFile = | ||
Path.Combine(Utilities.GetConfigsPath(), "ButterLib", "CampaignIdentifier", "Existing.json"); | ||
|
||
public IEnumerable<CampaignDescriptor> Load() | ||
{ | ||
var path = Path.GetDirectoryName(ExistingCampaignDescriptorsLogFile); | ||
if (string.IsNullOrEmpty(path) || !Directory.Exists(path) || !File.Exists(ExistingCampaignDescriptorsLogFile)) | ||
return Enumerable.Empty<CampaignDescriptor>(); | ||
|
||
var success = true; | ||
var settings = new JsonSerializerSettings | ||
{ | ||
Error = (sender, args) => { success = false; args.ErrorContext.Handled = true; }, | ||
MissingMemberHandling = MissingMemberHandling.Error | ||
}; | ||
using var fileStream = File.OpenRead(ExistingCampaignDescriptorsLogFile); | ||
var buffer = ReadFully(fileStream); | ||
var result = JsonConvert.DeserializeObject<IEnumerable<CampaignDescriptor>>(Encoding.UTF8.GetString(buffer), settings); | ||
return success ? result : Enumerable.Empty<CampaignDescriptor>(); | ||
} | ||
|
||
public void Save(IEnumerable<CampaignDescriptor> descriptors) | ||
{ | ||
var path = Path.GetDirectoryName(ExistingCampaignDescriptorsLogFile); | ||
if (!string.IsNullOrEmpty(path) && !Directory.Exists(path)) | ||
Directory.CreateDirectory(path!); | ||
|
||
using var fileStream = File.OpenWrite(ExistingCampaignDescriptorsLogFile); | ||
using var writer = new StreamWriter(fileStream); | ||
var buffer = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(descriptors, Formatting.Indented)); | ||
fileStream.Write(buffer, 0, buffer.Length); | ||
} | ||
|
||
private static byte[] ReadFully(Stream input) | ||
{ | ||
var buffer = new byte[16*1024]; | ||
using MemoryStream ms = new MemoryStream(); | ||
int read; | ||
while ((read = input.Read(buffer, 0, buffer.Length)) > 0) | ||
ms.Write(buffer, 0, read); | ||
return ms.ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
src/Bannerlord.ButterLib.Implementation/Common/ButterLibSerializationBinder.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Bannerlord.ButterLib/CampaignIdentifier/ICampaignDescriptorProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Bannerlord.ButterLib.CampaignIdentifier | ||
{ | ||
public interface ICampaignDescriptorProvider | ||
{ | ||
IEnumerable<CampaignDescriptor> Load(); | ||
void Save(IEnumerable<CampaignDescriptor> descriptors); | ||
} | ||
} |