package configgen.gen;

import configgen.Logger;
import configgen.genallref.GenAllRefValues;
import configgen.gencs.GenCs;
import configgen.gencs.GenPack;
import configgen.genjava.BinaryToText;
import configgen.genjava.GenJavaCode;
import configgen.genjava.GenJavaData;
import configgen.genlua.GenI18n;
import configgen.genlua.GenLua;
import configgen.util.CachedFiles;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public final class Main {
    private static void usage(String reason) {
        System.err.println(reason);

        System.out.println("Usage: java -jar configgen.jar [options]");
        System.out.println("	-datadir      配表所在目录");
        System.out.println("	-xml          配表结构文件，默认是config.xml");
        System.out.println("	-encoding     配表和配表结构文件的编码，默认是GBK，如果文件中含有bom则用bom标记的编码");

        System.out.println("	-i18nfile     国际化需要的文件，如果不用国际化，就不要配置");
        System.out.println("	-i18nencoding 国际化需要的文件的编码，默认是GBK，如果文件中含有bom则用bom标记的编码");
        System.out.println("    -i18ncrlfaslf 把字符串里的\\r\\n 替换为 \\n，默认是false");


        System.out.println("	-binaryToText 后可接2个参数table match，把java data转变为文本");

        System.out.println("	-verify       检查配表约束");
        System.out.println("	-v[1]         输出一些额外信息,1是额外gc测试内存");
        Generators.getAllProviders().forEach((k, v) -> System.out.println("	-gen          " + k + "," + v.usage()));

        Runtime.getRuntime().exit(1);
    }

    public static void main(String[] args) throws Exception {
        GenJavaCode.register();
        GenJavaData.register();
        GenLua.register();
        GenI18n.register();
        GenAllRefValues.register();
        GenCs.register();
        GenPack.register();

        String binaryToTextFile = null;
        String match = null;
        String datadir = null;
        String xml = null;
        String encoding = "GBK";
        String i18nfile = null;
        String i18nencoding = "GBK";
        boolean i18ncrlfaslf = false;
        boolean verify = false;
        List<Generator> generators = new ArrayList<>();

        for (int i = 0; i < args.length; ++i) {
            switch (args[i]) {
                case "-binaryToText":
                    binaryToTextFile = args[++i];
                    if (!args[i+1].startsWith("-")){
                        match = args[++i];
                    }
                    break;

                case "-datadir":
                    datadir = args[++i];
                    break;
                case "-xml":
                    xml = args[++i];
                    break;
                case "-encoding":
                    encoding = args[++i];
                    break;

                case "-i18nfile":
                    i18nfile = args[++i];
                    break;
                case "-i18nencoding":
                    i18nencoding = args[++i];
                    break;
                case "-i18ncrlfaslf":
                    i18ncrlfaslf = true;
                    break;

                case "-verify":
                    verify = true;
                    break;
                case "-v":
                    Logger.enableVerbose();
                    break;
                case "-v1":
                    Logger.enableVerbose();
                    Logger.enableMmGc();
                    break;

                case "-gen":
                    Generator generator = Generators.create(args[++i]);
                    if (generator == null)
                        usage("");
                    generators.add(generator);
                    break;
                default:
                    usage("unknown args " + args[i]);
                    break;
            }
        }

        if (binaryToTextFile != null){
            BinaryToText.parse(binaryToTextFile, match);
            return;
        }

        if (datadir == null) {
            usage("-datadir 未配置");
            return;
        }

        Path dataDir = Paths.get(datadir);
        File xmlFile = xml != null ? new File(xml) : dataDir.resolve("config.xml").toFile();

        Logger.mm(String.format("start total memory %dm", Runtime.getRuntime().maxMemory() / 1024 / 1024));
        Context ctx = new Context(dataDir, xmlFile, encoding, i18nfile, i18nencoding, i18ncrlfaslf);
        if (verify) {
            Logger.verbose("-----start verify");
            ctx.makeValue();
        }

        for (Generator generator : generators) {
            Logger.verbose("-----generate " + generator.parameter);
            generator.generate(ctx);
            Logger.mm("generate " + generator.parameter.type);
        }

        CachedFiles.finalExit();
        Logger.mm("end");
    }

}

@@ -694,17 +694,20 @@
 rintln(%22
-%09
+    
 -datadir
@@ -751,17 +751,20 @@
 rintln(%22
-%09
+    
 -xml    
@@ -822,17 +822,20 @@
 rintln(%22
-%09
+    
 -encodin
@@ -911,25 +911,28 @@
 ut.println(%22
-%09
+    
 -i18nfile   
@@ -988,17 +988,20 @@
 rintln(%22
-%09
+    
 -i18nenc
@@ -1132,18 +1132,16 @@
 e%22);%0D%0A%0D%0A
-%0D%0A
         
@@ -1160,17 +1160,20 @@
 rintln(%22
-%09
+    
 -binaryT
@@ -1247,17 +1247,20 @@
 rintln(%22
-%09
+    
 -verify 
@@ -1304,17 +1304,20 @@
 rintln(%22
-%09
+    
 -v%5B1%5D   
@@ -1425,9 +1425,12 @@
 ln(%22
-%09
+    
 -gen

Update Main.java