From 41b9eb7af0a86f6580ed957c6a0996a870c38e76 Mon Sep 17 00:00:00 2001 From: Synthasmagoria Date: Wed, 11 Nov 2026 15:55:26 +0100 Subject: [PATCH] create new instances --- jmap_config.json5 | 34 +++++++++--- main.odin | 138 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 140 insertions(+), 32 deletions(-) diff --git a/jmap_config.json5 b/jmap_config.json5 index 56c15a5..3a5ed10 100644 --- a/jmap_config.json5 +++ b/jmap_config.json5 @@ -1,20 +1,36 @@ -defaultLayer: "Instances" +layer: "Instances" objects: { - spikeUp: { + spike_up: { layer: "Threats", - objectName: "objSpikeUp", + object_name: "objSpikeUp", }, - spikeDown: { + spike_down: { layer: "Threats", - objectName: "objSpikeDown", + object_name: "objSpikeDown", }, - spikeLeft: { + spike_left: { layer: "Threats", - objectName: "objSpikeLeft", + object_name: "objSpikeLeft", }, - spikeRight: { + spike_right: { layer: "Threats", - objectName: "objSpikeRight", + object_name: "objSpikeRight", + }, + spike_up: { + layer: "Threats", + object_name: "objSpikeUp", + }, + spike_down: { + layer: "Threats", + object_name: "objSpikeDown", + }, + spike_left: { + layer: "Threats", + object_name: "objSpikeLeft", + }, + spike_right: { + layer: "Threats", + object_name: "objSpikeRight", }, } diff --git a/main.odin b/main.odin index 47bd6e9..8c75f2c 100644 --- a/main.odin +++ b/main.odin @@ -4,6 +4,8 @@ package main import "base:runtime" import "core:os" +import "core:math" +import "core:math/rand" import "core:fmt" import "core:encoding/json" import "core:strings" @@ -230,14 +232,59 @@ resource_room_layer_get_type :: proc(layer: json.Object) -> RoomLayerType { return ._Invalid } +resource_room_get_by_name :: proc(layers: json.Array, name: string) -> (_layer: json.Object, _ok: bool) { + for layer in layers { + layer := layer.(json.Object) + layer_name := layer["name"].(json.String) + if name == layer_name { + return layer, true + } + } + return {}, false +} + resource_path_is_type :: proc(path: string, type: ResourceType) -> bool { path := strings.split(path, "/") return path[0] == resource_type_folder_names[type] } -/* ### - PROGRAM -*/ +resource_room_layer_create_instance :: proc(version, resource_version, object_name: string, x, y: f64, allocator := context.allocator) -> json.Object { + instance_id_number := math.clamp(rand.uint64(), 0x10000000, 0xffffffff) + instance_id_hex_buffer: [8]byte + instance_id_hex := strconv.write_uint(instance_id_hex_buffer[:], instance_id_number, 16) + instance_id := strings.concatenate({"inst_", instance_id_hex}, allocator) + + instance := make(json.Object, allocator) + instance["$GMRInstance"] = version + instance["%Name"] = instance_id + instance["colour"] = 4294967295// 32 bit number + instance["frozen"] = false + instance["hasCreationCode"] = false + instance["ignore"] = false + instance["imageIndex"] = 0 + instance["imageSpeed"] = 1.0 + instance["inheritCode"] = false + instance["inheritedItemId"] = nil + instance["inheritItemSettings"] = false + instance["isDnd"] = false + instance["name"] = instance_id + + object_id := make(json.Object, allocator) + object_id["name"] = object_name + object_id["path"] = "" // todo get object resource path + instance["objectId"] = object_id + + instance["properties"] = json.Array({}) + instance["resourceType"] = "GMRinstance" + instance["resourceVersion"] = resource_version + instance["rotation"] = 0.0 + instance["scaleX"] = 1.0 + instance["scaleY"] = 1.0 + instance["x"] = x + instance["y"] = y + + return instance +} /* ### PROGRAM @@ -245,7 +292,7 @@ resource_path_is_type :: proc(path: string, type: ResourceType) -> bool { HELP_MESSAGE :: """ --- GAMEMAKER JMAP IMPORTER --- -Usage: [other flags...] +Usage: [other flags...] """ main :: proc() { @@ -255,10 +302,10 @@ main :: proc() { fmt.println("ERROR: Too few arguments") fmt.println(HELP_MESSAGE) case: - yyp_path := os.args[1] + room_resource_path := os.args[1] jmap_path := os.args[2] jmap_config_path := os.args[3] - error := program(yyp_path, jmap_path, jmap_config_path) + error := program(room_resource_path, jmap_path, jmap_config_path) if error != nil { error := error.(ProgramError) fmt.println("ERROR:", error.message) @@ -269,6 +316,9 @@ main :: proc() { ProgramErrorError :: enum { JmapBadObjectList, + BadJmapConfig, + RoomLayerNotFound, + RoomLayerNotInstance, } ProgramErrorType :: union { @@ -288,44 +338,72 @@ program_error_create :: proc(error: ProgramErrorType, message: string, loc := #c return {type = error, message = message, location = loc} } -program :: proc(yyp_path, jmap_path, jmap_config_path: string) -> Maybe(ProgramError) { - yyp_string, yyp_read_error := os.read_entire_file_from_path(yyp_path, context.allocator) - if yyp_read_error != nil { - return program_error_create(yyp_read_error, fmt.tprint("Couldn't open GameMaker project path at '", yyp_path, "'", sep = "")) +program :: proc(room_resource_path, jmap_path, jmap_config_path: string) -> Maybe(ProgramError) { + room_resource_string, room_resource_read_error := os.read_entire_file_from_path(room_resource_path, context.allocator) + if room_resource_read_error != nil { + return program_error_create(room_resource_read_error, fmt.tprint("Couldn't open GameMaker project path at '", room_resource_path, "'", sep = "")) } - yyp, yyp_parse_error := json.parse_bytes(yyp_string, allocator = context.allocator) - if yyp_parse_error != nil { - return program_error_create(yyp_parse_error ,"Failed to parse yyp project json") + room_resource_json, room_resource_parse_error := json.parse_bytes(room_resource_string, allocator = context.allocator) + if room_resource_parse_error != nil { + return program_error_create(room_resource_parse_error ,"Failed to parse yyp project json") } - jmap, jmap_read_error := os.read_entire_file_from_path(jmap_path, context.allocator) + jmap_json, jmap_read_error := os.read_entire_file_from_path(jmap_path, context.allocator) if jmap_read_error != nil { return program_error_create(jmap_read_error, fmt.tprint("Couldn't open jmap path at '", jmap_path, "'", sep = "")) } - jmap_object_list, jmap_object_list_error := jmap_object_list_from_text(string(jmap), context.allocator, context.temp_allocator) - switch jmap_object_list_error { + jmap_instance_list, jmap_instance_list_error := jmap_instance_list_from_text(string(jmap_json), context.allocator, context.temp_allocator) + switch jmap_instance_list_error { case .None: case .NoStartMarker: - return program_error_create(jmap_object_list_error, "Couldn't find start marker in jmap") + return program_error_create(jmap_instance_list_error, "Couldn't find start marker in jmap") case .NoEndMarker: - return program_error_create(jmap_object_list_error, "Couldn't find end marker in jmap") + return program_error_create(jmap_instance_list_error, "Couldn't find end marker in jmap") case .BadListLength: - return program_error_create(jmap_object_list_error, "Object list was not of a correct length") + return program_error_create(jmap_instance_list_error, "Object list was not of a correct length") case .BadObject: - return program_error_create(jmap_object_list_error, "Bad object in jmap object list") + return program_error_create(jmap_instance_list_error, "Bad object in jmap object list") } - fmt.println(jmap_object_list) - jmap_config_text, jmap_config_read_error := os.read_entire_file_from_path(jmap_config_path, allocator = context.allocator) if jmap_config_read_error != nil { return program_error_create(jmap_read_error, fmt.tprint("Couldn't open jmap config path at '", jmap_config_path, "'", sep = "")) } + + jmap_config_json, jmap_config_parse_error := json.parse_bytes(jmap_config_text, allocator = context.allocator) + if jmap_config_parse_error != nil { + return program_error_create(jmap_config_parse_error, "Failed to parse jmap config"); + } + + jmap_config, jmap_config_ok := jmap_config_get_from_json(jmap_config_json) + if !jmap_config_ok { + return program_error_create(ProgramErrorError.BadJmapConfig, "Malformed jmap config") + } + + room_resource := room_resource_json.(json.Object) + room_layers := room_resource["layers"].(json.Array) + + room_layer, room_layer_found := resource_room_get_by_name(room_layers, jmap_config.layer) + if !room_layer_found { + return program_error_create(ProgramErrorError.RoomLayerNotFound, fmt.aprint("Couldn't find room layer '", jmap_config.layer, "'", sep = "")) + } + if room_layer_type := resource_room_layer_get_type(room_layer); room_layer_type != .Instance { + return program_error_create(ProgramErrorError.RoomLayerNotInstance, fmt.aprint("Found room layer '", jmap_config.layer, "' was not an instance layer", sep = "")) + } + + for jmap_instance in jmap_instance_list { + + } + return nil } +JmapImportConfig :: struct { + layer: string, +} + JmapObjectListError :: enum { None, NoStartMarker, @@ -338,7 +416,7 @@ JmapObjectInstance :: struct { x, y, object_index: int, } -jmap_object_list_from_text :: proc(text: string, allocator: mem.Allocator, temp_allocator: mem.Allocator) -> (_object_list: []JmapObjectInstance, _error: JmapObjectListError) { +jmap_instance_list_from_text :: proc(text: string, allocator: mem.Allocator, temp_allocator: mem.Allocator) -> (_object_list: []JmapObjectInstance, _error: JmapObjectListError) { object_start_marker := "objects: (x, y, type)\n" object_start_marker_index := strings.index(text, object_start_marker) if object_start_marker_index == -1 { @@ -383,6 +461,20 @@ jmap_object_list_from_text :: proc(text: string, allocator: mem.Allocator, temp_ return object_list, .None } +jmap_config_get_from_json :: proc(root: json.Value) -> (_config: JmapImportConfig, _ok: bool) { + _ok = true + root := root.(json.Object) + + if layer, layer_ok := root["layer"]; !layer_ok { + fmt.println("ERROR: Couldn't find 'layer' property in jmap config") + _ok = false + } else { + _config.layer = layer.(json.String) + } + + return _config, _ok +} + JmapObject :: enum { Block = 1, MiniBlock = 2,