aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd-launch.c
blob: 4c445d500ed3fa5c9649515ff1e76810cf8c1600 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "commands.h"
#include "command.h"
#include "l2su.h"
#include "launch.h"
#include "user.h"
#include "version.h"

#include <string.h>

unsigned cmd_launch2(struct l2_context_node *ctx, char **args)
{
  unsigned ures;
  int res;

  if (*args) {
    CMD_FATAL0("sorry launch settings aren't implemented yet");
  }

  char *instancename = NULL;
  char *versionname = NULL;
  char *username = NULL;

  l2_cmd_collect_args(ctx, 3, "instance", &instancename, "version", &versionname, "user", &username);

  if ((ures = l2_instance_load_all()) != INSTANCE_SUCCESS) {
    CMD_FATAL("Failed to load instance: %s", l2_instance_errormsg[ures]);
  }

  if ((ures = l2_version_load_remote()) != VERSION_SUCCESS) {
    CMD_FATAL("Failed to load versions: %s", l2_version_strerror(ures));
  }

  if ((res = l2_user_load()) < 0) {
    CMD_FATAL0("Failed to load users");
  }

  struct l2_instance *inst = NULL;

  for (struct l2_instance *cur = l2_state.instance_head; cur; cur = cur->next) {
    if (!strcmp(cur->name, instancename)) {
      inst = cur;
      break;
    }
  }

  if (!inst) {
    CMD_FATAL("Could not find an instance by the name '%s'.", instancename);
  }

  struct l2_user *user = l2_user_search(username);
  if (!user) {
    CMD_FATAL0("Failed to load user");
  }

  struct l2_launch launch = { 0 };
  if (l2_launch_init(&launch, versionname, inst) < 0) {
    CMD_FATAL0("Failed to initialize launch context");
  }

  launch.user = user;

  if (l2_launch_init_substitutor(&launch) < 0) {
    CMD_FATAL0("Failed to initialize argument substitutor");
  }

  if (l2_launch_init_args(&launch) < 0) {
    CMD_FATAL0("Failed to set JVM and game arguments");
  }

  if (l2_launch_jni(&launch) < 0) {
    CMD_FATAL0("Failed to launch the game");
  }

  l2_launch_free_contents(&launch);

  return CMD_RESULT_SUCCESS;
}