Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmylo committed Jun 9, 2023
1 parent 51859a2 commit 04c8b2b
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 31 deletions.
38 changes: 30 additions & 8 deletions TavernAICardLib/TavernAiCard.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -36,21 +37,21 @@ public static bool ImageFullySupported()
[JsonInclude] [JsonPropertyName("system_prompt")] public string? SystemPrompt { get; set; }
[JsonInclude] [JsonPropertyName("tags")] public List<string>? Tags { get; set; }

public TavernAiCard(string? imagePath, Image? image)
public TavernAiCard(string? imagePath, Image? image, bool dontLoadImage = false)
{
this.ImagePath = imagePath;
if (image == null && imagePath != null && ImageFullySupported())
if (!dontLoadImage && image == null && imagePath != null && ImageFullySupported())
{
this.Image = new Bitmap(imagePath);
}
else this.Image = image;
}

public TavernAiCard() {}
public TavernAiCard(string? imagePath) : this(imagePath, null) {}
public TavernAiCard(string? imagePath, bool dontLoadImage = false) : this(imagePath, null, dontLoadImage) {}
public TavernAiCard(Image? image) : this(null, image) {}

public static string[] ImageFileTypes = {".png", ".webp", ".jpg", ".jpeg"};
public static string[] ImageFileTypes = {".png", ".apng", ".webp", ".jpg", ".jpeg", ".gif", ".bmp"};
public static string[] JsonFileTypes = {".json"};

private static Dictionary<string[], CardLoader> _cardLoaders = new()
Expand Down Expand Up @@ -170,16 +171,37 @@ public class ImageCardSaver : CardSaver
{
public override void Save(string filePath, TavernAiCard card)
{
if (card.Image == null) throw new NoImageException(filePath);
if (File.Exists(filePath)) File.Delete(filePath);
if (card.Image != null)
{
filePath = Path.GetFileNameWithoutExtension(filePath) + ".png";
card.Image.Save(filePath);
}
else if (card.ImagePath == null)
throw new NoImageSaveException(filePath);
else if (card.ImagePath != filePath)
File.Copy(card.ImagePath, filePath);

if (card.Image == null)
{
if (Path.GetExtension(filePath).ToLower() != "png")
{
Console.WriteLine("Converting to png");
Process? p = Process.Start(new ProcessStartInfo()
{
FileName = "ffmpeg",
Arguments = $"-y -i \"{card.ImagePath}\" \"{filePath}\""
});
p?.WaitForExit();
}
else
{
File.Copy(card.ImagePath, filePath);
}
}


// https://stackoverflow.com/a/32175522/
String tmpFile = "tmp.png";
if (File.Exists(tmpFile)) File.Delete(tmpFile);
PngReader reader = FileHelper.CreatePngReader(filePath);
PngWriter writer = FileHelper.CreatePngWriter(tmpFile, reader.ImgInfo, true);
int chunkBehav = ChunkCopyBehaviour.COPY_ALL_SAFE;
Expand Down
Binary file modified TavernAICardLib/bin/Debug/net6.0/TavernAICardLib.dll
Binary file not shown.
Binary file modified TavernAICardLib/bin/Debug/net6.0/TavernAICardLib.pdb
Binary file not shown.
Binary file modified TavernAICardLib/bin/Debug/net6.0/ref/TavernAICardLib.dll
Binary file not shown.
Binary file modified TavernAICardLib/obj/Debug/net6.0/TavernAICardLib.dll
Binary file not shown.
Binary file modified TavernAICardLib/obj/Debug/net6.0/TavernAICardLib.pdb
Binary file not shown.
Binary file modified TavernAICardLib/obj/Debug/net6.0/ref/TavernAICardLib.dll
Binary file not shown.
27 changes: 4 additions & 23 deletions TavernAICardLibTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
using System.Drawing;
using TavernAICardLib;
using TavernAICardLib;

// Load the character, this can be .json, .png, .webp, .jpeg or .jpg. If the file format is not supported an exception will be thrown.
TavernAiCard card = TavernAiCard.Load("character.json");
TavernAiCard card = new TavernAiCard("gaming_cat.jpg", true);

// Example: Load the character from a png card.
// TavernAiCard card = TavernAiCard.Load("character.png");
card.Name = "gaming cat";

// Example: Print the name of the loaded character.
Console.WriteLine(card.Name);

// Example: Set a new name
card.Name = "New name";

// Example: Saving, you can save in .json, .png, .webp, .jpeg or .jpg. The saving format used depends on the file extension.
card.Save("character_saved.json");

// Example: Creating a new image for the character, supported on Windows only.
// Check if full image support is available to create a custom bitmap (only on Windows, on other OS's only image paths are supported.)
if (TavernAiCard.ImageFullySupported())
{
// Create a transparent bitmap
card.Image = new Bitmap(400, 600);
// Save the card as an character card
card.Save("character_saved.png");
}
card.Save("gaming cat character card.png");
Binary file modified TavernAICardLibTest/bin/Debug/net6.0/TavernAICardLib.dll
Binary file not shown.
Binary file modified TavernAICardLibTest/bin/Debug/net6.0/TavernAICardLib.pdb
Binary file not shown.
Binary file modified TavernAICardLibTest/bin/Debug/net6.0/TavernAICardLibTest.dll
Binary file not shown.
Binary file modified TavernAICardLibTest/bin/Debug/net6.0/TavernAICardLibTest.pdb
Binary file not shown.
Binary file not shown.
Binary file modified TavernAICardLibTest/obj/Debug/net6.0/TavernAICardLibTest.dll
Binary file not shown.
Binary file modified TavernAICardLibTest/obj/Debug/net6.0/TavernAICardLibTest.pdb
Binary file not shown.

0 comments on commit 04c8b2b

Please sign in to comment.