#+feature dynamic-literals 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" import "core:strconv" import "core:mem" /* ### SWITCHES */ SwitchDefinition :: struct { name: string, fullname: string, shortname: string, usage: string, description: string, no_argument: bool, callback: proc(value: string, args: ^byte) -> (ok: bool), } SwitchSplitError :: enum { None, Empty, MissingDash, MissingStartQuote, MissingEndQuote, } switch_split :: proc(str: string) -> (name, value: string, err: SwitchSplitError) { name = "" value = "" err = .None if len(str) == 0 { err = .Empty return } if str[0] != '-' { err = .MissingDash return } colon_index := strings.index_rune(str, ':') if colon_index == -1 { name = str[1:] return } name = str[1:colon_index] value = str[colon_index + 1:] if value[0] != '"' && value[len(value) - 1] == '"' { err = .MissingStartQuote return } else if value[0] == '"' && value[len(value) - 1] != '"' { err = .MissingEndQuote return } if value[0] == '"' && value[len(value) - 1] == '"' { value = value[1:len(value) - 2] } return } process_switches :: proc(switch_definitions: []SwitchDefinition, args: []string, args_out: ^byte, temp_allocator := context.temp_allocator) -> (_ok: bool) { _ok = true error_state: bool switch_map := make(map[string]SwitchDefinition, temp_allocator) for switch_definition in switch_definitions { shortname := switch_definition.shortname shortname_length := len(shortname) if shortname_length > 0 { if shortname in switch_map { _ok = false existing_switch_definition := switch_map[shortname] fmt.eprintln("Argument error: Switch for ", switch_definition.name, " (", shortname, ") already existed for '", existing_switch_definition.name, sep = "") } else { switch_map[shortname] = switch_definition } } fullname := switch_definition.fullname fullname_length := len(fullname) if fullname_length > 0 { if fullname in switch_map { _ok = false existing_switch_definition := switch_map[fullname] fmt.eprintln("Argument error: Switch for ", switch_definition.name, " (", fullname, ") already existed for '", existing_switch_definition.name, sep = "") } else { switch_map[fullname] = switch_definition } } if shortname_length == 0 && fullname_length == 0 { _ok = false fmt.eprintln("Switch definition error: ", switch_definition.name, " had no shortname or fullname", sep = "") } } for arg in args[1:] { name, value, err := switch_split(arg) if err != .None { _ok = false fmt.eprintln("Argument error: ", err, " (", arg, ")", sep = "") continue } if name not_in switch_map { _ok = false fmt.eprintln("Argument error: Non-existent switch", name, sep = "") continue } switch_map[name].callback(value, args_out) } return _ok } /* ### GAMEMAKER RESOURCE HANDLING */ GAMEMAKER_RESOURCE_JSON5_MARSHAL_OPTIONS :: json.Marshal_Options { spec = .JSON5, pretty = true, sort_maps_by_key = true, indentation = 0, spaces = 4, use_spaces = true, } ResourceError :: union { os.Error, json.Error, json.Marshal_Error, } ResourceType :: enum { Extension, Font, Note, Object, Particle, Path, Room, Script, Sequence, Shader, Sound, Sprite, Tileset, } @rodata resource_type_folder_names := [ResourceType]string { .Extension = "extensions", .Font = "fonts", .Note = "notes", .Object = "objects", .Particle = "particles", .Path = "paths", .Room = "rooms", .Script = "scripts", .Sequence = "sequences", .Shader = "shaders", .Sound = "sound", .Sprite = "sprites", .Tileset = "tilesets", } RoomLayerType :: enum { Background, Instance, Tile, Path, Asset, Effects, UI, _Invalid, } @rodata room_layer_type_identifier := [RoomLayerType]string { .Background = "GMRBackgroundLayer", .Instance = "GMRInstanceLayer", .Tile = "GMRTileLayer", .Path = "GMRPathLayer", .Asset = "GMRAssetLayer", .Effects = "GMREffectsLayer", .UI = "GMRUiLayer", ._Invalid = "", } resource_read :: proc(path: string) -> (object: json.Object, err: ResourceError) { data := os.read_entire_file(path, context.allocator) or_return resource := json.parse(data, .JSON5, true) or_return object = resource.(json.Object) return } resource_write :: proc(path: string, data: json.Value) -> ResourceError { str := json.marshal(data, GAMEMAKER_RESOURCE_JSON5_MARSHAL_OPTIONS) or_return os.write_entire_file(path, str) or_return return nil } resource_get_version :: proc(resource: json.Object) -> string { return resource["resourceVersion"].(json.String) } resource_project_find_asset :: proc(project: json.Object, name: string, type: ResourceType) -> json.Value { resources := project["resources"].(json.Array) for resource in resources { id := resource.(json.Object)["id"].(json.Object) if id["name"].(json.String) != name { continue } if strings.split(id["path"].(json.String), "/")[0] != resource_type_folder_names[type] { continue } return resource } return nil } resource_room_layer_get_type :: proc(layer: json.Object) -> RoomLayerType { type_name := layer["resourceType"].(json.String) for type in RoomLayerType { if type_name == room_layer_type_identifier[type] { return type } } 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] } 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 */ HELP_MESSAGE :: """ --- GAMEMAKER JMAP IMPORTER --- Usage: [other flags...] """ main :: proc() { args := os.args switch len(args) { case 0, 1, 2, 3: fmt.println("ERROR: Too few arguments") fmt.println(HELP_MESSAGE) case: room_resource_path := os.args[1] jmap_path := os.args[2] jmap_config_path := os.args[3] error := program(room_resource_path, jmap_path, jmap_config_path) if error != nil { error := error.(ProgramError) fmt.println("ERROR:", error.message) fmt.println(error.location) } } } ProgramErrorError :: enum { JmapBadObjectList, BadJmapConfig, RoomLayerNotFound, RoomLayerNotInstance, } ProgramErrorType :: union { ProgramErrorError, JmapObjectListError, os.Error, json.Error } ProgramError :: struct { type: ProgramErrorType, message: string, location: runtime.Source_Code_Location, } program_error_create :: proc(error: ProgramErrorType, message: string, loc := #caller_location) -> ProgramError { return {type = error, message = message, location = loc} } 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 = "")) } 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_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_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_instance_list_error, "Couldn't find start marker in jmap") case .NoEndMarker: return program_error_create(jmap_instance_list_error, "Couldn't find end marker in jmap") case .BadListLength: return program_error_create(jmap_instance_list_error, "Object list was not of a correct length") case .BadObject: return program_error_create(jmap_instance_list_error, "Bad object in 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, NoEndMarker, BadListLength, BadObject, } JmapObjectInstance :: struct { x, y, object_index: int, } 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 { return {}, .NoStartMarker } object_start_index := object_start_marker_index + len(object_start_marker) object_end_marker := "version:" object_end_index := strings.index(text, object_end_marker) if object_end_index == -1 { return {}, .NoEndMarker } object_list_string := text[object_start_index:object_end_index] object_list_split := strings.split(object_list_string, sep = " ", allocator = temp_allocator) if len(object_list_split) % 3 != 0 { return {}, .BadListLength } object_list := make([]JmapObjectInstance, len(object_list_split) / 3, allocator) for i := 0; i < len(object_list); i += 1 { x, x_ok := strconv.parse_int(strings.trim_space(object_list_split[i * 3 + 0]), 10) if !x_ok { fmt.println("ERROR: Couldn't parse x of object '", object_list_split[i * 3 + 0], "'", sep = "") return {}, .BadObject } y, y_ok := strconv.parse_int(strings.trim_space(object_list_split[i * 3 + 1]), 10) if !y_ok { fmt.println("ERROR: Couldn't parse y of object '", object_list_split[i * 3 + 1], "'", sep = "") return {}, .BadObject } object_index, object_index_ok := strconv.parse_int(strings.trim_space(object_list_split[i * 3 + 2]), 10) if !object_index_ok { fmt.println("ERROR: Couldn't parse object_index of object '", object_list_split[i * 3 + 2], "'", sep = "") return {}, .BadObject } object_list[i] = {x, y, object_index} } 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, SpikeUp = 3, SpikeRight = 4, SpikeLeft = 5, SpikeDown = 6, MiniSpikeUp = 7, MiniSpikeRight = 8, MiniSpikeLeft = 9, MiniSpikeDown = 10, Fruit = 11, Save = 12, Platform = 13, Water1 = 14, Water3 = 15, VineR = 16, VineL = 17, KillerBlock = 18, PlayerStart = 20, Warp = 21, JumpRefresher = 22, Water2 = 23, GravityFlipperUp = 24, GravityFlipperDown = 25, SaveFlipped = 26, BulletBlocker = 27, SidewaysPlatform = 28, MiniWater1 = 30, MiniWater3 = 31, MiniWater2 = 32, MiniCatharsisWater = 33, WaterNekoron = 34, MiniWaterNekoron = 35, LineSpikeUp = 36, LineSpikeRight = 37, LineSpikeLeft = 38, LineSpikeDown = 39, MiniLineSpikeUp = 40, MiniLineSpikeRight = 41, MiniLineSpikeLeft = 42, MiniLineSpikeDown = 43, LineBlockHorizontal = 44, LineBlockVertical = 45, MiniLineBlockHorizontal = 46, MiniLineBlockVertical = 47, } jmap_object_map := map[int]string { 1 = "block", 2 = "miniBlock", 3 = "spikeUp", 4 = "spikeRight", 5 = "spikeLeft", 6 = "spikeDown", 7 = "miniSpikeUp", 8 = "miniSpikeRight", 9 = "miniSpikeLeft", 10 = "miniSpikeDown", 11 = "fruit", 12 = "save", 13 = "platform", 14 = "water1", 15 = "water3", 16 = "vineR", 17 = "vineL", 18 = "killerBlock", 20 = "playerStart", 21 = "warp", 22 = "jumpRefresher", 23 = "water2", 24 = "gravityFlipperUp", 25 = "gravityFlipperDown", 26 = "saveFlipped", 27 = "bulletBlocker", 28 = "sidewaysPlatform", 30 = "miniWater1", 31 = "miniWater3", 32 = "miniWater2", 33 = "miniCatharsisWater", 34 = "waterNekoron", 35 = "miniWaterNekoron", 36 = "spikeUp", 37 = "spikeRight", 38 = "spikeLeft", 39 = "spikeDown", 40 = "spikeUp", 41 = "spikeRight", 42 = "spikeLeft", 43 = "spikeDown", 44 = "lineBlockHorizontal", 45 = "lineBlockVertical", 46 = "miniLineBlockHorizontal", 47 = "miniLineBlockVertical", } jmap_object_map_reverse: map[string]int GameMakerInstanceDefinition :: struct { object_name: string, } jmap_instance_to_gamemaker_instance_map := #sparse [JmapObject]GameMakerInstanceDefinition { .Block = {object_name = "objBlock"}, .MiniBlock = {object_name = "objMiniBlock"}, .SpikeUp = {object_name = "objSpikeUp"}, .SpikeRight = {object_name = "objSpikeRight"}, .SpikeLeft = {object_name = "objSpikeLeft"}, .SpikeDown = {object_name = "objSpikeDown"}, .MiniSpikeUp = {object_name = "objMiniSpikeUp"}, .MiniSpikeRight = {object_name = "objMiniSpikeRight"}, .MiniSpikeLeft = {object_name = "objMiniSpikeLeft"}, .MiniSpikeDown = {object_name = "objMiniSpikeDown"}, .Fruit = {object_name = "objCherry"}, .Save = {object_name = "objSave"}, .Platform = {object_name = "objPlatform"}, .Water1 = {object_name = "objWater1"}, .Water3 = {object_name = "objWater3"}, .VineR = {object_name = "objVineR"}, .VineL = {object_name = "objVineL"}, .KillerBlock = {object_name = "objKillerBlock"}, .PlayerStart = {object_name = "objPlayerStart"}, .Warp = {object_name = "objWarp"}, .JumpRefresher = {object_name = "objJumpRefresher"}, .Water2 = {object_name = "objWater2"}, .GravityFlipperUp = {object_name = "objGravityFlipperUp"}, .GravityFlipperDown = {object_name = "objGravityFlipperDown"}, .SaveFlipped = {object_name = "objSaveFlipped"}, .BulletBlocker = {object_name = "objBulletBlocker"}, .SidewaysPlatform = {object_name = "objSidewaysPlatform"}, .MiniWater1 = {object_name = "objMiniWater1"}, .MiniWater3 = {object_name = "objMiniWater3"}, .MiniWater2 = {object_name = "objMiniWater2"}, .MiniCatharsisWater = {object_name = "objMiniCatharsisWater"}, .WaterNekoron = {object_name = "objWaterNekoron"}, .MiniWaterNekoron = {object_name = "objMiniWaterNekoron"}, .LineSpikeUp = {object_name = "objLineSpikeUp"}, .LineSpikeRight = {object_name = "objLineSpikeRight"}, .LineSpikeLeft = {object_name = "objLineSpikeLeft"}, .LineSpikeDown = {object_name = "objLineSpikeDown"}, .MiniLineSpikeUp = {object_name = "objMiniLineSpikeUp"}, .MiniLineSpikeRight = {object_name = "objMiniLineSpikeRight"}, .MiniLineSpikeLeft = {object_name = "objMiniLineSpikeLeft"}, .MiniLineSpikeDown = {object_name = "objMiniLineSpikeDown"}, .LineBlockHorizontal = {object_name = "objLineBlockHorizontal"}, .LineBlockVertical = {object_name = "objLineBlockVertical"}, .MiniLineBlockHorizontal = {object_name = "objMiniLineBlockHorizontal"}, .MiniLineBlockVertical = {object_name = "objMiniLineBlockVertical"}, }