7163D

c3mm.py

Oct 5th, 2016
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Cossacks 3 Mod Manager version 1.3.6
  2. # Require python 3.5.x to work : https://python.org/downloads
  3. # Get all update at http://www.cossacks3.com/forum/index.php?threads/cossacks-3-text-mod-manager.17753
  4.  
  5. """
  6. # === News in 1.3.6 === #
  7.     >Correction of several critical bugs (+10)
  8.     >Add a new command "info"
  9.  
  10. """
  11.  
  12.  
  13. """
  14. Structur uses
  15. Mod  := {'name':name, 'version':version, 'info':info, 'author':author, file':{name => list of (str old, str new)}, 'otherfile':{name => new}}
  16. Data := {'mod':{name => mod mod}, 'file':{name => list of str mod}}
  17. """
  18.  
  19. import json, sys, os, time
  20. from random import randrange
  21.  
  22. CONFIG_PATH = "c3mm/"
  23. MOD_PATH    = "mod/"
  24. COPY_PATH   = "c3mm/copy/"
  25. BCKUP_PATH  = "c3mm/bckup/"
  26. INFO_PATH   = "c3mm/c3mm.json"
  27. LOG_PATH    = "c3mm/log.txt"
  28. HELP = """
  29. Cossacks3 (text) Mod Manager Version 1.3.6.
  30. Type python c3mm.py action [parameter]
  31.     action:
  32.         install modname   -> intall a mod find by name
  33.         uninstall modname -> unintall a mod find by name
  34.         help              -> show this message
  35.         uninstall_all     -> uninstall all mod
  36.         giveall           -> print all installed mods
  37.         valid_mod modname -> check if mod modname is valid
  38. """
  39.  
  40. PATH = [CONFIG_PATH, MOD_PATH, COPY_PATH, BCKUP_PATH]
  41. FILE = [INFO_PATH, LOG_PATH]
  42. for path in PATH:
  43.     try:
  44.         os.mkdir(path)
  45.     except:
  46.         pass
  47. for file in FILE:
  48.     try:
  49.         open(file, 'r')
  50.     except:
  51.         open(file, 'w')
  52.  
  53. class Alert:
  54.     linfo = []
  55.     lerror = []
  56.     show = True
  57.  
  58.     def info(i):
  59.         if Alert.show:
  60.             Alert.linfo.append(i)
  61.         print(i)
  62.  
  63.     def error(e, data):
  64.         Alert.lerror.append(e)
  65.         if Alert.show:
  66.             print(e)
  67.         quit(data)
  68.  
  69.     def input(m):
  70.         return input(m)
  71.  
  72.     def list(l):
  73.         for i in l:
  74.             if Alert.show:
  75.                 print(i)
  76.  
  77. def loadjson(path):
  78.     return json.loads(open(path, 'r').read())
  79.  
  80. def savejson(path, data):
  81.     open(path, "w").write(json.dumps(data))
  82.  
  83. def loadjsonmod(path):
  84.     try:
  85.         mod = loadjson(path)
  86.     except:
  87.         Alert.info("Impossible to open " + path)
  88.         return False
  89.        
  90.     if not "otherfile" in mod:
  91.         mod["otherfile"] = {}
  92.     if not "addfile" in mod:
  93.         mod["addfile"] = {}
  94.     if not "file" in mod:
  95.         mod["file"] = mod["modify_file"]
  96.     if not "otherfile" in mod:
  97.         mod["otherfile"] = mod["replace_file"]
  98.     if not "addfile" in mod:
  99.         mod["addfile"] = mod["add_file"]
  100.     if not "remove_file" in mod:
  101.         mod["remove_file"] = {}
  102.     if not validMod(mod):
  103.         return False
  104.     return mod
  105.  
  106. def mkPath(path):
  107.     rpath = ""
  108.     for dir in path.split('/')[:-1]:
  109.         rpath += dir
  110.         try:
  111.             os.mkdir(rpath)
  112.         except:
  113.             pass
  114.         rpath += '/'
  115.  
  116. def copyFile(file, path):
  117.     mkPath(path)
  118.     open(path, "w").write(open(file, 'r').read())
  119.  
  120. def copyBFile(file, path):
  121.     mkPath(path)
  122.     open(path, "wb").write(open(file, 'rb').read())
  123.  
  124. def checkMod(mod, data):
  125.     """
  126.     checkMod function, use to check if a mod is valid
  127.     Mod mod     -> mod to check
  128.     Bool return -> is the mod valid?
  129.     """
  130.     for file in mod['file']:
  131.         Alert.info("Check existence of '" + file + "'")
  132.         try:
  133.             f = open(file, 'r').read()
  134.             Alert.info("'" + file + "' exist.")
  135.         except:
  136.             Alert.info("'" + file + "' don't exist.")
  137.             return False
  138.  
  139.         for old in mod['file'][file]:
  140.             Alert.info("Check exsitence of '" + old + "' in '" + file + "'")
  141.             if not old in f:
  142.                 Alert.info("Not found.")
  143.                 return False
  144.             else:
  145.                 Alert.info("Found.")
  146.  
  147.     for file in mod["otherfile"]:
  148.         Alert.info("Check existence of '" + file + "'")
  149.         try:
  150.             open(file, 'r')
  151.             Alert.info("'" + file + "' exist.")
  152.         except:
  153.             Alert.info("'" + file + "' don't exist.")
  154.             return False
  155.  
  156.         Alert.info("Check existence of '" + MOD_PATH + mod["otherfile"][file] + "'")
  157.         try:
  158.             open(MOD_PATH + mod["otherfile"][file], 'r')
  159.             Alert.info("'" + MOD_PATH + mod["otherfile"][file] + "' exist.")
  160.         except:
  161.             Alert.info("'" + MOD_PATH + mod["otherfile"][file] + "' don't exist.")
  162.             return False
  163.  
  164.         if file in data["otherfile"]:
  165.             Alert.info(file + " is already modified by " + data["otherfile"][file][0])
  166.             Alert.info("You have to uninstall " + data["otherfile"][file][0] + " to install " + mod['name'])
  167.             return False
  168.  
  169.     for file in mod["addfile"]:
  170.         Alert.info("Check existence of '" + MOD_PATH + mod["addfile"][file] + "'")
  171.         try:
  172.             open(MOD_PATH + mod["addfile"][file], 'r')
  173.             Alert.info("'" + MOD_PATH + mod["addfile"][file] + "' exist.")
  174.         except:
  175.             Alert.info("'" + MOD_PATH + mod["addfile"][file] + "' don't exist.")
  176.             return False
  177.  
  178.         if file in data["addfile"]:
  179.             Alert.info(file + " is already added by " + data["addfile"][file][0])
  180.             Alert.info("You have to uninstall " + data["addfile"][file][0] + " to install " + mod['name'])
  181.             return False
  182.  
  183.     return True
  184.  
  185. def validMod(mod):
  186.     """
  187.     validMod function, assert not error occured when install mod
  188.     Mod mod -> mod to check
  189.     Bool return
  190.     """
  191.     for field in ['name', 'info', 'file', 'version', 'author']:
  192.         if not field in mod:
  193.             Alert.info("Missing field '" + field + "'")
  194.             return False
  195.  
  196.     if not type(mod['file']) == type({}):
  197.         Alert.info("Wrong data mod")
  198.         return False
  199.  
  200.     for file in mod['file']:
  201.         if not type(mod['file'][file]) == type({}):
  202.             Alert.info("Wrong data mod")
  203.             return False
  204.         try:
  205.             for old in mod['file'][file]:
  206.                 new = mod['file'][file][old]
  207.                 if not type(old) == type("") or not type(new) == type(""):
  208.                     Alert.info("Wrong data mod")
  209.                     return False
  210.         except:
  211.             Alert.info("Wrong data mod")
  212.             return False
  213.  
  214.     if not type(mod['otherfile']) == type({}):
  215.         Alert.info("Wrong data mod")
  216.         return False
  217.  
  218.     if not type(mod['addfile']) == type({}):
  219.         Alert.info("Wrong data mod")
  220.         return False
  221.  
  222.  
  223.     return True
  224.  
  225.  
  226. def apply(mod):
  227.     """
  228.     apply function, use to modify files
  229.     Mod mod -> mod that contain all modifications
  230.     None return
  231.     """
  232.     mfile = {}
  233.     for file in mod['file']:
  234.         Alert.info("Calculate'" + file + '"')
  235.         f = open(file, 'r').read()
  236.  
  237.         #Calculate modifications
  238.         for old in mod['file'][file]:
  239.             new = mod['file'][file][old]
  240.             f = f.replace(old, new)
  241.  
  242.         mfile[file] = f
  243.    
  244.     #Apply mod
  245.     for file in mfile:
  246.         Alert.info("Patching '" + file + '"')
  247.         open(file, 'w').write(mfile[file])
  248.  
  249.     for file in mod["otherfile"]:
  250.         Alert.info("Replace '" + file+ "'")
  251.         copyBFile(MOD_PATH + mod["otherfile"][file], file)
  252.  
  253.     for file in mod["addfile"]:
  254.         Alert.info("Add '" + file + "'")
  255.         copyBFile(MOD_PATH + mod["addfile"][file], file)
  256.  
  257.     for file in mod["remove_file"]:
  258.         Alert.info("Remove'" + file + "'")
  259.         os.remove(file)
  260.  
  261. def install(mod, data):
  262.     """
  263.     install function, use to add a mod.
  264.     Mod mod   -> mod to install
  265.     Data data -> c3mm main data
  266.     None return
  267.     """
  268.     if not checkMod(mod, data):
  269.         Alert.error("'" + mod['name'] + "' mod version " + mod['version'] + " isn't valid", data)
  270.  
  271.     Alert.info("Install '" + mod['name'] + "' version " + mod['version'])
  272.  
  273.     #Add some "signature"
  274.     for file in mod['file']:
  275.         for old in mod['file'][file]:
  276.             mod['file'][file][old] = mod['file'][file][old] + "//" + str(randrange(100000, 999999))
  277.  
  278.     #Add mod to list of installed mod
  279.     data['mod'][mod['name']] = mod
  280.  
  281.     #Adding mod in list of file modification
  282.     for file in mod['file']:
  283.         if not file in data['file']:
  284.             data['file'][file] = [mod['name']]
  285.             #backup file
  286.             copyFile(file, BCKUP_PATH + file)
  287.         else:
  288.             data['file'][file].append(mod['name'])
  289.  
  290.     for file in mod["otherfile"]:
  291.         if not file in data['otherfile']:
  292.             data['otherfile'][file] = [mod['name']]
  293.             #backup file
  294.             copyBFile(file, BCKUP_PATH + file)
  295.         else:
  296.             data['otherfile'][file].append(mod['name'])
  297.  
  298.     for file in mod["addfile"]:
  299.         if not file in data['otherfile']:
  300.             data['addfile'][file] = [mod['name']]
  301.         else:
  302.             data['addfile'][file].append(mod['name'])
  303.  
  304.     for file in mod["remove_file"]:
  305.         copyBFile(file, BCKUP_PATH + file)
  306.         data["remove_file"][file] = [mod['name']]
  307.  
  308.     apply(mod)
  309.  
  310.     for file in mod['file']:
  311.         #make a save of the file
  312.         copyFile(file, COPY_PATH + file)
  313.  
  314. def uninstall(mod, data):
  315.     """
  316.     uninstall function, use to delete a mod.
  317.     Mod mod   -> mod to uninstall
  318.     Data data -> c3mm main data
  319.     None return
  320.     """
  321.     uninstallmod = mkuninstallmod(mod)
  322.  
  323.     if not checkMod(uninstallmod, data):
  324.         Alert.info("'" + mod['name'] + "' mod uninstaller isn't valid.")
  325.         Alert.info("Do you want restore all files concerned by this mod?")
  326.         Alert.info("List of mod will uninstalled in this case:")
  327.         modlist = []
  328.         for file in mod['file']:
  329.             for mod in data['file'][file]:
  330.                 if not mod in modlist:
  331.                     modlist.append(mod)
  332.                     Alert.info(mod)
  333.         choice = Alert.input("Restore all file? yes/no:")
  334.         if choice == 'no':
  335.             quit(data)
  336.  
  337.         filelist = [file for file in mod['file']]
  338.         for file in filelist:
  339.             Alert("Restore '" + file + "'")
  340.             copyFile(BCKUP_PATH + file, file)
  341.             for mod in modlist:
  342.                 if file in mod["file"]:
  343.                     del mod["file"][file]
  344.                 if file in mod["otherfile"]:
  345.                     del mod["otherfile"][file]
  346.                 if file in mod["addfile"]:
  347.                     del mod["addfile"][file]
  348.                 if file in mod["remove_file"]:
  349.                     mod["remove_file"].remove("file")
  350.  
  351.         for mod in modlist:
  352.             uninstall(mod, data)
  353.     #Delete mod to list of installed mod
  354.     del data['mod'][mod['name']]
  355.  
  356.     #Delete mod of list of file modification
  357.     for file in data['file']:
  358.         if mod['name'] in data['file'][file]:
  359.             data['file'][file].remove(mod['name'])
  360.  
  361.     Alert.info("Uninstall '" + mod['name'] + "' version " + mod['version'])
  362.  
  363.     for file in mod["otherfile"]:
  364.         Alert.info("Restore " + file)
  365.         copyBFile(BCKUP_PATH + file, file)
  366.         del data["otherfile"][file]
  367.  
  368.     for file in mod["addfile"]:
  369.         Alert.info("Delete " + file)
  370.         os.remove(file)
  371.         del data["addfile"][file]
  372.  
  373.     for file in mod["remove_file"]:
  374.         Alert.info("Restore " + file)
  375.         copyBFile(BCKUP_PATH + file, file)
  376.         del data["remove_file"][file]
  377.  
  378.     apply(uninstallmod)
  379.  
  380. def mkuninstallmod(mod):
  381.     """
  382.     mkuninstallmod function, use to make an uninstalator from a mod
  383.     Mod mod    -> mod to uninstall
  384.     Mod return -> uninstalator mod
  385.     """
  386.     uninstallmod = {}
  387.     uninstallmod['file'] = {}
  388.  
  389.     for file in mod['file']:
  390.         uninstallmod['file'][file] = {}
  391.         for old in mod['file'][file]:
  392.             new = mod['file'][file][old]
  393.             #For each modification of mod, inverse new and old
  394.             uninstallmod['file'][file][new] = old
  395.  
  396.     uninstallmod["otherfile"] = {}
  397.     uninstallmod["addfile"] = {}
  398.  
  399.     return uninstallmod
  400.  
  401. def checkStatu(data):
  402.     """
  403.     checkStatu funtion, check if a modded file has been modified since last launch of this program
  404.     Data data   -> main data
  405.     Bool return ->
  406.     """
  407.     badfile = []
  408.     #Check all modded file
  409.     for file in data['file']:
  410.         if open(file, 'r').read() != open(COPY_PATH + file, 'r').read():
  411.             Alert.info("File '" + file + "' has been modified since last launch of c3mm.")
  412.             badfile.append(file)
  413.  
  414.     badotherfile = []
  415.     for file in data["otherfile"]:
  416.         if open(file, 'rb').read() != open(BCKUP_PATH + file, 'rb').read():
  417.             Alert.info("File '" + file + "' has been modified since last launch of c3mm.")
  418.             badotherfile.append(file)
  419.  
  420.     badmod = []
  421.     #Uninstall all mod concerned by these files
  422.     for file in badfile:
  423.         for mod in data['file'][file]:
  424.             if not mod in badmod and not checkMod(mkuninstallmod(data['mod'][mod]), data):
  425.                 badmod.append(mod)
  426.     for file in badotherfile:
  427.         if not data["otherfile"][file][0] in badmod:
  428.             badmod.append(data["otherfile"][file][0])
  429.  
  430.     for mod in badmod:
  431.         _mod = data['mod'][mod]
  432.         for file in badfile:
  433.             if file in _mod['file']:
  434.                 del _mod['file'][file]
  435.         uninstall(_mod, data)
  436.  
  437.     #make a new copy of all modified files
  438.     for file in badfile:
  439.         copyFile(file, COPY_PATH + file)
  440.  
  441.     return len(badmod) == 0
  442.  
  443. def quit(data):
  444.     """
  445.     Save all data before exit the program
  446.     """
  447.     dfile = []
  448.     for file in data['file']:
  449.         if data['file'][file] == []:
  450.             dfile.append(file)
  451.  
  452.     for file in dfile:
  453.         del data['file'][file]
  454.  
  455.     savejson(INFO_PATH, data)
  456.  
  457.     logs = time.strftime('%D %H:%M\n',time.localtime())
  458.     for info in Alert.linfo:
  459.         logs += info + '\n'
  460.     for error in Alert.lerror:
  461.         logs += error + '\n'
  462.  
  463.     open(LOG_PATH, "a").write(logs)
  464.     exit()
  465.  
  466. def main(argc, argv):
  467.  
  468.     try:
  469.         data = loadjson(INFO_PATH)
  470.     except:
  471.         open(INFO_PATH, "w").write('{"mod":{}, "file":{}}')
  472.         data = loadjson(INFO_PATH)
  473.  
  474.     if not "otherfile" in data:
  475.         data["otherfile"] = {}
  476.  
  477.     if not "addfile" in data:
  478.         data["addfile"] = {}
  479.  
  480.     if not "remove_file" in data:
  481.         data["remove_file"] = {}
  482.  
  483.     if not checkStatu(data):
  484.         quit(data)
  485.  
  486.     elif argc == 1 or argv[1] == "help":
  487.         print(HELP)
  488.  
  489.     elif argv[1] == 'install':
  490.  
  491.         if argc < 3:
  492.             Alert.error("You must give a mod name", data)
  493.  
  494.         mod = loadjsonmod(MOD_PATH + argv[2] + '.json')
  495.         if not mod:
  496.             quit(data)
  497.         modname = mod['name']
  498.  
  499.         if modname in data['mod']:
  500.             if mod["version"] == data["mod"][modname]["version"]:
  501.                 Alert.error("Mod '" + modname + "' version " + mod["version"] + " is already install.", data)
  502.             else:
  503.                 Alert.info("Mod '" + modname + "' version " + data["mod"][modname]["version"] + " is already install. Do you want change it for version " + mod["version"] + "?")
  504.                 choice = Alert.input("yes/no:")
  505.                 if choice == "no":
  506.                     quit()
  507.  
  508.                 uninstall(data['mod'][modname], data)
  509.  
  510.         install(mod, data)
  511.  
  512.     elif argv[1] == 'uninstall':
  513.  
  514.         if argc < 3:
  515.             Alert.error("You must give a mod name", data)
  516.  
  517.         mod = loadjsonmod(MOD_PATH + argv[2] + '.json')
  518.         if not mod:
  519.             quit(data)
  520.         if not mod in data['mod']:
  521.             Alert.error(mod + " isn't installed!")
  522.         modname = mod['name']
  523.         uninstall(data['mod'][modname], data)
  524.  
  525.     elif argv[1] == 'uninstall_all':
  526.  
  527.         Alert.info("Restore all files")
  528.         for file in data['file']:
  529.             Alert.info("Restore '" + file + "'")
  530.             copyFile(BCKUP_PATH + file, file)
  531.         for file in data['otherfile']:
  532.             Alert.info("Restore '" + file + "'")
  533.             copyFile(BCKUP_PATH + file, file)
  534.         for file in data['remove_file']:
  535.             Alert.info("Restore '" + file + "'")
  536.             copyBFile(BCKUP_PATH + file, file)
  537.         for file in data['addfile']:
  538.             Alert.info("Remove '" + file + "'")
  539.             os.remove(file)
  540.  
  541.         data['file'] = {}
  542.         data["otherfile"] = {}
  543.         data["addfile"] = {}
  544.         data["remove_file"] = {}
  545.         data['mod'] = {}
  546.  
  547.     elif argv[1] == 'giveall':
  548.         Alert.list([mod + " version " + data['mod'][mod]['version'] + " by " + data['mod'][mod]["author"] for mod in data['mod']])
  549.  
  550.     elif argv[1] == 'valid_mod':
  551.         if argc < 3:
  552.             Alert.error("You must give a mod name", data)
  553.  
  554.         mod = loadjsonmod(MOD_PATH + argv[2] + '.json')
  555.         if mod and checkMod(mod, data):
  556.             Alert.info("Mod is valid")
  557.         else:
  558.             Alert.info("Mod isn't valid!")
  559.  
  560.     elif argv[1] == "info":
  561.         if argc < 3:
  562.             Alert.error("You must give a mod name", data)
  563.  
  564.         Alert.show = False
  565.         mod = loadjsonmod(MOD_PATH + argv[2] + '.json')
  566.         Alert.show = True
  567.  
  568.         if not mod:
  569.             Alert.info("Impossible to load mod.")
  570.         else:
  571.             Alert.info(mod["name"] + " version " + mod["version"] + " by " + mod["author"])
  572.             Alert.list(["\t" + line for line in mod["info"].split("\n")])
  573.         Alert.info()
  574.  
  575.     quit(data)
  576.  
  577. if __name__ == '__main__':
  578.     main(len(sys.argv), sys.argv)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×