using System; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; using System.Drawing; using System.Reflection; using System.Security.Cryptography; using System.Windows.Forms; using System.Xml.Linq; using System.Diagnostics; using System.Collections.Generic; [assembly:AssemblyTitle("dr.Dorr的小丑牌整合")] [assembly:AssemblyProduct("dr.Dorr的小丑牌整合")] [assembly:AssemblyVersion("2026.11.28.0")] [assembly:AssemblyFileVersion("2026.11.28.0")] public sealed class BundleEngine { public const string Version = "2026.11.28"; public static readonly string[] ModNames = {"Steamodded","ForesightHUD","ManualJokerSlots","TagManager","PreorderCards","UncapHermit","JezzLatro 1.0"}; public readonly string GameDir, ModsDir, StateDir; string StateFile { get { return Path.Combine(StateDir,"active.xml"); } } public BundleEngine(string game, string mods, string state) { GameDir=Path.GetFullPath(game); ModsDir=Path.GetFullPath(mods); StateDir=Path.GetFullPath(state); } public static string Hash(string file) { using(var sha=SHA256.Create()) using(var f=File.OpenRead(file)) return BitConverter.ToString(sha.ComputeHash(f)).Replace("-","").ToLowerInvariant(); } static void CopyTree(string from, string to) { Directory.CreateDirectory(to); foreach(var d in Directory.GetDirectories(from)) { if((File.GetAttributes(d)&FileAttributes.ReparsePoint)!=0) throw new IOException("不支持符号链接目录:"+d); CopyTree(d,Path.Combine(to,Path.GetFileName(d))); } foreach(var f in Directory.GetFiles(from)) File.Copy(f,Path.Combine(to,Path.GetFileName(f)),true); } static void RemovePath(string p) { if(Directory.Exists(p)) Directory.Delete(p,true); else if(File.Exists(p)) File.Delete(p); } static void CopyPath(string a,string b) { if(Directory.Exists(a)) CopyTree(a,b); else { Directory.CreateDirectory(Path.GetDirectoryName(b)); File.Copy(a,b,true); } } static bool Exists(string p) { return File.Exists(p)||Directory.Exists(p); } public static void ExtractPayload(string destination) { Directory.CreateDirectory(destination); using(var stream=Assembly.GetExecutingAssembly().GetManifestResourceStream("bundle.zip")) using(var zip=new ZipArchive(stream,ZipArchiveMode.Read)) { foreach(var entry in zip.Entries) { string path=Path.GetFullPath(Path.Combine(destination,entry.FullName)); if(!path.StartsWith(Path.GetFullPath(destination)+Path.DirectorySeparatorChar,StringComparison.OrdinalIgnoreCase)) throw new IOException("安装资源路径无效"); if(entry.FullName.EndsWith("/")) { Directory.CreateDirectory(path);continue; } Directory.CreateDirectory(Path.GetDirectoryName(path)); entry.ExtractToFile(path,true); } } using(var reader=new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("checksums.txt"))) { string line; int count=0; while((line=reader.ReadLine())!=null) { if(line.Length<67)continue; string expected=line.Substring(0,64);string relative=line.Substring(66);if(Hash(Path.Combine(destination,relative.Replace('/',Path.DirectorySeparatorChar)))!=expected)throw new IOException("资源校验失败:"+relative);count++; } if(count==0)throw new IOException("资源清单为空"); } } public void Install(string payload) { if(!File.Exists(Path.Combine(GameDir,"Balatro.exe"))) throw new IOException("请选择包含 Balatro.exe 的游戏文件夹。"); if(File.Exists(StateFile)) throw new IOException("已存在本整合包的安装或恢复记录。请先点击“卸载并恢复”,再安装。备份位于:"+StateDir); if(File.Exists(Path.Combine(GameDir,"dinput8.dll"))||File.Exists(Path.Combine(GameDir,"version.dll")))throw new IOException("检测到 dinput8.dll 或 version.dll。请先确认并移走旧加载器,避免重复加载 Lovely。"); Directory.CreateDirectory(ModsDir); Directory.CreateDirectory(StateDir); string backup=Path.Combine(StateDir,"backup-"+DateTime.Now.ToString("yyyyMMdd-HHmmss-fff"));Directory.CreateDirectory(backup); var state=new XDocument(new XElement("Bundle",new XAttribute("version",Version),new XAttribute("game",GameDir),new XAttribute("mods",ModsDir),new XAttribute("backup",backup),new XAttribute("status","installing"))); state.Save(StateFile); try { foreach(string mod in ModNames) { if(mod=="TagManager"&&!Directory.Exists(Path.Combine(payload,"Mods",mod)))continue; Replace(Path.Combine(payload,"Mods",mod),Path.Combine(ModsDir,mod),"mod",mod,backup,state); } Replace(Path.Combine(payload,"Loader","winmm.dll"),Path.Combine(GameDir,"winmm.dll"),"loader","winmm.dll",backup,state); CopyTree(Path.Combine(payload,"Docs"),Path.Combine(backup,"bundle-docs")); state.Root.SetAttributeValue("status","installed");state.Save(StateFile); } catch { try { Restore(false); } catch(Exception rollback) { throw new IOException("安装中断,自动恢复未完成。请保留备份并再次点击卸载恢复。"+rollback.Message); } throw; } } void Replace(string source,string target,string kind,string name,string backup,XDocument state) { if(!Exists(source))throw new IOException("缺少安装文件:"+name); string saved=Path.Combine(backup,"original",kind,name); bool existed=Exists(target); if(existed)CopyPath(target,saved); var entry=new XElement("Entry",new XAttribute("kind",kind),new XAttribute("name",name),new XAttribute("existed",existed),new XAttribute("saved",saved)); state.Root.Add(entry); state.Save(StateFile); RemovePath(target);CopyPath(source,target); } public void Restore(bool preserveCurrent) { if(!File.Exists(StateFile))throw new IOException("尚未找到本整合包的安装记录。"); var state=XDocument.Load(StateFile); if(!String.Equals((string)state.Root.Attribute("game"),GameDir,StringComparison.OrdinalIgnoreCase)||!String.Equals((string)state.Root.Attribute("mods"),ModsDir,StringComparison.OrdinalIgnoreCase))throw new IOException("游戏目录或 Mods 目录与原安装记录不一致。"); string backup=(string)state.Root.Attribute("backup"); if(!Path.GetFullPath(backup).StartsWith(StateDir+Path.DirectorySeparatorChar,StringComparison.OrdinalIgnoreCase))throw new IOException("备份路径无效"); string removed=Path.Combine(backup,"removed-"+DateTime.Now.ToString("yyyyMMdd-HHmmss-fff")); foreach(var entry in state.Root.Elements("Entry").Reverse().ToArray()) { string kind=(string)entry.Attribute("kind"),name=(string)entry.Attribute("name"); if(!((kind=="mod"&&ModNames.Contains(name))||(kind=="loader"&&name=="winmm.dll")))throw new IOException("恢复记录中的资源无效"); string target=kind=="mod"?Path.Combine(ModsDir,name):Path.Combine(GameDir,name); string saved=Path.Combine(backup,"original",kind,name); bool existed=(bool)entry.Attribute("existed"); if(existed&&!Exists(saved))throw new IOException("原文件备份缺失:"+name); if(preserveCurrent&&Exists(target))CopyPath(target,Path.Combine(removed,kind,name)); RemovePath(target); if(existed)CopyPath(saved,target); entry.Remove();state.Save(StateFile); } File.Copy(StateFile,Path.Combine(backup,"restored.xml"),true);File.Delete(StateFile); } public static string DetectGame() { var roots=new List(); foreach(var key in new[]{@"HKEY_CURRENT_USER\Software\Valve\Steam",@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Valve\Steam"}) { try {var value=Microsoft.Win32.Registry.GetValue(key,"SteamPath",null)??Microsoft.Win32.Registry.GetValue(key,"InstallPath",null);if(value!=null)roots.Add(value.ToString());} catch(System.Security.SecurityException){} catch(UnauthorizedAccessException){} } foreach(var drive in DriveInfo.GetDrives().Where(d=>d.IsReady)) { roots.Add(Path.Combine(drive.RootDirectory.FullName,"Program Files (x86)","Steam"));roots.Add(Path.Combine(drive.RootDirectory.FullName,"SteamLibrary")); } foreach(var root in roots.ToArray()) { string vdf=Path.Combine(root,"steamapps","libraryfolders.vdf");if(!File.Exists(vdf))continue; foreach(System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(vdf),"\"path\"\\s*\"([^\"]+)\""))roots.Add(m.Groups[1].Value.Replace("\\\\","\\")); } foreach(var root in roots.Distinct()) {string game=Path.Combine(root,"steamapps","common","Balatro");if(File.Exists(Path.Combine(game,"Balatro.exe")))return game;} return ""; } } public sealed class SetupForm:Form { TextBox gamePath; Label status; Button install,restore; bool busy; string tagImport=null; string Mods { get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"Balatro","Mods"); } } string State { get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"Balatro","drDorr-bundle"); } } public SetupForm() { Text="dr.Dorr的小丑牌整合";ClientSize=new Size(720,330);Font=new Font("Microsoft YaHei UI",10);AutoScaleMode=AutoScaleMode.Dpi;FormBorderStyle=FormBorderStyle.FixedDialog;MaximizeBox=false;StartPosition=FormStartPosition.CenterScreen;BackColor=Color.FromArgb(18,31,34);ForeColor=Color.White; Icon=Icon.ExtractAssociatedIcon(Application.ExecutablePath); var title=new Label{Text="dr.Dorr的小丑牌整合",Font=new Font(Font.FontFamily,22,FontStyle.Bold),Location=new Point(24,21),AutoSize=true};Controls.Add(title); AddLabel(BundleEngine.Version,24,69,400,23,Color.FromArgb(117,224,190)); #if PUBLIC var tagButton=new Button{Text="导入标签管理(可选)",Location=new Point(460,104),Size=new Size(236,32)}; tagButton.Click+=(s,e)=>{using(var d=new FolderBrowserDialog()){d.Description="选择解压后的 TagManager 文件夹";if(d.ShowDialog()==DialogResult.OK){if(!File.Exists(Path.Combine(d.SelectedPath,"mod.lua"))||!File.Exists(Path.Combine(d.SelectedPath,"manifest.json"))||!File.ReadAllText(Path.Combine(d.SelectedPath,"manifest.json")).Contains("TagManager")){MessageBox.Show("请选择有效的 TagManager 文件夹。");return;}tagImport=d.SelectedPath;status.Text="已选择标签管理。";}}}; Controls.Add(tagButton); #endif AddLabel("游戏目录",24,110,400,24,Color.White); gamePath=new TextBox{Text=BundleEngine.DetectGame(),Location=new Point(24,147),Size=new Size(550,31)};Controls.Add(gamePath); var browse=new Button{Text="选择…",Location=new Point(588,144),Size=new Size(108,35)};browse.Click+=(s,e)=>{using(var d=new FolderBrowserDialog()){d.Description="选择包含 Balatro.exe 的文件夹";if(Directory.Exists(gamePath.Text))d.SelectedPath=gamePath.Text;if(d.ShowDialog()==DialogResult.OK)gamePath.Text=d.SelectedPath;}};Controls.Add(browse); install=new Button{Text="安装",Location=new Point(24,245),Size=new Size(260,48),BackColor=Color.FromArgb(68,204,156),ForeColor=Color.FromArgb(15,30,30),FlatStyle=FlatStyle.Flat};install.Click+=(s,e)=>Run(false);Controls.Add(install); restore=new Button{Text="卸载并恢复",Location=new Point(300,245),Size=new Size(190,48)};restore.Click+=(s,e)=>Run(true);Controls.Add(restore); #if PUBLIC const string tagHelp="\n标签管理需从作者处下载解压后导入。"; #else const string tagHelp=""; #endif var docs=new Button{Text="使用说明",Location=new Point(506,245),Size=new Size(190,48)};docs.Click+=(s,e)=>MessageBox.Show("保存并退出游戏,选好目录后安装。\n\nCtrl+F6:种子工具\nCtrl+F7:槽位面板\nCtrl+F8:槽位 +1\n收藏中 F4–F8:预约卡牌\nF3:清空预约;F9:解锁悬停卡牌\n\n卸载前如需恢复槽位,请先在游戏里重置额外槽位。"+tagHelp+"\n\n备份位置:"+State,"使用说明");Controls.Add(docs); status=AddLabel(String.IsNullOrEmpty(gamePath.Text)?"请选择游戏目录。":"已找到游戏目录。",24,197,672,36,Color.FromArgb(117,224,190)); FormClosing+=(s,e)=>{if(busy)e.Cancel=true;}; } Label AddLabel(string text,int x,int y,int w,int h,Color color) {var l=new Label{Text=text,Location=new Point(x,y),Size=new Size(w,h),ForeColor=color};Controls.Add(l);return l;} void Run(bool undo) { if(busy)return; if(Process.GetProcessesByName("Balatro").Length>0){MessageBox.Show("请先保存并退出小丑牌,再执行安装或恢复。","游戏仍在运行");return;} if(String.IsNullOrWhiteSpace(gamePath.Text)){MessageBox.Show("请先选择游戏目录。");return;} busy=true;install.Enabled=restore.Enabled=false;status.Text=undo?"正在恢复…":"正在安装…";Application.DoEvents(); string staging=Path.Combine(Path.GetTempPath(),"drDorr-Balatro-"+Guid.NewGuid().ToString("N")); try { var engine=new BundleEngine(gamePath.Text,Mods,State); if(undo)engine.Restore(true); else { BundleEngine.ExtractPayload(staging); if(tagImport!=null) { string dest=Path.Combine(staging,"Mods","TagManager");Directory.CreateDirectory(dest); foreach(string file in Directory.GetFiles(tagImport,"*",SearchOption.AllDirectories)) { if((File.GetAttributes(file)&FileAttributes.ReparsePoint)!=0)throw new IOException("导入资源不能包含符号链接"); string relative=file.Substring(tagImport.Length).TrimStart(Path.DirectorySeparatorChar); string target=Path.Combine(dest,relative);Directory.CreateDirectory(Path.GetDirectoryName(target));File.Copy(file,target,true); } } engine.Install(staging); } status.Text=undo?"已卸载并恢复。":"安装完成。"; MessageBox.Show(status.Text,"dr.Dorr的小丑牌整合"); }catch(Exception ex){status.Text="未完成:"+ex.Message;MessageBox.Show(ex.Message,"操作未完成",MessageBoxButtons.OK,MessageBoxIcon.Warning);} finally{if(Directory.Exists(staging))try{Directory.Delete(staging,true);}catch{}busy=false;install.Enabled=restore.Enabled=true;} } [STAThread] public static int Main(string[] args) { if(args.Length==2&&args[0]=="--extract"){BundleEngine.ExtractPayload(args[1]);return 0;} if(args.Length==2&&args[0]=="--preview") {Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);using(var form=new SetupForm()){form.ShowInTaskbar=false;form.StartPosition=FormStartPosition.Manual;form.Location=new Point(-20000,-20000);form.Show();Application.DoEvents();using(var image=new Bitmap(form.Width,form.Height)){form.DrawToBitmap(image,new Rectangle(0,0,image.Width,image.Height));image.Save(args[1],System.Drawing.Imaging.ImageFormat.Png);}form.Close();}return 0;} Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new SetupForm());return 0; } }