def ids_loader(game_version, namespace=u"minecraft", json_dict=False, timestamps=False):
    """Load the whole files from mcver directory.
    :game_version: str/unicode: the game version for which the resources will be loaded.
    :namespace: unicode: the name to be put in front of some IDs. default to 'minecraft'.
    :json_dict: bool: Whether to return a ran dict from the JSon file(s) instead of the (MCEDIT_DEFS, MCEDIT_IDS) pair.
    :timestamp: bool: wheter the return also the loaded file timestamp."""
    log.info("Loading resources for MC %s"%game_version)
    global MCEDIT_DEFS
    global MCEDIT_IDS
    MCEDIT_DEFS = {}
    MCEDIT_IDS = {}
    if json_dict:
        _json = {}
    if timestamps:
        _timestamps = {}
    d = os.path.join('mcver', game_version)

    # If version 1.2.4 files are not found, try to load the one for the closest
    # lower version (like 1.2.3 or 1.2).
    if not os.path.isdir(d) and game_version != "Unknown":
        log.info("No definitions found for MC %s. Trying to find ones for the closest lower version." % game_version)
        ver_dirs = os.listdir('mcver')
        ver_dirs.append(game_version)
        ver_dirs.sort(key=LooseVersion)
        idx = ver_dirs.index(game_version) - 1
        ver = ver_dirs[idx]
        d = os.path.join('mcver', ver)
        log.info("Closest lower version found is %s." % ver)

    if os.path.isdir(d):
        for file_name in os.listdir(d):
            if os.path.splitext(file_name)[-1].lower() == '.json':
                log.info("Found %s" % file_name)
                path_name = os.path.join(d, file_name)
                data = _get_data(path_name)
                if timestamps:
                    _timestamps[path_name] = os.stat(path_name).st_mtime
                if data:
                    # We use here names coming from the 'minecraft:name_of_the_stuff' ids
                    # The second part of the name is present in the first file used (for MC 1.11) in the 'idStr' value).
                    # The keys of MCEDIT_DEFS are built by concatening the file base name and the idStr
                    # References to MCEDIT_DEFS elements are stored in MCEDIT_IDS dict.
                    # If an element "load" is defined in the JSON data, it must be a string corresponding to another game version.
                    # The corresponding file will be loaded before parsing the data.
                    log.info("Loading...")
                    prefix = os.path.splitext(file_name)[0]
                    deps = []
                    r = ''
                    _data = {}
                    first = True
                    while type(r) in (str, unicode):
                        if first:
                            r = _parse_data(data, prefix, namespace, MCEDIT_DEFS, MCEDIT_IDS)
                            if json_dict:
                                _json.update(data)
                            first = False
                        else:
                            r = _parse_data(_data, prefix, namespace, MCEDIT_DEFS, MCEDIT_IDS)
                            if json_dict:
                                _json.update(_data)
                        if isinstance(r, (str, unicode)):
                            v = game_version
                            if len(deps):
                                v = deps[-1]
                            log.info("Found dependency for %s %s"%(v, prefix))
                            deps.append(r)
                            _data = _get_data(os.path.join('mcver', r, file_name))
                        else:
                            defs, ids = r
                            MCEDIT_DEFS.update(defs)
                            MCEDIT_IDS.update(ids)
                    if deps:
                        deps.reverse()
                        log.info("Loading definitions dependencies")
                        _data = {}
                        for dep in deps:
                            _file_name = os.path.join('mcver', dep, file_name)
                            if os.path.exists(_file_name):
                                log.info("Found %s"%_file_name)
                                #_data.update(_get_data(_file_name))
                                update(_data, _get_data(_file_name))
                                if timestamps:
                                    _timestamps[_file_name] = os.stat(_file_name).st_mtime
                            else:
                                log.info("Could not find %s"%_file_name)
                        update(_data, data)
                        #_data.update(data)
                        _defs, _ids = _parse_data(_data, prefix, namespace, MCEDIT_DEFS, MCEDIT_IDS, ignore_load=True)
                        update(MCEDIT_DEFS, _defs)
                        update(MCEDIT_IDS, _ids)
                        #MCEDIT_DEFS.update(_defs)
                        #MCEDIT_IDS.update(_ids)
                        if json_dict:
                            _json.update(_data)
                    log.info("Done")
    else:
        log.info("MC %s resources not found."%game_version)
    # Override the module objects to expose them outside when (re)importing.
    pymclevel.MCEDIT_DEFS = MCEDIT_DEFS
    pymclevel.MCEDIT_IDS = MCEDIT_IDS
    log.info("Loaded %s defs and %s ids"%(len(MCEDIT_DEFS), len(MCEDIT_IDS)))
    toreturn = (MCEDIT_DEFS, MCEDIT_IDS)
    if json_dict:
        toreturn = _json
    if '--dump-defs' in sys.argv:
        dump_f_name = 'defs_ids-%s.json' % game_version
        log.info("Dumping definitions as Json data in '%s'." % dump_f_name)
        with open(dump_f_name, 'w') as f:
            f.write("#" * 80)
            f.write("\nDEFS\n")
            f.write(json.dumps(MCEDIT_DEFS, indent=4))
            f.write("\n\n" + "#" * 80)
            f.write("\nIDS\n")
            f.write(json.dumps(MCEDIT_IDS, indent=4))
            f.close()
    if timestamps:
        toreturn += (_timestamps,)
    return toreturn
Beispiel #2
0
def ids_loader(game_version, namespace=u"minecraft"):
    """Load the whole files from mcver directory.
    :game_version: str/unicode: the game version for which the resources will be loaded.
    namespace: unicode: the name to be put in front of some IDs. default to 'minecraft'."""
    log.info("Loading resources for MC %s" % game_version)
    global MCEDIT_DEFS
    global MCEDIT_IDS
    MCEDIT_DEFS = {}
    MCEDIT_IDS = {}
    d = os.path.join('mcver', game_version)
    if os.path.isdir(d):
        for file_name in os.listdir(d):
            if os.path.splitext(file_name)[-1].lower() == '.json':
                data = None
                log.info("Found %s" % file_name)
                data = _get_data(os.path.join(d, file_name))
                if data:
                    # We use here names coming from the 'minecraft:name_of_the_stuff' ids
                    # The second part of the name is present in the first file used (for MC 1.11) in the 'idStr' value).
                    # The keys of MCEDIT_DEFS are built by concatening the file base name and the idStr
                    # References to MCEDIT_DEFS elements are stored in MCEDIT_IDS dict.
                    # If an element "load" is defined in the JSON data, it must be a string corresponding to another game version.
                    # The corresponding file will be loaded before parsing the data.
                    log.info("Loading...")
                    prefix = os.path.splitext(file_name)[0]
                    deps = []
                    r = ''
                    _data = {}
                    first = True
                    while type(r) in (str, unicode):
                        if first:
                            r = _parse_data(data, prefix, namespace,
                                            MCEDIT_DEFS, MCEDIT_IDS)
                            first = False
                        else:
                            r = _parse_data(_data, prefix, namespace,
                                            MCEDIT_DEFS, MCEDIT_IDS)
                        if type(r) in (str, unicode):
                            v = game_version
                            if len(deps):
                                v = deps[-1]
                            log.info("Found dependency for %s %s" %
                                     (v, prefix))
                            deps.append(r)
                            _data = _get_data(
                                os.path.join('mcver', r, file_name))
                        else:
                            defs, ids = r
                            MCEDIT_DEFS.update(defs)
                            MCEDIT_IDS.update(ids)
                    if deps:
                        deps.reverse()
                        log.info("Loading definitions dependencies")
                        _data = {}
                        for dep in deps:
                            _file_name = os.path.join('mcver', dep, file_name)
                            if os.path.exists(_file_name):
                                log.info("Found %s" % _file_name)
                                _data.update(_get_data(_file_name))
                            else:
                                log.info("Could not find %s" % _file_name)
                        _data.update(data)
                        _defs, _ids = _parse_data(_data,
                                                  prefix,
                                                  namespace,
                                                  MCEDIT_DEFS,
                                                  MCEDIT_IDS,
                                                  ignore_load=True)
                        MCEDIT_DEFS.update(_defs)
                        MCEDIT_IDS.update(_ids)
                    log.info("Done")
    else:
        log.info("MC %s resources not found." % game_version)
    # Override the module objects to expose them outside when (re)importing.
    pymclevel.MCEDIT_DEFS = MCEDIT_DEFS
    pymclevel.MCEDIT_IDS = MCEDIT_IDS
    log.info("Loaded %s defs and %s ids" % (len(MCEDIT_DEFS), len(MCEDIT_IDS)))
    return MCEDIT_DEFS, MCEDIT_IDS
Beispiel #3
0
def ids_loader(game_version,
               namespace=u"minecraft",
               json_dict=False,
               timestamps=False):
    """Load the whole files from mcver directory.
    :game_version: str/unicode: the game version for which the resources will be loaded.
    :namespace: unicode: the name to be put in front of some IDs. default to 'minecraft'.
    :json_dict: bool: Whether to return a ran dict from the JSon file(s) instead of the (MCEDIT_DEFS, MCEDIT_IDS) pair.
    :timestamp: bool: wheter the return also the loaded file timestamp."""
    log.info("Loading resources for MC %s" % game_version)
    global MCEDIT_DEFS
    global MCEDIT_IDS
    MCEDIT_DEFS = {}
    MCEDIT_IDS = {}
    if json_dict:
        _json = {}
    if timestamps:
        _timestamps = {}
    d = os.path.join('mcver', game_version)

    # If version 1.2.4 files are not found, try to load the one for the closest
    # lower version (like 1.2.3 or 1.2).
    if not os.path.isdir(d) and game_version != "Unknown":
        log.info(
            "No definitions found for MC %s. Trying to find ones for the closest lower version."
            % game_version)
        ver_dirs = os.listdir('mcver')
        ver_dirs.append(game_version)
        ver_dirs.sort(key=LooseVersion)
        idx = ver_dirs.index(game_version) - 1
        ver = ver_dirs[idx]
        d = os.path.join('mcver', ver)
        log.info("Closest lower version found is %s." % ver)

    if os.path.isdir(d):
        for file_name in os.listdir(d):
            if os.path.splitext(file_name)[-1].lower() == '.json':
                log.info("Found %s" % file_name)
                path_name = os.path.join(d, file_name)
                data = _get_data(path_name)
                if timestamps:
                    _timestamps[path_name] = os.stat(path_name).st_mtime
                if data:
                    # We use here names coming from the 'minecraft:name_of_the_stuff' ids
                    # The second part of the name is present in the first file used (for MC 1.11) in the 'idStr' value).
                    # The keys of MCEDIT_DEFS are built by concatening the file base name and the idStr
                    # References to MCEDIT_DEFS elements are stored in MCEDIT_IDS dict.
                    # If an element "load" is defined in the JSON data, it must be a string corresponding to another game version.
                    # The corresponding file will be loaded before parsing the data.
                    log.info("Loading...")
                    prefix = os.path.splitext(file_name)[0]
                    deps = []
                    r = ''
                    _data = {}
                    first = True
                    while type(r) in (str, unicode):
                        if first:
                            r = _parse_data(data, prefix, namespace,
                                            MCEDIT_DEFS, MCEDIT_IDS)
                            if json_dict:
                                _json.update(data)
                            first = False
                        else:
                            r = _parse_data(_data, prefix, namespace,
                                            MCEDIT_DEFS, MCEDIT_IDS)
                            if json_dict:
                                _json.update(_data)
                        if isinstance(r, (str, unicode)):
                            v = game_version
                            if len(deps):
                                v = deps[-1]
                            log.info("Found dependency for %s %s" %
                                     (v, prefix))
                            deps.append(r)
                            _data = _get_data(
                                os.path.join('mcver', r, file_name))
                        else:
                            defs, ids = r
                            MCEDIT_DEFS.update(defs)
                            MCEDIT_IDS.update(ids)
                    if deps:
                        deps.reverse()
                        log.info("Loading definitions dependencies")
                        _data = {}
                        for dep in deps:
                            _file_name = os.path.join('mcver', dep, file_name)
                            if os.path.exists(_file_name):
                                log.info("Found %s" % _file_name)
                                #_data.update(_get_data(_file_name))
                                update(_data, _get_data(_file_name))
                                if timestamps:
                                    _timestamps[_file_name] = os.stat(
                                        _file_name).st_mtime
                            else:
                                log.info("Could not find %s" % _file_name)
                        update(_data, data)
                        #_data.update(data)
                        _defs, _ids = _parse_data(_data,
                                                  prefix,
                                                  namespace,
                                                  MCEDIT_DEFS,
                                                  MCEDIT_IDS,
                                                  ignore_load=True)
                        update(MCEDIT_DEFS, _defs)
                        update(MCEDIT_IDS, _ids)
                        #MCEDIT_DEFS.update(_defs)
                        #MCEDIT_IDS.update(_ids)
                        if json_dict:
                            _json.update(_data)
                    log.info("Done")
    else:
        log.info("MC %s resources not found." % game_version)
    # Override the module objects to expose them outside when (re)importing.
    pymclevel.MCEDIT_DEFS = MCEDIT_DEFS
    pymclevel.MCEDIT_IDS = MCEDIT_IDS
    log.info("Loaded %s defs and %s ids" % (len(MCEDIT_DEFS), len(MCEDIT_IDS)))
    toreturn = (MCEDIT_DEFS, MCEDIT_IDS)
    if json_dict:
        toreturn = _json
    if '--dump-defs' in sys.argv:
        dump_f_name = 'defs_ids-%s.json' % game_version
        log.info("Dumping definitions as Json data in '%s'." % dump_f_name)
        with open(dump_f_name, 'w') as f:
            f.write("#" * 80)
            f.write("\nDEFS\n")
            f.write(json.dumps(MCEDIT_DEFS, indent=4))
            f.write("\n\n" + "#" * 80)
            f.write("\nIDS\n")
            f.write(json.dumps(MCEDIT_IDS, indent=4))
            f.close()
    if timestamps:
        toreturn += (_timestamps, )
    return toreturn