minecraft吧 关注:2,568,784贴子:32,084,089

回复:我的世界启动器制作教程

只看楼主收藏回复

MainForm.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace JuicyLauncher
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
try
{
//读取版本
listBox1.Items.Clear();
String[] vers = Directory.GetDirectories(Application.StartupPath + "\\.minecraft\\versions");
foreach (String item0 in vers)
{
listBox1.Items.Add(item0.Replace(Application.StartupPath + "\\.minecraft\\versions\\",""));
}
}
catch (Exception)
{
throw;
}
}
private void button2_Click(object sender, EventArgs e)
{
Settings s = new Settings();
s.LoadForm();
s.Show();
}
private void button1_Click(object sender, EventArgs e)
{
//启动
String[] TCfg=new String[3];
TCfg=System.IO.File.ReadAllLines(Application.StartupPath + "\\JLConfig.JLConfig");//读取配置
Launcher launcher = new Launcher();
if (listBox1.SelectedIndex == -1)
{
listBox1.SelectedIndex = 0;
}
launcher.Launch(TCfg[0], TCfg[1], TCfg[2], listBox1.SelectedItem.ToString());
}
}
}


104楼2016-06-10 19:03
回复
    Settings.cs:
    using System;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Win32;
    namespace JuicyLauncher
    {
    public partial class Settings : Form
    {
    public Settings()
    {
    InitializeComponent();
    }
    public void LoadForm()
    {
    //读取配置
    if (!File.Exists(Application.StartupPath + "\\JLConfig.JLConfig"))
    {
    String[] js=Registry.LocalMachine.OpenSubKey(@"SOFTWARE\JavaSoft\Java Runtime Environment").GetSubKeyNames();//读取jre路径。
    StreamWriter sw=File.CreateText(Application.StartupPath + "\\JLConfig.JLConfig");
    sw.Write("1024\n" + Registry.LocalMachine.OpenSubKey(@"SOFTWARE\JavaSoft\Java Runtime Environment\" + js[0]).GetValue("JavaHome") + "\\bin\\javaw.exe\nPlayer\n");
    sw.Close();
    }
    string[] Ct = new string[] { };
    Ct = System.IO.File.ReadAllLines(Application.StartupPath + "\\JLConfig.JLConfig");
    mm.Text = Ct[0];
    jp.Text = Ct[1];
    un.Text = Ct[2];
    }
    private void button1_Click(object sender, EventArgs e)
    {
    //写配置
    String[] Ct = new String[] {mm.Text,jp.Text,un.Text};//拼接配置文件内容
    File.WriteAllLines(Application.StartupPath + "\\JLConfig.JLConfig", Ct);//写入
    this.Close();//关闭窗体
    }
    private void button2_Click(object sender, EventArgs e)
    {
    //浏览部分
    OpenFileDialog ofd = new OpenFileDialog();//初始化打开文件对话框
    ofd.Title = "请选择您的javaw.exe所在路径";//设置标题
    ofd.RestoreDirectory = true;//不还原路径
    ofd.Multiselect = false;//不允许选择多个
    ofd.Filter = "应用程序(*.exe)|*.exe";//设置格式过滤
    if (ofd.ShowDialog() == DialogResult.OK)
    {//显示对话框
    jp.Text = ofd.FileName;//返回路径名
    }
    }
    }
    }


    105楼2016-06-10 19:03
    回复
      2026-01-14 06:56:23
      广告
      不感兴趣
      开通SVIP免广告
      Launcher.cs:
      using System;
      using System.Text;
      using System.IO;
      using System.Windows.Forms;
      using System.Diagnostics;
      namespace JuicyLauncher
      {
      class Launcher
      {
      /// <summary>
      /// 启动主方法
      /// </summary>
      /// <param name="MaxMem">最大内存</param>
      /// <param name="JavaPath">Javaw.exe路径</param>
      /// <param name="UserName">用户名</param>
      /// <param name="VerName">版本名</param>
      public void Launch(string MaxMem,string JavaPath,string UserName,string VerName)
      {
      string rtxt = "";
      int tmp = 0;
      rtxt = File.ReadAllText(Application.StartupPath + "\\.minecraft\\versions\\" + VerName + "\\" + VerName + ".json").Replace(" ", "").Replace("\n","");//读取内容
      tmp = rtxt.IndexOf("mainClass") + "mainClass".Length + 3;
      String mainClass = rtxt.Substring(tmp, rtxt.IndexOf("\"", tmp) - tmp);
      tmp = rtxt.IndexOf("minecraftArguments") + "minecraftArguments".Length + 3;
      String minecraftArguments = rtxt.Substring(tmp, rtxt.IndexOf("\"", tmp) - tmp).Replace("${"," ${").Replace("}","} ");
      tmp = rtxt.IndexOf("libraries") + "libraries".Length + 3;
      String libraries = rtxt.Substring(tmp, rtxt.LastIndexOf("]") - tmp);
      String[] libs = libraries.Split("},{".ToCharArray());
      String libp="";
      for (int i = 1; i < libs.Length; i++)
      {
      if (libs[i].IndexOf("name") == -1)
      {
      continue;
      }
      tmp = libs[i].IndexOf("name") + "name".Length + 3;
      String libn = libs[i].Substring(tmp, libs[i].IndexOf("\"", tmp) - tmp);
      if (libn.IndexOf(":") == -1)
      {
      continue;
      }
      String[] tlib = new String[] { libn.Substring(0, libn.IndexOf(":")).Replace(":", ""), libn.Substring(libn.IndexOf(":") + 1, libn.IndexOf(":", libn.IndexOf(":") + 1) - libn.IndexOf(":")).Replace(":", ""), libn.Substring(libn.IndexOf(":", libn.IndexOf(":") +1)).Replace(":", "") };
      String tpath = Application.StartupPath + "\\.minecraft\\libraries\\" + tlib[0].Replace(".", "\\") + "\\" + tlib[1] + "\\" + tlib[2] + "\\" + tlib[1] + "-" + tlib[2] + ".jar";
      if (File.Exists(tpath))
      {
      libp = libp + tpath + ";";
      }
      }
      String natives = Application.StartupPath + "\\.minecraft\\versions\\" + VerName + "\\" + VerName + "-natives";
      libp = libp + Application.StartupPath + "\\.minecraft\\versions\\" + VerName + "\\" + VerName + ".jar";
      String assets=Application.StartupPath + "\\.minecraft\\assets";
      String gameDir=Application.StartupPath + "\\.minecraft";
      if (Application.StartupPath.IndexOf(" ") != -1)
      {
      natives = "\"" + natives + "\"";
      libp = "\"" + libp + "\"";
      assets = "\"" + assets + "\"";
      gameDir = "\"" + gameDir + "\"";
      }
      minecraftArguments = minecraftArguments.Replace("${auth_player_name}", UserName).Replace("${version_name}", "JuicyLauncher_1.0").Replace("${game_directory}", gameDir).Replace("${game_assets}", assets);//JuicyLauncher_1.0可以改成你的启动器名字
      //启动参数拼接
      String RunComm = "";
      RunComm = "-Xmx" + MaxMem + "m -Djava.library.path=" + natives + " -Dfml.ignoreInvalidMinecraftCertificates=true -Dfml.ignorePatchDiscrepancies=true -cp " + libp + " " + mainClass + " " + minecraftArguments;
      Process mjp=new Process();
      ProcessStartInfo psi=new ProcessStartInfo(JavaPath, RunComm);
      psi.UseShellExecute = false;
      mjp.StartInfo = psi;
      mjp.Start();
      Application.Exit();
      }
      }
      }


      106楼2016-06-10 19:04
      收起回复
        那么,恭喜您,完成以上部分,您的启动器就基本达到了ColorLauncher和HHS-1.1启动器的水平了!它现在可以启动游戏了!
        如果您觉得满意,那么您可以离开了。
        接下来将是进阶部分,很难,也很无聊,成就感不强,要有足够的耐心。


        107楼2016-06-10 19:07
        收起回复
          点击上方绿色的小三角或按f5即可运行程序,之后便可在<项目路径>\<项目名>\bin\Debug下找到生成的exe文件。


          来自Android客户端108楼2016-06-10 19:43
          回复
            进阶一:natives解压


            来自Android客户端109楼2016-06-10 20:04
            回复
              我觉得从这里开始@Jayshon_Yves应该看看。


              来自Android客户端110楼2016-06-10 20:06
              回复
                如果您完成了上述内容,您会发现有些minecraft客户端还是不能启动,这是为什么呢?


                来自Android客户端111楼2016-06-10 20:07
                回复
                  2026-01-14 06:50:23
                  广告
                  不感兴趣
                  开通SVIP免广告
                  原来,这些客户端没有用hmcl启动过,没有解压natives到指定的文件夹。


                  来自Android客户端112楼2016-06-10 20:08
                  回复
                    于是,我们就要用代码自己解压natives库。


                    来自Android客户端113楼2016-06-10 20:09
                    回复
                      让我们来看看这段数据: {
                      "extract": {
                      "exclude": [
                      "META-INF/"
                      ]
                      },
                      "name": "org.lwjgl.lwjgl:lwjgl-platform:2.9.0",
                      "natives": {
                      "linux": "natives-linux",
                      "windows": "natives-windows"
                      },
                      "rules": [
                      {
                      "action": "allow"
                      },
                      {
                      "action": "disallow",
                      "os": {
                      "name": "osx"
                      }
                      }
                      ]
                      },


                      来自Android客户端114楼2016-06-13 19:06
                      回复
                        它的文件名对应<游戏路径>\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl-platform\2.9.0\lwjgl-platform-2.9.0-natives-windows.jar


                        来自Android客户端115楼2016-06-13 19:28
                        回复
                          可怕


                          来自Android客户端117楼2016-06-13 20:26
                          收起回复
                            C#赞个


                            来自iPhone客户端118楼2016-06-13 21:54
                            收起回复
                              2026-01-14 06:44:23
                              广告
                              不感兴趣
                              开通SVIP免广告
                              您会发现文件路径中多出现了一个natives-windows


                              来自Android客户端119楼2016-06-14 20:38
                              回复