using System.Runtime.InteropServices; using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Api.Utility { public partial class INIHelper { /* * 声明API函数 */ [LibraryImport("kernel32.dll", EntryPoint = "WritePrivateProfileStringW", StringMarshalling = StringMarshalling.Utf16)] private static partial long WritePrivateProfileString(string section, string key, string val, string filePath); [LibraryImport("Kernel32.dll", EntryPoint = "GetPrivateProfileStringW", StringMarshalling = StringMarshalling.Utf16)] private static partial int GetPrivateProfileString(string section, string key, string def, char[] val, int size, string filePath); /// /// 默认的配置文件名称 /// public const string DefaultFileName = @"FunGame.ini"; /// /// 写入ini文件 /// /// Section /// 键 /// 值 /// 文件名,缺省为FunGame.ini public static void WriteINI(string Section, string Key, string Value, string FileName = DefaultFileName) { WritePrivateProfileString(Section, Key, Value, Environment.CurrentDirectory.ToString() + @"\" + FileName); } /// /// 读取ini文件 /// /// Section /// 键 /// 文件名,缺省为FunGame.ini /// 读取到的值 public static string ReadINI(string Section, string Key, string FileName = DefaultFileName) { char[] val = new char[General.StreamByteSize]; _ = GetPrivateProfileString(Section, Key, "", val, General.StreamByteSize, Environment.CurrentDirectory.ToString() + @"\" + FileName); string? read = new(val); return read != null ? read.Trim('\0') : ""; } /// /// 查询ini文件是否存在 /// /// 文件名,缺省为FunGame.ini /// 是否存在 public static bool ExistINIFile(string FileName = DefaultFileName) => File.Exists($@"{Environment.CurrentDirectory}\{FileName}"); /// /// 初始化ini模板文件 /// public static void Init(FunGameInfo.FunGame FunGameType) { StreamWriter writer = new(DefaultFileName, false, General.DefaultEncoding); switch (FunGameType) { case FunGameInfo.FunGame.FunGame_Core: case FunGameInfo.FunGame.FunGame_Core_Api: case FunGameInfo.FunGame.FunGame_Console: case FunGameInfo.FunGame.FunGame_Desktop: writer.Write("[Config]"); writer.Close(); /** * Config */ WriteINI("Config", "AutoConnect", "true"); WriteINI("Config", "AutoLogin", "false"); /** * Account */ WriteINI("Account", "UserName", ""); WriteINI("Account", "Password", ""); WriteINI("Account", "AutoKey", ""); break; case FunGameInfo.FunGame.FunGame_Server: writer.Write("[Server]"); writer.Close(); /** * Server */ WriteINI("Server", "Name", "FunGame Server"); WriteINI("Server", "Password", ""); WriteINI("Server", "Describe", "Just Another FunGame Server."); WriteINI("Server", "Notice", "This is the FunGame Server's Notice."); WriteINI("Server", "Key", ""); WriteINI("Server", "Status", "1"); WriteINI("Server", "BannedList", ""); /** * ServerMail */ WriteINI("ServerMail", "OfficialMail", ""); WriteINI("ServerMail", "SupportMail", ""); /** * Socket */ WriteINI("Socket", "Port", "22222"); WriteINI("Socket", "MaxPlayer", "20"); WriteINI("Socket", "MaxConnectFailed", "0"); /** * MySQL */ WriteINI("MySQL", "DBServer", "localhost"); WriteINI("MySQL", "DBPort", "3306"); WriteINI("MySQL", "DBName", "fungame"); WriteINI("MySQL", "DBUser", "root"); WriteINI("MySQL", "DBPassword", "pass"); /** * Mailer */ WriteINI("Mailer", "UseMailSender", "false"); WriteINI("Mailer", "MailAddress", ""); WriteINI("Mailer", "Name", ""); WriteINI("Mailer", "Password", ""); WriteINI("Mailer", "Host", ""); WriteINI("Mailer", "Port", "587"); WriteINI("Mailer", "OpenSSL", "true"); break; } } } public class TXTHelper { /// /// 读取TXT文件内容 /// /// 文件名 /// 相对路径 /// 内容 public static string ReadTXT(string filename, string path = "") { if (path.Trim() != "") path = Path.Combine(path, filename); else path = $@"{Environment.CurrentDirectory}\{filename}"; if (File.Exists(path)) { string s = ""; // 创建一个 StreamReader 的实例来读取文件 using StreamReader sr = new(path); string? line; // 从文件读取并显示行,直到文件的末尾 while ((line = sr.ReadLine()) != null) { s += line + " "; } return s; } return ""; } /// /// 写入TXT文件内容(如不存在文件会创建,反之新起一行追加) /// /// /// 文件名 /// 相对路径 /// 内容 public static void WriteTXT(string message, string filename, string path = "") { if (path.Trim() != "") { // 不存在文件夹将创建文件夹 if (!Directory.Exists(path)) Directory.CreateDirectory(path); path = Path.Combine(path, filename); } else path = $@"{Environment.CurrentDirectory}\{filename}"; // 写入内容 StreamWriter writer = File.Exists(path) ? new(path, true, General.DefaultEncoding) : new(path, false, General.DefaultEncoding); writer.WriteLine(message); writer.Close(); } /// /// 追加错误日志 默认写入logs文件夹下的当日日期.log文件 /// /// public static void AppendErrorLog(string msg) => WriteTXT(DateTimeUtility.GetDateTimeToString(TimeType.General) + ": " + msg + "\r\n", DateTimeUtility.GetDateTimeToString("yyyy-MM-dd") + ".log", "logs"); } }