Skip to content

Commit

Permalink
Remove events, use repo instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickid2018 committed Jul 20, 2024
1 parent a722723 commit 0d10bd8
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 313 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "cJSON"]
path = cJSON
url = https://github.com/DaveGamble/cJSON.git
[submodule "minecraft-data"]
path = minecraft-data
url = https://github.com/Nickid2018/minecraft-data.git
21 changes: 0 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ file(GLOB LIBRARY_CJSON_SOURCES "${PROJECT_SOURCE_DIR}/cJSON/cJSON*.c")
file(GLOB LIBRARY_CJSON_HEADERS "${PROJECT_SOURCE_DIR}/cJSON/cJSON*.h")
list(REMOVE_ITEM SOURCES "${PROJECT_SOURCE_DIR}/check_version.c")

set(GEN_RESOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/resources")
file(MAKE_DIRECTORY ${GEN_RESOURCE_DIR})
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/preprocess_resources")

set(WIRESHARK_DIR $ENV{WIRESHARK_DIR})
set(WIRESHARK_BUILD_DIR $ENV{WIRESHARK_BUILD_DIR})
set(WIRESHARK_LIB_DIR $ENV{WIRESHARK_LIB_DIR})
Expand Down Expand Up @@ -84,23 +80,6 @@ else ()
link_libraries(PkgConfig::gcrypt)
endif ()

macro(invoke_py message)
execute_process(COMMAND python3 ${ARGN} ERROR_VARIABLE GEN_ERROR OUTPUT_VARIABLE GEN_OUTPUT RESULT_VARIABLE GEN_RESULT)
if (NOT GEN_RESULT EQUAL 0)
message(STATUS "python3 not found or run failed, trying python")
execute_process(COMMAND python ${ARGN} ERROR_VARIABLE GEN_ERROR OUTPUT_VARIABLE GEN_OUTPUT RESULT_VARIABLE GEN_RESULT)
endif ()
if (NOT GEN_RESULT EQUAL 0)
message(FATAL_ERROR "Failed to ${message}:\n${GEN_OUTPUT}\n${GEN_ERROR}")
else ()
message(STATUS "${message}:\n${GEN_OUTPUT}")
endif ()
endmacro(invoke_py)

invoke_py("Generate Resources"
"${PROJECT_SOURCE_DIR}/codegen_script/resources_gen.py" "${PROJECT_SOURCE_DIR}/resources"
"${CMAKE_CURRENT_BINARY_DIR}/preprocess_resources" "${GEN_RESOURCE_DIR}")

file(GLOB GEN_RESOURCE_HEADERS "${GEN_RESOURCE_DIR}/*.h")
file(GLOB GEN_RESOURCE_SOURCES "${GEN_RESOURCE_DIR}/*.c")
include_directories(${GEN_RESOURCE_DIR})
Expand Down
56 changes: 0 additions & 56 deletions codegen_script/resources_gen.py

This file was deleted.

1 change: 0 additions & 1 deletion minecraft-data
Submodule minecraft-data deleted from b277ad
77 changes: 10 additions & 67 deletions protocol/schema/protocol_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,11 @@

#ifdef MC_DISSECTOR_FUNCTION_FEATURE

#include <stdlib.h>
#include "resources.h"
#include "protocol_functions.h"
#include "protocol/storage/storage.h"

extern int hf_generated_je;

typedef struct {
char *name;
long min_version;
long max_version;
} level_event_entry;

wmem_map_t *entity_event;
wmem_map_t *level_event;

void init_events() {
entity_event = wmem_map_new(wmem_epan_scope(), g_str_hash, g_str_equal);
level_event = wmem_map_new(wmem_epan_scope(), g_str_hash, g_str_equal);
char **split = g_strsplit(RESOURCE_ENTITY_EVENT, "\n", 256);
for (int i = 0; split[i] != NULL; i++) {
char *now = split[i];
wmem_map_insert(entity_event, g_strdup_printf("%d", i + 1), g_strdup(now));
}
g_strfreev(split);
split = g_strsplit(RESOURCE_LEVEL_EVENT, "\n", 1000);
for (int i = 0; split[i] != NULL; i++) {
char *now = split[i];
char **split_now = g_strsplit(now, " ", 2);
char **versions = g_strsplit(split_now[0], "|", 3);
char *event = versions[0];
long min_version = 0, max_version = 0x7fffffff;
if (versions[1] != NULL) {
min_version = strtol(versions[1], NULL, 10);
if (versions[2] != NULL)
max_version = strtol(versions[2], NULL, 10);
}
wmem_list_t *event_data = wmem_map_lookup(level_event, event);
if (event_data == NULL) {
event_data = wmem_list_new(wmem_epan_scope());
wmem_map_insert(level_event, g_strdup(event), event_data);
}
level_event_entry *entry = wmem_new(wmem_epan_scope(), level_event_entry);
entry->name = g_strdup(split_now[1]);
entry->min_version = min_version;
entry->max_version = max_version;
wmem_list_append(event_data, entry);
g_strfreev(split_now);
g_strfreev(versions);
}
g_strfreev(split);
}

void init_protocol_functions() {
init_events();
}

FIELD_MAKE_TREE(record_entity_id) {
wmem_map_t *entity_id_record = wmem_map_lookup(extra->data, "entity_id_record");
if (entity_id_record == NULL) {
Expand Down Expand Up @@ -158,7 +106,10 @@ FIELD_MAKE_TREE(entity_event) {
return 0;
char *event_id_path[] = {"entityStatus", NULL};
gchar *event_id = record_query(recorder, event_id_path);
gchar *event_name = wmem_map_lookup(entity_event, event_id);
gchar *event_name = get_entity_event_data(
GPOINTER_TO_UINT(wmem_map_lookup(extra->data, "protocol_version")),
event_id
);
if (event_name == NULL)
event_name = "Unknown";
proto_item *item = proto_tree_add_string(tree, hf_generated_je, tvb, 0, 0, event_name);
Expand All @@ -172,20 +123,12 @@ FIELD_MAKE_TREE(level_event) {
return 0;
char *event_id_path[] = {"effectId", NULL};
gchar *event_id = record_query(recorder, event_id_path);
wmem_list_t *event_data = wmem_map_lookup(level_event, event_id);
char *event_name = "Unknown";
if (event_data != NULL) {
guint data_version = GPOINTER_TO_UINT(wmem_map_lookup(extra->data, "data_version"));
wmem_list_frame_t *entry = wmem_list_head(event_data);
while (entry != NULL) {
level_event_entry *entry_data = wmem_list_frame_data(entry);
if (entry_data->min_version <= data_version && data_version <= entry_data->max_version) {
event_name = entry_data->name;
break;
}
entry = wmem_list_frame_next(entry);
}
}
gchar *event_name = get_level_event_data(
GPOINTER_TO_UINT(wmem_map_lookup(extra->data, "protocol_version")),
event_id
);
if (event_name == NULL)
event_name = "Unknown";
proto_item *item = proto_tree_add_string(tree, hf_generated_je, tvb, 0, 0, event_name);
proto_item_set_generated(item);
proto_item_prepend_text(item, "Level Event Type");
Expand Down
2 changes: 0 additions & 2 deletions protocol/schema/protocol_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ gint make_tree_##name(proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, extra

#ifdef MC_DISSECTOR_FUNCTION_FEATURE

void init_protocol_functions();

FIELD_MAKE_TREE(record_entity_id);

FIELD_MAKE_TREE(record_entity_id_player);
Expand Down
2 changes: 0 additions & 2 deletions protocol/schema/protocol_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,6 @@ void init_schema_data() {
ADD_FUNCTION(record_entity_id_painting, record_entity_id_painting)
ADD_FUNCTION(entity_event, entity_event)
ADD_FUNCTION(level_event, level_event)

init_protocol_functions();
#endif // MC_DISSECTOR_FUNCTION_FEATURE
}

Expand Down
82 changes: 82 additions & 0 deletions protocol/storage/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ JSON_CACHED(protocol_data_mapping, "java_edition/protocol_mapping.json")

JSON_CACHED(entity_sync_data_mapping, "java_edition/entity_sync_data_mapping.json")

JSON_CACHED(level_event_data_mapping, "java_edition/level_event_mapping.json")

JSON_CACHED(entity_event_data_mapping, "java_edition/entity_event_mapping.json")

JSON_CACHED(packet_names, "java_edition/packet_names.json")

DATA_CACHED_UINT(protocol)

DATA_CACHED_UINT(entity_sync_data)

DATA_CACHED_UINT(level_event)

DATA_CACHED_UINT(entity_event)

DATA_CACHED_STR(registry_data_mapping)

DATA_CACHED_STR(registry_data)
Expand All @@ -80,9 +88,13 @@ void clear_storage() {
CLEAR_CACHED_JSON(versions)
CLEAR_CACHED_JSON(protocol_data_mapping)
CLEAR_CACHED_JSON(entity_sync_data_mapping)
CLEAR_CACHED_JSON(level_event_data_mapping)
CLEAR_CACHED_JSON(entity_event_data_mapping)
CLEAR_CACHED_JSON(packet_names)
CLEAR_CACHED_DATA(protocol)
CLEAR_CACHED_DATA(entity_sync_data)
CLEAR_CACHED_DATA(level_event)
CLEAR_CACHED_DATA(entity_event)
CLEAR_CACHED_DATA(registry_data_mapping)
CLEAR_CACHED_DATA(registry_data)
}
Expand Down Expand Up @@ -270,4 +282,74 @@ gchar *get_registry_data(guint protocol_version, gchar *registry, guint index) {
if (data == NULL)
return "<Unknown Registry Entry>";
return data->valuestring;
}

gchar *get_level_event_data(guint protocol_version, gchar *index) {
cJSON *cached = get_cached_level_event(protocol_version);
if (cached == NULL) {
ensure_cached_level_event_data_mapping();
gchar *find_key = g_strdup_printf("%d", protocol_version);
cJSON *found = cJSON_GetObjectItem(cached_level_event_data_mapping, find_key);
g_free(find_key);
if (found == NULL)
return NULL;

gchar *file = g_build_filename(
pref_protocol_data_dir,
"java_edition/indexed_data",
found->valuestring,
"level_event.json",
NULL
);
gchar *content = NULL;
if (!g_file_get_contents(file, &content, NULL, NULL)) {
ws_log("MC-Dissector", LOG_LEVEL_WARNING, "Cannot read file %s", file);
g_free(file);
return NULL;
}

cached = cJSON_Parse(content);
g_free(content);
set_cached_level_event(protocol_version, cached);
}

cJSON *data = cJSON_GetObjectItem(cached, index);
if (data == NULL)
return NULL;
return data->valuestring;
}

gchar *get_entity_event_data(guint protocol_version, gchar *index) {
cJSON *cached = get_cached_entity_event(protocol_version);
if (cached == NULL) {
ensure_cached_entity_event_data_mapping();
gchar *find_key = g_strdup_printf("%d", protocol_version);
cJSON *found = cJSON_GetObjectItem(cached_entity_event_data_mapping, find_key);
g_free(find_key);
if (found == NULL)
return NULL;

gchar *file = g_build_filename(
pref_protocol_data_dir,
"java_edition/indexed_data",
found->valuestring,
"entity_event.json",
NULL
);
gchar *content = NULL;
if (!g_file_get_contents(file, &content, NULL, NULL)) {
ws_log("MC-Dissector", LOG_LEVEL_WARNING, "Cannot read file %s", file);
g_free(file);
return NULL;
}

cached = cJSON_Parse(content);
g_free(content);
set_cached_entity_event(protocol_version, cached);
}

cJSON *data = cJSON_GetObjectItem(cached, index);
if (data == NULL)
return NULL;
return data->valuestring;
}
4 changes: 4 additions & 0 deletions protocol/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ gchar *get_entity_sync_data_name(guint protocol_version, gchar *entity_id, guint

gchar *get_registry_data(guint protocol_version, gchar *registry, guint index);

gchar *get_level_event_data(guint protocol_version, gchar *index);

gchar *get_entity_event_data(guint protocol_version, gchar *index);

#endif //MC_DISSECTOR_STORAGE_H
Loading

0 comments on commit 0d10bd8

Please sign in to comment.