MaKiPL Nov 14th, 2018 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import struct
  4. PaletteStartOriginal = 0x33;
  5. PaletteStartReplace = 0x60 #the first palette to inject into first 15 colours
  6. PaletteShift = 48 #0x90 - 0x60
  7. ColoursToProcess = 13 #because (0x2D0 - 0x60)/PaletteShift
  9. def replaceBytes(FirstAddress, SecondAddresss):
  10.     SecondaryWriteBuffer[FirstAddress] = OriginalBuffer[SecondAddresss] #I'm doing this extremely easy way just to show step-by-step-by
  11.     SecondaryWriteBuffer[FirstAddress+1] = OriginalBuffer[SecondAddresss+1]
  12.     SecondaryWriteBuffer[FirstAddress+2] = OriginalBuffer[SecondAddresss+2]
  13.     #we just copied OriginalBuffer at address two (i.e. 0x90) to writeBuffer at location of first address. Now we rotate the buffers and do the opposite
  14.     SecondaryWriteBuffer[SecondAddresss] = OriginalBuffer[FirstAddress]
  15.     SecondaryWriteBuffer[SecondAddresss+1] = OriginalBuffer[FirstAddress+1]
  16.     SecondaryWriteBuffer[SecondAddresss+2] = OriginalBuffer[FirstAddress+2]
  17.     #that's all, we just changed in place the colours at FirstAddress and SecondAddresss
  20. FileStream = open(sys.argv[1], 'rb') #we are opening the file given on commandline parameter. rb stands for 'read binary'
  21. OriginalBuffer = #we are now reading FULL content of the file into OriginalBuffer array
  22. FileStream.close() #we don't need this file anymore locked as stream
  23. SecondaryWriteBuffer = list(OriginalBuffer) #we have to do a copy- swapping values on one buffer is problematic. It's simplier this way
  24. for i in range(0,ColoursToProcess):
  25.     replaceBytes(PaletteStartOriginal+ (i*3), PaletteStartReplace + (i*PaletteShift)) #i*3 because the first 0x33 is changing by 3, and i*48 because we replace every 15 colour
  26. FileWrite = open(sys.argv[1] + "_swapped", 'wb') #we appended a _swapped to filename and opened in WriteBinary mode
  27. FileWrite.write(''.join([str(i) for i in SecondaryWriteBuffer])) #here's the trick. Normally a string is immutable, so we cast it to list and then join an empty string with every element of the list
  28. FileWrite.close() #and we close the file so it's accesible outside the program
  29. exit() #good bye!
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand