# Path: ggpo/common/util.py
# def logdebug():
#     global _loggerInitialzed
#     if not _loggerInitialzed:
#         _loggerInitialzed = True
#         loggerInit()
#     return logging.getLogger('GGPODebug')
# 
# def findUnsupportedGamesavesDir():
#     d = Settings.value(Settings.UNSUPPORTED_GAMESAVES_DIR)
#     if d and os.path.isdir(d):
#         return d
#     d = os.path.abspath(os.path.join(expanduser("~"), "ggpoUnsupportedGamesavestates"))
#     if d and os.path.isdir(d):
#         return d
#     # noinspection PyBroadException
#     try:
#         os.makedirs(d)
#         return d
#     except:
#         pass
# 
# def sha256digest(fname):
#     return hashlib.sha256(open(fname, 'rb').read()).hexdigest()


import glob
import hashlib
import json
import os
import re
import urllib
import urllib2
import time
from PyQt4 import QtCore
from ggpo.common.util import logdebug, findUnsupportedGamesavesDir, sha256digest
# -*- coding: utf-8 -*-


def readLocalJsonDigest():
    localJsonDigest = {}
    d = findUnsupportedGamesavesDir()
    if d:
        localjson = os.path.join(d, SyncWorker.JSON_INDEX_FILENAME)
        if os.path.isfile(localjson):
            # noinspection PyBroadException
            try:
                localJsonDigest = json.load(file(localjson).read())
            except:
                pass
        if not localJsonDigest:
            return writeLocalJsonDigest()
    return localJsonDigest


def writeLocalJsonDigest():
    localJsonDigest = {}
    d = findUnsupportedGamesavesDir()
    if d:
        localjson = os.path.join(d, SyncWorker.JSON_INDEX_FILENAME)
        for filename in glob.glob(os.path.join(d, '*.fs')):

# Path: ggpo/common/util.py
# def logdebug():
#     global _loggerInitialzed
#     if not _loggerInitialzed:
#         _loggerInitialzed = True
#         loggerInit()
#     return logging.getLogger('GGPODebug')
# 
# def findUnsupportedGamesavesDir():
#     d = Settings.value(Settings.UNSUPPORTED_GAMESAVES_DIR)
#     if d and os.path.isdir(d):
#         return d
#     d = os.path.abspath(os.path.join(expanduser("~"), "ggpoUnsupportedGamesavestates"))
#     if d and os.path.isdir(d):
#         return d
#     # noinspection PyBroadException
#     try:
#         os.makedirs(d)
#         return d
#     except:
#         pass
# 
# def sha256digest(fname):
#     return hashlib.sha256(open(fname, 'rb').read()).hexdigest()


# Path: ggpo/common/unsupportedsavestates.py
import glob
import hashlib
import json
import os
import re
import urllib
import urllib2
import time
from PyQt4 import QtCore
from ggpo.common.util import logdebug, findUnsupportedGamesavesDir, sha256digest

# -*- coding: utf-8 -*-


def readLocalJsonDigest():
    localJsonDigest = {}
    d = findUnsupportedGamesavesDir()
    if d:
        localjson = os.path.join(d, SyncWorker.JSON_INDEX_FILENAME)
        if os.path.isfile(localjson):
            # noinspection PyBroadException
            try:
                localJsonDigest = json.load(file(localjson).read())
            except:
                pass
        if not localJsonDigest:
            return writeLocalJsonDigest()
    return localJsonDigest


def writeLocalJsonDigest():
    localJsonDigest = {}
    d = findUnsupportedGamesavesDir()
    if d:
        localjson = os.path.join(d, SyncWorker.JSON_INDEX_FILENAME)
        for filename in glob.glob(os.path.join(d, '*.fs')):

            localJsonDigest[os.path.basename(filename)] = sha256digest(filename)