package main import "core:os" import "core:fmt" import "core:encoding/json" import "core:mem" import "core:mem/virtual" import "core:strings" /* ### 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_path_is_type :: proc(path: string, type: ResourceType) -> bool { path := strings.split(path, "/") return path[0] == resource_type_folder_names[type] } /* ### PROGRAM */ print_help_message :: proc() { fmt.println( "--- GameMaker capitalization inconsistency check ---\n" + "Usage: ") } Error :: union { ResourceError, mem.Allocator_Error, } main :: proc() { arena: virtual.Arena arena_err := virtual.arena_init_growing(&arena) if arena_err != nil { fmt.println("Couldn't initialize arena", arena_err) } context.allocator = virtual.arena_allocator(&arena) switch len(os.args) { case 0, 1: print_help_message() break case 2: err := program(project_path = os.args[1]) if err != nil { fmt.println("Program failed with error:", err) } case 3: fmt.println("Too many arguments") } } program :: proc(project_path: string) -> Error { project := resource_read(project_path) or_return project_dir, _ := os.split_path(project_path) resources := project["resources"].(json.Array) room_paths := make([dynamic]string) for resource in resources { resource := resource.(json.Object) id := resource["id"].(json.Object) relative_resource_path := id["path"].(json.String) resource_path := os.join_path({project_dir, relative_resource_path}, context.allocator) or_return if !os.exists(resource_path) { fmt.println("Couldn't open resource at:", resource_path) } if resource_path_is_type(relative_resource_path, .Room) { append(&room_paths, resource_path) } } for room_path in room_paths { room := resource_read(room_path) or_return layers := room["layers"].(json.Array) for layer in layers { layer := layer.(json.Object) if resource_room_layer_get_type(layer) != .Instance { continue } instances := layer["instances"].(json.Array) for instance in instances { instance := instance.(json.Object) object_id := instance["objectId"].(json.Object) relative_object_path := object_id["path"].(json.String) object_path := os.join_path({project_dir, relative_object_path}, context.allocator) or_return if !os.exists(object_path) { fmt.println("Couldn't open object in room", object_path) } if len(object_id["name"].(json.String)) == 0 { fmt.println("Name was empty") } } } } return nil }