From df03ce3d440f8fe0f46e101cf15826d3caa13433 Mon Sep 17 00:00:00 2001 From: Ishotihadus Date: Sun, 9 Jul 2017 01:11:45 +0900 Subject: [PATCH] Add YAML mode to mikunyan-json --- README.md | 7 ++++--- exe/mikunyan-json | 27 +++++++++++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 813af5a..93507f9 100644 --- a/README.md +++ b/README.md @@ -118,16 +118,17 @@ img = Mikunyan::ImageDecoder.decode_object(obj) img.save('mikunyan.png') ``` -### Json Outputer +### Json / YAML Outputer -`mikunyan-json` is the executable command for converting unity3d to json. +`mikunyan-json` is an executable command for converting unity3d to json. $ mikunyan-json bundle.unity3d > bundle.json Available options: - `--as-asset` (`-a`): interpret input file as not AssetBudnle but Asset -- `--pretty` (`-p`): prettify output json +- `--pretty` (`-p`): prettify output json (`mikunyan-json` only) +- `--yaml` (`-y`): YAML mode ## Dependencies diff --git a/exe/mikunyan-json b/exe/mikunyan-json index 2a7e0ee..45241a4 100755 --- a/exe/mikunyan-json +++ b/exe/mikunyan-json @@ -1,6 +1,5 @@ #!/usr/bin/env ruby require 'mikunyan' -require 'json' require 'base64' def obj64(obj) @@ -19,7 +18,7 @@ def obj64(obj) end end -opts = {:as_asset => false, :pretty => false} +opts = {:as_asset => false, :pretty => false, :yaml => false} arg = nil i = 0 while i < ARGV.count @@ -29,9 +28,10 @@ while i < ARGV.count opts[:as_asset] = true when '--pretty', '-p' opts[:pretty] = true + when '--yaml', '-y' + opts[:yaml] = true else warn("Unknown option: #{ARGV[i]}") - exit(1) end else arg = ARGV[i] unless arg @@ -39,6 +39,10 @@ while i < ARGV.count i += 1 end +if option[:pretty] && option[:yaml] + warn("Option --pretty is ignored if --yaml is specified.") +end + unless File.file?(arg) warn("File not found: #{arg}") exit(1) @@ -51,7 +55,7 @@ if opts[:as_asset] objs = [] asset.path_ids.each do |e| obj = asset.parse_object_simple(e) - objs << obj64(obj) if obj + objs << obj end assets[asset.name] = objs else @@ -60,14 +64,21 @@ else objs = [] asset.path_ids.each do |e| obj = asset.parse_object_simple(e) - objs << obj64(obj) if obj + objs << obj end assets[asset.name] = objs end end -if opts[:pretty] - puts JSON.pretty_generate(assets) +if opts[:yaml] + require 'yaml' + puts YAML.dump(assets) else - puts JSON.generate(assets) + require 'json' + assets = assets.map{|k, v| [k, obj64(v)]}.to_h + if opts[:pretty] + puts JSON.pretty_generate(assets) + else + puts JSON.generate(assets) + end end