File indexing completed on 2024-04-21 05:37:02

0001 #!/usr/bin/python3
0002 # -*- coding: utf-8 -*-
0003 
0004 import json
0005 import sys, getopt
0006 from pprint import pprint
0007 from enum import Enum
0008 import os
0009 
0010 class ModeEdition(Enum):
0011     NONE = 0
0012     MINIMAL = 1
0013     COMPLETE = 2
0014     EXPORT = 3
0015     IMPORT = 4
0016 
0017 def Usage():
0018     print("Usage")
0019     print("")
0020     print("-h, --help; show this documentation")
0021     print("-i, --input <file>; path to input file")
0022     print("-o, --output <file>; path to output file, should not exist")
0023     print("-c, --complete; change the sheet to use v1.9 SDK, may change the look of some items.")
0024     print("-m, --minimal; change the sheet to make it work on v1.9")
0025     print("-e, --export <file>; export characters data into file")
0026     print("-a, --addCharacter <file>; import characters data from file (erase the actual ones)")
0027 
0028 
0029 def createNewField(data):
0030     newKeys=["additionnalBottomCode","additionnalHeadCode","additionnalImport"]
0031     for newKey in newKeys:
0032         if newKey not in data:
0033             data[newKey]=""
0034     return data
0035 
0036 
0037 def uuidAndPageCount(data,key,pageCount):
0038     data['uuid']=key
0039     data['pageCount']=pageCount
0040     return data
0041 
0042 
0043 def updateImage(data):
0044     key=""
0045     pageCount=0
0046     for p in data['background']:
0047         p['filename']=""
0048         p['isBg']=True
0049         pageCount+=1
0050         if len(key) == 0:
0051             tmp=p['key']
0052             key=tmp[0:tmp.find("_")]
0053         else:
0054             tmp=p['key']
0055             p['key']="{}{}".format(key,tmp[tmp.find("_"):])
0056 
0057     return (data,key,pageCount)
0058 
0059 
0060 def qmlIncludeMinimal(data):
0061     #print("qmlIncludeMinimal")
0062     pattern="import \"qrc:/resources/qml/\""
0063     newPat="import Rolisteam 1.0"
0064     qml = data['qml']
0065     qml = qml.replace(pattern,newPat)
0066     data['qml'] = qml
0067     return data
0068 
0069 def qmlIncludeComplete(data):
0070     #print("qmlIncludeComplete")
0071     pattern="import \"qrc:/resources/qml/\""
0072     newPat="import Rolisteam 1.1"
0073     qml = data['qml']
0074     qml = qml.replace(pattern,newPat)
0075     data['qml'] = qml
0076     return data
0077 
0078 def changeDiceButton(data):
0079     #print("DiceButton")
0080     qml = data['qml']
0081     inCheck=False
0082     result=[]
0083     for line in qml.splitlines():
0084         if(line.find("DiceButton")>-1):
0085             inCheck=True
0086         if(line.find("textColor")>-1 and inCheck):
0087             line=line.replace("textColor","color")
0088         elif(line.find("color:")>-1 and inCheck):
0089             line=line.replace("color","backgroundColor")
0090             inCheck=False
0091         result.append(line)
0092 
0093     data['qml']="\n".join(result)
0094     return data
0095 
0096 
0097 def changeTextFieldField(data):
0098     #print("TextFieldField")
0099     qml = data['qml']
0100     inCheck=False
0101     result=[]
0102     for line in qml.splitlines():
0103         if(line.find("TextFieldField")>-1):
0104             inCheck=True
0105         if(line.find("textColor")>-1 and inCheck):
0106             line=line.replace("textColor","color")
0107         elif(line.find("color:")>-1 and inCheck):
0108             line=line.replace("color","backgroundColor")
0109             inCheck=False
0110         result.append(line)
0111 
0112     data['qml']="\n".join(result)
0113     return data
0114 
0115 def changeTextArea(data):
0116     #print("changeTextArea")
0117     qml = data['qml']
0118     inCheck=False
0119     result=[]
0120     for line in qml.splitlines():
0121         if(line.find("TextAreaField")>-1):
0122             inCheck=True
0123         if(line.find("textColor")>-1 and inCheck):
0124             line=line.replace("textColor","color")
0125         elif(line.find("color:")>-1 and inCheck):
0126             line=line.replace("color","backgroundColor")
0127             inCheck=False
0128         result.append(line)
0129 
0130     data['qml']="\n".join(result)
0131     return data
0132 
0133 def changeCheckbox(data):
0134     #print("changeCheckbox")
0135     qml = data['qml']
0136     inCheck=False
0137     result=[]
0138     for line in qml.splitlines():
0139         if(line.find("CheckBoxField")>-1):
0140             inCheck=True
0141         if(line.find("textColor")>-1 and inCheck):
0142             line=line.replace("textColor","borderColor")
0143         if(line.find("color:")>-1 and inCheck):
0144             inCheck=False
0145             line="\n"
0146         result.append(line)
0147 
0148     data['qml']="\n".join(result)
0149     return data
0150 
0151 def changeTextInputField(data):
0152     #print("changeTextInputField")
0153     qml = data['qml']
0154     inCheck=False
0155     result=[]
0156     for line in qml.splitlines():
0157         if(line.find("TextInputField")>-1):
0158             inCheck=True
0159         if(line.find("textColor")>-1 and inCheck):
0160             line=line.replace("textColor","color")
0161         elif(line.find("color")>-1 and inCheck):
0162             line=line.replace("color","backgroundColor")
0163             inCheck=False
0164 
0165 
0166         result.append(line)
0167 
0168 
0169     data['qml']="\n".join(result)
0170     return data
0171 
0172 def completeChange(data):
0173     array = updateImage(data)
0174     data = array[0]
0175     key = array[1]
0176     pageCount = array[2]
0177     data = uuidAndPageCount(data,key,pageCount)
0178     data = qmlIncludeComplete(data)
0179     data = changeCheckbox(data)
0180     data = changeTextInputField(data)
0181     data = changeTextArea(data)
0182     data = changeTextFieldField(data)
0183     data = changeDiceButton(data)
0184     return data
0185 
0186 
0187 def minimalChange(data):
0188     array = updateImage(data)
0189     data = array[0]
0190     key = array[1]
0191     pageCount=array[2]
0192     data = uuidAndPageCount(data,key,pageCount)
0193     data = qmlIncludeMinimal(data)
0194     return data
0195 
0196 
0197 def extractCharacterData(data, outputFile):
0198     characters = data['characters']
0199     saveFile(outputFile, characters)
0200 
0201 def loadFile(path):
0202     try:
0203         with open(path) as f:
0204             data = json.load(f)
0205             #print(data)
0206         return data
0207     except:
0208         print("Error: fail to read the input file")
0209 
0210 
0211 def saveFile(path,data):
0212     try:
0213         with open(path, 'w') as f:
0214             json.dump(data, f, indent=4)
0215     except:
0216         print("Error: fail to save data into {}").format(path)
0217 
0218 def main(argv):
0219     try:
0220         opts, args = getopt.getopt(argv,"hi:o:e:a:cm",["help","input=","output=","complete","minimal"])
0221     except getopt.GetoptError:
0222         Usage()
0223         sys.exit(2)
0224 
0225     mode=ModeEdition.NONE
0226     inputFile=""
0227     outputFile=""
0228     importFile=""
0229     for key, value in opts:
0230         if key in ('-h',"--help"):
0231             Usage()
0232             sys.exit(0)
0233         elif key in ("-i","--input"):
0234             inputFile=value
0235         elif key in ("-o","--output"):
0236             outputFile=value
0237         elif key in ("-c", "--complete"):
0238             mode=ModeEdition.COMPLETE
0239         elif key in ("-m","--minimal"):
0240             mode=ModeEdition.MINIMAL
0241         elif key in ("-a","--add"):
0242             importFile = value
0243             mode=ModeEdition.IMPORT
0244         elif key in ("-e","--export"):
0245             importFile = value
0246             mode=ModeEdition.EXPORT
0247 
0248     if mode == ModeEdition.NONE:
0249         print("Error: no edition mode: minimal or complete")
0250         Usage()
0251         sys.exit(1)
0252 
0253     if os.path.exists(outputFile):
0254         print("Error: output file already exists")
0255         Usage()
0256         sys.exit(1)
0257 
0258     data=loadFile(inputFile)
0259 
0260     if mode == ModeEdition.MINIMAL:
0261         data = minimalChange(data)
0262     elif mode == ModeEdition.COMPLETE:
0263         data = completeChange(data)
0264     elif mode == ModeEdition.EXPORT:
0265         extractCharacterData(data, importFile)
0266         return
0267     elif mode == ModeEdition.IMPORT:
0268         character=loadFile(inputFile)
0269         data['characters']=character
0270 
0271     saveFile(outputFile, data)
0272 
0273 
0274 
0275 
0276     #i=1
0277     #for p in data['data']['items']:
0278     #    print(p['label'])
0279     #    print(str(i))
0280     #    p['id']='id_'+str(i)
0281     #    i+=1
0282 
0283 
0284 
0285 
0286 if __name__ == '__main__':
0287     main(sys.argv[1:])