import c4d, hashlib, socket, uuid, math, json, re
from c4d import gui
from uuid import getnode as get_mac

version = c4d.GetC4DVersion()

if version < 20100:
    sn1 = c4d.GeGetSerialInfo(c4d.SERIALINFO_CINEMA4D)
    sn2 = sn1['nr']
    siNr = sn2[-5:]
    partOne = sn2[2:4]
    partTwo = sn2[-2:]
    #IKMAX C4D v1.0
    ikC4DOld = "IKMX" + partOne + "CINE" + partTwo + "MAFD"
    #C4DToMax v1.4
    digAuth = hashlib.sha224(siNr+"53d93f3eab2ec11e3358918a9484b9df0bc31456b7246b35b525189b").hexdigest()
    c4dToMax = hashlib.sha224(digAuth+"40f6f7a19fbbb705173f3d3e3811bec3b7b781394aa61073d23162a3").hexdigest()
    #Max2C4D v1.0 - v1.5
    digAuth2 = hashlib.sha224(siNr+"8a3e7ffb99de159312fa690c988de40e45ade78a57b61a992dad97db").hexdigest()
    max2c4dOne = hashlib.sha224(digAuth2+"93664e868fa9cb1a5cd1ee6716507ed04e7b0967fde44d0827cffb3c").hexdigest()
    #Max2C4D v2.0
    digAuth3 = hashlib.sha224(siNr+"3f3b20af6213ffe2f705f1c95ad28ff70421a989673b48b7f76c0680").hexdigest()
    max2c4dTwo = hashlib.sha224(digAuth3+"8bd66dfe51e5fb6f70e213591ab25c97d7b6b482ae5ac8e539e8c4f6").hexdigest()
    #Max2C4D v3.01 - v4.0
    digAuth4 = hashlib.sha224(siNr+"891df377116ee3bb6dae89d51d7a432729d6ca2e2c50d967af906fbd").hexdigest()
    max2c4dThree = hashlib.sha224(digAuth4+"c1a3ccbc85a37d4c01e89f24245176e75cd0b5efc8d6e2d3219f84e5").hexdigest()
    #Max2C4D v4.01
    max2c4dFour = "MXTO" + partOne + "CINE" + partTwo + "MAFD"
    #Max2C4D v4.5
    mx2C4DFortyFive = "MXTO" + partOne + "CINE" + partTwo + "MAFD" + "15X"
else:
    pass

if version > 20100:
    data = json.loads(c4d.ExportLicenses())
    sysID = data["systemid"]
    c4dserial = sysID[0:11]
    serialPart1 = c4dserial[2:4]
    serialPart2 = c4dserial[-2:]
    #Max2C4D v4.5 R21
    mx2C4DFortyFive = "MXTO" + serialPart1 + "CINE" + serialPart2 + "MAFD" + "15X"
    
pc = socket.gethostname()
ip = socket.gethostbyname(socket.gethostname())

mac = get_mac()
last3 = str(mac)[-3:]
#IKMAXC4D v1.5 - v1.6
ikC4DSrl = 'I' + last3 + '15X' + last3 + 'IND'
#MaxToC4D v4.7 - v4.8
mx2C4DSrl = 'I' + last3 + '47X' + last3 + 'IND'

LBL_INFO0 = 1001
LBL_INFO1 = 1002
LBL_INFO2 = 1003
LBL_INFO3 = 1004
LBL_INFO4 = 1005
LBL_INFO5 = 1006
LBL_INFO6 = 1007
LBL_INFO7 = 1008
LBL_INFO8 = 1009
AUTH_CODE0 = 1010
AUTH_CODE1 = 1011
AUTH_CODE2 = 1012
AUTH_CODE3 = 1013
AUTH_CODE4 = 1014
AUTH_CODE5 = 1015
AUTH_CODE6 = 1016
AUTH_CODE7 = 1017
AUTH_CODE8 = 1018
BTN_GEN = 1019
BTN_CLOSE = 1020
BTN_COPY_ALL = 1021
BTN_COPY0 = 1022
BTN_COPY1 = 1023
BTN_COPY2 = 1024
BTN_COPY3 = 1025
BTN_COPY4 = 1026
BTN_COPY5 = 1027
BTN_COPY6 = 1028
BTN_COPY7 = 1029
BTN_COPY8 = 1030
BTN_HELP = 1031
GROUP_OPTIONS = 1032
COMP_INFO = 1033
COMP_INFO_TITLE = 1034

class Keygen(c4d.gui.GeDialog):

    def CreateLayout(self):
        self.SetDefaultColor(0, c4d.COLOR_BG, c4d.Vector(.4,.5,.6))
        self.SetTitle("3DtoAll C4D Master KG v1.01")
        self.GroupBegin(GROUP_OPTIONS, c4d.BFH_CENTER, 3, 1)
        self.AddStaticText(LBL_INFO0, c4d.BFH_LEFT, name='IKMAX C4D v1.00')
        self.SetDefaultColor(LBL_INFO0, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddEditText(AUTH_CODE0, c4d.BFH_SCALEFIT)
        self.SetString(AUTH_CODE0, 'Paste this serial into the auth box of IKMAXC4D')
        self.AddButton(BTN_COPY0, c4d.BFH_RIGHT, name='Copy')
        self.AddStaticText(LBL_INFO1, c4d.BFH_LEFT, name='IKMAX C4D v1.50')
        self.SetDefaultColor(LBL_INFO1, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddEditText(AUTH_CODE1, c4d.BFH_SCALEFIT)
        self.SetString(AUTH_CODE1, 'Paste this serial into the auth box of IKMAXC4D')
        self.AddButton(BTN_COPY1, c4d.BFH_RIGHT, name='Copy')
        self.AddStaticText(LBL_INFO5, c4d.BFH_LEFT, name='Max To C4D v1.00')
        self.SetDefaultColor(LBL_INFO5, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddEditText(AUTH_CODE5, c4d.BFH_SCALEFIT)
        self.SetString(AUTH_CODE5, 'Paste this serial into the auth box of MAXToC4D')
        self.AddButton(BTN_COPY5, c4d.BFH_RIGHT, name='Copy')
        self.AddStaticText(LBL_INFO6, c4d.BFH_LEFT, name='Max To C4D v2.00')
        self.SetDefaultColor(LBL_INFO6, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddEditText(AUTH_CODE6, c4d.BFH_SCALEFIT)
        self.SetString(AUTH_CODE6, 'Paste this serial into the auth box of MAXToC4D')
        self.AddButton(BTN_COPY6, c4d.BFH_RIGHT, name='Copy')
        self.AddStaticText(LBL_INFO7, c4d.BFH_LEFT, name='Max To C4D v3.01')
        self.SetDefaultColor(LBL_INFO7, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddEditText(AUTH_CODE7, c4d.BFH_SCALEFIT)
        self.SetString(AUTH_CODE7, 'Paste this serial into the auth box of MAXToC4D')
        self.AddButton(BTN_COPY7, c4d.BFH_RIGHT, name='Copy')
        self.AddStaticText(LBL_INFO8, c4d.BFH_LEFT, name='Max To C4D v4.01')
        self.SetDefaultColor(LBL_INFO8, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddEditText(AUTH_CODE8, c4d.BFH_SCALEFIT)
        self.SetString(AUTH_CODE8, 'Paste this serial into the auth box of MAXToC4D')
        self.AddButton(BTN_COPY8, c4d.BFH_RIGHT, name='Copy')
        self.AddStaticText(LBL_INFO2, c4d.BFH_LEFT, name='Max To C4D v4.50')
        self.SetDefaultColor(LBL_INFO2, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddEditText(AUTH_CODE2, c4d.BFH_SCALEFIT)
        self.SetString(AUTH_CODE2, 'Paste this serial into the auth box of MAXToC4D')
        self.AddButton(BTN_COPY2, c4d.BFH_RIGHT, name='Copy')
        self.AddStaticText(LBL_INFO3, c4d.BFH_LEFT, name='Max To C4D v4.7+')
        self.SetDefaultColor(LBL_INFO3, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddEditText(AUTH_CODE3, c4d.BFH_SCALEFIT)
        self.SetString(AUTH_CODE3, 'Paste this serial into the auth box of MAXToC4D')
        self.AddButton(BTN_COPY3, c4d.BFH_RIGHT, name='Copy')
        self.AddStaticText(LBL_INFO4, c4d.BFH_LEFT, name='C4D To Max v1.40')
        self.SetDefaultColor(LBL_INFO4, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddEditText(AUTH_CODE4, c4d.BFH_SCALEFIT)
        self.SetString(AUTH_CODE4, 'Paste this serial into the auth box of C4DToMAX')
        self.AddButton(BTN_COPY4, c4d.BFH_RIGHT, name='Copy')
        self.GroupEnd()
        self.GroupBegin(GROUP_OPTIONS, c4d.BFH_CENTER, 4, 1)
        self.AddButton(BTN_HELP, c4d.BFH_SCALE, name='Help')
        self.AddButton(BTN_GEN, c4d.BFH_SCALE, name='Generate')
        self.AddButton(BTN_COPY_ALL, c4d.BFH_SCALE, name='Copy All Serials')
        self.AddButton(BTN_CLOSE, c4d.BFH_SCALE, name='Close')
        self.GroupEnd()
        self.AddStaticText(COMP_INFO_TITLE, c4d.BFH_CENTER, name='Computer Information')
        self.SetDefaultColor(COMP_INFO_TITLE, c4d.COLOR_TEXT, c4d.Vector(0,255,255))
        self.AddMultiLineEditText(COMP_INFO, c4d.BFH_CENTER, 470, 51, c4d.DR_MULTILINE_READONLY)
        self.SetString(COMP_INFO,("PC-Name: " + str(pc) + "\nMAC Address: " + (':'.join(re.findall('..', '%012x' % uuid.getnode()))) + "\n48-bit MAC Address: " + str(mac) + "\nLocal IP Address: " + str(ip)))
        return True

    def Command(self, id, bc):
        if id == BTN_HELP:
            c4d.gui.MessageDialog("1.) Click Generate.\n2.) Press Copy or Copy All Serials button.\n3.) Paste copied serial into the authorization field of the plugin.\n4.) Buy it if you make money off of it!")
        if id == BTN_GEN:
            self.SetString(AUTH_CODE1, ikC4DSrl)
            self.SetDefaultColor(AUTH_CODE1, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
            self.SetString(AUTH_CODE2, mx2C4DFortyFive)
            self.SetDefaultColor(AUTH_CODE2, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
            self.SetString(AUTH_CODE3, mx2C4DSrl)
            self.SetDefaultColor(AUTH_CODE3, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
            if version > 20100:
                self.SetString(AUTH_CODE0, 'Wrong version of Cinema 4D! -->(R20 or Lower)')
                self.SetDefaultColor(AUTH_CODE0, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,255,0))
                self.SetString(AUTH_CODE4, 'Wrong version of Cinema 4D! -->(R20 or Lower)')
                self.SetDefaultColor(AUTH_CODE4, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,255,0))
                self.SetString(AUTH_CODE5, 'Wrong version of Cinema 4D! -->(R20 or Lower)')
                self.SetDefaultColor(AUTH_CODE5, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,255,0))
                self.SetString(AUTH_CODE6, 'Wrong version of Cinema 4D! -->(R20 or Lower)')
                self.SetDefaultColor(AUTH_CODE6, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,255,0))
                self.SetString(AUTH_CODE7, 'Wrong version of Cinema 4D! -->(R20 or Lower)')
                self.SetDefaultColor(AUTH_CODE7, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,255,0))
                self.SetString(AUTH_CODE8, 'Wrong version of Cinema 4D! -->(R20 or Lower)')
                self.SetDefaultColor(AUTH_CODE8, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,255,0))
            else:
                self.SetString(AUTH_CODE0, ikC4DOld)
                self.SetDefaultColor(AUTH_CODE0, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
                self.SetString(AUTH_CODE4, mx2C4DFortyFive)
                self.SetDefaultColor(AUTH_CODE4, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
                self.SetString(AUTH_CODE4, c4dToMax)
                self.SetDefaultColor(AUTH_CODE4, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
                self.SetString(AUTH_CODE5, max2c4dOne)
                self.SetDefaultColor(AUTH_CODE5, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
                self.SetString(AUTH_CODE6, max2c4dTwo)
                self.SetDefaultColor(AUTH_CODE6, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
                self.SetString(AUTH_CODE7, max2c4dThree)
                self.SetDefaultColor(AUTH_CODE7, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
                self.SetString(AUTH_CODE8, max2c4dFour)
                self.SetDefaultColor(AUTH_CODE8, c4d.COLOR_TEXT_EDIT, c4d.Vector(255,0,255))
        if id == BTN_COPY_ALL:
            if version > 20100:
                c4d.CopyStringToClipboard("IKMAXC4D 1.50 Serial: " + ikC4DSrl + "\nMAXTOC4D 4.50 Serial: " + mx2C4DFortyFive + "\nMAXTOC4D 4.7+ Serial: " + mx2C4DSrl)
            else:
                c4d.CopyStringToClipboard("IKMAXC4D 1.00 Serial: " + ikC4DOld + "\nIKMAXC4D 1.50 Serial: " + ikC4DSrl + "\nMAXTOC4D 1.00 Serial: " + max2c4dOne + "\nMAXTOC4D 2.00 Serial: " + max2c4dTwo + "\nMAXTOC4D 3.01 Serial: " + max2c4dThree + "\nMAXTOC4D 4.01 Serial: " + max2c4dFour + "\nMAXTOC4D 4.50 Serial: " + mx2C4DFortyFive + "\nMAXTOC4D 4.7+ Serial: " + mx2C4DSrl + "\nC4DTOMAX 1.40 Serial: " + c4dToMax)
        if id == BTN_COPY0:
            if version > 20100:
                pass
            else:
                c4d.CopyStringToClipboard(ikC4DOld)
        if id == BTN_COPY1:
            c4d.CopyStringToClipboard(ikC4DSrl)
        if id == BTN_COPY2:
            c4d.CopyStringToClipboard(mx2C4DFortyFive)
        if id == BTN_COPY3:
            c4d.CopyStringToClipboard(mx2C4DSrl)
        if id == BTN_COPY5:
            if version > 20100:
                pass
            else:
                c4d.CopyStringToClipboard(max2c4dOne)
        if id == BTN_COPY6:
            if version > 20100:
                pass
            else:
                c4d.CopyStringToClipboard(max2c4dTwo)
        if id == BTN_COPY7:
            if version > 20100:
                pass
            else:
                c4d.CopyStringToClipboard(max2c4dThree)
        if id == BTN_COPY8:
            if version > 20100:
                pass
            else:
                c4d.CopyStringToClipboard(max2c4dFour)
        if id == BTN_COPY4:
            if version > 20100:
                pass
            else:
                c4d.CopyStringToClipboard(c4dToMax)
        if id == BTN_CLOSE:
            self.Close()
        return True

def main():
    dlg = Keygen()
    dlg.Open(c4d.DLG_TYPE_MODAL, defaultw=405, defaulth=120)

if __name__ == "__main__":
    main()