Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KazuOfficial committed Mar 18, 2024
1 parent 1346a69 commit a5a5858
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 77 deletions.
127 changes: 80 additions & 47 deletions src/core/Instance.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include "Instance.h"
#include "FileSystem.h"
#include "JsonExtension.h"
#include "VersionList.h"
#include "Game.h"
#include "JavaInstallations.h"
#include "Network.h"
#include "Settings.h"
#include "Version.h"
#include "Logger.h"
#include "JsonExtension.h"
#include "Mod.h"
#include "Game.h"
#include "Version.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define INSTANCE_HEIGHT_DEFAULT_SIZE 480
#define INSTANCE_WIDTH_DEFAULT_SIZE 854

char* bc_instance_get_path(const char* instance_name) {
char path[PATH_MAX];
Expand Down Expand Up @@ -50,8 +49,8 @@ json_object* bc_instance_create_default_config(const char* name, const char* ver
json_object_object_add(config, "version", json_object_new_string(version));
json_object_object_add(config, "jvm_args", json_object_new_string("-Xmx1G\n-XX:HeapDumpPath=java.exe_minecraft.exe.heapdump"));
json_object_object_add(config, "program_args", json_object_new_string(""));
json_object_object_add(config, "height", json_object_new_int(480));
json_object_object_add(config, "width", json_object_new_int(854));
json_object_object_add(config, "height", json_object_new_int(INSTANCE_HEIGHT_DEFAULT_SIZE));
json_object_object_add(config, "width", json_object_new_int(INSTANCE_WIDTH_DEFAULT_SIZE));
json_object_object_add(config, "maximized", json_object_new_boolean(0));
json_object_object_add(config, "fullscreen", json_object_new_boolean(0));
json_object_object_add(config, "show_log", json_object_new_boolean(0));
Expand All @@ -66,7 +65,7 @@ json_object* bc_instance_create_default_config(const char* name, const char* ver

json_object* bc_instance_create_config(const bc_instance* instance) {
json_object* json = json_object_from_file(instance->path);
json_object* tmp;
json_object* tmp = NULL;

jext_replace_or_create_option_str(json, "name", instance->name);
jext_replace_or_create_option_str(json, "version", instance->version);
Expand All @@ -87,7 +86,8 @@ json_object* bc_instance_create_config(const bc_instance* instance) {

void bc_instance_group_create(const char* name) {
json_object* settings = json_object_from_file("settings.json");
json_object* tmp, * instance_tmp;
json_object* tmp = NULL;
json_object* instance_tmp = NULL;
json_object* group_object = json_object_new_object();

json_object_object_get_ex(settings, "instance", &instance_tmp);
Expand All @@ -101,26 +101,36 @@ void bc_instance_group_create(const char* name) {
json_object_put(settings);
}

int bc_instance_validate_name(const char* name) {
static bool bc_instance_name_char_meets_constraints(const char name_char) {
if (!isalnum(name_char) && !isspace(name_char) && name_char != '-' && name_char != '_' && name_char != '.') {
return false;
}

return true;
}

bool bc_instance_validate_name(const char* name) {
if (name[0] == '.') {
return 0;
return false;
}

for (int i = 0; i < strlen(name); i++) {
char ch = name[i];
char name_char = name[i];

if (!isalnum(ch) && !isspace(ch) && ch != '-' && ch != '_' && ch != '.') {
return 0;
if (!bc_instance_name_char_meets_constraints(name_char)) {
return false;
}
}

return 1;
return true;
}

void bc_instance_move(bc_instance_array* standard, bc_instance_group_array* grouped, const char* instanceSelected) {
json_object* json = json_object_from_file("settings.json");
json_object* instance = json_object_new_object();
json_object* tmp, * tmpArr, * tmpArrGrouped;
json_object* tmp = NULL;
json_object* tmpArr = NULL;
json_object* tmpArrGrouped = NULL;

json_object_object_del(json, "instance");

Expand Down Expand Up @@ -168,7 +178,9 @@ bc_instance_group_name_array* bc_instance_group_name_get_all() {
return group_array;
}

json_object* tmp, * instance_tmp, * group_tmp;
json_object* tmp = NULL;
json_object* instance_tmp = NULL;
json_object* group_tmp = NULL;
json_object_object_get_ex(settings, "instance", &instance_tmp);
json_object_object_get_ex(instance_tmp, "grouped", &tmp);

Expand All @@ -186,7 +198,10 @@ bc_instance_group_name_array* bc_instance_group_name_get_all() {

void bc_instance_update_settings(const char* instance_path, const char* group_name) {
json_object* settings = json_object_from_file("settings.json");
json_object* instance_tmp, * tmp, * val_tmp, * val_arr_tmp;
json_object* instance_tmp = NULL;
json_object* tmp = NULL;
json_object* val_tmp = NULL;
json_object* val_arr_tmp = NULL;

json_object_object_get_ex(settings, "instance", &instance_tmp);

Expand Down Expand Up @@ -216,7 +231,7 @@ bc_instance* bc_instance_select_get() {
bc_instance* instance_selected = NULL;

json_object* json = json_object_from_file("settings.json");
json_object* tmp;
json_object* tmp = NULL;

json_object_object_get_ex(json, "instance", &tmp);

Expand All @@ -233,7 +248,8 @@ bc_instance* bc_instance_select_get() {

void bc_instance_select(const char* path) {
json_object* json = json_object_from_file("settings.json");
json_object* tmp, *tmp_selected;
json_object* tmp = NULL;
json_object* tmp_selected = NULL;

json_object_object_get_ex(json, "instance", &tmp);
json_object_object_get_ex(tmp, "selected", &tmp_selected);
Expand All @@ -246,7 +262,9 @@ void bc_instance_select(const char* path) {

void bc_instance_remove_group(const char* name) {
json_object* settings = json_object_from_file("settings.json");
json_object* tmp, * instance_tmp, * arr_tmp;
json_object* tmp = NULL;
json_object* instance_tmp = NULL;
json_object* arr_tmp = NULL;

json_object_object_get_ex(settings, "instance", &instance_tmp);
json_object_object_get_ex(instance_tmp, "grouped", &tmp);
Expand All @@ -265,22 +283,23 @@ void bc_instance_remove_group(const char* name) {
}

void bc_instance_create(const char* name, const char* version, const char* group_name) {
char n[64];
snprintf(n, sizeof(n), "%s", name);
char instance_name[BC_INSTANCE_NAME_MAX_SIZE];
snprintf(instance_name, sizeof(instance_name), "%s", name);

char* path = bc_instance_get_path(n);
char* path = bc_instance_get_path(instance_name);
int counter = 1;

while (bc_file_exists(path)) {
for (int i = 0; i < counter; i++)
snprintf(n, sizeof(n), "%s-", n);
for (int i = 0; i < counter; i++) {
snprintf(instance_name, sizeof(instance_name), "%s-", instance_name);
}

free(path);
path = bc_instance_get_path(n);
path = bc_instance_get_path(instance_name);
counter++;
}

json_object* config = bc_instance_create_default_config(n, version);
json_object* config = bc_instance_create_default_config(instance_name, version);
bc_instance_update_settings(path, group_name);

make_path(path, 1);
Expand All @@ -299,7 +318,12 @@ void bc_instance_update(const bc_instance* instance) {

void bc_instance_remove(const char* instance_path) {
json_object* settings = json_object_from_file("settings.json");
json_object* instance_tmp, * tmp, * val_tmp, * val_arr_tmp, * val_grouped_arr_tmp, * tmp_selected;
json_object* instance_tmp = NULL;
json_object* tmp = NULL;
json_object* val_tmp = NULL;
json_object* val_arr_tmp = NULL;
json_object* val_grouped_arr_tmp = NULL;
json_object* tmp_selected = NULL;

json_object_object_get_ex(settings, "instance", &instance_tmp);
json_object_object_get_ex(instance_tmp, "standalone", &tmp);
Expand All @@ -319,7 +343,7 @@ void bc_instance_remove(const char* instance_path) {
val_tmp = json_object_array_get_idx(tmp, i);
json_object_object_get_ex(val_tmp, "instances", &val_arr_tmp);

for (int y = 0; y < json_object_array_length(val_arr_tmp); y++) {
for (int j = 0; j < json_object_array_length(val_arr_tmp); j++) {
val_grouped_arr_tmp = json_object_array_get_idx(val_arr_tmp, i);

if (strcmp(json_object_get_string(val_grouped_arr_tmp), instance_path) == 0) {
Expand Down Expand Up @@ -363,7 +387,10 @@ bc_instance_array* bc_instance_get_all() {
return instance_array;
}

json_object* tmp, * instance_tmp, * arr_tmp, * instance_file_tmp;
json_object* tmp = NULL;
json_object* instance_tmp = NULL;
json_object* arr_tmp = NULL;
json_object* instance_file_tmp = NULL;

json_object_object_get_ex(settings, "instance", &instance_tmp);
json_object_object_get_ex(instance_tmp, "standalone", &tmp);
Expand Down Expand Up @@ -395,7 +422,11 @@ bc_instance_group_array* bc_instance_group_get_all() {
return instance_array;
}

json_object* tmp, * instance_tmp, * arr_tmp, * group_instances_tmp, * instance_file_tmp;
json_object* tmp = NULL;
json_object* instance_tmp = NULL;
json_object* arr_tmp = NULL;
json_object* group_instances_tmp = NULL;
json_object* instance_file_tmp = NULL;

json_object_object_get_ex(settings, "instance", &instance_tmp);
json_object_object_get_ex(instance_tmp, "grouped", &tmp);
Expand All @@ -410,12 +441,12 @@ bc_instance_group_array* bc_instance_group_get_all() {
json_object_object_get_ex(arr_tmp, "instances", &group_instances_tmp);
instance_array->arr[i].len = json_object_array_length(group_instances_tmp);

for (int y = 0; y < instance_array->arr[i].len; y++) {
arr_tmp = json_object_array_get_idx(group_instances_tmp, y);
for (int j = 0; j < instance_array->arr[i].len; j++) {
arr_tmp = json_object_array_get_idx(group_instances_tmp, j);
const char* instance_path = json_object_get_string(arr_tmp);
instance_file_tmp = json_object_from_file(instance_path);

bc_instance_fill_object_from_json(&instance_array->arr[i].instances[y], instance_path, instance_file_tmp);
bc_instance_fill_object_from_json(&instance_array->arr[i].instances[j], instance_path, instance_file_tmp);
}
}

Expand All @@ -424,22 +455,24 @@ bc_instance_group_array* bc_instance_group_get_all() {
return instance_array;
}

bc_progress bc_instance_run_progress() { return bc_game_run_progress; }
bc_progress bc_instance_run_progress() {
return bc_game_run_progress;
}

void bc_instance_run(const char* server_ip, const char* server_port) {
bc_instance* in = bc_instance_select_get();
bc_mod_version_array* mods = bc_mod_list_installed(in->path);
bc_instance* selected_instance = bc_instance_select_get();
bc_mod_version_array* mods = bc_mod_list_installed(selected_instance->path);

// makes the path be not of the json, but of the instance directory
int pathSize = strlen(in->path) - strlen("bc_instance.json");
in->path[pathSize] = '\0';
unsigned long pathSize = strlen(selected_instance->path) - strlen("bc_instance.json");
selected_instance->path[pathSize] = '\0';

char* selectedJinst = bc_jinst_select_get();
snprintf(in->java_path, sizeof(in->java_path), "%s", selectedJinst);
snprintf(selected_instance->java_path, sizeof(selected_instance->java_path), "%s", selectedJinst);
free(selectedJinst);

char jsonLoc[PATH_MAX];
snprintf(jsonLoc, sizeof(jsonLoc), "versions/%s.json", in->version);
snprintf(jsonLoc, sizeof(jsonLoc), "versions/%s.json", selected_instance->version);

json_object* jsonObj = json_object_from_file(jsonLoc);
bc_version* ver = bc_version_read_json(jsonObj);
Expand All @@ -458,15 +491,15 @@ void bc_instance_run(const char* server_ip, const char* server_port) {
}

bc_game_data* data = malloc(sizeof(bc_game_data));
data->instance = in;
data->instance = selected_instance;
data->version = ver;
data->account = acc;
data->mods = mods;
strcpy(data->server_ip, server_ip);
strcpy(data->server_port, server_port);

char* random_uuid = bc_file_uuid();
snprintf(data->natives_folder, sizeof(data->natives_folder), "%snatives-%s/", in->path, random_uuid);
snprintf(data->natives_folder, sizeof(data->natives_folder), "%snatives-%s/", selected_instance->path, random_uuid);
free(random_uuid);

bc_game_run(data);
Expand Down
53 changes: 35 additions & 18 deletions src/core/Instance.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
#ifndef BC_INSTANCE_H
#define BC_INSTANCE_H

#include <limits.h>
#include "Betacraft.h"
#include <limits.h>
#include <stdbool.h>

#define BC_INSTANCE_NAME_MAX_SIZE 64
#define BC_INSTANCE_JVM_ARGS_SIZE 1024
#define BC_INSTANCE_PROGRAM_ARGS_SIZE 1024
#define BC_INSTANCE_SERVER_IP_SIZE 128
#define BC_INSTANCE_SERVER_PORT_SIZE 16
#define BC_INSTANCE_VERSION_SIZE 32

#define BC_INSTANCE_ARRAY_MAX_SIZE 128

#define BC_INSTANCE_GROUP_ARRAY_MAX_SIZE 128
#define BC_INSTANCE_GROUP_NAME_MAX_SIZE 64
#define BC_INSTANCE_GROUP_INSTANCES_MAX_SIZE 128

#define BC_INSTANCE_GROUP_NAME_ARRAY_MAX_SIZE 128
#define BC_INSTANCE_GROUP_NAME_ARRAY_NAME_MAX_SIZE 32

typedef struct bc_instance_group_name_array {
char arr[128][32];
int len;
char arr[BC_INSTANCE_GROUP_NAME_ARRAY_MAX_SIZE][BC_INSTANCE_GROUP_NAME_ARRAY_NAME_MAX_SIZE];
size_t len;
} bc_instance_group_name_array;

typedef struct bc_instance {
char name[64];
char jvm_args[1024];
char program_args[1024];
char server_ip[128];
char server_port[16];
char name[BC_INSTANCE_NAME_MAX_SIZE];
char jvm_args[BC_INSTANCE_JVM_ARGS_SIZE];
char program_args[BC_INSTANCE_PROGRAM_ARGS_SIZE];
char server_ip[BC_INSTANCE_SERVER_IP_SIZE];
char server_port[BC_INSTANCE_SERVER_PORT_SIZE];
int join_server;
char java_path[PATH_MAX];
char path[PATH_MAX];
char version[32];
char version[BC_INSTANCE_VERSION_SIZE];
int width;
int height;
int maximized;
Expand All @@ -28,19 +45,19 @@ typedef struct bc_instance {
} bc_instance;

typedef struct bc_instance_group {
char group_name[64];
bc_instance instances[128];
int len;
char group_name[BC_INSTANCE_GROUP_NAME_MAX_SIZE];
bc_instance instances[BC_INSTANCE_GROUP_INSTANCES_MAX_SIZE];
size_t len;
} bc_instance_group;

typedef struct bc_instance_group_array {
bc_instance_group arr[128];
int len;
bc_instance_group arr[BC_INSTANCE_GROUP_ARRAY_MAX_SIZE];
size_t len;
} bc_instance_group_array;

typedef struct bc_instance_array {
bc_instance arr[128];
int len;
bc_instance arr[BC_INSTANCE_ARRAY_MAX_SIZE];
size_t len;
} bc_instance_array;

char* bc_instance_get_path(const char* instance_name);
Expand All @@ -50,7 +67,7 @@ void bc_instance_update(const bc_instance* instance);
void bc_instance_select(const char* path);
void bc_instance_move(bc_instance_array* standard, bc_instance_group_array* grouped, const char* instanceSelected);

void bc_instance_remove(const char* instance_name);
void bc_instance_remove(const char* instance_path);
void bc_instance_remove_group(const char* name);

bc_instance* bc_instance_get(const char* instance_path);
Expand All @@ -61,6 +78,6 @@ bc_instance_group_name_array* bc_instance_group_name_get_all();

void bc_instance_run(const char* server_ip, const char* server_port);
bc_progress bc_instance_run_progress();
int bc_instance_validate_name(const char* name);
bool bc_instance_validate_name(const char* name);

#endif
Loading

0 comments on commit a5a5858

Please sign in to comment.