Skip to content

Commit

Permalink
Merge pull request #68 from ACaiCat/master
Browse files Browse the repository at this point in the history
添加 CaiLib库
  • Loading branch information
ACaiCat authored May 1, 2024
2 parents e406cb1 + c044ecb commit 81fcea1
Show file tree
Hide file tree
Showing 10 changed files with 701 additions and 0 deletions.
21 changes: 21 additions & 0 deletions CaiLib/CaiLib.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\template.targets" />

<ItemGroup>
<None Remove="SixLabors.ImageSharp.dll" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="SixLabors.ImageSharp.dll">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Reference Include="SixLabors.ImageSharp">
<HintPath>..\lib\SixLabors.ImageSharp.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
131 changes: 131 additions & 0 deletions CaiLib/Commad.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TShockAPI;
using Terraria;
using Terraria.Localization;

namespace CaiLib
{
public static class CaiCommand
{
/// <summary>
/// 删除指定命令
/// </summary>
/// <param name="names">命令名字</param>
public static void DelCommand(List<string> names)
{
Commands.ChatCommands.RemoveAll(c => c.Names.Any(n => names.Contains(n)));
}
/// <summary>
/// 删除指定命令
/// </summary>
/// <param name="names">命令名字</param>
public static void DelCommand(string names)
{
Commands.ChatCommands.RemoveAll(c => c.Names.Contains(names));
}
/// <summary>
/// 添加一条命令
/// </summary>
/// <param name="perms">权限</param>
/// <param name="commandDelegate">命令回调函数</param>
/// <param name="names">命令名字</param>
/// <param name="replaceCommand">替换原命令</param>
/// <param name="helpText">帮助提示</param>
/// <param name="helpDesc">帮助文档</param>
/// <param name="allowServer">允许非玩家</param>
/// <param name="doLog">日志记录参数</param>
public static void AddCommand(List<string> perms, CommandDelegate commandDelegate, List<string> names, bool replaceCommand = false, string helpText = null,string[] helpDesc = null, bool allowServer = true, bool doLog = true)
{
if (replaceCommand)
{
Commands.ChatCommands.RemoveAll(c => c.Names.Any(n => names.Contains(n)));
}
Commands.ChatCommands.Add(new Command(perms, commandDelegate, names.ToArray())
{
AllowServer = allowServer,
HelpDesc = helpDesc,
HelpText = helpText,
DoLog = doLog,
});
}
/// <summary>
/// 添加一条命令
/// </summary>
/// <param name="perms">权限</param>
/// <param name="commandDelegate">命令回调函数</param>
/// <param name="names">命令名字</param>
/// <param name="replaceCommand">替换原命令</param>
/// <param name="helpText">帮助提示</param>
/// <param name="helpDesc">帮助文档</param>
/// <param name="allowServer">允许非玩家</param>
/// <param name="doLog">日志记录参数</param>
public static void AddCommand(string perms, CommandDelegate commandDelegate, List<string> names, bool replaceCommand = false, string helpText = null, string[] helpDesc = null, bool allowServer = true, bool doLog = true)
{
if (replaceCommand)
{
Commands.ChatCommands.RemoveAll(c => c.Names.Any(n => names.Contains(n)));
}
Commands.ChatCommands.Add(new Command(perms, commandDelegate, names.ToArray())
{
AllowServer = allowServer,
HelpDesc = helpDesc,
HelpText = helpText,
DoLog = doLog,
});
}
/// <summary>
/// 添加一条命令
/// </summary>
/// <param name="perms">权限</param>
/// <param name="commandDelegate">命令回调函数</param>
/// <param name="names">命令名字</param>
/// <param name="replaceCommand">替换原命令</param>
/// <param name="helpText">帮助提示</param>
/// <param name="helpDesc">帮助文档</param>
/// <param name="allowServer">允许非玩家</param>
/// <param name="doLog">日志记录参数</param>
public static void AddCommand(List<string> perms, CommandDelegate commandDelegate, string names, bool replaceCommand = false, string helpText = null, string[] helpDesc = null, bool allowServer = true, bool doLog = true)
{
if (replaceCommand)
{
Commands.ChatCommands.RemoveAll(c => c.Names.Any(n => names.Contains(n)));
}
Commands.ChatCommands.Add(new Command(perms, commandDelegate, names)
{
AllowServer = allowServer,
HelpDesc = helpDesc,
HelpText = helpText,
DoLog = doLog,
});
}
/// <summary>
/// 添加一条命令
/// </summary>
/// <param name="perms">权限</param>
/// <param name="commandDelegate">命令回调函数</param>
/// <param name="names">命令名字</param>
/// <param name="replaceCommand">替换原命令</param>
/// <param name="helpText">帮助提示</param>
/// <param name="helpDesc">帮助文档</param>
/// <param name="allowServer">允许非玩家</param>
/// <param name="doLog">日志记录参数</param>
public static void AddCommand(string perms, CommandDelegate commandDelegate, string names, bool replaceCommand = false, string helpText = null, string[] helpDesc = null, bool allowServer = true, bool doLog = true)
{
if (replaceCommand)
{
Commands.ChatCommands.RemoveAll(c => c.Names.Any(n => names.Contains(n)));
}
Commands.ChatCommands.Add(new Command(perms, commandDelegate, names)
{
AllowServer = allowServer,
HelpDesc = helpDesc,
HelpText = helpText,
DoLog = doLog,
});
}
}
}
57 changes: 57 additions & 0 deletions CaiLib/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Newtonsoft.Json;
using TShockAPI;
using TShockAPI.Configuration;

namespace CaiLib
{
public class CaiConfig<TSettings> where TSettings : new()
{
/// <summary>
/// 设置Config对象
/// </summary>
/// <param name="name">配置文件的名字</param>
/// <param name="settings">配置文件的类模板</param>
public CaiConfig(string name, TSettings settings)
{
FilePath = Path.Combine(TShock.SavePath, name);
Settings = settings;
}


[JsonIgnore]
public string FilePath = Path.Combine(TShock.SavePath, "CaiLib.json");

public virtual TSettings Settings { get; set; } = new TSettings();

/// <summary>
/// 写入Config
/// </summary>
public void Write()
{
using (var fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.Write))
{
var str = JsonConvert.SerializeObject(this, Formatting.Indented);
using (var sw = new StreamWriter(fs))
{
sw.Write(str);
}
}
}
/// <summary>
/// 创造并读取Config
/// </summary>
public CaiConfig<TSettings> Read()
{
if (!File.Exists(FilePath))
return this;
using (var fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var sr = new StreamReader(fs))
{
var cf = JsonConvert.DeserializeObject<CaiConfig<TSettings>>(sr.ReadToEnd());
return cf;
}
}
}
}
}
61 changes: 61 additions & 0 deletions CaiLib/FileTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaiLib
{
public class CaiFileTools
{
/// <summary>
/// 文件转base64
/// </summary>
/// <returns>base64字符串</returns>
public static string FileToBase64String(string path)
{
FileStream fsForRead = new FileStream(path, FileMode.Open);//文件路径
string base64Str = "";
try
{
fsForRead.Seek(0, SeekOrigin.Begin);
byte[] bs = new byte[fsForRead.Length];
int log = Convert.ToInt32(fsForRead.Length);
fsForRead.Read(bs, 0, log);
base64Str = Convert.ToBase64String(bs);
return base64Str;
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.ReadLine();
return base64Str;
}
finally
{
fsForRead.Close();
}
}

/// <summary>
/// base64字符串转文件
/// </summary>
/// <param name="base64String">Base64字符串</param>
/// <param name="fileFullPath">文件路径</param>
/// <returns></returns>

public static bool Base64StringToFile(string base64String, string fileFullPath)
{
bool opResult = false;
string strbase64 = base64String.Trim().Substring(base64String.IndexOf(",") + 1); //将‘,’以前的多余字符串删除
MemoryStream stream = new MemoryStream(Convert.FromBase64String(strbase64));
FileStream fs = new FileStream(fileFullPath, FileMode.OpenOrCreate, FileAccess.Write);
byte[] b = stream.ToArray();
fs.Write(b, 0, b.Length);
fs.Close();

opResult = true;
return opResult;
}
}
}
50 changes: 50 additions & 0 deletions CaiLib/Map.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Terraria.Map;
using Terraria;
using System.Reflection;

namespace CaiLib
{
public static class CaiMap
{
private static Assembly LoadLib()
{
string resourceName = "CaiLib.SixLabors.ImageSharp.dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}

public static Image CreateMapImage()
{
LoadLib();
Image<Rgba32> image = new Image<Rgba32>(Main.maxTilesX, Main.maxTilesY);

MapHelper.Initialize();
if (Main.Map == null)
{
Main.Map = new WorldMap(0, 0);
}
for (var x = 0; x < Main.maxTilesX; x++)
{
for (var y = 0; y < Main.maxTilesY; y++)
{
var tile = MapHelper.CreateMapTile(x, y, byte.MaxValue);
var col = MapHelper.GetMapTileXnaColor(ref tile);
image[x, y] = new Rgba32(col.R, col.G, col.B, col.A);
}
}

return image;
}
}
}
Loading

0 comments on commit 81fcea1

Please sign in to comment.